Skip to content

Commit a6ebc15

Browse files
committed
refactor: show child-window and modal dialog
Signed-off-by: leo <[email protected]>
1 parent 4bfcfb8 commit a6ebc15

29 files changed

+127
-117
lines changed

src/App.Commands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public static bool IsCheckForUpdateCommandVisible
3737
}
3838
}
3939

40-
public static readonly Command OpenPreferencesCommand = new Command(_ => ShowWindow(new Views.Preferences(), false));
41-
public static readonly Command OpenHotkeysCommand = new Command(_ => ShowWindow(new Views.Hotkeys(), false));
40+
public static readonly Command OpenPreferencesCommand = new Command(async _ => await ShowDailog(new Views.Preferences()));
41+
public static readonly Command OpenHotkeysCommand = new Command(async _ => await ShowDailog(new Views.Hotkeys()));
4242
public static readonly Command OpenAppDataDirCommand = new Command(_ => Native.OS.OpenInFileManager(Native.OS.DataDir));
43-
public static readonly Command OpenAboutCommand = new Command(_ => ShowWindow(new Views.About(), false));
43+
public static readonly Command OpenAboutCommand = new Command(async _ => await ShowDailog(new Views.About()));
4444
public static readonly Command CheckForUpdateCommand = new Command(_ => (Current as App)?.Check4Update(true));
4545
public static readonly Command QuitCommand = new Command(_ => Quit(0));
4646
public static readonly Command CopyTextBlockCommand = new Command(async p =>

src/App.axaml.cs

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -106,43 +106,56 @@ public static void LogException(Exception ex)
106106
#endregion
107107

108108
#region Utility Functions
109-
public static void ShowWindow(object data, bool showAsDialog)
109+
public static object CreateViewForViewModel(object data)
110110
{
111-
var impl = (Views.ChromelessWindow target, bool isDialog) =>
111+
var dataTypeName = data.GetType().FullName;
112+
if (string.IsNullOrEmpty(dataTypeName) || !dataTypeName.Contains(".ViewModels.", StringComparison.Ordinal))
113+
return null;
114+
115+
var viewTypeName = dataTypeName.Replace(".ViewModels.", ".Views.");
116+
var viewType = Type.GetType(viewTypeName);
117+
if (viewType != null)
118+
return Activator.CreateInstance(viewType);
119+
120+
return null;
121+
}
122+
123+
public static Task ShowDailog(object data, Window owner = null)
124+
{
125+
if (owner == null)
112126
{
113-
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } owner })
114-
{
115-
if (isDialog)
116-
target.ShowDialog(owner);
117-
else
118-
target.Show(owner);
119-
}
127+
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } mainWindow })
128+
owner = mainWindow;
120129
else
121-
{
122-
target.Show();
123-
}
124-
};
130+
return null;
131+
}
125132

126133
if (data is Views.ChromelessWindow window)
134+
return window.ShowDialog(owner);
135+
136+
window = CreateViewForViewModel(data) as Views.ChromelessWindow;
137+
if (window != null)
127138
{
128-
impl(window, showAsDialog);
129-
return;
139+
window.DataContext = data;
140+
return window.ShowDialog(owner);
130141
}
131142

132-
var dataTypeName = data.GetType().FullName;
133-
if (string.IsNullOrEmpty(dataTypeName) || !dataTypeName.Contains(".ViewModels.", StringComparison.Ordinal))
134-
return;
143+
return null;
144+
}
135145

136-
var viewTypeName = dataTypeName.Replace(".ViewModels.", ".Views.");
137-
var viewType = Type.GetType(viewTypeName);
138-
if (viewType == null || !viewType.IsSubclassOf(typeof(Views.ChromelessWindow)))
146+
public static void ShowWindow(object data)
147+
{
148+
if (data is Views.ChromelessWindow window)
149+
{
150+
window.Show();
139151
return;
152+
}
140153

141-
window = Activator.CreateInstance(viewType) as Views.ChromelessWindow;
154+
window = CreateViewForViewModel(data) as Views.ChromelessWindow;
142155
if (window != null)
143156
{
144157
window.DataContext = data;
145-
impl(window, showAsDialog);
158+
window.Show();
146159
}
147160
}
148161

@@ -651,9 +664,9 @@ private void Check4Update(bool manually = false)
651664

