Skip to content

Commit

Permalink
Allow minimizing main window from taskbar
Browse files Browse the repository at this point in the history
  • Loading branch information
shatyuka authored and bagusnl committed Nov 25, 2023
1 parent 500c6a6 commit de9687d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CollapseLauncher/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,11 @@
</Setter.Value>
</Setter>
</Style>

<!-- WindowChrome min max close styles -->
<x:Double x:Key="WindowCaptionButtonStrokeWidth">1</x:Double>
<SolidColorBrush x:Key="WindowCaptionBackground" Color="#00000000"/>
<SolidColorBrush x:Key="WindowCaptionBackgroundDisabled" Color="#00000000"/>
</ResourceDictionary>
<!-- Custom controls styles -->
<ResourceDictionary Source="ms-appx:///XAMLs/Theme/DataGrid.xaml"/>
Expand Down
34 changes: 30 additions & 4 deletions CollapseLauncher/XAMLs/MainApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,37 @@
</Frame.ContentTransitions>
</Frame>
<customcontrol:ContentDialogCollapse x:Name="ContentDialog" x:FieldModifier="internal"/>
<Button x:Name="MinimizeButton" Width="48" Height="48"
Margin="0,0,48,0" HorizontalAlignment="Right"
<Grid x:Name="AppTitleBar">
<Button x:Name="MinimizeButton" Width="48" Height="48"
Margin="0,0,48,0" HorizontalAlignment="Right"
Style="{StaticResource WindowCaptionButton}"
Click="MinimizeButton_Click"
Content="M 0 0 H 10"/>
<Button x:Name="CloseButton" Width="48" Height="48"
Margin="0,0,0,0" HorizontalAlignment="Right"
Style="{StaticResource WindowCaptionButton}"
Click="MinimizeButton_Click"
Content="M 0 0 H 10"/>
Click="CloseButton_Click"
Content="M 0 0 L 10 10 M 10 0 L 0 10">
<Button.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<StaticResource x:Key="WindowCaptionButtonBackgroundPointerOver" ResourceKey="CloseButtonBackgroundPointerOver" />
<StaticResource x:Key="WindowCaptionButtonBackgroundPressed" ResourceKey="CloseButtonBackgroundPressed" />
<StaticResource x:Key="WindowCaptionButtonStrokePointerOver" ResourceKey="CloseButtonStrokePointerOver" />
<StaticResource x:Key="WindowCaptionButtonStrokePressed" ResourceKey="CloseButtonStrokePressed" />
</ResourceDictionary>
<ResourceDictionary x:Key="Default">
<StaticResource x:Key="WindowCaptionButtonBackgroundPointerOver" ResourceKey="CloseButtonBackgroundPointerOver" />
<StaticResource x:Key="WindowCaptionButtonBackgroundPressed" ResourceKey="CloseButtonBackgroundPressed" />
<StaticResource x:Key="WindowCaptionButtonStrokePointerOver" ResourceKey="CloseButtonStrokePointerOver" />
<StaticResource x:Key="WindowCaptionButtonStrokePressed" ResourceKey="CloseButtonStrokePressed" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Button.Resources>
</Button>
</Grid>
<local:TrayIcon x:Name="TrayIcon"/>
</Grid>
</Window>
41 changes: 23 additions & 18 deletions CollapseLauncher/XAMLs/MainApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.UI;
using Microsoft.UI.Input;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
Expand Down Expand Up @@ -133,35 +134,34 @@ public void InitializeWindowSettings()
m_presenter.IsResizable = false;
m_presenter.IsMaximizable = false;

