Skip to content

Commit

Permalink
Add TitleBarButtonAvailability
Browse files Browse the repository at this point in the history
  • Loading branch information
NotYoojun committed Apr 7, 2024
1 parent 3c1a32c commit 76518d0
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 5 deletions.
44 changes: 44 additions & 0 deletions source/iNKORE.UI.WPF.Modern/Controls/Helpers/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,50 @@ public static void SetCornerStyle(Window window, WindowCornerStyle value)
}


#endregion

#region ApplyBackground

public static readonly DependencyProperty ApplyBackgroundProperty =
DependencyProperty.RegisterAttached(
"ApplyBackground",
typeof(bool),
typeof(WindowHelper),
new PropertyMetadata(true));

public static bool GetApplyBackground(Window window)
{
return (bool)window.GetValue(ApplyBackgroundProperty);
}

public static void SetApplyBackground(Window window, bool value)
{
window.SetValue(ApplyBackgroundProperty, value);
}


#endregion

#region ApplyNoise

public static readonly DependencyProperty ApplyNoiseProperty =
DependencyProperty.RegisterAttached(
"ApplyNoise",
typeof(bool),
typeof(WindowHelper),
new PropertyMetadata(false));

public static bool GetApplyNoise(Window window)
{
return (bool)window.GetValue(ApplyNoiseProperty);
}

public static void SetApplyNoise(Window window, bool value)
{
window.SetValue(ApplyNoiseProperty, value);
}


#endregion


Expand Down
69 changes: 69 additions & 0 deletions source/iNKORE.UI.WPF.Modern/Controls/Primitives/TitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,66 @@ public static void SetHeight(Window window, double value)

#endregion

#region ButtonCloseAvailability

public static readonly DependencyProperty ButtonCloseAvailabilityProperty =
DependencyProperty.RegisterAttached(
"ButtonCloseAvailability",
typeof(TitleBarButtonAvailability),
typeof(TitleBar),
new PropertyMetadata(TitleBarButtonAvailability.Auto));


public static TitleBarButtonAvailability GetButtonCloseAvailability(Window window)
{
return (TitleBarButtonAvailability)window.GetValue(ButtonCloseAvailabilityProperty);
}

public static void SetButtonCloseAvailability(Window window, TitleBarButtonAvailability value)
{
window.SetValue(ButtonCloseAvailabilityProperty, value);
}

public static readonly DependencyProperty ButtonMaximizeAvailabilityProperty =
DependencyProperty.RegisterAttached(
"ButtonMaximizeAvailability",
typeof(TitleBarButtonAvailability),
typeof(TitleBar),
new PropertyMetadata(TitleBarButtonAvailability.Auto));


public static TitleBarButtonAvailability GetButtonMaximizeAvailability(Window window)
{
return (TitleBarButtonAvailability)window.GetValue(ButtonMaximizeAvailabilityProperty);
}

public static void SetButtonMaximizeAvailability(Window window, TitleBarButtonAvailability value)
{
window.SetValue(ButtonMaximizeAvailabilityProperty, value);
}

public static readonly DependencyProperty ButtonMinimizeAvailabilityProperty =
DependencyProperty.RegisterAttached(
"ButtonMinimizeAvailability",
typeof(TitleBarButtonAvailability),
typeof(TitleBar),
new PropertyMetadata(TitleBarButtonAvailability.Auto));


public static TitleBarButtonAvailability GetButtonMinimizeAvailability(Window window)
{
return (TitleBarButtonAvailability)window.GetValue(ButtonMinimizeAvailabilityProperty);
}

public static void SetButtonMinimizeAvailability(Window window, TitleBarButtonAvailability value)
{
window.SetValue(ButtonMinimizeAvailabilityProperty, value);
}

#endregion



//#region MaximizeButtonTouchOptimize

//public static readonly DependencyProperty MaximizeButtonTouchOptimizeProperty =
Expand Down Expand Up @@ -434,4 +494,13 @@ internal static void RaiseBackRequested(Window window)

#endregion
}


public enum TitleBarButtonAvailability
{
Auto,
Collapsed,
Disabled,
Enabled
}
}
17 changes: 17 additions & 0 deletions source/iNKORE.UI.WPF.Modern/Controls/Primitives/TitleBarButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,22 @@ public Brush PressedForeground
null);

#endregion

#region Availability

public TitleBarButtonAvailability Availability
{
get => (TitleBarButtonAvailability)GetValue(AvailabilityProperty);
set => SetValue(AvailabilityProperty, value);
}

public static readonly DependencyProperty AvailabilityProperty = DependencyProperty.Register(
nameof(Availability),
typeof(TitleBarButtonAvailability),
typeof(TitleBarButton),
new PropertyMetadata(TitleBarButtonAvailability.Enabled));

