Skip to content

Commit ef4b639

Browse files
committed
code_style: move platform dependent code to initialize window to namespace SourceGit.Native
Signed-off-by: leo <[email protected]>
1 parent c62b4a0 commit ef4b639

File tree

9 files changed

+41
-68
lines changed

9 files changed

+41
-68
lines changed

src/Native/Linux.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using Avalonia;
88
using Avalonia.Controls;
9+
using Avalonia.Platform;
910

1011
namespace SourceGit.Native
1112
{
@@ -19,7 +20,17 @@ public void SetupApp(AppBuilder builder)
1920

2021
public void SetupWindow(Window window)
2122
{
22-
// Do Nothing.
23+
if (OS.UseSystemWindowFrame)
24+
{
25+
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.Default;
26+
window.ExtendClientAreaToDecorationsHint = false;
27+
}
28+
else
29+
{
30+
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome;
31+
window.ExtendClientAreaToDecorationsHint = true;
32+
window.Classes.Add("custom_window_frame");
33+
}
2334
}
2435

2536
public string FindGitExecutable()

src/Native/MacOS.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using Avalonia;
88
using Avalonia.Controls;
9+
using Avalonia.Platform;
910

1011
namespace SourceGit.Native
1112
{
@@ -39,7 +40,8 @@ public void SetupApp(AppBuilder builder)
3940

4041
public void SetupWindow(Window window)
4142
{
42-
// Do Nothing.
43+
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.SystemChrome;
44+
window.ExtendClientAreaToDecorationsHint = true;
4345
}
4446

4547
public string FindGitExecutable()

src/Native/OS.cs

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public static List<Models.ExternalTool> ExternalTools
7070
set;
7171
} = [];
7272

73+
public static bool UseSystemWindowFrame
74+
{
75+
get => OperatingSystem.IsLinux() && _enableSystemWindowFrame;
76+
set => _enableSystemWindowFrame = value;
77+
}
78+
7379
static OS()
7480
{
7581
if (OperatingSystem.IsWindows())
@@ -232,5 +238,6 @@ private static void UpdateGitVersion()
232238

233239
private static IBackend _backend = null;
234240
private static string _gitExecutable = string.Empty;
241+
private static bool _enableSystemWindowFrame = false;
235242
}
236243
}

src/Native/Windows.cs

+6-18
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,14 @@
88

99
using Avalonia;
1010
using Avalonia.Controls;
11+
using Avalonia.Platform;
1112
using Avalonia.Threading;
1213

