Skip to content

Commit 4d48d1f

Browse files
committed
feature: allows to edit branch's description (branch.<name>.description) and show it in branch's tooltip (#1920)
Signed-off-by: leo <[email protected]>
1 parent 5975890 commit 4d48d1f

File tree

8 files changed

+173
-0
lines changed

8 files changed

+173
-0
lines changed

src/Resources/Locales/en_US.axaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<x:String x:Key="Text.BranchCM.CustomAction" xml:space="preserve">Custom Action</x:String>
6464
<x:String x:Key="Text.BranchCM.Delete" xml:space="preserve">Delete ${0}$...</x:String>
6565
<x:String x:Key="Text.BranchCM.DeleteMultiBranches" xml:space="preserve">Delete selected {0} branches</x:String>
66+
<x:String x:Key="Text.BranchCM.EditDescription" xml:space="preserve">Edit description for ${0}$...</x:String>
6667
<x:String x:Key="Text.BranchCM.FastForward" xml:space="preserve">Fast-Forward to ${0}$</x:String>
6768
<x:String x:Key="Text.BranchCM.FetchInto" xml:space="preserve">Fetch ${0}$ into ${1}$...</x:String>
6869
<x:String x:Key="Text.BranchCM.Finish" xml:space="preserve">Git Flow - Finish ${0}$</x:String>
@@ -371,6 +372,8 @@
371372
<x:String x:Key="Text.DropHead" xml:space="preserve">Drop Commit</x:String>
372373
<x:String x:Key="Text.DropHead.Commit" xml:space="preserve">Commit:</x:String>
373374
<x:String x:Key="Text.DropHead.NewHead" xml:space="preserve">New HEAD:</x:String>
375+
<x:String x:Key="Text.EditBranchDescription" xml:space="preserve">Edit Branch's Description</x:String>
376+
<x:String x:Key="Text.EditBranchDescription.Target" xml:space="preserve">Target:</x:String>
374377
<x:String x:Key="Text.EditRepositoryNode.Bookmark" xml:space="preserve">Bookmark:</x:String>
375378
<x:String x:Key="Text.EditRepositoryNode.Name" xml:space="preserve">New Name:</x:String>
376379
<x:String x:Key="Text.EditRepositoryNode.Target" xml:space="preserve">Target:</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<x:String x:Key="Text.BranchCM.CustomAction" xml:space="preserve">自定义操作</x:String>
6868
<x:String x:Key="Text.BranchCM.Delete" xml:space="preserve">删除 ${0}$...</x:String>
6969
<x:String x:Key="Text.BranchCM.DeleteMultiBranches" xml:space="preserve">删除选中的 {0} 个分支</x:String>
70+
<x:String x:Key="Text.BranchCM.EditDescription" xml:space="preserve">编辑 ${0}$ 的描述...</x:String>
7071
<x:String x:Key="Text.BranchCM.FastForward" xml:space="preserve">快进(fast-forward)到 ${0}$</x:String>
7172
<x:String x:Key="Text.BranchCM.FetchInto" xml:space="preserve">拉取(fetch) ${0}$ 至 ${1}$...</x:String>
7273
<x:String x:Key="Text.BranchCM.Finish" xml:space="preserve">GIT工作流 - 完成 ${0}$</x:String>
@@ -375,6 +376,8 @@
375376
<x:String x:Key="Text.DropHead" xml:space="preserve">丢弃提交</x:String>
376377
<x:String x:Key="Text.DropHead.Commit" xml:space="preserve">提交 :</x:String>
377378
<x:String x:Key="Text.DropHead.NewHead" xml:space="preserve">丢弃后 HEAD :</x:String>
379+
<x:String x:Key="Text.EditBranchDescription" xml:space="preserve">编辑分支描述</x:String>
380+
<x:String x:Key="Text.EditBranchDescription.Target" xml:space="preserve">目标 :</x:String>
378381
<x:String x:Key="Text.EditRepositoryNode.Bookmark" xml:space="preserve">书签 :</x:String>
379382
<x:String x:Key="Text.EditRepositoryNode.Name" xml:space="preserve">名称 :</x:String>
380383
<x:String x:Key="Text.EditRepositoryNode.Target" xml:space="preserve">目标 :</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<x:String x:Key="Text.BranchCM.CustomAction" xml:space="preserve">自訂動作</x:String>
6868
<x:String x:Key="Text.BranchCM.Delete" xml:space="preserve">刪除 ${0}$...</x:String>
6969
<x:String x:Key="Text.BranchCM.DeleteMultiBranches" xml:space="preserve">刪除所選的 {0} 個分支</x:String>
70+
<x:String x:Key="Text.BranchCM.EditDescription" xml:space="preserve">編輯 ${0}$ 的描述...</x:String>
7071
<x:String x:Key="Text.BranchCM.FastForward" xml:space="preserve">快轉 (fast-forward) 到 ${0}$</x:String>
7172
<x:String x:Key="Text.BranchCM.FetchInto" xml:space="preserve">提取 (fetch) ${0}$ 到 ${1}$...</x:String>
7273
<x:String x:Key="Text.BranchCM.Finish" xml:space="preserve">Git 工作流 - 完成 ${0}$</x:String>
@@ -375,6 +376,8 @@
375376
<x:String x:Key="Text.DropHead" xml:space="preserve">捨棄提交</x:String>
376377
<x:String x:Key="Text.DropHead.Commit" xml:space="preserve">提交:</x:String>
377378
<x:String x:Key="Text.DropHead.NewHead" xml:space="preserve">捨棄後新的 HEAD:</x:String>
379+
<x:String x:Key="Text.EditBranchDescription" xml:space="preserve">編輯分支的描述</x:String>
380+
<x:String x:Key="Text.EditBranchDescription.Target" xml:space="preserve">目標:</x:String>
378381
<x:String x:Key="Text.EditRepositoryNode.Bookmark" xml:space="preserve">書籤:</x:String>
379382
<x:String x:Key="Text.EditRepositoryNode.Name" xml:space="preserve">名稱:</x:String>
380383
<x:String x:Key="Text.EditRepositoryNode.Target" xml:space="preserve">目標:</x:String>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace SourceGit.ViewModels
5+
{
6+
public class EditBranchDescription : Popup
7+
{
8+
public Models.Branch Target
9+
{
10+
get;
11+
}
12+
13+
public string Description
14+
{
15+
get => _description;
16+
set => SetProperty(ref _description, value);
17+
}
18+
19+
public EditBranchDescription(Repository repo, Models.Branch target, string desc)
20+
{
21+
Target = target;
22+
23+
_repo = repo;
24+
_originalDescription = desc;
25+
_description = desc;
26+
}
27+
28+
public override async Task<bool> Sure()
29+
{
30+
var trimmed = _description.Trim();
31+
if (string.IsNullOrEmpty(trimmed))
32+
{
33+
if (string.IsNullOrEmpty(_originalDescription))
34+
return true;
35+
}
36+
else if (trimmed.Equals(_originalDescription, StringComparison.Ordinal))
37+
{
38+
return true;
39+
}
40+
41+
var log = _repo.CreateLog("Edit Branch Description");
42+
Use(log);
43+
44+
await new Commands.Config(_repo.FullPath)
45+
.Use(log)
46+
.SetAsync($"branch.{Target.Name}.description", trimmed);
47+
48+
log.Complete();
49+
return true;
50+
}
51+
52+
private readonly Repository _repo;
53+
private string _originalDescription = string.Empty;
54+
private string _description = string.Empty;
55+
}
56+
}

src/Views/BranchTree.axaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@
8989
Text="{Binding WorktreePath, Mode=OneWay}"
9090
IsVisible="{Binding HasWorktree}"/>
9191
</Grid>
92+
93+
<v:BranchTreeNodeDescription Margin="0,8,0,0"/>
9294
</StackPanel>
9395
</DataTemplate>
9496

src/Views/BranchTree.axaml.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,45 @@ protected override void OnDataContextChanged(EventArgs e)
229229
}
230230
}
231231

