+ManagedView
And some games with rust traits
This commit is contained in:
parent
2182cb2656
commit
2d1d75f482
8 changed files with 75 additions and 73 deletions
|
@ -4,8 +4,8 @@ use std::rc::Rc;
|
|||
use crate::prelude::*;
|
||||
use crate::{v_stack, Label, List, ListEntry, ListItem, ListSeparator, ListSubHeader};
|
||||
use gpui::{
|
||||
overlay, px, Action, AnchorCorner, AnyElement, Bounds, DispatchPhase, Div, EventEmitter,
|
||||
FocusHandle, FocusableView, LayoutId, MouseButton, MouseDownEvent, Pixels, Point, Render, View,
|
||||
overlay, px, Action, AnchorCorner, AnyElement, Bounds, Dismiss, DispatchPhase, Div,
|
||||
FocusHandle, LayoutId, ManagedView, MouseButton, MouseDownEvent, Pixels, Point, Render, View,
|
||||
};
|
||||
|
||||
pub struct ContextMenu {
|
||||
|
@ -13,17 +13,11 @@ pub struct ContextMenu {
|
|||
focus_handle: FocusHandle,
|
||||
}
|
||||
|
||||
pub enum MenuEvent {
|
||||
Dismissed,
|
||||
}
|
||||
|
||||
impl EventEmitter<MenuEvent> for ContextMenu {}
|
||||
impl FocusableView for ContextMenu {
|
||||
impl ManagedView for ContextMenu {
|
||||
fn focus_handle(&self, cx: &gpui::AppContext) -> FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
impl Menu for ContextMenu {}
|
||||
|
||||
impl ContextMenu {
|
||||
pub fn new(cx: &mut WindowContext) -> Self {
|
||||
|
@ -50,11 +44,11 @@ impl ContextMenu {
|
|||
|
||||
pub fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
|
||||
// todo!()
|
||||
cx.emit(MenuEvent::Dismissed);
|
||||
cx.emit(Dismiss);
|
||||
}
|
||||
|
||||
pub fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
|
||||
cx.emit(MenuEvent::Dismissed);
|
||||
cx.emit(Dismiss);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,9 +76,7 @@ impl Render for ContextMenu {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait Menu: Render + EventEmitter<MenuEvent> + FocusableView {}
|
||||
|
||||
pub struct MenuHandle<V: 'static, M: Menu> {
|
||||
pub struct MenuHandle<V: 'static, M: ManagedView> {
|
||||
id: Option<ElementId>,
|
||||
child_builder: Option<Box<dyn FnOnce(bool) -> AnyElement<V> + 'static>>,
|
||||
menu_builder: Option<Rc<dyn Fn(&mut V, &mut ViewContext<V>) -> View<M> + 'static>>,
|
||||
|
@ -93,7 +85,7 @@ pub struct MenuHandle<V: 'static, M: Menu> {
|
|||
attach: Option<AnchorCorner>,
|
||||
}
|
||||
|
||||
impl<V: 'static, M: Menu> MenuHandle<V, M> {
|
||||
impl<V: 'static, M: ManagedView> MenuHandle<V, M> {
|
||||
pub fn id(mut self, id: impl Into<ElementId>) -> Self {
|
||||
self.id = Some(id.into());
|
||||
self
|
||||
|
@ -123,7 +115,7 @@ impl<V: 'static, M: Menu> MenuHandle<V, M> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn menu_handle<V: 'static, M: Menu>() -> MenuHandle<V, M> {
|
||||
pub fn menu_handle<V: 'static, M: ManagedView>() -> MenuHandle<V, M> {
|
||||
MenuHandle {
|
||||
id: None,
|
||||
child_builder: None,
|
||||
|
@ -140,7 +132,7 @@ pub struct MenuHandleState<V, M> {
|
|||
child_element: Option<AnyElement<V>>,
|
||||
menu_element: Option<AnyElement<V>>,
|
||||
}
|
||||
impl<V: 'static, M: Menu> Element<V> for MenuHandle<V, M> {
|
||||
impl<V: 'static, M: ManagedView> Element<V> for MenuHandle<V, M> {
|
||||
type ElementState = MenuHandleState<V, M>;
|
||||
|
||||
fn element_id(&self) -> Option<gpui::ElementId> {
|
||||
|
@ -234,7 +226,7 @@ impl<V: 'static, M: Menu> 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 {
|
||||
MenuEvent::Dismissed => {
|
||||
&Dismiss => {
|
||||
*menu2.borrow_mut() = None;
|
||||
cx.notify();
|
||||
}
|
||||
|
@ -255,7 +247,7 @@ impl<V: 'static, M: Menu> Element<V> for MenuHandle<V, M> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<V: 'static, M: Menu> Component<V> for MenuHandle<V, M> {
|
||||
impl<V: 'static, M: ManagedView> Component<V> for MenuHandle<V, M> {
|
||||
fn render(self) -> AnyElement<V> {
|
||||
AnyElement::new(self)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue