feature: add global configuration for custom action (#1077)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-03-10 21:19:38 +08:00
parent 0f9087fac6
commit 5c279b4b56
No known key found for this signature in database
6 changed files with 182 additions and 6 deletions

View file

@ -471,11 +471,7 @@ namespace SourceGit.Models
public CustomAction AddNewCustomAction()
{
var act = new CustomAction()
{
Name = "Unnamed Custom Action",
};
var act = new CustomAction() { Name = "Unnamed Action" };
CustomActions.Add(act);
return act;
}

View file

@ -695,6 +695,11 @@ namespace SourceGit.ViewModels
menu.Items.Add(new MenuItem() { Header = "-" });
var actions = new List<Models.CustomAction>();
foreach (var action in Preferences.Instance.CustomActions)
{
if (action.Scope == Models.CustomActionScope.Commit)
actions.Add(action);
}
foreach (var action in _repo.Settings.CustomActions)
{
if (action.Scope == Models.CustomActionScope.Commit)

View file

@ -342,6 +342,12 @@ namespace SourceGit.ViewModels
set;
} = [];
public AvaloniaList<Models.CustomAction> CustomActions
{
get;
set;
} = [];
public AvaloniaList<Models.OpenAIService> OpenAIServices
{
get;

View file

@ -1444,6 +1444,12 @@ namespace SourceGit.ViewModels
public ContextMenu CreateContextMenuForCustomAction()
{
var actions = new List<Models.CustomAction>();
foreach (var action in Preferences.Instance.CustomActions)
{
if (action.Scope == Models.CustomActionScope.Repository)
actions.Add(action);
}
foreach (var action in _settings.CustomActions)
{
if (action.Scope == Models.CustomActionScope.Repository)
@ -2350,6 +2356,12 @@ namespace SourceGit.ViewModels
private void TryToAddCustomActionsToBranchContextMenu(ContextMenu menu, Models.Branch branch)
{
var actions = new List<Models.CustomAction>();
foreach (var action in Preferences.Instance.CustomActions)
{
if (action.Scope == Models.CustomActionScope.Branch)
actions.Add(action);
}
foreach (var action in Settings.CustomActions)
{
if (action.Scope == Models.CustomActionScope.Branch)

View file

@ -527,12 +527,126 @@
</StackPanel>
</TabItem>
<TabItem>
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Configure.CustomAction}"/>
</TabItem.Header>
<Grid MinHeight="340" Margin="0,8,0,16">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*" MaxWidth="400"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0"
BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}"
Background="{DynamicResource Brush.Contents}">
<Grid RowDefinitions="*,1,Auto">
<ListBox Grid.Row="0"
Background="Transparent"
ItemsSource="{Binding CustomActions}"
SelectedItem="{Binding #ThisControl.SelectedCustomAction, Mode=TwoWay}"
SelectionMode="Single">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="MinHeight" Value="0"/>
<Setter Property="Height" Value="26"/>
<Setter Property="Padding" Value="4,2"/>
</Style>
</ListBox.Styles>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="m:CustomAction">
<Grid Margin="4,0" ColumnDefinitions="Auto,*">
<Path Grid.Column="0" Width="12" Height="12" Data="{StaticResource Icons.Action}" Fill="{DynamicResource Brush.FG1}"/>
<TextBlock Grid.Column="1" Text="{Binding Name}" Margin="6,0,0,0" TextTrimming="CharacterEllipsis"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Rectangle Grid.Row="1" Height="1" Fill="{DynamicResource Brush.Border2}" HorizontalAlignment="Stretch" VerticalAlignment="Bottom"/>
<StackPanel Grid.Row="2" Orientation="Horizontal" Background="{DynamicResource Brush.ToolBar}">
<Button Classes="icon_button" Click="OnAddCustomAction">
<Path Width="14" Height="14" Data="{StaticResource Icons.Plus}"/>
</Button>
<Rectangle Width="1" Fill="{DynamicResource Brush.Border2}" HorizontalAlignment="Left" VerticalAlignment="Stretch"/>
<Button Classes="icon_button" Click="OnRemoveSelectedCustomAction">
<Path Width="14" Height="14" Data="{StaticResource Icons.Window.Minimize}"/>
</Button>
<Rectangle Width="1" Fill="{DynamicResource Brush.Border2}" HorizontalAlignment="Left" VerticalAlignment="Stretch"/>
</StackPanel>
</Grid>
</Border>
<ContentControl Grid.Column="1" Margin="16,0,0,0">
<ContentControl.Content>
<Binding Path="SelectedCustomAction" ElementName="ThisControl">
<Binding.TargetNullValue>
<Path Width="64" Height="64"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Fill="{DynamicResource Brush.FG2}"
Data="{StaticResource Icons.Empty}"/>
</Binding.TargetNullValue>
</Binding>
</ContentControl.Content>
<ContentControl.DataTemplates>
<DataTemplate DataType="m:CustomAction">
<StackPanel Orientation="Vertical">
<TextBlock Text="{DynamicResource Text.Configure.CustomAction.Name}"/>
<TextBox Margin="0,4,0,0" CornerRadius="3" Height="28" Text="{Binding Name, Mode=TwoWay}"/>
<TextBlock Margin="0,12,0,0" Text="{DynamicResource Text.Configure.CustomAction.Scope}"/>
<ComboBox Margin="0,4,0,0" Height="28" HorizontalAlignment="Stretch" SelectedIndex="{Binding Scope, Mode=TwoWay}">
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Repository}"/>
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Commit}"/>
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Branch}"/>
</ComboBox>
<TextBlock Margin="0,12,0,0" Text="{DynamicResource Text.Configure.CustomAction.Executable}"/>
<TextBox Margin="0,4,0,0" Height="28" CornerRadius="3" Text="{Binding Executable, Mode=TwoWay}">
<TextBox.InnerRightContent>
<Button Classes="icon_button" Width="30" Height="30" Click="SelectExecutableForCustomAction">
<Path Data="{StaticResource Icons.Folder.Open}" Fill="{DynamicResource Brush.FG1}"/>
</Button>
</TextBox.InnerRightContent>
</TextBox>
<TextBlock Margin="0,12,0,0" Text="{DynamicResource Text.Configure.CustomAction.Arguments}"/>
<TextBox Margin="0,4,0,0" CornerRadius="3" Height="28" Text="{Binding Arguments, Mode=TwoWay}"/>
<TextBlock Margin="0,2,0,0" TextWrapping="Wrap" Text="{DynamicResource Text.Configure.CustomAction.Arguments.Tip}" Foreground="{DynamicResource Brush.FG2}"/>
<CheckBox Margin="0,8,0,0" Content="{DynamicResource Text.Configure.CustomAction.WaitForExit}" IsChecked="{Binding WaitForExit, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
</Grid>
</TabItem>
<TabItem>
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Preferences.AI}"/>
</TabItem.Header>
<Grid ColumnDefinitions="200,*" Margin="0,8,0,16" MinHeight="400">
<Grid Margin="0,8,0,16" MinHeight="400">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*" MaxWidth="400"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0"
BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}"
Background="{DynamicResource Brush.Contents}">

