Your Digital Media Has Never Looked So Good

 
User avatar
bmceldowney
Topic Author
Posts: 4
Joined: Tue Mar 28, 2017 8:10 pm

Is there a way to get the currently focused node?

Tue Mar 28, 2017 8:25 pm

I'm wondering if there is a reliable way to get a reference to the currently focused node. I'm familiar with observing the focusedChild property, but that only gets me the immediate child in the focus chain. I'd prefer to be able to do something along these lines:
sub init()
    m.top.observeField("focusedChild", "focusChanged")
end sub

sub focusChanged(evt)
    if m.top.isInFocusChain()
        child = evt.getData()
        while true
            if type(child.focusedChild) = "roInvalid" then exit while
            child = child.focusedChild
        end while

        'do some stuff with the focused node
    end if
end sub


But according to the focusedChild documentation:

Accessing the value of the field will result in script errors.

Is there a way to get the node that actually has focus?
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Is there a way to get the currently focused node?

Tue Mar 28, 2017 8:36 pm

bmceldowney wrote:
            if type(child.focusedChild) = "roInvalid" then exit while


Don't you ever do this! It will break if the value turns out to be `invalid` and not `roInvalid`.
Instead, do this - which works with both:
if child.focusedChild = invalid then exit while
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Is there a way to get the currently focused node?

Tue Mar 28, 2017 8:39 pm

bmceldowney wrote:
I'm wondering if there is a reliable way to get a reference to the currently focused node.

Curious - why do you need to know the node that has the input focus? What's the use case.
 
belltown
Posts: 1445
Joined: Thu Dec 09, 2010 1:43 pm
Contact:

Re: Is there a way to get the currently focused node?

Tue Mar 28, 2017 8:51 pm

I was looking for the same thing a while back. The use case was a component that acted as a modal dialog (my own component, not a Dialog Node, because standard Dialog nodes are not very customizable). When exiting the dialog, I wanted to get back to where I was before the dialog was invoked. It's not sufficient to just turn off the dialog's visibility; you have to re-set the focus to the previous component. It would be easy if the modal component could just access the "lastFocusedNode", then re-set the focus as it exits. I ended up doing something kludgy like adding an "exit" field to the dialog component, which the invoking component observed to allow it to set the focus back. It's a pain if you're doing that in multiple different places. I miss the "screen stack" concept of the old SDK.
https://github.com/belltown/
 
User avatar
bmceldowney
Topic Author
Posts: 4
Joined: Tue Mar 28, 2017 8:10 pm

Re: Is there a way to get the currently focused node?

Tue Mar 28, 2017 8:56 pm

Thanks for the heads up about Invalid checking, I'll use that in the future.

In a nutshell, the use case is a component I'm creating that extends LayoutGroup. It checks the focused child bounds against screen bounds to determine if the focused node is partially or entirely offscreen and then adjusts it's own translation to make sure the focused node is inside the screen bounds. It works great if the focused node is an immediate descendant of my component, but I'd like it to work on nodes that are nested as well.

So far my biggest hurdle is the fact that simply accessing the focusedChild property results in a "Execution timeout (runtime error &h23)".

I'm hoping there's another way to get a reference to the currently focused node.
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Is there a way to get the currently focused node?

Tue Mar 28, 2017 9:09 pm

bmceldowney wrote:
So far my biggest hurdle is the fact that simply accessing the focusedChild property results in a "Execution timeout (runtime error &h23)".

This is a strange error, what firmware? 7.5 or 7.6 beta?
Programmatically or from console?
 
User avatar
bmceldowney
Topic Author
Posts: 4
Joined: Tue Mar 28, 2017 8:10 pm

Re: Is there a way to get the currently focused node?

Tue Mar 28, 2017 9:12 pm

From the console, 7.6.

According to the Roku docs, accessing the value of focusedChild will result in "script errors", so I'm assuming that's the problem. That's why I'm looking for any alternative methods to get the currently focused node.
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Is there a way to get the currently focused node?

Tue Mar 28, 2017 9:16 pm

