Skip to content

Commit dfe362f

Browse files
committed
feature: add Local Branch Selector and Remote Branch Selector control type for custom actions (#2274)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 53cd847 commit dfe362f

7 files changed

Lines changed: 85 additions & 11 deletions

File tree

src/Models/CustomAction.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public enum CustomActionControlType
1919
PathSelector,
2020
CheckBox,
2121
ComboBox,
22+
LocalBranchSelector,
23+
RemoteBranchSelector,
2224
}
2325

2426
public record CustomActionTargetFile(string File, Commit Revision);

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@
280280
<x:String x:Key="Text.ConfigureCustomActionControls.Options.Tip" xml:space="preserve">Use '|' as delimiter for options</x:String>
281281
<x:String x:Key="Text.ConfigureCustomActionControls.StringValue.Tip" xml:space="preserve">The built-in variables ${REPO}, ${REMOTE}, ${BRANCH}, ${BRANCH_FRIENDLY_NAME}, ${SHA}, ${FILE}, and ${TAG} remain available here</x:String>
282282
<x:String x:Key="Text.ConfigureCustomActionControls.Type" xml:space="preserve">Type:</x:String>
283+
<x:String x:Key="Text.ConfigureCustomActionControls.UseFriendlyName" xml:space="preserve">Use Friendly Name:</x:String>
283284
<x:String x:Key="Text.ConfigureWorkspace" xml:space="preserve">Workspaces</x:String>
284285
<x:String x:Key="Text.ConfigureWorkspace.Color" xml:space="preserve">Color</x:String>
285286
<x:String x:Key="Text.ConfigureWorkspace.Name" xml:space="preserve">Name</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
<x:String x:Key="Text.ConfigureCustomActionControls.Options.Tip" xml:space="preserve">选项之间请使用英文 '|' 作为分隔符</x:String>
285285
<x:String x:Key="Text.ConfigureCustomActionControls.StringValue.Tip" xml:space="preserve">内置变量 ${REPO}, ${REMOTE}, ${BRANCH}, ${BRANCH_FRIENDLY_NAME}, ${SHA}, ${FILE} 与 ${TAG} 在这里仍然可用</x:String>
286286
<x:String x:Key="Text.ConfigureCustomActionControls.Type" xml:space="preserve">类型 :</x:String>
287+
<x:String x:Key="Text.ConfigureCustomActionControls.UseFriendlyName" xml:space="preserve">输出结果带有远程名:</x:String>
287288
<x:String x:Key="Text.ConfigureWorkspace" xml:space="preserve">工作区</x:String>
288289
<x:String x:Key="Text.ConfigureWorkspace.Color" xml:space="preserve">颜色</x:String>
289290
<x:String x:Key="Text.ConfigureWorkspace.Name" xml:space="preserve">名称</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
<x:String x:Key="Text.ConfigureCustomActionControls.Options.Tip" xml:space="preserve">請使用英文「|」符號分隔選項</x:String>
285285
<x:String x:Key="Text.ConfigureCustomActionControls.StringValue.Tip" xml:space="preserve">內建變數 ${REPO}、${REMOTE}、${BRANCH}、${BRANCH_FRIENDLY_NAME}、${SHA}、${FILE} 及 ${TAG} 在此處仍可使用</x:String>
286286
<x:String x:Key="Text.ConfigureCustomActionControls.Type" xml:space="preserve">類型:</x:String>
287+
<x:String x:Key="Text.ConfigureCustomActionControls.UseFriendlyName" xml:space="preserve">輸出包含遠端的名稱:</x:String>
287288
<x:String x:Key="Text.ConfigureWorkspace" xml:space="preserve">工作區</x:String>
288289
<x:String x:Key="Text.ConfigureWorkspace.Color" xml:space="preserve">顏色</x:String>
289290
<x:String x:Key="Text.ConfigureWorkspace.Name" xml:space="preserve">名稱</x:String>

