1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-23 01:44:22 +00:00

PopupMenu: Add option to specify initially-selected menu item

This commit is contained in:
reuk 2021-03-09 19:11:13 +00:00
parent 326d8deb16
commit d62d3aaa4f
No known key found for this signature in database
GPG key ID: 9ADCD339CFC98A11
3 changed files with 23 additions and 3 deletions

View file

@ -1288,6 +1288,7 @@ PopupMenu::Options LookAndFeel_V2::getOptionsForComboBoxPopupMenu (ComboBox& box
{
return PopupMenu::Options().withTargetComponent (&box)
.withItemThatMustBeVisible (box.getSelectedId())
.withInitiallySelectedItem (box.getSelectedId())
.withMinimumWidth (box.getWidth())
.withMaximumNumColumns (1)
.withStandardItemHeight (label.getHeight());

View file

@ -252,12 +252,19 @@ struct MenuWindow : public Component
setOpaque (lf.findColour (PopupMenu::backgroundColourId).isOpaque()
|| ! Desktop::canUseSemiTransparentWindows());
const auto initialSelectedId = options.getInitiallySelectedItemId();
for (int i = 0; i < menu.items.size(); ++i)
{
auto& item = menu.items.getReference (i);
if (i + 1 < menu.items.size() || ! item.isSeparator)
items.add (new ItemComponent (item, options, *this));
{
auto* child = items.add (new ItemComponent (item, options, *this));
if (initialSelectedId != 0 && item.itemID == initialSelectedId)
setCurrentlyHighlightedChild (child);
}
}
auto targetArea = options.getTargetScreenArea() / scaleFactor;
@ -1239,7 +1246,7 @@ private:
{
if (globalMousePos != lastMousePos || timeNow > lastMouseMoveTime + 350)
{
const bool isMouseOver = window.reallyContains (localMousePos, true);
const auto isMouseOver = window.reallyContains (localMousePos, true);
if (isMouseOver)
window.hasBeenOver = true;
@ -1279,7 +1286,12 @@ private:
window.activeSubMenu->hide (nullptr, true);
if (! isMouseOver)
{
if (! window.hasBeenOver)
return;
itemUnderMouse = nullptr;
}
window.setCurrentlyHighlightedChild (itemUnderMouse);
}
@ -1820,6 +1832,11 @@ PopupMenu::Options PopupMenu::Options::withPreferredPopupDirection (PopupDirecti
return with (*this, &Options::preferredPopupDirection, direction);
}
PopupMenu::Options PopupMenu::Options::withInitiallySelectedItem (int idOfItemToBeSelected) const
{
return with (*this, &Options::initiallySelectedItemId, idOfItemToBeSelected);
}
Component* PopupMenu::createWindow (const Options& options,
ApplicationCommandManager** managerOfChosenCommand) const
{

View file

@ -470,6 +470,7 @@ public:
Options withItemThatMustBeVisible (int idOfItemToBeVisible) const;
Options withParentComponent (Component* parentComponent) const;
Options withPreferredPopupDirection (PopupDirection direction) const;
Options withInitiallySelectedItem (int idOfItemToBeSelected) const;
//==============================================================================
Component* getParentComponent() const noexcept { return parentComponent; }
@ -482,6 +483,7 @@ public:
int getStandardItemHeight() const noexcept { return standardHeight; }
int getItemThatMustBeVisible() const noexcept { return visibleItemID; }
PopupDirection getPreferredPopupDirection() const noexcept { return preferredPopupDirection; }
int getInitiallySelectedItemId() const noexcept { return initiallySelectedItemId; }
private:
//==============================================================================
@ -489,7 +491,7 @@ public:
Component* targetComponent = nullptr;
Component* parentComponent = nullptr;
WeakReference<Component> componentToWatchForDeletion;
int visibleItemID = 0, minWidth = 0, minColumns = 1, maxColumns = 0, standardHeight = 0;
int visibleItemID = 0, minWidth = 0, minColumns = 1, maxColumns = 0, standardHeight = 0, initiallySelectedItemId = 0;
bool isWatchingForDeletion = false;
PopupDirection preferredPopupDirection = PopupDirection::downwards;
};