-
-
Notifications
You must be signed in to change notification settings - Fork 688
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
base: main
Are you sure you want to change the base?
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. |
I know that this PR is a larger one to review at once, but if possible could you review only the core tests, so that I would have it cleaned up before the final review? |
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 requested, I've done an initial review of the core. The implementation looks fine; there's a couple of issues flagged in the tests.
I've had a quick look at some of the implementations as well; I've flagged a couple of things that stood out, but the biggest issue that occurred to me is the interaction of MINIMIZED and HIDE. What happens if a MINIMIZED window is HIDDEN, then restored to normal? It should still be HIDDEN... but AFAICT it will trigger an on_show
event in most of these implementations.
android/src/toga_android/app.py
Outdated
# than Q. onResume is the best indicator for the gain input focus event. | ||
# https://developer.android.com/reference/android/app/Activity#onWindowFocusChanged(boolean):~:text=If%20the%20intent,the%20best%20indicator. | ||
if Build.VERSION.SDK_INT < Build.VERSION_CODES.Q: | ||
for window in self._impl.interface.windows: |
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 iterating over all windows appropriate? Won't it just be current_window
that gains focus? Admittedly, this is mostly a moot point as long as Toga doesn't support multiple windows on Android, but in the event that we ever do support multiple windows (e.g., supporting an external display), it's worth getting the logic right here.
core/tests/window/test_window.py
Outdated
def test_on_gain_focus(window): | ||
assert window._on_gain_focus._raw is None | ||
|
||
on_gain_focus_handler = Mock() | ||
window.on_gain_focus = on_gain_focus_handler | ||
|
||
assert window.on_gain_focus._raw == on_gain_focus_handler | ||
|
||
window._impl.simulate_on_gain_focus() | ||
|
||
on_gain_focus_handler.assert_called_once_with(window) | ||
|
||
|
||
def test_on_lose_focus(window): | ||
assert window.on_lose_focus._raw is None | ||
|
||
on_lose_focus_handler = Mock() | ||
window.on_lose_focus = on_lose_focus_handler | ||
|
||
assert window.on_lose_focus._raw == on_lose_focus_handler | ||
|
||
window._impl.simulate_on_lose_focus() | ||
|
||
on_lose_focus_handler.assert_called_once_with(window) | ||
|
||
|
||
def test_on_show(window): | ||
assert window.on_show._raw is None | ||
|
||
on_show_handler = Mock() | ||
window.on_show = on_show_handler | ||
|
||
assert window.on_show._raw == on_show_handler | ||
|
||
window._impl.simulate_on_show() | ||
|
||
on_show_handler.assert_called_once_with(window) | ||
|
||
|
||
def test_on_hide(window): | ||
assert window.on_hide._raw is None | ||
|
||
on_hide_handler = Mock() | ||
window.on_hide = on_hide_handler | ||
|
||
assert window.on_hide._raw == on_hide_handler | ||
|
||
window._impl.simulate_on_hide() | ||
|
||
on_hide_handler.assert_called_once_with(window) |
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.
They're not testing anything that isn't covered by the "real" focus/show tests, so I'd say they're not needed.
core/tests/window/test_window.py
Outdated
window2.on_lose_focus = window2_on_lose_focus_handler | ||
|
||
app.current_window = window1 | ||
window1_on_gain_focus_handler.assert_called_once_with(window1) |
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.
These calls assertions will be potentially misleading, as the mocks haven't been reset between each use. The test also isn't asserting the handlers that aren't called.
window1_on_gain_focus_handler.assert_called_once_with(window1) | |
window1_on_gain_focus_handler.assert_called_once_with(window1) | |
window1_on_lose_focus_handler.assert_not_called() | |
window2_on_gain_focus_handler.assert_not_called() | |
window2_on_lose_focus_handler.assert_not_called() | |
window1_on_gain_focus_handler.reset_mock() | |
window1_on_lose_focus_handler.reset_mock() | |
window2_on_gain_focus_handler.reset_mock() | |
window2_on_lose_focus_handler.reset_mock() |
Given that this will be a repeated pattern, it might be worth wrapping this sequence of calls in a utility assert methods.
@@ -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.
AFAIR, get_current_window()
can return None; but if the window exists, it will always have an interface attribute. On that basis, this getattr() is protecting against the wrong missing attribute.
dummy/src/toga_dummy/window.py
Outdated
def simulate_on_gain_focus(self): | ||
self.interface.on_gain_focus() | ||
|
||
def simulate_on_lose_focus(self): | ||
self.interface.on_lose_focus() | ||
|
||
def simulate_on_show(self): | ||
self.interface.on_show() | ||
|
||
def simulate_on_hide(self): | ||
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 noted in the linked thread, I think these aren't needed. simulate_
calls are generally only needed when it's a user interaction that needs to be simulated but there's no corresponding API (or the programmatic API behaves differently to the user-initiated one). In this case, the "user action" is giving a window focus, which is equivalent to the programmatic focus()
call (and the same for hide/show)
Co-authored-by: Russell Keith-Magee <[email protected]>
Apologies - the close was a stray mouse click. |
Currently on the main branch, we are ignoring window state requests on hidden windows, as changing state of hidden window causes glitches on some platforms: Lines 477 to 480 in 7f02868
So, the following cannot be done: EDIT: To confirm that the valid pathway in the above works correctly, I have added a new test. This has helped me catch the cases where the events were getting double triggered. |
On testing, I have found that on gtk, when Also, when Furthermore, it turns out that Since, we need to accomodate all of these cases, the implementation on gtk will take some time. Therefore, can I do the gtk implementation on a separate PR, and skip it here? As the implementation is working correctly on all platforms except on gtk, so skipping the gtk implementation on this PR should not be an issue. |
Sure - an implementation that works on not-all platforms is fine - in fact, if there's an underlying issue with the state change tests, then we should definitely resolve those first. As long as the tests pass reliably on all platforms (and are skipped on platforms where the implementation doesn't exist/doesn't work reliably), and it's clear in the review request what does (and doesn't) work, I'm fine with reviewing and merging. All the usual guidelines about skips apply (i.e, avoid referencing GTK specifically in the test - try to raise the skip in the probe). |
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. |
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: