-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathChromelessWindow.cs
More file actions
155 lines (132 loc) · 4.85 KB
/
Copy pathChromelessWindow.cs
File metadata and controls
155 lines (132 loc) · 4.85 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace SourceGit.Views
{
public class ChromelessWindow : Window
{
public bool UseSystemWindowFrame
{
get => Native.OS.UseSystemWindowFrame;
}
public bool CloseOnESC
{
get;
set;
} = false;
protected override Type StyleKeyOverride => typeof(Window);
public ChromelessWindow()
{
Focusable = true;
Native.OS.SetupForWindow(this);
if (OperatingSystem.IsLinux() && !UseSystemWindowFrame)
{
PositionChanged += (_, _) => Native.LinuxUtilities.UpdateCustomWindowFrameStyle(this);
}
}
public void BeginMoveWindow(object _, PointerPressedEventArgs e)
{
if (e.ClickCount == 1)
BeginMoveDrag(e);
e.Handled = true;
}
public void MaximizeOrRestoreWindow(object _, TappedEventArgs e)
{
if (WindowState == WindowState.Maximized)
WindowState = WindowState.Normal;
else
WindowState = WindowState.Maximized;
e.Handled = true;
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
if (Classes.Contains("custom_window_frame") && CanResize)
{
string[] borderNames = [
"PART_BorderTopLeft",
"PART_BorderTop",
"PART_BorderTopRight",
"PART_BorderLeft",
"PART_BorderRight",
"PART_BorderBottomLeft",
"PART_BorderBottom",
"PART_BorderBottomRight",
];
foreach (var name in borderNames)
{
var border = e.NameScope.Find<Border>(name);
if (border != null)
{
border.PointerPressed -= OnWindowBorderPointerPressed;
border.PointerPressed += OnWindowBorderPointerPressed;
}
}
}
}
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
if (OperatingSystem.IsWindows())
Native.Win64Utilities.FixWindowFrame(this);
else if (OperatingSystem.IsLinux())
Native.LinuxUtilities.UpdateCustomWindowFrameStyle(this);
}
protected override void OnSizeChanged(SizeChangedEventArgs e)
{
base.OnSizeChanged(e);
if (OperatingSystem.IsLinux())
Native.LinuxUtilities.UpdateCustomWindowFrameStyle(this);
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (OperatingSystem.IsWindows() && change.Property == WindowStateProperty)
Native.Win64Utilities.FixWindowFrame(this);
else if (OperatingSystem.IsLinux() &&
Classes.Contains("custom_window_frame") &&
(change.Property == WindowStateProperty ||
change.Property == BoundsProperty ||
change.Property == WidthProperty ||
change.Property == HeightProperty))
{
Native.LinuxUtilities.UpdateCustomWindowFrameStyle(this);
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.Handled)
return;
if (e is { Key: Key.Escape, KeyModifiers: KeyModifiers.None } && CloseOnESC)
{
Close();
e.Handled = true;
return;
}
if (e.KeyModifiers == (OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
{
if (e.Key == Key.OemPlus)
{
var zoom = Math.Min(ViewModels.Preferences.Instance.Zoom + 0.05, 2.5);
ViewModels.Preferences.Instance.Zoom = zoom;
e.Handled = true;
}
else if (e.Key == Key.OemMinus)
{
var zoom = Math.Max(ViewModels.Preferences.Instance.Zoom - 0.05, 1);
ViewModels.Preferences.Instance.Zoom = zoom;
e.Handled = true;
}
}
}
private void OnWindowBorderPointerPressed(object sender, PointerPressedEventArgs e)
{
if (sender is Border { Tag: WindowEdge edge } && CanResize)
BeginResizeDrag(edge, e);
}
}
}