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:
leo 2025-04-21 14:44:02 +08:00
parent 86113701f3
commit 750ca8ec61
No known key found for this signature in database
15 changed files with 158 additions and 164 deletions

View file

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class AIAssistant : ObservableObject
{
public bool IsGenerating
{
get => _isGenerating;
private set => SetProperty(ref _isGenerating, value);
}
public string Text
{
get => _text;
private set => SetProperty(ref _text, value);
}
public AIAssistant(Repository repo, Models.OpenAIService service, List<Models.Change> changes, Action<string> onApply)
{
_repo = repo;
_service = service;
_changes = changes;
_onApply = onApply;
_cancel = new CancellationTokenSource();
Gen();
}
public void Regen()
{
if (_cancel is { IsCancellationRequested: false })
_cancel.Cancel();
Gen();
}
public void Apply()
{
_onApply?.Invoke(Text);
}
public void Cancel()
{
_cancel?.Cancel();
}
private void Gen()
{
Text = string.Empty;
IsGenerating = true;
_cancel = new CancellationTokenSource();
Task.Run(() =>
{
new Commands.GenerateCommitMessage(_service, _repo.FullPath, _changes, _cancel.Token, message =>
{
Dispatcher.UIThread.Invoke(() => Text = message);
}).Exec();
Dispatcher.UIThread.Invoke(() => IsGenerating = false);
}, _cancel.Token);
}
private readonly Repository _repo = null;
private Models.OpenAIService _service = null;
private List<Models.Change> _changes = null;
private Action<string> _onApply = null;
private CancellationTokenSource _cancel = null;
private bool _isGenerating = false;
private string _text = string.Empty;
}
}

View file

@ -332,8 +332,7 @@ namespace SourceGit.ViewModels
history.Icon = App.CreateMenuIcon("Icons.Histories");
history.Click += (_, ev) =>
{
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, change.Path, _commit.SHA) };
window.Show();
App.ShowWindow(new FileHistories(_repo, change.Path, _commit.SHA), false);
ev.Handled = true;
};
@ -343,8 +342,7 @@ namespace SourceGit.ViewModels
blame.IsEnabled = change.Index != Models.ChangeState.Deleted;
blame.Click += (_, ev) =>
{
var window = new Views.Blame() { DataContext = new Blame(_repo.FullPath, change.Path, _commit.SHA) };
window.Show();
App.ShowWindow(new Blame(_repo.FullPath, change.Path, _commit.SHA), false);
ev.Handled = true;
};
@ -508,8 +506,7 @@ namespace SourceGit.ViewModels
history.Icon = App.CreateMenuIcon("Icons.Histories");
history.Click += (_, ev) =>
{
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, file.Path, _commit.SHA) };
window.Show();
App.ShowWindow(new FileHistories(_repo, file.Path, _commit.SHA), false);
ev.Handled = true;
};
@ -519,8 +516,7 @@ namespace SourceGit.ViewModels
blame.IsEnabled = file.Type == Models.ObjectType.Blob;
blame.Click += (_, ev) =>
{
var window = new Views.Blame() { DataContext = new Blame(_repo.FullPath, file.Path, _commit.SHA) };
window.Show();
App.ShowWindow(new Blame(_repo.FullPath, file.Path, _commit.SHA), false);
ev.Handled = true;
};

View file

@ -570,11 +570,7 @@ namespace SourceGit.ViewModels
return;
}
App.OpenDialog(new Views.InteractiveRebase()
{
DataContext = new InteractiveRebase(_repo, current, commit)
});
App.ShowWindow(new InteractiveRebase(_repo, current, commit), true);
e.Handled = true;
};

View file

@ -380,7 +380,7 @@ namespace SourceGit.ViewModels
configure.Header = App.Text("Workspace.Configure");
configure.Click += (_, e) =>
{
App.OpenDialog(new Views.ConfigureWorkspace() { DataContext = new ConfigureWorkspace() });
App.ShowWindow(new ConfigureWorkspace(), true);
e.Handled = true;
};
menu.Items.Add(configure);

View file

