diff --git a/src/ViewModels/WorkingCopy.cs b/src/ViewModels/WorkingCopy.cs index 10ea126b..6fcbedd9 100644 --- a/src/ViewModels/WorkingCopy.cs +++ b/src/ViewModels/WorkingCopy.cs @@ -33,11 +33,6 @@ namespace SourceGit.ViewModels public class WorkingCopy : ObservableObject { - public string RepoPath - { - get => _repo.FullPath; - } - public bool IncludeUntracked { get => _repo.IncludeUntracked; @@ -408,6 +403,25 @@ namespace SourceGit.ViewModels } } + public void GenerateCommitMessageByAI() + { + if (!Models.OpenAI.IsValid) + { + App.RaiseException(_repo.FullPath, "Bad configuration for OpenAI"); + return; + } + + if (_staged is { Count: > 0 }) + { + var dialog = new Views.AIAssistant(_repo.FullPath, _staged, generated => CommitMessage = generated); + App.OpenDialog(dialog); + } + else + { + App.RaiseException(_repo.FullPath, "No files added to commit!"); + } + } + public void Commit() { DoCommit(false, false, false); diff --git a/src/Views/AIAssistant.axaml b/src/Views/AIAssistant.axaml index 7cbad86d..1c266715 100644 --- a/src/Views/AIAssistant.axaml +++ b/src/Views/AIAssistant.axaml @@ -7,7 +7,6 @@ xmlns:c="using:SourceGit.Converters" mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="120" x:Class="SourceGit.Views.AIAssistant" - x:DataType="vm:WorkingCopy" x:Name="ThisControl" Icon="/App.ico" Title="{DynamicResource Text.AIAssistant}" @@ -55,6 +54,7 @@ Margin="16" FontSize="{Binding Source={x:Static vm:Preference.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}" HorizontalAlignment="Center" + Text="Generating commit message... Please wait!" TextTrimming="CharacterEllipsis"/> diff --git a/src/Views/AIAssistant.axaml.cs b/src/Views/AIAssistant.axaml.cs index 5e62c273..6ceb5610 100644 --- a/src/Views/AIAssistant.axaml.cs +++ b/src/Views/AIAssistant.axaml.cs @@ -1,3 +1,5 @@ +using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -13,28 +15,36 @@ namespace SourceGit.Views { _cancel = new CancellationTokenSource(); InitializeComponent(); - ProgressMessage.Text = "Generating commit message... Please wait!"; } - public void GenerateCommitMessage() + public AIAssistant(string repo, List changes, Action onDone) { - if (DataContext is ViewModels.WorkingCopy vm) + _repo = repo; + _changes = changes; + _onDone = onDone; + _cancel = new CancellationTokenSource(); + InitializeComponent(); + } + + protected override void OnOpened(EventArgs e) + { + base.OnOpened(e); + + if (string.IsNullOrEmpty(_repo)) + return; + + Task.Run(() => { - Task.Run(() => + var message = new Commands.GenerateCommitMessage(_repo, _changes, _cancel.Token, SetDescription).Result(); + if (_cancel.IsCancellationRequested) + return; + + Dispatcher.UIThread.Invoke(() => { - var message = new Commands.GenerateCommitMessage(vm.RepoPath, vm.Staged, _cancel.Token, SetDescription).Result(); - if (_cancel.IsCancellationRequested) - return; - - Dispatcher.UIThread.Invoke(() => - { - if (DataContext is ViewModels.WorkingCopy wc) - wc.CommitMessage = message; - - Close(); - }); - }, _cancel.Token); - } + _onDone?.Invoke(message); + Close(); + }); + }, _cancel.Token); } protected override void OnClosing(WindowClosingEventArgs e) @@ -50,12 +60,12 @@ namespace SourceGit.Views private void SetDescription(string message) { - Dispatcher.UIThread.Invoke(() => - { - ProgressMessage.Text = message; - }); + Dispatcher.UIThread.Invoke(() => ProgressMessage.Text = message); } + private string _repo; + private List _changes; + private Action _onDone; private CancellationTokenSource _cancel; } } diff --git a/src/Views/WorkingCopy.axaml b/src/Views/WorkingCopy.axaml index 3333cda1..3d080fc6 100644 --- a/src/Views/WorkingCopy.axaml +++ b/src/Views/WorkingCopy.axaml @@ -199,7 +199,7 @@