-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.axaml.cs
More file actions
116 lines (99 loc) · 3.8 KB
/
Copy pathMainWindow.axaml.cs
File metadata and controls
116 lines (99 loc) · 3.8 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
namespace MoveBit;
public partial class MainWindow : Window
{
private const double DefaultWidth = 520;
private const double DefaultHeight = 900;
private const double MinimumWidth = 460;
private const double PreferredMinimumHeight = 560;
private const double AbsoluteMinimumHeight = 320;
private const double MaximumComfortableWidth = 720;
private const double ScreenMargin = 24;
public MainWindow()
{
InitializeComponent();
// Keep MoveBit compact by default, but let users make better use of larger displays.
// Never cap the native window itself: Windows can otherwise maximize the title bar
// while Avalonia's client area remains at a smaller MaxWidth/MaxHeight.
CanResize = true;
Width = DefaultWidth;
Height = DefaultHeight;
MinWidth = MinimumWidth;
MinHeight = AbsoluteMinimumHeight;
MaxWidth = double.PositiveInfinity;
MaxHeight = double.PositiveInfinity;
// Re-evaluate normal-window sizing when the window moves between displays or DPI changes.
PositionChanged += (_, _) => FitToCurrentScreen();
ScalingChanged += (_, _) => FitToCurrentScreen();
PropertyChanged += (_, change) =>
{
if (change.Property == WindowStateProperty && WindowState == WindowState.Normal)
{
FitToCurrentScreen();
}
};
}
/// True while the app is running: closing hides to tray. Set false only on real shutdown.
public bool HideOnClose { get; set; } = true;
protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
FitToCurrentScreen();
}
private void FitToCurrentScreen()
{
// A maximized/full-screen window belongs to the window manager. Only normal windows
// get our compact-product sizing so chrome and client area always share the same bounds.
if (WindowState != WindowState.Normal)
{
return;
}
var screen = Screens.ScreenFromWindow(this);
if (screen is null)
{
return;
}
var scaling = Math.Max(screen.Scaling, 0.1);
var workingArea = screen.WorkingArea;
// WorkingArea is reported in physical pixels while Window dimensions use DIPs.
// Leave breathing room around the native window chrome and taskbar/dock.
var availableWidth = Math.Max(MinimumWidth, workingArea.Width / scaling - ScreenMargin * 2);
var availableHeight = Math.Max(AbsoluteMinimumHeight, workingArea.Height / scaling - ScreenMargin * 2);
var comfortableWidth = Math.Min(MaximumComfortableWidth, availableWidth);
// Preserve the roomy normal minimum where possible, but allow a shorter viewport on
// high-DPI/small displays so the ScrollViewer can keep every setting reachable.
MinHeight = Math.Min(PreferredMinimumHeight, availableHeight);
if (Width > comfortableWidth)
{
Width = comfortableWidth;
}
if (Height > availableHeight)
{
Height = availableHeight;
}
}
private void OnHistoryWeekClicked(object? sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.MainViewModel viewModel)
{
viewModel.ShowHistoryRange(7);
}
}
private void OnHistoryMonthClicked(object? sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.MainViewModel viewModel)
{
viewModel.ShowHistoryRange(30);
}
}
private async void OnUpdateClicked(object? sender, RoutedEventArgs e)
{
if (Application.Current is App app)
{
await app.CheckOrInstallUpdateFromUiAsync();
}
}
}