Position pane new, split, and dock context menus

Co-Authored-By: Julia <30666851+ForLoveOfCats@users.noreply.github.com>
This commit is contained in:
Petros Amoiridis 2023-03-20 17:49:33 +02:00
parent 3bddf01962
commit 24bbca7326
No known key found for this signature in database

View file

@ -82,19 +82,13 @@ pub struct GoForward {
} }
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub struct DeploySplitMenu { pub struct DeploySplitMenu;
position: Vector2F,
}
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub struct DeployDockMenu { pub struct DeployDockMenu;
position: Vector2F,
}
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub struct DeployNewMenu { pub struct DeployNewMenu;
position: Vector2F,
}
impl_actions!(pane, [GoBack, GoForward, ActivateItem]); impl_actions!(pane, [GoBack, GoForward, ActivateItem]);
impl_internal_actions!( impl_internal_actions!(
@ -215,7 +209,7 @@ pub struct Pane {
autoscroll: bool, autoscroll: bool,
nav_history: Rc<RefCell<NavHistory>>, nav_history: Rc<RefCell<NavHistory>>,
toolbar: ViewHandle<Toolbar>, toolbar: ViewHandle<Toolbar>,
tab_bar_context_menu: ViewHandle<ContextMenu>, tab_bar_context_menu: TabBarContextMenu,
docked: Option<DockAnchor>, docked: Option<DockAnchor>,
_background_actions: BackgroundActions, _background_actions: BackgroundActions,
_workspace_id: usize, _workspace_id: usize,
@ -274,6 +268,27 @@ enum ItemType {
All, All,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TabBarContextMenuKind {
New,
Split,
Dock,
}
struct TabBarContextMenu {
kind: TabBarContextMenuKind,
handle: ViewHandle<ContextMenu>,
}
impl TabBarContextMenu {
fn handle_if_kind(&self, kind: TabBarContextMenuKind) -> Option<ViewHandle<ContextMenu>> {
if self.kind == kind {
return Some(self.handle.clone());
}
None
}
}
impl Pane { impl Pane {
pub fn new( pub fn new(
workspace_id: usize, workspace_id: usize,
@ -283,6 +298,10 @@ impl Pane {
) -> Self { ) -> Self {
let handle = cx.weak_handle(); let handle = cx.weak_handle();
let context_menu = cx.add_view(ContextMenu::new); let context_menu = cx.add_view(ContextMenu::new);
context_menu.update(cx, |menu, _| {
menu.set_position_mode(OverlayPositionMode::Local)
});
Self { Self {
items: Vec::new(), items: Vec::new(),
activation_history: Vec::new(), activation_history: Vec::new(),
@ -299,7 +318,10 @@ impl Pane {
pane: handle.clone(), pane: handle.clone(),
})), })),
toolbar: cx.add_view(|_| Toolbar::new(handle)), toolbar: cx.add_view(|_| Toolbar::new(handle)),
tab_bar_context_menu: context_menu, tab_bar_context_menu: TabBarContextMenu {
kind: TabBarContextMenuKind::New,
handle: context_menu,
},
docked, docked,
_background_actions: background_actions, _background_actions: background_actions,
_workspace_id: workspace_id, _workspace_id: workspace_id,
@ -1076,10 +1098,10 @@ impl Pane {
cx.emit(Event::Split(direction)); cx.emit(Event::Split(direction));
} }
fn deploy_split_menu(&mut self, action: &DeploySplitMenu, cx: &mut ViewContext<Self>) { fn deploy_split_menu(&mut self, _: &DeploySplitMenu, cx: &mut ViewContext<Self>) {
self.tab_bar_context_menu.update(cx, |menu, cx| { self.tab_bar_context_menu.handle.update(cx, |menu, cx| {
menu.show( menu.show(
action.position, Default::default(),
AnchorCorner::TopRight, AnchorCorner::TopRight,
vec![ vec![
ContextMenuItem::item("Split Right", SplitRight), ContextMenuItem::item("Split Right", SplitRight),
@ -1090,12 +1112,14 @@ impl Pane {
cx, cx,
); );
}); });
self.tab_bar_context_menu.kind = TabBarContextMenuKind::Split;
} }
fn deploy_dock_menu(&mut self, action: &DeployDockMenu, cx: &mut ViewContext<Self>) { fn deploy_dock_menu(&mut self, _: &DeployDockMenu, cx: &mut ViewContext<Self>) {
self.tab_bar_context_menu.update(cx, |menu, cx| { self.tab_bar_context_menu.handle.update(cx, |menu, cx| {
menu.show( menu.show(
action.position, Default::default(),
AnchorCorner::TopRight, AnchorCorner::TopRight,
vec![ vec![
ContextMenuItem::item("Anchor Dock Right", AnchorDockRight), ContextMenuItem::item("Anchor Dock Right", AnchorDockRight),
@ -1105,12 +1129,14 @@ impl Pane {
cx, cx,
); );
}); });
self.tab_bar_context_menu.kind = TabBarContextMenuKind::Dock;
} }
fn deploy_new_menu(&mut self, action: &DeployNewMenu, cx: &mut ViewContext<Self>) { fn deploy_new_menu(&mut self, _: &DeployNewMenu, cx: &mut ViewContext<Self>) {
self.tab_bar_context_menu.update(cx, |menu, cx| { self.tab_bar_context_menu.handle.update(cx, |menu, cx| {
menu.show( menu.show(
action.position, Default::default(),
AnchorCorner::TopRight, AnchorCorner::TopRight,
vec![ vec![
ContextMenuItem::item("New File", NewFile), ContextMenuItem::item("New File", NewFile),
@ -1120,6 +1146,8 @@ impl Pane {
cx, cx,
); );
}); });
self.tab_bar_context_menu.kind = TabBarContextMenuKind::New;
} }
pub fn toolbar(&self) -> &ViewHandle<Toolbar> { pub fn toolbar(&self) -> &ViewHandle<Toolbar> {
@ -1398,28 +1426,45 @@ impl Pane {
) -> ElementBox { ) -> ElementBox {
Flex::row() Flex::row()
// New menu // New menu
.with_child(tab_bar_button(0, "icons/plus_12.svg", cx, |position| { .with_child(render_tab_bar_button(
DeployNewMenu { position } 0,
})) "icons/plus_12.svg",
cx,
DeployNewMenu,
self.tab_bar_context_menu
.handle_if_kind(TabBarContextMenuKind::New),
))
.with_child( .with_child(
self.docked self.docked
.map(|anchor| { .map(|anchor| {
// Add the dock menu button if this pane is a dock // Add the dock menu button if this pane is a dock
let dock_icon = icon_for_dock_anchor(anchor); let dock_icon = icon_for_dock_anchor(anchor);
tab_bar_button(1, dock_icon, cx, |position| DeployDockMenu { position }) render_tab_bar_button(
1,
dock_icon,
cx,
DeployDockMenu,
self.tab_bar_context_menu
.handle_if_kind(TabBarContextMenuKind::Dock),
)
}) })
.unwrap_or_else(|| { .unwrap_or_else(|| {
// Add the split menu if this pane is not a dock // Add the split menu if this pane is not a dock
tab_bar_button(2, "icons/split_12.svg", cx, |position| DeploySplitMenu { render_tab_bar_button(
position, 2,
}) "icons/split_12.svg",
cx,
DeploySplitMenu,
self.tab_bar_context_menu
.handle_if_kind(TabBarContextMenuKind::Split),
)
}), }),
) )
// Add the close dock button if this pane is a dock // Add the close dock button if this pane is a dock
.with_children( .with_children(
self.docked self.docked
.map(|_| tab_bar_button(3, "icons/x_mark_8.svg", cx, |_| HideDock)), .map(|_| render_tab_bar_button(3, "icons/x_mark_8.svg", cx, HideDock, None)),
) )
.contained() .contained()
.with_style(theme.workspace.tab_bar.pane_button_container) .with_style(theme.workspace.tab_bar.pane_button_container)
@ -1554,7 +1599,6 @@ impl View for Pane {
}) })
.boxed(), .boxed(),
) )
.with_child(ChildView::new(&self.tab_bar_context_menu, cx).boxed())
.named("pane") .named("pane")
} }
@ -1575,7 +1619,7 @@ impl View for Pane {
} }
cx.focus(active_item); cx.focus(active_item);
} else if focused != self.tab_bar_context_menu { } else if focused != self.tab_bar_context_menu.handle {
self.last_focused_view_by_item self.last_focused_view_by_item
.insert(active_item.id(), focused.downgrade()); .insert(active_item.id(), focused.downgrade());
} }
@ -1591,14 +1635,17 @@ impl View for Pane {
} }
} }
fn tab_bar_button<A: Action>( fn render_tab_bar_button<A: Action + Clone>(
index: usize, index: usize,
icon: &'static str, icon: &'static str,
cx: &mut RenderContext<Pane>, cx: &mut RenderContext<Pane>,
action_builder: impl 'static + Fn(Vector2F) -> A, action: A,
context_menu: Option<ViewHandle<ContextMenu>>,
) -> ElementBox { ) -> ElementBox {
enum TabBarButton {} enum TabBarButton {}
Stack::new()
.with_child(
MouseEventHandler::<TabBarButton>::new(index, cx, |mouse_state, cx| { MouseEventHandler::<TabBarButton>::new(index, cx, |mouse_state, cx| {
let theme = &cx.global::<Settings>().theme.workspace.tab_bar; let theme = &cx.global::<Settings>().theme.workspace.tab_bar;
let style = theme.pane_button.style_for(mouse_state, false); let style = theme.pane_button.style_for(mouse_state, false);
@ -1610,13 +1657,17 @@ fn tab_bar_button<A: Action>(
.constrained() .constrained()
.with_width(style.button_width) .with_width(style.button_width)
.with_height(style.button_width) .with_height(style.button_width)
// .aligned()
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(MouseButton::Left, move |e, cx| { .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_action(action_builder(e.region.lower_right())); cx.dispatch_action(action.clone());
}) })
.boxed(),
)
.with_children(
context_menu.map(|menu| ChildView::new(menu, cx).aligned().bottom().right().boxed()),
)
.flex(1., false) .flex(1., false)
.boxed() .boxed()
} }