Skip to content

Commit

Permalink
fix: crushes when new SukiWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraZiling committed Aug 6, 2024
1 parent c28b8b3 commit b2ac08a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
10 changes: 6 additions & 4 deletions SukiUI/Controls/SukiWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
Style="{TemplateBinding BackgroundStyle}"
TransitionTime="{TemplateBinding BackgroundTransitionTime}"
TransitionsEnabled="{TemplateBinding BackgroundTransitionsEnabled}" />
<Panel Background="White" Opacity="0.1" IsVisible="{DynamicResource IsLight}" />

<Panel Background="White"
IsVisible="{DynamicResource IsLight}"
Opacity="0.1" />

<DockPanel LastChildFill="True">
<Panel DockPanel.Dock="Top">
<Panel.Styles>
Expand Down Expand Up @@ -78,7 +80,7 @@
<ItemsControl ItemsSource="{TemplateBinding RightWindowTitleBarControls}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft"/>
<StackPanel FlowDirection="RightToLeft" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Expand Down Expand Up @@ -116,7 +118,7 @@
</Border>
</ControlTemplate>
</Setter>

<Style Selector="^[WindowState=Maximized] /template/ PathIcon#MaximizeIcon">
<Setter Property="Data" Value="{x:Static icons:Icons.WindowRestore}" />
</Style>
Expand Down
63 changes: 29 additions & 34 deletions SukiUI/Controls/SukiWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public double TitleFontSize
}

public static readonly StyledProperty<FontWeight> TitleFontWeightProperty =
AvaloniaProperty.Register<SukiWindow, FontWeight>(nameof(TitleFontWeight),
defaultValue: FontWeight.Bold);
AvaloniaProperty.Register<SukiWindow, FontWeight>(nameof(TitleFontWeight), defaultValue: FontWeight.Bold);

public FontWeight TitleFontWeight
{
Expand Down Expand Up @@ -83,8 +82,7 @@ public AvaloniaList<MenuItem>? MenuItems
get => GetValue(MenuItemsProperty);
set => SetValue(MenuItemsProperty, value);
}



public static readonly StyledProperty<bool> CanMinimizeProperty =
AvaloniaProperty.Register<SukiWindow, bool>(nameof(CanMinimize), defaultValue: true);

Expand All @@ -103,7 +101,7 @@ public bool CanMove
set => SetValue(CanMoveProperty, value);
}

// BACKGROUND PROPERTIES
// Background properties
public static readonly StyledProperty<bool> BackgroundAnimationEnabledProperty =
AvaloniaProperty.Register<SukiWindow, bool>(nameof(BackgroundAnimationEnabled), defaultValue: false);

Expand All @@ -116,7 +114,6 @@ public bool BackgroundAnimationEnabled
public static readonly StyledProperty<SukiBackgroundStyle> BackgroundStyleProperty =
AvaloniaProperty.Register<SukiWindow, SukiBackgroundStyle>(nameof(BackgroundStyle),
defaultValue: SukiBackgroundStyle.Bubble);


/// <inheritdoc cref="SukiBackground.Style"/>
public SukiBackgroundStyle BackgroundStyle
Expand All @@ -137,7 +134,6 @@ public string? BackgroundShaderFile

public static readonly StyledProperty<string?> BackgroundShaderCodeProperty =
AvaloniaProperty.Register<SukiWindow, string?>(nameof(BackgroundShaderCode));


/// <inheritdoc cref="SukiBackground.ShaderCode"/>
public string? BackgroundShaderCode
Expand Down Expand Up @@ -166,10 +162,13 @@ public double BackgroundTransitionTime
set => SetValue(BackgroundTransitionTimeProperty, value);
}

public static readonly StyledProperty<Avalonia.Controls.Controls> RightWindowTitleBarControlsProperty = AvaloniaProperty.Register<SukiWindow, Avalonia.Controls.Controls>(nameof(RightWindowTitleBarControls), defaultValue: new Avalonia.Controls.Controls());
public static readonly StyledProperty<Avalonia.Controls.Controls> RightWindowTitleBarControlsProperty =
AvaloniaProperty.Register<SukiWindow, Avalonia.Controls.Controls>(nameof(RightWindowTitleBarControls),
defaultValue: new Avalonia.Controls.Controls());

