Skip to content

Commit cc57e4a

Browse files
AppLifecycleState enum and typed event handler (#2808)
1 parent 577ebb5 commit cc57e4a

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

sdk/python/packages/flet-core/src/flet_core/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
from flet_core.outlined_button import OutlinedButton
172172
from flet_core.padding import Padding
173173
from flet_core.page import (
174+
AppLifecycleStateChangeEvent,
174175
KeyboardEvent,
175176
LoginEvent,
176177
Page,
@@ -249,6 +250,7 @@
249250
from flet_core.transform import Offset, Rotate, Scale
250251
from flet_core.transparent_pointer import TransparentPointer
251252
from flet_core.types import (
253+
AppLifecycleState,
252254
BlendMode,
253255
BoxShape,
254256
Brightness,

sdk/python/packages/flet-core/src/flet_core/page.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from flet_core.snack_bar import SnackBar
4444
from flet_core.theme import Theme
4545
from flet_core.types import (
46+
AppLifecycleState,
4647
Brightness,
4748
CrossAxisAlignment,
4849
FloatingActionButtonLocation,
@@ -155,7 +156,13 @@ def __init__(
155156
"platformBrightnessChange",
156157
self.__on_platform_brightness_change.get_handler(),
157158
)
158-
self.__on_app_lifecycle_state_change = EventHandler()
159+
160+
def convert_app_lifecycle_state_change_event(e):
161+
return AppLifecycleStateChangeEvent(e)
162+
163+
self.__on_app_lifecycle_state_change = EventHandler(
164+
convert_app_lifecycle_state_change_event
165+
)
159166
self._add_event_handler(
160167
"app_lifecycle_state_change",
161168
self.__on_app_lifecycle_state_change.get_handler(),
@@ -2086,3 +2093,10 @@ def __init__(self, padding, view_padding, view_insets) -> None:
20862093

20872094
def __str__(self) -> str:
20882095
return f"PageMediaData(padding={self.padding}, view_padding={self.view_padding}, view_insets={self.view_insets})"
2096+
2097+
2098+
class AppLifecycleStateChangeEvent(ControlEvent):
2099+
def __init__(self, e: ControlEvent):
2100+
super().__init__(e.target, e.name, e.data, e.control, e.page)
2101+
2102+
self.state = AppLifecycleState(e.data)

sdk/python/packages/flet-core/src/flet_core/types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,13 @@ class FloatingActionButtonLocation(Enum):
239239
START_DOCKED = "startDocked"
240240
START_FLOAT = "startFloat"
241241
START_TOP = "startTop"
242+
243+
244+
class AppLifecycleState(Enum):
245+
SHOW = "show"
246+
RESUME = "resume"
247+
HIDE = "hide"
248+
INACTIVE = "inactive"
249+
PAUSE = "pause"
250+
DETACH = "detach"
251+
RESTART = "restart"

sdk/python/packages/flet-runtime/src/flet_runtime/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,13 @@ def on_app_startup(page_url):
135135
if view == AppView.WEB_BROWSER and url_prefix is None and not force_web_server:
136136
open_in_browser(page_url)
137137

138+
loop = asyncio.get_running_loop()
139+
138140
terminate = asyncio.Event()
139141

140142
def exit_gracefully(signum, frame):
141143
logger.debug("Gracefully terminating Flet app...")
142-
asyncio.get_event_loop().call_soon_threadsafe(terminate.set)
144+
loop.call_soon_threadsafe(terminate.set)
143145

144146
signal.signal(signal.SIGINT, exit_gracefully)
145147
signal.signal(signal.SIGTERM, exit_gracefully)

0 commit comments

Comments
 (0)