Skip to content

Commit

Permalink
[ComboBox] Ensure popup gets closed when form is moved.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpobst committed Jul 22, 2023
1 parent 20a1f06 commit 77ff148
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/Modern.Forms/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,23 @@ public static class Application

/// <summary>
/// This is the top level active menu, if any.
/// This is used to hide menus if the user clicks elsewhere on the Form or off the Form.
/// </summary>
internal static MenuBase? ActiveMenu { get; set; }

/// <summary>
/// This is the open popup window, like the ComboBox dropdown, if any.
/// </summary>
internal static PopupWindow? ActivePopupWindow { get; set; }

/// <summary>
/// Hides any open popups.
/// </summary>
internal static void ClosePopups ()
{
ActiveMenu?.Deactivate ();
ActivePopupWindow?.Hide ();
}

/// <summary>
/// Exits the application.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Modern.Forms/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public bool DroppedDown {
};

popup.Controls.Add (popup_listbox);

Application.ActivePopupWindow = popup;

popup.Show (this, 1, Height);

OnDropDownOpened (EventArgs.Empty);
Expand Down
4 changes: 2 additions & 2 deletions src/Modern.Forms/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,8 +1407,8 @@ internal void RaiseMouseDown (MouseEventArgs e)
else {
// If we're clicking on the a Control that isn't the active menu,
// we need to close the active menu (if any)
if ((this as MenuBase)?.GetTopLevelMenu () != Application.ActiveMenu)
Application.ActiveMenu?.Deactivate ();
if ((this as MenuBase)?.GetTopLevelMenu () != Application.ActiveMenu || Application.ActiveMenu is null)
Application.ClosePopups ();

if (Enabled) {
Select ();
Expand Down
2 changes: 1 addition & 1 deletion src/Modern.Forms/MenuDropDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected override void OnClick (MouseEventArgs e)
if (clicked_item != null && !clicked_item.HasItems) {

if (!clicked_item.HasItems) {
Application.ActiveMenu?.Deactivate ();
Application.ClosePopups ();

clicked_item.OnClick (e);
OnItemClicked (e, clicked_item);
Expand Down
6 changes: 5 additions & 1 deletion src/Modern.Forms/WindowBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal WindowBase (IWindowBaseImpl window)
window.Closed = () => Closed?.Invoke (this, EventArgs.Empty);
window.Deactivated = () => {
// If we're clicking off the form, deactivate any active menus
Application.ActiveMenu?.Deactivate ();
Application.ClosePopups ();
Deactivated?.Invoke (this, EventArgs.Empty);
};
}
Expand Down Expand Up @@ -145,6 +145,10 @@ public void Hide ()
{
Visible = false;
window.Hide ();

if (Application.ActivePopupWindow == this)
Application.ActivePopupWindow = null;

OnVisibleChanged (EventArgs.Empty);
}

Expand Down

0 comments on commit 77ff148

Please sign in to comment.