Browse Source

Switch SplitView.IsPaneOpen to a StyledProperty

Includes other small formatting changes and updates
pull/10138/head
robloo 4 years ago
parent
commit
553deb41fd
  1. 141
      src/Avalonia.Controls/SplitView.cs

141
src/Avalonia.Controls/SplitView.cs

@ -1,15 +1,13 @@
using Avalonia.Controls.Metadata; using System;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Metadata; using Avalonia.Metadata;
using Avalonia.VisualTree;
using System;
using Avalonia.Reactive;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates;
using Avalonia.LogicalTree;
namespace Avalonia.Controls namespace Avalonia.Controls
{ {
@ -54,10 +52,13 @@ namespace Avalonia.Controls
internal SplitViewTemplateSettings() { } internal SplitViewTemplateSettings() { }
public static readonly StyledProperty<double> ClosedPaneWidthProperty = public static readonly StyledProperty<double> ClosedPaneWidthProperty =
AvaloniaProperty.Register<SplitViewTemplateSettings, double>(nameof(ClosedPaneWidth), 0d); AvaloniaProperty.Register<SplitViewTemplateSettings,
double>(nameof(ClosedPaneWidth),
0d);
public static readonly StyledProperty<GridLength> PaneColumnGridLengthProperty = public static readonly StyledProperty<GridLength> PaneColumnGridLengthProperty =
AvaloniaProperty.Register<SplitViewTemplateSettings, GridLength>(nameof(PaneColumnGridLength)); AvaloniaProperty.Register<SplitViewTemplateSettings, GridLength>(
nameof(PaneColumnGridLength));
public double ClosedPaneWidth public double ClosedPaneWidth
{ {
@ -93,26 +94,34 @@ namespace Avalonia.Controls
/// Defines the <see cref="CompactPaneLength"/> property /// Defines the <see cref="CompactPaneLength"/> property
/// </summary> /// </summary>
public static readonly StyledProperty<double> CompactPaneLengthProperty = public static readonly StyledProperty<double> CompactPaneLengthProperty =
AvaloniaProperty.Register<SplitView, double>(nameof(CompactPaneLength), defaultValue: 48); AvaloniaProperty.Register<SplitView, double>(
nameof(CompactPaneLength),
defaultValue: 48);
/// <summary> /// <summary>
/// Defines the <see cref="DisplayMode"/> property /// Defines the <see cref="DisplayMode"/> property
/// </summary> /// </summary>
public static readonly StyledProperty<SplitViewDisplayMode> DisplayModeProperty = public static readonly StyledProperty<SplitViewDisplayMode> DisplayModeProperty =
AvaloniaProperty.Register<SplitView, SplitViewDisplayMode>(nameof(DisplayMode), defaultValue: SplitViewDisplayMode.Overlay); AvaloniaProperty.Register<SplitView, SplitViewDisplayMode>(
nameof(DisplayMode),
defaultValue: SplitViewDisplayMode.Overlay);
/// <summary> /// <summary>
/// Defines the <see cref="IsPaneOpen"/> property /// Defines the <see cref="IsPaneOpen"/> property
/// </summary> /// </summary>
public static readonly DirectProperty<SplitView, bool> IsPaneOpenProperty = public static readonly StyledProperty<bool> IsPaneOpenProperty =
AvaloniaProperty.RegisterDirect<SplitView, bool>(nameof(IsPaneOpen), AvaloniaProperty.Register<SplitView, bool>(
x => x.IsPaneOpen, (x, v) => x.IsPaneOpen = v); nameof(IsPaneOpen),
defaultValue: false,
coerce: CoerceIsPaneOpen);
/// <summary> /// <summary>
/// Defines the <see cref="OpenPaneLength"/> property /// Defines the <see cref="OpenPaneLength"/> property
/// </summary> /// </summary>
public static readonly StyledProperty<double> OpenPaneLengthProperty = public static readonly StyledProperty<double> OpenPaneLengthProperty =
AvaloniaProperty.Register<SplitView, double>(nameof(OpenPaneLength), defaultValue: 320); AvaloniaProperty.Register<SplitView, double>(
nameof(OpenPaneLength),
defaultValue: 320);
/// <summary> /// <summary>
/// Defines the <see cref="PaneBackground"/> property /// Defines the <see cref="PaneBackground"/> property
@ -150,7 +159,6 @@ namespace Avalonia.Controls
public static readonly StyledProperty<SplitViewTemplateSettings> TemplateSettingsProperty = public static readonly StyledProperty<SplitViewTemplateSettings> TemplateSettingsProperty =
AvaloniaProperty.Register<SplitView, SplitViewTemplateSettings>(nameof(TemplateSettings)); AvaloniaProperty.Register<SplitView, SplitViewTemplateSettings>(nameof(TemplateSettings));
private bool _isPaneOpen;
private Panel? _pane; private Panel? _pane;
private IDisposable? _pointerDisposable; private IDisposable? _pointerDisposable;
@ -169,7 +177,7 @@ namespace Avalonia.Controls
PanePlacementProperty.Changed.AddClassHandler<SplitView>((x, v) => x.OnPanePlacementChanged(v)); PanePlacementProperty.Changed.AddClassHandler<SplitView>((x, v) => x.OnPanePlacementChanged(v));
DisplayModeProperty.Changed.AddClassHandler<SplitView>((x, v) => x.OnDisplayModeChanged(v)); DisplayModeProperty.Changed.AddClassHandler<SplitView>((x, v) => x.OnDisplayModeChanged(v));
PaneProperty.Changed.AddClassHandler<SplitView>((x, e) => x.PaneChanged(e)); PaneProperty.Changed.AddClassHandler<SplitView>((x, e) => x.OnPaneChanged(e));
} }
/// <summary> /// <summary>
@ -196,37 +204,8 @@ namespace Avalonia.Controls
/// </summary> /// </summary>
public bool IsPaneOpen public bool IsPaneOpen
{ {
get => _isPaneOpen; get => GetValue(IsPaneOpenProperty);
set set => SetValue(IsPaneOpenProperty, value);
{
if (value == _isPaneOpen)
{
return;
}
if (value)
{
OnPaneOpening(EventArgs.Empty);
SetAndRaise(IsPaneOpenProperty, ref _isPaneOpen, value);
PseudoClasses.Add(":open");
PseudoClasses.Remove(":closed");
OnPaneOpened(EventArgs.Empty);
}
else
{
SplitViewPaneClosingEventArgs args = new SplitViewPaneClosingEventArgs(false);
OnPaneClosing(args);
if (!args.Cancel)
{
SetAndRaise(IsPaneOpenProperty, ref _isPaneOpen, value);
PseudoClasses.Add(":closed");
PseudoClasses.Remove(":open");
OnPaneClosed(EventArgs.Empty);
}
}
}
} }
/// <summary> /// <summary>
@ -351,6 +330,30 @@ namespace Avalonia.Controls
_pointerDisposable?.Dispose(); _pointerDisposable?.Dispose();
} }
/// <inheritdoc/>
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == IsPaneOpenProperty)
{
bool isPaneOpen = change.GetNewValue<bool>();
if (isPaneOpen)
{
PseudoClasses.Add(":open");
PseudoClasses.Remove(":closed");
OnPaneOpened(EventArgs.Empty);
}
else
{
PseudoClasses.Add(":closed");
PseudoClasses.Remove(":open");
OnPaneClosed(EventArgs.Empty);
}
}
}
private void PointerPressedOutside(object? sender, PointerPressedEventArgs e) private void PointerPressedOutside(object? sender, PointerPressedEventArgs e)
{ {
if (!IsPaneOpen) if (!IsPaneOpen)
@ -485,7 +488,7 @@ namespace Avalonia.Controls
PseudoClasses.Set(":lightdismiss", mode); PseudoClasses.Set(":lightdismiss", mode);
} }
private void PaneChanged(AvaloniaPropertyChangedEventArgs e) private void OnPaneChanged(AvaloniaPropertyChangedEventArgs e)
{ {
if (e.OldValue is ILogical oldChild) if (e.OldValue is ILogical oldChild)
{ {
@ -497,5 +500,45 @@ namespace Avalonia.Controls
LogicalChildren.Add(newChild); LogicalChildren.Add(newChild);
} }
} }
/// <summary>
/// Called when the <see cref="IsPaneOpen"/> property has to be coerced.
/// </summary>
/// <param name="value">The value to coerce.</param>
protected virtual bool OnCoerceIsPaneOpen(bool value)
{
if (value)
{
OnPaneOpening(EventArgs.Empty);
}
else
{
var eventArgs = new SplitViewPaneClosingEventArgs(false);
OnPaneClosing(eventArgs);
if (eventArgs.Cancel)
{
return !value;
}
}
return value;
}
/// <summary>
/// Coerces/validates the <see cref="IsPaneOpen"/> property value.
/// </summary>
/// <param name="instance">The <see cref="SplitView"/> instance.</param>
/// <param name="value">The value to coerce.</param>
/// <returns>The coerced/validated value.</returns>
private static bool CoerceIsPaneOpen(AvaloniaObject instance, bool value)
{
if (instance is SplitView splitView)
{
return splitView.OnCoerceIsPaneOpen(value);
}
return value;
}
} }
} }

Loading…
Cancel
Save