src/ViewModels/ExecuteCustomAction.cs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,7 @@ public class CustomActionControlComboBox : ObservableObject, ICustomActionContro
7777
public string Label { get; set; }
7878
public string Description { get; set; }
7979
public List<string> Options { get; set; } = [];
80-
81-
public string Value
82-
{
83-
get => _value;
84-
set => SetProperty(ref _value, value);
85-
}
80+
public string Value { get; set; }
8681

8782
public CustomActionControlComboBox(string label, string description, string options)
8883
{
@@ -93,13 +88,45 @@ public CustomActionControlComboBox(string label, string description, string opti
9388
if (parts.Length > 0)
9489
{
9590
Options.AddRange(parts);
96-
_value = parts[0];
91+
Value = parts[0];
9792
}
9893
}
9994

100-
public string GetValue() => _value;
95+
public string GetValue() => Value;
96+
}
97+
98+
public class CustomActionControlBranchSelector : ObservableObject, ICustomActionControlParameter
99+
{
100+
public string Label { get; set; }
101+
public string Description { get; set; }
102+
public List<Models.Branch> Branches { get; set; } = [];
103+
public Models.Branch SelectedBranch { get; set; }
104+
105+
public CustomActionControlBranchSelector(string label, string description, Repository repo, bool isLocal, bool useFriendlyName)
106+
{
107+
Label = label;
108+
Description = description;
109+
_useFriendlyName = useFriendlyName;
101110

102-
private string _value = string.Empty;
111+
foreach (var b in repo.Branches)
112+
{
113+
if (b.IsLocal == isLocal && !b.IsDetachedHead)
114+
Branches.Add(b);
115+
}
116+
117+
if (Branches.Count > 0)
118+
SelectedBranch = Branches[0];
119+
}
120+
121+
public string GetValue()
122+
{
123+
if (SelectedBranch == null)
124+
return string.Empty;
125+
126+
return _useFriendlyName ? SelectedBranch.FriendlyName : SelectedBranch.Name;
127+
}
128+
129+
private bool _useFriendlyName = false;
103130
}
104131

105132
public class ExecuteCustomAction : Popup
@@ -171,6 +198,12 @@ private void PrepareControlParameters()
171198
case Models.CustomActionControlType.ComboBox:
172199
ControlParameters.Add(new CustomActionControlComboBox(ctl.Label, ctl.Description, PrepareStringByTarget(ctl.StringValue)));
173200
break;
201+
case Models.CustomActionControlType.LocalBranchSelector:
202+
ControlParameters.Add(new CustomActionControlBranchSelector(ctl.Label, ctl.Description, _repo, true, false));
203+
break;
204+
case Models.CustomActionControlType.RemoteBranchSelector:
205+
ControlParameters.Add(new CustomActionControlBranchSelector(ctl.Label, ctl.Description, _repo, false, ctl.BoolValue));
206+
break;
174207
}
175208
}
176209
}

src/Views/ConfigureCustomActionControls.axaml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@
129129
<TextBlock Margin="0,12,0,0" Text="{DynamicResource Text.ConfigureCustomActionControls.Type}"/>
130130
<ComboBox Margin="0,4,0,0" Height="28" HorizontalAlignment="Stretch" SelectedIndex="{Binding Type, Mode=TwoWay}">
131131
<ComboBoxItem Content="TextBox"/>
132-
<ComboBoxItem Content="PathSelector"/>
132+
<ComboBoxItem Content="Path Selector"/>
133133
<ComboBoxItem Content="CheckBox"/>
134134
<ComboBoxItem Content="ComboBox"/>
135+
<ComboBoxItem Content="Local Branch Selector"/>
136+
<ComboBoxItem Content="Remote Branch Selector"/>
135137
</ComboBox>
136138

