-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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 convenience accessor for primaryFocus #43859
Add convenience accessor for primaryFocus #43859
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than my concern about using the global namespace, this LGTM.
@@ -1418,6 +1424,14 @@ class FocusManager with DiagnosticableTreeMixin { | |||
} | |||
} | |||
|
|||
/// Provides convenient access to the current [FocusManager] from the | |||
/// [WidgetsBinding] instance. | |||
FocusManager get focusManager => WidgetsBinding.instance.focusManager; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Did you consider using a static method on FocusManager
instead of a top-level getter here? Do we have other top-level globals like this? Is there any concern about name clashing with existing app code for either of these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I did consider a static, but it would have had to have a different name (because primaryFocus
is taken), and it's pretty useful at the top level. There are other top level globals like these: imageCache
, and defaultTargetPlatform
to name a couple. Like any new name, It could clash, of course, but I don't think it's very likely to clash.
However, I think I will convert the focusManager
one to a static FocusManager.instance
so that it appears as the singleton that it is (but still get it internally from the WidgetsBinding.instance.focusManager
). That way it's one less thing to clash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we already have a precedence for top level names, this is probably not a big deal. Pragmatically speaking I doubt there would be much collision. I prefer not using global namespaces, but this case maybe useful enough to do so.
Or given that you already changed it to FocusManager.instance
while I was typing this :), never-mind.
This adds accessors for WidgetsBinding.instance.focusManager and WidgetsBinding.instance.focusManager.primaryFocus so that they can be more easily found in IDEs and accessed. This adds a top level getter for WidgetsBinding.instance.focusManager.primaryFocus called primaryFocus, and a static accessor FocusManager.instance that returns WidgetsBinding.instance.focusManager.
Description
This adds accessors for
WidgetsBinding.instance.focusManager
andWidgetsBinding.instance.focusManager.primaryFocus
so that they can be more easily found in IDEs and accessed.This adds a top level getter for
WidgetsBinding.instance.focusManager.primaryFocus
calledprimaryFocus
, and a static accessorFocusManager.instance
that returnsWidgetsBinding.instance.focusManager
.Tests
Breaking Change