Merge MutableAppContext into AppContext

There may have been a good reason for the difference at some point, or I was
still learning Rust. But now it's just &mut AppContext vs &AppContext.
This commit is contained in:
Nathan Sobo
2023-04-06 15:49:03 -06:00
parent dd00966cc6
commit de9bf6dfbd
112 changed files with 882 additions and 1041 deletions
+297 -431
View File
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -1,4 +1,4 @@
use crate::MutableAppContext;
use crate::AppContext;
use collections::{BTreeMap, HashMap, HashSet};
use parking_lot::Mutex;
use std::sync::Arc;
@@ -93,10 +93,10 @@ impl<K: Clone + Hash + Eq + Copy, F> CallbackCollection<K, F> {
drop(callbacks);
}
pub fn emit<C: FnMut(&mut F, &mut MutableAppContext) -> bool>(
pub fn emit<C: FnMut(&mut F, &mut AppContext) -> bool>(
&mut self,
key: K,
cx: &mut MutableAppContext,
cx: &mut AppContext,
mut call_callback: C,
) {
let callbacks = self.internal.lock().callbacks.remove(&key);
+3 -3
View File
@@ -1,4 +1,4 @@
use crate::{Action, App, ForegroundPlatform, MutableAppContext};
use crate::{Action, App, AppContext, ForegroundPlatform};
pub struct Menu<'a> {
pub name: &'a str,
@@ -51,7 +51,7 @@ pub enum OsAction {
Redo,
}
impl MutableAppContext {
impl AppContext {
pub fn set_menus(&mut self, menus: Vec<Menu>) {
self.foreground_platform
.set_menus(menus, &self.keystroke_matcher);
@@ -77,7 +77,7 @@ pub(crate) fn setup_menu_handlers(foreground_platform: &dyn ForegroundPlatform,
let cx = app.0.clone();
move |action| {
let mut cx = cx.borrow_mut();
if let Some(main_window_id) = cx.cx.platform.main_window_id() {
if let Some(main_window_id) = cx.platform.main_window_id() {
if let Some(view_id) = cx.focused_view_id(main_window_id) {
cx.handle_dispatch_action_from_effect(main_window_id, Some(view_id), action);
return;
+13 -14
View File
@@ -19,9 +19,8 @@ use smol::stream::StreamExt;
use crate::{
executor, geometry::vector::Vector2F, keymap_matcher::Keystroke, platform, Action,
AnyViewHandle, AppContext, Appearance, Entity, Event, FontCache, Handle, InputHandler,
KeyDownEvent, ModelContext, ModelHandle, MutableAppContext, Platform, ReadModelWith,
ReadViewWith, RenderContext, Task, UpdateModel, UpdateView, View, ViewContext, ViewHandle,
WeakHandle,
KeyDownEvent, ModelContext, ModelHandle, Platform, ReadModelWith, ReadViewWith, RenderContext,
Task, UpdateModel, UpdateView, View, ViewContext, ViewHandle, WeakHandle,
};
use collections::BTreeMap;
@@ -30,7 +29,7 @@ use super::{
};
pub struct TestAppContext {
cx: Rc<RefCell<MutableAppContext>>,
cx: Rc<RefCell<AppContext>>,
foreground_platform: Rc<platform::test::ForegroundPlatform>,
condition_duration: Option<Duration>,
pub function_name: String,
@@ -48,7 +47,7 @@ impl TestAppContext {
first_entity_id: usize,
function_name: String,
) -> Self {
let mut cx = MutableAppContext::new(
let mut cx = AppContext::new(
foreground,
background,
platform,
@@ -149,15 +148,15 @@ impl TestAppContext {
self.cx.borrow().window_ids().collect()
}
pub fn root_view<T: View>(&self, window_id: usize) -> Option<ViewHandle<T>> {
pub fn root_view(&self, window_id: usize) -> Option<AnyViewHandle> {
self.cx.borrow().root_view(window_id)
}
pub fn read<T, F: FnOnce(&AppContext) -> T>(&self, callback: F) -> T {
callback(self.cx.borrow().as_ref())
callback(&*self.cx.borrow())
}
pub fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
pub fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, callback: F) -> T {
let mut state = self.cx.borrow_mut();
// Don't increment pending flushes in order for effects to be flushed before the callback
// completes, which is helpful in tests.
@@ -194,7 +193,7 @@ impl TestAppContext {
}
pub fn font_cache(&self) -> Arc<FontCache> {
self.cx.borrow().cx.font_cache.clone()
self.cx.borrow().font_cache.clone()
}
pub fn foreground_platform(&self) -> Rc<platform::test::ForegroundPlatform> {
@@ -202,7 +201,7 @@ impl TestAppContext {
}
pub fn platform(&self) -> Arc<dyn platform::Platform> {
self.cx.borrow().cx.platform.clone()
self.cx.borrow().platform.clone()
}
pub fn foreground(&self) -> Rc<executor::Foreground> {
@@ -396,7 +395,7 @@ impl ReadModelWith for TestAppContext {
read: &mut dyn FnMut(&E, &AppContext) -> T,
) -> T {
let cx = self.cx.borrow();
let cx = cx.as_ref();
let cx = &*cx;
read(handle.read(cx), cx)
}
}
@@ -424,7 +423,7 @@ impl ReadViewWith for TestAppContext {
V: View,
{
let cx = self.cx.borrow();
let cx = cx.as_ref();
let cx = &*cx;
read(handle.read(cx), cx)
}
}
@@ -513,7 +512,7 @@ impl<T: Entity> ModelHandle<T> {
loop {
{
let cx = cx.borrow();
let cx = cx.as_ref();
let cx = &*cx;
if predicate(
handle
.upgrade(cx)
@@ -600,7 +599,7 @@ impl<T: View> ViewHandle<T> {
loop {
{
let cx = cx.borrow();
let cx = cx.as_ref();
let cx = &*cx;
if predicate(
handle
.upgrade(cx)
+6 -6
View File
@@ -2,10 +2,10 @@ use std::{cell::RefCell, ops::Range, rc::Rc};
use pathfinder_geometry::rect::RectF;
use crate::{AnyView, AppContext, InputHandler, MutableAppContext};
use crate::{AnyView, AppContext, InputHandler};
pub struct WindowInputHandler {
pub app: Rc<RefCell<MutableAppContext>>,
pub app: Rc<RefCell<AppContext>>,
pub window_id: usize,
}
@@ -23,21 +23,21 @@ impl WindowInputHandler {
let app = self.app.try_borrow().ok()?;
let view_id = app.focused_view_id(self.window_id)?;
let view = app.cx.views.get(&(self.window_id, view_id))?;
let view = app.views.get(&(self.window_id, view_id))?;
let result = f(view.as_ref(), &app);
Some(result)
}
fn update_focused_view<T, F>(&mut self, f: F) -> Option<T>
where
F: FnOnce(usize, usize, &mut dyn AnyView, &mut MutableAppContext) -> T,
F: FnOnce(usize, usize, &mut dyn AnyView, &mut AppContext) -> T,
{
let mut app = self.app.try_borrow_mut().ok()?;
app.update(|app| {
let view_id = app.focused_view_id(self.window_id)?;
let mut view = app.cx.views.remove(&(self.window_id, view_id))?;
let mut view = app.views.remove(&(self.window_id, view_id))?;
let result = f(self.window_id, view_id, view.as_mut(), &mut *app);
app.cx.views.insert((self.window_id, view_id), view);
app.views.insert((self.window_id, view_id), view);
Some(result)
})
}
+1 -1
View File
@@ -210,7 +210,7 @@ mod tests {
use crate::fonts::{Properties as FontProperties, Weight};
#[crate::test(self)]
fn test_layout_label_with_highlights(cx: &mut crate::MutableAppContext) {
fn test_layout_label_with_highlights(cx: &mut crate::AppContext) {
let default_style = TextStyle::new(
"Menlo",
12.,
+2 -2
View File
@@ -630,7 +630,7 @@ mod tests {
use std::env;
#[crate::test(self)]
fn test_layout(cx: &mut crate::MutableAppContext) {
fn test_layout(cx: &mut crate::AppContext) {
let mut presenter = cx.build_presenter(0, 0., Default::default());
let (_, view) = cx.add_window(Default::default(), |_| TestView);
let constraint = SizeConstraint::new(vec2f(0., 0.), vec2f(100., 40.));
@@ -725,7 +725,7 @@ mod tests {
}
#[crate::test(self, iterations = 10, seed = 0)]
fn test_random(cx: &mut crate::MutableAppContext, mut rng: StdRng) {
fn test_random(cx: &mut crate::AppContext, mut rng: StdRng) {
let operations = env::var("OPERATIONS")
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
.unwrap_or(10);
+2 -4
View File
@@ -271,12 +271,10 @@ pub fn layout_highlighted_chunks<'a>(
#[cfg(test)]
mod tests {
use super::*;
use crate::{
elements::Empty, fonts, ElementBox, Entity, MutableAppContext, RenderContext, View,
};
use crate::{elements::Empty, fonts, AppContext, ElementBox, Entity, RenderContext, View};
#[crate::test(self)]
fn test_soft_wrapping_with_carriage_returns(cx: &mut MutableAppContext) {
fn test_soft_wrapping_with_carriage_returns(cx: &mut AppContext) {
let (window_id, _) = cx.add_window(Default::default(), |_| TestView);
let mut presenter = cx.build_presenter(window_id, Default::default(), Default::default());
fonts::with_font_cache(cx.font_cache().clone(), || {
+2 -2
View File
@@ -17,7 +17,7 @@ use std::{
use crate::{
platform::{self, Dispatcher},
util, MutableAppContext,
util, AppContext,
};
pub enum Foreground {
@@ -931,7 +931,7 @@ impl<T> Task<T> {
}
impl<T: 'static, E: 'static + Display> Task<Result<T, E>> {
pub fn detach_and_log_err(self, cx: &mut MutableAppContext) {
pub fn detach_and_log_err(self, cx: &mut AppContext) {
cx.spawn(|_| async move {
if let Err(err) = self.await {
log::error!("{}", err);
+2 -2
View File
@@ -498,12 +498,12 @@ extern "C" {
#[cfg(test)]
mod tests {
use super::*;
use crate::MutableAppContext;
use crate::AppContext;
use font_kit::properties::{Style, Weight};
use platform::FontSystem as _;
#[crate::test(self, retries = 5)]
fn test_layout_str(_: &mut MutableAppContext) {
fn test_layout_str(_: &mut AppContext) {
// This is failing intermittently on CI and we don't have time to figure it out
let fonts = FontSystem::new();
let menlo = fonts.load_family("Menlo", &Default::default()).unwrap();
+19 -19
View File
@@ -1,5 +1,5 @@
use crate::{
app::{AppContext, MutableAppContext, WindowInvalidation},
app::WindowInvalidation,
elements::Element,
font_cache::FontCache,
geometry::rect::RectF,
@@ -10,11 +10,11 @@ use crate::{
MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut, Scene,
},
text_layout::TextLayoutCache,
Action, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle, Appearance,
AssetCache, ElementBox, Entity, FontSystem, ModelHandle, MouseButton, MouseMovedEvent,
MouseRegion, MouseRegionId, MouseState, ParentId, ReadModel, ReadView, RenderContext,
RenderParams, SceneBuilder, UpgradeModelHandle, UpgradeViewHandle, View, ViewHandle,
WeakModelHandle, WeakViewHandle,
Action, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle, AppContext,
Appearance, AssetCache, ElementBox, Entity, FontSystem, ModelHandle, MouseButton,
MouseMovedEvent, MouseRegion, MouseRegionId, MouseState, ParentId, ReadModel, ReadView,
RenderContext, RenderParams, SceneBuilder, UpgradeModelHandle, UpgradeViewHandle, View,
ViewHandle, WeakModelHandle, WeakViewHandle,
};
use anyhow::bail;
use collections::{HashMap, HashSet};
@@ -56,7 +56,7 @@ impl Presenter {
font_cache: Arc<FontCache>,
text_layout_cache: TextLayoutCache,
asset_cache: Arc<AssetCache>,
cx: &mut MutableAppContext,
cx: &mut AppContext,
) -> Self {
Self {
window_id,
@@ -80,7 +80,7 @@ impl Presenter {
&mut self,
invalidation: &mut WindowInvalidation,
appearance: Appearance,
cx: &mut MutableAppContext,
cx: &mut AppContext,
) {
cx.start_frame();
self.appearance = appearance;
@@ -111,7 +111,7 @@ impl Presenter {
&mut self,
invalidation: &mut WindowInvalidation,
appearance: Appearance,
cx: &mut MutableAppContext,
cx: &mut AppContext,
) {
self.invalidate(invalidation, appearance, cx);
for (view_id, view) in &mut self.rendered_views {
@@ -138,7 +138,7 @@ impl Presenter {
window_size: Vector2F,
scale_factor: f32,
refreshing: bool,
cx: &mut MutableAppContext,
cx: &mut AppContext,
) -> Scene {
let mut scene_builder = SceneBuilder::new(scale_factor);
@@ -169,7 +169,7 @@ impl Presenter {
}
}
fn layout(&mut self, window_size: Vector2F, refreshing: bool, cx: &mut MutableAppContext) {
fn layout(&mut self, window_size: Vector2F, refreshing: bool, cx: &mut AppContext) {
if let Some(root_view_id) = cx.root_view_id(self.window_id) {
self.build_layout_context(window_size, refreshing, cx)
.layout(root_view_id, SizeConstraint::strict(window_size));
@@ -180,7 +180,7 @@ impl Presenter {
&'a mut self,
window_size: Vector2F,
refreshing: bool,
cx: &'a mut MutableAppContext,
cx: &'a mut AppContext,
) -> LayoutContext<'a> {
LayoutContext {
window_id: self.window_id,
@@ -206,7 +206,7 @@ impl Presenter {
&'a mut self,
scene: &'a mut SceneBuilder,
window_size: Vector2F,
cx: &'a mut MutableAppContext,
cx: &'a mut AppContext,
) -> PaintContext {
PaintContext {
scene,
@@ -234,7 +234,7 @@ impl Presenter {
&mut self,
event: Event,
event_reused: bool,
cx: &mut MutableAppContext,
cx: &mut AppContext,
) -> bool {
let mut mouse_events = SmallVec::<[_; 2]>::new();
let mut notified_views: HashSet<usize> = Default::default();
@@ -557,7 +557,7 @@ impl Presenter {
pub fn build_event_context<'a>(
&'a mut self,
notified_views: &'a mut HashSet<usize>,
cx: &'a mut MutableAppContext,
cx: &'a mut AppContext,
) -> EventContext<'a> {
EventContext {
font_cache: &self.font_cache,
@@ -595,7 +595,7 @@ pub struct LayoutContext<'a> {
pub font_system: Arc<dyn FontSystem>,
pub text_layout_cache: &'a TextLayoutCache,
pub asset_cache: &'a AssetCache,
pub app: &'a mut MutableAppContext,
pub app: &'a mut AppContext,
pub refreshing: bool,
pub window_size: Vector2F,
titlebar_height: f32,
@@ -692,7 +692,7 @@ impl<'a> LayoutContext<'a> {
}
impl<'a> Deref for LayoutContext<'a> {
type Target = MutableAppContext;
type Target = AppContext;
fn deref(&self) -> &Self::Target {
self.app
@@ -804,7 +804,7 @@ impl<'a> Deref for PaintContext<'a> {
pub struct EventContext<'a> {
pub font_cache: &'a FontCache,
pub text_layout_cache: &'a TextLayoutCache,
pub app: &'a mut MutableAppContext,
pub app: &'a mut AppContext,
pub window_id: usize,
pub notify_count: usize,
view_stack: Vec<usize>,
@@ -871,7 +871,7 @@ impl<'a> EventContext<'a> {
}
impl<'a> Deref for EventContext<'a> {
type Target = MutableAppContext;
type Target = AppContext;
fn deref(&self) -> &Self::Target {
self.app
+2 -2
View File
@@ -18,7 +18,7 @@ use crate::{
executor::{self, ExecutorEvent},
platform,
util::CwdBacktrace,
Element, ElementBox, Entity, FontCache, Handle, MutableAppContext, Platform, RenderContext,
AppContext, Element, ElementBox, Entity, FontCache, Handle, Platform, RenderContext,
Subscription, TestAppContext, View,
};
@@ -40,7 +40,7 @@ pub fn run_test(
detect_nondeterminism: bool,
test_fn: &mut (dyn RefUnwindSafe
+ Fn(
&mut MutableAppContext,
&mut AppContext,
Rc<platform::test::ForegroundPlatform>,
Arc<executor::Deterministic>,
u64,
+2 -2
View File
@@ -660,7 +660,7 @@ mod tests {
use crate::fonts::{Properties, Weight};
#[crate::test(self)]
fn test_wrap_line(cx: &mut crate::MutableAppContext) {
fn test_wrap_line(cx: &mut crate::AppContext) {
let font_cache = cx.font_cache().clone();
let font_system = cx.platform().fonts();
let family = font_cache
@@ -721,7 +721,7 @@ mod tests {
}
#[crate::test(self, retries = 5)]
fn test_wrap_shaped_line(cx: &mut crate::MutableAppContext) {
fn test_wrap_shaped_line(cx: &mut crate::AppContext) {
// This is failing intermittently on CI and we don't have time to figure it out
let font_cache = cx.font_cache().clone();
let font_system = cx.platform().fonts();
+1 -1
View File
@@ -2,6 +2,6 @@ mod select;
pub use select::{ItemType, Select, SelectStyle};
pub fn init(cx: &mut super::MutableAppContext) {
pub fn init(cx: &mut super::AppContext) {
select::init(cx);
}
+5 -8
View File
@@ -1,8 +1,8 @@
use serde::Deserialize;
use crate::{
actions, elements::*, impl_actions, AppContext, Entity, MouseButton, MutableAppContext,
RenderContext, View, ViewContext, WeakViewHandle,
actions, elements::*, impl_actions, AppContext, Entity, MouseButton, RenderContext, View,
ViewContext, WeakViewHandle,
};
pub struct Select {
@@ -12,7 +12,7 @@ pub struct Select {
item_count: usize,
is_open: bool,
list_state: UniformListState,
build_style: Option<Box<dyn FnMut(&mut MutableAppContext) -> SelectStyle>>,
build_style: Option<Box<dyn FnMut(&mut AppContext) -> SelectStyle>>,
}
#[derive(Clone, Default)]
@@ -35,7 +35,7 @@ impl_actions!(select, [SelectItem]);
pub enum Event {}
pub fn init(cx: &mut MutableAppContext) {
pub fn init(cx: &mut AppContext) {
cx.add_action(Select::toggle);
cx.add_action(Select::select_item);
}
@@ -57,10 +57,7 @@ impl Select {
}
}
pub fn with_style(
mut self,
f: impl 'static + FnMut(&mut MutableAppContext) -> SelectStyle,
) -> Self {
pub fn with_style(mut self, f: impl 'static + FnMut(&mut AppContext) -> SelectStyle) -> Self {
self.build_style = Some(Box::new(f));
self
}