From bc0f86b4bab85a0211be39d08e57826095e9b842 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 26 Mar 2026 16:19:50 +0800 Subject: [PATCH] feature: add `Use` context menu entry to apply selected text as commit message Signed-off-by: leo --- src/Resources/Locales/en_US.axaml | 1 + src/Resources/Locales/zh_CN.axaml | 1 + src/Resources/Locales/zh_TW.axaml | 1 + src/ViewModels/AIAssistant.cs | 11 ++++++++--- src/Views/AIAssistant.axaml.cs | 24 +++++++++++++----------- src/Views/CommitMessageToolBox.axaml.cs | 4 ++-- src/Views/WorkingCopy.axaml.cs | 4 ++-- 7 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index 4413dd35..ee25ba33 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -20,6 +20,7 @@ AI Assistant RE-GENERATE Use AI to generate commit message + Use Hide SourceGit Show All Patch diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index a87f8f65..d7342a5d 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -24,6 +24,7 @@ AI助手 重新生成 使用AI助手生成提交信息 + 应用所选 隐藏 SourceGit 显示所有窗口 应用补丁(apply) diff --git a/src/Resources/Locales/zh_TW.axaml b/src/Resources/Locales/zh_TW.axaml index 49e30349..6561521a 100644 --- a/src/Resources/Locales/zh_TW.axaml +++ b/src/Resources/Locales/zh_TW.axaml @@ -24,6 +24,7 @@ AI 助理 重新產生 使用 AI 產生提交訊息 + 套用選取 隱藏 SourceGit 顯示所有 套用修補檔 (apply patch) diff --git a/src/ViewModels/AIAssistant.cs b/src/ViewModels/AIAssistant.cs index d6eff19d..032572d0 100644 --- a/src/ViewModels/AIAssistant.cs +++ b/src/ViewModels/AIAssistant.cs @@ -22,7 +22,7 @@ namespace SourceGit.ViewModels private set => SetProperty(ref _text, value); } - public AIAssistant(string repo, AI.Service service, List changes) + public AIAssistant(Repository repo, AI.Service service, List changes) { _repo = repo; _service = service; @@ -49,7 +49,7 @@ namespace SourceGit.ViewModels try { - await agent.GenerateCommitMessageAsync(_repo, _changeList, message => + await agent.GenerateCommitMessageAsync(_repo.FullPath, _changeList, message => { builder.AppendLine(message); Dispatcher.UIThread.Post(() => Text = builder.ToString()); @@ -72,6 +72,11 @@ namespace SourceGit.ViewModels IsGenerating = false; } + public void Use(string text) + { + _repo.SetCommitMessage(text); + } + public void Cancel() { _cancel?.Cancel(); @@ -98,7 +103,7 @@ namespace SourceGit.ViewModels builder.Append(c.Path).AppendLine(); } - private readonly string _repo = null; + private readonly Repository _repo = null; private readonly AI.Service _service = null; private readonly string _changeList = null; private CancellationTokenSource _cancel = null; diff --git a/src/Views/AIAssistant.axaml.cs b/src/Views/AIAssistant.axaml.cs index d3fc8633..eee2b3cb 100644 --- a/src/Views/AIAssistant.axaml.cs +++ b/src/Views/AIAssistant.axaml.cs @@ -93,29 +93,31 @@ namespace SourceGit.Views private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e) { + if (DataContext is not ViewModels.AIAssistant vm) + return; + var selected = SelectedText; if (string.IsNullOrEmpty(selected)) return; + var apply = new MenuItem() { Header = App.Text("AIAssistant.Use") }; + apply.Icon = App.CreateMenuIcon("Icons.Check"); + apply.Click += (_, ev) => + { + vm.Use(selected); + ev.Handled = true; + }; + var copy = new MenuItem() { Header = App.Text("Copy") }; + copy.Icon = App.CreateMenuIcon("Icons.Copy"); copy.Click += async (_, ev) => { await App.CopyTextAsync(selected); ev.Handled = true; }; - if (this.FindResource("Icons.Copy") is Geometry geo) - { - copy.Icon = new Avalonia.Controls.Shapes.Path() - { - Width = 10, - Height = 10, - Stretch = Stretch.Uniform, - Data = geo, - }; - } - var menu = new ContextMenu(); + menu.Items.Add(apply); menu.Items.Add(copy); menu.Open(TextArea.TextView); diff --git a/src/Views/CommitMessageToolBox.axaml.cs b/src/Views/CommitMessageToolBox.axaml.cs index 8b72b9b3..ca448807 100644 --- a/src/Views/CommitMessageToolBox.axaml.cs +++ b/src/Views/CommitMessageToolBox.axaml.cs @@ -588,7 +588,7 @@ namespace SourceGit.Views if (services.Count == 1) { - await App.ShowDialog(new ViewModels.AIAssistant(repo.FullPath, services[0], vm.Staged)); + await App.ShowDialog(new ViewModels.AIAssistant(repo, services[0], vm.Staged)); e.Handled = true; return; } @@ -601,7 +601,7 @@ namespace SourceGit.Views item.Header = service.Name; item.Click += async (_, ev) => { - await App.ShowDialog(new ViewModels.AIAssistant(repo.FullPath, dup, vm.Staged)); + await App.ShowDialog(new ViewModels.AIAssistant(repo, dup, vm.Staged)); ev.Handled = true; }; diff --git a/src/Views/WorkingCopy.axaml.cs b/src/Views/WorkingCopy.axaml.cs index c31c5a59..5cd1012c 100644 --- a/src/Views/WorkingCopy.axaml.cs +++ b/src/Views/WorkingCopy.axaml.cs @@ -929,7 +929,7 @@ namespace SourceGit.Views { ai.Click += async (_, e) => { - await App.ShowDialog(new ViewModels.AIAssistant(repo.FullPath, services[0], selectedStaged)); + await App.ShowDialog(new ViewModels.AIAssistant(repo, services[0], selectedStaged)); e.Handled = true; }; } @@ -943,7 +943,7 @@ namespace SourceGit.Views item.Header = service.Name; item.Click += async (_, e) => { - await App.ShowDialog(new ViewModels.AIAssistant(repo.FullPath, dup, selectedStaged)); + await App.ShowDialog(new ViewModels.AIAssistant(repo, dup, selectedStaged)); e.Handled = true; };