652665
private void ShowSelfUpdateResult(object data)
653666
{
654-
Dispatcher.UIThread.Post(() =>
667+
Dispatcher.UIThread.Post(async () =>
655668
{
656-
ShowWindow(new ViewModels.SelfUpdate() { Data = data }, true);
669+
await ShowDailog(new ViewModels.SelfUpdate { Data = data });
657670
});
658671
}
659672

src/ViewModels/CommitDetail.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public ContextMenu CreateChangeContextMenuByFolder(ChangeTreeNode node, List<Mod
259259
history.Icon = App.CreateMenuIcon("Icons.Histories");
260260
history.Click += (_, ev) =>
261261
{
262-
App.ShowWindow(new DirHistories(_repo, node.FullPath, _commit.SHA), false);
262+
App.ShowWindow(new DirHistories(_repo, node.FullPath, _commit.SHA));
263263
ev.Handled = true;
264264
};
265265

@@ -351,7 +351,7 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
351351
history.Icon = App.CreateMenuIcon("Icons.Histories");
352352
history.Click += (_, ev) =>
353353
{
354-
App.ShowWindow(new FileHistories(_repo, change.Path, _commit.SHA), false);
354+
App.ShowWindow(new FileHistories(_repo, change.Path, _commit.SHA));
355355
ev.Handled = true;
356356
};
357357

@@ -361,7 +361,7 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
361361
blame.IsEnabled = change.Index != Models.ChangeState.Deleted;
362362
blame.Click += (_, ev) =>
363363
{
364-
App.ShowWindow(new Blame(_repo.FullPath, change.Path, _commit), false);
364+
App.ShowWindow(new Blame(_repo.FullPath, change.Path, _commit));
365365
ev.Handled = true;
366366
};
367367

@@ -470,7 +470,7 @@ public ContextMenu CreateRevisionFileContextMenuByFolder(string path)
470470
history.Icon = App.CreateMenuIcon("Icons.Histories");
471471
history.Click += (_, ev) =>
472472
{
473-
App.ShowWindow(new DirHistories(_repo, path, _commit.SHA), false);
473+
App.ShowWindow(new DirHistories(_repo, path, _commit.SHA));
474474
ev.Handled = true;
475475
};
476476

@@ -572,7 +572,7 @@ await Commands.SaveRevisionFile
572572
history.Icon = App.CreateMenuIcon("Icons.Histories");
573573
history.Click += (_, ev) =>
574574
{
575-
App.ShowWindow(new FileHistories(_repo, file.Path, _commit.SHA), false);
575+
App.ShowWindow(new FileHistories(_repo, file.Path, _commit.SHA));
576576
ev.Handled = true;
577577
};
578578

@@ -582,7 +582,7 @@ await Commands.SaveRevisionFile
582582
blame.IsEnabled = file.Type == Models.ObjectType.Blob;
583583
blame.Click += (_, ev) =>
584584
{
585-
App.ShowWindow(new Blame(_repo.FullPath, file.Path, _commit), false);
585+
App.ShowWindow(new Blame(_repo.FullPath, file.Path, _commit));
586586
ev.Handled = true;
587587
};
588588

src/ViewModels/Histories.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,15 +645,15 @@ public ContextMenu MakeContextMenu(DataGrid list)
645645
var interactiveRebase = new MenuItem();
646646
interactiveRebase.Header = App.Text("CommitCM.InteractiveRebase", current.Name);
647647
interactiveRebase.Icon = App.CreateMenuIcon("Icons.InteractiveRebase");
648-
interactiveRebase.Click += (_, e) =>
648+
interactiveRebase.Click += async (_, e) =>
649649
{
650650
if (_repo.LocalChangesCount > 0)
651651
{
652652
App.RaiseException(_repo.FullPath, "You have local changes. Please run stash or discard first.");
653653
return;
654654
}
655655

656-
App.ShowWindow(new InteractiveRebase(_repo, current, commit), true);
656+
await App.ShowDailog(new InteractiveRebase(_repo, current, commit));
657657
e.Handled = true;
658658
};
659659

src/ViewModels/Launcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,9 @@ public ContextMenu CreateContextForWorkspace()
450450

451451
var configure = new MenuItem();
452452
configure.Header = App.Text("Workspace.Configure");
453-
configure.Click += (_, e) =>
453+
configure.Click += async (_, e) =>
454454
{
455-
App.ShowWindow(new ConfigureWorkspace(), true);
455+
await App.ShowDailog(new ConfigureWorkspace());
456456
e.Handled = true;
457457
};
458458
menu.Items.Add(configure);

src/ViewModels/Repository.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,9 +1666,9 @@ public ContextMenu CreateContextMenuForGitLFS()
16661666
locks.IsEnabled = _remotes.Count > 0;
16671667
if (_remotes.Count == 1)
16681668
{
1669-
locks.Click += (_, e) =>
1669+
locks.Click += async (_, e) =>
16701670
{
1671-
App.ShowWindow(new LFSLocks(this, _remotes[0].Name), true);
1671+
await App.ShowDailog(new LFSLocks(this, _remotes[0].Name));
16721672
e.Handled = true;
16731673
};
16741674
}
@@ -1679,9 +1679,9 @@ public ContextMenu CreateContextMenuForGitLFS()
16791679
var remoteName = remote.Name;
16801680
var lockRemote = new MenuItem();
16811681
lockRemote.Header = remoteName;
1682-
lockRemote.Click += (_, e) =>
1682+
lockRemote.Click += async (_, e) =>
16831683
{
1684-
App.ShowWindow(new LFSLocks(this, remoteName), true);
1684+
await App.ShowDailog(new LFSLocks(this, remoteName));
16851685
e.Handled = true;
16861686
};
16871687
locks.Items.Add(lockRemote);
@@ -1984,7 +1984,7 @@ public ContextMenu CreateContextMenuForLocalBranch(Models.Branch branch)
19841984
compareWithCurrent.Icon = App.CreateMenuIcon("Icons.Compare");
19851985
compareWithCurrent.Click += (_, _) =>
19861986
{
1987-
App.ShowWindow(new BranchCompare(_fullpath, branch, _currentBranch), false);
1987+
App.ShowWindow(new BranchCompare(_fullpath, branch, _currentBranch));
19881988
};
19891989
menu.Items.Add(new MenuItem() { Header = "-" });
19901990
menu.Items.Add(compareWithCurrent);
@@ -2264,7 +2264,7 @@ public ContextMenu CreateContextMenuForRemoteBranch(Models.Branch branch)
22642264
compareWithHead.Icon = App.CreateMenuIcon("Icons.Compare");
22652265
compareWithHead.Click += (_, _) =>
22662266
{
2267-
App.ShowWindow(new BranchCompare(_fullpath, branch, _currentBranch), false);
2267+
App.ShowWindow(new BranchCompare(_fullpath, branch, _currentBranch));
22682268
};
22692269
menu.Items.Add(compareWithHead);
22702270

src/ViewModels/WorkingCopy.cs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,6 @@ public void OpenExternalMergeToolAllConflicts()
334334
UseExternalMergeTool(null);
335335
}
336336

337-
public void OpenAssumeUnchanged()
338-
{
339-
App.ShowWindow(new AssumeUnchangedManager(_repo), true);
340-
}
341-
342337
public void StashAll(bool autoStart)
343338
{
344339
if (!_repo.CanCreatePopup())
@@ -973,9 +968,9 @@ public ContextMenu CreateContextMenuForUnstagedChanges(string selectedSingleFold
973968
history.Click += (_, e) =>
974969
{
975970
if (hasSelectedFolder)
976-
App.ShowWindow(new DirHistories(_repo, selectedSingleFolder), false);
971+
App.ShowWindow(new DirHistories(_repo, selectedSingleFolder));
977972
else
978-
App.ShowWindow(new FileHistories(_repo, change.Path), false);
973+
App.ShowWindow(new FileHistories(_repo, change.Path));
979974

980975
e.Handled = true;
981976
};
@@ -1166,7 +1161,7 @@ public ContextMenu CreateContextMenuForUnstagedChanges(string selectedSingleFold
11661161
history.Icon = App.CreateMenuIcon("Icons.Histories");
11671162
history.Click += (_, e) =>
11681163
{
1169-
App.ShowWindow(new DirHistories(_repo, selectedSingleFolder), false);
1164+
App.ShowWindow(new DirHistories(_repo, selectedSingleFolder));
11701165
e.Handled = true;
11711166
};
11721167
menu.Items.Add(new MenuItem() { Header = "-" });
@@ -1215,9 +1210,9 @@ public ContextMenu CreateContextMenuForStagedChanges(string selectedSingleFolder
12151210

12161211
if (services.Count == 1)
12171212
{
1218-
ai.Click += (_, e) =>
1213+
ai.Click += async (_, e) =>
12191214
{
1220-
App.ShowWindow(new AIAssistant(_repo, services[0], _selectedStaged, t => CommitMessage = t), true);
1215+
await App.ShowDailog(new AIAssistant(_repo, services[0], _selectedStaged, t => CommitMessage = t));
12211216
e.Handled = true;
12221217
};
12231218
}
@@ -1229,9 +1224,9 @@ public ContextMenu CreateContextMenuForStagedChanges(string selectedSingleFolder
12291224

12301225
var item = new MenuItem();
12311226
item.Header = service.Name;
1232-
item.Click += (_, e) =>
1227+
item.Click += async (_, e) =>
12331228
{
1234-
App.ShowWindow(new AIAssistant(_repo, dup, _selectedStaged, t => CommitMessage = t), true);
1229+
await App.ShowDailog(new AIAssistant(_repo, dup, _selectedStaged, t => CommitMessage = t));
12351230
e.Handled = true;
12361231
};
12371232

@@ -1421,9 +1416,9 @@ public ContextMenu CreateContextMenuForStagedChanges(string selectedSingleFolder
14211416
history.Click += (_, e) =>
14221417
{
14231418
if (hasSelectedFolder)
1424-
App.ShowWindow(new DirHistories(_repo, selectedSingleFolder), false);
1419+
App.ShowWindow(new DirHistories(_repo, selectedSingleFolder));
14251420
else
1426-
App.ShowWindow(new FileHistories(_repo, change.Path), false);
1421+
App.ShowWindow(new FileHistories(_repo, change.Path));
14271422
e.Handled = true;
14281423
};
14291424

@@ -1532,7 +1527,7 @@ public ContextMenu CreateContextMenuForStagedChanges(string selectedSingleFolder
15321527
history.Icon = App.CreateMenuIcon("Icons.Histories");
15331528
history.Click += (_, e) =>
15341529
{
1535-
App.ShowWindow(new DirHistories(_repo, selectedSingleFolder), false);
1530+
App.ShowWindow(new DirHistories(_repo, selectedSingleFolder));
15361531
e.Handled = true;
15371532
};
15381533

@@ -1672,7 +1667,7 @@ public ContextMenu CreateContextForOpenAI()
16721667

16731668
if (services.Count == 1)
16741669
{
1675-
App.ShowWindow(new AIAssistant(_repo, services[0], _staged, t => CommitMessage = t), true);
1670+
_ = App.ShowDailog(new AIAssistant(_repo, services[0], _staged, t => CommitMessage = t));
16761671
return null;
16771672
}
16781673

@@ -1682,9 +1677,9 @@ public ContextMenu CreateContextForOpenAI()
16821677
var dup = service;
16831678
var item = new MenuItem();
16841679
item.Header = service.Name;
1685-
item.Click += (_, e) =>
1680+
item.Click += async (_, e) =>
16861681
{
1687-
App.ShowWindow(new AIAssistant(_repo, dup, _staged, t => CommitMessage = t), true);
1682+
await App.ShowDailog(new AIAssistant(_repo, dup, _staged, t => CommitMessage = t));
16881683
e.Handled = true;
16891684
};
16901685

@@ -1902,7 +1897,7 @@ private void DoCommit(bool autoStage, bool autoPush, CommitCheckPassed checkPass
19021897
{
19031898
if ((!autoStage && _staged.Count == 0) || (autoStage && _cached.Count == 0))
19041899
{
1905-
App.ShowWindow(new ConfirmEmptyCommit(_cached.Count > 0, stageAll => DoCommit(stageAll, autoPush, CommitCheckPassed.FileCount)), true);
1900+
_ = App.ShowDailog(new ConfirmEmptyCommit(_cached.Count > 0, stageAll => DoCommit(stageAll, autoPush, CommitCheckPassed.FileCount)));
19061901
return;
19071902
}
19081903
}

src/Views/AIAssistant.axaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public partial class AIAssistant : ChromelessWindow
115115
{
116116
public AIAssistant()
117117
{
118+
CloseOnESC = true;
118119
InitializeComponent();
119120
}
120121

src/Views/About.axaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public partial class About : ChromelessWindow
77
{
88
public About()
99
{
10+
CloseOnESC = true;
1011
InitializeComponent();
1112

1213
var assembly = Assembly.GetExecutingAssembly();

src/Views/AssumeUnchangedManager.axaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public partial class AssumeUnchangedManager : ChromelessWindow
77
{
88
public AssumeUnchangedManager()
99
{
10+
CloseOnESC = true;
1011
InitializeComponent();
1112
}
1213

0 commit comments

Comments
 (0)