if (IsAppThemeLight)
{
m_appWindow.TitleBar.ButtonForegroundColor = new Windows.UI.Color { A = 255, B = 0, G = 0, R = 0 };
m_appWindow.TitleBar.ButtonInactiveForegroundColor = new Windows.UI.Color { A = 0, B = 160, G = 160, R = 160 };
m_appWindow.TitleBar.ButtonHoverBackgroundColor = new Windows.UI.Color { A = 64, B = 0, G = 0, R = 0 };
}
else
m_appWindow.TitleBar.ButtonBackgroundColor = new Windows.UI.Color { A = 0, B = 0, G = 0, R = 0 };
m_appWindow.TitleBar.ButtonInactiveBackgroundColor = new Windows.UI.Color { A = 0, B = 0, G = 0, R = 0 };

// Hide system menu
var controlsHwnd = FindWindowEx(m_windowHandle, 0, "ReunionWindowingCaptionControls", "ReunionCaptionControlsWindow");
if (controlsHwnd != IntPtr.Zero)
{
m_appWindow.TitleBar.ButtonForegroundColor = new Windows.UI.Color { A = 255, B = 255, G = 255, R = 255 };
m_appWindow.TitleBar.ButtonHoverBackgroundColor = new Windows.UI.Color { A = 64, B = 0, G = 0, R = 0 };
DestroyWindow(controlsHwnd);
}

m_appWindow.TitleBar.ButtonBackgroundColor = new Windows.UI.Color { A = 0, B = 0, G = 0, R = 0 };
m_appWindow.TitleBar.ButtonInactiveBackgroundColor = new Windows.UI.Color { A = 0, B = 0, G = 0, R = 0 };
// Fix mouse event
var incps = InputNonClientPointerSource.GetForWindowId(m_windowID);
incps.SetRegionRects(NonClientRegionKind.Close, null);
incps.SetRegionRects(NonClientRegionKind.Minimize, null);
var safeArea = new RectInt32[] { new(m_appWindow.Size.Width - (int)((144 + 12) * m_appDPIScale), 0, (int)((144 + 12) * m_appDPIScale), (int)(48 * m_appDPIScale)) };
incps.SetRegionRects(NonClientRegionKind.Passthrough, safeArea);
}
else
{
// Shouldn't happen
// https://learn.microsoft.com/en-us/windows/apps/develop/title-bar#colors

m_presenter.IsResizable = false;
m_presenter.IsMaximizable = false;
ExtendsContentIntoTitleBar = false;
AppTitleBar.Visibility = Visibility.Collapsed;
}

// Hide minimize and maximize button
int gwl_style = -16;
uint minimizeBtn = 0x00020000;
uint maximizeBtn = 0x00010000;
var currentStyle = GetWindowLong(m_windowHandle, gwl_style);
SetWindowLong(m_windowHandle, gwl_style, currentStyle & ~minimizeBtn & ~maximizeBtn);

MainFrameChangerInvoker.WindowFrameEvent += MainFrameChangerInvoker_WindowFrameEvent;
LauncherUpdateInvoker.UpdateEvent += LauncherUpdateInvoker_UpdateEvent;
}
Expand Down Expand Up @@ -319,5 +319,10 @@ private void MinimizeButton_Click(object sender, RoutedEventArgs e)
}
else m_presenter.Minimize();
}

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
8 changes: 7 additions & 1 deletion Hi3Helper.Core/Classes/Data/InvokeProp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum SystemMetric : int
public enum SetWindowPosFlags : uint
{
SWP_NOMOVE = 2,
SWP_SHOWWINDOW = 40,
SWP_SHOWWINDOW = 0x40,
}

public enum SpecialWindowHandles
Expand Down Expand Up @@ -203,6 +203,12 @@ public struct WindowRect
[DllImport("user32.dll")]
public extern static uint SetWindowLong(IntPtr hwnd, int index, uint value);

[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll")]
public static extern bool DestroyWindow(IntPtr hwnd);

public static IntPtr GetProcessWindowHandle(string ProcName) => Process.GetProcessesByName(Path.GetFileNameWithoutExtension(ProcName), ".")[0].MainWindowHandle;

public class InvokePresence
Expand Down

0 comments on commit de9687d

Please sign in to comment.