Skip to content

Commit e720d9f

Browse files
committed
feature: allow searching for remote branches in pull/push (#2283)
Signed-off-by: leo <longshuang@msn.cn>
1 parent ca1ad2b commit e720d9f

4 files changed

Lines changed: 114 additions & 60 deletions

File tree

src/Views/BranchSelector.axaml

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
xmlns:m="using:SourceGit.Models"
66
xmlns:v="using:SourceGit.Views"
77
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450"
8-
x:Class="SourceGit.Views.BranchSelector">
8+
x:Class="SourceGit.Views.BranchSelector"
9+
x:Name="ThisControl">
910
<UserControl.Styles>
1011
<Style Selector="v|BranchSelector">
1112
<Setter Property="FocusAdorner">
@@ -25,25 +26,38 @@
2526

2627
<Grid x:Name="PART_Selected"
2728
Background="Transparent"
28-
ColumnDefinitions="Auto,*,32"
29+
ColumnDefinitions="*,32"
2930
PointerPressed="OnToggleDropDown">
30-
<Path Grid.Column="0"
31-
Margin="8,0,0,0"
32-
Width="14" Height="14"
33-
Data="{StaticResource Icons.Branch}"
34-
IsHitTestVisible="False"/>
35-
36-
<ContentControl Grid.Column="1"
31+
<ContentControl Grid.Column="0"
3732
Margin="8,0,0,0"
38-
Content="{TemplateBinding SelectedBranch, Mode=OneWay}">
33+
IsHitTestVisible="False"
34+
Content="{TemplateBinding SelectedBranch}">
3935
<ContentControl.DataTemplates>
4036
<DataTemplate DataType="m:Branch">
41-
<TextBlock Text="{Binding FriendlyName, Mode=OneWay}" VerticalAlignment="Center"/>
37+
<StackPanel Orientation="Horizontal">
38+
<Path Width="14" Height="14"
39+
Data="{StaticResource Icons.Branch}"
40+
Fill="{DynamicResource Brush.FG1}"/>
41+
42+
<v:BranchSelectorChoice Margin="8,0,0,0"
43+
VerticalAlignment="Center"
44+
Branch="{Binding Mode=OneWay}"
45+
UsePureName="{Binding #ThisControl.UsePureName, Mode=OneWay}"/>
46+
47+
<Border Height="14"
48+
CornerRadius="7"
49+
Margin="4,0,0,0" Padding="6,0"
50+
VerticalAlignment="Center"
51+
Background="Green"
52+
IsVisible="{Binding Head, Converter={x:Static StringConverters.IsNullOrEmpty}}">
53+
<TextBlock Text="{DynamicResource Text.Push.New}" FontSize="9" FontFamily="{DynamicResource Fonts.Monospace}" Foreground="White" VerticalAlignment="Center"/>
54+
</Border>
55+
</StackPanel>
4256
</DataTemplate>
4357
</ContentControl.DataTemplates>
4458
</ContentControl>
4559

46-
<Path Grid.Column="2"
60+
<Path Grid.Column="1"
4761
Width="12" Height="12"
4862
Margin="0,0,10,0"
4963
HorizontalAlignment="Right" VerticalAlignment="Center"
@@ -107,7 +121,8 @@
107121
BorderThickness="0"
108122
Background="Transparent"
109123
ItemsSource="{TemplateBinding VisibleBranches, Mode=OneWay}"
110-
SelectedItem="{TemplateBinding SelectedBranch, Mode=TwoWay}"
124+
SelectedItem="{TemplateBinding SelectedBranch, Mode=OneWay}"
125+
SelectionChanged="OnDropDownListSelectionChanged"
111126
KeyDown="OnDropDownListKeyDown">
112127
<ListBox.ItemsPanel>
113128
<ItemsPanelTemplate>
@@ -126,7 +141,18 @@
126141
<DataTemplate DataType="m:Branch">
127142
<StackPanel Orientation="Horizontal" Background="Transparent" Height="28" PointerPressed="OnDropDownItemPointerPressed">
128143
<Path Width="14" Height="14" Fill="{DynamicResource Brush.FG1}" Data="{StaticResource Icons.Branch}"/>
129-
<TextBlock Margin="8,0,0,0" Text="{Binding FriendlyName, Mode=OneWay}" VerticalAlignment="Center"/>
144+
<v:BranchSelectorChoice Margin="8,0,0,0"
145+
VerticalAlignment="Center"
146+
Branch="{Binding Mode=OneWay}"
147+
UsePureName="{Binding #ThisControl.UsePureName, Mode=OneWay}"/>
148+
<Border Height="14"
149+
CornerRadius="7"
150+
Margin="4,0,0,0" Padding="6,0"
151+
VerticalAlignment="Center"
152+
Background="Green"
153+
IsVisible="{Binding Head, Converter={x:Static StringConverters.IsNullOrEmpty}}">
154+
<TextBlock Text="{DynamicResource Text.Push.New}" FontSize="9" FontFamily="{DynamicResource Fonts.Monospace}" Foreground="White" VerticalAlignment="Center"/>
155+
</Border>
130156
</StackPanel>
131157
</DataTemplate>
132158
</ListBox.ItemTemplate>

src/Views/BranchSelector.axaml.cs

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,42 @@
1010

1111
namespace SourceGit.Views
1212
{
13+
public class BranchSelectorChoice : TextBlock
14+
{
15+
public static readonly StyledProperty<Models.Branch> BranchProperty =
16+
AvaloniaProperty.Register<BranchSelectorChoice, Models.Branch>(nameof(Branch));
17+
18+
public Models.Branch Branch
19+
{
20+
get => GetValue(BranchProperty);
21+
set => SetValue(BranchProperty, value);
22+
}
23+
24+
public static readonly StyledProperty<bool> UsePureNameProperty =
25+
AvaloniaProperty.Register<BranchSelectorChoice, bool>(nameof(UsePureName));
26+
27+
public bool UsePureName
28+
{
29+
get => GetValue(UsePureNameProperty);
30+
set => SetValue(UsePureNameProperty, value);
31+
}
32+
33+
protected override Type StyleKeyOverride => typeof(TextBlock);
34+
35+
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
36+
{
37+
base.OnPropertyChanged(change);
38+
39+
if (change.Property == BranchProperty || change.Property == UsePureNameProperty)
40+
{
41+
if (Branch is { } branch)
42+
Text = UsePureName ? branch.Name : branch.FriendlyName;
43+
else
44+
Text = "---";
45+
}
46+
}
47+
}
48+
1349
public partial class BranchSelector : UserControl
1450
{
1551
public static readonly StyledProperty<List<Models.Branch>> BranchesProperty =
@@ -30,13 +66,16 @@ public List<Models.Branch> VisibleBranches
3066
set => SetValue(VisibleBranchesProperty, value);
3167
}
3268

33-
public static readonly StyledProperty<Models.Branch> SelectedBranchProperty =
34-
AvaloniaProperty.Register<BranchSelector, Models.Branch>(nameof(SelectedBranch));
69+
public static readonly DirectProperty<BranchSelector, Models.Branch> SelectedBranchProperty =
70+
AvaloniaProperty.RegisterDirect<BranchSelector, Models.Branch>(
71+
nameof(SelectedBranch),
72+
o => o.SelectedBranch,
73+
(o, v) => o.SelectedBranch = v);
3574

3675
public Models.Branch SelectedBranch
3776
{
38-
get => GetValue(SelectedBranchProperty);
39-
set => SetValue(SelectedBranchProperty, value);
77+
get => _selectedBranch;
78+
set => SetAndRaise(SelectedBranchProperty, ref _selectedBranch, value);
4079
}
4180

4281
public static readonly StyledProperty<bool> IsDropDownOpenedProperty =
@@ -57,6 +96,15 @@ public string SearchFilter
5796
set => SetValue(SearchFilterProperty, value);
5897
}
5998

99+
public static readonly StyledProperty<bool> UsePureNameProperty =
100+
AvaloniaProperty.Register<BranchSelector, bool>(nameof(UsePureName));
101+
102+
public bool UsePureName
103+
{
104+
get => GetValue(UsePureNameProperty);
105+
set => SetValue(UsePureNameProperty, value);
106+
}
107+
60108
public BranchSelector()
61109
{
62110
Focusable = true;
@@ -97,7 +145,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
97145

98146
SetCurrentValue(VisibleBranchesProperty, visible);
99147
if (!keepSelection && visible.Count > 0)
100-
SetCurrentValue(SelectedBranchProperty, visible[0]);
148+
SelectedBranch = visible[0];
101149
}
102150
}
103151
}
@@ -215,6 +263,12 @@ private void OnDropDownListKeyDown(object _, KeyEventArgs e)
215263
}
216264
}
217265

