[GH-ISSUE #732] Why SetFocus returns Application? #537

Closed
opened 2026-03-04 01:05:50 +03:00 by kerem · 8 comments
Owner

Originally created by @abitrolly on GitHub (Jun 5, 2022).
Original GitHub issue: https://github.com/rivo/tview/issues/732

github.com/rivo/tview@9994674d60/application.go (L705)

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

Originally created by @abitrolly on GitHub (Jun 5, 2022). Original GitHub issue: https://github.com/rivo/tview/issues/732 https://github.com/rivo/tview/blob/9994674d60a85d2c18e2192ef58195fff743091f/application.go#L705 It doesn't seem logical to return Application. What is it for?
kerem closed this issue 2026-03-04 01:05:50 +03:00
Author
Owner

@moson-mo commented on GitHub (Jun 6, 2022):

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

<!-- gh-comment-id:1147820508 --> @moson-mo commented on GitHub (Jun 6, 2022): 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
Author
Owner

@abitrolly commented on GitHub (Jun 6, 2022):

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

github.com/rivo/tview@9994674d60/demos/form/main.go (L10-L17)

<!-- gh-comment-id:1147846370 --> @abitrolly commented on GitHub (Jun 6, 2022): 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
Author
Owner

@moson-mo commented on GitHub (Jun 6, 2022):

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... 🤷 😉

<!-- gh-comment-id:1147854760 --> @moson-mo commented on GitHub (Jun 6, 2022): > 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:
Author
Owner

@abitrolly commented on GitHub (Jun 6, 2022):

SetFocus should be passed to input handlers.

github.com/rivo/tview@9994674d60/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?

<!-- gh-comment-id:1147863002 --> @abitrolly commented on GitHub (Jun 6, 2022): `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?
Author
Owner

@rivo commented on GitHub (Nov 12, 2022):

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.

<!-- gh-comment-id:1312478146 --> @rivo commented on GitHub (Nov 12, 2022): 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.
Author
Owner

@abitrolly commented on GitHub (Nov 12, 2022):

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.

<!-- gh-comment-id:1312491920 --> @abitrolly commented on GitHub (Nov 12, 2022): 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.
Author
Owner

@rivo commented on GitHub (Nov 13, 2022):

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

<!-- gh-comment-id:1312735205 --> @rivo commented on GitHub (Nov 13, 2022): Not as part of the `tview` library. Most users will probably keep a package-global reference somewhere in their application.
Author
Owner

@abitrolly commented on GitHub (Nov 13, 2022):

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.

<!-- gh-comment-id:1312736982 --> @abitrolly commented on GitHub (Nov 13, 2022): 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.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#537
No description provided.