Browse Source

Merge branch 'master' into group-layers

Krzysztof Krysiński 4 years ago
parent
commit
e1eaa477b9

+ 101 - 0
PixiEditor/Helpers/WindowSizeHelper.cs

@@ -0,0 +1,101 @@
+using System;
+using System.Runtime.InteropServices;
+
+namespace PixiEditor.Helpers
+{
+    static class WindowSizeHelper
+    {
+        public static IntPtr SetMaxSizeHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
+        {
+            // All windows messages (msg) can be found here
+            // https://docs.microsoft.com/de-de/windows/win32/winmsg/window-notifications
+            if (msg == WM_GETMINMAXINFO)
+            {
+                // We need to tell the system what our size should be when maximized. Otherwise it will
+                // cover the whole screen, including the task bar.
+                MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
+
+                // Adjust the maximized size and position to fit the work area of the correct monitor
+                IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
+
+                if (monitor != IntPtr.Zero)
+                {
+                    MONITORINFO monitorInfo = default;
+                    monitorInfo.cbSize = Marshal.SizeOf(typeof(MONITORINFO));
+                    GetMonitorInfo(monitor, ref monitorInfo);
+                    RECT rcWorkArea = monitorInfo.rcWork;
+                    RECT rcMonitorArea = monitorInfo.rcMonitor;
+                    mmi.ptMaxPosition.X = Math.Abs(rcWorkArea.Left - rcMonitorArea.Left);
+                    mmi.ptMaxPosition.Y = Math.Abs(rcWorkArea.Top - rcMonitorArea.Top);
+                    mmi.ptMaxSize.X = Math.Abs(rcWorkArea.Right - rcWorkArea.Left);
+                    mmi.ptMaxSize.Y = Math.Abs(rcWorkArea.Bottom - rcWorkArea.Top);
+                }
+
+                Marshal.StructureToPtr(mmi, lParam, true);
+            }
+
+            return IntPtr.Zero;
+        }
+
+        private const int WM_GETMINMAXINFO = 0x0024;
+
+        private const uint MONITOR_DEFAULTTONEAREST = 0x00000002;
+
+        [DllImport("user32.dll")]
+        private static extern IntPtr MonitorFromWindow(IntPtr handle, uint flags);
+
+        [DllImport("user32.dll")]
+        private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
+
+        [Serializable]
+        [StructLayout(LayoutKind.Sequential)]
+        private struct RECT
+        {
+            public int Left;
+            public int Top;
+            public int Right;
+            public int Bottom;
+
+            public RECT(int left, int top, int right, int bottom)
+            {
+                this.Left = left;
+                this.Top = top;
+                this.Right = right;
+                this.Bottom = bottom;
+            }
+        }
+
+        [StructLayout(LayoutKind.Sequential)]
+        private struct MONITORINFO
+        {
+            public int cbSize;
+            public RECT rcMonitor;
+            public RECT rcWork;
+            public uint dwFlags;
+        }
+
+        [Serializable]
+        [StructLayout(LayoutKind.Sequential)]
+        private struct POINT
+        {
+            public int X;
+            public int Y;
+
+            public POINT(int x, int y)
+            {
+                this.X = x;
+                this.Y = y;
+            }
+        }
+
+        [StructLayout(LayoutKind.Sequential)]
+        private struct MINMAXINFO
+        {
+            public POINT ptReserved;
+            public POINT ptMaxSize;
+            public POINT ptMaxPosition;
+            public POINT ptMinTrackSize;
+            public POINT ptMaxTrackSize;
+        }
+    }
+}

+ 4 - 1
PixiEditor/Views/Dialogs/HelloTherePopup.xaml.cs

@@ -83,20 +83,23 @@ namespace PixiEditor.Views.Dialogs
 
         private void OpenFile(object parameter)
         {
+            Application.Current.MainWindow.Activate();
             Close();
             FileViewModel.OpenAny();
         }
 
         private void OpenNewFile(object parameter)
         {
+            Application.Current.MainWindow.Activate();
             Close();
             FileViewModel.OpenNewFilePopup(parameter);
         }
 
         private void OpenRecent(object parameter)
         {
-            FileViewModel.OpenRecent(parameter);
+            Application.Current.MainWindow.Activate();
             Close();
+            FileViewModel.OpenRecent(parameter);
         }
     }
 }