137139
<!-- Description -->
@@ -160,7 +162,14 @@
160162
<TextBox Margin="0,4,0,0"
161163
CornerRadius="3"
162164
Height="28"
163-
Text="{Binding StringValue, Mode=TwoWay}"/>
165+
Text="{Binding StringValue, Mode=TwoWay}">
166+
<TextBox.IsVisible>
167+
<MultiBinding Converter="{x:Static BoolConverters.And}">
168+
<Binding Path="Type" Converter="{x:Static ObjectConverters.NotEqual}" ConverterParameter="{x:Static m:CustomActionControlType.LocalBranchSelector}"/>
169+
<Binding Path="Type" Converter="{x:Static ObjectConverters.NotEqual}" ConverterParameter="{x:Static m:CustomActionControlType.RemoteBranchSelector}"/>
170+
</MultiBinding>
171+
</TextBox.IsVisible>
172+
</TextBox>
164173
<TextBlock Margin="0,2,0,0"
165174
Classes="small"
166175
TextWrapping="Wrap"
@@ -193,6 +202,7 @@
193202
<MultiBinding Converter="{x:Static BoolConverters.Or}">
194203
<Binding Path="Type" Converter="{x:Static ObjectConverters.Equal}" ConverterParameter="{x:Static m:CustomActionControlType.CheckBox}"/>
195204
<Binding Path="Type" Converter="{x:Static ObjectConverters.Equal}" ConverterParameter="{x:Static m:CustomActionControlType.PathSelector}"/>
205+
<Binding Path="Type" Converter="{x:Static ObjectConverters.Equal}" ConverterParameter="{x:Static m:CustomActionControlType.RemoteBranchSelector}"/>
196206
</MultiBinding>
197207
</Grid.IsVisible>
198208
<TextBlock Grid.Column="0"
@@ -201,6 +211,9 @@
201211
<TextBlock Grid.Column="0"
202212
Text="{DynamicResource Text.ConfigureCustomActionControls.IsFolder}"
203213
IsVisible="{Binding Type, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:CustomActionControlType.PathSelector}}"/>
214+
<TextBlock Grid.Column="0"
215+
Text="{DynamicResource Text.ConfigureCustomActionControls.UseFriendlyName}"
216+
IsVisible="{Binding Type, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:CustomActionControlType.RemoteBranchSelector}}"/>
204217
<CheckBox Grid.Column="1"
205218
Margin="8,0,0,0"
206219
Height="28"

src/Views/ExecuteCustomAction.axaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:m="using:SourceGit.Models"
66
xmlns:vm="using:SourceGit.ViewModels"
7+
xmlns:v="using:SourceGit.Views"
78
xmlns:c="using:SourceGit.Converters"
89
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450"
910
x:Class="SourceGit.Views.ExecuteCustomAction"
@@ -140,6 +141,28 @@
140141
IsVisible="{Binding Description, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"/>
141142
</Grid>
142143
</DataTemplate>
144+
145+
<DataTemplate DataType="vm:CustomActionControlBranchSelector">
146+
<Grid RowDefinitions="32,Auto" ColumnDefinitions="150,*">
147+
<TextBlock Grid.Row="0" Grid.Column="0"
148+
Text="{Binding Label}"
149+
HorizontalAlignment="Right" VerticalAlignment="Center"
150+
Margin="0,0,8,0"/>
151+
<v:BranchSelector Grid.Row="0" Grid.Column="1"
152+
Height="28" Padding="8,0"
153+
VerticalAlignment="Center" HorizontalAlignment="Stretch"
154+
Branches="{Binding Branches, Mode=OneWay}"
155+
SelectedBranch="{Binding SelectedBranch, Mode=TwoWay}"/>
156+
157+
<TextBlock Grid.Row="1" Grid.Column="1"
158+
Classes="small"
159+
Margin="0,2"
160+
TextWrapping="Wrap"
161+
Text="{Binding Description}"
162+
Foreground="{DynamicResource Brush.FG2}"
163+
IsVisible="{Binding Description, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"/>
164+
</Grid>
165+
</DataTemplate>
143166
</ItemsControl.DataTemplates>
144167
</ItemsControl>
145168
</StackPanel>

0 commit comments

Comments
 (0)