Skip to content

Commit

Permalink
Merge pull request #2647 from cwensley/curtis/window-focus-and-bringt…
Browse files Browse the repository at this point in the history
…ofront

BringToFront() and Focus() should un-minimize the Window
  • Loading branch information
cwensley committed Apr 24, 2024
2 parents 8819ec1 + 7697831 commit 008fb16
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/Eto.Gtk/Forms/GtkWindow.cs
Expand Up @@ -723,6 +723,8 @@ public WindowState WindowState
Control.Unfullscreen();
if (gdkWindow.State.HasFlag(Gdk.WindowState.Iconified))
Control.Deiconify();
if (gdkWindow.State.HasFlag(Gdk.WindowState.Iconified))
Control.Present();
}
Control.Maximize();
break;
Expand All @@ -738,6 +740,8 @@ public WindowState WindowState
Control.Unmaximize();
if (gdkWindow.State.HasFlag(Gdk.WindowState.Iconified))
Control.Deiconify();
if (gdkWindow.State.HasFlag(Gdk.WindowState.Iconified))
Control.Present();
}
break;
}
Expand Down Expand Up @@ -783,7 +787,18 @@ public Screen Screen

protected override void GrabFocus() => Control.Present();

public void BringToFront() => Control.GetWindow()?.Raise();
public void BringToFront()
{
if (WindowState == WindowState.Minimized)
{
Control.Deiconify();
// if it didn't work, present it
if (WindowState == WindowState.Minimized)
Control.Present();
}

Control.GetWindow()?.Raise();
}

public void SendToBack() => Control.GetWindow()?.Lower();

Expand Down
22 changes: 20 additions & 2 deletions src/Eto.WinForms/Forms/WindowHandler.cs
Expand Up @@ -547,8 +547,7 @@ public WindowStyle WindowStyle

public virtual void BringToFront()
{
if (Control.WindowState == swf.FormWindowState.Minimized)
Control.WindowState = swf.FormWindowState.Normal;
RestoreFromMinimized();

var hWnd = NativeHandle;
if (hWnd != IntPtr.Zero)
Expand All @@ -557,6 +556,25 @@ public virtual void BringToFront()

public void SendToBack() => Control.SendToBack();

public override void Focus()
{
RestoreFromMinimized();

base.Focus();
}

private void RestoreFromMinimized()
{
if (Control.WindowState == swf.FormWindowState.Minimized)
{
var placement = new Win32.WINDOWPLACEMENT();
if (Win32.GetWindowPlacement(NativeHandle, ref placement) && placement.flags.HasFlag(Win32.WPF.RESTORETOMAXIMIZED))
Control.WindowState = swf.FormWindowState.Maximized;
else
Control.WindowState = swf.FormWindowState.Normal;
}
}

public virtual void SetOwner(Window owner)
{
Control.Owner = owner.ToSWF();
Expand Down
24 changes: 24 additions & 0 deletions src/Eto.WinForms/Win32.cs
Expand Up @@ -768,5 +768,29 @@ public struct SHSTOCKICONINFO
/// <returns>N/A</returns>
[DllImport("User32.dll")]
public static extern int DestroyIcon(IntPtr hIcon);

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

[Flags]
public enum WPF : int
{
SETMINPOSITION = 0x01,
RESTORETOMAXIMIZED = 0x02,
ASYNCWINDOWPLACEMENT = 0x04
}

[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPLACEMENT
{
public int length;
public WPF flags;
public int showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}

}
}
7 changes: 7 additions & 0 deletions src/Eto.Wpf/Forms/WpfWindow.cs
Expand Up @@ -967,6 +967,13 @@ void SetWindowChrome(bool enabled)
RestoreWindowStyle(oldStyle);
}

public override void Focus()
{
if (Control.WindowState == sw.WindowState.Minimized)
Control.WindowState = sw.WindowState.Normal;
base.Focus();
}

public void BringToFront()
{
if (Control.WindowState == sw.WindowState.Minimized)
Expand Down

0 comments on commit 008fb16

Please sign in to comment.