Skip to content

Commit

Permalink
b/304584429 Fall back to using process main window handle as parent (#…
Browse files Browse the repository at this point in the history
…1150)

When no handle is provided, use the process main window handle
as parent for error dialogs. This fixes an issue where the
authorize dialog, after a reauth, registered itself as the
default error window parent but was subsequently disposed.
  • Loading branch information
jpassing authored Oct 10, 2023
1 parent 0182d62 commit 98c609a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ public AuthorizeView()

public void Bind(AuthorizeViewModel viewModel, IBindingContext bindingContext)
{
//
// Register this window with the binding context so that any
// error dialos can use this window as owner.
//
((ViewBindingContext)bindingContext).SetCurrentMainWindow(this);

//
// Bind controls.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,16 @@ namespace Google.Solutions.IapDesktop.Application.Windows
public class ViewBindingContext : IBindingContext
{
private readonly IExceptionDialog exceptionDialog;
private IWin32Window errorReportingOwner;

/// <summary>
/// Sets the current main window that can be used as parent
/// for any error messages.
///
/// NB. During startup, we have a different main window than
/// later.
/// </summary>
public void SetCurrentMainWindow(IWin32Window owner)
public ViewBindingContext(IExceptionDialog exceptionDialog)
{
this.errorReportingOwner = owner;
this.exceptionDialog = exceptionDialog.ExpectNotNull(nameof(exceptionDialog));
}

public ViewBindingContext(IExceptionDialog exceptionDialog)
private IWin32Window GetMainWindow()
{
this.exceptionDialog = exceptionDialog.ExpectNotNull(nameof(exceptionDialog));
var mainWindowHwnd = Process.GetCurrentProcess().MainWindowHandle;
return mainWindowHwnd == IntPtr.Zero ? null : new Win32Window(mainWindowHwnd);
}

//---------------------------------------------------------------------
Expand All @@ -61,7 +54,6 @@ public ViewBindingContext(IExceptionDialog exceptionDialog)

public void OnBindingCreated(IComponent control, IDisposable binding)
{
Debug.Assert(this.errorReportingOwner != null);
Debug.WriteLine($"Binding added for {control.GetType().Name} ({control})");
}

Expand All @@ -70,13 +62,12 @@ public void OnCommandFailed(
ICommand command,
Exception exception)
{
Debug.Assert(this.errorReportingOwner != null);

//
// NB. We might not know the parent window if this is
// a menu command.
// NB. window might be null. If so, try to use the main
// window as parent. This is typically the main form,
// but could also be the authorize dialog.
//
var parent = window ?? this.errorReportingOwner;
var parent = window ?? GetMainWindow();

ApplicationEventSource.Log.CommandFailed(
command.Id,
Expand All @@ -96,5 +87,19 @@ public void OnCommandExecuted(ICommand command)
ApplicationEventSource.Log.CommandExecuted(command.Id);
}
}

//---------------------------------------------------------------------
// Inner classes.
//---------------------------------------------------------------------

private class Win32Window : IWin32Window
{
public Win32Window(IntPtr handle)
{
this.Handle = handle;
}

public IntPtr Handle { get; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"profiles": {
"Google.Solutions.IapDesktop": {
"commandName": "Project",
"commandLineArgs": "/profile \"Marge BYOID\""
"commandName": "Project"
}
}
}
6 changes: 0 additions & 6 deletions sources/Google.Solutions.IapDesktop/Windows/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ public MainForm(IServiceProvider serviceProvider)
this.applicationSettings = this.serviceProvider.GetService<IRepository<IApplicationSettings>>();
this.bindingContext = serviceProvider.GetService<IBindingContext>();

//
// Register this window with the binding context so that any
// error dialogs can use this window as owner.
//
((ViewBindingContext)this.bindingContext).SetCurrentMainWindow(this);

//
// Restore window settings.
//
Expand Down

0 comments on commit 98c609a

Please sign in to comment.