From 1149c768d3a3946e853cb858474076cfc1dff16e Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 28 Feb 2024 18:55:23 +0800 Subject: [PATCH] refactor: use NameHighlightedTextBlock's constructor instead of functions in each caller --- src/ViewModels/Histories.cs | 41 +++++++++++---------------- src/ViewModels/Repository.cs | 41 +++++++++++---------------- src/Views/NameHighlightedTextBlock.cs | 5 ++++ 3 files changed, 39 insertions(+), 48 deletions(-) diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index 74c59f0c..d945368e 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -146,7 +146,7 @@ namespace SourceGit.ViewModels { if (current.Head != commit.SHA) { var reset = new MenuItem(); - reset.Header = CreateHighlightLabel("CommitCM.Reset", current.Name); + reset.Header = new Views.NameHighlightedTextBlock("CommitCM.Reset", current.Name); reset.Icon = App.CreateMenuIcon("Icons.Reset"); reset.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new Reset(_repo, current, commit)); @@ -180,7 +180,7 @@ namespace SourceGit.ViewModels { if (!commit.IsMerged) { var rebase = new MenuItem(); - rebase.Header = CreateHighlightLabel("CommitCM.Rebase", current.Name); + rebase.Header = new Views.NameHighlightedTextBlock("CommitCM.Rebase", current.Name); rebase.Icon = App.CreateMenuIcon("Icons.Rebase"); rebase.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new Rebase(_repo, current, commit)); @@ -277,7 +277,7 @@ namespace SourceGit.ViewModels { var upstream = current.Upstream.Substring(13); var fastForward = new MenuItem(); - fastForward.Header = CreateHighlightLabel("BranchCM.FastForward", upstream); + fastForward.Header = new Views.NameHighlightedTextBlock("BranchCM.FastForward", upstream); fastForward.Icon = App.CreateMenuIcon("Icons.FastForward"); fastForward.IsEnabled = dirty; fastForward.Click += (o, e) => { @@ -287,7 +287,7 @@ namespace SourceGit.ViewModels { submenu.Items.Add(fastForward); var pull = new MenuItem(); - pull.Header = CreateHighlightLabel("BranchCM.Pull", upstream); + pull.Header = new Views.NameHighlightedTextBlock("BranchCM.Pull", upstream); pull.Icon = App.CreateMenuIcon("Icons.Pull"); pull.IsEnabled = dirty; pull.Click += (o, e) => { @@ -298,7 +298,7 @@ namespace SourceGit.ViewModels { } var push = new MenuItem(); - push.Header = CreateHighlightLabel("BranchCM.Push", current.Name); + push.Header = new Views.NameHighlightedTextBlock("BranchCM.Push", current.Name); push.Icon = App.CreateMenuIcon("Icons.Push"); push.IsEnabled = _repo.Remotes.Count > 0 && dirty; push.Click += (o, e) => { @@ -311,7 +311,7 @@ namespace SourceGit.ViewModels { var type = _repo.GitFlow.GetBranchType(current.Name); if (type != Models.GitFlowBranchType.None) { var finish = new MenuItem(); - finish.Header = CreateHighlightLabel("BranchCM.Finish", current.Name); + finish.Header = new Views.NameHighlightedTextBlock("BranchCM.Finish", current.Name); finish.Icon = App.CreateMenuIcon("Icons.Flow"); finish.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new GitFlowFinish(_repo, current, type)); @@ -322,7 +322,7 @@ namespace SourceGit.ViewModels { } var rename = new MenuItem(); - rename.Header = CreateHighlightLabel("BranchCM.Rename", current.Name); + rename.Header = new Views.NameHighlightedTextBlock("BranchCM.Rename", current.Name); rename.Icon = App.CreateMenuIcon("Icons.Rename"); rename.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new RenameBranch(_repo, current)); @@ -339,7 +339,7 @@ namespace SourceGit.ViewModels { submenu.Header = branch.Name; var checkout = new MenuItem(); - checkout.Header = CreateHighlightLabel("BranchCM.Checkout", branch.Name); + checkout.Header = new Views.NameHighlightedTextBlock("BranchCM.Checkout", branch.Name); checkout.Icon = App.CreateMenuIcon("Icons.Check"); checkout.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowAndStartPopup(new Checkout(_repo, branch.Name)); @@ -348,7 +348,7 @@ namespace SourceGit.ViewModels { submenu.Items.Add(checkout); var merge = new MenuItem(); - merge.Header = CreateHighlightLabel("BranchCM.Merge", branch.Name, current.Name); + merge.Header = new Views.NameHighlightedTextBlock("BranchCM.Merge", branch.Name, current.Name); merge.Icon = App.CreateMenuIcon("Icons.Merge"); merge.IsEnabled = !merged; merge.Click += (o, e) => { @@ -361,7 +361,7 @@ namespace SourceGit.ViewModels { var type = _repo.GitFlow.GetBranchType(branch.Name); if (type != Models.GitFlowBranchType.None) { var finish = new MenuItem(); - finish.Header = CreateHighlightLabel("BranchCM.Finish", branch.Name); + finish.Header = new Views.NameHighlightedTextBlock("BranchCM.Finish", branch.Name); finish.Icon = App.CreateMenuIcon("Icons.Flow"); finish.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new GitFlowFinish(_repo, branch, type)); @@ -372,7 +372,7 @@ namespace SourceGit.ViewModels { } var rename = new MenuItem(); - rename.Header = CreateHighlightLabel("BranchCM.Rename", branch.Name); + rename.Header = new Views.NameHighlightedTextBlock("BranchCM.Rename", branch.Name); rename.Icon = App.CreateMenuIcon("Icons.Rename"); rename.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new RenameBranch(_repo, branch)); @@ -381,7 +381,7 @@ namespace SourceGit.ViewModels { submenu.Items.Add(rename); var delete = new MenuItem(); - delete.Header = CreateHighlightLabel("BranchCM.Delete", branch.Name); + delete.Header = new Views.NameHighlightedTextBlock("BranchCM.Delete", branch.Name); delete.Icon = App.CreateMenuIcon("Icons.Clear"); delete.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new DeleteBranch(_repo, branch)); @@ -400,7 +400,7 @@ namespace SourceGit.ViewModels { submenu.Header = name; var checkout = new MenuItem(); - checkout.Header = CreateHighlightLabel("BranchCM.Checkout", name); + checkout.Header = new Views.NameHighlightedTextBlock("BranchCM.Checkout", name); checkout.Icon = App.CreateMenuIcon("Icons.Check"); checkout.Click += (o, e) => { foreach (var b in _repo.Branches) { @@ -417,7 +417,7 @@ namespace SourceGit.ViewModels { submenu.Items.Add(checkout); var merge = new MenuItem(); - merge.Header = CreateHighlightLabel("BranchCM.Merge", name, current.Name); + merge.Header = new Views.NameHighlightedTextBlock("BranchCM.Merge", name, current.Name); merge.Icon = App.CreateMenuIcon("Icons.Merge"); merge.IsEnabled = !merged; merge.Click += (o, e) => { @@ -429,7 +429,7 @@ namespace SourceGit.ViewModels { submenu.Items.Add(new MenuItem() { Header = "-" }); var delete = new MenuItem(); - delete.Header = CreateHighlightLabel("BranchCM.Delete", name); + delete.Header = new Views.NameHighlightedTextBlock("BranchCM.Delete", name); delete.Icon = App.CreateMenuIcon("Icons.Clear"); delete.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new DeleteBranch(_repo, branch)); @@ -447,7 +447,7 @@ namespace SourceGit.ViewModels { submenu.MinWidth = 200; var push = new MenuItem(); - push.Header = CreateHighlightLabel("TagCM.Push", tag.Name); + push.Header = new Views.NameHighlightedTextBlock("TagCM.Push", tag.Name); push.Icon = App.CreateMenuIcon("Icons.Push"); push.IsEnabled = _repo.Remotes.Count > 0; push.Click += (o, e) => { @@ -457,7 +457,7 @@ namespace SourceGit.ViewModels { submenu.Items.Add(push); var delete = new MenuItem(); - delete.Header = CreateHighlightLabel("TagCM.Delete", tag.Name); + delete.Header = new Views.NameHighlightedTextBlock("TagCM.Delete", tag.Name); delete.Icon = App.CreateMenuIcon("Icons.Clear"); delete.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new DeleteTag(_repo, tag)); @@ -468,13 +468,6 @@ namespace SourceGit.ViewModels { menu.Items.Add(submenu); } - private object CreateHighlightLabel(string key, params object[] args) { - var label = new Views.NameHighlightedTextBlock(); - label.Text = App.Text(key, args); - label.VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center; - return label; - } - private Repository _repo = null; private double _dataGridRowHeight = 28; private bool _isLoading = true; diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index b4a5b581..af22366f 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -578,7 +578,7 @@ namespace SourceGit.ViewModels { var menu = new ContextMenu(); var push = new MenuItem(); - push.Header = CreateHighlightLabel("BranchCM.Push", branch.Name); + push.Header = new Views.NameHighlightedTextBlock("BranchCM.Push", branch.Name); push.Icon = App.CreateMenuIcon("Icons.Push"); push.IsEnabled = Remotes.Count > 0; push.Click += (_, e) => { @@ -601,7 +601,7 @@ namespace SourceGit.ViewModels { if (!string.IsNullOrEmpty(branch.Upstream)) { var upstream = branch.Upstream.Substring(13); var fastForward = new MenuItem(); - fastForward.Header = CreateHighlightLabel("BranchCM.FastForward", upstream); + fastForward.Header = new Views.NameHighlightedTextBlock("BranchCM.FastForward", upstream); fastForward.Icon = App.CreateMenuIcon("Icons.FastForward"); fastForward.IsEnabled = !string.IsNullOrEmpty(branch.UpstreamTrackStatus) && branch.UpstreamTrackStatus.IndexOf('↑') < 0; fastForward.Click += (o, e) => { @@ -610,7 +610,7 @@ namespace SourceGit.ViewModels { }; var pull = new MenuItem(); - pull.Header = CreateHighlightLabel("BranchCM.Pull", upstream); + pull.Header = new Views.NameHighlightedTextBlock("BranchCM.Pull", upstream); pull.Icon = App.CreateMenuIcon("Icons.Pull"); pull.IsEnabled = !string.IsNullOrEmpty(branch.UpstreamTrackStatus); pull.Click += (o, e) => { @@ -627,7 +627,7 @@ namespace SourceGit.ViewModels { var current = Branches.Find(x => x.IsCurrent); var checkout = new MenuItem(); - checkout.Header = CreateHighlightLabel("BranchCM.Checkout", branch.Name); + checkout.Header = new Views.NameHighlightedTextBlock("BranchCM.Checkout", branch.Name); checkout.Icon = App.CreateMenuIcon("Icons.Check"); checkout.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowAndStartPopup(new Checkout(this, branch.Name)); @@ -638,7 +638,7 @@ namespace SourceGit.ViewModels { var upstream = Branches.Find(x => x.FullName == branch.Upstream); if (upstream != null) { var fastForward = new MenuItem(); - fastForward.Header = CreateHighlightLabel("BranchCM.FastForward", $"{upstream.Remote}/{upstream.Name}"); + fastForward.Header = new Views.NameHighlightedTextBlock("BranchCM.FastForward", $"{upstream.Remote}/{upstream.Name}"); fastForward.Icon = App.CreateMenuIcon("Icons.FastForward"); fastForward.IsEnabled = !string.IsNullOrEmpty(branch.UpstreamTrackStatus) && branch.UpstreamTrackStatus.IndexOf('↑') < 0; fastForward.Click += (o, e) => { @@ -654,7 +654,7 @@ namespace SourceGit.ViewModels { menu.Items.Add(push); var merge = new MenuItem(); - merge.Header = CreateHighlightLabel("BranchCM.Merge", branch.Name, current.Name); + merge.Header = new Views.NameHighlightedTextBlock("BranchCM.Merge", branch.Name, current.Name); merge.Icon = App.CreateMenuIcon("Icons.Merge"); merge.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new Merge(this, branch.Name, current.Name)); @@ -662,7 +662,7 @@ namespace SourceGit.ViewModels { }; var rebase = new MenuItem(); - rebase.Header = CreateHighlightLabel("BranchCM.Rebase", current.Name, branch.Name); + rebase.Header = new Views.NameHighlightedTextBlock("BranchCM.Rebase", current.Name, branch.Name); rebase.Icon = App.CreateMenuIcon("Icons.Rebase"); rebase.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new Rebase(this, current, branch)); @@ -676,7 +676,7 @@ namespace SourceGit.ViewModels { var type = GitFlow.GetBranchType(branch.Name); if (type != Models.GitFlowBranchType.None) { var finish = new MenuItem(); - finish.Header = CreateHighlightLabel("BranchCM.Finish", branch.Name); + finish.Header = new Views.NameHighlightedTextBlock("BranchCM.Finish", branch.Name); finish.Icon = App.CreateMenuIcon("Icons.Flow"); finish.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new GitFlowFinish(this, branch, type)); @@ -687,7 +687,7 @@ namespace SourceGit.ViewModels { } var rename = new MenuItem(); - rename.Header = CreateHighlightLabel("BranchCM.Rename", branch.Name); + rename.Header = new Views.NameHighlightedTextBlock("BranchCM.Rename", branch.Name); rename.Icon = App.CreateMenuIcon("Icons.Rename"); rename.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new RenameBranch(this, branch)); @@ -695,7 +695,7 @@ namespace SourceGit.ViewModels { }; var delete = new MenuItem(); - delete.Header = CreateHighlightLabel("BranchCM.Delete", branch.Name); + delete.Header = new Views.NameHighlightedTextBlock("BranchCM.Delete", branch.Name); delete.Icon = App.CreateMenuIcon("Icons.Clear"); delete.IsEnabled = !branch.IsCurrent; delete.Click += (o, e) => { @@ -847,7 +847,7 @@ namespace SourceGit.ViewModels { var current = Branches.Find(x => x.IsCurrent); var checkout = new MenuItem(); - checkout.Header = CreateHighlightLabel("BranchCM.Checkout", $"{branch.Remote}/{branch.Name}"); + checkout.Header = new Views.NameHighlightedTextBlock("BranchCM.Checkout", $"{branch.Remote}/{branch.Name}"); checkout.Icon = App.CreateMenuIcon("Icons.Check"); checkout.Click += (o, e) => { foreach (var b in Branches) { @@ -866,7 +866,7 @@ namespace SourceGit.ViewModels { if (current != null) { var pull = new MenuItem(); - pull.Header = CreateHighlightLabel("BranchCM.PullInto", $"{branch.Remote}/{branch.Name}", current.Name); + pull.Header = new Views.NameHighlightedTextBlock("BranchCM.PullInto", $"{branch.Remote}/{branch.Name}", current.Name); pull.Icon = App.CreateMenuIcon("Icons.Pull"); pull.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new Pull(this, branch)); @@ -874,7 +874,7 @@ namespace SourceGit.ViewModels { }; var merge = new MenuItem(); - merge.Header = CreateHighlightLabel("BranchCM.Merge", $"{branch.Remote}/{branch.Name}", current.Name); + merge.Header = new Views.NameHighlightedTextBlock("BranchCM.Merge", $"{branch.Remote}/{branch.Name}", current.Name); merge.Icon = App.CreateMenuIcon("Icons.Merge"); merge.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new Merge(this, $"{branch.Remote}/{branch.Name}", current.Name)); @@ -882,7 +882,7 @@ namespace SourceGit.ViewModels { }; var rebase = new MenuItem(); - rebase.Header = CreateHighlightLabel("BranchCM.Rebase", current.Name, $"{branch.Remote}/{branch.Name}"); + rebase.Header = new Views.NameHighlightedTextBlock("BranchCM.Rebase", current.Name, $"{branch.Remote}/{branch.Name}"); rebase.Icon = App.CreateMenuIcon("Icons.Rebase"); rebase.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new Rebase(this, current, branch)); @@ -896,7 +896,7 @@ namespace SourceGit.ViewModels { } var delete = new MenuItem(); - delete.Header = CreateHighlightLabel("BranchCM.Delete", $"{branch.Remote}/{branch.Name}"); + delete.Header = new Views.NameHighlightedTextBlock("BranchCM.Delete", $"{branch.Remote}/{branch.Name}"); delete.Icon = App.CreateMenuIcon("Icons.Clear"); delete.Click += (o, e) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new DeleteBranch(this, branch)); @@ -956,7 +956,7 @@ namespace SourceGit.ViewModels { }; var pushTag = new MenuItem(); - pushTag.Header = CreateHighlightLabel("TagCM.Push", tag.Name); + pushTag.Header = new Views.NameHighlightedTextBlock("TagCM.Push", tag.Name); pushTag.Icon = App.CreateMenuIcon("Icons.Push"); pushTag.IsEnabled = Remotes.Count > 0; pushTag.Click += (o, ev) => { @@ -965,7 +965,7 @@ namespace SourceGit.ViewModels { }; var deleteTag = new MenuItem(); - deleteTag.Header = CreateHighlightLabel("TagCM.Delete", tag.Name); + deleteTag.Header = new Views.NameHighlightedTextBlock("TagCM.Delete", tag.Name); deleteTag.Icon = App.CreateMenuIcon("Icons.Clear"); deleteTag.Click += (o, ev) => { if (PopupHost.CanCreatePopup()) PopupHost.ShowPopup(new DeleteTag(this, tag)); @@ -1046,13 +1046,6 @@ namespace SourceGit.ViewModels { return menu; } - private object CreateHighlightLabel(string key, params object[] args) { - var label = new Views.NameHighlightedTextBlock(); - label.Text = App.Text(key, args); - label.VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center; - return label; - } - private string _fullpath = string.Empty; private string _gitDir = string.Empty; private Models.GitFlow _gitflow = new Models.GitFlow(); diff --git a/src/Views/NameHighlightedTextBlock.cs b/src/Views/NameHighlightedTextBlock.cs index 625c571b..c6473116 100644 --- a/src/Views/NameHighlightedTextBlock.cs +++ b/src/Views/NameHighlightedTextBlock.cs @@ -43,6 +43,11 @@ namespace SourceGit.Views { AffectsMeasure(TextProperty); } + public NameHighlightedTextBlock(string nameKey, params object[] args) { + Text = App.Text(nameKey, args); + VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center; + } + protected override Size MeasureOverride(Size availableSize) { var text = Text; if (string.IsNullOrEmpty(text)) return base.MeasureOverride(availableSize);