From 6ef447866ac571feca4710450fe5fba4954d1a7b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 28 May 2021 15:25:15 -0700 Subject: [PATCH] Rename context parameters to `cx` in gpui --- gpui/examples/text.rs | 20 +- gpui/src/app.rs | 846 ++++++++++------------- gpui/src/elements.rs | 58 +- gpui/src/elements/align.rs | 20 +- gpui/src/elements/canvas.rs | 4 +- gpui/src/elements/constrained_box.rs | 20 +- gpui/src/elements/container.rs | 24 +- gpui/src/elements/event_handler.rs | 22 +- gpui/src/elements/flex.rs | 42 +- gpui/src/elements/label.rs | 28 +- gpui/src/elements/line_box.rs | 30 +- gpui/src/elements/mouse_event_handler.rs | 36 +- gpui/src/elements/stack.rs | 24 +- gpui/src/elements/svg.rs | 8 +- gpui/src/elements/uniform_list.rs | 36 +- gpui/src/keymap.rs | 36 +- gpui/src/platform/mac/fonts.rs | 12 +- gpui/src/presenter.rs | 96 +-- gpui/src/text_layout.rs | 6 +- gpui_macros/src/lib.rs | 8 +- 20 files changed, 643 insertions(+), 733 deletions(-) diff --git a/gpui/examples/text.rs b/gpui/examples/text.rs index 7c59336672f0db03fbd26b284be4fa4ea00329df..11c327e2bb75488e1a9c33d8065af4aa920ffb37 100644 --- a/gpui/examples/text.rs +++ b/gpui/examples/text.rs @@ -10,9 +10,9 @@ use simplelog::SimpleLogger; fn main() { SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger"); - gpui::App::new(()).unwrap().run(|ctx| { - ctx.platform().activate(true); - ctx.add_window(|_| TextView); + gpui::App::new(()).unwrap().run(|cx| { + cx.platform().activate(true); + cx.add_window(|_| TextView); }); } @@ -58,15 +58,15 @@ impl gpui::Element for TextElement { &mut self, bounds: RectF, _: &mut Self::LayoutState, - ctx: &mut gpui::PaintContext, + cx: &mut gpui::PaintContext, ) -> Self::PaintState { let font_size = 12.; - let family = ctx.font_cache.load_family(&["SF Pro Display"]).unwrap(); - let normal = ctx + let family = cx.font_cache.load_family(&["SF Pro Display"]).unwrap(); + let normal = cx .font_cache .select_font(family, &Default::default()) .unwrap(); - let bold = ctx + let bold = cx .font_cache .select_font( family, @@ -78,7 +78,7 @@ impl gpui::Element for TextElement { .unwrap(); let text = "Hello world!"; - let line = ctx.text_layout_cache.layout_str( + let line = cx.text_layout_cache.layout_str( text, font_size, &[ @@ -90,12 +90,12 @@ impl gpui::Element for TextElement { ], ); - ctx.scene.push_quad(Quad { + cx.scene.push_quad(Quad { bounds: bounds, background: Some(ColorU::white()), ..Default::default() }); - line.paint(bounds.origin(), bounds, ctx); + line.paint(bounds.origin(), bounds, cx); } fn dispatch_event( diff --git a/gpui/src/app.rs b/gpui/src/app.rs index efaf410dbb4c59407c776cd5b92fdf6cd826c803..b11f5c63da91f9365f0e7aab957399458ece87c7 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -34,16 +34,16 @@ pub trait Entity: 'static + Send + Sync { pub trait View: Entity { fn ui_name() -> &'static str; - fn render<'a>(&self, app: &AppContext) -> ElementBox; - fn on_focus(&mut self, _ctx: &mut ViewContext) {} - fn on_blur(&mut self, _ctx: &mut ViewContext) {} + fn render<'a>(&self, cx: &AppContext) -> ElementBox; + fn on_focus(&mut self, _: &mut ViewContext) {} + fn on_blur(&mut self, _: &mut ViewContext) {} fn keymap_context(&self, _: &AppContext) -> keymap::Context { Self::default_keymap_context() } fn default_keymap_context() -> keymap::Context { - let mut ctx = keymap::Context::default(); - ctx.set.insert(Self::ui_name().into()); - ctx + let mut cx = keymap::Context::default(); + cx.set.insert(Self::ui_name().into()); + cx } } @@ -114,14 +114,14 @@ impl App { ) -> T { let platform = platform::test::platform(); let foreground = Rc::new(executor::Foreground::test()); - let ctx = Rc::new(RefCell::new(MutableAppContext::new( + let cx = Rc::new(RefCell::new(MutableAppContext::new( foreground, Rc::new(platform), asset_source, ))); - ctx.borrow_mut().weak_self = Some(Rc::downgrade(&ctx)); - let mut ctx = ctx.borrow_mut(); - f(&mut *ctx) + cx.borrow_mut().weak_self = Some(Rc::downgrade(&cx)); + let mut cx = cx.borrow_mut(); + f(&mut *cx) } pub fn test_async(asset_source: A, f: Fn) -> T @@ -131,7 +131,7 @@ impl App { { let platform = Rc::new(platform::test::platform()); let foreground = Rc::new(executor::Foreground::test()); - let ctx = TestAppContext( + let cx = TestAppContext( Rc::new(RefCell::new(MutableAppContext::new( foreground.clone(), platform.clone(), @@ -139,9 +139,9 @@ impl App { ))), platform, ); - ctx.0.borrow_mut().weak_self = Some(Rc::downgrade(&ctx.0)); + cx.0.borrow_mut().weak_self = Some(Rc::downgrade(&cx.0)); - let future = f(ctx); + let future = f(cx); smol::block_on(foreground.run(future)) } @@ -154,21 +154,20 @@ impl App { asset_source, )))); - let ctx = app.0.clone(); + let cx = app.0.clone(); platform.on_menu_command(Box::new(move |command, arg| { - let mut ctx = ctx.borrow_mut(); - if let Some(key_window_id) = ctx.platform.key_window_id() { - if let Some((presenter, _)) = - ctx.presenters_and_platform_windows.get(&key_window_id) + let mut cx = cx.borrow_mut(); + if let Some(key_window_id) = cx.platform.key_window_id() { + if let Some((presenter, _)) = cx.presenters_and_platform_windows.get(&key_window_id) { let presenter = presenter.clone(); - let path = presenter.borrow().dispatch_path(ctx.as_ref()); - ctx.dispatch_action_any(key_window_id, &path, command, arg.unwrap_or(&())); + let path = presenter.borrow().dispatch_path(cx.as_ref()); + cx.dispatch_action_any(key_window_id, &path, command, arg.unwrap_or(&())); } else { - ctx.dispatch_global_action_any(command, arg.unwrap_or(&())); + cx.dispatch_global_action_any(command, arg.unwrap_or(&())); } } else { - ctx.dispatch_global_action_any(command, arg.unwrap_or(&())); + cx.dispatch_global_action_any(command, arg.unwrap_or(&())); } })); @@ -180,11 +179,11 @@ impl App { where F: 'static + FnMut(&mut MutableAppContext), { - let ctx = self.0.clone(); + let cx = self.0.clone(); self.0 .borrow() .platform - .on_become_active(Box::new(move || callback(&mut *ctx.borrow_mut()))); + .on_become_active(Box::new(move || callback(&mut *cx.borrow_mut()))); self } @@ -192,11 +191,11 @@ impl App { where F: 'static + FnMut(&mut MutableAppContext), { - let ctx = self.0.clone(); + let cx = self.0.clone(); self.0 .borrow() .platform - .on_resign_active(Box::new(move || callback(&mut *ctx.borrow_mut()))); + .on_resign_active(Box::new(move || callback(&mut *cx.borrow_mut()))); self } @@ -204,9 +203,9 @@ impl App { where F: 'static + FnMut(Event, &mut MutableAppContext) -> bool, { - let ctx = self.0.clone(); + let cx = self.0.clone(); self.0.borrow().platform.on_event(Box::new(move |event| { - callback(event, &mut *ctx.borrow_mut()) + callback(event, &mut *cx.borrow_mut()) })); self } @@ -215,12 +214,12 @@ impl App { where F: 'static + FnMut(Vec, &mut MutableAppContext), { - let ctx = self.0.clone(); + let cx = self.0.clone(); self.0 .borrow() .platform .on_open_files(Box::new(move |paths| { - callback(paths, &mut *ctx.borrow_mut()) + callback(paths, &mut *cx.borrow_mut()) })); self } @@ -231,13 +230,13 @@ impl App { { let platform = self.0.borrow().platform.clone(); platform.run(Box::new(move || { - let mut ctx = self.0.borrow_mut(); - on_finish_launching(&mut *ctx); + let mut cx = self.0.borrow_mut(); + on_finish_launching(&mut *cx); })) } pub fn font_cache(&self) -> Arc { - self.0.borrow().ctx.font_cache.clone() + self.0.borrow().cx.font_cache.clone() } fn update T>(&mut self, callback: F) -> T { @@ -347,7 +346,7 @@ impl TestAppContext { } pub fn font_cache(&self) -> Arc { - self.0.borrow().ctx.font_cache.clone() + self.0.borrow().cx.font_cache.clone() } pub fn platform(&self) -> Rc { @@ -398,11 +397,11 @@ impl AsyncAppContext { T: Entity, F: FnOnce(&mut ModelContext) -> T, { - self.update(|ctx| ctx.add_model(build_model)) + self.update(|cx| cx.add_model(build_model)) } pub fn background_executor(&self) -> Arc { - self.0.borrow().ctx.background.clone() + self.0.borrow().cx.background.clone() } } @@ -426,9 +425,9 @@ impl ReadModelWith for AsyncAppContext { handle: &ModelHandle, read: F, ) -> T { - let ctx = self.0.borrow(); - let ctx = ctx.as_ref(); - read(handle.read(ctx), ctx) + let cx = self.0.borrow(); + let cx = cx.as_ref(); + read(handle.read(cx), cx) } } @@ -452,9 +451,9 @@ impl ReadViewWith for AsyncAppContext { V: View, F: FnOnce(&V, &AppContext) -> T, { - let ctx = self.0.borrow(); - let ctx = ctx.as_ref(); - read(handle.read(ctx), ctx) + let cx = self.0.borrow(); + let cx = cx.as_ref(); + read(handle.read(cx), cx) } } @@ -478,9 +477,9 @@ impl ReadModelWith for TestAppContext { handle: &ModelHandle, read: F, ) -> T { - let ctx = self.0.borrow(); - let ctx = ctx.as_ref(); - read(handle.read(ctx), ctx) + let cx = self.0.borrow(); + let cx = cx.as_ref(); + read(handle.read(cx), cx) } } @@ -504,9 +503,9 @@ impl ReadViewWith for TestAppContext { V: View, F: FnOnce(&V, &AppContext) -> T, { - let ctx = self.0.borrow(); - let ctx = ctx.as_ref(); - read(handle.read(ctx), ctx) + let cx = self.0.borrow(); + let cx = cx.as_ref(); + read(handle.read(cx), cx) } } @@ -519,7 +518,7 @@ pub struct MutableAppContext { weak_self: Option>>, platform: Rc, assets: Arc, - ctx: AppContext, + cx: AppContext, actions: HashMap>>>, global_actions: HashMap>>, keystroke_matcher: keymap::Matcher, @@ -548,7 +547,7 @@ impl MutableAppContext { weak_self: None, platform, assets: Arc::new(AssetCache::new(asset_source)), - ctx: AppContext { + cx: AppContext { models: Default::default(), views: Default::default(), windows: Default::default(), @@ -584,7 +583,7 @@ impl MutableAppContext { } pub fn font_cache(&self) -> &Arc { - &self.ctx.font_cache + &self.cx.font_cache } pub fn foreground_executor(&self) -> &Rc { @@ -592,7 +591,7 @@ impl MutableAppContext { } pub fn background_executor(&self) -> &Arc { - &self.ctx.background + &self.cx.background } pub fn on_debug_elements(&mut self, window_id: usize, callback: F) @@ -606,7 +605,7 @@ impl MutableAppContext { pub fn debug_elements(&self, window_id: usize) -> Option { self.debug_elements_callbacks .get(&window_id) - .map(|debug_elements| debug_elements(&self.ctx)) + .map(|debug_elements| debug_elements(&self.cx)) } pub fn add_action(&mut self, name: S, mut handler: F) @@ -621,20 +620,20 @@ impl MutableAppContext { let handler = Box::new( move |view: &mut dyn AnyView, arg: &dyn Any, - app: &mut MutableAppContext, + cx: &mut MutableAppContext, window_id: usize, view_id: usize| { match arg.downcast_ref() { Some(arg) => { - let mut ctx = ViewContext::new(app, window_id, view_id); + let mut cx = ViewContext::new(cx, window_id, view_id); handler( view.as_any_mut() .downcast_mut() .expect("downcast is type safe"), arg, - &mut ctx, + &mut cx, ); - ctx.halt_action_dispatch + cx.halt_action_dispatch } None => { log::error!("Could not downcast argument for action {}", name_clone); @@ -660,9 +659,9 @@ impl MutableAppContext { { let name = name.into(); let name_clone = name.clone(); - let handler = Box::new(move |arg: &dyn Any, app: &mut MutableAppContext| { + let handler = Box::new(move |arg: &dyn Any, cx: &mut MutableAppContext| { if let Some(arg) = arg.downcast_ref() { - handler(arg, app); + handler(arg, cx); } else { log::error!("Could not downcast argument for action {}", name_clone); } @@ -672,30 +671,30 @@ impl MutableAppContext { } pub fn window_ids(&self) -> impl Iterator + '_ { - self.ctx.windows.keys().cloned() + self.cx.windows.keys().cloned() } pub fn root_view(&self, window_id: usize) -> Option> { - self.ctx + self.cx .windows .get(&window_id) .and_then(|window| window.root_view.clone().downcast::()) } pub fn root_view_id(&self, window_id: usize) -> Option { - self.ctx.root_view_id(window_id) + self.cx.root_view_id(window_id) } pub fn focused_view_id(&self, window_id: usize) -> Option { - self.ctx.focused_view_id(window_id) + self.cx.focused_view_id(window_id) } pub fn render_view(&self, window_id: usize, view_id: usize) -> Result { - self.ctx.render_view(window_id, view_id) + self.cx.render_view(window_id, view_id) } pub fn render_views(&self, window_id: usize) -> HashMap { - self.ctx.render_views(window_id) + self.cx.render_views(window_id) } pub fn update T>(&mut self, callback: F) -> T { @@ -792,7 +791,7 @@ impl MutableAppContext { let mut halted_dispatch = false; for view_id in path.iter().rev() { - if let Some(mut view) = self.ctx.views.remove(&(window_id, *view_id)) { + if let Some(mut view) = self.cx.views.remove(&(window_id, *view_id)) { let type_id = view.as_any().type_id(); if let Some((name, mut handlers)) = self @@ -813,7 +812,7 @@ impl MutableAppContext { .insert(name, handlers); } - self.ctx.views.insert((window_id, *view_id), view); + self.cx.views.insert((window_id, *view_id), view); if halted_dispatch { break; @@ -857,7 +856,7 @@ impl MutableAppContext { let mut context_chain = Vec::new(); let mut context = keymap::Context::default(); for view_id in &responder_chain { - if let Some(view) = self.ctx.views.get(&(window_id, *view_id)) { + if let Some(view) = self.cx.views.get(&(window_id, *view_id)) { context.extend(view.keymap_context(self.as_ref())); context_chain.push(context.clone()); } else { @@ -869,10 +868,10 @@ impl MutableAppContext { } let mut pending = false; - for (i, ctx) in context_chain.iter().enumerate().rev() { + for (i, cx) in context_chain.iter().enumerate().rev() { match self .keystroke_matcher - .push_keystroke(keystroke.clone(), responder_chain[i], ctx) + .push_keystroke(keystroke.clone(), responder_chain[i], cx) { MatchResult::None => {} MatchResult::Pending => pending = true, @@ -899,10 +898,10 @@ impl MutableAppContext { { self.pending_flushes += 1; let model_id = post_inc(&mut self.next_entity_id); - let handle = ModelHandle::new(model_id, &self.ctx.ref_counts); - let mut ctx = ModelContext::new(self, model_id); - let model = build_model(&mut ctx); - self.ctx.models.insert(model_id, Box::new(model)); + let handle = ModelHandle::new(model_id, &self.cx.ref_counts); + let mut cx = ModelContext::new(self, model_id); + let model = build_model(&mut cx); + self.cx.models.insert(model_id, Box::new(model)); self.flush_effects(); handle } @@ -916,7 +915,7 @@ impl MutableAppContext { let window_id = post_inc(&mut self.next_window_id); let root_view = self.add_view(window_id, build_root_view); - self.ctx.windows.insert( + self.cx.windows.insert( window_id, Window { root_view: root_view.clone().into(), @@ -925,14 +924,14 @@ impl MutableAppContext { }, ); self.open_platform_window(window_id); - root_view.update(self, |view, ctx| view.on_focus(ctx)); + root_view.update(self, |view, cx| view.on_focus(cx)); self.flush_effects(); (window_id, root_view) } pub fn remove_window(&mut self, window_id: usize) { - self.ctx.windows.remove(&window_id); + self.cx.windows.remove(&window_id); self.presenters_and_platform_windows.remove(&window_id); self.remove_dropped_entities(); } @@ -949,7 +948,7 @@ impl MutableAppContext { let text_layout_cache = TextLayoutCache::new(self.platform.fonts()); let presenter = Rc::new(RefCell::new(Presenter::new( window_id, - self.ctx.font_cache.clone(), + self.cx.font_cache.clone(), text_layout_cache, self.assets.clone(), self, @@ -959,12 +958,12 @@ impl MutableAppContext { let mut app = self.upgrade(); let presenter = presenter.clone(); window.on_event(Box::new(move |event| { - app.update(|ctx| { + app.update(|cx| { if let Event::KeyDown { keystroke, .. } = &event { - if ctx + if cx .dispatch_keystroke( window_id, - presenter.borrow().dispatch_path(ctx.as_ref()), + presenter.borrow().dispatch_path(cx.as_ref()), keystroke, ) .unwrap() @@ -973,7 +972,7 @@ impl MutableAppContext { } } - presenter.borrow_mut().dispatch_event(event, ctx); + presenter.borrow_mut().dispatch_event(event, cx); }) })); } @@ -982,11 +981,11 @@ impl MutableAppContext { let mut app = self.upgrade(); let presenter = presenter.clone(); window.on_resize(Box::new(move |window| { - app.update(|ctx| { + app.update(|cx| { let scene = presenter.borrow_mut().build_scene( window.size(), window.scale_factor(), - ctx, + cx, ); window.present_scene(scene); }) @@ -996,15 +995,15 @@ impl MutableAppContext { { let mut app = self.upgrade(); window.on_close(Box::new(move || { - app.update(|ctx| ctx.remove_window(window_id)); + app.update(|cx| cx.remove_window(window_id)); })); } self.presenters_and_platform_windows .insert(window_id, (presenter.clone(), window)); - self.on_debug_elements(window_id, move |ctx| { - presenter.borrow().debug_elements(ctx).unwrap() + self.on_debug_elements(window_id, move |cx| { + presenter.borrow().debug_elements(cx).unwrap() }); } @@ -1013,7 +1012,7 @@ impl MutableAppContext { T: View, F: FnOnce(&mut ViewContext) -> T, { - self.add_option_view(window_id, |ctx| Some(build_view(ctx))) + self.add_option_view(window_id, |cx| Some(build_view(cx))) .unwrap() } @@ -1028,11 +1027,11 @@ impl MutableAppContext { { let view_id = post_inc(&mut self.next_entity_id); self.pending_flushes += 1; - let handle = ViewHandle::new(window_id, view_id, &self.ctx.ref_counts); - let mut ctx = ViewContext::new(self, window_id, view_id); - let handle = if let Some(view) = build_view(&mut ctx) { - self.ctx.views.insert((window_id, view_id), Box::new(view)); - if let Some(window) = self.ctx.windows.get_mut(&window_id) { + let handle = ViewHandle::new(window_id, view_id, &self.cx.ref_counts); + let mut cx = ViewContext::new(self, window_id, view_id); + let handle = if let Some(view) = build_view(&mut cx) { + self.cx.views.insert((window_id, view_id), Box::new(view)); + if let Some(window) = self.cx.windows.get_mut(&window_id) { window .invalidation .get_or_insert_with(Default::default) @@ -1050,13 +1049,13 @@ impl MutableAppContext { fn remove_dropped_entities(&mut self) { loop { let (dropped_models, dropped_views, dropped_values) = - self.ctx.ref_counts.lock().take_dropped(); + self.cx.ref_counts.lock().take_dropped(); if dropped_models.is_empty() && dropped_views.is_empty() && dropped_values.is_empty() { break; } for model_id in dropped_models { - self.ctx.models.remove(&model_id); + self.cx.models.remove(&model_id); self.subscriptions.remove(&model_id); self.model_observations.remove(&model_id); } @@ -1064,8 +1063,8 @@ impl MutableAppContext { for (window_id, view_id) in dropped_views { self.subscriptions.remove(&view_id); self.model_observations.remove(&view_id); - self.ctx.views.remove(&(window_id, view_id)); - let change_focus_to = self.ctx.windows.get_mut(&window_id).and_then(|window| { + self.cx.views.remove(&(window_id, view_id)); + let change_focus_to = self.cx.windows.get_mut(&window_id).and_then(|window| { window .invalidation .get_or_insert_with(Default::default) @@ -1083,7 +1082,7 @@ impl MutableAppContext { } } - let mut values = self.ctx.values.write(); + let mut values = self.cx.values.write(); for key in dropped_values { values.remove(&key); } @@ -1126,7 +1125,7 @@ impl MutableAppContext { fn update_windows(&mut self) { let mut invalidations = HashMap::new(); - for (window_id, window) in &mut self.ctx.windows { + for (window_id, window) in &mut self.cx.windows { if let Some(invalidation) = window.invalidation.take() { invalidations.insert(*window_id, invalidation); } @@ -1153,9 +1152,9 @@ impl MutableAppContext { for mut subscription in subscriptions { let alive = match &mut subscription { Subscription::FromModel { model_id, callback } => { - if let Some(mut model) = self.ctx.models.remove(model_id) { + if let Some(mut model) = self.cx.models.remove(model_id) { callback(model.as_any_mut(), payload.as_ref(), self, *model_id); - self.ctx.models.insert(*model_id, model); + self.cx.models.insert(*model_id, model); true } else { false @@ -1166,7 +1165,7 @@ impl MutableAppContext { view_id, callback, } => { - if let Some(mut view) = self.ctx.views.remove(&(*window_id, *view_id)) { + if let Some(mut view) = self.cx.views.remove(&(*window_id, *view_id)) { callback( view.as_any_mut(), payload.as_ref(), @@ -1174,7 +1173,7 @@ impl MutableAppContext { *window_id, *view_id, ); - self.ctx.views.insert((*window_id, *view_id), view); + self.cx.views.insert((*window_id, *view_id), view); true } else { false @@ -1194,13 +1193,13 @@ impl MutableAppContext { fn notify_model_observers(&mut self, observed_id: usize) { if let Some(observations) = self.model_observations.remove(&observed_id) { - if self.ctx.models.contains_key(&observed_id) { + if self.cx.models.contains_key(&observed_id) { for mut observation in observations { let alive = match &mut observation { ModelObservation::FromModel { model_id, callback } => { - if let Some(mut model) = self.ctx.models.remove(model_id) { + if let Some(mut model) = self.cx.models.remove(model_id) { callback(model.as_any_mut(), observed_id, self, *model_id); - self.ctx.models.insert(*model_id, model); + self.cx.models.insert(*model_id, model); true } else { false @@ -1211,7 +1210,7 @@ impl MutableAppContext { view_id, callback, } => { - if let Some(mut view) = self.ctx.views.remove(&(*window_id, *view_id)) { + if let Some(mut view) = self.cx.views.remove(&(*window_id, *view_id)) { callback( view.as_any_mut(), observed_id, @@ -1219,7 +1218,7 @@ impl MutableAppContext { *window_id, *view_id, ); - self.ctx.views.insert((*window_id, *view_id), view); + self.cx.views.insert((*window_id, *view_id), view); true } else { false @@ -1239,7 +1238,7 @@ impl MutableAppContext { } fn notify_view_observers(&mut self, window_id: usize, view_id: usize) { - if let Some(window) = self.ctx.windows.get_mut(&window_id) { + if let Some(window) = self.cx.windows.get_mut(&window_id) { window .invalidation .get_or_insert_with(Default::default) @@ -1248,10 +1247,10 @@ impl MutableAppContext { } if let Some(observations) = self.view_observations.remove(&view_id) { - if self.ctx.views.contains_key(&(window_id, view_id)) { + if self.cx.views.contains_key(&(window_id, view_id)) { for mut observation in observations { let alive = if let Some(mut view) = self - .ctx + .cx .views .remove(&(observation.window_id, observation.view_id)) { @@ -1263,7 +1262,7 @@ impl MutableAppContext { observation.window_id, observation.view_id, ); - self.ctx + self.cx .views .insert((observation.window_id, observation.view_id), view); true @@ -1284,7 +1283,7 @@ impl MutableAppContext { fn focus(&mut self, window_id: usize, focused_id: usize) { if self - .ctx + .cx .windows .get(&window_id) .map(|w| w.focused_view_id) @@ -1295,22 +1294,22 @@ impl MutableAppContext { self.pending_flushes += 1; - let blurred_id = self.ctx.windows.get_mut(&window_id).map(|window| { + let blurred_id = self.cx.windows.get_mut(&window_id).map(|window| { let blurred_id = window.focused_view_id; window.focused_view_id = focused_id; blurred_id }); if let Some(blurred_id) = blurred_id { - if let Some(mut blurred_view) = self.ctx.views.remove(&(window_id, blurred_id)) { + if let Some(mut blurred_view) = self.cx.views.remove(&(window_id, blurred_id)) { blurred_view.on_blur(self, window_id, blurred_id); - self.ctx.views.insert((window_id, blurred_id), blurred_view); + self.cx.views.insert((window_id, blurred_id), blurred_view); } } - if let Some(mut focused_view) = self.ctx.views.remove(&(window_id, focused_id)) { + if let Some(mut focused_view) = self.cx.views.remove(&(window_id, focused_id)) { focused_view.on_focus(self, window_id, focused_id); - self.ctx.views.insert((window_id, focused_id), focused_view); + self.cx.views.insert((window_id, focused_id), focused_view); } self.flush_effects(); @@ -1322,8 +1321,8 @@ impl MutableAppContext { Fut: 'static + Future, T: 'static, { - let ctx = AsyncAppContext(self.weak_self.as_ref().unwrap().upgrade().unwrap()); - self.foreground.spawn(f(ctx)) + let cx = AsyncAppContext(self.weak_self.as_ref().unwrap().upgrade().unwrap()); + self.foreground.spawn(f(cx)) } pub fn write_to_clipboard(&self, item: ClipboardItem) { @@ -1337,7 +1336,7 @@ impl MutableAppContext { impl ReadModel for MutableAppContext { fn read_model(&self, handle: &ModelHandle) -> &T { - if let Some(model) = self.ctx.models.get(&handle.model_id) { + if let Some(model) = self.cx.models.get(&handle.model_id) { model .as_any() .downcast_ref() @@ -1354,17 +1353,17 @@ impl UpdateModel for MutableAppContext { T: Entity, F: FnOnce(&mut T, &mut ModelContext) -> S, { - if let Some(mut model) = self.ctx.models.remove(&handle.model_id) { + if let Some(mut model) = self.cx.models.remove(&handle.model_id) { self.pending_flushes += 1; - let mut ctx = ModelContext::new(self, handle.model_id); + let mut cx = ModelContext::new(self, handle.model_id); let result = update( model .as_any_mut() .downcast_mut() .expect("downcast is type safe"), - &mut ctx, + &mut cx, ); - self.ctx.models.insert(handle.model_id, model); + self.cx.models.insert(handle.model_id, model); self.flush_effects(); result } else { @@ -1375,7 +1374,7 @@ impl UpdateModel for MutableAppContext { impl ReadView for MutableAppContext { fn read_view(&self, handle: &ViewHandle) -> &T { - if let Some(view) = self.ctx.views.get(&(handle.window_id, handle.view_id)) { + if let Some(view) = self.cx.views.get(&(handle.window_id, handle.view_id)) { view.as_any().downcast_ref().expect("downcast is type safe") } else { panic!("circular view reference"); @@ -1391,19 +1390,19 @@ impl UpdateView for MutableAppContext { { self.pending_flushes += 1; let mut view = self - .ctx + .cx .views .remove(&(handle.window_id, handle.view_id)) .expect("circular view update"); - let mut ctx = ViewContext::new(self, handle.window_id, handle.view_id); + let mut cx = ViewContext::new(self, handle.window_id, handle.view_id); let result = update( view.as_any_mut() .downcast_mut() .expect("downcast is type safe"), - &mut ctx, + &mut cx, ); - self.ctx + self.cx .views .insert((handle.window_id, handle.view_id), view); self.flush_effects(); @@ -1413,7 +1412,7 @@ impl UpdateView for MutableAppContext { impl AsRef for MutableAppContext { fn as_ref(&self) -> &AppContext { - &self.ctx + &self.cx } } @@ -1558,10 +1557,10 @@ pub trait AnyView: Send + Sync { fn as_any(&self) -> &dyn Any; fn as_any_mut(&mut self) -> &mut dyn Any; fn ui_name(&self) -> &'static str; - fn render<'a>(&self, app: &AppContext) -> ElementBox; - fn on_focus(&mut self, app: &mut MutableAppContext, window_id: usize, view_id: usize); - fn on_blur(&mut self, app: &mut MutableAppContext, window_id: usize, view_id: usize); - fn keymap_context(&self, app: &AppContext) -> keymap::Context; + fn render<'a>(&self, cx: &AppContext) -> ElementBox; + fn on_focus(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize); + fn on_blur(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize); + fn keymap_context(&self, cx: &AppContext) -> keymap::Context; } impl AnyView for T @@ -1580,22 +1579,22 @@ where T::ui_name() } - fn render<'a>(&self, app: &AppContext) -> ElementBox { - View::render(self, app) + fn render<'a>(&self, cx: &AppContext) -> ElementBox { + View::render(self, cx) } - fn on_focus(&mut self, app: &mut MutableAppContext, window_id: usize, view_id: usize) { - let mut ctx = ViewContext::new(app, window_id, view_id); - View::on_focus(self, &mut ctx); + fn on_focus(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize) { + let mut cx = ViewContext::new(cx, window_id, view_id); + View::on_focus(self, &mut cx); } - fn on_blur(&mut self, app: &mut MutableAppContext, window_id: usize, view_id: usize) { - let mut ctx = ViewContext::new(app, window_id, view_id); - View::on_blur(self, &mut ctx); + fn on_blur(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize) { + let mut cx = ViewContext::new(cx, window_id, view_id); + View::on_blur(self, &mut cx); } - fn keymap_context(&self, app: &AppContext) -> keymap::Context { - View::keymap_context(self, app) + fn keymap_context(&self, cx: &AppContext) -> keymap::Context { + View::keymap_context(self, cx) } } @@ -1617,11 +1616,11 @@ impl<'a, T: Entity> ModelContext<'a, T> { } pub fn background_executor(&self) -> &Arc { - &self.app.ctx.background + &self.app.cx.background } pub fn thread_pool(&self) -> &scoped_pool::Pool { - &self.app.ctx.thread_pool + &self.app.cx.thread_pool } pub fn halt_stream(&mut self) { @@ -1654,8 +1653,8 @@ impl<'a, T: Entity> ModelContext<'a, T> { callback: Box::new(move |model, payload, app, model_id| { let model = model.downcast_mut().expect("downcast is type safe"); let payload = payload.downcast_ref().expect("downcast is type safe"); - let mut ctx = ModelContext::new(app, model_id); - callback(model, payload, &mut ctx); + let mut cx = ModelContext::new(app, model_id); + callback(model, payload, &mut cx); }), }); } @@ -1680,9 +1679,9 @@ impl<'a, T: Entity> ModelContext<'a, T> { model_id: self.model_id, callback: Box::new(move |model, observed_id, app, model_id| { let model = model.downcast_mut().expect("downcast is type safe"); - let observed = ModelHandle::new(observed_id, &app.ctx.ref_counts); - let mut ctx = ModelContext::new(app, model_id); - callback(model, observed, &mut ctx); + let observed = ModelHandle::new(observed_id, &app.cx.ref_counts); + let mut cx = ModelContext::new(app, model_id); + callback(model, observed, &mut cx); }), }); } @@ -1696,7 +1695,7 @@ impl<'a, T: Entity> ModelContext<'a, T> { } pub fn handle(&self) -> ModelHandle { - ModelHandle::new(self.model_id, &self.app.ctx.ref_counts) + ModelHandle::new(self.model_id, &self.app.cx.ref_counts) } pub fn spawn(&self, f: F) -> Task @@ -1706,13 +1705,13 @@ impl<'a, T: Entity> ModelContext<'a, T> { S: 'static, { let handle = self.handle(); - self.app.spawn(|ctx| f(handle, ctx)) + self.app.spawn(|cx| f(handle, cx)) } } impl AsRef for ModelContext<'_, M> { fn as_ref(&self) -> &AppContext { - &self.app.ctx + &self.app.cx } } @@ -1758,7 +1757,7 @@ impl<'a, T: View> ViewContext<'a, T> { } pub fn handle(&self) -> ViewHandle { - ViewHandle::new(self.window_id, self.view_id, &self.app.ctx.ref_counts) + ViewHandle::new(self.window_id, self.view_id, &self.app.cx.ref_counts) } pub fn window_id(&self) -> usize { @@ -1774,7 +1773,7 @@ impl<'a, T: View> ViewContext<'a, T> { } pub fn background_executor(&self) -> &Arc { - &self.app.ctx.background + &self.app.cx.background } pub fn prompt(&self, level: PromptLevel, msg: &str, answers: &[&str], done_fn: F) @@ -1852,9 +1851,9 @@ impl<'a, T: View> ViewContext<'a, T> { F: 'static + FnMut(&mut T, ModelHandle, &E::Event, &mut ViewContext), { let emitter_handle = handle.downgrade(); - self.subscribe(handle, move |model, payload, ctx| { - if let Some(emitter_handle) = emitter_handle.upgrade(ctx.as_ref()) { - callback(model, emitter_handle, payload, ctx); + self.subscribe(handle, move |model, payload, cx| { + if let Some(emitter_handle) = emitter_handle.upgrade(cx.as_ref()) { + callback(model, emitter_handle, payload, cx); } }); } @@ -1866,9 +1865,9 @@ impl<'a, T: View> ViewContext<'a, T> { F: 'static + FnMut(&mut T, ViewHandle, &V::Event, &mut ViewContext), { let emitter_handle = handle.downgrade(); - self.subscribe(handle, move |view, payload, ctx| { - if let Some(emitter_handle) = emitter_handle.upgrade(ctx.as_ref()) { - callback(view, emitter_handle, payload, ctx); + self.subscribe(handle, move |view, payload, cx| { + if let Some(emitter_handle) = emitter_handle.upgrade(cx.as_ref()) { + callback(view, emitter_handle, payload, cx); } }); } @@ -1889,8 +1888,8 @@ impl<'a, T: View> ViewContext<'a, T> { callback: Box::new(move |entity, payload, app, window_id, view_id| { let entity = entity.downcast_mut().expect("downcast is type safe"); let payload = payload.downcast_ref().expect("downcast is type safe"); - let mut ctx = ViewContext::new(app, window_id, view_id); - callback(entity, payload, &mut ctx); + let mut cx = ViewContext::new(app, window_id, view_id); + callback(entity, payload, &mut cx); }), }); } @@ -1916,9 +1915,9 @@ impl<'a, T: View> ViewContext<'a, T> { view_id: self.view_id, callback: Box::new(move |view, observed_id, app, window_id, view_id| { let view = view.downcast_mut().expect("downcast is type safe"); - let observed = ModelHandle::new(observed_id, &app.ctx.ref_counts); - let mut ctx = ViewContext::new(app, window_id, view_id); - callback(view, observed, &mut ctx); + let observed = ModelHandle::new(observed_id, &app.cx.ref_counts); + let mut cx = ViewContext::new(app, window_id, view_id); + callback(view, observed, &mut cx); }), }); } @@ -1946,10 +1945,10 @@ impl<'a, T: View> ViewContext<'a, T> { let observed_handle = ViewHandle::new( observed_view_id, observed_window_id, - &app.ctx.ref_counts, + &app.cx.ref_counts, ); - let mut ctx = ViewContext::new(app, observing_window_id, observing_view_id); - callback(view, observed_handle, &mut ctx); + let mut cx = ViewContext::new(app, observing_window_id, observing_view_id); + callback(view, observed_handle, &mut cx); }, ), }); @@ -1970,7 +1969,7 @@ impl<'a, T: View> ViewContext<'a, T> { S: 'static, { let handle = self.handle(); - self.app.spawn(|ctx| f(handle, ctx)) + self.app.spawn(|cx| f(handle, cx)) } } @@ -1982,7 +1981,7 @@ impl AsRef for &AppContext { impl AsRef for ViewContext<'_, M> { fn as_ref(&self) -> &AppContext { - &self.app.ctx + &self.app.cx } } @@ -2059,42 +2058,42 @@ impl ModelHandle { self.model_id } - pub fn read<'a, A: ReadModel>(&self, app: &'a A) -> &'a T { - app.read_model(self) + pub fn read<'a, C: ReadModel>(&self, cx: &'a C) -> &'a T { + cx.read_model(self) } - pub fn read_with<'a, A, F, S>(&self, ctx: &A, read: F) -> S + pub fn read_with<'a, C, F, S>(&self, cx: &C, read: F) -> S where - A: ReadModelWith, + C: ReadModelWith, F: FnOnce(&T, &AppContext) -> S, { - ctx.read_model_with(self, read) + cx.read_model_with(self, read) } - pub fn update(&self, app: &mut A, update: F) -> S + pub fn update(&self, cx: &mut C, update: F) -> S where - A: UpdateModel, + C: UpdateModel, F: FnOnce(&mut T, &mut ModelContext) -> S, { - app.update_model(self, update) + cx.update_model(self, update) } pub fn condition( &self, - ctx: &TestAppContext, + cx: &TestAppContext, mut predicate: impl FnMut(&T, &AppContext) -> bool, ) -> impl Future { let (tx, mut rx) = mpsc::channel(1024); - let mut ctx = ctx.0.borrow_mut(); - self.update(&mut *ctx, |_, ctx| { - ctx.observe(self, { + let mut cx = cx.0.borrow_mut(); + self.update(&mut *cx, |_, cx| { + cx.observe(self, { let mut tx = tx.clone(); move |_, _, _| { tx.blocking_send(()).ok(); } }); - ctx.subscribe(self, { + cx.subscribe(self, { let mut tx = tx.clone(); move |_, _, _| { tx.blocking_send(()).ok(); @@ -2102,7 +2101,7 @@ impl ModelHandle { }) }); - let ctx = ctx.weak_self.as_ref().unwrap().upgrade().unwrap(); + let cx = cx.weak_self.as_ref().unwrap().upgrade().unwrap(); let handle = self.downgrade(); let duration = if std::env::var("CI").is_ok() { Duration::from_secs(2) @@ -2114,14 +2113,14 @@ impl ModelHandle { timeout(duration, async move { loop { { - let ctx = ctx.borrow(); - let ctx = ctx.as_ref(); + let cx = cx.borrow(); + let cx = cx.as_ref(); if predicate( handle - .upgrade(ctx) + .upgrade(cx) .expect("model dropped with pending condition") - .read(ctx), - ctx, + .read(cx), + cx, ) { break; } @@ -2209,10 +2208,10 @@ impl WeakModelHandle { } } - pub fn upgrade(&self, ctx: impl AsRef) -> Option> { - let ctx = ctx.as_ref(); - if ctx.models.contains_key(&self.model_id) { - Some(ModelHandle::new(self.model_id, &ctx.ref_counts)) + pub fn upgrade(&self, cx: impl AsRef) -> Option> { + let cx = cx.as_ref(); + if cx.models.contains_key(&self.model_id) { + Some(ModelHandle::new(self.model_id, &cx.ref_counts)) } else { None } @@ -2258,48 +2257,48 @@ impl ViewHandle { self.view_id } - pub fn read<'a, A: ReadView>(&self, app: &'a A) -> &'a T { - app.read_view(self) + pub fn read<'a, C: ReadView>(&self, cx: &'a C) -> &'a T { + cx.read_view(self) } - pub fn read_with(&self, ctx: &A, read: F) -> S + pub fn read_with(&self, cx: &C, read: F) -> S where - A: ReadViewWith, + C: ReadViewWith, F: FnOnce(&T, &AppContext) -> S, { - ctx.read_view_with(self, read) + cx.read_view_with(self, read) } - pub fn update(&self, app: &mut A, update: F) -> S + pub fn update(&self, cx: &mut C, update: F) -> S where - A: UpdateView, + C: UpdateView, F: FnOnce(&mut T, &mut ViewContext) -> S, { - app.update_view(self, update) + cx.update_view(self, update) } - pub fn is_focused(&self, app: &AppContext) -> bool { - app.focused_view_id(self.window_id) + pub fn is_focused(&self, cx: &AppContext) -> bool { + cx.focused_view_id(self.window_id) .map_or(false, |focused_id| focused_id == self.view_id) } pub fn condition( &self, - ctx: &TestAppContext, + cx: &TestAppContext, mut predicate: impl FnMut(&T, &AppContext) -> bool, ) -> impl Future { let (tx, mut rx) = mpsc::channel(1024); - let mut ctx = ctx.0.borrow_mut(); - self.update(&mut *ctx, |_, ctx| { - ctx.observe_view(self, { + let mut cx = cx.0.borrow_mut(); + self.update(&mut *cx, |_, cx| { + cx.observe_view(self, { let mut tx = tx.clone(); move |_, _, _| { tx.blocking_send(()).ok(); } }); - ctx.subscribe(self, { + cx.subscribe(self, { let mut tx = tx.clone(); move |_, _, _| { tx.blocking_send(()).ok(); @@ -2307,7 +2306,7 @@ impl ViewHandle { }) }); - let ctx = ctx.weak_self.as_ref().unwrap().upgrade().unwrap(); + let cx = cx.weak_self.as_ref().unwrap().upgrade().unwrap(); let handle = self.downgrade(); let duration = if std::env::var("CI").is_ok() { Duration::from_secs(2) @@ -2319,14 +2318,14 @@ impl ViewHandle { timeout(duration, async move { loop { { - let ctx = ctx.borrow(); - let ctx = ctx.as_ref(); + let cx = cx.borrow(); + let cx = cx.as_ref(); if predicate( handle - .upgrade(ctx) + .upgrade(cx) .expect("view dropped with pending condition") - .read(ctx), - ctx, + .read(cx), + cx, ) { break; } @@ -2515,13 +2514,13 @@ impl WeakViewHandle { } } - pub fn upgrade(&self, ctx: impl AsRef) -> Option> { - let ctx = ctx.as_ref(); - if ctx.ref_counts.lock().is_entity_alive(self.view_id) { + pub fn upgrade(&self, cx: impl AsRef) -> Option> { + let cx = cx.as_ref(); + if cx.ref_counts.lock().is_entity_alive(self.view_id) { Some(ViewHandle::new( self.window_id, self.view_id, - &ctx.ref_counts, + &cx.ref_counts, )) } else { None @@ -2557,9 +2556,8 @@ impl ValueHandle { } } - pub fn read(&self, ctx: &AppContext, f: impl FnOnce(&T) -> R) -> R { - f(ctx - .values + pub fn read(&self, cx: &AppContext, f: impl FnOnce(&T) -> R) -> R { + f(cx.values .read() .get(&(self.tag_type_id, self.id)) .unwrap() @@ -2567,9 +2565,8 @@ impl ValueHandle { .unwrap()) } - pub fn update(&self, ctx: &AppContext, f: impl FnOnce(&mut T) -> R) -> R { - f(ctx - .values + pub fn update(&self, cx: &AppContext, f: impl FnOnce(&mut T) -> R) -> R { + f(cx.values .write() .get_mut(&(self.tag_type_id, self.id)) .unwrap() @@ -2706,7 +2703,7 @@ mod tests { use smol::future::poll_once; #[crate::test(self)] - fn test_model_handles(app: &mut MutableAppContext) { + fn test_model_handles(cx: &mut MutableAppContext) { struct Model { other: Option>, events: Vec, @@ -2717,12 +2714,12 @@ mod tests { } impl Model { - fn new(other: Option>, ctx: &mut ModelContext) -> Self { + fn new(other: Option>, cx: &mut ModelContext) -> Self { if let Some(other) = other.as_ref() { - ctx.observe(other, |me, _, _| { + cx.observe(other, |me, _, _| { me.events.push("notified".into()); }); - ctx.subscribe(other, |me, event, _| { + cx.subscribe(other, |me, event, _| { me.events.push(format!("observed event {}", event)); }); } @@ -2734,19 +2731,19 @@ mod tests { } } - let handle_1 = app.add_model(|ctx| Model::new(None, ctx)); - let handle_2 = app.add_model(|ctx| Model::new(Some(handle_1.clone()), ctx)); - assert_eq!(app.ctx.models.len(), 2); + let handle_1 = cx.add_model(|cx| Model::new(None, cx)); + let handle_2 = cx.add_model(|cx| Model::new(Some(handle_1.clone()), cx)); + assert_eq!(cx.cx.models.len(), 2); - handle_1.update(app, |model, ctx| { + handle_1.update(cx, |model, cx| { model.events.push("updated".into()); - ctx.emit(1); - ctx.notify(); - ctx.emit(2); + cx.emit(1); + cx.notify(); + cx.emit(2); }); - assert_eq!(handle_1.read(app).events, vec!["updated".to_string()]); + assert_eq!(handle_1.read(cx).events, vec!["updated".to_string()]); assert_eq!( - handle_2.read(app).events, + handle_2.read(cx).events, vec![ "observed event 1".to_string(), "notified".to_string(), @@ -2754,18 +2751,18 @@ mod tests { ] ); - handle_2.update(app, |model, _| { + handle_2.update(cx, |model, _| { drop(handle_1); model.other.take(); }); - assert_eq!(app.ctx.models.len(), 1); - assert!(app.subscriptions.is_empty()); - assert!(app.model_observations.is_empty()); + assert_eq!(cx.cx.models.len(), 1); + assert!(cx.subscriptions.is_empty()); + assert!(cx.model_observations.is_empty()); } #[crate::test(self)] - fn test_subscribe_and_emit_from_model(app: &mut MutableAppContext) { + fn test_subscribe_and_emit_from_model(cx: &mut MutableAppContext) { #[derive(Default)] struct Model { events: Vec, @@ -2775,11 +2772,11 @@ mod tests { type Event = usize; } - let handle_1 = app.add_model(|_| Model::default()); - let handle_2 = app.add_model(|_| Model::default()); + let handle_1 = cx.add_model(|_| Model::default()); + let handle_2 = cx.add_model(|_| Model::default()); let handle_2b = handle_2.clone(); - handle_1.update(app, |_, c| { + handle_1.update(cx, |_, c| { c.subscribe(&handle_2, move |model: &mut Model, event, c| { model.events.push(*event); @@ -2789,15 +2786,15 @@ mod tests { }); }); - handle_2.update(app, |_, c| c.emit(7)); - assert_eq!(handle_1.read(app).events, vec![7]); + handle_2.update(cx, |_, c| c.emit(7)); + assert_eq!(handle_1.read(cx).events, vec![7]); - handle_2.update(app, |_, c| c.emit(5)); - assert_eq!(handle_1.read(app).events, vec![7, 10, 5]); + handle_2.update(cx, |_, c| c.emit(5)); + assert_eq!(handle_1.read(cx).events, vec![7, 10, 5]); } #[crate::test(self)] - fn test_observe_and_notify_from_model(app: &mut MutableAppContext) { + fn test_observe_and_notify_from_model(cx: &mut MutableAppContext) { #[derive(Default)] struct Model { count: usize, @@ -2808,11 +2805,11 @@ mod tests { type Event = (); } - let handle_1 = app.add_model(|_| Model::default()); - let handle_2 = app.add_model(|_| Model::default()); + let handle_1 = cx.add_model(|_| Model::default()); + let handle_2 = cx.add_model(|_| Model::default()); let handle_2b = handle_2.clone(); - handle_1.update(app, |_, c| { + handle_1.update(cx, |_, c| { c.observe(&handle_2, move |model, observed, c| { model.events.push(observed.read(c).count); c.observe(&handle_2b, |model, observed, c| { @@ -2821,21 +2818,21 @@ mod tests { }); }); - handle_2.update(app, |model, c| { + handle_2.update(cx, |model, c| { model.count = 7; c.notify() }); - assert_eq!(handle_1.read(app).events, vec![7]); + assert_eq!(handle_1.read(cx).events, vec![7]); - handle_2.update(app, |model, c| { + handle_2.update(cx, |model, c| { model.count = 5; c.notify() }); - assert_eq!(handle_1.read(app).events, vec![7, 10, 5]) + assert_eq!(handle_1.read(cx).events, vec![7, 10, 5]) } #[crate::test(self)] - fn test_view_handles(app: &mut MutableAppContext) { + fn test_view_handles(cx: &mut MutableAppContext) { struct View { other: Option>, events: Vec, @@ -2856,9 +2853,9 @@ mod tests { } impl View { - fn new(other: Option>, ctx: &mut ViewContext) -> Self { + fn new(other: Option>, cx: &mut ViewContext) -> Self { if let Some(other) = other.as_ref() { - ctx.subscribe_to_view(other, |me, _, event, _| { + cx.subscribe_to_view(other, |me, _, event, _| { me.events.push(format!("observed event {}", event)); }); } @@ -2869,37 +2866,37 @@ mod tests { } } - let (window_id, _) = app.add_window(|ctx| View::new(None, ctx)); - let handle_1 = app.add_view(window_id, |ctx| View::new(None, ctx)); - let handle_2 = app.add_view(window_id, |ctx| View::new(Some(handle_1.clone()), ctx)); - assert_eq!(app.ctx.views.len(), 3); + let (window_id, _) = cx.add_window(|cx| View::new(None, cx)); + let handle_1 = cx.add_view(window_id, |cx| View::new(None, cx)); + let handle_2 = cx.add_view(window_id, |cx| View::new(Some(handle_1.clone()), cx)); + assert_eq!(cx.cx.views.len(), 3); - handle_1.update(app, |view, ctx| { + handle_1.update(cx, |view, cx| { view.events.push("updated".into()); - ctx.emit(1); - ctx.emit(2); + cx.emit(1); + cx.emit(2); }); - assert_eq!(handle_1.read(app).events, vec!["updated".to_string()]); + assert_eq!(handle_1.read(cx).events, vec!["updated".to_string()]); assert_eq!( - handle_2.read(app).events, + handle_2.read(cx).events, vec![ "observed event 1".to_string(), "observed event 2".to_string(), ] ); - handle_2.update(app, |view, _| { + handle_2.update(cx, |view, _| { drop(handle_1); view.other.take(); }); - assert_eq!(app.ctx.views.len(), 2); - assert!(app.subscriptions.is_empty()); - assert!(app.model_observations.is_empty()); + assert_eq!(cx.cx.views.len(), 2); + assert!(cx.subscriptions.is_empty()); + assert!(cx.model_observations.is_empty()); } #[crate::test(self)] - fn test_subscribe_and_emit_from_view(app: &mut MutableAppContext) { + fn test_subscribe_and_emit_from_view(cx: &mut MutableAppContext) { #[derive(Default)] struct View { events: Vec, @@ -2925,12 +2922,12 @@ mod tests { type Event = usize; } - let (window_id, handle_1) = app.add_window(|_| View::default()); - let handle_2 = app.add_view(window_id, |_| View::default()); + let (window_id, handle_1) = cx.add_window(|_| View::default()); + let handle_2 = cx.add_view(window_id, |_| View::default()); let handle_2b = handle_2.clone(); - let handle_3 = app.add_model(|_| Model); + let handle_3 = cx.add_model(|_| Model); - handle_1.update(app, |_, c| { + handle_1.update(cx, |_, c| { c.subscribe_to_view(&handle_2, move |me, _, event, c| { me.events.push(*event); @@ -2944,18 +2941,18 @@ mod tests { }) }); - handle_2.update(app, |_, c| c.emit(7)); - assert_eq!(handle_1.read(app).events, vec![7]); + handle_2.update(cx, |_, c| c.emit(7)); + assert_eq!(handle_1.read(cx).events, vec![7]); - handle_2.update(app, |_, c| c.emit(5)); - assert_eq!(handle_1.read(app).events, vec![7, 10, 5]); + handle_2.update(cx, |_, c| c.emit(5)); + assert_eq!(handle_1.read(cx).events, vec![7, 10, 5]); - handle_3.update(app, |_, c| c.emit(9)); - assert_eq!(handle_1.read(app).events, vec![7, 10, 5, 9]); + handle_3.update(cx, |_, c| c.emit(9)); + assert_eq!(handle_1.read(cx).events, vec![7, 10, 5, 9]); } #[crate::test(self)] - fn test_dropping_subscribers(app: &mut MutableAppContext) { + fn test_dropping_subscribers(cx: &mut MutableAppContext) { struct View; impl Entity for View { @@ -2978,31 +2975,31 @@ mod tests { type Event = (); } - let (window_id, _) = app.add_window(|_| View); - let observing_view = app.add_view(window_id, |_| View); - let emitting_view = app.add_view(window_id, |_| View); - let observing_model = app.add_model(|_| Model); - let observed_model = app.add_model(|_| Model); + let (window_id, _) = cx.add_window(|_| View); + let observing_view = cx.add_view(window_id, |_| View); + let emitting_view = cx.add_view(window_id, |_| View); + let observing_model = cx.add_model(|_| Model); + let observed_model = cx.add_model(|_| Model); - observing_view.update(app, |_, ctx| { - ctx.subscribe_to_view(&emitting_view, |_, _, _, _| {}); - ctx.subscribe_to_model(&observed_model, |_, _, _, _| {}); + observing_view.update(cx, |_, cx| { + cx.subscribe_to_view(&emitting_view, |_, _, _, _| {}); + cx.subscribe_to_model(&observed_model, |_, _, _, _| {}); }); - observing_model.update(app, |_, ctx| { - ctx.subscribe(&observed_model, |_, _, _| {}); + observing_model.update(cx, |_, cx| { + cx.subscribe(&observed_model, |_, _, _| {}); }); - app.update(|| { + cx.update(|| { drop(observing_view); drop(observing_model); }); - emitting_view.update(app, |_, ctx| ctx.emit(())); - observed_model.update(app, |_, ctx| ctx.emit(())); + emitting_view.update(cx, |_, cx| cx.emit(())); + observed_model.update(cx, |_, cx| cx.emit(())); } #[crate::test(self)] - fn test_observe_and_notify_from_view(app: &mut MutableAppContext) { + fn test_observe_and_notify_from_view(cx: &mut MutableAppContext) { #[derive(Default)] struct View { events: Vec, @@ -3031,24 +3028,24 @@ mod tests { type Event = (); } - let (_, view) = app.add_window(|_| View::default()); - let model = app.add_model(|_| Model::default()); + let (_, view) = cx.add_window(|_| View::default()); + let model = cx.add_model(|_| Model::default()); - view.update(app, |_, c| { + view.update(cx, |_, c| { c.observe_model(&model, |me, observed, c| { me.events.push(observed.read(c).count) }); }); - model.update(app, |model, c| { + model.update(cx, |model, c| { model.count = 11; c.notify(); }); - assert_eq!(view.read(app).events, vec![11]); + assert_eq!(view.read(cx).events, vec![11]); } #[crate::test(self)] - fn test_dropping_observers(app: &mut MutableAppContext) { + fn test_dropping_observers(cx: &mut MutableAppContext) { struct View; impl Entity for View { @@ -3071,28 +3068,28 @@ mod tests { type Event = (); } - let (window_id, _) = app.add_window(|_| View); - let observing_view = app.add_view(window_id, |_| View); - let observing_model = app.add_model(|_| Model); - let observed_model = app.add_model(|_| Model); + let (window_id, _) = cx.add_window(|_| View); + let observing_view = cx.add_view(window_id, |_| View); + let observing_model = cx.add_model(|_| Model); + let observed_model = cx.add_model(|_| Model); - observing_view.update(app, |_, ctx| { - ctx.observe_model(&observed_model, |_, _, _| {}); + observing_view.update(cx, |_, cx| { + cx.observe_model(&observed_model, |_, _, _| {}); }); - observing_model.update(app, |_, ctx| { - ctx.observe(&observed_model, |_, _, _| {}); + observing_model.update(cx, |_, cx| { + cx.observe(&observed_model, |_, _, _| {}); }); - app.update(|| { + cx.update(|| { drop(observing_view); drop(observing_model); }); - observed_model.update(app, |_, ctx| ctx.notify()); + observed_model.update(cx, |_, cx| cx.notify()); } #[crate::test(self)] - fn test_focus(app: &mut MutableAppContext) { + fn test_focus(cx: &mut MutableAppContext) { struct View { name: String, events: Arc>>, @@ -3121,19 +3118,19 @@ mod tests { } let events: Arc>> = Default::default(); - let (window_id, view_1) = app.add_window(|_| View { + let (window_id, view_1) = cx.add_window(|_| View { events: events.clone(), name: "view 1".to_string(), }); - let view_2 = app.add_view(window_id, |_| View { + let view_2 = cx.add_view(window_id, |_| View { events: events.clone(), name: "view 2".to_string(), }); - view_1.update(app, |_, ctx| ctx.focus(&view_2)); - view_1.update(app, |_, ctx| ctx.focus(&view_1)); - view_1.update(app, |_, ctx| ctx.focus(&view_2)); - view_1.update(app, |_, _| drop(view_2)); + view_1.update(cx, |_, cx| cx.focus(&view_2)); + view_1.update(cx, |_, cx| cx.focus(&view_1)); + view_1.update(cx, |_, cx| cx.focus(&view_2)); + view_1.update(cx, |_, _| drop(view_2)); assert_eq!( *events.lock(), @@ -3151,7 +3148,7 @@ mod tests { } #[crate::test(self)] - fn test_dispatch_action(app: &mut MutableAppContext) { + fn test_dispatch_action(cx: &mut MutableAppContext) { struct ViewA { id: usize, } @@ -3195,48 +3192,48 @@ mod tests { let actions = Rc::new(RefCell::new(Vec::new())); let actions_clone = actions.clone(); - app.add_global_action("action", move |_: &ActionArg, _: &mut MutableAppContext| { + cx.add_global_action("action", move |_: &ActionArg, _: &mut MutableAppContext| { actions_clone.borrow_mut().push("global a".to_string()); }); let actions_clone = actions.clone(); - app.add_global_action("action", move |_: &ActionArg, _: &mut MutableAppContext| { + cx.add_global_action("action", move |_: &ActionArg, _: &mut MutableAppContext| { actions_clone.borrow_mut().push("global b".to_string()); }); let actions_clone = actions.clone(); - app.add_action("action", move |view: &mut ViewA, arg: &ActionArg, ctx| { + cx.add_action("action", move |view: &mut ViewA, arg: &ActionArg, cx| { assert_eq!(arg.foo, "bar"); - ctx.propagate_action(); + cx.propagate_action(); actions_clone.borrow_mut().push(format!("{} a", view.id)); }); let actions_clone = actions.clone(); - app.add_action("action", move |view: &mut ViewA, _: &ActionArg, ctx| { + cx.add_action("action", move |view: &mut ViewA, _: &ActionArg, cx| { if view.id != 1 { - ctx.propagate_action(); + cx.propagate_action(); } actions_clone.borrow_mut().push(format!("{} b", view.id)); }); let actions_clone = actions.clone(); - app.add_action("action", move |view: &mut ViewB, _: &ActionArg, ctx| { - ctx.propagate_action(); + cx.add_action("action", move |view: &mut ViewB, _: &ActionArg, cx| { + cx.propagate_action(); actions_clone.borrow_mut().push(format!("{} c", view.id)); }); let actions_clone = actions.clone(); - app.add_action("action", move |view: &mut ViewB, _: &ActionArg, ctx| { - ctx.propagate_action(); + cx.add_action("action", move |view: &mut ViewB, _: &ActionArg, cx| { + cx.propagate_action(); actions_clone.borrow_mut().push(format!("{} d", view.id)); }); - let (window_id, view_1) = app.add_window(|_| ViewA { id: 1 }); - let view_2 = app.add_view(window_id, |_| ViewB { id: 2 }); - let view_3 = app.add_view(window_id, |_| ViewA { id: 3 }); - let view_4 = app.add_view(window_id, |_| ViewB { id: 4 }); + let (window_id, view_1) = cx.add_window(|_| ViewA { id: 1 }); + let view_2 = cx.add_view(window_id, |_| ViewB { id: 2 }); + let view_3 = cx.add_view(window_id, |_| ViewA { id: 3 }); + let view_4 = cx.add_view(window_id, |_| ViewB { id: 4 }); - app.dispatch_action( + cx.dispatch_action( window_id, vec![view_1.id(), view_2.id(), view_3.id(), view_4.id()], "action", @@ -3250,7 +3247,7 @@ mod tests { // Remove view_1, which doesn't propagate the action actions.borrow_mut().clear(); - app.dispatch_action( + cx.dispatch_action( window_id, vec![view_2.id(), view_3.id(), view_4.id()], "action", @@ -3264,7 +3261,7 @@ mod tests { } #[crate::test(self)] - fn test_dispatch_keystroke(app: &mut MutableAppContext) { + fn test_dispatch_keystroke(cx: &mut MutableAppContext) { use std::cell::Cell; #[derive(Clone)] @@ -3311,25 +3308,25 @@ mod tests { view_2.keymap_context.set.insert("b".into()); view_3.keymap_context.set.insert("c".into()); - let (window_id, view_1) = app.add_window(|_| view_1); - let view_2 = app.add_view(window_id, |_| view_2); - let view_3 = app.add_view(window_id, |_| view_3); + let (window_id, view_1) = cx.add_window(|_| view_1); + let view_2 = cx.add_view(window_id, |_| view_2); + let view_3 = cx.add_view(window_id, |_| view_3); // This keymap's only binding dispatches an action on view 2 because that view will have // "a" and "b" in its context, but not "c". let binding = keymap::Binding::new("a", "action", Some("a && b && !c")) .with_arg(ActionArg { key: "a".into() }); - app.add_bindings(vec![binding]); + cx.add_bindings(vec![binding]); let handled_action = Rc::new(Cell::new(false)); let handled_action_clone = handled_action.clone(); - app.add_action("action", move |view: &mut View, arg: &ActionArg, _ctx| { + cx.add_action("action", move |view: &mut View, arg: &ActionArg, _| { handled_action_clone.set(true); assert_eq!(view.id, 2); assert_eq!(arg.key, "a"); }); - app.dispatch_keystroke( + cx.dispatch_keystroke( window_id, vec![view_1.id(), view_2.id(), view_3.id()], &Keystroke::parse("a").unwrap(), @@ -3340,7 +3337,7 @@ mod tests { } #[crate::test(self)] - async fn test_model_condition(mut app: TestAppContext) { + async fn test_model_condition(mut cx: TestAppContext) { struct Counter(usize); impl super::Entity for Counter { @@ -3348,62 +3345,62 @@ mod tests { } impl Counter { - fn inc(&mut self, ctx: &mut ModelContext) { + fn inc(&mut self, cx: &mut ModelContext) { self.0 += 1; - ctx.notify(); + cx.notify(); } } - let model = app.add_model(|_| Counter(0)); + let model = cx.add_model(|_| Counter(0)); - let condition1 = model.condition(&app, |model, _| model.0 == 2); - let condition2 = model.condition(&app, |model, _| model.0 == 3); + let condition1 = model.condition(&cx, |model, _| model.0 == 2); + let condition2 = model.condition(&cx, |model, _| model.0 == 3); smol::pin!(condition1, condition2); - model.update(&mut app, |model, ctx| model.inc(ctx)); + model.update(&mut cx, |model, cx| model.inc(cx)); assert_eq!(poll_once(&mut condition1).await, None); assert_eq!(poll_once(&mut condition2).await, None); - model.update(&mut app, |model, ctx| model.inc(ctx)); + model.update(&mut cx, |model, cx| model.inc(cx)); assert_eq!(poll_once(&mut condition1).await, Some(())); assert_eq!(poll_once(&mut condition2).await, None); - model.update(&mut app, |model, ctx| model.inc(ctx)); + model.update(&mut cx, |model, cx| model.inc(cx)); assert_eq!(poll_once(&mut condition2).await, Some(())); - model.update(&mut app, |_, ctx| ctx.notify()); + model.update(&mut cx, |_, cx| cx.notify()); } #[crate::test(self)] #[should_panic] - async fn test_model_condition_timeout(mut app: TestAppContext) { + async fn test_model_condition_timeout(mut cx: TestAppContext) { struct Model; impl super::Entity for Model { type Event = (); } - let model = app.add_model(|_| Model); - model.condition(&app, |_, _| false).await; + let model = cx.add_model(|_| Model); + model.condition(&cx, |_, _| false).await; } #[crate::test(self)] #[should_panic(expected = "model dropped with pending condition")] - async fn test_model_condition_panic_on_drop(mut app: TestAppContext) { + async fn test_model_condition_panic_on_drop(mut cx: TestAppContext) { struct Model; impl super::Entity for Model { type Event = (); } - let model = app.add_model(|_| Model); - let condition = model.condition(&app, |_, _| false); - app.update(|_| drop(model)); + let model = cx.add_model(|_| Model); + let condition = model.condition(&cx, |_, _| false); + cx.update(|_| drop(model)); condition.await; } #[crate::test(self)] - async fn test_view_condition(mut app: TestAppContext) { + async fn test_view_condition(mut cx: TestAppContext) { struct Counter(usize); impl super::Entity for Counter { @@ -3421,34 +3418,34 @@ mod tests { } impl Counter { - fn inc(&mut self, ctx: &mut ViewContext) { + fn inc(&mut self, cx: &mut ViewContext) { self.0 += 1; - ctx.notify(); + cx.notify(); } } - let (_, view) = app.add_window(|_| Counter(0)); + let (_, view) = cx.add_window(|_| Counter(0)); - let condition1 = view.condition(&app, |view, _| view.0 == 2); - let condition2 = view.condition(&app, |view, _| view.0 == 3); + let condition1 = view.condition(&cx, |view, _| view.0 == 2); + let condition2 = view.condition(&cx, |view, _| view.0 == 3); smol::pin!(condition1, condition2); - view.update(&mut app, |view, ctx| view.inc(ctx)); + view.update(&mut cx, |view, cx| view.inc(cx)); assert_eq!(poll_once(&mut condition1).await, None); assert_eq!(poll_once(&mut condition2).await, None); - view.update(&mut app, |view, ctx| view.inc(ctx)); + view.update(&mut cx, |view, cx| view.inc(cx)); assert_eq!(poll_once(&mut condition1).await, Some(())); assert_eq!(poll_once(&mut condition2).await, None); - view.update(&mut app, |view, ctx| view.inc(ctx)); + view.update(&mut cx, |view, cx| view.inc(cx)); assert_eq!(poll_once(&mut condition2).await, Some(())); - view.update(&mut app, |_, ctx| ctx.notify()); + view.update(&mut cx, |_, cx| cx.notify()); } #[crate::test(self)] #[should_panic] - async fn test_view_condition_timeout(mut app: TestAppContext) { + async fn test_view_condition_timeout(mut cx: TestAppContext) { struct View; impl super::Entity for View { @@ -3465,13 +3462,13 @@ mod tests { } } - let (_, view) = app.add_window(|_| View); - view.condition(&app, |_, _| false).await; + let (_, view) = cx.add_window(|_| View); + view.condition(&cx, |_, _| false).await; } #[crate::test(self)] #[should_panic(expected = "view dropped with pending condition")] - async fn test_view_condition_panic_on_drop(mut app: TestAppContext) { + async fn test_view_condition_panic_on_drop(mut cx: TestAppContext) { struct View; impl super::Entity for View { @@ -3488,92 +3485,11 @@ mod tests { } } - let window_id = app.add_window(|_| View).0; - let view = app.add_view(window_id, |_| View); + let window_id = cx.add_window(|_| View).0; + let view = cx.add_view(window_id, |_| View); - let condition = view.condition(&app, |_, _| false); - app.update(|_| drop(view)); + let condition = view.condition(&cx, |_, _| false); + cx.update(|_| drop(view)); condition.await; } - - // #[crate::test(self)] - // fn test_ui_and_window_updates() { - // struct View { - // count: usize, - // } - - // impl Entity for View { - // type Event = (); - // } - - // impl super::View for View { - // fn render<'a>(&self, _: &AppContext) -> ElementBox { - // Empty::new().boxed() - // } - - // fn ui_name() -> &'static str { - // "View" - // } - // } - - // App::test(|app| async move { - // let (window_id, _) = app.add_window(|_| View { count: 3 }); - // let view_1 = app.add_view(window_id, |_| View { count: 1 }); - // let view_2 = app.add_view(window_id, |_| View { count: 2 }); - - // // Ensure that registering for UI updates after mutating the app still gives us all the - // // updates. - // let ui_updates = Rc::new(RefCell::new(Vec::new())); - // let ui_updates_ = ui_updates.clone(); - // app.on_ui_update(move |update, _| ui_updates_.borrow_mut().push(update)); - - // assert_eq!( - // ui_updates.borrow_mut().drain(..).collect::>(), - // vec![UiUpdate::OpenWindow { - // window_id, - // width: 1024.0, - // height: 768.0, - // }] - // ); - - // let window_invalidations = Rc::new(RefCell::new(Vec::new())); - // let window_invalidations_ = window_invalidations.clone(); - // app.on_window_invalidated(window_id, move |update, _| { - // window_invalidations_.borrow_mut().push(update) - // }); - - // let view_2_id = view_2.id(); - // view_1.update(app, |view, ctx| { - // view.count = 7; - // ctx.notify(); - // drop(view_2); - // }); - - // let invalidation = window_invalidations.borrow_mut().drain(..).next().unwrap(); - // assert_eq!(invalidation.updated.len(), 1); - // assert!(invalidation.updated.contains(&view_1.id())); - // assert_eq!(invalidation.removed, vec![view_2_id]); - - // let view_3 = view_1.update(app, |_, ctx| ctx.add_view(|_| View { count: 8 })); - - // let invalidation = window_invalidations.borrow_mut().drain(..).next().unwrap(); - // assert_eq!(invalidation.updated.len(), 1); - // assert!(invalidation.updated.contains(&view_3.id())); - // assert!(invalidation.removed.is_empty()); - - // view_3 - // .update(app, |_, ctx| { - // ctx.spawn_local(async { 9 }, |me, output, ctx| { - // me.count = output; - // ctx.notify(); - // }) - // }) - // .await; - - // let invalidation = window_invalidations.borrow_mut().drain(..).next().unwrap(); - // assert_eq!(invalidation.updated.len(), 1); - // assert!(invalidation.updated.contains(&view_3.id())); - // assert!(invalidation.removed.is_empty()); - // }); - // } } diff --git a/gpui/src/elements.rs b/gpui/src/elements.rs index f78a3f999d34e6af9b056d934f5d1bbc6a25d028..a37ee7fea5801e9864ec75194f1d7dbf3359d45a 100644 --- a/gpui/src/elements.rs +++ b/gpui/src/elements.rs @@ -38,11 +38,11 @@ use replace_with::replace_with_or_abort; use std::{any::Any, borrow::Cow}; trait AnyElement { - fn layout(&mut self, constraint: SizeConstraint, ctx: &mut LayoutContext) -> Vector2F; + fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F; fn after_layout(&mut self, _: &mut AfterLayoutContext) {} - fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext); - fn dispatch_event(&mut self, event: &Event, ctx: &mut EventContext) -> bool; - fn debug(&self, ctx: &DebugContext) -> serde_json::Value; + fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext); + fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool; + fn debug(&self, cx: &DebugContext) -> serde_json::Value; fn size(&self) -> Vector2F; fn metadata(&self) -> Option<&dyn Any>; @@ -55,21 +55,21 @@ pub trait Element { fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState); fn after_layout( &mut self, size: Vector2F, layout: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ); fn paint( &mut self, bounds: RectF, layout: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState; fn dispatch_event( @@ -78,7 +78,7 @@ pub trait Element { bounds: RectF, layout: &mut Self::LayoutState, paint: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool; fn metadata(&self) -> Option<&dyn Any> { @@ -90,7 +90,7 @@ pub trait Element { bounds: RectF, layout: &Self::LayoutState, paint: &Self::PaintState, - ctx: &DebugContext, + cx: &DebugContext, ) -> serde_json::Value; fn boxed(self) -> ElementBox @@ -138,13 +138,13 @@ pub struct ElementBox { } impl AnyElement for Lifecycle { - fn layout(&mut self, constraint: SizeConstraint, ctx: &mut LayoutContext) -> Vector2F { + fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F { let mut result = None; replace_with_or_abort(self, |me| match me { Lifecycle::Init { mut element } | Lifecycle::PostLayout { mut element, .. } | Lifecycle::PostPaint { mut element, .. } => { - let (size, layout) = element.layout(constraint, ctx); + let (size, layout) = element.layout(constraint, cx); debug_assert!(size.x().is_finite()); debug_assert!(size.y().is_finite()); @@ -160,7 +160,7 @@ impl AnyElement for Lifecycle { result.unwrap() } - fn after_layout(&mut self, ctx: &mut AfterLayoutContext) { + fn after_layout(&mut self, cx: &mut AfterLayoutContext) { if let Lifecycle::PostLayout { element, size, @@ -168,13 +168,13 @@ impl AnyElement for Lifecycle { .. } = self { - element.after_layout(*size, layout, ctx); + element.after_layout(*size, layout, cx); } else { panic!("invalid element lifecycle state"); } } - fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext) { + fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) { replace_with_or_abort(self, |me| { if let Lifecycle::PostLayout { mut element, @@ -184,7 +184,7 @@ impl AnyElement for Lifecycle { } = me { let bounds = RectF::new(origin, size); - let paint = element.paint(bounds, &mut layout, ctx); + let paint = element.paint(bounds, &mut layout, cx); Lifecycle::PostPaint { element, constraint, @@ -198,7 +198,7 @@ impl AnyElement for Lifecycle { }); } - fn dispatch_event(&mut self, event: &Event, ctx: &mut EventContext) -> bool { + fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool { if let Lifecycle::PostPaint { element, bounds, @@ -207,7 +207,7 @@ impl AnyElement for Lifecycle { .. } = self { - element.dispatch_event(event, *bounds, layout, paint, ctx) + element.dispatch_event(event, *bounds, layout, paint, cx) } else { panic!("invalid element lifecycle state"); } @@ -229,7 +229,7 @@ impl AnyElement for Lifecycle { } } - fn debug(&self, ctx: &DebugContext) -> serde_json::Value { + fn debug(&self, cx: &DebugContext) -> serde_json::Value { match self { Lifecycle::PostPaint { element, @@ -238,7 +238,7 @@ impl AnyElement for Lifecycle { layout, paint, } => { - let mut value = element.debug(*bounds, layout, paint, ctx); + let mut value = element.debug(*bounds, layout, paint, cx); if let json::Value::Object(map) = &mut value { let mut new_map: crate::json::Map = Default::default(); @@ -258,20 +258,20 @@ impl AnyElement for Lifecycle { } impl ElementBox { - pub fn layout(&mut self, constraint: SizeConstraint, ctx: &mut LayoutContext) -> Vector2F { - self.element.layout(constraint, ctx) + pub fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F { + self.element.layout(constraint, cx) } - pub fn after_layout(&mut self, ctx: &mut AfterLayoutContext) { - self.element.after_layout(ctx); + pub fn after_layout(&mut self, cx: &mut AfterLayoutContext) { + self.element.after_layout(cx); } - pub fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext) { - self.element.paint(origin, ctx); + pub fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) { + self.element.paint(origin, cx); } - pub fn dispatch_event(&mut self, event: &Event, ctx: &mut EventContext) -> bool { - self.element.dispatch_event(event, ctx) + pub fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool { + self.element.dispatch_event(event, cx) } pub fn size(&self) -> Vector2F { @@ -282,8 +282,8 @@ impl ElementBox { self.element.metadata() } - pub fn debug(&self, ctx: &DebugContext) -> json::Value { - let mut value = self.element.debug(ctx); + pub fn debug(&self, cx: &DebugContext) -> json::Value { + let mut value = self.element.debug(cx); if let Some(name) = &self.name { if let json::Value::Object(map) = &mut value { diff --git a/gpui/src/elements/align.rs b/gpui/src/elements/align.rs index 8bb59169e2ea42ba0634bb9daf53d670b9e18de8..5b3fd5d0b51f62c7ca36ab85afe2900d783efb60 100644 --- a/gpui/src/elements/align.rs +++ b/gpui/src/elements/align.rs @@ -37,11 +37,11 @@ impl Element for Align { fn layout( &mut self, mut constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { let mut size = constraint.max; constraint.min = Vector2F::zero(); - let child_size = self.child.layout(constraint, ctx); + let child_size = self.child.layout(constraint, cx); if size.x().is_infinite() { size.set_x(child_size.x()); } @@ -55,16 +55,16 @@ impl Element for Align { &mut self, _: Vector2F, _: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ) { - self.child.after_layout(ctx); + self.child.after_layout(cx); } fn paint( &mut self, bounds: pathfinder_geometry::rect::RectF, _: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { let my_center = bounds.size() / 2.; let my_target = my_center + my_center * self.alignment; @@ -73,7 +73,7 @@ impl Element for Align { let child_target = child_center + child_center * self.alignment; self.child - .paint(bounds.origin() - (child_target - my_target), ctx); + .paint(bounds.origin() - (child_target - my_target), cx); } fn dispatch_event( @@ -82,9 +82,9 @@ impl Element for Align { _: pathfinder_geometry::rect::RectF, _: &mut Self::LayoutState, _: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { - self.child.dispatch_event(event, ctx) + self.child.dispatch_event(event, cx) } fn debug( @@ -92,13 +92,13 @@ impl Element for Align { bounds: pathfinder_geometry::rect::RectF, _: &Self::LayoutState, _: &Self::PaintState, - ctx: &DebugContext, + cx: &DebugContext, ) -> json::Value { json!({ "type": "Align", "bounds": bounds.to_json(), "alignment": self.alignment.to_json(), - "child": self.child.debug(ctx), + "child": self.child.debug(cx), }) } } diff --git a/gpui/src/elements/canvas.rs b/gpui/src/elements/canvas.rs index a82ef50c900c1270e631ef89793b88f41f371e1d..e90c377be13c1285e5ff508e042089b6984efb46 100644 --- a/gpui/src/elements/canvas.rs +++ b/gpui/src/elements/canvas.rs @@ -51,9 +51,9 @@ where &mut self, bounds: RectF, _: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { - self.0(bounds, ctx) + self.0(bounds, cx) } fn after_layout( diff --git a/gpui/src/elements/constrained_box.rs b/gpui/src/elements/constrained_box.rs index a705be3612a5519d9deeba2b26f271b479f50810..3d50b70a57fb8a74c25a8b68f001acb7aa7ddf55 100644 --- a/gpui/src/elements/constrained_box.rs +++ b/gpui/src/elements/constrained_box.rs @@ -58,12 +58,12 @@ impl Element for ConstrainedBox { fn layout( &mut self, mut constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { constraint.min = constraint.min.max(self.constraint.min); constraint.max = constraint.max.min(self.constraint.max); constraint.max = constraint.max.max(constraint.min); - let size = self.child.layout(constraint, ctx); + let size = self.child.layout(constraint, cx); (size, ()) } @@ -71,18 +71,18 @@ impl Element for ConstrainedBox { &mut self, _: Vector2F, _: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ) { - self.child.after_layout(ctx); + self.child.after_layout(cx); } fn paint( &mut self, bounds: RectF, _: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { - self.child.paint(bounds.origin(), ctx); + self.child.paint(bounds.origin(), cx); } fn dispatch_event( @@ -91,9 +91,9 @@ impl Element for ConstrainedBox { _: RectF, _: &mut Self::LayoutState, _: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { - self.child.dispatch_event(event, ctx) + self.child.dispatch_event(event, cx) } fn debug( @@ -101,8 +101,8 @@ impl Element for ConstrainedBox { _: RectF, _: &Self::LayoutState, _: &Self::PaintState, - ctx: &DebugContext, + cx: &DebugContext, ) -> json::Value { - json!({"type": "ConstrainedBox", "set_constraint": self.constraint.to_json(), "child": self.child.debug(ctx)}) + json!({"type": "ConstrainedBox", "set_constraint": self.constraint.to_json(), "child": self.child.debug(cx)}) } } diff --git a/gpui/src/elements/container.rs b/gpui/src/elements/container.rs index d228c54d077009e0f578939299ace7f8014842bb..c0b829fbe6f881b0cf24e8c80d2bf92a16cabcb2 100644 --- a/gpui/src/elements/container.rs +++ b/gpui/src/elements/container.rs @@ -141,14 +141,14 @@ impl Element for Container { fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { let size_buffer = self.margin_size() + self.padding_size() + self.border_size(); let child_constraint = SizeConstraint { min: (constraint.min - size_buffer).max(Vector2F::zero()), max: (constraint.max - size_buffer).max(Vector2F::zero()), }; - let child_size = self.child.layout(child_constraint, ctx); + let child_size = self.child.layout(child_constraint, cx); (child_size + size_buffer, ()) } @@ -156,16 +156,16 @@ impl Element for Container { &mut self, _: Vector2F, _: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ) { - self.child.after_layout(ctx); + self.child.after_layout(cx); } fn paint( &mut self, bounds: RectF, _: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { let quad_bounds = RectF::from_points( bounds.origin() + vec2f(self.margin.left, self.margin.top), @@ -173,14 +173,14 @@ impl Element for Container { ); if let Some(shadow) = self.shadow.as_ref() { - ctx.scene.push_shadow(scene::Shadow { + cx.scene.push_shadow(scene::Shadow { bounds: quad_bounds + shadow.offset, corner_radius: self.corner_radius, sigma: shadow.blur, color: shadow.color, }); } - ctx.scene.push_quad(Quad { + cx.scene.push_quad(Quad { bounds: quad_bounds, background: self.background_color, border: self.border, @@ -190,7 +190,7 @@ impl Element for Container { let child_origin = quad_bounds.origin() + vec2f(self.padding.left, self.padding.top) + vec2f(self.border.left_width(), self.border.top_width()); - self.child.paint(child_origin, ctx); + self.child.paint(child_origin, cx); } fn dispatch_event( @@ -199,9 +199,9 @@ impl Element for Container { _: RectF, _: &mut Self::LayoutState, _: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { - self.child.dispatch_event(event, ctx) + self.child.dispatch_event(event, cx) } fn debug( @@ -209,7 +209,7 @@ impl Element for Container { bounds: RectF, _: &Self::LayoutState, _: &Self::PaintState, - ctx: &crate::DebugContext, + cx: &crate::DebugContext, ) -> serde_json::Value { json!({ "type": "Container", @@ -222,7 +222,7 @@ impl Element for Container { "corner_radius": self.corner_radius, "shadow": self.shadow.to_json(), }, - "child": self.child.debug(ctx), + "child": self.child.debug(cx), }) } } diff --git a/gpui/src/elements/event_handler.rs b/gpui/src/elements/event_handler.rs index 717d7db1f673afd6cf7b5f979bf91f46c477bba7..a66778f8b7c91d979ee13eb4fd1de804702f4eb0 100644 --- a/gpui/src/elements/event_handler.rs +++ b/gpui/src/elements/event_handler.rs @@ -35,9 +35,9 @@ impl Element for EventHandler { fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { - let size = self.child.layout(constraint, ctx); + let size = self.child.layout(constraint, cx); (size, ()) } @@ -45,18 +45,18 @@ impl Element for EventHandler { &mut self, _: Vector2F, _: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ) { - self.child.after_layout(ctx); + self.child.after_layout(cx); } fn paint( &mut self, bounds: RectF, _: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { - self.child.paint(bounds.origin(), ctx); + self.child.paint(bounds.origin(), cx); } fn dispatch_event( @@ -65,16 +65,16 @@ impl Element for EventHandler { bounds: RectF, _: &mut Self::LayoutState, _: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { - if self.child.dispatch_event(event, ctx) { + if self.child.dispatch_event(event, cx) { true } else { match event { Event::LeftMouseDown { position, .. } => { if let Some(callback) = self.mouse_down.as_mut() { if bounds.contains_point(*position) { - return callback(ctx); + return callback(cx); } } false @@ -89,11 +89,11 @@ impl Element for EventHandler { _: RectF, _: &Self::LayoutState, _: &Self::PaintState, - ctx: &DebugContext, + cx: &DebugContext, ) -> serde_json::Value { json!({ "type": "EventHandler", - "child": self.child.debug(ctx), + "child": self.child.debug(cx), }) } } diff --git a/gpui/src/elements/flex.rs b/gpui/src/elements/flex.rs index cbce89ed2fdadb9df3eeeb8e309632d264b42a65..1ed9204de4796fc45056acaf16f550de7a181287 100644 --- a/gpui/src/elements/flex.rs +++ b/gpui/src/elements/flex.rs @@ -53,7 +53,7 @@ impl Element for Flex { fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { let mut total_flex = 0.0; let mut fixed_space = 0.0; @@ -74,7 +74,7 @@ impl Element for Flex { vec2f(constraint.max.x(), INFINITY), ), }; - let size = child.layout(child_constraint, ctx); + let size = child.layout(child_constraint, cx); fixed_space += size.along(self.axis); cross_axis_max = cross_axis_max.max(size.along(cross_axis)); } @@ -105,7 +105,7 @@ impl Element for Flex { vec2f(constraint.max.x(), child_max), ), }; - let child_size = child.layout(child_constraint, ctx); + let child_size = child.layout(child_constraint, cx); remaining_space -= child_size.along(self.axis); remaining_flex -= flex; cross_axis_max = cross_axis_max.max(child_size.along(cross_axis)); @@ -138,10 +138,10 @@ impl Element for Flex { &mut self, _: Vector2F, _: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ) { for child in &mut self.children { - child.after_layout(ctx); + child.after_layout(cx); } } @@ -149,11 +149,11 @@ impl Element for Flex { &mut self, bounds: RectF, _: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { let mut child_origin = bounds.origin(); for child in &mut self.children { - child.paint(child_origin, ctx); + child.paint(child_origin, cx); match self.axis { Axis::Horizontal => child_origin += vec2f(child.size().x(), 0.0), Axis::Vertical => child_origin += vec2f(0.0, child.size().y()), @@ -167,11 +167,11 @@ impl Element for Flex { _: RectF, _: &mut Self::LayoutState, _: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { let mut handled = false; for child in &mut self.children { - handled = child.dispatch_event(event, ctx) || handled; + handled = child.dispatch_event(event, cx) || handled; } handled } @@ -181,13 +181,13 @@ impl Element for Flex { bounds: RectF, _: &Self::LayoutState, _: &Self::PaintState, - ctx: &DebugContext, + cx: &DebugContext, ) -> json::Value { json!({ "type": "Flex", "bounds": bounds.to_json(), "axis": self.axis.to_json(), - "children": self.children.iter().map(|child| child.debug(ctx)).collect::>() + "children": self.children.iter().map(|child| child.debug(cx)).collect::>() }) } } @@ -217,9 +217,9 @@ impl Element for Expanded { fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { - let size = self.child.layout(constraint, ctx); + let size = self.child.layout(constraint, cx); (size, ()) } @@ -227,18 +227,18 @@ impl Element for Expanded { &mut self, _: Vector2F, _: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ) { - self.child.after_layout(ctx); + self.child.after_layout(cx); } fn paint( &mut self, bounds: RectF, _: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { - self.child.paint(bounds.origin(), ctx) + self.child.paint(bounds.origin(), cx) } fn dispatch_event( @@ -247,9 +247,9 @@ impl Element for Expanded { _: RectF, _: &mut Self::LayoutState, _: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { - self.child.dispatch_event(event, ctx) + self.child.dispatch_event(event, cx) } fn metadata(&self) -> Option<&dyn Any> { @@ -261,12 +261,12 @@ impl Element for Expanded { _: RectF, _: &Self::LayoutState, _: &Self::PaintState, - ctx: &DebugContext, + cx: &DebugContext, ) -> Value { json!({ "type": "Expanded", "flex": self.metadata.flex, - "child": self.child.debug(ctx) + "child": self.child.debug(cx) }) } } diff --git a/gpui/src/elements/label.rs b/gpui/src/elements/label.rs index 02d89200dda6ac2c9e78e3db2f2679de187bc954..9a473606913b4f796698e508f8c343b970430943 100644 --- a/gpui/src/elements/label.rs +++ b/gpui/src/elements/label.rs @@ -109,20 +109,20 @@ impl Element for Label { fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { - let font_id = ctx + let font_id = cx .font_cache .select_font(self.family_id, &self.font_properties) .unwrap(); - let runs = self.compute_runs(&ctx.font_cache, font_id); + let runs = self.compute_runs(&cx.font_cache, font_id); let line = - ctx.text_layout_cache + cx.text_layout_cache .layout_str(self.text.as_str(), self.font_size, runs.as_slice()); let size = vec2f( line.width().max(constraint.min.x()).min(constraint.max.x()), - ctx.font_cache.line_height(font_id, self.font_size).ceil(), + cx.font_cache.line_height(font_id, self.font_size).ceil(), ); (size, line) @@ -135,12 +135,12 @@ impl Element for Label { &mut self, bounds: RectF, line: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { line.paint( bounds.origin(), RectF::new(vec2f(0., 0.), bounds.size()), - ctx, + cx, ) } @@ -160,12 +160,12 @@ impl Element for Label { bounds: RectF, _: &Self::LayoutState, _: &Self::PaintState, - ctx: &DebugContext, + cx: &DebugContext, ) -> Value { json!({ "type": "Label", "bounds": bounds.to_json(), - "font_family": ctx.font_cache.family_name(self.family_id).unwrap(), + "font_family": cx.font_cache.family_name(self.family_id).unwrap(), "font_size": self.font_size, "font_properties": self.font_properties.to_json(), "text": &self.text, @@ -191,13 +191,13 @@ mod tests { use super::*; #[crate::test(self)] - fn test_layout_label_with_highlights(app: &mut crate::MutableAppContext) { - let menlo = app.font_cache().load_family(&["Menlo"]).unwrap(); - let menlo_regular = app + fn test_layout_label_with_highlights(cx: &mut crate::MutableAppContext) { + let menlo = cx.font_cache().load_family(&["Menlo"]).unwrap(); + let menlo_regular = cx .font_cache() .select_font(menlo, &Properties::new()) .unwrap(); - let menlo_bold = app + let menlo_bold = cx .font_cache() .select_font(menlo, Properties::new().weight(Weight::BOLD)) .unwrap(); @@ -216,7 +216,7 @@ mod tests { ], ); - let runs = label.compute_runs(app.font_cache().as_ref(), menlo_regular); + let runs = label.compute_runs(cx.font_cache().as_ref(), menlo_regular); assert_eq!( runs.as_slice(), &[ diff --git a/gpui/src/elements/line_box.rs b/gpui/src/elements/line_box.rs index 83f58feea59e679b63bec5e8b16be56c5559a059..16baf6e00ba5d483891a41088248bf32fd4b1270 100644 --- a/gpui/src/elements/line_box.rs +++ b/gpui/src/elements/line_box.rs @@ -35,20 +35,20 @@ impl Element for LineBox { fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { - match ctx + match cx .font_cache .select_font(self.family_id, &self.font_properties) { Ok(font_id) => { - let line_height = ctx.font_cache.line_height(font_id, self.font_size); - let character_height = ctx.font_cache.ascent(font_id, self.font_size) - + ctx.font_cache.descent(font_id, self.font_size); + let line_height = cx.font_cache.line_height(font_id, self.font_size); + let character_height = cx.font_cache.ascent(font_id, self.font_size) + + cx.font_cache.descent(font_id, self.font_size); let child_max = vec2f(constraint.max.x(), character_height); let child_size = self.child.layout( SizeConstraint::new(constraint.min.min(child_max), child_max), - ctx, + cx, ); let size = vec2f(child_size.x(), line_height); (size, (line_height - character_height) / 2.) @@ -64,19 +64,19 @@ impl Element for LineBox { &mut self, _: Vector2F, _: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ) { - self.child.after_layout(ctx); + self.child.after_layout(cx); } fn paint( &mut self, bounds: pathfinder_geometry::rect::RectF, padding_top: &mut f32, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { self.child - .paint(bounds.origin() + vec2f(0., *padding_top), ctx); + .paint(bounds.origin() + vec2f(0., *padding_top), cx); } fn dispatch_event( @@ -85,9 +85,9 @@ impl Element for LineBox { _: pathfinder_geometry::rect::RectF, _: &mut Self::LayoutState, _: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { - self.child.dispatch_event(event, ctx) + self.child.dispatch_event(event, cx) } fn debug( @@ -95,14 +95,14 @@ impl Element for LineBox { bounds: RectF, _: &Self::LayoutState, _: &Self::PaintState, - ctx: &DebugContext, + cx: &DebugContext, ) -> serde_json::Value { json!({ "bounds": bounds.to_json(), - "font_family": ctx.font_cache.family_name(self.family_id).unwrap(), + "font_family": cx.font_cache.family_name(self.family_id).unwrap(), "font_size": self.font_size, "font_properties": self.font_properties.to_json(), - "child": self.child.debug(ctx), + "child": self.child.debug(cx), }) } } diff --git a/gpui/src/elements/mouse_event_handler.rs b/gpui/src/elements/mouse_event_handler.rs index aed9779f81a21b2e7794ba93ac51612f0ade865c..98de1f6a0f4d0a0b47f8876452c7d2a067967ced 100644 --- a/gpui/src/elements/mouse_event_handler.rs +++ b/gpui/src/elements/mouse_event_handler.rs @@ -18,13 +18,13 @@ pub struct MouseState { } impl MouseEventHandler { - pub fn new(id: usize, ctx: &AppContext, render_child: F) -> Self + pub fn new(id: usize, cx: &AppContext, render_child: F) -> Self where Tag: 'static, F: FnOnce(MouseState) -> ElementBox, { - let state_handle = ctx.value::(id); - let state = state_handle.read(ctx, |state| *state); + let state_handle = cx.value::(id); + let state = state_handle.read(cx, |state| *state); let child = render_child(state); Self { state: state_handle, @@ -46,27 +46,27 @@ impl Element for MouseEventHandler { fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { - (self.child.layout(constraint, ctx), ()) + (self.child.layout(constraint, cx), ()) } fn after_layout( &mut self, _: Vector2F, _: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ) { - self.child.after_layout(ctx); + self.child.after_layout(cx); } fn paint( &mut self, bounds: RectF, _: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { - self.child.paint(bounds.origin(), ctx); + self.child.paint(bounds.origin(), cx); } fn dispatch_event( @@ -75,18 +75,18 @@ impl Element for MouseEventHandler { bounds: RectF, _: &mut Self::LayoutState, _: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { let click_handler = self.click_handler.as_mut(); - let handled_in_child = self.child.dispatch_event(event, ctx); + let handled_in_child = self.child.dispatch_event(event, cx); - self.state.update(ctx.app, |state| match event { + self.state.update(cx.app, |state| match event { Event::MouseMoved { position } => { let mouse_in = bounds.contains_point(*position); if state.hovered != mouse_in { state.hovered = mouse_in; - ctx.notify(); + cx.notify(); true } else { handled_in_child @@ -95,7 +95,7 @@ impl Element for MouseEventHandler { Event::LeftMouseDown { position, .. } => { if !handled_in_child && bounds.contains_point(*position) { state.clicked = true; - ctx.notify(); + cx.notify(); true } else { handled_in_child @@ -104,10 +104,10 @@ impl Element for MouseEventHandler { Event::LeftMouseUp { position, .. } => { if !handled_in_child && state.clicked { state.clicked = false; - ctx.notify(); + cx.notify(); if let Some(handler) = click_handler { if bounds.contains_point(*position) { - handler(ctx); + handler(cx); } } true @@ -124,11 +124,11 @@ impl Element for MouseEventHandler { _: RectF, _: &Self::LayoutState, _: &Self::PaintState, - ctx: &DebugContext, + cx: &DebugContext, ) -> serde_json::Value { json!({ "type": "MouseEventHandler", - "child": self.child.debug(ctx), + "child": self.child.debug(cx), }) } } diff --git a/gpui/src/elements/stack.rs b/gpui/src/elements/stack.rs index 6fdae680108b8fc7bcc6d5d9d7ba9babbad2c40f..cfc4d9cc6cfc486a55d26f9c4df333bfe47b346b 100644 --- a/gpui/src/elements/stack.rs +++ b/gpui/src/elements/stack.rs @@ -24,11 +24,11 @@ impl Element for Stack { fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { let mut size = constraint.min; for child in &mut self.children { - size = size.max(child.layout(constraint, ctx)); + size = size.max(child.layout(constraint, cx)); } (size, ()) } @@ -37,10 +37,10 @@ impl Element for Stack { &mut self, _: Vector2F, _: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ) { for child in &mut self.children { - child.after_layout(ctx); + child.after_layout(cx); } } @@ -48,12 +48,12 @@ impl Element for Stack { &mut self, bounds: RectF, _: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { for child in &mut self.children { - ctx.scene.push_layer(None); - child.paint(bounds.origin(), ctx); - ctx.scene.pop_layer(); + cx.scene.push_layer(None); + child.paint(bounds.origin(), cx); + cx.scene.pop_layer(); } } @@ -63,10 +63,10 @@ impl Element for Stack { _: RectF, _: &mut Self::LayoutState, _: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { for child in self.children.iter_mut().rev() { - if child.dispatch_event(event, ctx) { + if child.dispatch_event(event, cx) { return true; } } @@ -78,12 +78,12 @@ impl Element for Stack { bounds: RectF, _: &Self::LayoutState, _: &Self::PaintState, - ctx: &DebugContext, + cx: &DebugContext, ) -> json::Value { json!({ "type": "Stack", "bounds": bounds.to_json(), - "children": self.children.iter().map(|child| child.debug(ctx)).collect::>() + "children": self.children.iter().map(|child| child.debug(cx)).collect::>() }) } } diff --git a/gpui/src/elements/svg.rs b/gpui/src/elements/svg.rs index b52112f65c664234e81db93bdce647f8291d5769..855d30b1a3b619f902b06a354100dcebaac5276e 100644 --- a/gpui/src/elements/svg.rs +++ b/gpui/src/elements/svg.rs @@ -38,9 +38,9 @@ impl Element for Svg { fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { - match ctx.asset_cache.svg(&self.path) { + match cx.asset_cache.svg(&self.path) { Ok(tree) => { let size = if constraint.max.x().is_infinite() && constraint.max.y().is_infinite() { let rect = from_usvg_rect(tree.svg_node().view_box.rect); @@ -69,9 +69,9 @@ impl Element for Svg { fn after_layout(&mut self, _: Vector2F, _: &mut Self::LayoutState, _: &mut AfterLayoutContext) { } - fn paint(&mut self, bounds: RectF, svg: &mut Self::LayoutState, ctx: &mut PaintContext) { + fn paint(&mut self, bounds: RectF, svg: &mut Self::LayoutState, cx: &mut PaintContext) { if let Some(svg) = svg.clone() { - ctx.scene.push_icon(scene::Icon { + cx.scene.push_icon(scene::Icon { bounds, svg, path: self.path.clone(), diff --git a/gpui/src/elements/uniform_list.rs b/gpui/src/elements/uniform_list.rs index 0b1d51d9aca28a31732ebbd056945f12a5785fe7..69cc55217bbcc83896257614e4b20e1650909e0a 100644 --- a/gpui/src/elements/uniform_list.rs +++ b/gpui/src/elements/uniform_list.rs @@ -72,7 +72,7 @@ where delta: Vector2F, precise: bool, scroll_max: f32, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { if !precise { todo!("still need to handle non-precise scroll events from a mouse wheel"); @@ -80,7 +80,7 @@ where let mut state = self.state.0.lock(); state.scroll_top = (state.scroll_top - delta.y()).max(0.0).min(scroll_max); - ctx.dispatch_action("uniform_list:scroll", state.scroll_top); + cx.dispatch_action("uniform_list:scroll", state.scroll_top); true } @@ -119,7 +119,7 @@ where fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { if constraint.max.y().is_infinite() { unimplemented!( @@ -133,9 +133,9 @@ where let mut scroll_max = 0.; let mut items = Vec::new(); - (self.append_items)(0..1, &mut items, ctx.app); + (self.append_items)(0..1, &mut items, cx.app); if let Some(first_item) = items.first_mut() { - let mut item_size = first_item.layout(item_constraint, ctx); + let mut item_size = first_item.layout(item_constraint, cx); item_size.set_x(size.x()); item_constraint.min = item_size; item_constraint.max = item_size; @@ -155,9 +155,9 @@ where self.item_count, start + (size.y() / item_height).ceil() as usize + 1, ); - (self.append_items)(start..end, &mut items, ctx.app); + (self.append_items)(start..end, &mut items, cx.app); for item in &mut items { - item.layout(item_constraint, ctx); + item.layout(item_constraint, cx); } } @@ -175,10 +175,10 @@ where &mut self, _: Vector2F, layout: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ) { for item in &mut layout.items { - item.after_layout(ctx); + item.after_layout(cx); } } @@ -186,19 +186,19 @@ where &mut self, bounds: RectF, layout: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { - ctx.scene.push_layer(Some(bounds)); + cx.scene.push_layer(Some(bounds)); let mut item_origin = bounds.origin() - vec2f(0.0, self.state.scroll_top() % layout.item_height); for item in &mut layout.items { - item.paint(item_origin, ctx); + item.paint(item_origin, cx); item_origin += vec2f(0.0, layout.item_height); } - ctx.scene.pop_layer(); + cx.scene.pop_layer(); } fn dispatch_event( @@ -207,11 +207,11 @@ where bounds: RectF, layout: &mut Self::LayoutState, _: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { let mut handled = false; for item in &mut layout.items { - handled = item.dispatch_event(event, ctx) || handled; + handled = item.dispatch_event(event, cx) || handled; } match event { @@ -221,7 +221,7 @@ where precise, } => { if bounds.contains_point(*position) { - if self.scroll(*position, *delta, *precise, layout.scroll_max, ctx) { + if self.scroll(*position, *delta, *precise, layout.scroll_max, cx) { handled = true; } } @@ -237,14 +237,14 @@ where bounds: RectF, layout: &Self::LayoutState, _: &Self::PaintState, - ctx: &crate::DebugContext, + cx: &crate::DebugContext, ) -> json::Value { json!({ "type": "UniformList", "bounds": bounds.to_json(), "scroll_max": layout.scroll_max, "item_height": layout.item_height, - "items": layout.items.iter().map(|item| item.debug(ctx)).collect::>() + "items": layout.items.iter().map(|item| item.debug(cx)).collect::>() }) } diff --git a/gpui/src/keymap.rs b/gpui/src/keymap.rs index e38beade8c657a15d9f56f230c55b19df5547679..a3a2cbf58c1078d9e20a9b2975f4bf7cdc8c9a25 100644 --- a/gpui/src/keymap.rs +++ b/gpui/src/keymap.rs @@ -98,12 +98,12 @@ impl Matcher { &mut self, keystroke: Keystroke, view_id: usize, - ctx: &Context, + cx: &Context, ) -> MatchResult { let pending = self.pending.entry(view_id).or_default(); if let Some(pending_ctx) = pending.context.as_ref() { - if pending_ctx != ctx { + if pending_ctx != cx { pending.keystrokes.clear(); } } @@ -113,11 +113,7 @@ impl Matcher { let mut retain_pending = false; for binding in self.keymap.0.iter().rev() { if binding.keystrokes.starts_with(&pending.keystrokes) - && binding - .context - .as_ref() - .map(|c| c.eval(ctx)) - .unwrap_or(true) + && binding.context.as_ref().map(|c| c.eval(cx)).unwrap_or(true) { if binding.keystrokes.len() == pending.keystrokes.len() { self.pending.remove(&view_id); @@ -127,7 +123,7 @@ impl Matcher { }; } else { retain_pending = true; - pending.context = Some(ctx.clone()); + pending.context = Some(cx.clone()); } } } @@ -312,22 +308,20 @@ impl ContextPredicate { } } - fn eval(&self, ctx: &Context) -> bool { + fn eval(&self, cx: &Context) -> bool { match self { - Self::Identifier(name) => ctx.set.contains(name.as_str()), - Self::Equal(left, right) => ctx + Self::Identifier(name) => cx.set.contains(name.as_str()), + Self::Equal(left, right) => cx .map .get(left) .map(|value| value == right) .unwrap_or(false), - Self::NotEqual(left, right) => ctx - .map - .get(left) - .map(|value| value != right) - .unwrap_or(true), - Self::Not(pred) => !pred.eval(ctx), - Self::And(left, right) => left.eval(ctx) && right.eval(ctx), - Self::Or(left, right) => left.eval(ctx) || right.eval(ctx), + Self::NotEqual(left, right) => { + cx.map.get(left).map(|value| value != right).unwrap_or(true) + } + Self::Not(pred) => !pred.eval(cx), + Self::And(left, right) => left.eval(cx) && right.eval(cx), + Self::Or(left, right) => left.eval(cx) || right.eval(cx), } } } @@ -488,10 +482,10 @@ mod tests { &mut self, keystroke: &str, view_id: usize, - ctx: &Context, + cx: &Context, ) -> Option<(String, Option)> { if let MatchResult::Action { name, arg } = - self.push_keystroke(Keystroke::parse(keystroke).unwrap(), view_id, ctx) + self.push_keystroke(Keystroke::parse(keystroke).unwrap(), view_id, cx) { Some((name, arg.and_then(|arg| arg.downcast_ref::().cloned()))) } else { diff --git a/gpui/src/platform/mac/fonts.rs b/gpui/src/platform/mac/fonts.rs index ac455b93b997a3086c466d905cc229930e6de800..b1605c8b0bad6328fca5c991d127396c7ee5a030 100644 --- a/gpui/src/platform/mac/fonts.rs +++ b/gpui/src/platform/mac/fonts.rs @@ -145,7 +145,7 @@ impl FontSystemState { // Make room for subpixel variants. let bounds = RectI::new(bounds.origin(), bounds.size() + vec2i(1, 1)); let mut pixels = vec![0; bounds.width() as usize * bounds.height() as usize]; - let ctx = CGContext::create_bitmap_context( + let cx = CGContext::create_bitmap_context( Some(pixels.as_mut_ptr() as *mut _), bounds.width() as usize, bounds.height() as usize, @@ -157,9 +157,9 @@ impl FontSystemState { // Move the origin to bottom left and account for scaling, this // makes drawing text consistent with the font-kit's raster_bounds. - ctx.translate(0.0, bounds.height() as CGFloat); + cx.translate(0.0, bounds.height() as CGFloat); let transform = scale.translate(-bounds.origin().to_f32()); - ctx.set_text_matrix(&CGAffineTransform { + cx.set_text_matrix(&CGAffineTransform { a: transform.matrix.m11() as CGFloat, b: -transform.matrix.m21() as CGFloat, c: -transform.matrix.m12() as CGFloat, @@ -168,9 +168,9 @@ impl FontSystemState { ty: -transform.vector.y() as CGFloat, }); - ctx.set_font(&font.native_font().copy_to_CGFont()); - ctx.set_font_size(font_size as CGFloat); - ctx.show_glyphs_at_positions( + cx.set_font(&font.native_font().copy_to_CGFont()); + cx.set_font_size(font_size as CGFloat); + cx.show_glyphs_at_positions( &[glyph_id as CGGlyph], &[CGPoint::new( (subpixel_shift.x() / scale_factor) as CGFloat, diff --git a/gpui/src/presenter.rs b/gpui/src/presenter.rs index 8fdf94de005bf133e015a8947c60e8ea5fa5245a..a6e37097c08229f0363071823e5a2af93134a06d 100644 --- a/gpui/src/presenter.rs +++ b/gpui/src/presenter.rs @@ -31,11 +31,11 @@ impl Presenter { font_cache: Arc, text_layout_cache: TextLayoutCache, asset_cache: Arc, - app: &MutableAppContext, + cx: &MutableAppContext, ) -> Self { Self { window_id, - rendered_views: app.render_views(window_id), + rendered_views: cx.render_views(window_id), parents: HashMap::new(), font_cache, text_layout_cache, @@ -55,7 +55,7 @@ impl Presenter { path } - pub fn invalidate(&mut self, mut invalidation: WindowInvalidation, app: &AppContext) { + pub fn invalidate(&mut self, mut invalidation: WindowInvalidation, cx: &AppContext) { for view_id in invalidation.removed { invalidation.updated.remove(&view_id); self.rendered_views.remove(&view_id); @@ -63,7 +63,7 @@ impl Presenter { } for view_id in invalidation.updated { self.rendered_views - .insert(view_id, app.render_view(self.window_id, view_id).unwrap()); + .insert(view_id, cx.render_view(self.window_id, view_id).unwrap()); } } @@ -71,25 +71,25 @@ impl Presenter { &mut self, window_size: Vector2F, scale_factor: f32, - app: &mut MutableAppContext, + cx: &mut MutableAppContext, ) -> Scene { let mut scene = Scene::new(scale_factor); - if let Some(root_view_id) = app.root_view_id(self.window_id) { - self.layout(window_size, app.as_ref()); - self.after_layout(app); - let mut ctx = PaintContext { + if let Some(root_view_id) = cx.root_view_id(self.window_id) { + self.layout(window_size, cx.as_ref()); + self.after_layout(cx); + let mut paint_cx = PaintContext { scene: &mut scene, font_cache: &self.font_cache, text_layout_cache: &self.text_layout_cache, rendered_views: &mut self.rendered_views, - app: app.as_ref(), + app: cx.as_ref(), }; - ctx.paint(root_view_id, Vector2F::zero()); + paint_cx.paint(root_view_id, Vector2F::zero()); self.text_layout_cache.finish_frame(); if let Some(event) = self.last_mouse_moved_event.clone() { - self.dispatch_event(event, app) + self.dispatch_event(event, cx) } } else { log::error!("could not find root_view_id for window {}", self.window_id); @@ -98,8 +98,8 @@ impl Presenter { scene } - fn layout(&mut self, size: Vector2F, app: &AppContext) { - if let Some(root_view_id) = app.root_view_id(self.window_id) { + fn layout(&mut self, size: Vector2F, cx: &AppContext) { + if let Some(root_view_id) = cx.root_view_id(self.window_id) { let mut layout_ctx = LayoutContext { rendered_views: &mut self.rendered_views, parents: &mut self.parents, @@ -107,49 +107,49 @@ impl Presenter { text_layout_cache: &self.text_layout_cache, asset_cache: &self.asset_cache, view_stack: Vec::new(), - app, + app: cx, }; layout_ctx.layout(root_view_id, SizeConstraint::strict(size)); } } - fn after_layout(&mut self, app: &mut MutableAppContext) { - if let Some(root_view_id) = app.root_view_id(self.window_id) { - let mut ctx = AfterLayoutContext { + fn after_layout(&mut self, cx: &mut MutableAppContext) { + if let Some(root_view_id) = cx.root_view_id(self.window_id) { + let mut layout_cx = AfterLayoutContext { rendered_views: &mut self.rendered_views, font_cache: &self.font_cache, text_layout_cache: &self.text_layout_cache, - app, + app: cx, }; - ctx.after_layout(root_view_id); + layout_cx.after_layout(root_view_id); } } - pub fn dispatch_event(&mut self, event: Event, app: &mut MutableAppContext) { - if let Some(root_view_id) = app.root_view_id(self.window_id) { + pub fn dispatch_event(&mut self, event: Event, cx: &mut MutableAppContext) { + if let Some(root_view_id) = cx.root_view_id(self.window_id) { if matches!(event, Event::MouseMoved { .. }) { self.last_mouse_moved_event = Some(event.clone()); } - let mut ctx = EventContext { + let mut event_cx = EventContext { rendered_views: &mut self.rendered_views, actions: Default::default(), font_cache: &self.font_cache, text_layout_cache: &self.text_layout_cache, view_stack: Default::default(), invalidated_views: Default::default(), - app: app.as_ref(), + app: cx.as_ref(), }; - ctx.dispatch_event(root_view_id, &event); + event_cx.dispatch_event(root_view_id, &event); - let invalidated_views = ctx.invalidated_views; - let actions = ctx.actions; + let invalidated_views = event_cx.invalidated_views; + let actions = event_cx.actions; for view_id in invalidated_views { - app.notify_view(self.window_id, view_id); + cx.notify_view(self.window_id, view_id); } for action in actions { - app.dispatch_action_any( + cx.dispatch_action_any( self.window_id, &action.path, action.name, @@ -159,14 +159,14 @@ impl Presenter { } } - pub fn debug_elements(&self, ctx: &AppContext) -> Option { - ctx.root_view_id(self.window_id) + pub fn debug_elements(&self, cx: &AppContext) -> Option { + cx.root_view_id(self.window_id) .and_then(|root_view_id| self.rendered_views.get(&root_view_id)) .map(|root_element| { root_element.debug(&DebugContext { rendered_views: &self.rendered_views, font_cache: &self.font_cache, - app: ctx, + app: cx, }) }) } @@ -380,9 +380,9 @@ impl Element for ChildView { fn layout( &mut self, constraint: SizeConstraint, - ctx: &mut LayoutContext, + cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { - let size = ctx.layout(self.view_id, constraint); + let size = cx.layout(self.view_id, constraint); (size, ()) } @@ -390,18 +390,18 @@ impl Element for ChildView { &mut self, _: Vector2F, _: &mut Self::LayoutState, - ctx: &mut AfterLayoutContext, + cx: &mut AfterLayoutContext, ) { - ctx.after_layout(self.view_id); + cx.after_layout(self.view_id); } fn paint( &mut self, bounds: pathfinder_geometry::rect::RectF, _: &mut Self::LayoutState, - ctx: &mut PaintContext, + cx: &mut PaintContext, ) -> Self::PaintState { - ctx.paint(self.view_id, bounds.origin()); + cx.paint(self.view_id, bounds.origin()); } fn dispatch_event( @@ -410,9 +410,9 @@ impl Element for ChildView { _: pathfinder_geometry::rect::RectF, _: &mut Self::LayoutState, _: &mut Self::PaintState, - ctx: &mut EventContext, + cx: &mut EventContext, ) -> bool { - ctx.dispatch_event(self.view_id, event) + cx.dispatch_event(self.view_id, event) } fn debug( @@ -420,14 +420,14 @@ impl Element for ChildView { bounds: pathfinder_geometry::rect::RectF, _: &Self::LayoutState, _: &Self::PaintState, - ctx: &DebugContext, + cx: &DebugContext, ) -> serde_json::Value { json!({ "type": "ChildView", "view_id": self.view_id, "bounds": bounds.to_json(), - "child": if let Some(view) = ctx.rendered_views.get(&self.view_id) { - view.debug(ctx) + "child": if let Some(view) = cx.rendered_views.get(&self.view_id) { + view.debug(cx) } else { json!(null) } @@ -441,9 +441,9 @@ mod tests { // fn test_responder_chain() { // let settings = settings_rx(None); // let mut app = App::new().unwrap(); - // let workspace = app.add_model(|ctx| Workspace::new(Vec::new(), ctx)); + // let workspace = app.add_model(|cx| Workspace::new(Vec::new(), cx)); // let (window_id, workspace_view) = - // app.add_window(|ctx| WorkspaceView::new(workspace.clone(), settings, ctx)); + // app.add_window(|cx| WorkspaceView::new(workspace.clone(), settings, cx)); // let invalidations = Rc::new(RefCell::new(Vec::new())); // let invalidations_ = invalidations.clone(); @@ -451,8 +451,8 @@ mod tests { // invalidations_.borrow_mut().push(invalidation) // }); - // let active_pane_id = workspace_view.update(&mut app, |view, ctx| { - // ctx.focus(view.active_pane()); + // let active_pane_id = workspace_view.update(&mut app, |view, cx| { + // cx.focus(view.active_pane()); // view.active_pane().id() // }); @@ -468,7 +468,7 @@ mod tests { // } // assert_eq!( - // presenter.responder_chain(app.ctx()).unwrap(), + // presenter.responder_chain(app.cx()).unwrap(), // vec![workspace_view.id(), active_pane_id] // ); // }); diff --git a/gpui/src/text_layout.rs b/gpui/src/text_layout.rs index af30630fb24b8020bc313b2b3526205334ad2f0d..dfba1810233fb4874210029eea1c2d52b441a750 100644 --- a/gpui/src/text_layout.rs +++ b/gpui/src/text_layout.rs @@ -200,7 +200,7 @@ impl Line { } } - pub fn paint(&self, origin: Vector2F, visible_bounds: RectF, ctx: &mut PaintContext) { + pub fn paint(&self, origin: Vector2F, visible_bounds: RectF, cx: &mut PaintContext) { let padding_top = (visible_bounds.height() - self.layout.ascent - self.layout.descent) / 2.; let baseline_origin = vec2f(0., padding_top + self.layout.ascent); @@ -209,7 +209,7 @@ impl Line { let mut color = ColorU::black(); for run in &self.layout.runs { - let max_glyph_width = ctx + let max_glyph_width = cx .font_cache .bounding_box(run.font_id, self.layout.font_size) .x(); @@ -234,7 +234,7 @@ impl Line { } } - ctx.scene.push_glyph(scene::Glyph { + cx.scene.push_glyph(scene::Glyph { font_id: run.font_id, font_size: self.layout.font_size, id: glyph.id, diff --git a/gpui_macros/src/lib.rs b/gpui_macros/src/lib.rs index cdd4cacb803107baca67fe83359571736d547bf0..c7218387dffe627a1f40084a670f8702628beccc 100644 --- a/gpui_macros/src/lib.rs +++ b/gpui_macros/src/lib.rs @@ -34,8 +34,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream { fn #outer_fn_name() { #inner_fn - #namespace::App::test_async((), move |ctx| async { - #inner_fn_name(ctx).await; + #namespace::App::test_async((), move |cx| async { + #inner_fn_name(cx).await; }); } } @@ -45,8 +45,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream { fn #outer_fn_name() { #inner_fn - #namespace::App::test((), |ctx| { - #inner_fn_name(ctx); + #namespace::App::test((), |cx| { + #inner_fn_name(cx); }); } }