Skip to content

Commit 3588523

Browse files
committed
winui: extract parent window handling
Extract logic display a model window that is correctly parented to the GCM_MODAL_PARENTHWND window handle.
1 parent 8852047 commit 3588523

File tree

3 files changed

+20
-44
lines changed

3 files changed

+20
-44
lines changed

src/windows/GitHub.UI.Windows/Program.cs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,7 @@ public static class Program
1212
{
1313
public static void Main(string[] args)
1414
{
15-
IGui gui;
16-
if (TryGetParentWindowHandle(out IntPtr parentHwnd))
17-
{
18-
gui = new Gui(parentHwnd);
19-
}
20-
else
21-
{
22-
gui = new Gui();
23-
}
15+
IGui gui = new Gui();
2416

2517
try
2618
{
@@ -97,19 +89,5 @@ public static void Main(string[] args)
9789
Environment.Exit(-1);
9890
}
9991
}
100-
101-
private static bool TryGetParentWindowHandle(out IntPtr hwnd)
102-
{
103-
string envar = Environment.GetEnvironmentVariable(Constants.EnvironmentVariables.GcmParentWindow);
104-
105-
if (long.TryParse(envar, out long ptrInt))
106-
{
107-
hwnd = new IntPtr(ptrInt);
108-
return true;
109-
}
110-
111-
hwnd = default(IntPtr);
112-
return false;
113-
}
11492
}
11593
}

src/windows/GitHub.UI.Windows/Tester.xaml.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ public Tester()
2323

2424
private void ShowCredentials(object sender, RoutedEventArgs e)
2525
{
26-
new GitHubDialogWindow(new LoginCredentialsViewModel(true, true), new LoginCredentialsView()).ShowDialog(Handle);
26+
var model = new LoginCredentialsViewModel(true, true);
27+
var view = new LoginCredentialsView();
28+
var window = new GitHubDialogWindow(model, view);
29+
Gui.ShowDialog(window, Handle);
2730
}
2831

2932
private void ShowAuthenticationCode(object sender, RoutedEventArgs e)
3033
{
31-
new GitHubDialogWindow(new Login2FaViewModel(TwoFactorType.AuthenticatorApp), new Login2FaView()).ShowDialog(Handle);
34+
var model = new Login2FaViewModel(TwoFactorType.AuthenticatorApp);
35+
var view = new Login2FaView();
36+
var window = new GitHubDialogWindow(model, view);
37+
Gui.ShowDialog(window, Handle);
3238
}
3339
}
3440
}

src/windows/Shared.UI.Windows/Gui.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ public interface IGui
3030

3131
public class Gui : IGui
3232
{
33-
private readonly IntPtr? _parentHwnd;
33+
private readonly IntPtr _parentHwnd = IntPtr.Zero;
3434

3535
public Gui()
3636
{
37-
_parentHwnd = null;
38-
}
37+
string envar = Environment.GetEnvironmentVariable(Constants.EnvironmentVariables.GcmParentWindow);
3938

40-
public Gui(IntPtr parentHwnd)
41-
{
42-
_parentHwnd = parentHwnd;
39+
if (long.TryParse(envar, out long ptrInt))
40+
{
41+
_parentHwnd = new IntPtr(ptrInt);
42+
}
4343
}
4444

4545
public bool ShowDialogWindow(Func<Window> windowCreator)
@@ -50,7 +50,7 @@ public bool ShowDialogWindow(Func<Window> windowCreator)
5050
{
5151
var window = windowCreator();
5252

53-
windowResult = window.ShowDialog(_parentHwnd) ?? false;
53+
windowResult = ShowDialog(window, _parentHwnd) ?? false;
5454
})
5555
.Wait();
5656

@@ -67,7 +67,7 @@ public bool ShowViewModel(ViewModel viewModel, Func<Window> windowCreator)
6767

6868
window.DataContext = viewModel;
6969

70-
windowResult = window.ShowDialog(_parentHwnd) ?? false;
70+
windowResult = ShowDialog(window, _parentHwnd) ?? false;
7171
})
7272
.Wait();
7373

@@ -95,25 +95,17 @@ private static Task StartSTATask(Action action)
9595

9696
return completionSource.Task;
9797
}
98-
}
99-
100-
public static class WindowExtensions
101-
{
102-
public static void SetOwnerHandle(this Window window, IntPtr hwnd)
103-
{
104-
new System.Windows.Interop.WindowInteropHelper(window).Owner = hwnd;
105-
}
10698

107-
public static bool? ShowDialog(this Window window, IntPtr? parentHwnd)
99+
public static bool? ShowDialog(Window window, IntPtr parentHwnd)
108100
{
109101
// Zero is not a valid window handles
110-
if (!parentHwnd.HasValue || parentHwnd.Value == IntPtr.Zero)
102+
if (parentHwnd == IntPtr.Zero)
111103
{
112104
return window.ShowDialog();
113105
}
114106

115107
// Set the parent window handle and ensure the dialog starts in the correct location
116-
window.SetOwnerHandle(parentHwnd.Value);
108+
new System.Windows.Interop.WindowInteropHelper(window).Owner = parentHwnd;
117109
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
118110

119111
const int ERROR_INVALID_WINDOW_HANDLE = 1400;

0 commit comments

Comments
 (0)