+ 2 - 3
PixiEditor/Views/MainWindow.xaml

@@ -17,8 +17,7 @@
         Title="PixiEditor" Name="mainWindow" Height="1000" Width="1600" Background="{StaticResource MainColor}"
         WindowStartupLocation="CenterScreen" WindowState="Maximized">
     <WindowChrome.WindowChrome>
-        <WindowChrome CaptionHeight="32"
-                      ResizeBorderThickness="{x:Static SystemParameters.WindowResizeBorderThickness}" />
+        <WindowChrome CaptionHeight="35" x:Name="windowsChrome"/>
     </WindowChrome.WindowChrome>
 
     <Window.Resources>
@@ -159,7 +158,7 @@
                     <MenuItem Header="_Open Install Location"  Command="{Binding DebugSubViewModel.OpenInstallLocationCommand}"/>
                 </MenuItem>
             </Menu>
-            <StackPanel DockPanel.Dock="Right" VerticalAlignment="Top" Orientation="Horizontal"
+            <StackPanel DockPanel.Dock="Right" VerticalAlignment="Top" Orientation="Horizontal" Margin="0,-5,-5,0"
                         HorizontalAlignment="Right" WindowChrome.IsHitTestVisibleInChrome="True">
                 <Button Style="{StaticResource MinimizeButtonStyle}" WindowChrome.IsHitTestVisibleInChrome="True"
                         ToolTip="Minimize"

+ 26 - 7
PixiEditor/Views/MainWindow.xaml.cs

@@ -1,4 +1,4 @@
-using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.ViewModels;
 using System;
@@ -9,9 +9,9 @@ using PixiEditor.ViewModels.SubViewModels.Main;
 using System.Diagnostics;
 using System.Linq;
 using PixiEditor.Views.Dialogs;
-using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using PixiEditor.Models.DataHolders;
+using System.Windows.Interop;
 
 namespace PixiEditor
 {
@@ -41,11 +41,10 @@ namespace PixiEditor
 
             pixiEditorLogo = BitmapFactory.FromResource(@"/Images/PixiEditorLogo.png");
 
-            StateChanged += MainWindowStateChangeRaised;
+            UpdateWindowChromeBorderThickness();
+            StateChanged += MainWindow_StateChanged;
             Activated += MainWindow_Activated;
 
-            MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
-            rawLayerAnchorable.IsVisible = DataContext.IsDebug;
             DataContext.CloseAction = Close;
             Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
 
@@ -69,6 +68,12 @@ namespace PixiEditor
             DataContext.DiscordViewModel.Dispose();
         }
 
+        protected override void OnSourceInitialized(EventArgs e)
+        {
+            base.OnSourceInitialized(e);
+            ((HwndSource)PresentationSource.FromVisual(this)).AddHook(Helpers.WindowSizeHelper.SetMaxSizeHook);
+        }
+
         [Conditional("RELEASE")]
         private static void CloseHelloThereIfRelease()
         {
@@ -129,8 +134,22 @@ namespace PixiEditor
             CloseHelloThereIfRelease();
         }
 
-        private void MainWindowStateChangeRaised(object sender, EventArgs e)
+        private void UpdateWindowChromeBorderThickness()
         {
+            if (WindowState == WindowState.Maximized)
+            {
+                windowsChrome.ResizeBorderThickness = new Thickness(0, 0, 0, 0);
+            }
+            else
+            {
+                windowsChrome.ResizeBorderThickness = new Thickness(5, 5, 5, 5);
+            }
+        }
+
+        private void MainWindow_StateChanged(object sender, EventArgs e)
+        {
+            UpdateWindowChromeBorderThickness();
+
             if (WindowState == WindowState.Maximized)
             {
                 RestoreButton.Visibility = Visibility.Visible;
@@ -158,4 +177,4 @@ namespace PixiEditor
             }
         }
     }
-}
+}