tview icon indicating copy to clipboard operation
tview copied to clipboard

Why SetFocus returns Application?

Open abitrolly opened this issue 3 years ago • 4 comments

https://github.com/rivo/tview/blob/9994674d60a85d2c18e2192ef58195fff743091f/application.go#L705

It doesn't seem logical to return Application. What is it for?

abitrolly avatar Jun 05 '22 07:06 abitrolly

So that you can chain several method calls. Most of the methods do this in this library.

Example (lines 10 to 17): https://github.com/rivo/tview/blob/master/demos/form/main.go

moson-mo avatar Jun 06 '22 19:06 moson-mo

Yea, well, but I could not find where SetFocus is used this way.

https://github.com/rivo/tview/blob/9994674d60a85d2c18e2192ef58195fff743091f/demos/form/main.go#L10-L17

abitrolly avatar Jun 06 '22 19:06 abitrolly

Yea, well, but I could not find where SetFocus is used this way.

Well, it probably isn't in any of the examples. If you'd need to, you could though. Nobody forces you to use the return value of that method... :shrug: :wink:

moson-mo avatar Jun 06 '22 19:06 moson-mo

SetFocus should be passed to input handlers.

https://github.com/rivo/tview/blob/9994674d60a85d2c18e2192ef58195fff743091f/frame.go#L172

So I wonder if it is some kind of hack to pass application pointer to widgets that otherwise do not know where the application is?

abitrolly avatar Jun 06 '22 20:06 abitrolly

The hierarchy is top-down. Application knows about other primitives but primitives don't know Application directly. They get what they need, when they need it (like here, a setFocus function), but otherwise they don't need a reference to the Application object.

rivo avatar Nov 12 '22 13:11 rivo

Is there a way for primitive to directly receive the Application handler?

Like if I want to pass focus to the previous element in UI, and I don't know which element it was, I could potentially use Application to find out.

abitrolly avatar Nov 12 '22 14:11 abitrolly

Not as part of the tview library. Most users will probably keep a package-global reference somewhere in their application.

rivo avatar Nov 13 '22 13:11 rivo

Fair enough. So SetFocus returns Application so that the call can be chained with other methods. Not for passing Application around. Thanks for the explanations.

abitrolly avatar Nov 13 '22 13:11 abitrolly