Skip to content

Commit c2d339a

Browse files
refactor: Add secondary window support in Launcher class
- Introduced `IsSecondaryWindow` property and `OnRequestClose` event in the `Launcher` view model. - Updated the constructor to accept a boolean for secondary window status. - Modified `Quit` method to handle close requests for secondary windows. - Refactored `MoveToNewWindow` and `CopyToNewWindow` methods to streamline window creation. - Added `RegisterOnRequestClose` method in the `Launcher` view to manage close requests. - Updated `using` directives in `Launcher.axaml.cs` to include `Avalonia.Threading`.
1 parent d61246d commit c2d339a

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/ViewModels/Launcher.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ public IDisposable Switcher
5050
private set => SetProperty(ref _switcher, value);
5151
}
5252

53-
public Launcher(string startupRepo)
53+
public bool IsSecondaryWindow
54+
{
55+
get => _isSecondaryWindow;
56+
set => SetProperty(ref _isSecondaryWindow, value);
57+
}
58+
59+
public event Action OnRequestClose;
60+
61+
public Launcher(string startupRepo, bool isSecondaryWindow = false)
5462
{
5563
_ignoreIndexChange = true;
5664

@@ -116,6 +124,8 @@ public Launcher(string startupRepo)
116124

117125
if (string.IsNullOrEmpty(_title))
118126
UpdateTitle();
127+
128+
IsSecondaryWindow = isSecondaryWindow;
119129
}
120130

121131
public void Quit()
@@ -273,7 +283,10 @@ public void CloseTab(LauncherPage page)
273283
}
274284
else
275285
{
276-
App.Quit(0);
286+
if (IsSecondaryWindow)
287+
OnRequestClose?.Invoke();
288+
else
289+
App.Quit(0);
277290
}
278291

279292
return;
@@ -554,15 +567,16 @@ public ContextMenu CreateContextForPageTab(LauncherPage page)
554567

555568
public void MoveToNewWindow(LauncherPage page)
556569
{
570+
NewWindow(page);
557571
CloseTab(page);
558-
559-
var newWindow = new Views.Launcher() { DataContext = new Launcher(page.Node.Id) };
560-
newWindow.Show();
561572
}
562573

563-
public void CopyToNewWindow(LauncherPage page)
574+
public void CopyToNewWindow(LauncherPage page) => NewWindow(page);
575+
576+
private void NewWindow(LauncherPage page)
564577
{
565-
var newWindow = new Views.Launcher() { DataContext = new Launcher(page.Node.Id) };
578+
var newWindow = new Views.Launcher() { DataContext = new Launcher(page.Node.Id, true) };
579+
newWindow.RegisterOnRequestClose();
566580
newWindow.Show();
567581
}
568582

@@ -643,5 +657,6 @@ private void UpdateTitle()
643657
private bool _ignoreIndexChange = false;
644658
private string _title = string.Empty;
645659
private IDisposable _switcher = null;
660+
private bool _isSecondaryWindow = false;
646661
}
647662
}

src/Views/Launcher.axaml.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Avalonia.Input;
66
using Avalonia.Interactivity;
77
using Avalonia.Platform;
8+
using Avalonia.Threading;
89
using Avalonia.VisualTree;
910

1011
namespace SourceGit.Views
@@ -85,6 +86,12 @@ public Launcher()
8586
WindowStartupLocation = WindowStartupLocation.CenterScreen;
8687
}
8788

89+
public void RegisterOnRequestClose()
90+
{
91+
if (DataContext is ViewModels.Launcher vm)
92+
vm.OnRequestClose += () => Dispatcher.UIThread.Post(Close);
93+
}
94+
8895
public void BringToTop()
8996
{
9097
if (WindowState == WindowState.Minimized)

0 commit comments

Comments
 (0)