Replace zed::watch with postage::watch for settings
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
parent
ab10e27424
commit
4eac765f1a
8 changed files with 27 additions and 99 deletions
|
@ -2,7 +2,7 @@ use super::{
|
||||||
buffer, movement, Anchor, Bias, Buffer, BufferElement, DisplayMap, DisplayPoint, Point,
|
buffer, movement, Anchor, Bias, Buffer, BufferElement, DisplayMap, DisplayPoint, Point,
|
||||||
Selection, SelectionSetId, ToOffset, ToPoint,
|
Selection, SelectionSetId, ToOffset, ToPoint,
|
||||||
};
|
};
|
||||||
use crate::{settings::Settings, watch, workspace, worktree::FileHandle};
|
use crate::{settings::Settings, workspace, worktree::FileHandle};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
|
@ -12,6 +12,7 @@ use gpui::{
|
||||||
};
|
};
|
||||||
use gpui::{geometry::vector::Vector2F, TextLayoutCache};
|
use gpui::{geometry::vector::Vector2F, TextLayoutCache};
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
use postage::watch;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use smol::Timer;
|
use smol::Timer;
|
||||||
|
@ -288,19 +289,13 @@ impl BufferView {
|
||||||
settings: watch::Receiver<Settings>,
|
settings: watch::Receiver<Settings>,
|
||||||
ctx: &mut ViewContext<Self>,
|
ctx: &mut ViewContext<Self>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
settings.notify_view_on_change(ctx);
|
|
||||||
|
|
||||||
if let Some(file) = file.as_ref() {
|
if let Some(file) = file.as_ref() {
|
||||||
file.observe_from_view(ctx, |_, _, ctx| ctx.emit(Event::FileHandleChanged));
|
file.observe_from_view(ctx, |_, _, ctx| ctx.emit(Event::FileHandleChanged));
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.observe_model(&buffer, Self::on_buffer_changed);
|
ctx.observe_model(&buffer, Self::on_buffer_changed);
|
||||||
ctx.subscribe_to_model(&buffer, Self::on_buffer_event);
|
ctx.subscribe_to_model(&buffer, Self::on_buffer_event);
|
||||||
let display_map = DisplayMap::new(
|
let display_map = DisplayMap::new(buffer.clone(), settings.borrow().tab_size, ctx.as_ref());
|
||||||
buffer.clone(),
|
|
||||||
smol::block_on(settings.read()).tab_size,
|
|
||||||
ctx.as_ref(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let (selection_set_id, _) = buffer.update(ctx, |buffer, ctx| {
|
let (selection_set_id, _) = buffer.update(ctx, |buffer, ctx| {
|
||||||
buffer.add_selection_set(
|
buffer.add_selection_set(
|
||||||
|
@ -1924,31 +1919,31 @@ impl BufferView {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn font_size(&self) -> f32 {
|
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 {
|
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 font_id = font_cache.default_font(settings.buffer_font_family);
|
||||||
let ascent = font_cache.metric(font_id, |m| m.ascent);
|
let ascent = font_cache.metric(font_id, |m| m.ascent);
|
||||||
font_cache.scale_metric(ascent, font_id, settings.buffer_font_size)
|
font_cache.scale_metric(ascent, font_id, settings.buffer_font_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn font_descent(&self, font_cache: &FontCache) -> f32 {
|
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 font_id = font_cache.default_font(settings.buffer_font_family);
|
||||||
let ascent = font_cache.metric(font_id, |m| m.descent);
|
let ascent = font_cache.metric(font_id, |m| m.descent);
|
||||||
font_cache.scale_metric(ascent, font_id, settings.buffer_font_size)
|
font_cache.scale_metric(ascent, font_id, settings.buffer_font_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn line_height(&self, font_cache: &FontCache) -> f32 {
|
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);
|
let font_id = font_cache.default_font(settings.buffer_font_family);
|
||||||
font_cache.line_height(font_id, settings.buffer_font_size)
|
font_cache.line_height(font_id, settings.buffer_font_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn em_width(&self, font_cache: &FontCache) -> f32 {
|
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);
|
let font_id = font_cache.default_font(settings.buffer_font_family);
|
||||||
font_cache.em_width(font_id, settings.buffer_font_size)
|
font_cache.em_width(font_id, settings.buffer_font_size)
|
||||||
}
|
}
|
||||||
|
@ -1960,7 +1955,7 @@ impl BufferView {
|
||||||
layout_cache: &TextLayoutCache,
|
layout_cache: &TextLayoutCache,
|
||||||
app: &AppContext,
|
app: &AppContext,
|
||||||
) -> Result<f32> {
|
) -> Result<f32> {
|
||||||
let settings = smol::block_on(self.settings.read());
|
let settings = self.settings.borrow();
|
||||||
let font_size = settings.buffer_font_size;
|
let font_size = settings.buffer_font_size;
|
||||||
let font_id =
|
let font_id =
|
||||||
font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?;
|
font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?;
|
||||||
|
@ -1985,7 +1980,7 @@ impl BufferView {
|
||||||
layout_cache: &TextLayoutCache,
|
layout_cache: &TextLayoutCache,
|
||||||
ctx: &AppContext,
|
ctx: &AppContext,
|
||||||
) -> Result<Vec<Arc<text_layout::Line>>> {
|
) -> Result<Vec<Arc<text_layout::Line>>> {
|
||||||
let settings = smol::block_on(self.settings.read());
|
let settings = self.settings.borrow();
|
||||||
let font_size = settings.buffer_font_size;
|
let font_size = settings.buffer_font_size;
|
||||||
let font_id =
|
let font_id =
|
||||||
font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?;
|
font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?;
|
||||||
|
@ -2029,7 +2024,7 @@ impl BufferView {
|
||||||
return Ok(Vec::new());
|
return Ok(Vec::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
let settings = smol::block_on(self.settings.read());
|
let settings = self.settings.borrow();
|
||||||
let font_id =
|
let font_id =
|
||||||
font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?;
|
font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?;
|
||||||
let font_size = settings.buffer_font_size;
|
let font_size = settings.buffer_font_size;
|
||||||
|
@ -2067,7 +2062,7 @@ impl BufferView {
|
||||||
layout_cache: &TextLayoutCache,
|
layout_cache: &TextLayoutCache,
|
||||||
app: &AppContext,
|
app: &AppContext,
|
||||||
) -> Result<Arc<text_layout::Line>> {
|
) -> Result<Arc<text_layout::Line>> {
|
||||||
let settings = smol::block_on(self.settings.read());
|
let settings = self.settings.borrow();
|
||||||
let font_id =
|
let font_id =
|
||||||
font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?;
|
font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?;
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
editor::{buffer_view, BufferView},
|
editor::{buffer_view, BufferView},
|
||||||
settings::Settings,
|
settings::Settings,
|
||||||
util, watch,
|
util,
|
||||||
workspace::Workspace,
|
workspace::Workspace,
|
||||||
worktree::{match_paths, PathMatch, Worktree},
|
worktree::{match_paths, PathMatch, Worktree},
|
||||||
};
|
};
|
||||||
|
@ -14,6 +14,7 @@ use gpui::{
|
||||||
AppContext, Axis, Border, Entity, MutableAppContext, View, ViewContext, ViewHandle,
|
AppContext, Axis, Border, Entity, MutableAppContext, View, ViewContext, ViewHandle,
|
||||||
WeakViewHandle,
|
WeakViewHandle,
|
||||||
};
|
};
|
||||||
|
use postage::watch;
|
||||||
use std::{
|
use std::{
|
||||||
cmp,
|
cmp,
|
||||||
path::Path,
|
path::Path,
|
||||||
|
@ -105,7 +106,7 @@ impl View for FileFinder {
|
||||||
impl FileFinder {
|
impl FileFinder {
|
||||||
fn render_matches(&self) -> ElementBox {
|
fn render_matches(&self) -> ElementBox {
|
||||||
if self.matches.is_empty() {
|
if self.matches.is_empty() {
|
||||||
let settings = smol::block_on(self.settings.read());
|
let settings = self.settings.borrow();
|
||||||
return Container::new(
|
return Container::new(
|
||||||
Label::new(
|
Label::new(
|
||||||
"No matches".into(),
|
"No matches".into(),
|
||||||
|
@ -148,7 +149,7 @@ impl FileFinder {
|
||||||
) -> Option<ElementBox> {
|
) -> Option<ElementBox> {
|
||||||
self.labels_for_match(path_match, app).map(
|
self.labels_for_match(path_match, app).map(
|
||||||
|(file_name, file_name_positions, full_path, full_path_positions)| {
|
|(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 highlight_color = ColorU::from_u32(0x304ee2ff);
|
||||||
let bold = *Properties::new().weight(Weight::BOLD);
|
let bold = *Properties::new().weight(Weight::BOLD);
|
||||||
let mut container = Container::new(
|
let mut container = Container::new(
|
||||||
|
@ -292,8 +293,6 @@ impl FileFinder {
|
||||||
let query_buffer = ctx.add_view(|ctx| BufferView::single_line(settings.clone(), ctx));
|
let query_buffer = ctx.add_view(|ctx| BufferView::single_line(settings.clone(), ctx));
|
||||||
ctx.subscribe_to_view(&query_buffer, Self::on_query_buffer_event);
|
ctx.subscribe_to_view(&query_buffer, Self::on_query_buffer_event);
|
||||||
|
|
||||||
settings.notify_view_on_change(ctx);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
handle: ctx.handle().downgrade(),
|
handle: ctx.handle().downgrade(),
|
||||||
settings,
|
settings,
|
||||||
|
|
|
@ -9,6 +9,5 @@ mod sum_tree;
|
||||||
mod test;
|
mod test;
|
||||||
mod time;
|
mod time;
|
||||||
mod util;
|
mod util;
|
||||||
pub mod watch;
|
|
||||||
pub mod workspace;
|
pub mod workspace;
|
||||||
mod worktree;
|
mod worktree;
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use crate::{settings::Settings, watch::Receiver};
|
use crate::settings::Settings;
|
||||||
use gpui::{Menu, MenuItem};
|
use gpui::{Menu, MenuItem};
|
||||||
|
use postage::watch;
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
pub fn menus(settings: Receiver<Settings>) -> Vec<Menu<'static>> {
|
pub fn menus(settings: watch::Receiver<Settings>) -> Vec<Menu<'static>> {
|
||||||
vec![
|
vec![
|
||||||
Menu {
|
Menu {
|
||||||
name: "Zed",
|
name: "Zed",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::watch;
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use gpui::font_cache::{FamilyId, FontCache};
|
use gpui::font_cache::{FamilyId, FontCache};
|
||||||
|
use postage::watch;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
|
@ -26,5 +26,5 @@ impl Settings {
|
||||||
pub fn channel(
|
pub fn channel(
|
||||||
font_cache: &FontCache,
|
font_cache: &FontCache,
|
||||||
) -> Result<(watch::Sender<Settings>, watch::Receiver<Settings>)> {
|
) -> Result<(watch::Sender<Settings>, watch::Receiver<Settings>)> {
|
||||||
Ok(watch::channel(Settings::new(font_cache)?))
|
Ok(watch::channel_with(Settings::new(font_cache)?))
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<T> {
|
|
||||||
value: Arc<RwLock<T>>,
|
|
||||||
updated: channel::Sender<()>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Receiver<T> {
|
|
||||||
value: Arc<RwLock<T>>,
|
|
||||||
updated: channel::Receiver<()>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Sender<T> {
|
|
||||||
pub async fn update<R>(&mut self, f: impl FnOnce(&mut T) -> R) -> R {
|
|
||||||
let result = f(&mut *self.value.write().await);
|
|
||||||
self.updated.send(()).await.unwrap();
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Receiver<T> {
|
|
||||||
pub async fn updated(&self) {
|
|
||||||
let _ = self.updated.recv().await;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn read<'a>(&'a self) -> impl 'a + Deref<Target = T> {
|
|
||||||
self.value.read().await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: These implementations are broken because they only handle a single update.
|
|
||||||
impl<T: 'static + Clone> Receiver<T> {
|
|
||||||
pub fn notify_model_on_change<M: 'static + Entity>(&self, ctx: &mut ModelContext<M>) {
|
|
||||||
let watch = self.clone();
|
|
||||||
ctx.spawn(async move { watch.updated().await }, |_, _, ctx| {
|
|
||||||
ctx.notify()
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn notify_view_on_change<V: 'static + View>(&self, ctx: &mut ViewContext<V>) {
|
|
||||||
let watch = self.clone();
|
|
||||||
ctx.spawn(async move { watch.updated().await }, |_, _, ctx| {
|
|
||||||
ctx.notify()
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn channel<T>(value: T) -> (Sender<T>, Receiver<T>) {
|
|
||||||
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)
|
|
||||||
}
|
|
|
@ -3,11 +3,9 @@ pub mod pane_group;
|
||||||
pub use pane::*;
|
pub use pane::*;
|
||||||
pub use pane_group::*;
|
pub use pane_group::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::settings::Settings;
|
||||||
settings::Settings,
|
|
||||||
watch::{self, Receiver},
|
|
||||||
};
|
|
||||||
use gpui::{MutableAppContext, PathPromptOptions};
|
use gpui::{MutableAppContext, PathPromptOptions};
|
||||||
|
use postage::watch;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
pub fn init(app: &mut MutableAppContext) {
|
pub fn init(app: &mut MutableAppContext) {
|
||||||
app.add_global_action("workspace:open", open);
|
app.add_global_action("workspace:open", open);
|
||||||
|
@ -44,7 +42,7 @@ pub struct OpenParams {
|
||||||
pub settings: watch::Receiver<Settings>,
|
pub settings: watch::Receiver<Settings>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open(settings: &Receiver<Settings>, ctx: &mut MutableAppContext) {
|
fn open(settings: &watch::Receiver<Settings>, ctx: &mut MutableAppContext) {
|
||||||
let settings = settings.clone();
|
let settings = settings.clone();
|
||||||
ctx.prompt_for_paths(
|
ctx.prompt_for_paths(
|
||||||
PathPromptOptions {
|
PathPromptOptions {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use super::{ItemViewHandle, SplitDirection};
|
use super::{ItemViewHandle, SplitDirection};
|
||||||
use crate::{settings::Settings, watch};
|
use crate::settings::Settings;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
color::ColorU,
|
color::ColorU,
|
||||||
elements::*,
|
elements::*,
|
||||||
|
@ -7,6 +7,7 @@ use gpui::{
|
||||||
keymap::Binding,
|
keymap::Binding,
|
||||||
AppContext, Border, Entity, MutableAppContext, Quad, View, ViewContext,
|
AppContext, Border, Entity, MutableAppContext, Quad, View, ViewContext,
|
||||||
};
|
};
|
||||||
|
use postage::watch;
|
||||||
use std::{cmp, path::Path, sync::Arc};
|
use std::{cmp, path::Path, sync::Arc};
|
||||||
|
|
||||||
pub fn init(app: &mut MutableAppContext) {
|
pub fn init(app: &mut MutableAppContext) {
|
||||||
|
@ -185,7 +186,7 @@ impl Pane {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_tabs(&self, ctx: &AppContext) -> ElementBox {
|
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 border_color = ColorU::from_u32(0xdbdbdcff);
|
||||||
let line_height = ctx.font_cache().line_height(
|
let line_height = ctx.font_cache().line_height(
|
||||||
ctx.font_cache().default_font(settings.ui_font_family),
|
ctx.font_cache().default_font(settings.ui_font_family),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue