- Notifications
You must be signed in to change notification settings - Fork3
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletionsApp/Controls/SizedFrame.cs
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,68 @@ | ||
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, which will be triggered when: | ||
/// - The contained Page's content's size changes | ||
/// - We switch to a different page. | ||
/// | ||
/// 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); | ||
} | ||
} |
3 changes: 2 additions & 1 deletionApp/Views/SignInWindow.xaml
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
39 changes: 31 additions & 8 deletionsApp/Views/SignInWindow.xaml.cs
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
2 changes: 1 addition & 1 deletionApp/Views/TrayWindow.xaml
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
54 changes: 14 additions & 40 deletionsApp/Views/TrayWindow.xaml.cs
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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.