Browse Source

Fix: Allow ColorPicker-Fylout to swap position if needed (#13567)

* Add `PlacementConstraintAdjustmentProperty` to PopupFlyoutBase

This adds much more flexibility to control flyout positioning when there is not enough space to show the Flyout.

* Make Fylout position of the ColorPicker a DynamicResource

This allows the position to be overridden easily. It should no longer be hard coded.

* Address review
pull/13596/head
Tim 2 years ago
committed by GitHub
parent
commit
4007dfa0b9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml
  2. 7
      src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPicker.xaml
  3. 17
      src/Avalonia.Controls/Flyouts/PopupFlyoutBase.cs

6
src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml

@ -1,9 +1,11 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" <ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Avalonia.Controls" xmlns:controls="using:Avalonia.Controls"
xmlns:primitives="using:Avalonia.Controls.Primitives" xmlns:primitives="using:Avalonia.Controls.Primitives"
x:ClassModifier="internal"> x:ClassModifier="internal">
<PlacementMode x:Key="ColorPickerFlyoutPlacement">Top</PlacementMode>
<ControlTheme x:Key="{x:Type ColorPicker}" <ControlTheme x:Key="{x:Type ColorPicker}"
TargetType="ColorPicker"> TargetType="ColorPicker">
<Setter Property="CornerRadius" Value="{DynamicResource ControlCornerRadius}" /> <Setter Property="CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
@ -51,7 +53,7 @@
</DropDownButton.Styles> </DropDownButton.Styles>
<DropDownButton.Flyout> <DropDownButton.Flyout>
<Flyout FlyoutPresenterClasses="nopadding" <Flyout FlyoutPresenterClasses="nopadding"
Placement="Top"> Placement="{DynamicResource ColorPickerFlyoutPlacement}">
<!-- The following is copy-pasted from the ColorView's control template. <!-- The following is copy-pasted from the ColorView's control template.
It MUST always be kept in sync with the ColorView (which is master). It MUST always be kept in sync with the ColorView (which is master).

7
src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPicker.xaml

@ -1,8 +1,10 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" <ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Avalonia.Controls" xmlns:controls="using:Avalonia.Controls"
xmlns:primitives="using:Avalonia.Controls.Primitives" xmlns:primitives="using:Avalonia.Controls.Primitives"
x:ClassModifier="internal"> x:ClassModifier="internal">
<PlacementMode x:Key="ColorPickerFlyoutPlacement">Top</PlacementMode>
<ControlTheme x:Key="{x:Type ColorPicker}" <ControlTheme x:Key="{x:Type ColorPicker}"
TargetType="ColorPicker"> TargetType="ColorPicker">
@ -50,7 +52,8 @@
</Style> </Style>
</DropDownButton.Styles> </DropDownButton.Styles>
<DropDownButton.Flyout> <DropDownButton.Flyout>
<Flyout Placement="Top"> <Flyout FlyoutPresenterClasses="nopadding"
Placement="{DynamicResource ColorPickerFlyoutPlacement}">
<!-- The following is copy-pasted from the ColorView's control template. <!-- The following is copy-pasted from the ColorView's control template.
It MUST always be kept in sync with the ColorView (which is master). It MUST always be kept in sync with the ColorView (which is master).

17
src/Avalonia.Controls/Flyouts/PopupFlyoutBase.cs

@ -47,6 +47,12 @@ namespace Avalonia.Controls.Primitives
public static readonly StyledProperty<IInputElement?> OverlayInputPassThroughElementProperty = public static readonly StyledProperty<IInputElement?> OverlayInputPassThroughElementProperty =
Popup.OverlayInputPassThroughElementProperty.AddOwner<PopupFlyoutBase>(); Popup.OverlayInputPassThroughElementProperty.AddOwner<PopupFlyoutBase>();
/// <summary>
/// Defines the <see cref="PlacementConstraintAdjustment"/> property
/// </summary>
public static readonly StyledProperty<PopupPositionerConstraintAdjustment> PlacementConstraintAdjustmentProperty =
Popup.PlacementConstraintAdjustmentProperty.AddOwner<PopupFlyoutBase>();
private readonly Lazy<Popup> _popupLazy; private readonly Lazy<Popup> _popupLazy;
private Rect? _enlargedPopupRect; private Rect? _enlargedPopupRect;
private PixelRect? _enlargePopupRectScreenPixelRect; private PixelRect? _enlargePopupRectScreenPixelRect;
@ -119,6 +125,13 @@ namespace Avalonia.Controls.Primitives
set => SetValue(OverlayInputPassThroughElementProperty, value); set => SetValue(OverlayInputPassThroughElementProperty, value);
} }
/// <inheritdoc cref="Popup.PlacementConstraintAdjustment"/>
public PopupPositionerConstraintAdjustment PlacementConstraintAdjustment
{
get => GetValue(PlacementConstraintAdjustmentProperty);
set => SetValue(PlacementConstraintAdjustmentProperty, value);
}
IPopupHost? IPopupHostProvider.PopupHost => Popup?.Host; IPopupHost? IPopupHostProvider.PopupHost => Popup?.Host;
event Action<IPopupHost?>? IPopupHostProvider.PopupHostChanged event Action<IPopupHost?>? IPopupHostProvider.PopupHostChanged
@ -424,9 +437,7 @@ namespace Avalonia.Controls.Primitives
else else
{ {
Popup.Placement = Placement; Popup.Placement = Placement;
Popup.PlacementConstraintAdjustment = Popup.PlacementConstraintAdjustment = PlacementConstraintAdjustment;
PopupPositioning.PopupPositionerConstraintAdjustment.SlideX |
PopupPositioning.PopupPositionerConstraintAdjustment.SlideY;
} }
} }

Loading…
Cancel
Save