feature: support to use input control in custom action

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-06-25 16:28:54 +08:00
parent a8803ca188
commit 676785f8b1
26 changed files with 659 additions and 56 deletions

View File

@@ -0,0 +1,62 @@
using Avalonia.Collections;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class ConfigureCustomActionControls : ObservableObject
{
public AvaloniaList<Models.CustomActionControl> Controls
{
get;
}
public Models.CustomActionControl Edit
{
get => _edit;
set => SetProperty(ref _edit, value);
}
public ConfigureCustomActionControls(AvaloniaList<Models.CustomActionControl> controls)
{
Controls = controls;
}
public void Add()
{
var added = new Models.CustomActionControl() { Type = Models.CustomActionControlType.TextBox };
Controls.Add(added);
Edit = added;
}
public void Remove()
{
if (_edit == null)
return;
Controls.Remove(_edit);
Edit = null;
}
public void MoveUp()
{
if (_edit == null)
return;
var idx = Controls.IndexOf(_edit);
if (idx > 0)
Controls.Move(idx - 1, idx);
}
public void MoveDown()
{
if (_edit == null)
return;
var idx = Controls.IndexOf(_edit);
if (idx < Controls.Count - 1)
Controls.Move(idx + 1, idx);
}
private Models.CustomActionControl _edit;
}
}

View File

@@ -1,8 +1,75 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public interface ICustomActionControlParameter
{
string GetValue();
}
public class CustomActionControlTextBox : ICustomActionControlParameter
{
public string Label { get; set; } = string.Empty;
public string Placeholder { get; set; } = string.Empty;
public string Text { get; set; } = string.Empty;
public CustomActionControlTextBox(string label, string placeholder, string defaultValue)
{
Label = label;
Placeholder = placeholder;
Text = defaultValue;
}
public string GetValue() => Text;
}
public class CustomActionControlPathSelector : ObservableObject, ICustomActionControlParameter
{
public string Label { get; set; } = string.Empty;
public string Placeholder { get; set; } = string.Empty;
public bool IsFolder { get; set; } = false;
public string Path
{
get => _path;
set => SetProperty(ref _path, value);
}
public CustomActionControlPathSelector(string label, string placeholder, bool isFolder, string defaultValue)
{
Label = label;
Placeholder = placeholder;
IsFolder = isFolder;
_path = defaultValue;
}
public string GetValue() => _path;
private string _path = string.Empty;
}
public class CustomActionControlCheckBox : ICustomActionControlParameter
{
public string Label { get; set; } = string.Empty;
public string ToolTip { get; set; } = string.Empty;
public string CheckedValue { get; set; } = string.Empty;
public bool IsChecked { get; set; }
public CustomActionControlCheckBox(string label, string tooltip, string checkedValue, bool isChecked)
{
Label = label;
ToolTip = string.IsNullOrEmpty(tooltip) ? null : tooltip;
CheckedValue = checkedValue;
IsChecked = isChecked;
}
public string GetValue() => IsChecked ? CheckedValue : string.Empty;
}
public class ExecuteCustomAction : Popup
{
public Models.CustomAction CustomAction
@@ -10,25 +77,38 @@ namespace SourceGit.ViewModels
get;
}
public List<ICustomActionControlParameter> ControlParameters
{
get;
} = [];
public bool IsSimpleMode
{
get => ControlParameters.Count == 0;
}
public ExecuteCustomAction(Repository repo, Models.CustomAction action)
{
_repo = repo;
_args = action.Arguments.Replace("${REPO}", GetWorkdir());
_commandline = action.Arguments.Replace("${REPO}", GetWorkdir());
CustomAction = action;
PrepareControlParameters();
}
public ExecuteCustomAction(Repository repo, Models.CustomAction action, Models.Branch branch)
{
_repo = repo;
_args = action.Arguments.Replace("${REPO}", GetWorkdir()).Replace("${BRANCH}", branch.FriendlyName);
_commandline = action.Arguments.Replace("${REPO}", GetWorkdir()).Replace("${BRANCH}", branch.FriendlyName);
CustomAction = action;
PrepareControlParameters();
}
public ExecuteCustomAction(Repository repo, Models.CustomAction action, Models.Commit commit)
{
_repo = repo;
_args = action.Arguments.Replace("${REPO}", GetWorkdir()).Replace("${SHA}", commit.SHA);
_commandline = action.Arguments.Replace("${REPO}", GetWorkdir()).Replace("${SHA}", commit.SHA);
CustomAction = action;
PrepareControlParameters();
}
public override Task<bool> Sure()
@@ -36,15 +116,22 @@ namespace SourceGit.ViewModels
_repo.SetWatcherEnabled(false);
ProgressDescription = "Run custom action ...";
var cmdline = _commandline;
for (var i = 0; i < ControlParameters.Count; i++)
{
var param = ControlParameters[i];
cmdline = cmdline.Replace($"${i}", param.GetValue());
}
var log = _repo.CreateLog(CustomAction.Name);
Use(log);
return Task.Run(() =>
{
if (CustomAction.WaitForExit)
Commands.ExecuteCustomAction.RunAndWait(_repo.FullPath, CustomAction.Executable, _args, log);
Commands.ExecuteCustomAction.RunAndWait(_repo.FullPath, CustomAction.Executable, cmdline, log);
else
Commands.ExecuteCustomAction.Run(_repo.FullPath, CustomAction.Executable, _args);
Commands.ExecuteCustomAction.Run(_repo.FullPath, CustomAction.Executable, cmdline);
log.Complete();
CallUIThread(() => _repo.SetWatcherEnabled(true));
@@ -52,12 +139,31 @@ namespace SourceGit.ViewModels
});
}
private void PrepareControlParameters()
{
foreach (var ctl in CustomAction.Controls)
{
switch (ctl.Type)
{
case Models.CustomActionControlType.TextBox:
ControlParameters.Add(new CustomActionControlTextBox(ctl.Label, ctl.Description, ctl.StringValue));
break;
case Models.CustomActionControlType.CheckBox:
ControlParameters.Add(new CustomActionControlCheckBox(ctl.Label, ctl.Description, ctl.StringValue, ctl.BoolValue));
break;
case Models.CustomActionControlType.PathSelector:
ControlParameters.Add(new CustomActionControlPathSelector(ctl.Label, ctl.Description, ctl.BoolValue, ctl.StringValue));
break;
}
}
}
private string GetWorkdir()
{
return OperatingSystem.IsWindows() ? _repo.FullPath.Replace("/", "\\") : _repo.FullPath;
}
private readonly Repository _repo = null;
private readonly string _args;
private readonly string _commandline = string.Empty;
}
}

