Ooh generics

This commit is contained in:
Conrad Irwin 2023-11-16 22:16:29 -07:00
parent 6d4276ea5f
commit 2182cb2656
2 changed files with 14 additions and 14 deletions

View file

@ -1,7 +1,7 @@
use anyhow::Result; use anyhow::Result;
use gpui::AssetSource; use gpui::AssetSource;
use gpui::{ use gpui::{
div, hsla, px, size, AnyView, Bounds, Div, Render, ViewContext, VisualContext, WindowBounds, div, px, size, AnyView, Bounds, Div, Render, ViewContext, VisualContext, WindowBounds,
WindowOptions, WindowOptions,
}; };
use settings::{default_settings, Settings, SettingsStore}; use settings::{default_settings, Settings, SettingsStore};

View file

@ -23,6 +23,7 @@ impl FocusableView for ContextMenu {
self.focus_handle.clone() self.focus_handle.clone()
} }
} }
impl Menu for ContextMenu {}
impl ContextMenu { impl ContextMenu {
pub fn new(cx: &mut WindowContext) -> Self { pub fn new(cx: &mut WindowContext) -> Self {
@ -81,25 +82,24 @@ impl Render for ContextMenu {
} }
} }
pub struct MenuHandle<V: 'static> { pub trait Menu: Render + EventEmitter<MenuEvent> + FocusableView {}
pub struct MenuHandle<V: 'static, M: Menu> {
id: Option<ElementId>, id: Option<ElementId>,
child_builder: Option<Box<dyn FnOnce(bool) -> AnyElement<V> + 'static>>, child_builder: Option<Box<dyn FnOnce(bool) -> AnyElement<V> + 'static>>,
menu_builder: Option<Rc<dyn Fn(&mut V, &mut ViewContext<V>) -> View<ContextMenu> + 'static>>, menu_builder: Option<Rc<dyn Fn(&mut V, &mut ViewContext<V>) -> View<M> + 'static>>,
anchor: Option<AnchorCorner>, anchor: Option<AnchorCorner>,
attach: Option<AnchorCorner>, attach: Option<AnchorCorner>,
} }
impl<V: 'static> MenuHandle<V> { impl<V: 'static, M: Menu> MenuHandle<V, M> {
pub fn id(mut self, id: impl Into<ElementId>) -> Self { pub fn id(mut self, id: impl Into<ElementId>) -> Self {
self.id = Some(id.into()); self.id = Some(id.into());
self self
} }
pub fn menu( pub fn menu(mut self, f: impl Fn(&mut V, &mut ViewContext<V>) -> View<M> + 'static) -> Self {
mut self,
f: impl Fn(&mut V, &mut ViewContext<V>) -> View<ContextMenu> + 'static,
) -> Self {
self.menu_builder = Some(Rc::new(f)); self.menu_builder = Some(Rc::new(f));
self self
} }
@ -123,7 +123,7 @@ impl<V: 'static> MenuHandle<V> {
} }
} }
pub fn menu_handle<V: 'static>() -> MenuHandle<V> { pub fn menu_handle<V: 'static, M: Menu>() -> MenuHandle<V, M> {
MenuHandle { MenuHandle {
id: None, id: None,
child_builder: None, child_builder: None,
@ -133,15 +133,15 @@ pub fn menu_handle<V: 'static>() -> MenuHandle<V> {
} }
} }
pub struct MenuHandleState<V> { pub struct MenuHandleState<V, M> {
menu: Rc<RefCell<Option<View<ContextMenu>>>>, menu: Rc<RefCell<Option<View<M>>>>,
position: Rc<RefCell<Point<Pixels>>>, position: Rc<RefCell<Point<Pixels>>>,
child_layout_id: Option<LayoutId>, child_layout_id: Option<LayoutId>,
child_element: Option<AnyElement<V>>, child_element: Option<AnyElement<V>>,
menu_element: Option<AnyElement<V>>, menu_element: Option<AnyElement<V>>,
} }
impl<V: 'static> Element<V> for MenuHandle<V> { impl<V: 'static, M: Menu> Element<V> for MenuHandle<V, M> {
type ElementState = MenuHandleState<V>; type ElementState = MenuHandleState<V, M>;
fn element_id(&self) -> Option<gpui::ElementId> { fn element_id(&self) -> Option<gpui::ElementId> {
Some(self.id.clone().expect("menu_handle must have an id()")) Some(self.id.clone().expect("menu_handle must have an id()"))
@ -255,7 +255,7 @@ impl<V: 'static> Element<V> for MenuHandle<V> {
} }
} }
impl<V: 'static> Component<V> for MenuHandle<V> { impl<V: 'static, M: Menu> Component<V> for MenuHandle<V, M> {
fn render(self) -> AnyElement<V> { fn render(self) -> AnyElement<V> {
AnyElement::new(self) AnyElement::new(self)
} }