-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSignInWindow.xaml.cs
80 lines (66 loc) · 2.37 KB
/
SignInWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using Windows.Graphics;
using Coder.Desktop.App.Controls;
using Coder.Desktop.App.ViewModels;
using Coder.Desktop.App.Views.Pages;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
namespace Coder.Desktop.App.Views;
/// <summary>
/// The dialog window to allow the user to sign in to their Coder server.
/// </summary>
public sealed partial class SignInWindow : Window
{
private const double WIDTH = 500.0;
private readonly SignInUrlPage _signInUrlPage;
private readonly SignInTokenPage _signInTokenPage;
public SignInWindow(SignInViewModel viewModel)
{
InitializeComponent();
SystemBackdrop = new DesktopAcrylicBackdrop();
RootFrame.SizeChanged += RootFrame_SizeChanged;
_signInUrlPage = new SignInUrlPage(this, viewModel);
_signInTokenPage = new SignInTokenPage(this, viewModel);
// Prevent the window from being resized.
if (AppWindow.Presenter is not OverlappedPresenter presenter)
throw new Exception("Failed to get OverlappedPresenter for window");
presenter.IsMaximizable = false;
presenter.IsResizable = false;
NavigateToUrlPage();
ResizeWindow();
MoveWindowToCenterOfDisplay();
}
public void NavigateToTokenPage()
{
RootFrame.SetPage(_signInTokenPage);
}
public void NavigateToUrlPage()
{
RootFrame.SetPage(_signInUrlPage);
}
private void RootFrame_SizeChanged(object sender, SizedFrameEventArgs e)
{
ResizeWindow(e.NewSize.Height);
}
private void ResizeWindow()
{
ResizeWindow(RootFrame.GetContentSize().Height);
}
private void ResizeWindow(double height)
{
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.ResizeClient(new SizeInt32(newWidth, newHeight));
}
private void MoveWindowToCenterOfDisplay()
{
var workArea = DisplayArea.GetFromWindowId(AppWindow.Id, DisplayAreaFallback.Primary).WorkArea;
var x = (workArea.Width - AppWindow.Size.Width) / 2;
var y = (workArea.Height - AppWindow.Size.Height) / 2;
if (x < 0 || y < 0) return;
AppWindow.Move(new PointInt32(x, y));
}
}