Merge branch 'main' into managed-view-adjustment
This commit is contained in:
commit
149b9d1aa6
18 changed files with 330 additions and 179 deletions
|
@ -5,59 +5,90 @@ use crate::prelude::*;
|
|||
use crate::{v_stack, Label, List, ListEntry, ListItem, ListSeparator, ListSubHeader};
|
||||
use gpui::{
|
||||
overlay, px, Action, AnchorCorner, AnyElement, AppContext, Bounds, DispatchPhase, Div,
|
||||
EventEmitter, FocusHandle, FocusableView, LayoutId, ManagedEvent, ManagedView, MouseButton,
|
||||
MouseDownEvent, Pixels, Point, Render, View,
|
||||
EventEmitter, FocusHandle, FocusableView, LayoutId, ManagedView, Manager, MouseButton,
|
||||
MouseDownEvent, Pixels, Point, Render, View, VisualContext, WeakView,
|
||||
};
|
||||
|
||||
pub struct ContextMenu {
|
||||
items: Vec<ListItem>,
|
||||
focus_handle: FocusHandle,
|
||||
pub enum ContextMenuItem<V> {
|
||||
Separator(ListSeparator),
|
||||
Header(ListSubHeader),
|
||||
Entry(
|
||||
ListEntry<ContextMenu<V>>,
|
||||
Rc<dyn Fn(&mut V, &mut ViewContext<V>)>,
|
||||
),
|
||||
}
|
||||
|
||||
impl FocusableView for ContextMenu {
|
||||
pub struct ContextMenu<V> {
|
||||
items: Vec<ContextMenuItem<V>>,
|
||||
focus_handle: FocusHandle,
|
||||
handle: WeakView<V>,
|
||||
}
|
||||
|
||||
impl<V: Render> FocusableView for ContextMenu<V> {
|
||||
fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<ManagedEvent> for ContextMenu {}
|
||||
impl<V: Render> EventEmitter<Manager> for ContextMenu<V> {}
|
||||
|
||||
impl ContextMenu {
|
||||
pub fn new(cx: &mut WindowContext) -> Self {
|
||||
Self {
|
||||
items: Default::default(),
|
||||
focus_handle: cx.focus_handle(),
|
||||
}
|
||||
impl<V: Render> ContextMenu<V> {
|
||||
pub fn build(
|
||||
cx: &mut ViewContext<V>,
|
||||
f: impl FnOnce(Self, &mut ViewContext<Self>) -> Self,
|
||||
) -> View<Self> {
|
||||
let handle = cx.view().downgrade();
|
||||
cx.build_view(|cx| {
|
||||
f(
|
||||
Self {
|
||||
handle,
|
||||
items: Default::default(),
|
||||
focus_handle: cx.focus_handle(),
|
||||
},
|
||||
cx,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn header(mut self, title: impl Into<SharedString>) -> Self {
|
||||
self.items.push(ListItem::Header(ListSubHeader::new(title)));
|
||||
self.items
|
||||
.push(ContextMenuItem::Header(ListSubHeader::new(title)));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn separator(mut self) -> Self {
|
||||
self.items.push(ListItem::Separator(ListSeparator));
|
||||
self.items.push(ContextMenuItem::Separator(ListSeparator));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn entry(mut self, label: Label, action: Box<dyn Action>) -> Self {
|
||||
self.items.push(ListEntry::new(label).action(action).into());
|
||||
pub fn entry(
|
||||
mut self,
|
||||
view: ListEntry<Self>,
|
||||
on_click: impl Fn(&mut V, &mut ViewContext<V>) + 'static,
|
||||
) -> Self {
|
||||
self.items
|
||||
.push(ContextMenuItem::Entry(view, Rc::new(on_click)));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn action(self, view: ListEntry<Self>, action: Box<dyn Action>) -> Self {
|
||||
// todo: add the keybindings to the list entry
|
||||
self.entry(view, move |_, cx| cx.dispatch_action(action.boxed_clone()))
|
||||
}
|
||||
|
||||
pub fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
|
||||
// todo!()
|
||||
cx.emit(ManagedEvent::Dismiss);
|
||||
cx.emit(Manager::Dismiss);
|
||||
}
|
||||
|
||||
pub fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
|
||||
cx.emit(ManagedEvent::Dismiss);
|
||||
cx.emit(Manager::Dismiss);
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for ContextMenu {
|
||||
impl<V: Render> Render for ContextMenu<V> {
|
||||
type Element = Div<Self>;
|
||||
// todo!()
|
||||
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
|
||||
div().elevation_2(cx).flex().flex_row().child(
|
||||
v_stack()
|
||||
|
@ -74,7 +105,25 @@ impl Render for ContextMenu {
|
|||
// .bg(cx.theme().colors().elevated_surface_background)
|
||||
// .border()
|
||||
// .border_color(cx.theme().colors().border)
|
||||
.child(List::new(self.items.clone())),
|
||||
.child(List::new(
|
||||
self.items
|
||||
.iter()
|
||||
.map(|item| match item {
|
||||
ContextMenuItem::Separator(separator) => {
|
||||
ListItem::Separator(separator.clone())
|
||||
}
|
||||
ContextMenuItem::Header(header) => ListItem::Header(header.clone()),
|
||||
ContextMenuItem::Entry(entry, callback) => {
|
||||
let callback = callback.clone();
|
||||
let handle = self.handle.clone();
|
||||
ListItem::Entry(entry.clone().on_click(move |this, cx| {
|
||||
handle.update(cx, |view, cx| callback(view, cx)).ok();
|
||||
cx.emit(Manager::Dismiss);
|
||||
}))
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -229,12 +278,13 @@ impl<V: 'static, M: ManagedView> Element<V> for MenuHandle<V, M> {
|
|||
let new_menu = (builder)(view_state, cx);
|
||||
let menu2 = menu.clone();
|
||||
cx.subscribe(&new_menu, move |this, modal, e, cx| match e {
|
||||
&ManagedEvent::Dismiss => {
|
||||
&Manager::Dismiss => {
|
||||
*menu2.borrow_mut() = None;
|
||||
cx.notify();
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
cx.focus_view(&new_menu);
|
||||
*menu.borrow_mut() = Some(new_menu);
|
||||
|
||||
*position.borrow_mut() = if attach.is_some() && child_layout_id.is_some() {
|
||||
|
@ -263,16 +313,25 @@ pub use stories::*;
|
|||
mod stories {
|
||||
use super::*;
|
||||
use crate::story::Story;
|
||||
use gpui::{actions, Div, Render, VisualContext};
|
||||
use gpui::{actions, Div, Render};
|
||||
|
||||
actions!(PrintCurrentDate);
|
||||
actions!(PrintCurrentDate, PrintBestFood);
|
||||
|
||||
fn build_menu(cx: &mut WindowContext, header: impl Into<SharedString>) -> View<ContextMenu> {
|
||||
cx.build_view(|cx| {
|
||||
ContextMenu::new(cx).header(header).separator().entry(
|
||||
Label::new("Print current time"),
|
||||
PrintCurrentDate.boxed_clone(),
|
||||
)
|
||||
fn build_menu<V: Render>(
|
||||
cx: &mut ViewContext<V>,
|
||||
header: impl Into<SharedString>,
|
||||
) -> View<ContextMenu<V>> {
|
||||
let handle = cx.view().clone();
|
||||
ContextMenu::build(cx, |menu, _| {
|
||||
menu.header(header)
|
||||
.separator()
|
||||
.entry(ListEntry::new(Label::new("Print current time")), |v, cx| {
|
||||
println!("dispatching PrintCurrentTime action");
|
||||
cx.dispatch_action(PrintCurrentDate.boxed_clone())
|
||||
})
|
||||
.entry(ListEntry::new(Label::new("Print best food")), |v, cx| {
|
||||
cx.dispatch_action(PrintBestFood.boxed_clone())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -284,10 +343,14 @@ mod stories {
|
|||
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
|
||||
Story::container(cx)
|
||||
.on_action(|_, _: &PrintCurrentDate, _| {
|
||||
println!("printing unix time!");
|
||||
if let Ok(unix_time) = std::time::UNIX_EPOCH.elapsed() {
|
||||
println!("Current Unix time is {:?}", unix_time.as_secs());
|
||||
}
|
||||
})
|
||||
.on_action(|_, _: &PrintBestFood, _| {
|
||||
println!("burrito");
|
||||
})
|
||||
.flex()
|
||||
.flex_row()
|
||||
.justify_between()
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
use gpui::{div, Action};
|
||||
use std::rc::Rc;
|
||||
|
||||
use gpui::{div, Div, Stateful, StatefulInteractiveComponent};
|
||||
|
||||
use crate::settings::user_settings;
|
||||
use crate::{
|
||||
|
@ -172,35 +174,35 @@ pub enum ListEntrySize {
|
|||
Medium,
|
||||
}
|
||||
|
||||
#[derive(Component, Clone)]
|
||||
pub enum ListItem {
|
||||
Entry(ListEntry),
|
||||
#[derive(Clone)]
|
||||
pub enum ListItem<V: 'static> {
|
||||
Entry(ListEntry<V>),
|
||||
Separator(ListSeparator),
|
||||
Header(ListSubHeader),
|
||||
}
|
||||
|
||||
impl From<ListEntry> for ListItem {
|
||||
fn from(entry: ListEntry) -> Self {
|
||||
impl<V: 'static> From<ListEntry<V>> for ListItem<V> {
|
||||
fn from(entry: ListEntry<V>) -> Self {
|
||||
Self::Entry(entry)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ListSeparator> for ListItem {
|
||||
impl<V: 'static> From<ListSeparator> for ListItem<V> {
|
||||
fn from(entry: ListSeparator) -> Self {
|
||||
Self::Separator(entry)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ListSubHeader> for ListItem {
|
||||
impl<V: 'static> From<ListSubHeader> for ListItem<V> {
|
||||
fn from(entry: ListSubHeader) -> Self {
|
||||
Self::Header(entry)
|
||||
}
|
||||
}
|
||||
|
||||
impl ListItem {
|
||||
fn render<V: 'static>(self, view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
|
||||
impl<V: 'static> ListItem<V> {
|
||||
fn render(self, view: &mut V, ix: usize, cx: &mut ViewContext<V>) -> impl Component<V> {
|
||||
match self {
|
||||
ListItem::Entry(entry) => div().child(entry.render(view, cx)),
|
||||
ListItem::Entry(entry) => div().child(entry.render(ix, cx)),
|
||||
ListItem::Separator(separator) => div().child(separator.render(view, cx)),
|
||||
ListItem::Header(header) => div().child(header.render(view, cx)),
|
||||
}
|
||||
|
@ -210,7 +212,7 @@ impl ListItem {
|
|||
Self::Entry(ListEntry::new(label))
|
||||
}
|
||||
|
||||
pub fn as_entry(&mut self) -> Option<&mut ListEntry> {
|
||||
pub fn as_entry(&mut self) -> Option<&mut ListEntry<V>> {
|
||||
if let Self::Entry(entry) = self {
|
||||
Some(entry)
|
||||
} else {
|
||||
|
@ -219,8 +221,7 @@ impl ListItem {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct ListEntry {
|
||||
pub struct ListEntry<V> {
|
||||
disabled: bool,
|
||||
// TODO: Reintroduce this
|
||||
// disclosure_control_style: DisclosureControlVisibility,
|
||||
|
@ -231,15 +232,13 @@ pub struct ListEntry {
|
|||
size: ListEntrySize,
|
||||
toggle: Toggle,
|
||||
variant: ListItemVariant,
|
||||
on_click: Option<Box<dyn Action>>,
|
||||
on_click: Option<Rc<dyn Fn(&mut V, &mut ViewContext<V>) + 'static>>,
|
||||
}
|
||||
|
||||
impl Clone for ListEntry {
|
||||
impl<V> Clone for ListEntry<V> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
disabled: self.disabled,
|
||||
// TODO: Reintroduce this
|
||||
// disclosure_control_style: DisclosureControlVisibility,
|
||||
indent_level: self.indent_level,
|
||||
label: self.label.clone(),
|
||||
left_slot: self.left_slot.clone(),
|
||||
|
@ -247,12 +246,12 @@ impl Clone for ListEntry {
|
|||
size: self.size,
|
||||
toggle: self.toggle,
|
||||
variant: self.variant,
|
||||
on_click: self.on_click.as_ref().map(|opt| opt.boxed_clone()),
|
||||
on_click: self.on_click.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ListEntry {
|
||||
impl<V: 'static> ListEntry<V> {
|
||||
pub fn new(label: Label) -> Self {
|
||||
Self {
|
||||
disabled: false,
|
||||
|
@ -267,8 +266,8 @@ impl ListEntry {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn action(mut self, action: impl Into<Box<dyn Action>>) -> Self {
|
||||
self.on_click = Some(action.into());
|
||||
pub fn on_click(mut self, handler: impl Fn(&mut V, &mut ViewContext<V>) + 'static) -> Self {
|
||||
self.on_click = Some(Rc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -307,7 +306,7 @@ impl ListEntry {
|
|||
self
|
||||
}
|
||||
|
||||
fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
|
||||
fn render(self, ix: usize, cx: &mut ViewContext<V>) -> Stateful<V, Div<V>> {
|
||||
let settings = user_settings(cx);
|
||||
|
||||
let left_content = match self.left_slot.clone() {
|
||||
|
@ -328,21 +327,21 @@ impl ListEntry {
|
|||
ListEntrySize::Medium => div().h_7(),
|
||||
};
|
||||
div()
|
||||
.id(ix)
|
||||
.relative()
|
||||
.hover(|mut style| {
|
||||
style.background = Some(cx.theme().colors().editor_background.into());
|
||||
style
|
||||
})
|
||||
.on_mouse_down(gpui::MouseButton::Left, {
|
||||
let action = self.on_click.map(|action| action.boxed_clone());
|
||||
.on_click({
|
||||
let on_click = self.on_click.clone();
|
||||
|
||||
move |entry: &mut V, event, cx| {
|
||||
if let Some(action) = action.as_ref() {
|
||||
cx.dispatch_action(action.boxed_clone());
|
||||
move |view: &mut V, event, cx| {
|
||||
if let Some(on_click) = &on_click {
|
||||
(on_click)(view, cx)
|
||||
}
|
||||
}
|
||||
})
|
||||
.group("")
|
||||
.bg(cx.theme().colors().surface_background)
|
||||
// TODO: Add focus state
|
||||
// .when(self.state == InteractionState::Focused, |this| {
|
||||
|
@ -391,8 +390,8 @@ impl ListSeparator {
|
|||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct List {
|
||||
items: Vec<ListItem>,
|
||||
pub struct List<V: 'static> {
|
||||
items: Vec<ListItem<V>>,
|
||||
/// Message to display when the list is empty
|
||||
/// Defaults to "No items"
|
||||
empty_message: SharedString,
|
||||
|
@ -400,8 +399,8 @@ pub struct List {
|
|||
toggle: Toggle,
|
||||
}
|
||||
|
||||
impl List {
|
||||
pub fn new(items: Vec<ListItem>) -> Self {
|
||||
impl<V: 'static> List<V> {
|
||||
pub fn new(items: Vec<ListItem<V>>) -> Self {
|
||||
Self {
|
||||
items,
|
||||
empty_message: "No items".into(),
|
||||
|
@ -425,9 +424,14 @@ impl List {
|
|||
self
|
||||
}
|
||||
|
||||
fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
|
||||
fn render(self, view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
|
||||
let list_content = match (self.items.is_empty(), self.toggle) {
|
||||
(false, _) => div().children(self.items),
|
||||
(false, _) => div().children(
|
||||
self.items
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(ix, item)| item.render(view, ix, cx)),
|
||||
),
|
||||
(true, Toggle::Toggled(false)) => div(),
|
||||
(true, _) => {
|
||||
div().child(Label::new(self.empty_message.clone()).color(TextColor::Muted))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue