Skip to content

Commit 42a739c

Browse files
timuniemaxkatz6
authored andcommitted
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
1 parent 70987be commit 42a739c

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
<ResourceDictionary xmlns="https://github.com/avaloniaui"
1+
<ResourceDictionary xmlns="https://github.com/avaloniaui"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:controls="using:Avalonia.Controls"
44
xmlns:primitives="using:Avalonia.Controls.Primitives"
55
x:ClassModifier="internal">
66

7+
<PlacementMode x:Key="ColorPickerFlyoutPlacement">Top</PlacementMode>
8+
79
<ControlTheme x:Key="{x:Type ColorPicker}"
810
TargetType="ColorPicker">
911
<Setter Property="CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
@@ -46,7 +48,7 @@
4648
</DropDownButton.Content>
4749
<DropDownButton.Flyout>
4850
<Flyout FlyoutPresenterClasses="nopadding"
49-
Placement="Top">
51+
Placement="{DynamicResource ColorPickerFlyoutPlacement}">
5052

5153
<!-- The following is copy-pasted from the ColorView's control template.
5254
It MUST always be kept in sync with the ColorView (which is master).

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
<ResourceDictionary xmlns="https://github.com/avaloniaui"
1+
<ResourceDictionary xmlns="https://github.com/avaloniaui"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:controls="using:Avalonia.Controls"
44
xmlns:primitives="using:Avalonia.Controls.Primitives"
55
x:ClassModifier="internal">
6+
7+
<PlacementMode x:Key="ColorPickerFlyoutPlacement">Top</PlacementMode>
68

79
<ControlTheme x:Key="{x:Type ColorPicker}"
810
TargetType="ColorPicker">
@@ -45,7 +47,8 @@
4547
</Panel>
4648
</DropDownButton.Content>
4749
<DropDownButton.Flyout>
48-
<Flyout Placement="Top">
50+
<Flyout FlyoutPresenterClasses="nopadding"
51+
Placement="{DynamicResource ColorPickerFlyoutPlacement}">
4952

5053
<!-- The following is copy-pasted from the ColorView's control template.
5154
It MUST always be kept in sync with the ColorView (which is master).

src/Avalonia.Controls/Flyouts/PopupFlyoutBase.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public abstract class PopupFlyoutBase : FlyoutBase, IPopupHostProvider
4747
public static readonly StyledProperty<IInputElement?> OverlayInputPassThroughElementProperty =
4848
Popup.OverlayInputPassThroughElementProperty.AddOwner<PopupFlyoutBase>();
4949

50+
/// <summary>
51+
/// Defines the <see cref="PlacementConstraintAdjustment"/> property
52+
/// </summary>
53+
public static readonly StyledProperty<PopupPositionerConstraintAdjustment> PlacementConstraintAdjustmentProperty =
54+
Popup.PlacementConstraintAdjustmentProperty.AddOwner<PopupFlyoutBase>();
55+
5056
private readonly Lazy<Popup> _popupLazy;
5157
private Rect? _enlargedPopupRect;
5258
private PixelRect? _enlargePopupRectScreenPixelRect;
@@ -119,6 +125,13 @@ public IInputElement? OverlayInputPassThroughElement
119125
set => SetValue(OverlayInputPassThroughElementProperty, value);
120126
}
121127

128+
/// <inheritdoc cref="Popup.PlacementConstraintAdjustment"/>
129+
public PopupPositionerConstraintAdjustment PlacementConstraintAdjustment
130+
{
131+
get => GetValue(PlacementConstraintAdjustmentProperty);
132+
set => SetValue(PlacementConstraintAdjustmentProperty, value);
133+
}
134+
122135
IPopupHost? IPopupHostProvider.PopupHost => Popup?.Host;
123136

124137
event Action<IPopupHost?>? IPopupHostProvider.PopupHostChanged
@@ -427,9 +440,7 @@ private void PositionPopup(bool showAtPointer)
427440
else
428441
{
429442
Popup.Placement = Placement;
430-
Popup.PlacementConstraintAdjustment =
431-
PopupPositioning.PopupPositionerConstraintAdjustment.SlideX |
432-
PopupPositioning.PopupPositionerConstraintAdjustment.SlideY;
443+
Popup.PlacementConstraintAdjustment = PlacementConstraintAdjustment;
433444
}
434445
}
435446

0 commit comments

Comments
 (0)