-
-
Notifications
You must be signed in to change notification settings - Fork 668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "focus" property to widgets / allow setting focus on instantiation #2863
Comments
I'm going to close this as "not possible". The problem is that focus is a concept that only makes sense in the context of a widget that is in an active layout; and it's also imperative - it applies right now. If It might be possible to find a way to implement this by registering the focus request and deferring until the widget was added to a layout - but it would be a complex mechanism, and would result in some odd side effects, so I'm not convinced it would make for intuitive API. |
It would be rare, and human error, but the one to be honoured should be the last widget that sets
I don't know about Toga's internals, so can't I speak to the complexity, but yes, deferring is what I had in mind. Pseudocode:
|
But what do you mean by "last"? Last one to be constructed? Last one to be added to the layout? Neither of these might be obvious from the code in a non-trivial app, so you end up with "spooky action at a distance". With a method, the ordering is much clearer. |
The last one to make the assignment when attaching the widgets to the tree. Basically let Python run its course: get_a() # Returns 2 In other words, what I have in mind is:
Collisions should be extremely rare, since there's no intention to set focus on more than one widget. I would hope a non-trivial app is made modular enough to allow finding this issue rather quickly The way I've been doing it since I made the report is:
|
They're "extremely rare" until the API actively encourages people to use that pattern. If an API can be used in the wrong way, it will be used in the wrong way. The way you avoid this is by having an API that only allows for correct usage. And that is what an explicit method-based approach provides. |
Sure, but offering both a setter method that does things the right way and a property that can be changed directly is fairly common. That's what allows In Toga itself, for example, we can set Edit:
I can live with it, but it obviously makes the app's code a bit more convoluted and less clear than it needs to be. It would nice if we had this built-in. |
What is the problem or limitation you are having?
Currently, we cannot set focus on unnamed instances of Widget. Only in named, after instantiation, and apparently after attaching the widget to the visible window.
And
toga.Button(...).focus()
doesn't work.But in some cases, we might need to set focus to an unnamed instance, like in the example below:
Or even:
Describe the solution you'd like
Add "focus" property to Widget, and allow us to set focus on instantiation. So that this is possible (notice the second button):
Describe alternatives you've considered
Enable us to call .focus() on instantiation:
toga.Button(...).focus()
Additional context
No response
The text was updated successfully, but these errors were encountered: