Bladeren bron

Added Window.WindowState.

To allow minimize/maximize/restore of Window from code.
Steven Kirk 9 jaren geleden
bovenliggende
commit
fbb6dfbe1a

+ 33 - 0
src/Gtk/Perspex.Gtk/WindowImpl.cs

@@ -75,6 +75,39 @@ namespace Perspex.Gtk
             }
         }
 
+        public Perspex.Controls.WindowState WindowState
+        {
+            get
+            {
+                switch (GdkWindow.State)
+                {
+                    case Gdk.WindowState.Iconified:
+                        return Controls.WindowState.Minimized;
+                    case Gdk.WindowState.Maximized:
+                        return Controls.WindowState.Maximized;
+                    default:
+                        return Controls.WindowState.Normal;
+                }
+            }
+
+            set
+            {
+                switch (value)
+                {
+                    case Controls.WindowState.Minimized:
+                        GdkWindow.Iconify();
+                        break;
+                    case Controls.WindowState.Maximized:
+                        GdkWindow.Maximize();
+                        break;
+                    case Controls.WindowState.Normal:
+                        GdkWindow.Deiconify();
+                        GdkWindow.Unmaximize();
+                        break;
+                }
+            }
+        }
+
         IPlatformHandle ITopLevelImpl.Handle => this;
 
         [DllImport("libgdk-win32-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]

+ 1 - 0
src/Perspex.Controls/Perspex.Controls.csproj

@@ -163,6 +163,7 @@
     <Compile Include="Primitives\PopupRoot.cs" />
     <Compile Include="Utils\UndoRedoHelper.cs" />
     <Compile Include="LogicalTreeAttachmentEventArgs.cs" />
+    <Compile Include="WindowState.cs" />
     <Compile Include="Window.cs" />
     <Compile Include="RowDefinition.cs" />
     <Compile Include="RowDefinitions.cs" />

+ 6 - 0
src/Perspex.Controls/Platform/IWindowImpl.cs

@@ -2,6 +2,7 @@
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
 using System;
+using Perspex.Controls;
 
 namespace Perspex.Platform
 {
@@ -15,6 +16,11 @@ namespace Perspex.Platform
         /// </summary>
         Size MaxClientSize { get; }
 
+        /// <summary>
+        /// Gets or sets the minimized/maximized state of the window.
+        /// </summary>
+        WindowState WindowState { get; set; }
+
         /// <summary>
         /// Sets the title of the window.
         /// </summary>

+ 6 - 5
src/Perspex.Controls/Platform/PlatformManager.cs

@@ -135,8 +135,6 @@ namespace Perspex.Controls.Platform
             public Action<Rect> Paint { get; set; }
             public Action<Size> Resized { get; set; }
 
-
-
             public Action Activated
             {
                 get { return _tl.Activated; }
@@ -155,6 +153,12 @@ namespace Perspex.Controls.Platform
                 set { _tl.Deactivated = value; }
             }
 
+            public WindowState WindowState
+            {
+                get { return _window.WindowState; }
+                set { _window.WindowState = value; }
+            }
+
             public void Dispose() => _tl.Dispose();
 
             public IPlatformHandle Handle => _tl.Handle;
@@ -163,9 +167,6 @@ namespace Perspex.Controls.Platform
             
             public void SetCursor(IPlatformHandle cursor) => _tl.SetCursor(cursor);
 
-
-
-
             public void SetTitle(string title) => _window.SetTitle(title);
 
             public void Show() => _tl.Show();

+ 9 - 0
src/Perspex.Controls/Window.cs

@@ -133,6 +133,15 @@ namespace Perspex.Controls
             set { SetValue(HasSystemDecorationsProperty, value); }
         }
 
+        /// <summary>
+        /// Gets or sets the minimized/maximized state of the window.
+        /// </summary>
+        public WindowState WindowState
+        {
+            get { return this.PlatformImpl.WindowState; }
+            set { this.PlatformImpl.WindowState = value; }
+        }
+
         /// <inheritdoc/>
         Size ILayoutRoot.MaxClientSize => _maxPlatformClientSize;
 

+ 26 - 0
src/Perspex.Controls/WindowState.cs

@@ -0,0 +1,26 @@
+// Copyright (c) The Perspex Project. All rights reserved.
+// Licensed under the MIT license. See licence.md file in the project root for full license information.
+
+namespace Perspex.Controls
+{
+    /// <summary>
+    /// Defines the minimized/maximized state of a <see cref="Window"/>.
+    /// </summary>
+    public enum WindowState
+    {
+        /// <summary>
+        /// The window is neither minimized or maximized.
+        /// </summary>
+        Normal,
+
+        /// <summary>
+        /// The window is minimized.
+        /// </summary>
+        Minimized,
+
+        /// <summary>
+        /// The window is maximized.
+        /// </summary>
+        Maximized,
+    }
+}

+ 53 - 0
src/Windows/Perspex.Win32/Interop/UnmanagedMethods.cs

@@ -602,6 +602,9 @@ namespace Perspex.Win32.Interop
         [DllImport("user32.dll", SetLastError = true)]
         public static extern uint SetWindowLong(IntPtr hWnd, int nIndex, uint value);
 
+        [DllImport("user32.dll", SetLastError = true)]
+        public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
+
         [DllImport("user32.dll")]
         public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
 
@@ -787,6 +790,56 @@ namespace Perspex.Win32.Interop
             public int dwHoverTime;
         }
 
+        [StructLayout(LayoutKind.Sequential)]
+        public struct WINDOWPLACEMENT
+        {
+            /// <summary>
+            /// The length of the structure, in bytes. Before calling the GetWindowPlacement or SetWindowPlacement functions, set this member to sizeof(WINDOWPLACEMENT).
+            /// <para>
+            /// GetWindowPlacement and SetWindowPlacement fail if this member is not set correctly.
+            /// </para>
+            /// </summary>
+            public int Length;
+
+            /// <summary>
+            /// Specifies flags that control the position of the minimized window and the method by which the window is restored.
+            /// </summary>
+            public int Flags;
+
+            /// <summary>
+            /// The current show state of the window.
+            /// </summary>
+            public ShowWindowCommand ShowCmd;
+
+            /// <summary>
+            /// The coordinates of the window's upper-left corner when the window is minimized.
+            /// </summary>
+            public POINT MinPosition;
+
+            /// <summary>
+            /// The coordinates of the window's upper-left corner when the window is maximized.
+            /// </summary>
+            public POINT MaxPosition;
+
+            /// <summary>
+            /// The window's coordinates when the window is in the restored position.
+            /// </summary>
+            public RECT NormalPosition;
+
+            /// <summary>
+            /// Gets the default (empty) value.
+            /// </summary>
+            public static WINDOWPLACEMENT Default
+            {
+                get
+                {
+                    WINDOWPLACEMENT result = new WINDOWPLACEMENT();
+                    result.Length = Marshal.SizeOf(result);
+                    return result;
+                }
+            }
+        }
+
         [StructLayout(LayoutKind.Sequential)]
         public struct WNDCLASSEX
         {

+ 41 - 0
src/Windows/Perspex.Win32/WindowImpl.cs

@@ -126,6 +126,47 @@ namespace Perspex.Win32
             }
         }
 
