Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Grabacr07 committed Jun 3, 2023
1 parent f668cd3 commit 2509d94
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 71 deletions.
25 changes: 0 additions & 25 deletions src/MetroTrilithon.Desktop/UI/Interop/MaximizeButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,6 @@ public bool CanMaximize

#endregion

#region IsRestoreMode readonly dependency property

private static readonly DependencyPropertyKey IsRestoreModePropertyKey
= DependencyProperty.RegisterReadOnly(
nameof(IsRestoreMode),
typeof(bool),
typeof(MaximizeButton),
new PropertyMetadata(BooleanBoxes.FalseBox));

public static readonly DependencyProperty IsRestoreModeProperty
= IsRestoreModePropertyKey.DependencyProperty;

public bool IsRestoreMode
{
get => (bool)this.GetValue(IsRestoreModeProperty);
private set => this.SetValue(IsRestoreModePropertyKey, BooleanBoxes.Box(value));
}

#endregion

protected override void OnWindowStateChanged(object? sender, EventArgs e)
{
this.IsRestoreMode = this.Window.WindowState == WindowState.Maximized;
}

protected override void OnClick()
{
base.OnClick();
Expand Down
23 changes: 13 additions & 10 deletions src/MetroTrilithon.Desktop/UI/Interop/TitleBar.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Reactive.Disposables;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -99,10 +98,6 @@ public bool CanMinimize
public Window Window
=> this._window ??= this.GetWindow();


internal IReadOnlyCollection<TitleBarButton> KnownButtons
=> this._knownButtons;

public TitleBar()
{
this.Loaded += this.OnLoaded;
Expand All @@ -122,15 +117,23 @@ public override void OnApplyTemplate()
if (this.GetTemplateChild(PART_MaximizeButton) is MaximizeButton maximizeButton)
{
maximizeButton.TitleBar = this;
var binding = new Binding(nameof(this.CanMaximize)) { Source = this, };
var binding = new Binding(nameof(this.CanMaximize))
{
Source = this,
Mode = BindingMode.TwoWay,
};
BindingOperations.SetBinding(maximizeButton, MaximizeButton.CanMaximizeProperty, binding);
this._knownButtons.Add(maximizeButton);
}

if (this.GetTemplateChild(PART_MinimizeButton) is MinimizeButton minimizeButton)
{
minimizeButton.TitleBar = this;
var binding = new Binding(nameof(this.CanMinimize)) { Source = this, };
var binding = new Binding(nameof(this.CanMinimize))
{
Source = this,
Mode = BindingMode.TwoWay,
};
BindingOperations.SetBinding(minimizeButton, MinimizeButton.CanMinimizeProperty, binding);
this._knownButtons.Add(minimizeButton);
}
Expand Down Expand Up @@ -161,7 +164,7 @@ IntPtr IWndProcListener.WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lPar
return IntPtr.Zero;
}

var result = IntPtr.Zero;
var returnValue = IntPtr.Zero;
foreach (var button in this._knownButtons)
{
if (handled)
Expand All @@ -171,11 +174,11 @@ IntPtr IWndProcListener.WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lPar
}
else
{
result = ((IWndProcListener)button).WndProc(hwnd, msg, wParam, lParam, ref handled);
returnValue = ((IWndProcListener)button).WndProc(hwnd, msg, wParam, lParam, ref handled);
}
}

if (handled) return result;
if (handled) return returnValue;