View File

@@ -806,9 +806,7 @@ namespace SourceGit.ViewModels
item.Header = dup.Name;
item.Click += (_, e) =>
{
if (_repo.CanCreatePopup())
_repo.ShowAndStartPopup(new ExecuteCustomAction(_repo, dup, commit));
_repo.ExecCustomAction(dup, commit);
e.Handled = true;
};

View File

@@ -819,17 +819,34 @@ namespace SourceGit.ViewModels
}
public void ApplyPatch()
{
if (CanCreatePopup())
ShowPopup(new Apply(this));
}
public void ExecCustomAction(Models.CustomAction action, object scope)
{
if (!CanCreatePopup())
return;
ShowPopup(new Apply(this));
var popup = null as ExecuteCustomAction;
if (scope is Models.Branch b)
popup = new ExecuteCustomAction(this, action, b);
else if (scope is Models.Commit c)
popup = new ExecuteCustomAction(this, action, c);
else
popup = new ExecuteCustomAction(this, action);
if (action.Controls.Count == 0)
ShowAndStartPopup(popup);
else
ShowPopup(popup);
}
public void Cleanup()
{
if (!CanCreatePopup())
return;
ShowAndStartPopup(new Cleanup(this));
if (CanCreatePopup())
ShowAndStartPopup(new Cleanup(this));
}
public void ClearFilter()
@@ -1706,9 +1723,7 @@ namespace SourceGit.ViewModels
item.Header = dup.Name;
item.Click += (_, e) =>
{
if (CanCreatePopup())
ShowAndStartPopup(new ExecuteCustomAction(this, dup));
ExecCustomAction(dup, null);
e.Handled = true;
};
@@ -2805,9 +2820,7 @@ namespace SourceGit.ViewModels
item.Header = dup.Name;
item.Click += (_, e) =>
{
if (CanCreatePopup())
ShowAndStartPopup(new ExecuteCustomAction(this, dup, branch));
ExecCustomAction(dup, branch);
e.Handled = true;
};