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

Merged
merged 105 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
105 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
961bd2c
Merge branch 'main' into patch-12
proneon267 Jan 21, 2025
99006e1
Simplify implementations
proneon267 Jan 21, 2025
915e7f9
Implement visibility events on gtk
proneon267 Jan 21, 2025
ea5ece0
Skip test on wayland
proneon267 Jan 21, 2025
e6124a5
Remove unused probe flag
proneon267 Jan 21, 2025
8583069
Add note on docs
proneon267 Jan 21, 2025
fc50763
Restart CI
proneon267 Jan 21, 2025
f2cfd35
merged tests
proneon267 Jan 23, 2025
968acfd
merge tests on core
proneon267 Jan 23, 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
20 changes: 20 additions & 0 deletions android/src/toga_android/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import toga
from android.media import RingtoneManager
from android.os import Build
from toga.command import Group

from .libs.activity import IPythonApp, MainActivity
Expand Down Expand Up @@ -36,9 +37,17 @@ def onStart(self):

def onResume(self):
print("Toga app: onResume")
if Build.VERSION.SDK_INT < Build.VERSION_CODES.Q:
proneon267 marked this conversation as resolved.
Show resolved Hide resolved
self._impl.interface.on_gain_focus(self._impl.interface)
for window in self._impl.interface.windows:
Copy link
Member

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.

window.on_gain_focus(self._impl.interface)

def onPause(self):
print("Toga app: onPause")
if Build.VERSION.SDK_INT < Build.VERSION_CODES.Q:
self._impl.interface.on_lose_focus(self._impl.interface)
for window in self._impl.interface.windows:
window.on_lose_focus(self._impl.interface)

def onStop(self):
print("Toga app: onStop")
Expand All @@ -49,6 +58,17 @@ def onDestroy(self):
def onRestart(self):
print("Toga app: onRestart")

def onTopResumedActivityChanged(self, isTopResumedActivity):
print("Toga app: onTopResumedActivityChanged")
if isTopResumedActivity:
self._impl.interface.on_gain_focus(self._impl.interface)
for window in self._impl.interface.windows:
window.on_gain_focus(self._impl.interface)
else:
self._impl.interface.on_lose_focus(self._impl.interface)
for window in self._impl.interface.windows:
window.on_lose_focus(self._impl.interface)

def onActivityResult(self, requestCode, resultCode, resultData):
"""Callback method, called from MainActivity when an Intent ends.

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 @@
Added `on_gain_focus` and `on_lose_focus` handlers both on the `toga.App` and `toga.Window`.
proneon267 marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions cocoa/src/toga_cocoa/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def applicationOpenUntitledFile_(self, sender) -> bool:
self.impl.select_file()
return True

@objc_method
def applicationDidBecomeActive_(self, application):
self.impl.interface.on_gain_focus(self.interface)
proneon267 marked this conversation as resolved.
Show resolved Hide resolved

@objc_method
def applicationWillResignActive_(self, application):
self.impl.interface.on_lose_focus(self.interface)

@objc_method
def addDocument_(self, document) -> None:
# print("Add Document", document)
Expand Down
8 changes: 8 additions & 0 deletions cocoa/src/toga_cocoa/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def windowDidResize_(self, notification) -> None:
# Set the window to the new size
self.interface.content.refresh()

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

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

######################################################################
# Toolbar delegate methods
######################################################################
Expand Down
25 changes: 25 additions & 0 deletions core/src/toga/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def __init__(
minimizable: bool = True,
factory: None = None, # DEPRECATED !
on_close: None = None,
on_gain_focus: callable | None = None,
on_lose_focus: callable | None = None,
) -> None:
######################################################################
# 2022-09: Backwards compatibility
Expand All @@ -152,6 +154,8 @@ def __init__(
closeable=True,
minimizable=minimizable,
on_close=on_close,
on_gain_focus=on_gain_focus,
on_lose_focus=on_lose_focus,
)

@Window.on_close.setter
Expand Down Expand Up @@ -185,6 +189,8 @@ def __init__(
startup: AppStartupMethod | None = None,
windows: Iterable[Window] = (),
on_exit: OnExitHandler | None = None,
on_gain_focus: callable | None = None,
on_lose_focus: callable | None = None,
factory: None = None, # DEPRECATED !
):
"""An App is the top level of any GUI program.
Expand Down Expand Up @@ -384,6 +390,9 @@ class is defined, and look for a ``.dist-info`` file matching that name.
self._impl = self._create_impl()
self.on_exit = on_exit

self.on_gain_focus = on_gain_focus
self.on_lose_focus = on_lose_focus

def _create_impl(self):
return self.factory.App(interface=self)

Expand Down Expand Up @@ -628,6 +637,22 @@ def add_background_task(self, handler: BackgroundTask) -> None:
"""
self._impl.loop.call_soon_threadsafe(wrapped_handler(self, handler), None)

@property
def on_gain_focus(self) -> callable:
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:
return self._on_lose_focus

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


class DocumentApp(App):
def __init__(
Expand Down
21 changes: 21 additions & 0 deletions core/src/toga/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ def __init__(
minimizable: bool = True,
factory: None = None, # DEPRECATED !
on_close: OnCloseHandler | None = None,
on_gain_focus: callable | None = None,
on_lose_focus: callable | None = None,
) -> None:
######################################################################
# 2022-09: Backwards compatibility
Expand Down Expand Up @@ -123,6 +125,9 @@ def __init__(

self.on_close = on_close

self.on_gain_focus = on_gain_focus
self.on_lose_focus = on_lose_focus

@property
def id(self) -> str:
"""The DOM identifier for the window.
Expand Down Expand Up @@ -271,6 +276,22 @@ def close(self) -> None:
self.app.windows -= self
self._impl.close()

