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 64 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
64 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
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
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
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
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)
11 changes: 11 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,11 @@ def resize_content(self):
self.native.ClientSize.Width,
self.native.ClientSize.Height - vertical_shift,
)

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)