enhance: show command icon in command palette

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2026-03-05 12:18:08 +08:00
parent 5ffea6069e
commit 3ec37a205b
3 changed files with 44 additions and 17 deletions

View File

@@ -5,13 +5,14 @@ namespace SourceGit.ViewModels
{
public class RepositoryCommandPaletteCmd
{
public string Key { get; set; }
public string Label { get; set; }
public string Icon { get; set; }
public Action Action { get; set; }
public string Label => $"{App.Text(Key)}...";
public RepositoryCommandPaletteCmd(string key, Action action)
public RepositoryCommandPaletteCmd(string label, string icon, Action action)
{
Key = key;
Label = label;
Icon = icon;
Action = action;
}
}
@@ -45,37 +46,37 @@ namespace SourceGit.ViewModels
_launcher = launcher;
_repo = repo;
_cmds.Add(new("Blame", () =>
_cmds.Add(new($"{App.Text("Blame")}...", "Blame", () =>
{
var sub = new BlameCommandPalette(_launcher, _repo.FullPath);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("Checkout", () =>
_cmds.Add(new($"{App.Text("Checkout")}...", "Check", () =>
{
var sub = new CheckoutCommandPalette(_launcher, _repo);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("Compare.WithHead", () =>
_cmds.Add(new($"{App.Text("Compare.WithHead")}...", "Compare", () =>
{
var sub = new CompareCommandPalette(_launcher, _repo, _repo.CurrentBranch);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("FileHistory", () =>
_cmds.Add(new($"{App.Text("FileHistory")}...", "Histories", () =>
{
var sub = new FileHistoryCommandPalette(_launcher, _repo.FullPath);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("Merge", () =>
_cmds.Add(new($"{App.Text("Merge")}...", "Merge", () =>
{
var sub = new MergeCommandPalette(_launcher, _repo);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("OpenFile", () =>
_cmds.Add(new($"{App.Text("OpenFile")}...", "OpenWith", () =>
{
var sub = new OpenFileCommandPalette(_launcher, _repo.FullPath);
_launcher.OpenCommandPalette(sub);
@@ -120,8 +121,7 @@ namespace SourceGit.ViewModels
foreach (var cmd in _cmds)
{
if (cmd.Key.Contains(_filter, StringComparison.OrdinalIgnoreCase) ||
cmd.Label.Contains(_filter, StringComparison.OrdinalIgnoreCase))
if (cmd.Label.Contains(_filter, StringComparison.OrdinalIgnoreCase))
visible.Add(cmd);
}

View File

@@ -86,12 +86,9 @@
<ListBox.ItemTemplate>
<DataTemplate DataType="vm:RepositoryCommandPaletteCmd">
<Grid ColumnDefinitions="Auto,*" Background="Transparent" Tapped="OnItemTapped">
<Path Grid.Column="0"
Width="12" Height="12"
Data="{StaticResource Icons.Command}"
IsHitTestVisible="False"/>
<v:RepositoryCommandPaletteIcon Grid.Column="0" Width="12" Height="12" IsHitTestVisible="False"/>
<TextBlock Grid.Column="1"
Margin="6,0,0,0"
Margin="8,0,0,0"
VerticalAlignment="Center"
IsHitTestVisible="False"
Text="{Binding Label, Mode=OneWay}"/>

View File

@@ -0,0 +1,30 @@
using System;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Media;
namespace SourceGit.Views
{
public class RepositoryCommandPaletteIcon : Path
{
protected override Type StyleKeyOverride => typeof(Path);
protected override void OnDataContextChanged(EventArgs e)
{
base.OnDataContextChanged(e);
if (DataContext is ViewModels.RepositoryCommandPaletteCmd cmd && !string.IsNullOrEmpty(cmd.Icon))
{
var geo = this.FindResource($"Icons.{cmd.Icon}") as StreamGeometry;
if (geo != null)
{
Data = geo;
return;
}
}
Data = this.FindResource("Icon.Command") as StreamGeometry;
}
}
}