Skip to content

Commit c4c04b8

Browse files
committed
enhance: bring window into view after receive IPC message
Signed-off-by: leo <[email protected]>
1 parent e2da44c commit c4c04b8

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

src/App.axaml.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -510,19 +510,30 @@ private void TryLaunchAsNormal(IClassicDesktopStyleApplicationLifetime desktop)
510510

511511
private void TryOpenRepository(string repo)
512512
{
513-
if (string.IsNullOrEmpty(repo) || !Directory.Exists(repo))
514-
return;
515-
516-
var test = new Commands.QueryRepositoryRootPath(repo).ReadToEnd();
517-
if (test.IsSuccess && !string.IsNullOrEmpty(test.StdOut))
513+
if (!string.IsNullOrEmpty(repo) && Directory.Exists(repo))
518514
{
519-
Dispatcher.UIThread.Invoke(() =>
515+
var test = new Commands.QueryRepositoryRootPath(repo).ReadToEnd();
516+
if (test.IsSuccess && !string.IsNullOrEmpty(test.StdOut))
520517
{
521-
var node = ViewModels.Preferences.Instance.FindOrAddNodeByRepositoryPath(test.StdOut.Trim(), null, false);
522-
ViewModels.Welcome.Instance.Refresh();
523-
_launcher?.OpenRepositoryInTab(node, null);
524-
});
518+
Dispatcher.UIThread.Invoke(() =>
519+
{
520+
var node = ViewModels.Preferences.Instance.FindOrAddNodeByRepositoryPath(test.StdOut.Trim(), null, false);
521+
ViewModels.Welcome.Instance.Refresh();
522+
_launcher?.OpenRepositoryInTab(node, null);
523+
524+
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: Views.Launcher wnd })
525+
wnd.BringToTop();
526+
});
527+
528+
return;
529+
}
525530
}
531+
532+
Dispatcher.UIThread.Invoke(() =>
533+
{
534+
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: Views.Launcher launcher })
535+
launcher.BringToTop();
536+
});
526537
}
527538

528539
private void Check4Update(bool manually = false)

src/Models/IpcChannel.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,19 @@ public void SendToFirstInstance(string cmd)
4646
using (var client = new NamedPipeClientStream(".", "SourceGitIPCChannel", PipeDirection.Out))
4747
{
4848
client.Connect(1000);
49-
if (client.IsConnected)
49+
if (!client.IsConnected)
50+
return;
51+
52+
using (var writer = new StreamWriter(client))
5053
{
51-
using (var writer = new StreamWriter(client))
52-
{
53-
writer.WriteLine(cmd);
54-
writer.Flush();
55-
}
54+
writer.WriteLine(cmd);
55+
writer.Flush();
5656
}
57+
58+
if (OperatingSystem.IsWindows())
59+
client.WaitForPipeDrain();
60+
else
61+
Thread.Sleep(1000);
5762
}
5863
}
5964
catch
@@ -78,13 +83,18 @@ private async void StartServer()
7883
{
7984
await _server.WaitForConnectionAsync(_cancellationTokenSource.Token);
8085

81-
var line = (await reader.ReadLineAsync(_cancellationTokenSource.Token))?.Trim();
82-
MessageReceived?.Invoke(line);
86+
if (!_cancellationTokenSource.IsCancellationRequested)
87+
{
88+
var line = await reader.ReadToEndAsync(_cancellationTokenSource.Token);
89+
MessageReceived?.Invoke(line?.Trim());
90+
}
91+
8392
_server.Disconnect();
8493
}
8594
catch
8695
{
87-
// IGNORE
96+
if (!_cancellationTokenSource.IsCancellationRequested && _server.IsConnected)
97+
_server.Disconnect();
8898
}
8999
}
90100
}

src/ViewModels/Preferences.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ public static Preferences Instance
1818
if (_instance != null)
1919
return _instance;
2020

21-
_isLoading = true;
2221
_instance = Load();
23-
_isLoading = false;
22+
_instance._isLoading = false;
2423

2524
_instance.PrepareGit();
2625
_instance.PrepareShellOrTerminal();
@@ -629,8 +628,8 @@ private bool RemoveInvalidRepositoriesRecursive(List<RepositoryNode> collection)
629628
}
630629

631630
private static Preferences _instance = null;
632-
private static bool _isLoading = false;
633631

632+
private bool _isLoading = true;
634633
private bool _isReadonly = true;
635634
private string _locale = "en_US";
636635
private string _theme = "Default";

src/Views/Launcher.axaml.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public Launcher()
6767
InitializeComponent();
6868
}
6969

70+
public void BringToTop()
71+
{
72+
if (WindowState == WindowState.Minimized)
73+
WindowState = _lastWindowState;
74+
else
75+
Activate();
76+
}
77+
7078
public bool HasKeyModifier(KeyModifiers modifier)
7179
{
7280
return _unhandledModifiers.HasFlag(modifier);
@@ -92,6 +100,8 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
92100

93101
if (change.Property == WindowStateProperty)
94102
{
103+
_lastWindowState = (WindowState)change.OldValue;
104+
95105
var state = (WindowState)change.NewValue!;
96106
if (!OperatingSystem.IsMacOS() && !UseSystemWindowFrame)
97107
CaptionHeight = new GridLength(state == WindowState.Maximized ? 30 : 38);
@@ -291,5 +301,6 @@ private void OnOpenWorkspaceMenu(object sender, RoutedEventArgs e)
291301
}
292302

293303
private KeyModifiers _unhandledModifiers = KeyModifiers.None;
304+
private WindowState _lastWindowState = WindowState.Normal;
294305
}
295306
}

0 commit comments

Comments
 (0)