Skip to content
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

Open
wants to merge 96 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
6e2eda0
Added support for `WinForms` and `gtk`.
proneon267 Aug 23, 2023
4bb2a42
Added changelog.
proneon267 Aug 23, 2023
3a202e4
Added support for `cocoa`.
proneon267 Aug 24, 2023
9cecb4a
Added support for `web`
proneon267 Aug 24, 2023
60a3cac
Merge branch 'beeware:main' into patch-12
proneon267 Aug 24, 2023
87ea0a4
Merge branch 'beeware:main' into patch-12
proneon267 Aug 25, 2023
6f38ae2
Added support for `android`.
proneon267 Aug 28, 2023
6dce43e
Merge branch 'patch-12' of https://github.com/proneon267/toga into pa…
proneon267 Aug 28, 2023
3133dfb
Merge branch 'beeware:main' into patch-12
proneon267 Aug 28, 2023
3b03ec4
Coreected `cocoa` implementation.
proneon267 Aug 28, 2023
d746ba8
Merge branch 'patch-12' of https://github.com/proneon267/toga into pa…
proneon267 Aug 28, 2023
096e6bf
Re: `cocoa` implementation correction.
proneon267 Aug 28, 2023
943612d
Added support for `iOS`.
proneon267 Aug 28, 2023
7b864fe
Fixed `winforms` implementation.
proneon267 Aug 28, 2023
88c56b9
Added a test in window example app.
proneon267 Aug 28, 2023
156bc9e
Corrected `Android` implementation.
proneon267 Aug 30, 2023
aa4714e
Merge branch 'beeware:main' into patch-12
proneon267 Aug 30, 2023
6895d8d
Merge branch 'beeware:main' into patch-12
proneon267 Sep 4, 2023
27bf2a7
Merge branch 'beeware:main' into patch-12
proneon267 Sep 11, 2023
9fabc3e
Merge branch 'beeware:main' into patch-12
proneon267 Sep 12, 2023
7b23209
Added `on_show` & `on_hide` handlers on winforms.
proneon267 Sep 12, 2023
0427a8f
Fixed overlapping event triggers on winforms.
proneon267 Sep 12, 2023
b9e0412
Added `on_show` & `on_hide` handlers on gtk.
proneon267 Sep 13, 2023
7f7468d
Added `on_show()` & `on_hide()` handlers on cocoa.
proneon267 Sep 14, 2023
429568b
Added `on_show()` & `on_hide()` on iOS.
proneon267 Sep 14, 2023
3440221
Corrected iOS implementation.
proneon267 Sep 14, 2023
fc8a29c
Added `on_show` & `on_hide` handlers on Android.
proneon267 Sep 14, 2023
6fd5e3c
Added `on_show` & `on_hide` on web.
proneon267 Sep 14, 2023
44bade9
Merge branch 'beeware:main' into patch-12
proneon267 Sep 14, 2023
8604faa
Modified as per suggestions.
proneon267 Sep 15, 2023
84234e8
Merge branch 'patch-12' of https://github.com/proneon267/toga into pa…
proneon267 Sep 15, 2023
eaeaf5d
Added comment for android version exclusion.
proneon267 Sep 15, 2023
ea28017
Merge branch 'main' into patch-12
proneon267 Nov 18, 2023
894a89c
Rebasing on the latest main branch
proneon267 Nov 18, 2023
cb2e541
Miscellaneous Fixes
proneon267 Nov 18, 2023
f7a014b
Added Core Tests
proneon267 Nov 18, 2023
0a1d093
Miscellaneous Fixes
proneon267 Nov 18, 2023
c441e77
Added tests for windows testbed
proneon267 Nov 19, 2023
059ea14
Fixed event triggers on gtk, cocoa
proneon267 Nov 19, 2023
81dd316
Fixed event triggers on Android
proneon267 Nov 19, 2023
d958394
Merge branch 'beeware:main' into patch-12
proneon267 Nov 19, 2023
d94a69a
Fixed cocoa implementation
Nov 19, 2023
8c364a1
Empty commit for CI
proneon267 Nov 19, 2023
c0405c3
Miscellaneous Fixes
proneon267 Nov 19, 2023
927b3d4
Fixed iOS implementation
proneon267 Nov 19, 2023
869da6a
Miscellaneous Fixes
proneon267 Nov 20, 2023
2423548
Miscellaneous Fixes
proneon267 Nov 20, 2023
9fd0e0e
Update changes/2009.feature.rst
proneon267 Nov 20, 2023
77b8822
Miscellaneous Fixes
proneon267 Nov 20, 2023
89f095d
Merge branch 'patch-12' of https://github.com/proneon267/toga into pa…
proneon267 Nov 20, 2023
69f90d5
Miscellaneous Fixes
proneon267 Nov 20, 2023
d5e8e3f
Merge branch 'beeware:main' into patch-12
proneon267 Dec 2, 2023
52e75b8
Merge branch 'beeware:main' into patch-12
proneon267 Dec 10, 2023
dc8b52d
Merge branch 'beeware:main' into patch-12
proneon267 Dec 17, 2023
004a98a
Merge branch 'beeware:main' into patch-12
proneon267 Dec 19, 2023
2fa2bfa
Merge branch 'beeware:main' into patch-12
proneon267 Dec 22, 2023
246eeb0
Merge branch 'beeware:main' into patch-12
proneon267 Jan 13, 2024
673e2cd
Merge branch 'beeware:main' into patch-12
proneon267 Jan 14, 2024
5cf9bbe
Merge branch 'beeware:main' into patch-12
proneon267 Jan 17, 2024
bfe62fa
Merge branch 'beeware:main' into patch-12
proneon267 Jan 17, 2024
e65c528
Merge branch 'beeware:main' into patch-12
proneon267 Jan 20, 2024
5273e54
Merge branch 'beeware:main' into patch-12
proneon267 Jan 25, 2024
69f3f25
Merge branch 'main' into patch-12
proneon267 Feb 3, 2024
30cb114
Merge branch 'beeware:main' into patch-12
proneon267 Feb 9, 2024
40d0320
Revert Changes
proneon267 Dec 23, 2024
f25ee59
Merge branch 'main' into pr/proneon267/2096
proneon267 Dec 23, 2024
11c6323
Add back PR changes
proneon267 Dec 23, 2024
4063113
Fix cocoa crash
proneon267 Dec 24, 2024
c3a55d8
Remove unused code from macOS
proneon267 Dec 24, 2024
cc9a023
Restart CI
proneon267 Dec 24, 2024
43b895f
Add New Tests
proneon267 Jan 1, 2025
b072c6b
Remove complexity from gtk
proneon267 Jan 1, 2025
8d0477d
Add test descriptions
proneon267 Jan 1, 2025
a017847
Cleanup winforms naming
proneon267 Jan 2, 2025
cb75101
Add DocStrings
proneon267 Jan 2, 2025
9b9b569
Add textual implementation
proneon267 Jan 2, 2025
db8e5e4
Fix textual
proneon267 Jan 2, 2025
e3fc53b
Cleanup textual
proneon267 Jan 2, 2025
7543ccb
Apply suggestions from code review
proneon267 Jan 7, 2025
6c62a86
Remove unused simulate implementation from core
proneon267 Jan 9, 2025
45e7a6e
Merge branch 'beeware:main' into patch-12
proneon267 Jan 9, 2025
58fe8aa
Apply suggestions from code review
proneon267 Jan 9, 2025
990372b
Add helper assertion utility
proneon267 Jan 9, 2025
331ce99
Restart CI
proneon267 Jan 9, 2025
14ce0aa
Add new test
proneon267 Jan 9, 2025
4d94fd2
Fix double triggering on winforms
proneon267 Jan 10, 2025
0f1fb13
Fix double triggering on gtk
proneon267 Jan 10, 2025
e131ca3
Ensure coverage on gtk
proneon267 Jan 10, 2025
b344c41
Add new test to core
proneon267 Jan 10, 2025
bd7615a
Fix double triggering test on testbed
proneon267 Jan 11, 2025
550f036
Save Progress
proneon267 Jan 12, 2025
b38e1d4
Cleanup code
proneon267 Jan 13, 2025
5393d09
Remove gtk visibility events implementation
proneon267 Jan 13, 2025
4c94efe
Apply suggestions from code review
proneon267 Jan 14, 2025
502a9eb
Apply suggestions from code review
proneon267 Jan 14, 2025
3a480e5
Pre-commit fix
proneon267 Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions android/src/toga_android/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from android.content import Context
from android.graphics.drawable import BitmapDrawable
from android.media import RingtoneManager
from android.os import Build
from android.view import Menu, MenuItem
from androidx.core.content import ContextCompat
from java import dynamic_proxy
Expand Down Expand Up @@ -35,22 +36,40 @@ def onCreate(self):

