mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-24 05:35:00 +00:00
refactor: use custom view locator to create new window/dialog (#1216)
Signed-off-by: leo <longshuang@msn.cn> (cherry picked from commit 3e6f2b25f15b263e2b84922abc5cf6d621d62a83)
This commit is contained in:
parent
86113701f3
commit
750ca8ec61
15 changed files with 158 additions and 164 deletions
|
@ -7,6 +7,7 @@
|
|||
xmlns:c="using:SourceGit.Converters"
|
||||
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="120"
|
||||
x:Class="SourceGit.Views.AIAssistant"
|
||||
x:DataType="vm:AIAssistant"
|
||||
x:Name="ThisControl"
|
||||
Icon="/App.ico"
|
||||
Title="{DynamicResource Text.AIAssistant}"
|
||||
|
@ -48,20 +49,22 @@
|
|||
<!-- Options -->
|
||||
<Border Grid.Row="2" Margin="0,0,0,8">
|
||||
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
|
||||
<v:LoadingIcon x:Name="IconInProgress" Width="14" Height="14" Margin="0,0,8,0"/>
|
||||
<v:LoadingIcon Width="14" Height="14"
|
||||
Margin="0,0,8,0"
|
||||
IsVisible="{Binding IsGenerating}"/>
|
||||
<Button Classes="flat"
|
||||
x:Name="BtnGenerateCommitMessage"
|
||||
Height="28"
|
||||
Margin="0,0,8,0"
|
||||
Padding="12,0"
|
||||
Content="{DynamicResource Text.AIAssistant.Use}"
|
||||
Click="OnGenerateCommitMessage"/>
|
||||
IsEnabled="{Binding !IsGenerating}"
|
||||
Click="OnApply"/>
|
||||
<Button Classes="flat"
|
||||
x:Name="BtnRegenerate"
|
||||
Height="28"
|
||||
Padding="12,0"
|
||||
Content="{DynamicResource Text.AIAssistant.Regen}"
|
||||
Click="OnRegen"/>
|
||||
IsEnabled="{Binding !IsGenerating}"
|
||||
Command="{Binding Regen}"/>
|
||||
</StackPanel>
|
||||
</Border>
|
||||
</Grid>
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Threading;
|
||||
|
||||
using AvaloniaEdit;
|
||||
using AvaloniaEdit.Document;
|
||||
using AvaloniaEdit.Editing;
|
||||
|
@ -101,76 +99,16 @@ namespace SourceGit.Views
|
|||
InitializeComponent();
|
||||
}
|
||||
|
||||
public AIAssistant(Models.OpenAIService service, string repo, ViewModels.WorkingCopy wc, List<Models.Change> changes)
|
||||
{
|
||||
_service = service;
|
||||
_repo = repo;
|
||||
_wc = wc;
|
||||
_changes = changes;
|
||||
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
protected override void OnOpened(EventArgs e)
|
||||
{
|
||||
base.OnOpened(e);
|
||||
Generate();
|
||||
}
|
||||
|
||||
protected override void OnClosing(WindowClosingEventArgs e)
|
||||
{
|
||||
base.OnClosing(e);
|
||||
_cancel?.Cancel();
|
||||
(DataContext as ViewModels.AIAssistant)?.Cancel();
|
||||
}
|
||||
|
||||
private void OnGenerateCommitMessage(object sender, RoutedEventArgs e)
|
||||
private void OnApply(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_wc != null)
|
||||
_wc.CommitMessage = TxtResponse.Text;
|
||||
|
||||
(DataContext as ViewModels.AIAssistant)?.Apply();
|
||||
Close();
|
||||
}
|
||||
|
||||
private void OnRegen(object sender, RoutedEventArgs e)
|
||||
{
|
||||
TxtResponse.Text = string.Empty;
|
||||
Generate();
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void Generate()
|
||||
{
|
||||
if (_repo == null)
|
||||
return;
|
||||
|
||||
IconInProgress.IsVisible = true;
|
||||
BtnGenerateCommitMessage.IsEnabled = false;
|
||||
BtnRegenerate.IsEnabled = false;
|
||||
|
||||
_cancel = new CancellationTokenSource();
|
||||
Task.Run(() =>
|
||||
{
|
||||
new Commands.GenerateCommitMessage(_service, _repo, _changes, _cancel.Token, message =>
|
||||
{
|
||||
Dispatcher.UIThread.Invoke(() => TxtResponse.Text = message);
|
||||
}).Exec();
|
||||
|
||||
if (!_cancel.IsCancellationRequested)
|
||||
{
|
||||
Dispatcher.UIThread.Invoke(() =>
|
||||
{
|
||||
IconInProgress.IsVisible = false;
|
||||
BtnGenerateCommitMessage.IsEnabled = true;
|
||||
BtnRegenerate.IsEnabled = true;
|
||||
});
|
||||
}
|
||||
}, _cancel.Token);
|
||||
}
|
||||
|
||||
private Models.OpenAIService _service;
|
||||
private string _repo;
|
||||
private ViewModels.WorkingCopy _wc;
|
||||
private List<Models.Change> _changes;
|
||||
private CancellationTokenSource _cancel;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -201,12 +201,7 @@ namespace SourceGit.Views
|
|||
|
||||
private void OnOpenConventionalCommitHelper(object _, RoutedEventArgs e)
|
||||
{
|
||||
var dialog = new ConventionalCommitMessageBuilder()
|
||||
{
|
||||
DataContext = new ViewModels.ConventionalCommitMessageBuilder(text => Text = text)
|
||||
};
|
||||
|
||||
App.OpenDialog(dialog);
|
||||
App.ShowWindow(new ViewModels.ConventionalCommitMessageBuilder(text => Text = text), true);
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
|
|
|
@ -136,7 +136,7 @@ namespace SourceGit.Views
|
|||
// Ctrl+Shift+P opens preference dialog (macOS use hotkeys in system menu bar)
|
||||
if (!OperatingSystem.IsMacOS() && e is { KeyModifiers: (KeyModifiers.Control | KeyModifiers.Shift), Key: Key.P })
|
||||
{
|
||||
App.OpenDialog(new Preferences());
|
||||
App.ShowWindow(new Preferences(), true);
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -68,7 +68,7 @@ namespace SourceGit.Views
|
|||
return;
|
||||
}
|
||||
|
||||
var viewTypeName = dataTypeName.Replace("ViewModels", "Views");
|
||||
var viewTypeName = dataTypeName.Replace(".ViewModels.", ".Views.");
|
||||
var viewType = Type.GetType(viewTypeName);
|
||||
if (viewType == null)
|
||||
{
|
||||
|
|
|
@ -349,9 +349,7 @@ namespace SourceGit.Views
|
|||
if (sender is CheckBox box)
|
||||
{
|
||||
ViewModels.Preferences.Instance.UseSystemWindowFrame = box.IsChecked == true;
|
||||
|
||||
var dialog = new ConfirmRestart();
|
||||
App.OpenDialog(dialog);
|
||||
App.ShowWindow(new ConfirmRestart(), true);
|
||||
}
|
||||
|
||||
e.Handled = true;
|
||||
|
|
|
@ -22,22 +22,20 @@ namespace SourceGit.Views
|
|||
}
|
||||
}
|
||||
|
||||
private async void OpenStatistics(object _, RoutedEventArgs e)
|
||||
private void OpenStatistics(object _, RoutedEventArgs e)
|
||||
{
|
||||
if (DataContext is ViewModels.Repository repo && TopLevel.GetTopLevel(this) is Window owner)
|
||||
if (DataContext is ViewModels.Repository repo)
|
||||
{
|
||||
var dialog = new Statistics() { DataContext = new ViewModels.Statistics(repo.FullPath) };
|
||||
await dialog.ShowDialog(owner);
|
||||
App.ShowWindow(new ViewModels.Statistics(repo.FullPath), true);
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
private async void OpenConfigure(object sender, RoutedEventArgs e)
|
||||
private void OpenConfigure(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (DataContext is ViewModels.Repository repo && TopLevel.GetTopLevel(this) is Window owner)
|
||||
if (DataContext is ViewModels.Repository repo)
|
||||
{
|
||||
var dialog = new RepositoryConfigure() { DataContext = new ViewModels.RepositoryConfigure(repo) };
|
||||
await dialog.ShowDialog(owner);
|
||||
App.ShowWindow(new ViewModels.RepositoryConfigure(repo), true);
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
@ -129,12 +127,11 @@ namespace SourceGit.Views
|
|||
e.Handled = true;
|
||||
}
|
||||
|
||||
private async void OpenGitLogs(object sender, RoutedEventArgs e)
|
||||
private void OpenGitLogs(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (DataContext is ViewModels.Repository repo && TopLevel.GetTopLevel(this) is Window owner)
|
||||
if (DataContext is ViewModels.Repository repo)
|
||||
{
|
||||
var dialog = new ViewLogs() { DataContext = new ViewModels.ViewLogs(repo) };
|
||||
await dialog.ShowDialog(owner);
|
||||
App.ShowWindow(new ViewModels.ViewLogs(repo), true);
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue