@@ -130,6 +130,27 @@ impl<R: Runtime> WindowManager<R> {
130
130
}
131
131
}
132
132
133
+ impl < R : Runtime > Window < R > {
134
+ /// Emits event to [`EventTarget::Window`] and [`EventTarget::WebviewWindow`]
135
+ fn emit_to_window < S : Serialize + Clone > ( & self , event : & str , payload : S ) -> crate :: Result < ( ) > {
136
+ let window_label = self . label ( ) ;
137
+ self . emit_filter ( event, payload, |target| match target {
138
+ EventTarget :: Window { label } | EventTarget :: WebviewWindow { label } => label == window_label,
139
+ _ => false ,
140
+ } )
141
+ }
142
+
143
+ /// Checks whether has js listener for [`EventTarget::Window`] or [`EventTarget::WebviewWindow`]
144
+ fn has_js_listener ( & self , event : & str ) -> bool {
145
+ let window_label = self . label ( ) ;
146
+ let listeners = self . manager ( ) . listeners ( ) ;
147
+ listeners. has_js_listener ( event, |target| match target {
148
+ EventTarget :: Window { label } | EventTarget :: WebviewWindow { label } => label == window_label,
149
+ _ => false ,
150
+ } )
151
+ }
152
+ }
153
+
133
154
#[ derive( Serialize , Clone ) ]
134
155
struct FileDropPayload < ' a > {
135
156
paths : & ' a Vec < PathBuf > ,
@@ -142,24 +163,16 @@ fn on_window_event<R: Runtime>(
142
163
event : & WindowEvent ,
143
164
) -> crate :: Result < ( ) > {
144
165
match event {
145
- WindowEvent :: Resized ( size) => window. emit ( WINDOW_RESIZED_EVENT , size) ?,
146
- WindowEvent :: Moved ( position) => window. emit ( WINDOW_MOVED_EVENT , position) ?,
166
+ WindowEvent :: Resized ( size) => window. emit_to_window ( WINDOW_RESIZED_EVENT , size) ?,
167
+ WindowEvent :: Moved ( position) => window. emit_to_window ( WINDOW_MOVED_EVENT , position) ?,
147
168
WindowEvent :: CloseRequested { api } => {
148
- let listeners = window. manager ( ) . listeners ( ) ;
149
- let has_js_listener =
150
- listeners. has_js_listener ( WINDOW_CLOSE_REQUESTED_EVENT , |target| match target {
151
- EventTarget :: Window { label } | EventTarget :: WebviewWindow { label } => {
152
- label == window. label ( )
153
- }
154
- _ => false ,
155
- } ) ;
156
- if has_js_listener {
169
+ if window. has_js_listener ( WINDOW_CLOSE_REQUESTED_EVENT ) {
157
170
api. prevent_close ( ) ;
158
171
}
159
- window. emit ( WINDOW_CLOSE_REQUESTED_EVENT , ( ) ) ?;
172
+ window. emit_to_window ( WINDOW_CLOSE_REQUESTED_EVENT , ( ) ) ?;
160
173
}
161
174
WindowEvent :: Destroyed => {
162
- window. emit ( WINDOW_DESTROYED_EVENT , ( ) ) ?;
175
+ window. emit_to_window ( WINDOW_DESTROYED_EVENT , ( ) ) ?;
163
176
let label = window. label ( ) ;
164
177
let webviews_map = manager. webview . webviews_lock ( ) ;
165
178
let webviews = webviews_map. values ( ) ;
@@ -169,7 +182,7 @@ fn on_window_event<R: Runtime>(
169
182
) ) ?;
170
183
}
171
184
}
172
- WindowEvent :: Focused ( focused) => window. emit (
185
+ WindowEvent :: Focused ( focused) => window. emit_to_window (
173
186
if * focused {
174
187
WINDOW_FOCUS_EVENT
175
188
} else {
@@ -181,7 +194,7 @@ fn on_window_event<R: Runtime>(
181
194
scale_factor,
182
195
new_inner_size,
183
196
..
184
- } => window. emit (
197
+ } => window. emit_to_window (
185
198
WINDOW_SCALE_FACTOR_CHANGED_EVENT ,
186
199
ScaleFactorChanged {
187
200
scale_factor : * scale_factor,
@@ -191,7 +204,7 @@ fn on_window_event<R: Runtime>(
191
204
WindowEvent :: FileDrop ( event) => match event {
192
205
FileDropEvent :: Hovered { paths, position } => {
193
206
let payload = FileDropPayload { paths, position } ;
194
- window. emit ( WINDOW_FILE_DROP_HOVER_EVENT , payload) ?
207
+ window. emit_to_window ( WINDOW_FILE_DROP_HOVER_EVENT , payload) ?
195
208
}
196
209
FileDropEvent :: Dropped { paths, position } => {
197
210
let scopes = window. state :: < Scopes > ( ) ;
@@ -203,12 +216,14 @@ fn on_window_event<R: Runtime>(
203
216
}
204
217
}
205
218
let payload = FileDropPayload { paths, position } ;
206
- window. emit ( WINDOW_FILE_DROP_EVENT , payload) ?
219
+ window. emit_to_window ( WINDOW_FILE_DROP_EVENT , payload) ?
207
220
}
208
- FileDropEvent :: Cancelled => window. emit ( WINDOW_FILE_DROP_CANCELLED_EVENT , ( ) ) ?,
221
+ FileDropEvent :: Cancelled => window. emit_to_window ( WINDOW_FILE_DROP_CANCELLED_EVENT , ( ) ) ?,
209
222
_ => unimplemented ! ( ) ,
210
223
} ,
211
- WindowEvent :: ThemeChanged ( theme) => window. emit ( WINDOW_THEME_CHANGED , theme. to_string ( ) ) ?,
224
+ WindowEvent :: ThemeChanged ( theme) => {
225
+ window. emit_to_window ( WINDOW_THEME_CHANGED , theme. to_string ( ) ) ?
226
+ }
212
227
}
213
228
Ok ( ( ) )
214
229
}
0 commit comments