1314
namespace SourceGit.Native
1415
{
1516
[SupportedOSPlatform("windows")]
1617
internal class Windows : OS.IBackend
1718
{
18-
[StructLayout(LayoutKind.Sequential)]
19-
internal struct RTL_OSVERSIONINFOEX
20-
{
21-
internal uint dwOSVersionInfoSize;
22-
internal uint dwMajorVersion;
23-
internal uint dwMinorVersion;
24-
internal uint dwBuildNumber;
25-
internal uint dwPlatformId;
26-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
27-
internal string szCSDVersion;
28-
}
29-
3019
internal struct RECT
3120
{
3221
public int left;
@@ -72,9 +61,6 @@ internal struct MARGINS
7261
public int cyBottomHeight;
7362
}
7463

75-
[DllImport("ntdll.dll")]
76-
private static extern int RtlGetVersion(ref RTL_OSVERSIONINFOEX lpVersionInformation);
77-
7864
[DllImport("dwmapi.dll")]
7965
private static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);
8066

@@ -96,9 +82,7 @@ internal struct MARGINS
9682
public void SetupApp(AppBuilder builder)
9783
{
9884
// Fix drop shadow issue on Windows 10
99-
RTL_OSVERSIONINFOEX v = new RTL_OSVERSIONINFOEX();
100-
v.dwOSVersionInfoSize = (uint)Marshal.SizeOf<RTL_OSVERSIONINFOEX>();
101-
if (RtlGetVersion(ref v) == 0 && (v.dwMajorVersion < 10 || v.dwBuildNumber < 22000))
85+
if (!OperatingSystem.IsWindowsVersionAtLeast(10, 22000, 0))
10286
{
10387
Window.WindowStateProperty.Changed.AddClassHandler<Window>((w, _) => FixWindowFrameOnWin10(w));
10488
Control.LoadedEvent.AddClassHandler<Window>((w, _) => FixWindowFrameOnWin10(w));
@@ -107,6 +91,10 @@ public void SetupApp(AppBuilder builder)
10791

10892
public void SetupWindow(Window window)
10993
{
94+
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome;
95+
window.ExtendClientAreaToDecorationsHint = true;
96+
window.Classes.Add("fix_maximized_padding");
97+
11098
Win32Properties.AddWndProcHookCallback(window, (IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam, ref bool handled) =>
11199
{
112100
// Custom WM_NCHITTEST

src/ViewModels/Preferences.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public bool OnlyUseMonoFontInEditor
9191

9292
public bool UseSystemWindowFrame
9393
{
94-
get => _useSystemWindowFrame;
95-
set => SetProperty(ref _useSystemWindowFrame, value);
94+
get => Native.OS.UseSystemWindowFrame;
95+
set => Native.OS.UseSystemWindowFrame = value;
9696
}
9797

9898
public double DefaultFontSize
@@ -656,7 +656,6 @@ private bool RemoveInvalidRepositoriesRecursive(List<RepositoryNode> collection)
656656
private string _defaultFontFamily = string.Empty;
657657
private string _monospaceFontFamily = string.Empty;
658658
private bool _onlyUseMonoFontInEditor = false;
659-
private bool _useSystemWindowFrame = false;
660659
private double _defaultFontSize = 13;
661660
private double _editorFontSize = 13;
662661
private int _editorTabWidth = 4;

src/Views/ChromelessWindow.cs

+1-29
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,21 @@
33
using Avalonia.Controls;
44
using Avalonia.Controls.Primitives;
55
using Avalonia.Input;
6-
using Avalonia.Platform;
76

87
namespace SourceGit.Views
98
{
109
public class ChromelessWindow : Window
1110
{
1211
public bool UseSystemWindowFrame
1312
{
14-
get => OperatingSystem.IsLinux() && ViewModels.Preferences.Instance.UseSystemWindowFrame;
13+
get => Native.OS.UseSystemWindowFrame;
1514
}
1615

1716
protected override Type StyleKeyOverride => typeof(Window);
1817

1918
public ChromelessWindow()
2019
{
2120
Focusable = true;
22-
23-
if (OperatingSystem.IsLinux())
24-
{
25-
if (UseSystemWindowFrame)
26-
{
27-
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.Default;
28-
ExtendClientAreaToDecorationsHint = false;
29-
}
30-
else
31-
{
32-
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome;
33-
ExtendClientAreaToDecorationsHint = true;
34-
Classes.Add("custom_window_frame");
35-
}
36-
}
37-
else if (OperatingSystem.IsWindows())
38-
{
39-
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome;
40-
ExtendClientAreaToDecorationsHint = true;
41-
Classes.Add("fix_maximized_padding");
42-
}
43-
else
44-
{
45-
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.SystemChrome;
46-
ExtendClientAreaToDecorationsHint = true;
47-
}
48-
4921
Native.OS.SetupForWindow(this);
5022
}
5123

src/Views/Launcher.axaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<v:LauncherTabBar Grid.Column="2" Height="30" Margin="0,0,16,0" VerticalAlignment="Bottom"/>
9090

9191
<!-- Caption Buttons (Windows/Linux)-->
92-
<Border Grid.Column="3" Margin="16,0,0,0" IsVisible="{Binding #ThisControl.IsRightCaptionButtonsVisible}">
92+
<Border Grid.Column="3" Margin="16,0,0,0" IsVisible="{Binding #ThisControl.HasRightCaptionButton}">
9393
<v:CaptionButtons Height="30" VerticalAlignment="Top"/>
9494
</Border>
9595
</Grid>

src/Views/Launcher.axaml.cs

+3-9
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,9 @@ public bool HasLeftCaptionButton
2929
set => SetValue(HasLeftCaptionButtonProperty, value);
3030
}
3131

32-
public bool IsRightCaptionButtonsVisible
32+
public bool HasRightCaptionButton
3333
{
34-
get
35-
{
36-
if (OperatingSystem.IsLinux())
37-
return !ViewModels.Preferences.Instance.UseSystemWindowFrame;
38-
return OperatingSystem.IsWindows();
39-
}
34+
get => OperatingSystem.IsWindows() || !Native.OS.UseSystemWindowFrame;
4035
}
4136

4237
public Launcher()
@@ -52,8 +47,7 @@ public Launcher()
5247
{
5348
HasLeftCaptionButton = true;
5449
CaptionHeight = new GridLength(34);
55-
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.SystemChrome |
56-
ExtendClientAreaChromeHints.OSXThickTitleBar;
50+
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.SystemChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
5751
}
5852
else if (UseSystemWindowFrame)
5953
{

src/Views/Preferences.axaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,23 @@
129129
<CheckBox Grid.Row="5" Grid.Column="1"
130130
Height="32"
131131
Content="{DynamicResource Text.Preferences.General.ShowAuthorTime}"
132-
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowAuthorTimeInGraph, Mode=TwoWay}"/>
132+
IsChecked="{Binding ShowAuthorTimeInGraph, Mode=TwoWay}"/>
133133

134134
<CheckBox Grid.Row="6" Grid.Column="1"
135135
Height="32"
136136
Content="{DynamicResource Text.Preferences.General.ShowTagsInGraph}"
137-
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowTagsInGraph, Mode=TwoWay}"/>
137+
IsChecked="{Binding ShowTagsInGraph, Mode=TwoWay}"/>
138138

139139
<CheckBox Grid.Row="7" Grid.Column="1"
140140
Height="32"
141141
Content="{DynamicResource Text.Preferences.General.ShowChildren}"
142-
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowChildren, Mode=TwoWay}"/>
142+
IsChecked="{Binding ShowChildren, Mode=TwoWay}"/>
143143

144144
<CheckBox Grid.Row="8" Grid.Column="1"
145145
Height="32"
146146
Content="{DynamicResource Text.Preferences.General.Check4UpdatesOnStartup}"
147147
IsVisible="{x:Static s:App.IsCheckForUpdateCommandVisible}"
148-
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=Check4UpdatesOnStartup, Mode=TwoWay}"/>
148+
IsChecked="{Binding Check4UpdatesOnStartup, Mode=TwoWay}"/>
149149
</Grid>
150150
</TabItem>
151151

@@ -257,12 +257,12 @@
257257
<CheckBox Grid.Row="7" Grid.Column="1"
258258
Height="32"
259259
Content="{DynamicResource Text.Preferences.Appearance.UseFixedTabWidth}"
260-
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseFixedTabWidth, Mode=TwoWay}"/>
260+
IsChecked="{Binding UseFixedTabWidth, Mode=TwoWay}"/>
261261

262262
<CheckBox Grid.Row="8" Grid.Column="1"
263263
Height="32"
264264
Content="{DynamicResource Text.Preferences.Appearance.UseNativeWindowFrame}"
265-
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSystemWindowFrame, Mode=OneTime}"
265+
IsChecked="{Binding UseSystemWindowFrame, Mode=OneTime}"
266266
IsVisible="{OnPlatform False, Linux=True}"
267267
IsCheckedChanged="OnUseNativeWindowFrameChanged"/>
268268
</Grid>

0 commit comments

Comments
 (0)