Skip to content

Commit

Permalink
PopupMenu: Add withMousePosition helper function to Options
Browse files Browse the repository at this point in the history
  • Loading branch information
reuk committed Sep 28, 2021
1 parent 84c5627 commit ab966fb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion examples/Plugins/DSPModulePluginDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,8 @@ class DspModulePluginDemoEditor : public AudioProcessorEditor
if (e.mods.isRightButtonDown())
if (auto* c = editor.getHostContext())
if (auto menuInfo = c->getContextMenuForParameterIndex (&param))
menuInfo->getEquivalentPopupMenu().showMenuAsync ({});
menuInfo->getEquivalentPopupMenu().showMenuAsync (PopupMenu::Options{}.withTargetComponent (this)
.withMousePosition());
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,11 @@ class ParameterDisplayComponent : public Component,
if (e.mods.isRightButtonDown())
if (auto* context = editor.getHostContext())
if (auto menu = context->getContextMenuForParameterIndex (&parameter))
menu->getEquivalentPopupMenu().showMenuAsync (getMenuOptions());
menu->getEquivalentPopupMenu().showMenuAsync (PopupMenu::Options().withTargetComponent (this)
.withMousePosition());
}

private:
PopupMenu::Options getMenuOptions()
{
return PopupMenu::Options().withTargetComponent (this)
.withTargetScreenArea ({ Desktop::getMousePosition(), Desktop::getMousePosition() });
}

AudioProcessorEditor& editor;
AudioProcessorParameter& parameter;
Label parameterName, parameterLabel;
Expand Down
5 changes: 5 additions & 0 deletions modules/juce_gui_basics/menus/juce_PopupMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,11 @@ PopupMenu::Options PopupMenu::Options::withTargetScreenArea (Rectangle<int> area
return with (*this, &Options::targetArea, area);
}

PopupMenu::Options PopupMenu::Options::withMousePosition() const
{
return withTargetScreenArea (Rectangle<int>{}.withPosition (Desktop::getMousePosition()));
}

PopupMenu::Options PopupMenu::Options::withDeletionCheck (Component& comp) const
{
return with (with (*this, &Options::isWatchingForDeletion, true),
Expand Down
17 changes: 15 additions & 2 deletions modules/juce_gui_basics/menus/juce_PopupMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,28 @@ class JUCE_API PopupMenu

/** Sets the region of the screen next to which the menu should be displayed.
To display the menu next to the mouse cursor, pass
Rectangle<int>{}.withPosition (Desktop::getMousePosition()).
To display the menu next to the mouse cursor use withMousePosition(),
which is equivalent to passing the following to this function:
@code
Rectangle<int>{}.withPosition (Desktop::getMousePosition())
@endcode
withTargetComponent() will also set the target screen area. If you need
a target component and a target screen area, make sure to call
withTargetScreenArea() after withTargetComponent().
@see withMousePosition
*/
Options withTargetScreenArea (Rectangle<int> targetArea) const;

/** Sets the target screen area to match the current mouse position.
Make sure to call this after withTargetComponent().
@see withTargetScreenArea
*/
Options withMousePosition() const;

/** If the passed component has been deleted when the popup menu exits,
the selected item's action will not be called.
Expand Down

0 comments on commit ab966fb

Please sign in to comment.