+        public WindowState WindowState
+        {
+            get
+            {
+                var placement = default(UnmanagedMethods.WINDOWPLACEMENT);
+                UnmanagedMethods.GetWindowPlacement(_hwnd, ref placement);
+                
+                switch (placement.ShowCmd)
+                {
+                    case UnmanagedMethods.ShowWindowCommand.Maximize:
+                        return WindowState.Maximized;
+                    case UnmanagedMethods.ShowWindowCommand.Minimize:
+                        return WindowState.Minimized;
+                    default:
+                        return WindowState.Normal;
+                }
+            }
+
+            set
+            {
+                UnmanagedMethods.ShowWindowCommand command;
+
+                switch (value)
+                {
+                    case WindowState.Minimized:
+                        command = UnmanagedMethods.ShowWindowCommand.Minimize;
+                        break;
+                    case WindowState.Maximized:
+                        command = UnmanagedMethods.ShowWindowCommand.Maximize;
+                        break;
+                    case WindowState.Normal:
+                        command = UnmanagedMethods.ShowWindowCommand.Restore;
+                        break;
+                    default:
+                        throw new ArgumentException("Invalid WindowState.");
+                }
+
+                UnmanagedMethods.ShowWindow(_hwnd, command);
+            }
+        }
+
         public void Activate()
         {
             UnmanagedMethods.SetActiveWindow(_hwnd);