| @@ -147,7 +147,7 @@ pub struct Window { · 146 unmodified lines |
| 147 | content_mask_stack: Vec<ContentMask<Pixels>>, | 147 | content_mask_stack: Vec<ContentMask<Pixels>>, |
| 148 | mouse_listeners: HashMap<TypeId, Vec<(StackingOrder, AnyListener)>>, | 148 | mouse_listeners: HashMap<TypeId, Vec<(StackingOrder, AnyListener)>>, |
| 149 | key_listeners: HashMap<TypeId, Vec<AnyListener>>, | 149 | key_listeners: HashMap<TypeId, Vec<AnyListener>>, |
| 150 | push_key_listeners: bool, | 150 | key_events_enabled: bool, |
| 151 | focus_stack: Vec<FocusId>, | 151 | focus_stack: Vec<FocusId>, |
| 152 | focus_parents_by_child: HashMap<FocusId, FocusId>, | 152 | focus_parents_by_child: HashMap<FocusId, FocusId>, |
| 153 | pub(crate) focus_listeners: Vec<AnyFocusListener>, | 153 | pub(crate) focus_listeners: Vec<AnyFocusListener>, |
| @@ -220,7 +220,7 @@ impl Window { · 66 unmodified lines |
| 220 | content_mask_stack: Vec::new(), | 220 | content_mask_stack: Vec::new(), |
| 221 | mouse_listeners: HashMap::default(), | 221 | mouse_listeners: HashMap::default(), |
| 222 | key_listeners: HashMap::default(), | 222 | key_listeners: HashMap::default(), |
| 223 | push_key_listeners: true, | 223 | key_events_enabled: true, |
| 224 | focus_stack: Vec::new(), | 224 | focus_stack: Vec::new(), |
| 225 | focus_parents_by_child: HashMap::default(), | 225 | focus_parents_by_child: HashMap::default(), |
| 226 | focus_listeners: Vec::new(), | 226 | focus_listeners: Vec::new(), |
| @@ -823,7 +823,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { · 596 unmodified lines |
| 823 | window.focus_listeners.clear(); | 823 | window.focus_listeners.clear(); |
| 824 | window.key_listeners.values_mut().for_each(Vec::clear); | 824 | window.key_listeners.values_mut().for_each(Vec::clear); |
| 825 | window.focus_parents_by_child.clear(); | 825 | window.focus_parents_by_child.clear(); |
| 826 | window.push_key_listeners = true; | 826 | window.key_events_enabled = true; |
| 827 | } | 827 | } |
| 828 | | 828 | |
| 829 | fn end_frame(&mut self) { | 829 | fn end_frame(&mut self) { |
| @@ -1219,31 +1219,17 @@ impl<'a, 'w, V: Send + Sync + 'static> ViewContext<'a, 'w, V> { · 389 unmodified lines |
| 1219 | })); | 1219 | })); |
| 1220 | } | 1220 | } |
| 1221 | | 1221 | |
| 1222 | pub fn with_focus<R>( | 1222 | pub fn with_key_listeners<R>( |
| 1223 | &mut self, | 1223 | &mut self, |
| 1224 | focus_handle: Option<FocusHandle>, | | |
| 1225 | key_listeners: &[(TypeId, KeyListener<V>)], | 1224 | key_listeners: &[(TypeId, KeyListener<V>)], |
| 1226 | f: impl FnOnce(&mut Self) -> R, | 1225 | f: impl FnOnce(&mut Self) -> R, |
| 1227 | ) -> R { | 1226 | ) -> R { |
| 1228 | let Some(focus_handle) = focus_handle else { | 1227 | if self.window.key_events_enabled { |
| 1229 | return f(self); | 1228 | let handle = self.handle(); |
| 1230 | }; | | |
| 1231 | | | |
| 1232 | let handle = self.handle(); | | |
| 1233 | let window = &mut *self.window; | | |
| 1234 | | | |
| 1235 | if let Some(parent_focus_id) = window.focus_stack.last() { | | |
| 1236 | window | | |
| 1237 | .focus_parents_by_child | | |
| 1238 | .insert(focus_handle.id, *parent_focus_id); | | |
| 1239 | } | | |
| 1240 | window.focus_stack.push(focus_handle.id); | | |
| 1241 | | | |
| 1242 | if window.push_key_listeners { | | |
| 1243 | for (type_id, listener) in key_listeners { | 1229 | for (type_id, listener) in key_listeners { |
| 1244 | let handle = handle.clone(); | 1230 | let handle = handle.clone(); |
| 1245 | let listener = listener.clone(); | 1231 | let listener = listener.clone(); |
| 1246 | window | 1232 | self.window |
| 1247 | .key_listeners | 1233 | .key_listeners |
| 1248 | .entry(*type_id) | 1234 | .entry(*type_id) |
| 1249 | .or_default() | 1235 | .or_default() |
| @@ -1255,17 +1241,35 @@ impl<'a, 'w, V: Send + Sync + 'static> ViewContext<'a, 'w, V> { · 5 unmodified lines |
| 1255 | } | 1241 | } |
| 1256 | } | 1242 | } |
| 1257 | | 1243 | |
| 1258 | if Some(focus_handle.id) == window.focus { | | |
| 1259 | window.push_key_listeners = false; | | |
| 1260 | } | | |
| 1261 | | | |
| 1262 | let result = f(self); | 1244 | let result = f(self); |
| 1263 | | 1245 | |
| 1264 | if self.window.push_key_listeners { | 1246 | if self.window.key_events_enabled { |
| 1265 | for (type_id, _) in key_listeners { | 1247 | for (type_id, _) in key_listeners { |
| 1266 | self.window.key_listeners.get_mut(type_id).unwrap().pop(); | 1248 | self.window.key_listeners.get_mut(type_id).unwrap().pop(); |
| 1267 | } | 1249 | } |
| 1268 | } | 1250 | } |
| | 1251 | |
| | 1252 | result |
| | 1253 | } |
| | 1254 | |
| | 1255 | pub fn with_focus<R>( |
| | 1256 | &mut self, |
| | 1257 | focus_handle: FocusHandle, |
| | 1258 | f: impl FnOnce(&mut Self) -> R, |
| | 1259 | ) -> R { |
| | 1260 | if let Some(parent_focus_id) = self.window.focus_stack.last().copied() { |
| | 1261 | self.window |
| | 1262 | .focus_parents_by_child |
| | 1263 | .insert(focus_handle.id, parent_focus_id); |
| | 1264 | } |
| | 1265 | self.window.focus_stack.push(focus_handle.id); |
| | 1266 | |
| | 1267 | if Some(focus_handle.id) == self.window.focus { |
| | 1268 | self.window.key_events_enabled = false; |
| | 1269 | } |
| | 1270 | |
| | 1271 | let result = f(self); |
| | 1272 | |
| 1269 | self.window.focus_stack.pop(); | 1273 | self.window.focus_stack.pop(); |
| 1270 | result | 1274 | result |
| 1271 | } | 1275 | } |