#endregion

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
<Setter TargetName="RootGrid" Property="Background" Value="{DynamicResource SystemControlDisabledTransparentBrush}" />
<Setter TargetName="Content" Property="TextElement.Foreground" Value="{DynamicResource SystemControlDisabledBaseMediumLowBrush}" />
</Trigger>

<Trigger Property="Availability" Value="Collapsed">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="Availability" Value="Disabled">
<Setter Property="IsEnabled" Value="false"/>
</Trigger>

</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
Expand Down
172 changes: 172 additions & 0 deletions source/iNKORE.UI.WPF.Modern/Controls/Primitives/TitleBarControl.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using iNKORE.UI.WPF.Modern.Helpers;
using iNKORE.UI.WPF.Modern.Helpers.Styles;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation.Peers;
Expand Down Expand Up @@ -30,6 +31,10 @@ public class TitleBarControl : Control
private SnapLayout _snapLayout;
private KeyBinding _altLeftBinding;


private static readonly DependencyPropertyDescriptor descriptor_WindowStyle = DependencyPropertyDescriptor.FromProperty(Window.WindowStyleProperty, typeof(Window));
private static readonly DependencyPropertyDescriptor descriptor_ResizeMode = DependencyPropertyDescriptor.FromProperty(Window.ResizeModeProperty, typeof(Window));

static TitleBarControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TitleBarControl),
Expand Down Expand Up @@ -340,6 +345,70 @@ internal static void SetInsideTitleBar(UIElement element, bool value)

#endregion

#region Button Availability

private static void ButtonAvailabilityProperty_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as TitleBarControl)?.RefreshButtonActualAvailabilities();
}

public static readonly DependencyProperty ButtonCloseAvailabilityProperty =
TitleBar.ButtonCloseAvailabilityProperty.AddOwner(typeof(TitleBarControl), new PropertyMetadata(TitleBarButtonAvailability.Auto, ButtonAvailabilityProperty_ValueChanged));

public TitleBarButtonAvailability ButtonCloseAvailability
{
get => (TitleBarButtonAvailability)GetValue(ButtonCloseAvailabilityProperty);
set => SetValue(ButtonCloseAvailabilityProperty, value);
}

// User-customized values

public static readonly DependencyProperty ButtonMinimizeAvailabilityProperty =
TitleBar.ButtonMinimizeAvailabilityProperty.AddOwner(typeof(TitleBarControl), new PropertyMetadata(TitleBarButtonAvailability.Auto, ButtonAvailabilityProperty_ValueChanged));

public TitleBarButtonAvailability ButtonMinimizeAvailability
{
get => (TitleBarButtonAvailability)GetValue(ButtonMinimizeAvailabilityProperty);
set => SetValue(ButtonMinimizeAvailabilityProperty, value);
}

public static readonly DependencyProperty ButtonMaximizeAvailabilityProperty =
TitleBar.ButtonMaximizeAvailabilityProperty.AddOwner(typeof(TitleBarControl), new PropertyMetadata(TitleBarButtonAvailability.Auto, ButtonAvailabilityProperty_ValueChanged));

public TitleBarButtonAvailability ButtonMaximizeAvailability
{
get => (TitleBarButtonAvailability)GetValue(ButtonMaximizeAvailabilityProperty);
set => SetValue(ButtonMaximizeAvailabilityProperty, value);
}

// Actual values

public static readonly DependencyPropertyKey ButtonCloseActualAvailabilityPropertyKey = DependencyProperty.RegisterReadOnly(nameof(ButtonCloseActualAvailability), typeof(TitleBarButtonAvailability), typeof(TitleBarControl), new PropertyMetadata(TitleBarButtonAvailability.Enabled));
public static readonly DependencyProperty ButtonCloseActualAvailabilityProperty = ButtonCloseActualAvailabilityPropertyKey.DependencyProperty;
public TitleBarButtonAvailability ButtonCloseActualAvailability
{
get => (TitleBarButtonAvailability)GetValue(ButtonCloseActualAvailabilityProperty);
private set => SetValue(ButtonCloseActualAvailabilityPropertyKey, value);
}

public static readonly DependencyPropertyKey ButtonMaximizeActualAvailabilityPropertyKey = DependencyProperty.RegisterReadOnly(nameof(ButtonMaximizeActualAvailability), typeof(TitleBarButtonAvailability), typeof(TitleBarControl), new PropertyMetadata(TitleBarButtonAvailability.Enabled));
public static readonly DependencyProperty ButtonMaximizeActualAvailabilityProperty = ButtonMaximizeActualAvailabilityPropertyKey.DependencyProperty;
public TitleBarButtonAvailability ButtonMaximizeActualAvailability
{
get => (TitleBarButtonAvailability)GetValue(ButtonMaximizeActualAvailabilityProperty);
private set => SetValue(ButtonMaximizeActualAvailabilityPropertyKey, value);
}

