Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit24853b0

Browse files
authored
chore: add SizedFrame for window sizing (#54)
1 parent449bbd9 commit24853b0

File tree

5 files changed

+116
-50
lines changed

5 files changed

+116
-50
lines changed

‎App/Controls/SizedFrame.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
usingSystem;
2+
usingWindows.Foundation;
3+
usingMicrosoft.UI.Xaml;
4+
usingMicrosoft.UI.Xaml.Controls;
5+
6+
namespaceCoder.Desktop.App.Controls;
7+
8+
publicclassSizedFrameEventArgs:EventArgs
9+
{
10+
publicSizeNewSize{get;init;}
11+
}
12+
13+
/// <summary>
14+
/// SizedFrame extends Frame by adding a SizeChanged event, which will be triggered when:
15+
/// - The contained Page's content's size changes
16+
/// - We switch to a different page.
17+
///
18+
/// Sadly this is necessary because Window.Content.SizeChanged doesn't trigger when the Page's content changes.
19+
/// </summary>
20+
publicclassSizedFrame:Frame
21+
{
22+
publicdelegatevoidSizeChangeDelegate(objectsender,SizedFrameEventArgse);
23+
24+
publicneweventSizeChangeDelegate?SizeChanged;
25+
26+
privateSize_lastSize;
27+
28+
publicvoidSetPage(Pagepage)
29+
{
30+
if(ReferenceEquals(page,Content))return;
31+
32+
// Set the new event listener.
33+
if(page.Contentis notFrameworkElementnewElement)
34+
thrownewException("Failed to get Page.Content as FrameworkElement on SizedFrame navigation");
35+
newElement.SizeChanged+=Content_SizeChanged;
36+
37+
// Unset the previous event listener.
38+
if(ContentisPage{Content:FrameworkElementoldElement})
39+
oldElement.SizeChanged-=Content_SizeChanged;
40+
41+
// We don't use RootFrame.Navigate here because it doesn't let you
42+
// instantiate the page yourself. We also don't need forwards/backwards
43+
// capabilities.
44+
Content=page;
45+
46+
// Fire an event.
47+
Content_SizeChanged(newElement,null);
48+
}
49+
50+
publicSizeGetContentSize()
51+
{
52+
if(Contentis notPage{Content:FrameworkElementframeworkElement})
53+
thrownewException("Failed to get Content as FrameworkElement for SizedFrame");
54+
55+
frameworkElement.Measure(newSize(double.PositiveInfinity,double.PositiveInfinity));
56+
returnnewSize(frameworkElement.ActualWidth,frameworkElement.ActualHeight);
57+
}
58+
59+
privatevoidContent_SizeChanged(objectsender,SizeChangedEventArgs?_)
60+
{
61+
varsize=GetContentSize();
62+
if(size==_lastSize)return;
63+
_lastSize=size;
64+
65+
varargs=newSizedFrameEventArgs{NewSize=size};
66+
SizeChanged?.Invoke(this,args);
67+
}
68+
}

‎App/Views/SignInWindow.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
x:Class="Coder.Desktop.App.Views.SignInWindow"
55
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
66
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
7+
xmlns:controls="using:Coder.Desktop.App.Controls"
78
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
89
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
910
mc:Ignorable="d"
@@ -13,5 +14,5 @@
1314
<DesktopAcrylicBackdrop />
1415
</Window.SystemBackdrop>
1516

16-
<Framex:Name="RootFrame" />
17+
<controls:SizedFramex:Name="RootFrame" />
1718
</Window>

‎App/Views/SignInWindow.xaml.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,72 @@
1+
usingSystem;
12
usingWindows.Graphics;
3+
usingCoder.Desktop.App.Controls;
24
usingCoder.Desktop.App.ViewModels;
35
usingCoder.Desktop.App.Views.Pages;
46
usingMicrosoft.UI.Windowing;
57
usingMicrosoft.UI.Xaml;
8+
usingMicrosoft.UI.Xaml.Media;
69

710
namespaceCoder.Desktop.App.Views;
811

912
/// <summary>
10-
/// The dialog window to allow the user to signinto their Coder server.
13+
/// The dialog window to allow the user to signin to their Coder server.
1114
/// </summary>
1215
publicsealedpartialclassSignInWindow:Window
1316
{
14-
privateconstdoubleWIDTH=600.0;
15-
privateconstdoubleHEIGHT=300.0;
17+
privateconstdoubleWIDTH=500.0;
1618

1719
privatereadonlySignInUrlPage_signInUrlPage;
1820
privatereadonlySignInTokenPage_signInTokenPage;
1921

2022
publicSignInWindow(SignInViewModelviewModel)
2123
{
2224
InitializeComponent();
25+
SystemBackdrop=newDesktopAcrylicBackdrop();
26+
RootFrame.SizeChanged+=RootFrame_SizeChanged;
27+
2328
_signInUrlPage=newSignInUrlPage(this,viewModel);
2429
_signInTokenPage=newSignInTokenPage(this,viewModel);
2530

31+
// Prevent the window from being resized.
32+
if(AppWindow.Presenteris notOverlappedPresenterpresenter)
33+
thrownewException("Failed to get OverlappedPresenter for window");
34+
presenter.IsMaximizable=false;
35+
presenter.IsResizable=false;
36+
2637
NavigateToUrlPage();
2738
ResizeWindow();
2839
MoveWindowToCenterOfDisplay();
2940
}
3041

3142
publicvoidNavigateToTokenPage()
3243
{
33-
RootFrame.Content=_signInTokenPage;
44+
RootFrame.SetPage(_signInTokenPage);
3445
}
3546

3647
publicvoidNavigateToUrlPage()
3748
{
38-
RootFrame.Content=_signInUrlPage;
49+
RootFrame.SetPage(_signInUrlPage);
50+
}
51+
52+
privatevoidRootFrame_SizeChanged(objectsender,SizedFrameEventArgse)
53+
{
54+
ResizeWindow(e.NewSize.Height);
3955
}
4056

4157
privatevoidResizeWindow()
4258
{
59+
ResizeWindow(RootFrame.GetContentSize().Height);
60+
}
61+
62+
privatevoidResizeWindow(doubleheight)
63+
{
64+
if(height<=0)height=100;// will be resolved next frame typically
65+
4366
varscale=DisplayScale.WindowScale(this);
44-
varheight=(int)(HEIGHT*scale);
45-
varwidth=(int)(WIDTH*scale);
46-
AppWindow.Resize(newSizeInt32(width,height));
67+
varnewWidth=(int)(WIDTH*scale);
68+
varnewHeight=(int)(height*scale);
69+
AppWindow.ResizeClient(newSizeInt32(newWidth,newHeight));
4770
}
4871

4972
privatevoidMoveWindowToCenterOfDisplay()

‎App/Views/TrayWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
<controls:TrayIconx:Name="TrayIcon" />
2020

2121
<!-- This is where the current Page is displayed-->
22-
<Framex:Name="RootFrame" />
22+
<controls:SizedFramex:Name="RootFrame" />
2323
</Grid>
2424
</Window>

‎App/Views/TrayWindow.xaml.cs

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
usingSystem;
22
usingSystem.Runtime.InteropServices;
3-
usingWindows.Foundation;
43
usingWindows.Graphics;
54
usingWindows.System;
65
usingWindows.UI.Core;
6+
usingCoder.Desktop.App.Controls;
77
usingCoder.Desktop.App.Models;
88
usingCoder.Desktop.App.Services;
99
usingCoder.Desktop.App.Views.Pages;
@@ -48,6 +48,7 @@ public TrayWindow(IRpcController rpcController, ICredentialManager credentialMan
4848
AppWindow.Hide();
4949
SystemBackdrop=newDesktopAcrylicBackdrop();
5050
Activated+=Window_Activated;
51+
RootFrame.SizeChanged+=RootFrame_SizeChanged;
5152

5253
rpcController.StateChanged+=RpcController_StateChanged;
5354
credentialManager.CredentialsChanged+=CredentialManager_CredentialsChanged;
@@ -120,55 +121,31 @@ public void SetRootFrame(Page page)
120121
return;
121122
}
122123

123-
if(ReferenceEquals(page,RootFrame.Content))return;
124-
125-
if(page.Contentis notFrameworkElementnewElement)
126-
thrownewException("Failed to get Page.Content as FrameworkElement on RootFrame navigation");
127-
newElement.SizeChanged+=Content_SizeChanged;
128-
129-
// Unset the previous event listener.
130-
if(RootFrame.ContentisPage{Content:FrameworkElementoldElement})
131-
oldElement.SizeChanged-=Content_SizeChanged;
132-
133-
// Swap them out and reconfigure the window.
134-
// We don't use RootFrame.Navigate here because it doesn't let you
135-
// instantiate the page yourself. We also don't need forwards/backwards
136-
// capabilities.
137-
RootFrame.Content=page;
138-
ResizeWindow();
139-
MoveWindow();
124+
RootFrame.SetPage(page);
140125
}
141126

142-
privatevoidContent_SizeChanged(objectsender,SizeChangedEventArgse)
127+
privatevoidRootFrame_SizeChanged(objectsender,SizedFrameEventArgse)
143128
{
144-
ResizeWindow();
129+
ResizeWindow(e.NewSize.Height);
145130
MoveWindow();
146131
}
147132

148133
privatevoidResizeWindow()
149134
{
150-
if(RootFrame.Contentis notPage{Content:FrameworkElementframeworkElement})
151-
thrownewException("Failed to get Content as FrameworkElement for window");
152-
153-
// Measure the desired size of the content
154-
frameworkElement.Measure(newSize(double.PositiveInfinity,double.PositiveInfinity));
155-
156-
// Adjust the AppWindow size
157-
varscale=GetDisplayScale();
158-
varheight=(int)(frameworkElement.ActualHeight*scale);
159-
varwidth=(int)(WIDTH*scale);
160-
AppWindow.Resize(newSizeInt32(width,height));
135+
ResizeWindow(RootFrame.GetContentSize().Height);
161136
}
162137

163-
privatedoubleGetDisplayScale()
138+
privatevoidResizeWindow(doubleheight)
164139
{
165-
varhwnd=WindowNative.GetWindowHandle(this);
166-
vardpi=NativeApi.GetDpiForWindow(hwnd);
167-
if(dpi==0)return1;// assume scale of 1
168-
returndpi/96.0;// 96 DPI == 1
140+
if(height<=0)height=100;// will be resolved next frame typically
141+
142+
varscale=DisplayScale.WindowScale(this);
143+
varnewWidth=(int)(WIDTH*scale);
144+
varnewHeight=(int)(height*scale);
145+
AppWindow.Resize(newSizeInt32(newWidth,newHeight));
169146
}
170147

171-
publicvoidMoveResizeAndActivate()
148+
privatevoidMoveResizeAndActivate()
172149
{
173150
SaveCursorPos();
174151
ResizeWindow();
@@ -268,9 +245,6 @@ public static class NativeApi
268245
[DllImport("user32.dll")]
269246
publicstaticexternboolSetForegroundWindow(IntPtrhwnd);
270247

271-
[DllImport("user32.dll")]
272-
publicstaticexternintGetDpiForWindow(IntPtrhwnd);
273-
274248
publicstructPOINT
275249
{
276250
publicintX;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp