diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 8a91e447c1..08de7d619d 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -2,7 +2,7 @@ use super::{ buffer, movement, Anchor, Bias, Buffer, BufferElement, DisplayMap, DisplayPoint, Point, Selection, SelectionSetId, ToOffset, ToPoint, }; -use crate::{settings::Settings, watch, workspace, worktree::FileHandle}; +use crate::{settings::Settings, workspace, worktree::FileHandle}; use anyhow::Result; use futures_core::future::LocalBoxFuture; use gpui::{ @@ -12,6 +12,7 @@ use gpui::{ }; use gpui::{geometry::vector::Vector2F, TextLayoutCache}; use parking_lot::Mutex; +use postage::watch; use serde::{Deserialize, Serialize}; use smallvec::SmallVec; use smol::Timer; @@ -288,19 +289,13 @@ impl BufferView { settings: watch::Receiver, ctx: &mut ViewContext, ) -> Self { - settings.notify_view_on_change(ctx); - if let Some(file) = file.as_ref() { file.observe_from_view(ctx, |_, _, ctx| ctx.emit(Event::FileHandleChanged)); } ctx.observe_model(&buffer, Self::on_buffer_changed); ctx.subscribe_to_model(&buffer, Self::on_buffer_event); - let display_map = DisplayMap::new( - buffer.clone(), - smol::block_on(settings.read()).tab_size, - ctx.as_ref(), - ); + let display_map = DisplayMap::new(buffer.clone(), settings.borrow().tab_size, ctx.as_ref()); let (selection_set_id, _) = buffer.update(ctx, |buffer, ctx| { buffer.add_selection_set( @@ -1924,31 +1919,31 @@ impl BufferView { } pub fn font_size(&self) -> f32 { - smol::block_on(self.settings.read()).buffer_font_size + self.settings.borrow().buffer_font_size } pub fn font_ascent(&self, font_cache: &FontCache) -> f32 { - let settings = smol::block_on(self.settings.read()); + let settings = self.settings.borrow(); let font_id = font_cache.default_font(settings.buffer_font_family); let ascent = font_cache.metric(font_id, |m| m.ascent); font_cache.scale_metric(ascent, font_id, settings.buffer_font_size) } pub fn font_descent(&self, font_cache: &FontCache) -> f32 { - let settings = smol::block_on(self.settings.read()); + let settings = self.settings.borrow(); let font_id = font_cache.default_font(settings.buffer_font_family); let ascent = font_cache.metric(font_id, |m| m.descent); font_cache.scale_metric(ascent, font_id, settings.buffer_font_size) } pub fn line_height(&self, font_cache: &FontCache) -> f32 { - let settings = smol::block_on(self.settings.read()); + let settings = self.settings.borrow(); let font_id = font_cache.default_font(settings.buffer_font_family); font_cache.line_height(font_id, settings.buffer_font_size) } pub fn em_width(&self, font_cache: &FontCache) -> f32 { - let settings = smol::block_on(self.settings.read()); + let settings = self.settings.borrow(); let font_id = font_cache.default_font(settings.buffer_font_family); font_cache.em_width(font_id, settings.buffer_font_size) } @@ -1960,7 +1955,7 @@ impl BufferView { layout_cache: &TextLayoutCache, app: &AppContext, ) -> Result { - let settings = smol::block_on(self.settings.read()); + let settings = self.settings.borrow(); let font_size = settings.buffer_font_size; let font_id = font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?; @@ -1985,7 +1980,7 @@ impl BufferView { layout_cache: &TextLayoutCache, ctx: &AppContext, ) -> Result>> { - let settings = smol::block_on(self.settings.read()); + let settings = self.settings.borrow(); let font_size = settings.buffer_font_size; let font_id = font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?; @@ -2029,7 +2024,7 @@ impl BufferView { return Ok(Vec::new()); } - let settings = smol::block_on(self.settings.read()); + let settings = self.settings.borrow(); let font_id = font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?; let font_size = settings.buffer_font_size; @@ -2067,7 +2062,7 @@ impl BufferView { layout_cache: &TextLayoutCache, app: &AppContext, ) -> Result> { - let settings = smol::block_on(self.settings.read()); + let settings = self.settings.borrow(); let font_id = font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?; diff --git a/zed/src/file_finder.rs b/zed/src/file_finder.rs index 0965cdcda5..0f966cc578 100644 --- a/zed/src/file_finder.rs +++ b/zed/src/file_finder.rs @@ -1,7 +1,7 @@ use crate::{ editor::{buffer_view, BufferView}, settings::Settings, - util, watch, + util, workspace::Workspace, worktree::{match_paths, PathMatch, Worktree}, }; @@ -14,6 +14,7 @@ use gpui::{ AppContext, Axis, Border, Entity, MutableAppContext, View, ViewContext, ViewHandle, WeakViewHandle, }; +use postage::watch; use std::{ cmp, path::Path, @@ -105,7 +106,7 @@ impl View for FileFinder { impl FileFinder { fn render_matches(&self) -> ElementBox { if self.matches.is_empty() { - let settings = smol::block_on(self.settings.read()); + let settings = self.settings.borrow(); return Container::new( Label::new( "No matches".into(), @@ -148,7 +149,7 @@ impl FileFinder { ) -> Option { self.labels_for_match(path_match, app).map( |(file_name, file_name_positions, full_path, full_path_positions)| { - let settings = smol::block_on(self.settings.read()); + let settings = self.settings.borrow(); let highlight_color = ColorU::from_u32(0x304ee2ff); let bold = *Properties::new().weight(Weight::BOLD); let mut container = Container::new( @@ -292,8 +293,6 @@ impl FileFinder { let query_buffer = ctx.add_view(|ctx| BufferView::single_line(settings.clone(), ctx)); ctx.subscribe_to_view(&query_buffer, Self::on_query_buffer_event); - settings.notify_view_on_change(ctx); - Self { handle: ctx.handle().downgrade(), settings, diff --git a/zed/src/lib.rs b/zed/src/lib.rs index 7c6155f2d1..7dd383f56e 100644 --- a/zed/src/lib.rs +++ b/zed/src/lib.rs @@ -9,6 +9,5 @@ mod sum_tree; mod test; mod time; mod util; -pub mod watch; pub mod workspace; mod worktree; diff --git a/zed/src/menus.rs b/zed/src/menus.rs index 08afb1e990..0feae2128b 100644 --- a/zed/src/menus.rs +++ b/zed/src/menus.rs @@ -1,8 +1,9 @@ -use crate::{settings::Settings, watch::Receiver}; +use crate::settings::Settings; use gpui::{Menu, MenuItem}; +use postage::watch; #[cfg(target_os = "macos")] -pub fn menus(settings: Receiver) -> Vec> { +pub fn menus(settings: watch::Receiver) -> Vec> { vec![ Menu { name: "Zed", diff --git a/zed/src/settings.rs b/zed/src/settings.rs index 7b84740000..44b05c99fa 100644 --- a/zed/src/settings.rs +++ b/zed/src/settings.rs @@ -1,6 +1,6 @@ -use crate::watch; use anyhow::Result; use gpui::font_cache::{FamilyId, FontCache}; +use postage::watch; #[derive(Clone)] pub struct Settings { @@ -26,5 +26,5 @@ impl Settings { pub fn channel( font_cache: &FontCache, ) -> Result<(watch::Sender, watch::Receiver)> { - Ok(watch::channel(Settings::new(font_cache)?)) + Ok(watch::channel_with(Settings::new(font_cache)?)) } diff --git a/zed/src/watch.rs b/zed/src/watch.rs deleted file mode 100644 index 33622edc6f..0000000000 --- a/zed/src/watch.rs +++ /dev/null @@ -1,65 +0,0 @@ -// TODO: This implementation is actually broken in that it will only - -use gpui::{Entity, ModelContext, View, ViewContext}; -use smol::{channel, lock::RwLock}; -use std::ops::Deref; -use std::sync::Arc; - -pub struct Sender { - value: Arc>, - updated: channel::Sender<()>, -} - -#[derive(Clone)] -pub struct Receiver { - value: Arc>, - updated: channel::Receiver<()>, -} - -impl Sender { - pub async fn update(&mut self, f: impl FnOnce(&mut T) -> R) -> R { - let result = f(&mut *self.value.write().await); - self.updated.send(()).await.unwrap(); - result - } -} - -impl Receiver { - pub async fn updated(&self) { - let _ = self.updated.recv().await; - } - - pub async fn read<'a>(&'a self) -> impl 'a + Deref { - self.value.read().await - } -} - -// TODO: These implementations are broken because they only handle a single update. -impl Receiver { - pub fn notify_model_on_change(&self, ctx: &mut ModelContext) { - let watch = self.clone(); - ctx.spawn(async move { watch.updated().await }, |_, _, ctx| { - ctx.notify() - }) - .detach(); - } - - pub fn notify_view_on_change(&self, ctx: &mut ViewContext) { - let watch = self.clone(); - ctx.spawn(async move { watch.updated().await }, |_, _, ctx| { - ctx.notify() - }) - .detach(); - } -} - -pub fn channel(value: T) -> (Sender, Receiver) { - let value = Arc::new(RwLock::new(value)); - let (s, r) = channel::unbounded(); - let sender = Sender { - value: value.clone(), - updated: s, - }; - let receiver = Receiver { value, updated: r }; - (sender, receiver) -} diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 629d23beec..40152ddfeb 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -3,11 +3,9 @@ pub mod pane_group; pub use pane::*; pub use pane_group::*; -use crate::{ - settings::Settings, - watch::{self, Receiver}, -}; +use crate::settings::Settings; use gpui::{MutableAppContext, PathPromptOptions}; +use postage::watch; use std::path::PathBuf; pub fn init(app: &mut MutableAppContext) { app.add_global_action("workspace:open", open); @@ -44,7 +42,7 @@ pub struct OpenParams { pub settings: watch::Receiver, } -fn open(settings: &Receiver, ctx: &mut MutableAppContext) { +fn open(settings: &watch::Receiver, ctx: &mut MutableAppContext) { let settings = settings.clone(); ctx.prompt_for_paths( PathPromptOptions { diff --git a/zed/src/workspace/pane.rs b/zed/src/workspace/pane.rs index 5da80e560c..e7957ec948 100644 --- a/zed/src/workspace/pane.rs +++ b/zed/src/workspace/pane.rs @@ -1,5 +1,5 @@ use super::{ItemViewHandle, SplitDirection}; -use crate::{settings::Settings, watch}; +use crate::settings::Settings; use gpui::{ color::ColorU, elements::*, @@ -7,6 +7,7 @@ use gpui::{ keymap::Binding, AppContext, Border, Entity, MutableAppContext, Quad, View, ViewContext, }; +use postage::watch; use std::{cmp, path::Path, sync::Arc}; pub fn init(app: &mut MutableAppContext) { @@ -185,7 +186,7 @@ impl Pane { } fn render_tabs(&self, ctx: &AppContext) -> ElementBox { - let settings = smol::block_on(self.settings.read()); + let settings = self.settings.borrow(); let border_color = ColorU::from_u32(0xdbdbdcff); let line_height = ctx.font_cache().line_height( ctx.font_cache().default_font(settings.ui_font_family),