/// <summary>
/// Controls that are displayed on the right side of the title bar, to the left of the normal window control buttons. (Displays provided controls right-to-left)
/// Controls that are displayed on the right side of the title bar,
/// to the left of the normal window control buttons. (Displays provided controls right-to-left)
/// </summary>
public Avalonia.Controls.Controls RightWindowTitleBarControls
{
Expand All @@ -180,25 +179,25 @@ public Avalonia.Controls.Controls RightWindowTitleBarControls
public SukiWindow()
{
MenuItems = new AvaloniaList<MenuItem>();
RightWindowTitleBarControls = new Avalonia.Controls.Controls();
}

private IDisposable? _subscriptionDisposables;
private SukiBackground? _background;

protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);

if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
return;
if (desktop.MainWindow is SukiWindow s && s != this)
if (desktop.MainWindow is SukiWindow window && window != this)
{
if (Icon == null) Icon = s.Icon;
Icon ??= window.Icon;
// This would be nice to do, but obviously LogoContent is a control and you can't attach it twice.
// if (LogoContent is null) LogoContent = s.LogoContent;
}
}



protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
Expand All @@ -213,10 +212,12 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
if (e.NameScope.Get<Button>("PART_MaximizeButton") is { } maximize)
{
maximize.Click += OnMaximizeButtonClicked;
bool pointerOnMaxButton = false;
var setter = typeof(Button).GetProperty("IsPointerOver");
var pointerOnMaxButton = false;
var setter = typeof(Button).GetProperty("IsPointerOver");

var proc = (IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam, ref bool handled) =>
// Windows Snap Layout
var proc =
(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam, ref bool handled) =>
{
switch (msg)
{
Expand All @@ -231,17 +232,17 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
var point = new PixelPoint(
(short)(ToInt32(lParam) & 0xffff),
(short)(ToInt32(lParam) >> 16));
var buttonLeftTop = maximize.PointToScreen(new(0, 0));
var x = (buttonLeftTop.X - point.X) / RenderScaling;
var y = (point.Y - buttonLeftTop.Y) / RenderScaling;
var buttonLeftTop = maximize.PointToScreen(new Point(0, 0));
var x = (buttonLeftTop.X - point.X) / RenderScaling;
var y = (point.Y - buttonLeftTop.Y) / RenderScaling;
if (new Rect(0, 0,
maximize.DesiredSize.Width,
maximize.DesiredSize.Height)
.Contains(new Point(x, y)))
{
setter?.SetValue(maximize, true);
pointerOnMaxButton = true;
handled = true;
pointerOnMaxButton = true;
handled = true;
return (IntPtr)9;
}

Expand All @@ -252,21 +253,15 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)

return IntPtr.Zero;

static int ToInt32(IntPtr ptr) => IntPtr.Size == 4 ? ptr.ToInt32() : (int)(ptr.ToInt64() & 0xffffffff);
static int ToInt32(IntPtr ptr) => IntPtr.Size == 4
? ptr.ToInt32() : (int)(ptr.ToInt64() & 0xffffffff);
};



Win32Properties.AddWndProcHookCallback(this, new Win32Properties.CustomWndProcHookCallback(proc));
}

if (e.NameScope.Get<Button>("PART_MinimizeButton") is { } minimize)
minimize.Click += (_, _) =>
{

WindowState = WindowState.Minimized;

};
minimize.Click += (_, _) => WindowState = WindowState.Minimized;

if (e.NameScope.Get<Button>("PART_CloseButton") is { } close)
close.Click += (_, _) => Close();
Expand All @@ -279,15 +274,16 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
}
catch
{
// ignored
}
}

private void OnMaximizeButtonClicked(object? sender, RoutedEventArgs args)
{
if (!CanResize) return;
WindowState = WindowState == WindowState.Maximized
? WindowState.Normal
: WindowState.Maximized;
? WindowState.Normal
: WindowState.Maximized;
}

private void OnWindowStateChanged(WindowState state)
Expand All @@ -307,5 +303,4 @@ protected override void OnUnloaded(RoutedEventArgs e)
base.OnUnloaded(e);
_subscriptionDisposables?.Dispose();
}

}

0 comments on commit b2ac08a

Please sign in to comment.