Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable drag region workaround #3

Merged
merged 5 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CoreAppUWP/CoreAppUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
<RepositoryUrl>https://github.com/wherewhere/CoreAppUWP</RepositoryUrl>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<TargetFramework>net7.0-windows10.0.22621.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<TrimMode>partial</TrimMode>
<UseWinUI>True</UseWinUI>
Expand Down
7 changes: 7 additions & 0 deletions CoreAppUWP/Helpers/UIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation.Metadata;
using Windows.Graphics.Display;

namespace CoreAppUWP.Helpers
{
public static class UIHelper
{
public static bool HasStatusBar => ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar");

public static int GetActualPixel(this double pixel)
{
double currentDpi = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
return Convert.ToInt32(pixel * currentDpi);
}

public static string ExceptionToMessage(this Exception ex)
{
StringBuilder builder = new();
Expand Down
23 changes: 23 additions & 0 deletions CoreAppUWP/Helpers/WindowHelper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using CoreAppUWP.Common;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.Win32.Foundation;
using Windows.Win32.System.WinRT;
using WinRT;

namespace CoreAppUWP.Helpers
{
Expand Down Expand Up @@ -48,6 +53,24 @@ public static void TrackWindow(this Window window)
}
}

public static AppWindow GetAppWindow(this CoreWindow window)
{
if (!ActiveAppWindows.TryGetValue(window, out AppWindow appWindow))
{
HWND handle = window.As<ICoreWindowInterop>().WindowHandle;
WindowId id = Win32Interop.GetWindowIdFromWindow(handle);
appWindow = AppWindow.GetFromWindowId(id);
window.Closed += (sender, args) =>
{
ActiveAppWindows.Remove(window);
window = null;
};
ActiveAppWindows[window] = appWindow;
}
return appWindow;
}

public static Dictionary<CoreDispatcher, Window> ActiveWindows { get; } = [];
public static Dictionary<CoreWindow, AppWindow> ActiveAppWindows { get; } = [];
}
}
9 changes: 6 additions & 3 deletions CoreAppUWP/Pages/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
Height="48"
VerticalAlignment="Top"
Canvas.ZIndex="1"
IsHitTestVisible="True">
IsHitTestVisible="True"
SizeChanged="CustomTitleBar_SizeChanged">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="LeftPaddingColumn" Width="0" />
<ColumnDefinition Width="Auto" />
Expand Down Expand Up @@ -56,10 +57,12 @@
TextTrimming="CharacterEllipsis"
TextWrapping="NoWrap" />
</Grid>
<Grid
<Border
x:Name="DragRegion"
Grid.Column="1"
Grid.ColumnSpan="3"
Grid.ColumnSpan="4"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="{ThemeResource SystemControlTransparentBrush}" />
</Grid>
<NavigationView
Expand Down
8 changes: 8 additions & 0 deletions CoreAppUWP/Pages/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
using System.Collections.Generic;
using System.Linq;
using Windows.ApplicationModel.Core;
using Windows.Graphics;
using Windows.UI.Core;


// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

Expand Down Expand Up @@ -180,5 +182,11 @@ private void System_BackRequested(object sender, BackRequestedEventArgs e)
e.Handled = TryGoBack();
}
}

private void CustomTitleBar_SizeChanged(object sender, SizeChangedEventArgs e)
{
RectInt32 Rect = new((ActualWidth - DragRegion.ActualWidth).GetActualPixel(), 0, DragRegion.ActualWidth.GetActualPixel(), DragRegion.ActualHeight.GetActualPixel());
Window.Current.CoreWindow.GetAppWindow().TitleBar.SetDragRectangles([Rect]);
}
}
}
Loading