feature: add Branch Compare command palette

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-10-28 15:25:56 +08:00
parent 6538efcd9f
commit 258480dd41
4 changed files with 289 additions and 15 deletions

View File

@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
namespace SourceGit.ViewModels
{
public class BranchCompareCommandPalette : ICommandPalette
{
public List<Models.Branch> Branches
{
get => _branches;
private set => SetProperty(ref _branches, value);
}
public Models.Branch SelectedBranch
{
get => _selectedBranch;
set => SetProperty(ref _selectedBranch, value);
}
public string Filter
{
get => _filter;
set
{
if (SetProperty(ref _filter, value))
UpdateBranches();
}
}
public BranchCompareCommandPalette(Launcher launcher, Repository repo)
{
_launcher = launcher;
_repo = repo;
UpdateBranches();
}
public override void Cleanup()
{
_launcher = null;
_repo = null;
_branches.Clear();
_selectedBranch = null;
_filter = null;
}
public void ClearFilter()
{
Filter = string.Empty;
}
public void Launch()
{
if (_selectedBranch != null)
App.ShowWindow(new BranchCompare(_repo.FullPath, _selectedBranch, _repo.CurrentBranch));
_launcher?.CancelCommandPalette();
}
private void UpdateBranches()
{
var current = _repo.CurrentBranch;
if (current == null)
return;
var branches = new List<Models.Branch>();
foreach (var b in _repo.Branches)
{
if (b == current)
continue;
if (string.IsNullOrEmpty(_filter) || b.FriendlyName.Contains(_filter, StringComparison.OrdinalIgnoreCase))
branches.Add(b);
}
branches.Sort((l, r) =>
{
if (l.IsLocal == r.IsLocal)
return l.Name.CompareTo(r.Name);
return l.IsLocal ? -1 : 1;
});
Branches = branches;
}
private Launcher _launcher;
private Repository _repo;
private List<Models.Branch> _branches = [];
private Models.Branch _selectedBranch = null;
private string _filter;
}
}

View File

