Developers

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
sanitycheck
Visitor

Returning focus to a custom component

Hello, I'm new to BrightScript and trying to get to grips with it. Apologies in advance if this is super basic, I shall try to ask progressively more difficult questions as time goes on!

So I have a custom component with some other components inside it, simplistic example (real life is complicated):
<component name="TopMenu" extends="Group" initialFocus="foo1">
  <children>
    <LayoutGroup>
      <Button id="foo1" text="Hello" />
      <Button id="foo2" text="World" />
    </LayoutGroup>
  </children>
</component>

InitialFocus works fine when the app starts up, then upon user interaction the scene transfers focus to another component (e.g. a MarkupGrid).

Later, in the scene code, I try to transfer focus back to my component, e.g.
topMenu.setFocus(true)

...but this doesn't do anything automatically (i.e. re-trigger initialFocus) and I can't find anything built-in to observe which would allow me to react in the component code.

This (reaching in and focusing the LayoutGroup directly, not nice but worth trying in case there's some special magic) doesn't do anything either
topMenu.getChild(0).setFocus(true)


This (directly setting focus on the first button) does work
topMenu.getChild(0).getChild(0).setFocus(true)

...so obviously I could observe a new "hasCustomFocus" interface on my component, then make sure I use that instead of setFocus, and react to changes to refocus the previously focused button (which I'd have to keep track of myself). Surely I'd be clumsily duplicating functionality that's already built-in though, and future code maintainers will realise I'm an idiot.

What's the right thing to do here? Thanks in advance!
Tags (1)
0 Kudos
2 REPLIES 2
MuviDev
Binge Watcher

Re: Returning focus to a custom component

Thank you so much i was stuck on this!

0 Kudos
sanity-check
Roku Guru

Re: Returning focus to a custom component

Oh, hilarious.. I saw this thread in the list and thought somebody had nicked my name. But it's my old account, from back when I didn't know anything!

So, I eventually progressed beyond the custom focus setter but I'm not sure the current approach is cleaner. As you can see, nobody contributed a good answer. 🙂

I ended up with a pretty messy solution involving observing 'focusedChild', and making use of .isInFocusChain(). I seem to recall you get a 'focusedChild' event in a component when you .setFocus() it, and then you can do a setFocus on the child component you want to actually be selected.

I'm afraid I abstracted it all away into code I no longer totally understand, but you at least have two avenues to investigate now.

0 Kudos
Community is Temporarily in Read-Only Mode!

We’re upgrading Roku Community to bring you a faster, more mobile-friendly experience. You may notice limited functionality or read-only access during this time. You will not be able to log in or post new comments or kudos during this time. Read more here.

Planned Downtime:
Community will be unavailable for up to 24–48 hours during the upgrade window during the week of May 12 and you may notice reduced functionality. In the meantime, for additional assistance, visit our Support Site.

Thanks for your patience — we’re excited to share what’s next!