@property
def on_gain_focus(self) -> callable:
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:
return self._on_lose_focus

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

############################################################
# Dialogs
############################################################
Expand Down
29 changes: 28 additions & 1 deletion examples/window/window/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,42 @@ def close_handler(self, window, **kwargs):
return False
return True

def on_app_gain_focus(self, widget, **kwargs):
self.app_focus_label.text = "App is in focus"
print("App is in focus")

def on_app_lose_focus(self, widget, **kwargs):
self.app_focus_label.text = "App is not in focus"
print("App is not in focus")

def on_window_gain_focus(self, widget, **kwargs):
self.window_focus_label.text = "MainWindow is in focus"
print("MainWindow is in focus")

def on_window_lose_focus(self, widget, **kwargs):
self.window_focus_label.text = "MainWindow is not in focus"
print("MainWindow is not in focus")

def startup(self):
# Track in-app closes
self.close_count = 0

self.on_gain_focus = self.on_app_gain_focus
self.on_lose_focus = self.on_app_lose_focus

# Set up main window
self.main_window = toga.MainWindow(title=self.name)
self.main_window = toga.MainWindow(
title=self.name,
on_gain_focus=self.on_window_gain_focus,
on_lose_focus=self.on_window_lose_focus,
)
self.on_exit = self.exit_handler

# Label to show responses.
self.label = toga.Label("Ready.")

self.app_focus_label = toga.Label("App focus status")
self.window_focus_label = toga.Label("Window focus status")
# Buttons
btn_style = Pack(flex=1, padding=5)
btn_do_origin = toga.Button(
Expand Down Expand Up @@ -175,6 +200,8 @@ def startup(self):
self.inner_box = toga.Box(
children=[
self.label,
self.app_focus_label,
self.window_focus_label,
btn_do_origin,
btn_do_left,
btn_do_right,
Expand Down
10 changes: 10 additions & 0 deletions gtk/src/toga_gtk/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def __init__(self, interface, title, position, size):
self.native._impl = self

self.native.connect("delete-event", self.gtk_delete_event)
self.native.connect("focus-in-event", self.window_on_gain_focus)
self.native.connect("focus-out-event", self.window_on_lose_focus)

self.native.set_default_size(size[0], size[1])

Expand Down Expand Up @@ -138,3 +140,11 @@ def set_full_screen(self, is_full_screen):
self.native.fullscreen()
else:
self.native.unfullscreen()

def window_on_gain_focus(self, sender, event):
self.interface.app.on_gain_focus(self.interface)
self.interface.on_gain_focus(self.interface)

def window_on_lose_focus(self, sender, event):
self.interface.app.on_lose_focus(self.interface)
self.interface.on_lose_focus(self.interface)
6 changes: 6 additions & 0 deletions iOS/src/toga_iOS/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ class PythonAppDelegate(UIResponder):
@objc_method
def applicationDidBecomeActive_(self, application) -> None:
print("App became active.")
App.app.interface.on_gain_focus(App.app.interface)
for window in App.app.interface.windows:
window.on_gain_focus(App.app.interface)

@objc_method
def applicationWillResignActive_(self, application) -> None:
print("App about to leave foreground.", flush=True)
App.app.interface.on_lose_focus(App.app.interface)
for window in App.app.interface.windows:
window.on_lose_focus(App.app.interface)

@objc_method
def applicationDidEnterBackground_(self, application) -> None:
Expand Down
10 changes: 10 additions & 0 deletions web/src/toga_web/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def __init__(self, interface, title, position, size):
app_placeholder = js.document.getElementById("app-placeholder")
app_placeholder.appendChild(self.native)

js.document.body.onfocus = self.window_on_gain_focus
js.document.body.onblur = self.window_on_lose_focusS
self.set_title(title)

def get_title(self):
Expand Down Expand Up @@ -77,3 +79,11 @@ def set_size(self, size):

def set_full_screen(self, is_full_screen):
self.interface.factory.not_implemented("Window.set_full_screen()")

def window_on_gain_focus(self, sender, event):
self.interface.app.on_gain_focus(self.interface)
self.interface.on_gain_focus(self.interface)

def window_on_lose_focus(self, sender, event):
self.interface.app.on_lose_focus(self.interface)
self.interface.on_lose_focus(self.interface)
13 changes: 13 additions & 0 deletions winforms/src/toga_winforms/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def __init__(self, interface, title, position, size):
self.native.FormBorderStyle = self.native.FormBorderStyle.FixedSingle
self.native.MaximizeBox = False

self.native.Activated += self.window_on_gain_focus
self.native.Deactivate += self.window_on_lose_focus

def create_toolbar(self):
if self.interface.toolbar:
if self.toolbar_native:
Expand Down Expand Up @@ -173,3 +176,13 @@ def resize_content(self):
self.native.ClientSize.Width,
self.native.ClientSize.Height - vertical_shift,
)

def window_on_gain_focus(self, sender, event):
if self.interface.app is not None:
self.interface.app.on_gain_focus(self.interface)
self.interface.on_gain_focus(self.interface)

def window_on_lose_focus(self, sender, event):
if self.interface.app is not None:
self.interface.app.on_lose_focus(self.interface)
self.interface.on_lose_focus(self.interface)