# PopupMenu options not set correctly for child sub menu

**URL:** <https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937>\
**Category:** General JUCE discussion\
**Created:** [February 9, 2023, 2:25pm UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937 "2023-02-09T14:25:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [February 9, 2023, 2:25pm UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/1 "2023-02-09T14:25:08Z")

</div>

If I use in my LnF the target component to have a custom per menu LnF then it stop working as soon as I have child/sub menu  
in bool showSubMenuFor (ItemComponent\* childComp)  
The target component is reseted.  
Is there a rationale for this ?

just using

```auto
            activeSubMenu.reset (new HelperClasses::MenuWindow (*(childComp->item.subMenu), this,
                                                                options.withTargetScreenArea (childComp->getScreenBounds())
                                                                       .withMinimumWidth (0),
                                                                false, dismissOnMouseUp, managerOfChosenCommand, scaleFactor));

```

Fix my issue.

Thanks !

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [February 23, 2023, 11:03am UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/2 "2023-02-23T11:03:34Z")

</div>

sounds good ?

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [February 27, 2023, 10:31am UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/3 "2023-02-27T10:31:31Z")

</div>

> [@otristan](#):
>
> Is there a rationale for this ?

I’m not sure without spending some time investigating, but it looks to me like that line (`withTargetComponent (nullptr)`) has been in place since 2011 - I’d be quite surprised if it served no purpose and we were only discovering it now…

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [February 27, 2023, 10:38am UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/4 "2023-02-27T10:38:52Z")

</div>

I have release a version with this code and didn’t notice any issue FWIW

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [February 27, 2023, 5:17pm UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/5 "2023-02-27T17:17:24Z")

</div>

> [@reuk](#):
>
> withTargetComponent (nullptr)

I believe this line makes sense for a submenu. If the submenu were to inherit the exact same options of its parent menu, then the submenu would appear at the same topleft position of the main menu, but that’s not what is usually desired: the desired behaviour of the sub menu is to appear right next to the item in the main menu that opens the sub menu.

For example, if the target component of the main menu is a button, then the main menu appears next to the button. Any sub menu for the main menu should not appear right next to the same button, but should appear at the correct position next to the main menu.

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [February 27, 2023, 5:31pm UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/6 "2023-02-27T17:31:53Z")

</div>

This is not what happens if you remove this line though 🙂

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [February 27, 2023, 6:02pm UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/7 "2023-02-27T18:02:25Z")

</div>

Ahh, you’re right, because `withTargetScreenArea (childComp->getScreenBounds())` overrides the target area anyway.

So, you want to retain information also in the sub menus about the component that was originally targeted by the main menu.

That seems a valid expectation, and the documentation proves that having both a target component and a target screen area is valid and well defined (emphasis mine):

For withTargetComponent():

> This function will also set the target screen area, so that the menu displays  
> next to the target component. **If you need to display the menu at a specific**  
> **location, you should call withTargetScreenArea() after withTargetComponent()**.

For withTargetScreenArea():

> 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().

So your request for the change (albeit breaking) looks totally valid to me

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [February 27, 2023, 6:22pm UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/8 "2023-02-27T18:22:40Z")

</div>

The thing is that without this fix, all the new withOptions LnF additions in order to have LnF that depends on the target component get pointless as soon as your menu have sub menu.

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [April 18, 2023, 8:00am UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/9 "2023-04-18T08:00:19Z")

</div>

Bump 🙂

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [May 15, 2023, 8:26am UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/10 "2023-05-15T08:26:30Z")

</div>

Bumpity bump 🙂

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [August 18, 2023, 1:12pm UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/11 "2023-08-18T13:12:21Z")

</div>

Would love to see this getting fixed. Without this all the new withOptions LnF are useless.  
FWIW I have release a product with my included fix and never add any issue. I would just like to avoid to fork Juce just for this.

Thanks !

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [November 24, 2023, 9:28am UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/12 "2023-11-24T09:28:24Z")

</div>

Obi-Wan Kenobi, you are my only hope.

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [December 1, 2023, 8:14am UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/13 "2023-12-01T08:14:37Z")

</div>

> <https://github.com/juce-framework/JUCE/commit/903c77b977eca8d5ffe54a59ccbe6405fdccbc11>
>
> This allows the LookAndFeel of submenus to query the target component
> used for t…he top-level menu. getTargetComponent() isn't suitable for
> this because the target component is set to null for submenus, and this
> behaviour can't be changed without potentially breaking code that relies
> on the current behaviour.

does the trick.

Thanks !

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [December 5, 2023, 6:06pm UTC](https://forum.juce.com/t/popupmenu-options-not-set-correctly-for-child-sub-menu/54937/14 "2023-12-05T18:06:17Z")

</div>

Sorry for the lack of updates. I think I didn’t really get back into the swing of things after ADC!

As you pointed out, a fix was added on develop that allows the original target component to be retrieved from submenus. This behaviour is opt-in (i.e. you must call `getTopLevelTargetComponent()`) to preserve the behaviour of the existing `getTargetComponent()`.