View file

@ -103,6 +103,15 @@ namespace SourceGit.Views
set => SetValue(SelectedOpenAIServiceProperty, value);
}
public static readonly StyledProperty<Models.CustomAction> SelectedCustomActionProperty =
AvaloniaProperty.Register<Preferences, Models.CustomAction>(nameof(SelectedCustomAction));
public Models.CustomAction SelectedCustomAction
{
get => GetValue(SelectedCustomActionProperty);
set => SetValue(SelectedCustomActionProperty, value);
}
public Preferences()
{
var pref = ViewModels.Preferences.Instance;
@ -368,6 +377,40 @@ namespace SourceGit.Views
e.Handled = true;
}
private void OnAddCustomAction(object sender, RoutedEventArgs e)
{
var action = new Models.CustomAction() { Name = "Unnamed Action (Global)" };
ViewModels.Preferences.Instance.CustomActions.Add(action);
SelectedCustomAction = action;
e.Handled = true;
}
private async void SelectExecutableForCustomAction(object sender, RoutedEventArgs e)
{
var options = new FilePickerOpenOptions()
{
FileTypeFilter = [new FilePickerFileType("Executable file(script)") { Patterns = ["*.*"] }],
AllowMultiple = false,
};
var selected = await StorageProvider.OpenFilePickerAsync(options);
if (selected.Count == 1 && sender is Button { DataContext: Models.CustomAction action })
action.Executable = selected[0].Path.LocalPath;
e.Handled = true;
}
private void OnRemoveSelectedCustomAction(object sender, RoutedEventArgs e)
{
if (SelectedCustomAction == null)
return;
ViewModels.Preferences.Instance.CustomActions.Remove(SelectedCustomAction);
SelectedCustomAction = null;
e.Handled = true;
}
private void UpdateGitVersion()
{
GitVersion = Native.OS.GitVersionString;