@ -1405,8 +1405,7 @@ namespace SourceGit.ViewModels
{
locks.Click += (_, e) =>
{
var dialog = new Views.LFSLocks() { DataContext = new LFSLocks(this, _remotes[0].Name) };
App.OpenDialog(dialog);
App.ShowWindow(new LFSLocks(this, _remotes[0].Name), true);
e.Handled = true;
};
}
@ -1419,8 +1418,7 @@ namespace SourceGit.ViewModels
lockRemote.Header = remoteName;
lockRemote.Click += (_, e) =>
{
var dialog = new Views.LFSLocks() { DataContext = new LFSLocks(this, remoteName) };
App.OpenDialog(dialog);
App.ShowWindow(new LFSLocks(this, remoteName), true);
e.Handled = true;
};
locks.Items.Add(lockRemote);
@ -1706,10 +1704,7 @@ namespace SourceGit.ViewModels
compareWithHead.Icon = App.CreateMenuIcon("Icons.Compare");
compareWithHead.Click += (_, _) =>
{
App.OpenDialog(new Views.BranchCompare()
{
DataContext = new BranchCompare(_fullpath, branch, _currentBranch)
});
App.ShowWindow(new BranchCompare(_fullpath, branch, _currentBranch), false);
};
menu.Items.Add(new MenuItem() { Header = "-" });
menu.Items.Add(compareWithHead);
@ -1989,10 +1984,7 @@ namespace SourceGit.ViewModels
compareWithHead.Icon = App.CreateMenuIcon("Icons.Compare");
compareWithHead.Click += (_, _) =>
{
App.OpenDialog(new Views.BranchCompare()
{
DataContext = new BranchCompare(_fullpath, branch, _currentBranch)
});
App.ShowWindow(new BranchCompare(_fullpath, branch, _currentBranch), false);
};
menu.Items.Add(compareWithHead);

View file

@ -323,10 +323,7 @@ namespace SourceGit.ViewModels
public void OpenAssumeUnchanged()
{
App.OpenDialog(new Views.AssumeUnchangedManager()
{
DataContext = new AssumeUnchangedManager(_repo)
});
App.ShowWindow(new AssumeUnchangedManager(_repo), true);
}
public void StashAll(bool autoStart)
@ -726,8 +723,7 @@ namespace SourceGit.ViewModels
history.Icon = App.CreateMenuIcon("Icons.Histories");
history.Click += (_, e) =>
{
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, change.Path) };
window.Show();
App.ShowWindow(new FileHistories(_repo, change.Path), false);
e.Handled = true;
};
@ -1093,8 +1089,7 @@ namespace SourceGit.ViewModels
{
ai.Click += (_, e) =>
{
var dialog = new Views.AIAssistant(services[0], _repo.FullPath, this, _selectedStaged);
App.OpenDialog(dialog);
App.ShowWindow(new AIAssistant(_repo, services[0], _selectedStaged, t => CommitMessage = t), true);
e.Handled = true;
};
}
@ -1108,8 +1103,7 @@ namespace SourceGit.ViewModels
item.Header = service.Name;
item.Click += (_, e) =>
{
var dialog = new Views.AIAssistant(dup, _repo.FullPath, this, _selectedStaged);
App.OpenDialog(dialog);
App.ShowWindow(new AIAssistant(_repo, dup, _selectedStaged, t => CommitMessage = t), true);
e.Handled = true;
};
@ -1193,8 +1187,7 @@ namespace SourceGit.ViewModels
history.Icon = App.CreateMenuIcon("Icons.Histories");
history.Click += (_, e) =>
{
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, change.Path) };
window.Show();
App.ShowWindow(new FileHistories(_repo, change.Path), false);
e.Handled = true;
};
@ -1490,8 +1483,7 @@ namespace SourceGit.ViewModels
if (services.Count == 1)
{
var dialog = new Views.AIAssistant(services[0], _repo.FullPath, this, _staged);
App.OpenDialog(dialog);
App.ShowWindow(new AIAssistant(_repo, services[0], _staged, t => CommitMessage = t), true);
return null;
}
@ -1503,8 +1495,7 @@ namespace SourceGit.ViewModels
item.Header = service.Name;
item.Click += (_, e) =>
{
var dialog = new Views.AIAssistant(dup, _repo.FullPath, this, _staged);
App.OpenDialog(dialog);
App.ShowWindow(new AIAssistant(_repo, dup, _staged, t => CommitMessage = t), true);
e.Handled = true;
};
@ -1705,14 +1696,7 @@ namespace SourceGit.ViewModels
if (!string.IsNullOrEmpty(_filter) && _staged.Count > _visibleStaged.Count && !confirmWithFilter)
{
var confirmMessage = App.Text("WorkingCopy.ConfirmCommitWithFilter", _staged.Count, _visibleStaged.Count, _staged.Count - _visibleStaged.Count);
App.OpenDialog(new Views.ConfirmCommit()
{
DataContext = new ConfirmCommit(confirmMessage, () =>
{
DoCommit(autoStage, autoPush, allowEmpty, true);
})
});
App.ShowWindow(new ConfirmCommit(confirmMessage, () => DoCommit(autoStage, autoPush, allowEmpty, true)), true);
return;
}
@ -1720,14 +1704,7 @@ namespace SourceGit.ViewModels
{
if ((autoStage && _count == 0) || (!autoStage && _staged.Count == 0))
{
App.OpenDialog(new Views.ConfirmEmptyCommit()
{
DataContext = new ConfirmEmptyCommit(_count > 0, stageAll =>
{
DoCommit(stageAll, autoPush, true, confirmWithFilter);
})
});
App.ShowWindow(new ConfirmEmptyCommit(_count > 0, stageAll => DoCommit(stageAll, autoPush, true, confirmWithFilter)), true);
return;
}
}