switch ((WM)msg)
{
Expand Down
3 changes: 2 additions & 1 deletion src/MetroTrilithon.Desktop/UI/Interop/TitleBar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
Focusable="False" />

<StackPanel Grid.Column="1"
Orientation="Horizontal">
Orientation="Horizontal"
VerticalAlignment="Top">
<ctrls:MinimizeButton x:Name="PART_MinimizeButton" />
<ctrls:MaximizeButton x:Name="PART_MaximizeButton" />
<ctrls:CloseButton x:Name="PART_CloseButton" />
Expand Down
63 changes: 31 additions & 32 deletions src/MetroTrilithon.Desktop/UI/Interop/TitleBarButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Reflection;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using Windows.Win32;
using MetroTrilithon.Lifetime;
using MetroTrilithon.Linq;

namespace MetroTrilithon.UI.Interop;

Expand Down Expand Up @@ -40,23 +40,31 @@ protected Window Window
protected virtual NCHITTEST HitTestReturnValue
=> NCHITTEST.HTHELP;

#region IsActive readonly dependency property
#region WindowIsActive dependency property

private static readonly DependencyPropertyKey IsActivePropertyKey
= DependencyProperty.RegisterReadOnly(
nameof(IsActive),
public static readonly DependencyProperty WindowIsActiveProperty
= DependencyProperty.Register(
nameof(WindowIsActive),
typeof(bool),
typeof(TitleBarButton),
new PropertyMetadata(BooleanBoxes.FalseBox));

public static readonly DependencyProperty IsActiveProperty
= IsActivePropertyKey.DependencyProperty;
public bool WindowIsActive
=> (bool)this.GetValue(WindowIsActiveProperty);

public bool IsActive
{
get => (bool)this.GetValue(IsActiveProperty);
private set => this.SetValue(IsActivePropertyKey, BooleanBoxes.Box(value));
}
#endregion

#region WindowState dependency property

public static readonly DependencyProperty WindowStateProperty
= DependencyProperty.Register(
nameof(WindowState),
typeof(WindowState),
typeof(TitleBarButton),
new PropertyMetadata(default(WindowState)));

public WindowState WindowState
=> (WindowState)this.GetValue(WindowStateProperty);

#endregion

Expand Down Expand Up @@ -88,12 +96,17 @@ protected virtual void OnLoaded(object sender, RoutedEventArgs e)
_buttons.Add(this);
this._listener.Add(() => _buttons.Remove(this));

this.Window.StateChanged += this.OnWindowStateChanged;
this.Window.Activated += this.OnWindowActivated;
this.Window.Deactivated += this.OnWindowDeactivated;
this._listener.Add(() => this.Window.StateChanged -= this.OnWindowStateChanged);
this._listener.Add(() => this.Window.Activated -= this.OnWindowActivated);
this._listener.Add(() => this.Window.Deactivated -= this.OnWindowDeactivated);
var bindingIsActive = new Binding(nameof(System.Windows.Window.IsActive))
{
Source = this.Window,
};
this.SetBinding(WindowIsActiveProperty, bindingIsActive);

var bindingState = new Binding(nameof(System.Windows.Window.WindowState))
{
Source = this.Window,
};
this.SetBinding(WindowStateProperty, bindingState);
}

protected virtual void OnUnloaded(object sender, RoutedEventArgs e)
Expand All @@ -103,20 +116,6 @@ protected virtual void OnUnloaded(object sender, RoutedEventArgs e)
this._listener.Clear();
}

protected virtual void OnWindowActivated(object? sender, EventArgs e)
{
this.IsActive = true;
}

protected virtual void OnWindowDeactivated(object? sender, EventArgs e)
{
this.IsActive = false;
}

protected virtual void OnWindowStateChanged(object? sender, EventArgs e)
{
}

protected override void OnClick()
{
base.OnClick();
Expand Down
6 changes: 3 additions & 3 deletions src/MetroTrilithon.Desktop/UI/Interop/TitleBarButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsActive"
<Condition Property="WindowIsActive"
Value="False" />
<Condition Property="IsMouseOver"
Value="False" />
Expand Down Expand Up @@ -151,8 +151,8 @@
</Viewbox>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsRestoreMode"
Value="True">
<Trigger Property="WindowState"
Value="Maximized">
<Setter TargetName="CanvasPath"
Property="Data"
Value="M10.62,72a9.92,9.92,0,0,1-4-.86A11.15,11.15,0,0,1,.86,65.43,9.92,9.92,0,0,1,0,61.38V25A9.86,9.86,0,0,1,.86,21,11.32,11.32,0,0,1,3.18,17.6a11,11,0,0,1,3.38-2.32,9.68,9.68,0,0,1,4.06-.87H47a9.84,9.84,0,0,1,4.08.87A11,11,0,0,1,56.72,21,9.84,9.84,0,0,1,57.59,25V61.38a9.68,9.68,0,0,1-.87,4.06,11,11,0,0,1-2.32,3.38A11.32,11.32,0,0,1,51,71.14,9.86,9.86,0,0,1,47,72Zm36.17-7.21a3.39,3.39,0,0,0,1.39-.28,3.79,3.79,0,0,0,1.16-.77,3.47,3.47,0,0,0,1.07-2.53v-36a3.55,3.55,0,0,0-.28-1.41,3.51,3.51,0,0,0-.77-1.16,3.67,3.67,0,0,0-1.16-.77,3.55,3.55,0,0,0-1.41-.28h-36a3.45,3.45,0,0,0-1.39.28,3.59,3.59,0,0,0-1.14.79,3.79,3.79,0,0,0-.77,1.16,3.39,3.39,0,0,0-.28,1.39v36a3.45,3.45,0,0,0,.28,1.39A3.62,3.62,0,0,0,9.4,64.51a3.45,3.45,0,0,0,1.39.28Zm18-43.45a13.14,13.14,0,0,0-1.16-5.5,14.41,14.41,0,0,0-3.14-4.5,15,15,0,0,0-4.61-3,14.14,14.14,0,0,0-5.5-1.1H15A10.73,10.73,0,0,1,21.88.51,10.93,10.93,0,0,1,25.21,0H50.38a20.82,20.82,0,0,1,8.4,1.71A21.72,21.72,0,0,1,70.29,13.18,20.91,20.91,0,0,1,72,21.59v25.2a10.93,10.93,0,0,1-.51,3.33A10.71,10.71,0,0,1,70,53.05a10.84,10.84,0,0,1-2.28,2.36,10.66,10.66,0,0,1-3,1.58Z" />
Expand Down

0 comments on commit 2509d94

Please sign in to comment.