Merge branch 'main' into nate/gpui2-ui-components

This commit is contained in:
Nate Butler 2023-09-19 00:26:41 -04:00
commit 26f442a675
77 changed files with 3152 additions and 406 deletions

View file

@ -684,23 +684,41 @@ impl AppContext {
);
},
);
fn inner(
this: &mut AppContext,
name: &'static str,
deserializer: fn(serde_json::Value) -> anyhow::Result<Box<dyn Action>>,
action_id: TypeId,
view_id: TypeId,
handler: Box<ActionCallback>,
capture: bool,
) {
this.action_deserializers
.entry(name)
.or_insert((action_id.clone(), deserializer));
self.action_deserializers
.entry(A::qualified_name())
.or_insert((TypeId::of::<A>(), A::from_json_str));
let actions = if capture {
&mut this.capture_actions
} else {
&mut this.actions
};
let actions = if capture {
&mut self.capture_actions
} else {
&mut self.actions
};
actions
.entry(TypeId::of::<V>())
.or_default()
.entry(TypeId::of::<A>())
.or_default()
.push(handler);
actions
.entry(view_id)
.or_default()
.entry(action_id)
.or_default()
.push(handler);
}
inner(
self,
A::qualified_name(),
A::from_json_str,
TypeId::of::<A>(),
TypeId::of::<V>(),
handler,
capture,
);
}
pub fn add_async_action<A, V, F>(&mut self, mut handler: F)

View file

@ -2,6 +2,4 @@ mod select;
pub use select::{ItemType, Select, SelectStyle};
pub fn init(cx: &mut super::AppContext) {
select::init(cx);
}
pub fn init(_: &mut super::AppContext) {}

View file

@ -1,13 +1,12 @@
use serde::Deserialize;
use crate::{
actions, elements::*, impl_actions, platform::MouseButton, AppContext, Entity, View,
ViewContext, WeakViewHandle,
elements::*,
platform::{CursorStyle, MouseButton},
AppContext, Entity, View, ViewContext, WeakViewHandle,
};
pub struct Select {
handle: WeakViewHandle<Self>,
render_item: Box<dyn Fn(usize, ItemType, bool, &AppContext) -> AnyElement<Self>>,
render_item: Box<dyn Fn(usize, ItemType, bool, &mut ViewContext<Select>) -> AnyElement<Self>>,
selected_item_ix: usize,
item_count: usize,
is_open: bool,
@ -27,21 +26,12 @@ pub enum ItemType {
Unselected,
}
#[derive(Clone, Deserialize, PartialEq)]
pub struct SelectItem(pub usize);
actions!(select, [ToggleSelect]);
impl_actions!(select, [SelectItem]);
pub enum Event {}
pub fn init(cx: &mut AppContext) {
cx.add_action(Select::toggle);
cx.add_action(Select::select_item);
}
impl Select {
pub fn new<F: 'static + Fn(usize, ItemType, bool, &AppContext) -> AnyElement<Self>>(
pub fn new<
F: 'static + Fn(usize, ItemType, bool, &mut ViewContext<Self>) -> AnyElement<Self>,
>(
item_count: usize,
cx: &mut ViewContext<Self>,
render_item: F,
@ -67,13 +57,13 @@ impl Select {
cx.notify();
}
fn toggle(&mut self, _: &ToggleSelect, cx: &mut ViewContext<Self>) {
fn toggle(&mut self, cx: &mut ViewContext<Self>) {
self.is_open = !self.is_open;
cx.notify();
}
fn select_item(&mut self, action: &SelectItem, cx: &mut ViewContext<Self>) {
self.selected_item_ix = action.0;
pub fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Self>) {
self.selected_item_ix = ix;
self.is_open = false;
cx.notify();
}
@ -116,8 +106,9 @@ impl View for Select {
.contained()
.with_style(style.header)
})
.with_cursor_style(CursorStyle::PointingHand)
.on_click(MouseButton::Left, move |_, this, cx| {
this.toggle(&Default::default(), cx);
this.toggle(cx);
}),
);
if self.is_open {
@ -142,8 +133,9 @@ impl View for Select {
cx,
)
})
.with_cursor_style(CursorStyle::PointingHand)
.on_click(MouseButton::Left, move |_, this, cx| {
this.select_item(&SelectItem(ix), cx);
this.set_selected_index(ix, cx);
})
.into_any()
}))