Skip to content

Commit

Permalink
cancellable before_close event for menus
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Apr 6, 2024
1 parent 753f907 commit f9c2db4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
7 changes: 7 additions & 0 deletions haxe/ui/containers/menus/Menu.hx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ class MenuEvents extends haxe.ui.events.Events {
event.menuItem = item;
findRootMenu().dispatch(event);

var beforeCloseEvent = new UIEvent(UIEvent.BEFORE_CLOSE);
beforeCloseEvent.relatedComponent = item;
findRootMenu().dispatch(beforeCloseEvent);
if (beforeCloseEvent.canceled) {
return;
}

hideMenu();
removeScreenMouseDown();
_menu.dispatch(new UIEvent(UIEvent.CLOSE));
Expand Down
17 changes: 13 additions & 4 deletions haxe/ui/containers/menus/MenuBar.hx
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ private class Events extends haxe.ui.events.Events {
var menu = builder._menus[index];

if (menu != _currentMenu) {
showMenu(index);
showMenu(index, true);
}
}

private function showMenu(index:Int) {
private function showMenu(index:Int, force:Bool = false) {
var builder:Builder = cast(_menubar._compositeBuilder, Builder);
var menu:Menu = builder._menus[index];
menu.addClass("primary-menu");
Expand All @@ -140,7 +140,7 @@ private class Events extends haxe.ui.events.Events {

target.selected = true;

hideCurrentMenu();
hideCurrentMenu(force);

cast(menu._internalEvents, MenuEvents).button = target;
if (menu.hasEvent(UIEvent.CLOSE, onMenuClose) == false) {
Expand Down Expand Up @@ -215,14 +215,23 @@ private class Events extends haxe.ui.events.Events {
}
}

private function hideCurrentMenu() {
private function hideCurrentMenu(force:Bool = false) {
if (_currentMenu != null) {
var builder:Builder = cast(_menubar._compositeBuilder, Builder);
for (button in builder._buttons) {
if (button.hasClass("menubar-button-no-children-active")) {
button.swapClass("menubar-button-no-children", "menubar-button-no-children-active");
}
}

if (!force && _currentMenu.hitTest(Screen.instance.currentMouseX, Screen.instance.currentMouseY)) {
var beforeCloseEvent = new UIEvent(UIEvent.BEFORE_CLOSE);
beforeCloseEvent.relatedComponent = _currentMenu.findComponentsUnderPoint(Screen.instance.currentMouseX, Screen.instance.currentMouseY, MenuItem)[0];
_currentMenu.dispatch(beforeCloseEvent);
if (beforeCloseEvent.canceled) {
return;
}
}

var menuEvent = new MenuEvent(MenuEvent.MENU_CLOSED);
menuEvent.menu = _currentMenu;
Expand Down

0 comments on commit f9c2db4

Please sign in to comment.