code_style: move platform dependent code to initialize window to namespace SourceGit.Native

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-05-12 21:52:50 +08:00
parent c62b4a031f
commit ef4b639f8e
No known key found for this signature in database
9 changed files with 41 additions and 68 deletions

View file

@ -6,6 +6,7 @@ using System.Runtime.Versioning;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Platform;
namespace SourceGit.Native
{
@ -19,7 +20,17 @@ namespace SourceGit.Native
public void SetupWindow(Window window)
{
// Do Nothing.
if (OS.UseSystemWindowFrame)
{
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.Default;
window.ExtendClientAreaToDecorationsHint = false;
}
else
{
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome;
window.ExtendClientAreaToDecorationsHint = true;
window.Classes.Add("custom_window_frame");
}
}
public string FindGitExecutable()

View file

@ -6,6 +6,7 @@ using System.Runtime.Versioning;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Platform;
namespace SourceGit.Native
{
@ -39,7 +40,8 @@ namespace SourceGit.Native
public void SetupWindow(Window window)
{
// Do Nothing.
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.SystemChrome;
window.ExtendClientAreaToDecorationsHint = true;
}
public string FindGitExecutable()

View file

@ -70,6 +70,12 @@ namespace SourceGit.Native
set;
} = [];
public static bool UseSystemWindowFrame
{
get => OperatingSystem.IsLinux() && _enableSystemWindowFrame;
set => _enableSystemWindowFrame = value;
}
static OS()
{
if (OperatingSystem.IsWindows())
@ -232,5 +238,6 @@ namespace SourceGit.Native
private static IBackend _backend = null;
private static string _gitExecutable = string.Empty;
private static bool _enableSystemWindowFrame = false;
}
}

View file

