-
Notifications
You must be signed in to change notification settings - Fork 5
chore: add SizedFrame for window sizing #54
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using Windows.Foundation; | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
|
||
namespace Coder.Desktop.App.Controls; | ||
|
||
public class SizedFrameEventArgs : EventArgs | ||
{ | ||
public Size NewSize { get; init; } | ||
} | ||
|
||
/// <summary> | ||
/// SizedFrame extends Frame by adding a SizeChanged event. Sadly this is necessary because | ||
/// Window.Content.SizeChanged doesn't trigger when the Page's content changes. | ||
/// </summary> | ||
public class SizedFrame : Frame | ||
{ | ||
public delegate void SizeChangeDelegate(object sender, SizedFrameEventArgs e); | ||
|
||
public new event SizeChangeDelegate? SizeChanged; | ||
|
||
private Size _lastSize; | ||
|
||
public void SetPage(Page page) | ||
{ | ||
if (ReferenceEquals(page, Content)) return; | ||
|
||
// Set the new event listener. | ||
if (page.Content is not FrameworkElement newElement) | ||
throw new Exception("Failed to get Page.Content as FrameworkElement on SizedFrame navigation"); | ||
newElement.SizeChanged += Content_SizeChanged; | ||
|
||
// Unset the previous event listener. | ||
if (Content is Page { Content: FrameworkElement oldElement }) | ||
oldElement.SizeChanged -= Content_SizeChanged; | ||
|
||
// We don't use RootFrame.Navigate here because it doesn't let you | ||
// instantiate the page yourself. We also don't need forwards/backwards | ||
// capabilities. | ||
Content = page; | ||
|
||
// Fire an event. | ||
Content_SizeChanged(newElement, null); | ||
} | ||
|
||
public Size GetContentSize() | ||
{ | ||
if (Content is not Page { Content: FrameworkElement frameworkElement }) | ||
throw new Exception("Failed to get Content as FrameworkElement for SizedFrame"); | ||
|
||
frameworkElement.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); | ||
return new Size(frameworkElement.ActualWidth, frameworkElement.ActualHeight); | ||
} | ||
|
||
private void Content_SizeChanged(object sender, SizeChangedEventArgs? _) | ||
{ | ||
var size = GetContentSize(); | ||
if (size == _lastSize) return; | ||
_lastSize = size; | ||
|
||
var args = new SizedFrameEventArgs { NewSize = size }; | ||
SizeChanged?.Invoke(this, args); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,9 +1,9 @@ | ||||||
using System; | ||||||
using System.Runtime.InteropServices; | ||||||
using Windows.Foundation; | ||||||
using Windows.Graphics; | ||||||
using Windows.System; | ||||||
using Windows.UI.Core; | ||||||
using Coder.Desktop.App.Controls; | ||||||
using Coder.Desktop.App.Models; | ||||||
using Coder.Desktop.App.Services; | ||||||
using Coder.Desktop.App.Views.Pages; | ||||||
|
@@ -48,6 +48,7 @@ public TrayWindow(IRpcController rpcController, ICredentialManager credentialMan | |||||
AppWindow.Hide(); | ||||||
SystemBackdrop = new DesktopAcrylicBackdrop(); | ||||||
Activated += Window_Activated; | ||||||
RootFrame.SizeChanged += RootFrame_SizeChanged; | ||||||
|
||||||
rpcController.StateChanged += RpcController_StateChanged; | ||||||
credentialManager.CredentialsChanged += CredentialManager_CredentialsChanged; | ||||||
|
@@ -120,55 +121,31 @@ public void SetRootFrame(Page page) | |||||
return; | ||||||
} | ||||||
|
||||||
if (ReferenceEquals(page, RootFrame.Content)) return; | ||||||
|
||||||
if (page.Content is not FrameworkElement newElement) | ||||||
throw new Exception("Failed to get Page.Content as FrameworkElement on RootFrame navigation"); | ||||||
newElement.SizeChanged += Content_SizeChanged; | ||||||
|
||||||
// Unset the previous event listener. | ||||||
if (RootFrame.Content is Page { Content: FrameworkElement oldElement }) | ||||||
oldElement.SizeChanged -= Content_SizeChanged; | ||||||
|
||||||
// Swap them out and reconfigure the window. | ||||||
// We don't use RootFrame.Navigate here because it doesn't let you | ||||||
// instantiate the page yourself. We also don't need forwards/backwards | ||||||
// capabilities. | ||||||
RootFrame.Content = page; | ||||||
ResizeWindow(); | ||||||
MoveWindow(); | ||||||
RootFrame.SetPage(page); | ||||||
} | ||||||
|
||||||
private void Content_SizeChanged(object sender, SizeChangedEventArgs e) | ||||||
private void RootFrame_SizeChanged(object sender, SizedFrameEventArgs e) | ||||||
{ | ||||||
ResizeWindow(); | ||||||
ResizeWindow(e.NewSize.Width); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤦 |
||||||
MoveWindow(); | ||||||
} | ||||||
|
||||||
private void ResizeWindow() | ||||||
{ | ||||||
if (RootFrame.Content is not Page { Content: FrameworkElement frameworkElement }) | ||||||
throw new Exception("Failed to get Content as FrameworkElement for window"); | ||||||
|
||||||
// Measure the desired size of the content | ||||||
frameworkElement.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); | ||||||
|
||||||
// Adjust the AppWindow size | ||||||
var scale = GetDisplayScale(); | ||||||
var height = (int)(frameworkElement.ActualHeight * scale); | ||||||
var width = (int)(WIDTH * scale); | ||||||
AppWindow.Resize(new SizeInt32(width, height)); | ||||||
ResizeWindow(RootFrame.GetContentSize().Height); | ||||||
} | ||||||
|
||||||
private double GetDisplayScale() | ||||||
private void ResizeWindow(double height) | ||||||
{ | ||||||
var hwnd = WindowNative.GetWindowHandle(this); | ||||||
var dpi = NativeApi.GetDpiForWindow(hwnd); | ||||||
if (dpi == 0) return 1; // assume scale of 1 | ||||||
return dpi / 96.0; // 96 DPI == 1 | ||||||
if (height <= 0) height = 100; // will be resolved next frame typically | ||||||
|
||||||
var scale = DisplayScale.WindowScale(this); | ||||||
var newWidth = (int)(WIDTH * scale); | ||||||
var newHeight = (int)(height * scale); | ||||||
AppWindow.Resize(new SizeInt32(newWidth, newHeight)); | ||||||
} | ||||||
|
||||||
public void MoveResizeAndActivate() | ||||||
private void MoveResizeAndActivate() | ||||||
{ | ||||||
SaveCursorPos(); | ||||||
ResizeWindow(); | ||||||
|
@@ -268,9 +245,6 @@ public static class NativeApi | |||||
[DllImport("user32.dll")] | ||||||
public static extern bool SetForegroundWindow(IntPtr hwnd); | ||||||
|
||||||
[DllImport("user32.dll")] | ||||||
public static extern int GetDpiForWindow(IntPtr hwnd); | ||||||
|
||||||
public struct POINT | ||||||
{ | ||||||
public int X; | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.