266+
private void OnDropDownListSelectionChanged(object sender, SelectionChangedEventArgs e)
267+
{
268+
if (e.AddedItems.Count == 1 && e.AddedItems[0] is Models.Branch branch)
269+
SelectedBranch = branch;
270+
}
271+
218272
private void OnDropDownItemPointerPressed(object sender, PointerPressedEventArgs e)
219273
{
220274
if (sender is Control { DataContext: Models.Branch branch })
@@ -225,5 +279,6 @@ private void OnDropDownItemPointerPressed(object sender, PointerPressedEventArgs
225279
}
226280

227281
private Popup _popup = null;
282+
private Models.Branch _selectedBranch = null;
228283
}
229284
}

src/Views/Pull.axaml

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,13 @@
4444
HorizontalAlignment="Right" VerticalAlignment="Center"
4545
Margin="0,0,8,0"
4646
Text="{DynamicResource Text.Pull.Branch}"/>
47-
<ComboBox Grid.Row="1" Grid.Column="1"
48-
Height="28" Padding="8,0"
49-
VerticalAlignment="Center" HorizontalAlignment="Stretch"
50-
ItemsSource="{Binding RemoteBranches}"
51-
SelectedItem="{Binding SelectedBranch, Mode=TwoWay}"
52-
IsTextSearchEnabled="True"
53-
TextSearch.TextBinding="{Binding Name, DataType=m:Branch}"
54-
IsEnabled="{Binding !HasSpecifiedRemoteBranch}">
55-
<ComboBox.ItemTemplate>
56-
<DataTemplate x:DataType="{x:Type m:Branch}">
57-
<StackPanel Orientation="Horizontal" Height="20" VerticalAlignment="Center">
58-
<Path Margin="0,0,8,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG1}" Data="{StaticResource Icons.Branch}"/>
59-
<TextBlock Text="{Binding Name}"/>
60-
</StackPanel>
61-
</DataTemplate>
62-
</ComboBox.ItemTemplate>
63-
</ComboBox>
47+
<v:BranchSelector Grid.Row="1" Grid.Column="1"
48+
Height="28" Padding="8,0"
49+
VerticalAlignment="Center" HorizontalAlignment="Stretch"
50+
Branches="{Binding RemoteBranches, Mode=OneWay}"
51+
SelectedBranch="{Binding SelectedBranch, Mode=TwoWay}"
52+
UsePureName="True"
53+
IsEnabled="{Binding !HasSpecifiedRemoteBranch}"/>
6454