def onStart(self):
print("Toga app: onStart")
self._impl.interface.current_window.on_show()

def onResume(self):
def onResume(self): # pragma: no cover
print("Toga app: onResume")

def onPause(self):
print("Toga app: onPause") # pragma: no cover

def onStop(self):
print("Toga app: onStop") # pragma: no cover
# onTopResumedActivityChanged is not available on android versions less
# 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:
proneon267 marked this conversation as resolved.
Show resolved Hide resolved
self._impl.interface.current_window.on_gain_focus()

def onPause(self): # pragma: no cover
print("Toga app: onPause")
# onTopResumedActivityChanged is not available on android versions less
# than Q. onPause is the best indicator for the lost input focus event.
if Build.VERSION.SDK_INT < Build.VERSION_CODES.Q:
self._impl.interface.current_window.on_lose_focus()

def onStop(self): # pragma: no cover
print("Toga app: onStop")
self._impl.interface.current_window.on_hide()

def onDestroy(self):
print("Toga app: onDestroy") # pragma: no cover

def onRestart(self):
print("Toga app: onRestart") # pragma: no cover

def onTopResumedActivityChanged(self, isTopResumedActivity): # pragma: no cover
print("Toga app: onTopResumedActivityChanged")
if isTopResumedActivity:
self._impl.interface.current_window.on_gain_focus()
else:
self._impl.interface.current_window.on_lose_focus()

