@@ -106,6 +106,58 @@ def __call__(self, window: Window, **kwargs: Any) -> bool:
106
106
"""
107
107
108
108
109
+ class OnGainFocusHandler (Protocol ):
110
+ def __call__ (self , window : Window , ** kwargs : Any ) -> None :
111
+ """A handler to invoke when a window gains input focus.
112
+
113
+ :param window: The window instance that gains input focus.
114
+ :param kwargs: Ensures compatibility with additional arguments introduced in
115
+ future versions.
116
+ """
117
+ ...
118
+
119
+
120
+ class OnLoseFocusHandler (Protocol ):
121
+ def __call__ (self , window : Window , ** kwargs : Any ) -> None :
122
+ """A handler to invoke when a window loses input focus.
123
+
124
+ :param window: The window instance that loses input focus.
125
+ :param kwargs: Ensures compatibility with additional arguments introduced in
126
+ future ver
127
+ """
128
+ ...
129
+
130
+
131
+ class OnShowHandler (Protocol ):
132
+ def __call__ (self , window : Window , ** kwargs : Any ) -> None :
133
+ """A handler to invoke when a window becomes visible.
134
+
135
+ This event will be triggered when a window is first displayed, and when the
136
+ window is restored from a minimized or hidden state. On mobile platforms, it is
137
+ also triggered when an app is made the currently active app.
138
+
139
+ :param window: The window instance that becomes visible.
140
+ :param kwargs: Ensures compatibility with additional arguments introduced in
141
+ future ver
142
+ """
143
+ ...
144
+
145
+
146
+ class OnHideHandler (Protocol ):
147
+ def __call__ (self , window : Window , ** kwargs : Any ) -> None :
148
+ """A handler to invoke when a window stops being visible.
149
+
150
+ This event will be triggered when a window moves to a minimized or hidden state.
151
+ On mobile platforms, it is also triggered when an app is moved to the background
152
+ and is no longer the currently active app.
153
+
154
+ :param window: The window instance that becomes not visible to the user.
155
+ :param kwargs: Ensures compatibility with additional arguments introduced in
156
+ future ver
157
+ """
158
+ ...
159
+
160
+
109
161
_DialogResultT = TypeVar ("_DialogResultT" )
110
162
111
163
@@ -141,6 +193,10 @@ def __init__(
141
193
closable : bool = True ,
142
194
minimizable : bool = True ,
143
195
on_close : OnCloseHandler | None = None ,
196
+ on_gain_focus : OnGainFocusHandler | None = None ,
197
+ on_lose_focus : OnLoseFocusHandler | None = None ,
198
+ on_show : OnShowHandler | None = None ,
199
+ on_hide : OnHideHandler | None = None ,
144
200
content : Widget | None = None ,
145
201
) -> None :
146
202
"""Create a new Window.
@@ -193,6 +249,11 @@ def __init__(
193
249
194
250
self .on_close = on_close
195
251
252
+ self .on_gain_focus = on_gain_focus
253
+ self .on_lose_focus = on_lose_focus
254
+ self .on_show = on_show
255
+ self .on_hide = on_hide
256
+
196
257
def __lt__ (self , other : Window ) -> bool :
197
258
return self .id < other .id
198
259
@@ -554,6 +615,42 @@ def cleanup(window: Window, should_close: bool) -> None:
554
615
555
616
self ._on_close = wrapped_handler (self , handler , cleanup = cleanup )
556
617
618
+ @property
619
+ def on_gain_focus (self ) -> callable :
620
+ """The handler to invoke if the window gains input focus."""
621
+ return self ._on_gain_focus
622
+
623
+ @on_gain_focus .setter
624
+ def on_gain_focus (self , handler ):
625
+ self ._on_gain_focus = wrapped_handler (self , handler )
626
+
627
+ @property
628
+ def on_lose_focus (self ) -> callable :
629
+ """The handler to invoke if the window loses input focus."""
630
+ return self ._on_lose_focus
631
+
632
+ @on_lose_focus .setter
633
+ def on_lose_focus (self , handler ):
634
+ self ._on_lose_focus = wrapped_handler (self , handler )
635
+
636
+ @property
637
+ def on_show (self ) -> callable :
638
+ """The handler to invoke if the window is shown from a hidden state."""
639
+ return self ._on_show
640
+
641
+ @on_show .setter
642
+ def on_show (self , handler ):
643
+ self ._on_show = wrapped_handler (self , handler )
644
+
645
+ @property
646
+ def on_hide (self ) -> callable :
647
+ """The handler to invoke if the window is hidden from a visible state."""
648
+ return self ._on_hide
649
+
650
+ @on_hide .setter
651
+ def on_hide (self , handler ):
652
+ self ._on_hide = wrapped_handler (self , handler )
653
+
557
654
######################################################################
558
655
# 2024-06: Backwards compatibility for <= 0.4.5
559
656
######################################################################
0 commit comments