6555
<TextBlock Grid.Row="2" Grid.Column="0"
6656
HorizontalAlignment="Right" VerticalAlignment="Center"

src/Views/Push.axaml

Lines changed: 7 additions & 24 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
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450"
89
x:Class="SourceGit.Views.Push"
910
x:DataType="vm:Push">
@@ -65,30 +66,12 @@
6566
Margin="0,0,8,0"
6667
Text="{DynamicResource Text.Push.To}"/>
6768
<Grid Grid.Row="2" Grid.Column="1" ColumnDefinitions="*,Auto">
68-
<ComboBox Grid.Column="0"
69-
Height="28" Padding="8,0"
70-
VerticalAlignment="Center" HorizontalAlignment="Stretch"
71-
ItemsSource="{Binding RemoteBranches}"
72-
IsTextSearchEnabled="True"
73-
TextSearch.TextBinding="{Binding Name, DataType=m:Branch}"
74-
SelectedItem="{Binding SelectedRemoteBranch, Mode=TwoWay}">
75-
<ComboBox.ItemTemplate>
76-
<DataTemplate x:DataType="{x:Type m:Branch}">
77-
<StackPanel Orientation="Horizontal" Height="20" VerticalAlignment="Center">
78-
<Path Margin="0,0,8,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG1}" Data="{StaticResource Icons.Branch}"/>
79-
<TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
80-
<Border Height="14"
81-
CornerRadius="7"
82-
Margin="4,0,0,0" Padding="6,0"
83-
VerticalAlignment="Center"
84-
Background="Green"
85-
IsVisible="{Binding Head, Converter={x:Static StringConverters.IsNullOrEmpty}}">
86-
<TextBlock Text="{DynamicResource Text.Push.New}" FontSize="9" FontFamily="{DynamicResource Fonts.Monospace}" Foreground="White" VerticalAlignment="Center"/>
87-
</Border>
88-
</StackPanel>
89-
</DataTemplate>
90-
</ComboBox.ItemTemplate>
91-
</ComboBox>
69+
<v:BranchSelector Grid.Column="0"
70+
Height="28" Padding="8,0"
71+
VerticalAlignment="Center" HorizontalAlignment="Stretch"
72+
Branches="{Binding RemoteBranches, Mode=OneWay}"
73+
SelectedBranch="{Binding SelectedRemoteBranch, Mode=TwoWay}"
74+
UsePureName="True"/>
9275

9376
<Button Grid.Column="1"
9477
Classes="icon_button"

0 commit comments

Comments
 (0)