mirror of
https://fastgit.cc/github.com/sourcegit-scm/sourcegit
synced 2026-04-21 13:20:30 +08:00
feature: add CustomAction to command palette (#2165)
Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
127
src/ViewModels/ExecuteCustomActionCommandPalette.cs
Normal file
127
src/ViewModels/ExecuteCustomActionCommandPalette.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SourceGit.ViewModels
|
||||
{
|
||||
public class ExecuteCustomActionCommandPaletteCmd
|
||||
{
|
||||
public Models.CustomAction Action { get; set; }
|
||||
public bool IsGlobal { get; set; }
|
||||
public string Name { get => Action.Name; }
|
||||
|
||||
public ExecuteCustomActionCommandPaletteCmd(Models.CustomAction action, bool isGlobal)
|
||||
{
|
||||
Action = action;
|
||||
IsGlobal = isGlobal;
|
||||
}
|
||||
}
|
||||
|
||||
public class ExecuteCustomActionCommandPalette : ICommandPalette
|
||||
{
|
||||
public List<ExecuteCustomActionCommandPaletteCmd> VisibleActions
|
||||
{
|
||||
get => _visibleActions;
|
||||
private set => SetProperty(ref _visibleActions, value);
|
||||
}
|
||||
|
||||
public ExecuteCustomActionCommandPaletteCmd Selected
|
||||
{
|
||||
get => _selected;
|
||||
set => SetProperty(ref _selected, value);
|
||||
}
|
||||
|
||||
public string Filter
|
||||
{
|
||||
get => _filter;
|
||||
set
|
||||
{
|
||||
if (SetProperty(ref _filter, value))
|
||||
UpdateVisibleActions();
|
||||
}
|
||||
}
|
||||
|
||||
public ExecuteCustomActionCommandPalette(Launcher launcher, Repository repo)
|
||||
{
|
||||
_launcher = launcher;
|
||||
_repo = repo;
|
||||
|
||||
var actions = repo.GetCustomActions(Models.CustomActionScope.Repository);
|
||||
foreach (var (action, menu) in actions)
|
||||
_actions.Add(new(action, menu.IsGlobal));
|
||||
|
||||
if (_actions.Count > 0)
|
||||
{
|
||||
_actions.Sort((l, r) =>
|
||||
{
|
||||
if (l.IsGlobal != r.IsGlobal)
|
||||
return l.IsGlobal ? -1 : 1;
|
||||
|
||||
return l.Name.CompareTo(r.Name, StringComparison.OrdinalIgnoreCase);
|
||||
});
|
||||
|
||||
_visibleActions = _actions;
|
||||
_selected = _actions[0];
|
||||
}
|
||||
}
|
||||
|
||||
public override void Cleanup()
|
||||
{
|
||||
_launcher = null;
|
||||
_repo = null;
|
||||
_actions.Clear();
|
||||
_visibleActions.Clear();
|
||||
_selected = null;
|
||||
_filter = null;
|
||||
}
|
||||
|
||||
public void ClearFilter()
|
||||
{
|
||||
Filter = string.Empty;
|
||||
}
|
||||
|
||||
public async Task ExecAsync()
|
||||
{
|
||||
_launcher.CommandPalette = null;
|
||||
|
||||
if (_selected != null)
|
||||
await _repo.ExecCustomActionAsync(_selected.Action, null);
|
||||
|
||||
Dispose();
|
||||
GC.Collect();
|
||||
}
|
||||
|
||||
private void UpdateVisibleActions()
|
||||
{
|
||||
var filter = _filter?.Trim();
|
||||
if (string.IsNullOrEmpty(filter))
|
||||
{
|
||||
VisibleActions = _actions;
|
||||
return;
|
||||
}
|
||||
|
||||
var visible = new List<ExecuteCustomActionCommandPaletteCmd>();
|
||||
foreach (var act in _actions)
|
||||
{
|
||||
if (act.Name.Contains(filter, StringComparison.OrdinalIgnoreCase))
|
||||
visible.Add(act);
|
||||
}
|
||||
|
||||
var autoSelected = _selected;
|
||||
if (visible.Count == 0)
|
||||
autoSelected = null;
|
||||
else if (_selected == null || !visible.Contains(_selected))
|
||||
autoSelected = visible[0];
|
||||
|
||||
VisibleActions = visible;
|
||||
Selected = autoSelected;
|
||||
}
|
||||
|
||||
private Launcher _launcher;
|
||||
private Repository _repo;
|
||||
private List<ExecuteCustomActionCommandPaletteCmd> _actions = [];
|
||||
private List<ExecuteCustomActionCommandPaletteCmd> _visibleActions = [];
|
||||
private ExecuteCustomActionCommandPaletteCmd _selected = null;
|
||||
private string _filter;
|
||||
}
|
||||
}
|
||||
@@ -140,6 +140,12 @@ namespace SourceGit.ViewModels
|
||||
await App.ShowDialog(new RepositoryConfigure(repo));
|
||||
}));
|
||||
|
||||
_cmds.Add(new($"{App.Text("Repository.CustomActions")}...", "custom actions", "Action", async () =>
|
||||
{
|
||||
var sub = new ExecuteCustomActionCommandPalette(_launcher, _repo);
|
||||
_launcher.OpenCommandPalette(sub);
|
||||
}));
|
||||
|
||||
_cmds.Sort((l, r) => l.Label.CompareTo(r.Label));
|
||||
_visibleCmds = _cmds;
|
||||
_selectedCmd = _cmds[0];
|
||||
|
||||
118
src/Views/ExecuteCustomActionCommandPalette.axaml
Normal file
118
src/Views/ExecuteCustomActionCommandPalette.axaml
Normal file
@@ -0,0 +1,118 @@
|
||||
<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: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.ExecuteCustomActionCommandPalette"
|
||||
x:DataType="vm:ExecuteCustomActionCommandPalette">
|
||||
<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.Repository.CustomActions}"
|
||||
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="ActionListBox"
|
||||
MaxHeight="250"
|
||||
Margin="4,8,4,0"
|
||||
BorderThickness="0"
|
||||
SelectionMode="Single"
|
||||
Background="Transparent"
|
||||
Focusable="True"
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
|
||||
ScrollViewer.VerticalScrollBarVisibility="Auto"
|
||||
ItemsSource="{Binding VisibleActions, Mode=OneWay}"
|
||||
SelectedItem="{Binding Selected, Mode=TwoWay}"
|
||||
IsVisible="{Binding VisibleActions, 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="vm:ExecuteCustomActionCommandPaletteCmd">
|
||||
<Grid ColumnDefinitions="Auto,*,Auto" Background="Transparent" Tapped="OnItemTapped">
|
||||
<Path Grid.Column="0"
|
||||
Width="12" Height="12"
|
||||
Data="{StaticResource Icons.Action}"
|
||||
IsHitTestVisible="False"/>
|
||||
<TextBlock Grid.Column="1"
|
||||
Margin="4,0,0,0"
|
||||
VerticalAlignment="Center"
|
||||
IsHitTestVisible="False"
|
||||
Text="{Binding Name, Mode=OneWay}"/>
|
||||
<Border Grid.Column="2" Margin="4,0,0,0" Height="16" Background="Green" CornerRadius="8" VerticalAlignment="Center" IsVisible="{Binding IsGlobal}">
|
||||
<TextBlock Text="GLOBAL" Margin="8,0" FontSize="10" Foreground="White"/>
|
||||
</Border>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
||||
<Path Grid.Row="1"
|
||||
Width="64" Height="64"
|
||||
Margin="0,16,0,0"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
Fill="{DynamicResource Brush.FG2}"
|
||||
Data="{StaticResource Icons.Empty}"
|
||||
IsVisible="{Binding VisibleActions, Mode=OneWay, Converter={x:Static c:ListConverters.IsNullOrEmpty}}"/>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
63
src/Views/ExecuteCustomActionCommandPalette.axaml.cs
Normal file
63
src/Views/ExecuteCustomActionCommandPalette.axaml.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
|
||||
namespace SourceGit.Views
|
||||
{
|
||||
public partial class ExecuteCustomActionCommandPalette : UserControl
|
||||
{
|
||||
public ExecuteCustomActionCommandPalette()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
protected override async void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
base.OnKeyDown(e);
|
||||
|
||||
if (DataContext is not ViewModels.ExecuteCustomActionCommandPalette vm)
|
||||
return;
|
||||
|
||||
if (e.Key == Key.Enter)
|
||||
{
|
||||
await vm.ExecAsync();
|
||||
e.Handled = true;
|
||||
}
|
||||
else if (e.Key == Key.Up)
|
||||
{
|
||||
if (ActionListBox.IsKeyboardFocusWithin)
|
||||
{
|
||||
FilterTextBox.Focus(NavigationMethod.Directional);
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (e.Key == Key.Down || e.Key == Key.Tab)
|
||||
{
|
||||
if (FilterTextBox.IsKeyboardFocusWithin)
|
||||
{
|
||||
if (vm.VisibleActions.Count > 0)
|
||||
ActionListBox.Focus(NavigationMethod.Directional);
|
||||
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (ActionListBox.IsKeyboardFocusWithin && e.Key == Key.Tab)
|
||||
{
|
||||
FilterTextBox.Focus(NavigationMethod.Directional);
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async void OnItemTapped(object sender, TappedEventArgs e)
|
||||
{
|
||||
if (DataContext is ViewModels.ExecuteCustomActionCommandPalette vm)
|
||||
{
|
||||
await vm.ExecAsync();
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@
|
||||
|
||||
<ListBox Grid.Row="2"
|
||||
x:Name="CmdListBox"
|
||||
MaxHeight="400"
|
||||
MaxHeight="420"
|
||||
Margin="4,0"
|
||||
BorderThickness="0"
|
||||
SelectionMode="Single"
|
||||
|
||||
Reference in New Issue
Block a user