This commit is contained in:
Nathan Sobo 2023-08-12 01:11:12 -06:00
parent 5e36040533
commit 4b4b949972
2 changed files with 108 additions and 584 deletions

View file

@ -51,182 +51,26 @@ impl<V> Playground<V> {
fn workspace<V: 'static>(theme: &ThemeColors) -> impl Element<V> {
column()
.size(auto())
.fill(theme.base(0.5))
.text_color(theme.text(0.5))
.child(title_bar(theme))
.child(stage(theme))
.child(status_bar(theme))
// .size(auto())
// .fill(theme.base(0.5))
// .text_color(theme.text(0.5))
// .child(title_bar(theme))
// .child(stage(theme))
// .child(status_bar(theme))
}
fn title_bar<V: 'static>(theme: &ThemeColors) -> impl Element<V> {
row()
.fill(theme.base(0.2))
.justify(0.)
.width(auto())
.child(text("Zed Playground"))
}
fn stage<V: 'static>(theme: &ThemeColors) -> impl Element<V> {
row().fill(theme.surface(0.9))
}
fn status_bar<V: 'static>(theme: &ThemeColors) -> impl Element<V> {
row().fill(theme.surface(0.1))
}
pub trait DialogDelegate<V>: 'static {}
impl<V> DialogDelegate<V> for () {}
#[derive(Element)]
pub struct Dialog<V: 'static, D: DialogDelegate<V>> {
title: Cow<'static, str>,
description: Cow<'static, str>,
delegate: Option<Rc<RefCell<D>>>,
buttons: Vec<Box<dyn FnOnce() -> AnyElement<V>>>,
view_type: PhantomData<V>,
}
pub fn dialog<V>(
title: impl Into<Cow<'static, str>>,
description: impl Into<Cow<'static, str>>,
) -> Dialog<V, ()> {
Dialog {
title: title.into(),
description: description.into(),
delegate: None,
buttons: Vec::new(),
view_type: PhantomData,
}
}
impl<V, D: DialogDelegate<V>> Dialog<V, D> {
pub fn delegate(mut self, delegate: D) -> Dialog<V, D> {
let old_delegate = self.delegate.replace(Rc::new(RefCell::new(delegate)));
debug_assert!(old_delegate.is_none(), "delegate already set");
self
}
pub fn button<L, Data, H>(mut self, label: L, data: Data, handler: H) -> Self
where
L: 'static + Into<Cow<'static, str>>,
Data: 'static + Clone,
H: ClickHandler<V, Data>,
{
let label = label.into();
self.buttons.push(Box::new(move || {
button(label).data(data).click(handler).into_any()
}));
self
}
}
#[derive(Element)]
struct Button<V: 'static, D: 'static, H: ClickHandler<V, D>> {
label: Cow<'static, str>,
click_handler: Option<H>,
data: Option<D>,
view_type: PhantomData<V>,
}
pub trait ClickHandler<V, D>: 'static {
fn handle(&self, view: &mut V, data: &D, cx: &mut ViewContext<V>);
}
impl<V, M, F: 'static + Fn(&mut V, &M, &mut ViewContext<V>)> ClickHandler<V, M> for F {
fn handle(&self, view: &mut V, data: &M, cx: &mut ViewContext<V>) {
self(view, data, cx)
}
}
impl<V, D> ClickHandler<V, D> for () {
fn handle(&self, view: &mut V, data: &D, cx: &mut ViewContext<V>) {}
}
fn button<V>(label: impl Into<Cow<'static, str>>) -> Button<V, (), ()> {
Button {
label: label.into(),
click_handler: None,
data: None,
view_type: PhantomData,
}
}
impl<V, D, F> Button<V, D, F>
where
F: ClickHandler<V, D>,
{
fn render(&mut self, _: &mut V, _: &mut LayoutContext<V>) -> AnyElement<V> {
// TODO! Handle click etc
row().child(text(self.label.clone())).into_any()
}
}
// impl<V, D, F> Button<V, D, F>
// where
// V,
// F: ClickHandler<V, D>,
// {
// fn render(&mut self, _: &mut V, _: &mut LayoutContext<V>) -> impl Element<V> {
// // TODO! Handle click etc
// row()
// .fill(theme.colors.primary(5))
// .child(text(self.label.clone()).text_color(theme.colors.on_primary()))
// }
// fn title_bar<V: 'static>(theme: &ThemeColors) -> impl Element<V> {
// row()
// .fill(theme.base(0.2))
// .justify(0.)
// .width(auto())
// .child(text("Zed Playground"))
// }
// struct Tab<V> {
// active: bool,
// fn stage<V: 'static>(theme: &ThemeColors) -> impl Element<V> {
// row().fill(theme.surface(0.9))
// }
// impl<V> Tab<V>
// where
// V,
// {
// fn tab(&mut self, _: &mut V, _: &mut LayoutContext<V>) -> impl Element<V> {
// let theme = todo!();
// // TODO! Handle click etc
// row()
// .fill(theme.colors.neutral(6))
// .child(text(self.label.clone()).text_color(theme.colors.on_neutral()))
// }
// fn status_bar<V: 'static>(theme: &ThemeColors) -> impl Element<V> {
// row().fill(theme.surface(0.1))
// }
impl<V> Button<V, (), ()> {
fn data<D>(self, data: D) -> Button<V, D, ()>
where
D: 'static,
{
Button {
label: self.label,
click_handler: self.click_handler,
data: Some(data),
view_type: self.view_type,
}
}
}
impl<V, D> Button<V, D, ()> {
fn click<H>(self, handler: H) -> Button<V, D, H>
where
H: 'static + ClickHandler<V, D>,
{
Button {
label: self.label,
click_handler: Some(handler),
data: self.data,
view_type: self.view_type,
}
}
}
impl<V, D: DialogDelegate<V>> Dialog<V, D> {
pub fn render(&mut self, _: &mut V, _: &mut gpui::ViewContext<V>) -> AnyElement<V> {
column()
.child(text(self.title.clone()).text_size(lg()))
.child(text(self.description.clone()).margins((m4(), auto())))
.child(row().children(self.buttons.drain(..).map(|button| (button)())))
.into_any()
}
}