232+
public class BranchTreeNodeDescription : TextBlock
233+
{
234+
protected override Type StyleKeyOverride => typeof(TextBlock);
235+
236+
public BranchTreeNodeDescription()
237+
{
238+
IsVisible = false;
239+
}
240+
241+
protected override async void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
242+
{
243+
base.OnAttachedToVisualTree(e);
244+
245+
var visible = false;
246+
247+
do
248+
{
249+
if (DataContext is not Models.Branch branch)
250+
break;
251+
252+
if (e.Root is not PopupRoot { Parent: Popup { Parent: Border owner } })
253+
break;
254+
255+
var tree = owner.FindAncestorOfType<BranchTree>();
256+
if (tree is not { DataContext: ViewModels.Repository repo })
257+
break;
258+
259+
var description = await new Commands.Config(repo.FullPath).GetAsync($"branch.{branch.Name}.description");
260+
if (string.IsNullOrEmpty(description))
261+
break;
262+
263+
Text = description;
264+
visible = true;
265+
} while (false);
266+
267+
SetCurrentValue(IsVisibleProperty, visible);
268+
}
269+
}
270+
232271
public partial class BranchTree : UserControl
233272
{
234273
public static readonly StyledProperty<List<ViewModels.BranchTreeNode>> NodesProperty =
@@ -805,6 +844,17 @@ private ContextMenu CreateContextMenuForLocalBranch(ViewModels.Repository repo,
805844
e.Handled = true;
806845
};
807846

847+
var editDescription = new MenuItem();
848+
editDescription.Header = App.Text("BranchCM.EditDescription", branch.Name);
849+
editDescription.Icon = App.CreateMenuIcon("Icons.Edit");
850+
editDescription.Click += async (_, e) =>
851+
{
852+
var desc = await new Commands.Config(repo.FullPath).GetAsync($"branch.{branch.Name}.description");
853+
if (repo.CanCreatePopup())
854+
repo.ShowPopup(new ViewModels.EditBranchDescription(repo, branch, desc));
855+
e.Handled = true;
856+
};
857+
808858
var delete = new MenuItem();
809859
delete.Header = App.Text("BranchCM.Delete", branch.Name);
810860
delete.Icon = App.CreateMenuIcon("Icons.Clear");
@@ -837,6 +887,7 @@ private ContextMenu CreateContextMenuForLocalBranch(ViewModels.Repository repo,
837887
};
838888

839889
menu.Items.Add(new MenuItem() { Header = "-" });
890+
menu.Items.Add(editDescription);
840891
menu.Items.Add(rename);
841892
menu.Items.Add(delete);
842893
menu.Items.Add(new MenuItem() { Header = "-" });
@@ -1068,6 +1119,17 @@ public ContextMenu CreateContextMenuForRemoteBranch(ViewModels.Repository repo,
10681119
}
10691120
menu.Items.Add(new MenuItem() { Header = "-" });
10701121

1122+
var editDescription = new MenuItem();
1123+
editDescription.Header = App.Text("BranchCM.EditDescription", branch.Name);
1124+
editDescription.Icon = App.CreateMenuIcon("Icons.Edit");
1125+
editDescription.Click += async (_, e) =>
1126+
{
1127+
var desc = await new Commands.Config(repo.FullPath).GetAsync($"branch.{branch.Name}.description");
1128+
if (repo.CanCreatePopup())
1129+
repo.ShowPopup(new ViewModels.EditBranchDescription(repo, branch, desc));
1130+
e.Handled = true;
1131+
};
1132+
10711133
var delete = new MenuItem();
10721134
delete.Header = App.Text("BranchCM.Delete", name);
10731135
delete.Icon = App.CreateMenuIcon("Icons.Clear");
@@ -1078,6 +1140,7 @@ public ContextMenu CreateContextMenuForRemoteBranch(ViewModels.Repository repo,
10781140
e.Handled = true;
10791141
};
10801142

1143+
menu.Items.Add(editDescription);
10811144
menu.Items.Add(delete);
10821145
menu.Items.Add(new MenuItem() { Header = "-" });
10831146

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<UserControl xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:vm="using:SourceGit.ViewModels"
6+
xmlns:v="using:SourceGit.Views"
7+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
8+
x:Class="SourceGit.Views.EditBranchDescription"
9+
x:DataType="vm:EditBranchDescription">
10+
<StackPanel Orientation="Vertical" Margin="8,0">
11+
<TextBlock FontSize="18"
12+
Classes="bold"
13+
Text="{DynamicResource Text.EditBranchDescription}"/>
14+
15+
<StackPanel Margin="8,16,8,0" Orientation="Horizontal">
16+
<TextBlock Text="{DynamicResource Text.EditBranchDescription.Target}"/>
17+
<Path Margin="8,0" Width="14" Height="14" Data="{StaticResource Icons.Branch}"/>
18+
<TextBlock Text="{Binding Target.FriendlyName}"/>
19+
</StackPanel>
20+
21+
<TextBox Height="120"
22+
Margin="8,8,8,0" Padding="2,4"
23+
CornerRadius="4"
24+
VerticalContentAlignment="Top"
25+
AcceptsReturn="True" AcceptsTab="False"
26+
Text="{Binding Description, Mode=TwoWay}"
27+
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
28+
ScrollViewer.VerticalScrollBarVisibility="Auto"
29+
Tag="{Binding Source={x:Static v:StealHotKey.Enter}}"/>
30+
</StackPanel>
31+
</UserControl>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Avalonia.Controls;
2+
3+
namespace SourceGit.Views
4+
{
5+
public partial class EditBranchDescription : UserControl
6+
{
7+
public EditBranchDescription()
8+
{
9+
InitializeComponent();
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)