public static readonly DependencyPropertyKey ButtonMinimizeActualAvailabilityPropertyKey = DependencyProperty.RegisterReadOnly(nameof(ButtonMinimizeActualAvailability), typeof(TitleBarButtonAvailability), typeof(TitleBarControl), new PropertyMetadata(TitleBarButtonAvailability.Enabled));
public static readonly DependencyProperty ButtonMinimizeActualAvailabilityProperty = ButtonMinimizeActualAvailabilityPropertyKey.DependencyProperty;
public TitleBarButtonAvailability ButtonMinimizeActualAvailability
{
get => (TitleBarButtonAvailability)GetValue(ButtonMinimizeActualAvailabilityProperty);
private set => SetValue(ButtonMinimizeActualAvailabilityPropertyKey, value);
}

#endregion

private Button BackButton { get; set; }

private TitleBarButton MaximizeRestoreButton { get; set; }
Expand Down Expand Up @@ -410,6 +479,9 @@ protected override void OnVisualParentChanged(DependencyObject oldParent)
{
if (_parentWindow != null)
{
descriptor_ResizeMode.RemoveValueChanged(_parentWindow, _window_ButtonAvailabilityShouldRefresh);
descriptor_WindowStyle.RemoveValueChanged(_parentWindow, _window_ButtonAvailabilityShouldRefresh);

if (_altLeftBinding != null)
{
_parentWindow.InputBindings.Remove(_altLeftBinding);
Expand All @@ -425,6 +497,18 @@ protected override void OnVisualParentChanged(DependencyObject oldParent)
{
_altLeftBinding = new KeyBinding(new GoBackCommand(this), Key.Left, ModifierKeys.Alt);
_parentWindow.InputBindings.Add(_altLeftBinding);
descriptor_ResizeMode.AddValueChanged(_parentWindow, _window_ButtonAvailabilityShouldRefresh);
descriptor_WindowStyle.AddValueChanged(_parentWindow, _window_ButtonAvailabilityShouldRefresh);

RefreshButtonActualAvailabilities();
}
}

private void _window_ButtonAvailabilityShouldRefresh(object sender, EventArgs e)
{
if(sender == _parentWindow)
{
RefreshButtonActualAvailabilities();
}
}

Expand All @@ -447,6 +531,94 @@ private void OnBackButtonClick(object sender, RoutedEventArgs e)
}
}

public void RefreshButtonActualAvailabilities()
{

// Close button
if (ButtonCloseAvailability != TitleBarButtonAvailability.Auto)
{
ButtonCloseActualAvailability = ButtonCloseAvailability;
}
else
{
if (_parentWindow.WindowStyle == WindowStyle.None)
{
ButtonCloseActualAvailability = TitleBarButtonAvailability.Collapsed;
}
else
{
ButtonCloseActualAvailability = TitleBarButtonAvailability.Enabled;
}
}

// Maximize button
if (ButtonMaximizeAvailability != TitleBarButtonAvailability.Auto)
{
ButtonMaximizeActualAvailability = ButtonMaximizeAvailability;
}
else
{
if(_parentWindow.WindowStyle == WindowStyle.ToolWindow)
{
ButtonMaximizeActualAvailability = TitleBarButtonAvailability.Collapsed;
}
else if (_parentWindow.WindowStyle == WindowStyle.None)
{
ButtonMaximizeActualAvailability = TitleBarButtonAvailability.Collapsed;
}
else
{
switch (_parentWindow.ResizeMode)
{
case ResizeMode.CanMinimize:
ButtonMaximizeActualAvailability = TitleBarButtonAvailability.Disabled;
break;
case ResizeMode.CanResizeWithGrip:
case ResizeMode.CanResize:
ButtonMaximizeActualAvailability = TitleBarButtonAvailability.Enabled;
break;
case ResizeMode.NoResize:
ButtonMaximizeActualAvailability = TitleBarButtonAvailability.Collapsed;
break;
}
}
}

// Minimize button
if (ButtonMinimizeAvailability != TitleBarButtonAvailability.Auto)
{
ButtonMinimizeActualAvailability = ButtonMinimizeAvailability;
}
else
{
if (_parentWindow.WindowStyle == WindowStyle.ToolWindow)
{
ButtonMinimizeActualAvailability = TitleBarButtonAvailability.Collapsed;
}
else if (_parentWindow.WindowStyle == WindowStyle.None)
{
ButtonMinimizeActualAvailability = TitleBarButtonAvailability.Collapsed;
}
else
{
switch (_parentWindow.ResizeMode)
{
case ResizeMode.CanMinimize:
case ResizeMode.CanResizeWithGrip:
case ResizeMode.CanResize:
ButtonMinimizeActualAvailability = TitleBarButtonAvailability.Enabled;
break;
case ResizeMode.NoResize:
ButtonMinimizeActualAvailability = TitleBarButtonAvailability.Collapsed;
break;
}
}
}


}


private void OnLeftSystemOverlaySizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateSystemOverlayLeftInset(e.NewSize.Width);
Expand Down
Loading

0 comments on commit 76518d0

Please sign in to comment.