-
-
Notifications
You must be signed in to change notification settings - Fork 691
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
Added on_gain_focus
, on_lose_focus
, on_show
& on_hide
handlers on toga.Window
#2096
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.
Thanks for the contribution - this is another feature that is going to hit up against #2058 (and #2075); as such, I'm hesitant to merge it without tests.
I'm also hesitant because it doesn't currently have a Cocoa implementation (that should be easy enough, but it's worth flagging); and it's not 100% obvious how this would behave on mobile. My immediate reaction (and the one raised in #2006) is that gain/lose focus might link into application lifecycle hooks (so when the app comes into the foreground, that's the "gain focus" event for both the window and the app), but there's an open question of how those signals interact with tablet platforms that allow split-screen and other multi-app modes. This is an area where some additional design is required.
Yes, I agree with you. This should not be merged currently. I will need some time to write the implementation for other backends. Like #1930, I will wait until the audits are merged, and will write the tests thereafter. As for additional design for tablet modes, I agree with you. I will research more about it and will discuss with you while implementing for mobile platforms. |
@mhsmith I need your guidance on android side. According to: https://developer.android.com/guide/components/activities/intro-activities#onpause
On the Emulator(Android 12):When starting the app, the following events are triggered: When I select the app by pressing the Recents button, only
When I press the home button, neither On a Physical Device(Android 13):When starting the app, the following events are triggered:
When I select the app by pressing the Recents button, only
When I press the home button, neither
As, you can see, Why are the documented Activity lifecycle events not being triggered as per the documentation? |
Regarding the gain/lose focus on mobile platforms like android: From my testing, the app will lose focus when either the In split screen mode (like dual app mode), suppose there are two apps A and B. App In floating window mode, the app will gain focus when the user touches the app's screen. The focus is lost when the user touches anything outside the app, like interacting with the system launcher or another app. In iOS like the cocoa, there exists But, there needs to be another handler to differentiate between the states when (the app is not visible to the user & is not receiving inputs) and (when the app is visible to the user & is receiving inputs). Hence, I would like to propose other additional handlers, What do you guys think? Also, without confirmation from @mhsmith regarding the Activity life events triggering behavior, I cannot proceed with the android implementation. Hence, I was thinking about working on the iOS implementation first. |
That's because those methods aren't included in the Android template, either in
See this page for how this is notified on Android.
Every API has a maintenance and testing cost, so I'd prefer not to add additional events unless there's a clear need for them, especially if they're only applicable to certain platforms. |
Thank you for helping. I will add default implementations for the remaining methods in the Android template and will submit a PR there after getting a stable behavior. I agree with you that additional events will incur more maintenance. I feel that the For example, the app should be put to a sleep mode(not updating the layout or text) when it is in background or What do you think? |
I have tested android implementation both on a physical device and on the emulator. I have submitted a PR at beeware/briefcase-android-gradle-template#69 so that the app focus event can be detected. |
Completed implementations of all the platforms and also added a test in the window example app. I will write the tests after the audits are merged. But I think this PR is ready for a review. Also, the CI android testbed is failing on its own for some reason. |
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.
What you've done here looks like a good pass at implementing the API as proposed in #2009; however, I think we're hitting an area where we need more design before we proceed.
The detail you've dug up as part of the Android implementation has opened a bunch of design questions about what "focus" even means at the App/Window level. What are we actually trying to achieve with these signal handlers? Is "visibility" a better metaphor than "focus" in this case? Is there any use case for a literal "focus" event on a window? Do we need to differentiate between an app that is "visible", but isn't currently accepting input events, and an app that isn't accepting input events? How does the rest of the app lifecycle map into these events (on all platforms)?
Rather than pressing forward with an implementation, I think we need to step back and come up with a consistent design for these app/window lifecycle events, and work out how they map onto all the platforms we're targeting.
Researching some more on the topic, it seems like we need 3 categories of events:
The following are the states associated with the event categories mentioned above and their implications for other event categories: Input Focus ---> Visiblity -> Hover -> Use Cases:
Who should have which event categories:
APIs are not much of a problem as the available platform APIs can be properly mapped onto the above described event categories. |
Ok, I have removed the gtk's visibility events(i.e., So, both the focus events i.e., Also, another issue is testing of the events on mobile platforms, where the app cannot be sent to background and brought forward, switch between apps, or switch to the "App switcher" screen. All of these are required to automate the testing of focus and visibility events on the mobile platforms. Since, these automated tests are currently not possible, hence I have manually tested them on a physical Android 14 device, on the Android emulator and on the iOS simulator. |
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.
I started doing a full review; but two things
- There's a couple of outstanding comments from my previous review pass
- It seems like there's some bigger picture API design issues that still need to be resolved - in particular, the interaction of minimise and hide.
The interaction between Show/Hide and Minimize is clearly a lot more complex than just #3105 - there's also the inconsistent behavior on macOS. On that basis, it seems like it might make sense to make the two mutually exclusive - state changes are already prevented on non-visible windows, so it doesn't seem unreasonable that visibility changes should be prevented on windows that aren't in a state that can accept them. What does it mean to hide a window in presentation mode? What does it mean to show a minimized window? At the very least, the test suite indicates that there are inconsistencies; I'd argue that the best approach for now would be to prohibit all those interactions, and add them in the future if it makes sense to do so.
dummy/src/toga_dummy/app.py
Outdated
@@ -133,9 +133,15 @@ def get_current_window(self): | |||
return self._get_value("current_window", main_window) | |||
|
|||
def set_current_window(self, window): | |||
previous_current_window = getattr(self.get_current_window(), "interface", None) |
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.
It's somewhat frustrating to be asked for a review when previous review comments have been left without a response.
core/tests/utils.py
Outdated
@@ -24,3 +24,36 @@ def _create(self): | |||
|
|||
def __repr__(self): | |||
return f"Widget(id={self.id!r})" | |||
|
|||
|
|||
def assert_window_event_triggered(window, expected_event=None): |
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.
So - this is simultaneously extremely specific, and extremely generic.
This assertion method is in the top level testing utilities... but can only be used for on/off versions of 2 very specific events. It allows the developer to check 1 specific event has been triggered... but then asserts that completely unrelated events haven't been triggered.
What I was suggesting was a very literal assert_window_gain_focus
method that asserts on_focus is fired, on_hide isn't, and then resets both mocks. It's then clear what the assertion is asserting, and requires almost no logic in the assertion.
Yes, this means there's a second assert_window_lose_focus
, and another pair for show/hide... but that's easy code to write, and easy code to validate - but it dramatically simplifies the test case making it clear what is being tested, and ensures the mocks are consistently reset. As I've noted in previous reviews on other PRs - test code should avoid being smart, because if test code is smart, it can go wrong, and therefore the test code needs to be tested.
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.
I have removed the assert_window_event_triggered
assertion utility and replaced it with 4 separate assertion utilities: assert_window_gain_focus
, assert_window_lose_focus
, assert_window_on_hide
, assert_window_on_show
.
testbed/tests/window/test_window.py
Outdated
|
||
second_window.show() | ||
await second_window_probe.wait_for_window(f"Showing {second_window.title}") | ||
if second_window_probe.show_unminimizes_window: # For cocoa |
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.
The behavior of show()
on a minimized window should either be consistent on all platforms, or show()
/hide()
should raise an error if invoked on a minimized window. We shouldn't need a test workaround here - it's an area where we're in control of the API design.
If we're going to make the behavior consistent, I'd argue the Cocoa behavior is the better interpretation - if someone is calling "show()", they're indicating they want the window to be visible, not just minimized. But I suspect the "show/hide is an error if minimized" approach might be better as it's more explicit about intent.
Co-authored-by: Russell Keith-Magee <[email protected]>
The inconsistency is that on macOS, calling But this inconsistency is only in the calling of Since, hiding a MINIMIZED window is supported by every platfform, so disabling show/hide on a MINIMIZED window, seems like a bit too restrictive. So, I think the best approach would be to produce the Cocoa's interpretation of |
I can't argue it wouldn't be restrictive to disable show/hide on MINIMIZED. The question is whether that's a better option than the alternative - whether implementing macOS's behavior on GTK and Windows is problematic. The major argument again using macOS's behaviour is that it couples two otherwise unrelated features "show/hide" and state are now closely related features. I guess there's an argument that they already are, in that you can't change the state of a hidden window; but implementing macOS's behavior doubles down on that relationship in a way that I'm not sure is helpful. Another argument against macOS's behavior is a question I raised in my previous comment - what about other window states? What does it mean to hide a window in presentation mode? In fullscreen mode? We already restrict changing window state on non-visible windows; do we need to restrict changing visibility on some window states? This will require some more experimentation to determine what is even possible. Another complication that came up this morning in a conversation with @mhsmith - macOS has an app-level concept of "Hide all windows". It's not completely clear how this would interact with window-level hide - but it clearly needs to. One last thing that might be worth exploring - are we sure that |
I have tested the following methods:
All of them deminiaturize the window after unhiding the window. So, it seems like there is no other way to unhide a window while not deminiaturizing the window.
I have tested by hiding the window from every state and then showing the window. The following are the results:
Although calling
Yes, I think this is the one: https://developer.apple.com/documentation/appkit/nsapplication/hide(_:)?language=objc
I also found its unhide counterpart: https://developer.apple.com/documentation/appkit/nsapplication/unhide(_:)?language=objc, which invokes: https://developer.apple.com/documentation/appkit/nsapplication/unhidewithoutactivation()?language=objc
Since, there are 2 reliable delegate notifications:
As you have noted previously about the behavior of
I totally agree with this and think that this interpretation of |
Well that sucks... Back to the drawing board, I guess :-)
Thanks - that's really comprehensive for macOS; to me, it looks like the summary is that this is all as expected, with
I figured there would be.
My hesitation here is that while I think I agree, I'm not 100% sure - and if I'm wrong, walking it back will be difficult without breaking backwards compatibility. In the interests of moving things forward, my suggestion is to treat this as a future problem. For now, prohibit the interaction, and raise a ValueError for any of the "weird" edge cases (i.e., show/hide when minimized, full screen or presentation). As I noted previously, we already prohibit changing state while hidden; so there's at least some symmetry in having an error for these cases. Prohibiting the behaviour may seem extreme - but the cases we're talking about are edge cases. Honestly, if you're leaning hard on window.hide() in your UX, I'm already raising my eyebrow. However, if we explicitly prohibit these cases for now on the basis of the inconsistencies we're seeing, the implementation is simple, there's less testing, and it's guaranteed cross platform. If at some point in the future, we decide that it makes sense to allow |
Since, #3109 is merged, so the implementation has become much more simplified, so I have also re-added the gtk implementation to the PR. I think that there is overlap between the core tests and testbed tests. But, I am not sure if they should be excluded from either the core tests and the testbed tests, as it would lead to missing coverage on the dummy backend, and on other backends. I believe that I have gone through all the previous review questions. If I have missed any of them, then do let me know, I'll try to respond to them quickly. |
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.
A couple of comments about possible simplifications to some of the logic
dummy/src/toga_dummy/window.py
Outdated
if self.get_window_state() != WindowState.MINIMIZED: | ||
self.interface.on_show() |
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.
Is this if statement needed? The contract for the backend should be that it will only be invoked in "valid" states
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.
I agree, I have removed it.
dummy/src/toga_dummy/window.py
Outdated
if self.get_window_state() != WindowState.MINIMIZED: | ||
self.interface.on_hide() |
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.
As above - is this if needed?
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.
I have removed it, Thanks.
dummy/src/toga_dummy/window.py
Outdated
} | ||
and state == WindowState.MINIMIZED | ||
): | ||
self.interface.on_hide() |
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.
Again, are these ifs necessary?
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.
I have removed it.
gtk/src/toga_gtk/window.py
Outdated
WindowState.FULLSCREEN, | ||
WindowState.PRESENTATION, | ||
}: # pragma: no-cover-if-linux-wayland | ||
self.interface.on_show() |
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.
Can't these be simplified as:
if previous_state != current state:
if current_state == MINIMIZED:
on_hide()
elif previous_state == MINIMIZED:
on_show()
?
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.
I agree, I have changed it to use simplified logic. The complex logic was written before #3109 was merged.
testbed/tests/window/test_window.py
Outdated
[ | ||
# Ideally, we would be testing for every possible pairings of the | ||
# visible-to-user window states, but it would increase the runtime | ||
# of the testbed. |
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.
Why do this as a completely new test? We're already doing a bunch of transitions as part of the window state changes - why not add event notification checks to that existing test?
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.
Thanks, I have added the event notification checks to the test_window_state_change
test on the testbed. I have also done the same at the core tests.
winforms/src/toga_winforms/window.py
Outdated
self.interface.on_hide() | ||
|
||
def winforms_SizeChanged(self, sender, event): | ||
if ( |
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.
As with the GTK implementation - can this be simplified? The only state that affects on_show/on_hide is minimized.
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.
Thanks, I have changed it to use the simplified check.
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.
Awesome - thanks for the PR!
Thanks for keeping patience with me :) |
Implements the APIs described in #2009.
on_gain_focus
,on_lose_focus
,on_show
&on_hide
handles are available both as properties and also as initialization parameters intoga.Window
.Only tested on
WinForms
andgtk
. This will take sometime to complete for all backends.Fixes #2009
PR Checklist: