From e8eddc44632b8ed7e4c66336b7af7665a0dde4a7 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 29 Sep 2017 07:59:40 -0400 Subject: [PATCH] Keep MenuItem active when its child menu is open --- include/widgets.hpp | 7 ++++--- src/widgets/MenuItem.cpp | 23 ++++++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/include/widgets.hpp b/include/widgets.hpp index 3d5fc5fe..4c12fabc 100644 --- a/include/widgets.hpp +++ b/include/widgets.hpp @@ -273,9 +273,13 @@ struct MenuOverlay : OpaqueWidget { Widget *onHoverKey(Vec pos, int key); }; +struct MenuEntry; + struct Menu : OpaqueWidget { Menu *parentMenu = NULL; Menu *childMenu = NULL; + /** The entry which created the child menu */ + MenuEntry *activeEntry = NULL; Menu() { box.size = Vec(0, 0); @@ -304,14 +308,11 @@ struct MenuLabel : MenuEntry { }; struct MenuItem : MenuEntry { - BNDwidgetState state = BND_DEFAULT; - float computeMinWidth(NVGcontext *vg); void draw(NVGcontext *vg); virtual Menu *createChildMenu() {return NULL;} void onMouseEnter(); - void onMouseLeave(); void onDragDrop(Widget *origin); }; diff --git a/src/widgets/MenuItem.cpp b/src/widgets/MenuItem.cpp index 31fe8d36..97967bfa 100644 --- a/src/widgets/MenuItem.cpp +++ b/src/widgets/MenuItem.cpp @@ -12,31 +12,36 @@ float MenuItem::computeMinWidth(NVGcontext *vg) { } void MenuItem::draw(NVGcontext *vg) { + // Get state + BNDwidgetState state = (gHoveredWidget == this) ? BND_HOVER : BND_DEFAULT; + Menu *parentMenu = dynamic_cast(parent); + if (parentMenu && parentMenu->activeEntry == this) { + state = BND_ACTIVE; + } + bndMenuItem(vg, 0.0, 0.0, box.size.x, box.size.y, state, -1, text.c_str()); float x = box.size.x - bndLabelWidth(vg, -1, rightText.c_str()); NVGcolor rightColor = (state == BND_DEFAULT) ? bndGetTheme()->menuTheme.textColor : bndGetTheme()->menuTheme.textSelectedColor; - bndIconLabelValue(vg, x, 0.0, box.size.x, box.size.y, -1, - rightColor, BND_LEFT, BND_LABEL_FONT_SIZE, rightText.c_str(), NULL); + bndIconLabelValue(vg, x, 0.0, box.size.x, box.size.y, -1, rightColor, BND_LEFT, BND_LABEL_FONT_SIZE, rightText.c_str(), NULL); } void MenuItem::onMouseEnter() { - state = BND_HOVER; + Menu *parentMenu = dynamic_cast(parent); + if (!parentMenu) + return; + + parentMenu->activeEntry = NULL; // Try to create child menu Menu *childMenu = createChildMenu(); if (childMenu) { childMenu->box.pos = parent->box.pos.plus(box.getTopRight()); - Menu *parentMenu = dynamic_cast(parent); - assert(parentMenu); parentMenu->setChildMenu(childMenu); + parentMenu->activeEntry = this; } } -void MenuItem::onMouseLeave() { - state = BND_DEFAULT; -} - void MenuItem::onDragDrop(Widget *origin) { if (origin != this) return;