def onActivityResult(self, requestCode, resultCode, resultData):
print(f"Toga app: onActivityResult {requestCode=} {resultCode=} {resultData=}")
try:
Expand Down
1 change: 1 addition & 0 deletions changes/2009.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Windows can now respond to changes in focus and visibility.
23 changes: 23 additions & 0 deletions cocoa/src/toga_cocoa/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,17 @@ def windowDidResize_(self, notification) -> None:
# Set the window to the new size
self.interface.content.refresh()

@objc_method
def windowDidBecomeMain_(self, notification):
self.interface.on_gain_focus()

@objc_method
def windowDidResignMain_(self, notification):
self.interface.on_lose_focus()

@objc_method
def windowDidMiniaturize_(self, notification) -> None:
self.interface.on_hide()
if (
self.impl._pending_state_transition
and self.impl._pending_state_transition != WindowState.MINIMIZED
Expand All @@ -80,6 +89,7 @@ def windowDidMiniaturize_(self, notification) -> None:

@objc_method
def windowDidDeminiaturize_(self, notification) -> None:
self.interface.on_show()
self.impl._apply_state(self.impl._pending_state_transition)

@objc_method
Expand Down Expand Up @@ -257,7 +267,14 @@ def set_app(self, app):
pass

def show(self):
previously_visible = self.get_visible()
previous_state = self.get_window_state(in_progress_state=True)
self.native.makeKeyAndOrderFront(None)
# Avoid triggering the event here, as makeKeyAndOrderFront() also
# deminiaturizes the window, which will trigger windowDidDeminiaturize_.
# This could lead to double triggering, so prevent it here.
if not previously_visible and previous_state != WindowState.MINIMIZED:
self.interface.on_show()

######################################################################
# Window content and resources
Expand Down Expand Up @@ -333,7 +350,13 @@ def set_position(self, position):
######################################################################

def hide(self):
previously_visible = self.get_visible()
previous_state = self.get_window_state(in_progress_state=True)
self.native.orderOut(self.native)
# Avoid triggering the event if the window was already in a
# not-visible-user(minimized or hidden) state.
if previously_visible and previous_state != WindowState.MINIMIZED:
self.interface.on_hide()

def get_visible(self):
return (
Expand Down
3 changes: 3 additions & 0 deletions cocoa/tests_backend/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class WindowProbe(BaseProbe, DialogsMixin):
supports_unminimize = True
supports_minimize = True
supports_placement = True
# Calling window.show() un-minimizes the window on cocoa.
show_unminimizes_window = True
supports_visibility_events = True

def __init__(self, app, window):
super().__init__()
Expand Down
97 changes: 97 additions & 0 deletions core/src/toga/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,58 @@ def __call__(self, window: Window, **kwargs: Any) -> bool:
"""


class OnGainFocusHandler(Protocol):
def __call__(self, window: Window, **kwargs: Any) -> None:
"""A handler to invoke when a window gains input focus.

:param window: The window instance that gains input focus.
:param kwargs: Ensures compatibility with additional arguments introduced in
future versions.
"""
...


class OnLoseFocusHandler(Protocol):
def __call__(self, window: Window, **kwargs: Any) -> None:
"""A handler to invoke when a window loses input focus.

:param window: The window instance that loses input focus.
:param kwargs: Ensures compatibility with additional arguments introduced in
future ver
"""
...


class OnShowHandler(Protocol):
def __call__(self, window: Window, **kwargs: Any) -> None:
"""A handler to invoke when a window becomes visible.

This event will be triggered when a window is first displayed, and when the
window is restored from a minimized or hidden state. On mobile platforms, it is
also triggered when an app is made the currently active app.

:param window: The window instance that becomes visible.
:param kwargs: Ensures compatibility with additional arguments introduced in
future ver
"""
...


class OnHideHandler(Protocol):
def __call__(self, window: Window, **kwargs: Any) -> None:
"""A handler to invoke when a window stops being visible.

This event will be triggered when a window moves to a minimized or hidden state.
On mobile platforms, it is also triggered when an app is moved to the background
and is no longer the currently active app.

:param window: The window instance that becomes not visible to the user.
:param kwargs: Ensures compatibility with additional arguments introduced in
future ver
"""
...


_DialogResultT = TypeVar("_DialogResultT")


Expand Down Expand Up @@ -141,6 +193,10 @@ def __init__(
closable: bool = True,
minimizable: bool = True,
on_close: OnCloseHandler | None = None,
on_gain_focus: OnGainFocusHandler | None = None,
on_lose_focus: OnLoseFocusHandler | None = None,
on_show: OnShowHandler | None = None,
on_hide: OnHideHandler | None = None,
content: Widget | None = None,
) -> None:
"""Create a new Window.
Expand Down Expand Up @@ -193,6 +249,11 @@ def __init__(

self.on_close = on_close

self.on_gain_focus = on_gain_focus
self.on_lose_focus = on_lose_focus
self.on_show = on_show
self.on_hide = on_hide

def __lt__(self, other: Window) -> bool:
return self.id < other.id

Expand Down Expand Up @@ -530,6 +591,42 @@ def cleanup(window: Window, should_close: bool) -> None:

self._on_close = wrapped_handler(self, handler, cleanup=cleanup)

@property
def on_gain_focus(self) -> callable:
"""The handler to invoke if the window gains input focus."""
return self._on_gain_focus

@on_gain_focus.setter
def on_gain_focus(self, handler):
self._on_gain_focus = wrapped_handler(self, handler)

@property
def on_lose_focus(self) -> callable:
"""The handler to invoke if the window loses input focus."""
return self._on_lose_focus

@on_lose_focus.setter
def on_lose_focus(self, handler):
self._on_lose_focus = wrapped_handler(self, handler)

@property
def on_show(self) -> callable:
"""The handler to invoke if the window is shown from a hidden state."""
return self._on_show

@on_show.setter
def on_show(self, handler):
self._on_show = wrapped_handler(self, handler)

@property
def on_hide(self) -> callable:
"""The handler to invoke if the window is hidden from a visible state."""
return self._on_hide

@on_hide.setter
def on_hide(self, handler):
self._on_hide = wrapped_handler(self, handler)

######################################################################
# 2024-06: Backwards compatibility for <= 0.4.5
######################################################################
Expand Down
52 changes: 52 additions & 0 deletions core/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,55 @@ def _create(self):

def __repr__(self):
return f"Widget(id={self.id!r})"


def assert_window_gain_focus(window, trigger_expected=True):
on_gain_focus_handler = window.on_gain_focus._raw
on_lose_focus_handler = window.on_lose_focus._raw
if trigger_expected:
on_gain_focus_handler.assert_called_once_with(window)
else:
on_gain_focus_handler.assert_not_called()
on_lose_focus_handler.assert_not_called()

on_gain_focus_handler.reset_mock()
on_lose_focus_handler.reset_mock()


def assert_window_lose_focus(window, trigger_expected=True):
on_gain_focus_handler = window.on_gain_focus._raw
on_lose_focus_handler = window.on_lose_focus._raw
if trigger_expected:
on_lose_focus_handler.assert_called_once_with(window)
else:
on_lose_focus_handler.assert_not_called()
on_gain_focus_handler.assert_not_called()

on_gain_focus_handler.reset_mock()
on_lose_focus_handler.reset_mock()


def assert_window_on_show(window, trigger_expected=True):
on_show_handler = window.on_show._raw
on_hide_handler = window.on_hide._raw
if trigger_expected:
on_show_handler.assert_called_once_with(window)
else:
on_show_handler.assert_not_called()
on_hide_handler.assert_not_called()

on_show_handler.reset_mock()
on_hide_handler.reset_mock()


def assert_window_on_hide(window, trigger_expected=True):
on_show_handler = window.on_show._raw
on_hide_handler = window.on_hide._raw
if trigger_expected:
on_hide_handler.assert_called_once_with(window)
else:
on_hide_handler.assert_not_called()
on_show_handler.assert_not_called()

on_show_handler.reset_mock()
on_hide_handler.reset_mock()
Loading
Loading