Skip to content

Commit 85cf60d

Browse files
committed
Resolved (hopefully) .Activate() call in ActivateWindow() in LauncherCommands.cs
-> AvaloniaUI/Avalonia#2398
1 parent bde8f8d commit 85cf60d

File tree

1 file changed

+55
-39
lines changed

1 file changed

+55
-39
lines changed

SS14.Launcher/LauncherCommands.cs

+55-39
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class LauncherCommands
2020
private LauncherMessaging _msgr;
2121
public readonly Channel<string> CommandChannel;
2222

23+
/// <summary>
24+
/// Constructor for LauncherCommands
25+
/// </summary>
26+
/// <param name="windowVm">ViewModel of the Main Window</param>
2327
public LauncherCommands(MainWindowViewModel windowVm)
2428
{
2529
_windowVm = windowVm;
@@ -29,12 +33,18 @@ public LauncherCommands(MainWindowViewModel windowVm)
2933
CommandChannel = Channel.CreateUnbounded<string>();
3034
}
3135

36+
/// <summary>
37+
/// Activates Main Window and brings to front.
38+
/// </summary>
3239
private void ActivateWindow()
3340
{
34-
// This may not work, but let's try anyway...
35-
// In particular keep in mind:
36-
// https://github.com/AvaloniaUI/Avalonia/issues/2398
37-
_windowVm.Control?.Activate();
41+
var window = _windowVm.Control;
42+
if (window != null)
43+
{
44+
Dispatcher.UIThread.Post(window.Activate);
45+
return;
46+
}
47+
Console.WriteLine($"MainWindowViewModel not activated; window returned null. :: ActivateWindow()");
3848
}
3949

4050
private async Task Connect(string param)
@@ -75,6 +85,7 @@ private async Task Connect(string param)
7585
Log.Warning($"Dropping connect command: Busy connecting to a server");
7686
return;
7787
}
88+
7889
// Note that we don't want to activate the window for something we'll requeue again and again.
7990
ActivateWindow();
8091
Log.Information($"Connect command: \"{param}\", \"{reason}\"");
@@ -125,41 +136,47 @@ private async Task RunSingleCommand(string cmd)
125136
}
126137
}
127138

128-
if (cmd == PingCommand)
129-
{
130-
// Yup!
131-
ActivateWindow();
132-
}
133-
else if (cmd == RedialWaitCommand)
134-
{
135-
// Redialling wait
136-
await Task.Delay(ConfigConstants.LauncherCommandsRedialWaitTimeout);
137-
}
138-
else if (cmd.StartsWith("R"))
139-
{
140-
// Reason (encoded in UTF-8 and then into hex for safety)
141-
_reason = GetUntrustedTextField() ?? "";
142-
}
143-
else if (cmd.StartsWith("r"))
144-
{
145-
// Reason (no encoding)
146-
_reason = cmd.Substring(1);
147-
}
148-
else if (cmd.StartsWith("C"))
139+
switch (cmd)
149140
{
150-
// Uri (encoded in UTF-8 and then into hex for safety)
151-
var uri = GetUntrustedTextField();
152-
if (uri != null)
153-
await Connect(uri);
154-
}
155-
else if (cmd.StartsWith("c"))
156-
{
157-
// Used by the "pass URI as argument" logic, doesn't need to bother with safety measures
158-
await Connect(cmd.Substring(1));
159-
}
160-
else
161-
{
162-
Log.Error($"Unhandled launcher command: {cmd}");
141+
case PingCommand:
142+
// Yup!
143+
ActivateWindow();
144+
break;
145+
case RedialWaitCommand:
146+
// Redialling wait
147+
await Task.Delay(ConfigConstants.LauncherCommandsRedialWaitTimeout);
148+
break;
149+
default:
150+
{
151+
if (cmd.StartsWith('R'))
152+
{
153+
// Reason (encoded in UTF-8 and then into hex for safety)
154+
_reason = GetUntrustedTextField() ?? "";
155+
}
156+
else if (cmd.StartsWith('r'))
157+
{
158+
// Reason (no encoding)
159+
_reason = cmd.Substring(1);
160+
}
161+
else if (cmd.StartsWith('C'))
162+
{
163+
// Uri (encoded in UTF-8 and then into hex for safety)
164+
var uri = GetUntrustedTextField();
165+
if (uri != null)
166+
await Connect(uri);
167+
}
168+
else if (cmd.StartsWith('c'))
169+
{
170+
// Used by the "pass URI as argument" logic, doesn't need to bother with safety measures
171+
await Connect(cmd.Substring(1));
172+
}
173+
else
174+
{
175+
Log.Error($"Unhandled launcher command: {cmd}");
176+
}
177+
178+
break;
179+
}
163180
}
164181
}
165182

@@ -170,4 +187,3 @@ private async Task RunSingleCommand(string cmd)
170187
public const string BlankReasonCommand = "r";
171188
public static string ConstructConnectCommand(Uri uri) => "c" + uri.ToString();
172189
}
173-

0 commit comments

Comments
 (0)