Skip to content

Commit

Permalink
Fix date/time picker popup positioning.
Browse files Browse the repository at this point in the history
Was broken by #7071 - `DatePicker` was doing a manual position configuration by calling `ConfigurePosition`. When the popup is shown, `Popup` gets a position change, and then re-calls `ConfigurePosition` with the settings in the popup, which are different to those passed during the manual configuration.

Change this to set properties on the `Popup` so that when the popup position is changed, the correct parameters are passed to `ConfigurePosition` by `Popup`.

Fixes #7633
  • Loading branch information
grokys committed Feb 17, 2022
1 parent 3138316 commit 4a844e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
14 changes: 9 additions & 5 deletions src/Avalonia.Controls/DateTimePickers/DatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,18 +398,22 @@ private void SetSelectedDateText()
private void OnFlyoutButtonClicked(object? sender, RoutedEventArgs e)
{
if (_presenter == null)
throw new InvalidOperationException("No DatePickerPresenter found");
throw new InvalidOperationException("No DatePickerPresenter found.");
if (_popup == null)
throw new InvalidOperationException("No Popup found.");

_presenter.Date = SelectedDate ?? DateTimeOffset.Now;

_popup!.IsOpen = true;
_popup.PlacementMode = PlacementMode.AnchorAndGravity;
_popup.PlacementAnchor = Primitives.PopupPositioning.PopupAnchor.Bottom;
_popup.PlacementGravity = Primitives.PopupPositioning.PopupGravity.Bottom;
_popup.PlacementConstraintAdjustment = Primitives.PopupPositioning.PopupPositionerConstraintAdjustment.SlideY;
_popup.IsOpen = true;

var deltaY = _presenter.GetOffsetForPopup();

// The extra 5 px I think is related to default popup placement behavior
_popup!.Host!.ConfigurePosition(_popup.PlacementTarget!, PlacementMode.AnchorAndGravity, new Point(0, deltaY + 5),
Primitives.PopupPositioning.PopupAnchor.Bottom, Primitives.PopupPositioning.PopupGravity.Bottom,
Primitives.PopupPositioning.PopupPositionerConstraintAdjustment.SlideY);
_popup.VerticalOffset = deltaY + 5;
}

protected virtual void OnSelectedDateChanged(object? sender, DatePickerSelectedValueChangedEventArgs e)
Expand Down
17 changes: 12 additions & 5 deletions src/Avalonia.Controls/DateTimePickers/TimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,23 @@ protected virtual void OnSelectedTimeChanged(TimeSpan? oldTime, TimeSpan? newTim

private void OnFlyoutButtonClicked(object? sender, Interactivity.RoutedEventArgs e)
{
_presenter!.Time = SelectedTime ?? DateTime.Now.TimeOfDay;
if (_presenter == null)
throw new InvalidOperationException("No DatePickerPresenter found.");
if (_popup == null)
throw new InvalidOperationException("No Popup found.");

_popup!.IsOpen = true;
_presenter.Time = SelectedTime ?? DateTime.Now.TimeOfDay;

_popup.PlacementMode = PlacementMode.AnchorAndGravity;
_popup.PlacementAnchor = Primitives.PopupPositioning.PopupAnchor.Bottom;
_popup.PlacementGravity = Primitives.PopupPositioning.PopupGravity.Bottom;
_popup.PlacementConstraintAdjustment = Primitives.PopupPositioning.PopupPositionerConstraintAdjustment.SlideY;
_popup.IsOpen = true;

var deltaY = _presenter.GetOffsetForPopup();

// The extra 5 px I think is related to default popup placement behavior
_popup.Host!.ConfigurePosition(_popup.PlacementTarget!, PlacementMode.AnchorAndGravity, new Point(0, deltaY + 5),
Primitives.PopupPositioning.PopupAnchor.Bottom, Primitives.PopupPositioning.PopupGravity.Bottom,
Primitives.PopupPositioning.PopupPositionerConstraintAdjustment.SlideY);
_popup.VerticalOffset = deltaY + 5;
}

private void OnDismissPicker(object? sender, EventArgs e)
Expand Down

0 comments on commit 4a844e9

Please sign in to comment.