Add ability to toggle user settings

This commit is contained in:
Marshall Bowers 2023-10-19 13:38:19 -04:00
parent 4aac733238
commit a869de3b1f
2 changed files with 59 additions and 15 deletions

View file

@ -1,7 +1,9 @@
use std::ops::Deref;
use std::sync::Arc;
use gpui3::{
rems, AbsoluteLength, AnyElement, BorrowAppContext, Bounds, LayoutId, Pixels, WindowContext,
rems, AbsoluteLength, AnyElement, BorrowAppContext, Bounds, Interactive, LayoutId, Pixels,
WindowContext,
};
use crate::prelude::*;
@ -11,6 +13,10 @@ pub fn user_settings(cx: &WindowContext) -> FakeSettings {
cx.global::<FakeSettings>().clone()
}
pub fn user_settings_mut<'cx>(cx: &'cx mut WindowContext) -> &'cx mut FakeSettings {
cx.global_mut::<FakeSettings>()
}
#[derive(Clone)]
pub enum SettingValue<T> {
UserDefined(T),
@ -143,3 +149,32 @@ impl<E: Element> Element for WithSettings<E> {
});
}
}
impl<E: Element + Interactive> Interactive for WithSettings<E> {
fn listeners(&mut self) -> &mut gpui3::EventListeners<Self::ViewState> {
self.child.listeners()
}
fn on_mouse_down(
mut self,
button: gpui3::MouseButton,
handler: impl Fn(&mut Self::ViewState, &gpui3::MouseDownEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
) -> Self
where
Self: Sized,
{
println!("WithSettings on_mouse_down");
let settings = self.settings.clone();
self.listeners()
.mouse_down
.push(Arc::new(move |view, event, bounds, phase, cx| {
cx.with_global(settings.clone(), |cx| handler(view, event, cx))
}));
self
}
}