Browse Source

Merge pull request #1526 from AvaloniaUI/feature/context-menus-obey-screen-edges

Make menus and context menus obey screen edges.
repro/1326-data-binding-issue
danwalmsley 9 years ago
committed by GitHub
parent
commit
93a6da0b9c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      src/Avalonia.Controls/ContextMenu.cs
  2. 39
      src/Avalonia.Controls/Primitives/Popup.cs
  3. 26
      src/Avalonia.Controls/Primitives/PopupRoot.cs
  4. 6
      src/Avalonia.Themes.Default/MenuItem.xaml

7
src/Avalonia.Controls/ContextMenu.cs

@ -19,7 +19,7 @@ namespace Avalonia.Controls
{ {
ContextMenuProperty.Changed.Subscribe(ContextMenuChanged); ContextMenuProperty.Changed.Subscribe(ContextMenuChanged);
MenuItem.ClickEvent.AddClassHandler<ContextMenu>(x => x.OnContextMenuClick, handledEventsToo: true); MenuItem.ClickEvent.AddClassHandler<ContextMenu>(x => x.OnContextMenuClick, handledEventsToo: true);
} }
/// <summary> /// <summary>
@ -75,13 +75,14 @@ namespace Avalonia.Controls
{ {
if (control != null) if (control != null)
{ {
if(_popup == null) if (_popup == null)
{ {
_popup = new Popup() _popup = new Popup()
{ {
PlacementMode = PlacementMode.Pointer, PlacementMode = PlacementMode.Pointer,
PlacementTarget = control, PlacementTarget = control,
StaysOpen = false StaysOpen = false,
ObeyScreenEdges = true
}; };
_popup.Closed += PopupClosed; _popup.Closed += PopupClosed;

39
src/Avalonia.Controls/Primitives/Popup.cs

@ -40,6 +40,12 @@ namespace Avalonia.Controls.Primitives
public static readonly StyledProperty<PlacementMode> PlacementModeProperty = public static readonly StyledProperty<PlacementMode> PlacementModeProperty =
AvaloniaProperty.Register<Popup, PlacementMode>(nameof(PlacementMode), defaultValue: PlacementMode.Bottom); AvaloniaProperty.Register<Popup, PlacementMode>(nameof(PlacementMode), defaultValue: PlacementMode.Bottom);
/// <summary>
/// Defines the <see cref="ObeyScreenEdges"/> property.
/// </summary>
public static readonly StyledProperty<bool> ObeyScreenEdgesProperty =
AvaloniaProperty.Register<Popup, bool>(nameof(ObeyScreenEdges));
/// <summary> /// <summary>
/// Defines the <see cref="HorizontalOffset"/> property. /// Defines the <see cref="HorizontalOffset"/> property.
/// </summary> /// </summary>
@ -136,6 +142,16 @@ namespace Avalonia.Controls.Primitives
set { SetValue(PlacementModeProperty, value); } set { SetValue(PlacementModeProperty, value); }
} }
/// <summary>
/// Gets or sets a value indicating whether the popup positions itself within the nearest screen boundary
/// when its opened at a position where it would otherwise overlap the screen edge.
/// </summary>
public bool ObeyScreenEdges
{
get => GetValue(ObeyScreenEdgesProperty);
set => SetValue(ObeyScreenEdgesProperty, value);
}
/// <summary> /// <summary>
/// Gets or sets the Horizontal offset of the popup in relation to the <see cref="PlacementTarget"/> /// Gets or sets the Horizontal offset of the popup in relation to the <see cref="PlacementTarget"/>
/// </summary> /// </summary>
@ -216,12 +232,12 @@ namespace Avalonia.Controls.Primitives
var window = _topLevel as Window; var window = _topLevel as Window;
if (window != null) if (window != null)
{ {
window.Deactivated += WindowDeactivated; window.Deactivated += WindowDeactivated;
} }
else else
{ {
var parentPopuproot = _topLevel as PopupRoot; var parentPopuproot = _topLevel as PopupRoot;
if(parentPopuproot != null && parentPopuproot.Parent!=null) if (parentPopuproot != null && parentPopuproot.Parent != null)
{ {
((Popup)(parentPopuproot.Parent)).Closed += ParentClosed; ((Popup)(parentPopuproot.Parent)).Closed += ParentClosed;
} }
@ -234,13 +250,18 @@ namespace Avalonia.Controls.Primitives
_popupRoot.Show(); _popupRoot.Show();
if (ObeyScreenEdges)
{
_popupRoot.SnapInsideScreenEdges();
}
_ignoreIsOpenChanged = true; _ignoreIsOpenChanged = true;
IsOpen = true; IsOpen = true;
_ignoreIsOpenChanged = false; _ignoreIsOpenChanged = false;
Opened?.Invoke(this, EventArgs.Empty); Opened?.Invoke(this, EventArgs.Empty);
} }
/// <summary> /// <summary>
/// Closes the popup. /// Closes the popup.
/// </summary> /// </summary>
@ -346,8 +367,10 @@ namespace Avalonia.Controls.Primitives
/// <returns>The popup's position in screen coordinates.</returns> /// <returns>The popup's position in screen coordinates.</returns>
protected virtual Point GetPosition() protected virtual Point GetPosition()
{ {
return GetPosition(PlacementTarget ?? this.GetVisualParent<Control>(), PlacementMode, PopupRoot, var result = GetPosition(PlacementTarget ?? this.GetVisualParent<Control>(), PlacementMode, PopupRoot,
HorizontalOffset, VerticalOffset); HorizontalOffset, VerticalOffset);
return result;
} }
internal static Point GetPosition(Control target, PlacementMode placement, PopupRoot popupRoot, double horizontalOffset, double verticalOffset) internal static Point GetPosition(Control target, PlacementMode placement, PopupRoot popupRoot, double horizontalOffset, double verticalOffset)
@ -399,8 +422,8 @@ namespace Avalonia.Controls.Primitives
{ {
if (!StaysOpen) if (!StaysOpen)
{ {
if(!IsChildOrThis((IVisual)e.Source)) if (!IsChildOrThis((IVisual)e.Source))
{ {
Close(); Close();
e.Handled = true; e.Handled = true;
} }
@ -412,12 +435,12 @@ namespace Avalonia.Controls.Primitives
IVisual root = child.GetVisualRoot(); IVisual root = child.GetVisualRoot();
while (root is PopupRoot) while (root is PopupRoot)
{ {
if (root == PopupRoot) return true; if (root == PopupRoot) return true;
root = ((PopupRoot)root).Parent.GetVisualRoot(); root = ((PopupRoot)root).Parent.GetVisualRoot();
} }
return false; return false;
} }
private void WindowDeactivated(object sender, EventArgs e) private void WindowDeactivated(object sender, EventArgs e)
{ {
if (!StaysOpen) if (!StaysOpen)

26
src/Avalonia.Controls/Primitives/PopupRoot.cs

@ -2,10 +2,12 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information. // Licensed under the MIT license. See licence.md file in the project root for full license information.
using System; using System;
using System.Linq;
using Avalonia.Controls.Platform; using Avalonia.Controls.Platform;
using Avalonia.Controls.Presenters; using Avalonia.Controls.Presenters;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Layout; using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Platform; using Avalonia.Platform;
using Avalonia.Styling; using Avalonia.Styling;
@ -75,6 +77,30 @@ namespace Avalonia.Controls.Primitives
/// <inheritdoc/> /// <inheritdoc/>
public void Dispose() => PlatformImpl?.Dispose(); public void Dispose() => PlatformImpl?.Dispose();
/// <summary>
/// Moves the Popups position so that it doesnt overlap screen edges.
/// This method can be called immediately after Show has been called.
/// </summary>
public void SnapInsideScreenEdges()
{
var window = this.GetSelfAndLogicalAncestors().OfType<Window>().First();
var screen = window.Screens.ScreenFromPoint(Position);
var screenX = Position.X + Bounds.Width - screen.Bounds.X;
var screenY = Position.Y + Bounds.Height - screen.Bounds.Y;
if (screenX > screen.Bounds.Width)
{
Position = Position.WithX(Position.X - (screenX - screen.Bounds.Width));
}
if (screenY > screen.Bounds.Height)
{
Position = Position.WithY(Position.Y - (screenY - screen.Bounds.Height));
}
}
/// <inheritdoc/> /// <inheritdoc/>
protected override void OnTemplateApplied(TemplateAppliedEventArgs e) protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
{ {

6
src/Avalonia.Themes.Default/MenuItem.xaml

@ -45,7 +45,8 @@
<Popup Name="PART_Popup" <Popup Name="PART_Popup"
PlacementMode="Right" PlacementMode="Right"
StaysOpen="True" StaysOpen="True"
IsOpen="{TemplateBinding Path=IsSubMenuOpen, Mode=TwoWay}"> IsOpen="{TemplateBinding Path=IsSubMenuOpen, Mode=TwoWay}"
ObeyScreenEdges="True">
<Border Background="{TemplateBinding Background}" <Border Background="{TemplateBinding Background}"
BorderBrush="{DynamicResource ThemeBorderMidBrush}" BorderBrush="{DynamicResource ThemeBorderMidBrush}"
BorderThickness="1"> BorderThickness="1">
@ -92,7 +93,8 @@
</ContentPresenter> </ContentPresenter>
<Popup Name="PART_Popup" <Popup Name="PART_Popup"
IsOpen="{TemplateBinding Path=IsSubMenuOpen, Mode=TwoWay}" IsOpen="{TemplateBinding Path=IsSubMenuOpen, Mode=TwoWay}"
StaysOpen="True"> StaysOpen="True"
ObeyScreenEdges="True">
<Border Background="{TemplateBinding Background}" <Border Background="{TemplateBinding Background}"
BorderBrush="{DynamicResource ThemeBorderMidBrush}" BorderBrush="{DynamicResource ThemeBorderMidBrush}"
BorderThickness="1"> BorderThickness="1">

Loading…
Cancel
Save