@ -8,6 +8,7 @@ using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Platform;
using Avalonia.Threading;
namespace SourceGit.Native
@ -15,18 +16,6 @@ namespace SourceGit.Native
[SupportedOSPlatform("windows")]
internal class Windows : OS.IBackend
{
[StructLayout(LayoutKind.Sequential)]
internal struct RTL_OSVERSIONINFOEX
{
internal uint dwOSVersionInfoSize;
internal uint dwMajorVersion;
internal uint dwMinorVersion;
internal uint dwBuildNumber;
internal uint dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
internal string szCSDVersion;
}
internal struct RECT
{
public int left;
@ -72,9 +61,6 @@ namespace SourceGit.Native
public int cyBottomHeight;
}
[DllImport("ntdll.dll")]
private static extern int RtlGetVersion(ref RTL_OSVERSIONINFOEX lpVersionInformation);
[DllImport("dwmapi.dll")]
private static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);
@ -96,9 +82,7 @@ namespace SourceGit.Native
public void SetupApp(AppBuilder builder)
{
// Fix drop shadow issue on Windows 10
RTL_OSVERSIONINFOEX v = new RTL_OSVERSIONINFOEX();
v.dwOSVersionInfoSize = (uint)Marshal.SizeOf<RTL_OSVERSIONINFOEX>();
if (RtlGetVersion(ref v) == 0 && (v.dwMajorVersion < 10 || v.dwBuildNumber < 22000))
if (!OperatingSystem.IsWindowsVersionAtLeast(10, 22000, 0))
{
Window.WindowStateProperty.Changed.AddClassHandler<Window>((w, _) => FixWindowFrameOnWin10(w));
Control.LoadedEvent.AddClassHandler<Window>((w, _) => FixWindowFrameOnWin10(w));
@ -107,6 +91,10 @@ namespace SourceGit.Native
public void SetupWindow(Window window)
{
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome;
window.ExtendClientAreaToDecorationsHint = true;
window.Classes.Add("fix_maximized_padding");
Win32Properties.AddWndProcHookCallback(window, (IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam, ref bool handled) =>
{
// Custom WM_NCHITTEST

View file

@ -91,8 +91,8 @@ namespace SourceGit.ViewModels
public bool UseSystemWindowFrame
{
get => _useSystemWindowFrame;
set => SetProperty(ref _useSystemWindowFrame, value);
get => Native.OS.UseSystemWindowFrame;
set => Native.OS.UseSystemWindowFrame = value;
}
public double DefaultFontSize
@ -656,7 +656,6 @@ namespace SourceGit.ViewModels
private string _defaultFontFamily = string.Empty;
private string _monospaceFontFamily = string.Empty;
private bool _onlyUseMonoFontInEditor = false;
private bool _useSystemWindowFrame = false;
private double _defaultFontSize = 13;
private double _editorFontSize = 13;
private int _editorTabWidth = 4;

View file

@ -3,7 +3,6 @@
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Platform;
namespace SourceGit.Views
{
@ -11,7 +10,7 @@ namespace SourceGit.Views
{
public bool UseSystemWindowFrame
{
get => OperatingSystem.IsLinux() && ViewModels.Preferences.Instance.UseSystemWindowFrame;
get => Native.OS.UseSystemWindowFrame;
}
protected override Type StyleKeyOverride => typeof(Window);
@ -19,33 +18,6 @@ namespace SourceGit.Views
public ChromelessWindow()
{
Focusable = true;
if (OperatingSystem.IsLinux())
{
if (UseSystemWindowFrame)
{
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.Default;
ExtendClientAreaToDecorationsHint = false;
}
else
{
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome;
ExtendClientAreaToDecorationsHint = true;
Classes.Add("custom_window_frame");
}
}
else if (OperatingSystem.IsWindows())
{
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome;
ExtendClientAreaToDecorationsHint = true;
Classes.Add("fix_maximized_padding");
}
else
{
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.SystemChrome;
ExtendClientAreaToDecorationsHint = true;
}
Native.OS.SetupForWindow(this);
}

View file

@ -89,7 +89,7 @@
<v:LauncherTabBar Grid.Column="2" Height="30" Margin="0,0,16,0" VerticalAlignment="Bottom"/>
<!-- Caption Buttons (Windows/Linux)-->
<Border Grid.Column="3" Margin="16,0,0,0" IsVisible="{Binding #ThisControl.IsRightCaptionButtonsVisible}">
<Border Grid.Column="3" Margin="16,0,0,0" IsVisible="{Binding #ThisControl.HasRightCaptionButton}">
<v:CaptionButtons Height="30" VerticalAlignment="Top"/>
</Border>
</Grid>

View file

@ -29,14 +29,9 @@ namespace SourceGit.Views
set => SetValue(HasLeftCaptionButtonProperty, value);
}
public bool IsRightCaptionButtonsVisible
public bool HasRightCaptionButton
{
get
{
if (OperatingSystem.IsLinux())
return !ViewModels.Preferences.Instance.UseSystemWindowFrame;
return OperatingSystem.IsWindows();
}
get => OperatingSystem.IsWindows() || !Native.OS.UseSystemWindowFrame;
}
public Launcher()
@ -52,8 +47,7 @@ namespace SourceGit.Views
{
HasLeftCaptionButton = true;
CaptionHeight = new GridLength(34);
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.SystemChrome |
ExtendClientAreaChromeHints.OSXThickTitleBar;
ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.SystemChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
}
else if (UseSystemWindowFrame)
{

View file

@ -129,23 +129,23 @@
<CheckBox Grid.Row="5" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preferences.General.ShowAuthorTime}"
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowAuthorTimeInGraph, Mode=TwoWay}"/>
IsChecked="{Binding ShowAuthorTimeInGraph, Mode=TwoWay}"/>
<CheckBox Grid.Row="6" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preferences.General.ShowTagsInGraph}"
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowTagsInGraph, Mode=TwoWay}"/>
IsChecked="{Binding ShowTagsInGraph, Mode=TwoWay}"/>
<CheckBox Grid.Row="7" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preferences.General.ShowChildren}"
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowChildren, Mode=TwoWay}"/>
IsChecked="{Binding ShowChildren, Mode=TwoWay}"/>
<CheckBox Grid.Row="8" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preferences.General.Check4UpdatesOnStartup}"
IsVisible="{x:Static s:App.IsCheckForUpdateCommandVisible}"
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=Check4UpdatesOnStartup, Mode=TwoWay}"/>
IsChecked="{Binding Check4UpdatesOnStartup, Mode=TwoWay}"/>
</Grid>
</TabItem>
@ -257,12 +257,12 @@
<CheckBox Grid.Row="7" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preferences.Appearance.UseFixedTabWidth}"
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseFixedTabWidth, Mode=TwoWay}"/>
IsChecked="{Binding UseFixedTabWidth, Mode=TwoWay}"/>
<CheckBox Grid.Row="8" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preferences.Appearance.UseNativeWindowFrame}"
IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSystemWindowFrame, Mode=OneTime}"
IsChecked="{Binding UseSystemWindowFrame, Mode=OneTime}"
IsVisible="{OnPlatform False, Linux=True}"
IsCheckedChanged="OnUseNativeWindowFrameChanged"/>
</Grid>