belltown wrote:
When exiting the dialog, I wanted to get back to where I was before the dialog was invoked. It's not sufficient to just turn off the dialog's visibility; you have to re-set the focus to the previous component. It would be easy if the modal component could just access the "lastFocusedNode", then re-set the focus as it exits.

I can think of couple of complications to this - which may not apply to your case but in principle - first, it's possible the last focused node does not exist at this point (e.g. somebody removed it in the mean time) - what's to do then? And second, somebody was saying there are cases in which setFocus() cannot restore the exact focused item - something to do with compound components (was it grids or row lists... i don't remember). 
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Is there a way to get the currently focused node?

Tue Mar 28, 2017 9:17 pm

bmceldowney wrote:
From the console, 7.6.

According to the Roku docs, accessing the value of focusedChild will result in "script errors", so I'm assuming that's the problem. That's why I'm looking for any alternative methods to get the currently focused node.

And according to me, that timeout is a bug in the 7.6 beta which you are enrolled in - please report in the beta forum
(Try on 7.5 if you have one handy)
 
belltown
Posts: 1445
Joined: Thu Dec 09, 2010 1:43 pm
Contact:

Re: Is there a way to get the currently focused node?

Tue Mar 28, 2017 9:27 pm

EnTerr wrote:
belltown wrote:
When exiting the dialog, I wanted to get back to where I was before the dialog was invoked. It's not sufficient to just turn off the dialog's visibility; you have to re-set the focus to the previous component. It would be easy if the modal component could just access the "lastFocusedNode", then re-set the focus as it exits.

I can think of couple of complications to this - which may not apply to your case but in principle - first, it's possible the last focused node does not exist at this point (e.g. somebody removed it in the mean time) - what's to do then? And second, somebody was saying there are cases in which setFocus() cannot restore the exact focused item - something to do with compound components (was it grids or row lists... i don't remember). 

In the first case, return Invalid. In the second case, I'd have to know more about those "cases in which setFocus() cannot restore the exact focused item".

It's a moot point anyway. I'm not going to hold my breath waiting for Roku to implement such a feature, just carry on and work around it.
https://github.com/belltown/
 
User avatar
bmceldowney
Topic Author
Posts: 4
Joined: Tue Mar 28, 2017 8:10 pm

Re: Is there a way to get the currently focused node?

Tue Mar 28, 2017 9:29 pm

EnTerr wrote:
And according to me, that timeout is a bug in the 7.6 beta which you are enrolled in - please report in the beta forum
(Try on 7.5 if you have one handy)

Hmm, that's curious. Well, thank you for your assistance, I'll give 7.5 a shot and post in the beta forum as you suggest.
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Is there a way to get the currently focused node?

Tue Mar 28, 2017 10:13 pm

belltown wrote:
In the first case, return Invalid. In the second case, I'd have to know more about those "cases in which setFocus() cannot restore the exact focused item".

What?
I think you and me were thinking different things - me, that there be a .getFocusedNode() method that returns currently focused node that can be stored in variable `lastFocusedNode` and then my concern is by the time one gets to use that stored info is unusable (node was detached).

In your case, you are imagining some magical function getLastFocusedNode() that would keep track of what, prior focused nodes? That's confusing to me, what is the definition of "last" - for example if i do .setFocus() on a node that is already focused, does "last" become the same node or does it do special check that it's the "last focused node that is not the current focused one".
 
jeremie
Posts: 6
Joined: Wed Jul 05, 2017 8:34 am

Re: Is there a way to get the currently focused node?

Tue Aug 08, 2017 11:40 am

bmceldowney wrote:
From the console, 7.6.

According to the Roku docs, accessing the value of focusedChild will result in "script errors", so I'm assuming that's the problem. That's why I'm looking for any alternative methods to get the currently focused node.

It's not a bug, focusedChild can be used with observeField to know if the object himself lose or get the focus. So, the last child has himself as focusedChild.
I need the same loop like you, but we need to that to break the loop : 

if(child.focusedChild = invalid or child.isSameNode(child.focusedChild)) then

it's working for me

Who is online

Users browsing this forum: No registered users and 3 guests