@@ -3,7 +3,18 @@ using System.Collections.Generic;
namespace SourceGit.ViewModels
{
public record RepositoryCommandPaletteCmd(string Name, string Label, bool AutoCloseCommandPalette, Action Action);
public class RepositoryCommandPaletteCmd
{
public string Key { get; set; }
public Action Action { get; set; }
public string Label => $"{App.Text(Key)}...";
public RepositoryCommandPaletteCmd(string key, Action action)
{
Key = key;
Action = action;
}
}
public class RepositoryCommandPalette : ICommandPalette
{
@@ -34,24 +45,30 @@ namespace SourceGit.ViewModels
_launcher = launcher;
_repo = repo;
_cmds.Add(new("File History", App.Text("FileHistory") + "...", false, () =>
_cmds.Add(new("FileHistory", () =>
{
var sub = new FileHistoryCommandPalette(_launcher, _repo.FullPath);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("Blame", App.Text("Blame") + "...", false, () =>
_cmds.Add(new("Blame", () =>
{
var sub = new BlameCommandPalette(_launcher, _repo.FullPath);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("Merge", App.Text("Merge") + "...", false, () =>
_cmds.Add(new("Merge", () =>
{
var sub = new MergeCommandPalette(_launcher, _repo);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("BranchCompare", () =>
{
var sub = new BranchCompareCommandPalette(_launcher, _repo);
_launcher.OpenCommandPalette(sub);
}));
_visibleCmds = _cmds;
}
@@ -72,16 +89,9 @@ namespace SourceGit.ViewModels
public void Exec()
{
if (_selectedCmd == null)
{
_launcher?.CancelCommandPalette();
return;
}
var autoClose = _selectedCmd.AutoCloseCommandPalette;
_selectedCmd.Action?.Invoke();
if (autoClose)
if (_selectedCmd != null)
_selectedCmd.Action?.Invoke();
else
_launcher?.CancelCommandPalette();
}
@@ -96,7 +106,7 @@ namespace SourceGit.ViewModels
var visible = new List<RepositoryCommandPaletteCmd>();
foreach (var cmd in VisibleCmds)
{
if (cmd.Name.Contains(_filter, StringComparison.OrdinalIgnoreCase) ||
if (cmd.Key.Contains(_filter, StringComparison.OrdinalIgnoreCase) ||
cmd.Label.Contains(_filter, StringComparison.OrdinalIgnoreCase))
visible.Add(cmd);
}

View File

@@ -0,0 +1,107 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="using:SourceGit.Models"
xmlns:vm="using:SourceGit.ViewModels"
xmlns:v="using:SourceGit.Views"
xmlns:c="using:SourceGit.Converters"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.BranchCompareCommandPalette"
x:DataType="vm:BranchCompareCommandPalette">
<Grid RowDefinitions="Auto,Auto">
<v:RepositoryCommandPaletteTextBox Grid.Row="0"
x:Name="FilterTextBox"
Height="24"
Margin="4,8,4,0"
BorderThickness="1"
CornerRadius="12"
Text="{Binding Filter, Mode=TwoWay}"
BorderBrush="{DynamicResource Brush.Border2}"
VerticalContentAlignment="Center">
<TextBox.InnerLeftContent>
<StackPanel Orientation="Horizontal">
<Path Width="14" Height="14"
Margin="6,0,0,0"
Fill="{DynamicResource Brush.FG2}"
Data="{StaticResource Icons.Search}"/>
<Border BorderThickness="0"
Background="{DynamicResource Brush.Badge}"
Height="18"
CornerRadius="4"
Margin="4,0,0,0" Padding="4,0">
<TextBlock Text="{DynamicResource Text.BranchCompare}"
Foreground="Black"
FontWeight="Bold"/>
</Border>
</StackPanel>
</TextBox.InnerLeftContent>
<TextBox.InnerRightContent>
<Button Classes="icon_button"
Width="16"
Margin="0,0,6,0"
Command="{Binding ClearFilter}"
IsVisible="{Binding Filter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
HorizontalAlignment="Right">
<Path Width="14" Height="14"
Margin="0,1,0,0"
Fill="{DynamicResource Brush.FG1}"
Data="{StaticResource Icons.Clear}"/>
</Button>
</TextBox.InnerRightContent>
</v:RepositoryCommandPaletteTextBox>
<ListBox Grid.Row="1"
x:Name="BranchListBox"
MaxHeight="250"
Margin="4,8,4,0"
BorderThickness="0"
SelectionMode="Single"
Background="Transparent"
Focusable="True"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Branches, Mode=OneWay}"
SelectedItem="{Binding SelectedBranch, Mode=TwoWay}"
IsVisible="{Binding Branches, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Padding" Value="8,0"/>
<Setter Property="MinHeight" Value="26"/>
<Setter Property="CornerRadius" Value="4"/>
</Style>
<Style Selector="ListBox">
<Setter Property="FocusAdorner">
<FocusAdornerTemplate>
<Grid/>
</FocusAdornerTemplate>
</Setter>
</Style>
</ListBox.Styles>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="m:Branch">
<Grid ColumnDefinitions="Auto,*" Background="Transparent" Tapped="OnItemTapped">
<Path Grid.Column="0"
Width="12" Height="12"
Data="{StaticResource Icons.Branch}"
IsHitTestVisible="False"/>
<TextBlock Grid.Column="1"
Margin="4,0,0,0"
VerticalAlignment="Center"
IsHitTestVisible="False"
Text="{Binding FriendlyName, Mode=OneWay}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>

View File

@@ -0,0 +1,66 @@
using Avalonia.Controls;
using Avalonia.Input;
namespace SourceGit.Views
{
public partial class BranchCompareCommandPalette : UserControl
{
public BranchCompareCommandPalette()
{
InitializeComponent();
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (DataContext is not ViewModels.BranchCompareCommandPalette vm)
return;
if (e.Key == Key.Enter)
{
vm.Launch();
e.Handled = true;
}
else if (e.Key == Key.Up)
{
if (BranchListBox.IsKeyboardFocusWithin)
{
FilterTextBox.Focus(NavigationMethod.Directional);
e.Handled = true;
return;
}
}
else if (e.Key == Key.Down || e.Key == Key.Tab)
{
if (FilterTextBox.IsKeyboardFocusWithin)
{
if (vm.Branches.Count > 0)
{
BranchListBox.Focus(NavigationMethod.Directional);
vm.SelectedBranch = vm.Branches[0];
}
e.Handled = true;
return;
}
if (BranchListBox.IsKeyboardFocusWithin && e.Key == Key.Tab)
{
FilterTextBox.Focus(NavigationMethod.Directional);
e.Handled = true;
return;
}
}
}
private void OnItemTapped(object sender, TappedEventArgs e)
{
if (DataContext is ViewModels.BranchCompareCommandPalette vm)
{
vm.Launch();
e.Handled = true;
}
}
}
}