Get project compiling with type-safe actions
This commit is contained in:
+27
-31
@@ -1,6 +1,7 @@
|
||||
use super::{ItemViewHandle, SplitDirection};
|
||||
use crate::{settings::Settings, theme};
|
||||
use gpui::{
|
||||
action,
|
||||
color::Color,
|
||||
elements::*,
|
||||
geometry::{rect::RectF, vector::vec2f},
|
||||
@@ -11,46 +12,41 @@ use gpui::{
|
||||
use postage::watch;
|
||||
use std::{cmp, path::Path, sync::Arc};
|
||||
|
||||
action!(Split, SplitDirection);
|
||||
action!(ActivateItem, usize);
|
||||
action!(ActivatePrevItem);
|
||||
action!(ActivateNextItem);
|
||||
action!(CloseActiveItem);
|
||||
action!(CloseItem, usize);
|
||||
|
||||
pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_action(
|
||||
"pane:activate_item",
|
||||
|pane: &mut Pane, index: &usize, cx| {
|
||||
pane.activate_item(*index, cx);
|
||||
},
|
||||
);
|
||||
cx.add_action("pane:activate_prev_item", |pane: &mut Pane, _: &(), cx| {
|
||||
cx.add_action(|pane: &mut Pane, action: &ActivateItem, cx| {
|
||||
pane.activate_item(action.0, cx);
|
||||
});
|
||||
cx.add_action(|pane: &mut Pane, _: &ActivatePrevItem, cx| {
|
||||
pane.activate_prev_item(cx);
|
||||
});
|
||||
cx.add_action("pane:activate_next_item", |pane: &mut Pane, _: &(), cx| {
|
||||
cx.add_action(|pane: &mut Pane, _: &ActivateNextItem, cx| {
|
||||
pane.activate_next_item(cx);
|
||||
});
|
||||
cx.add_action("pane:close_active_item", |pane: &mut Pane, _: &(), cx| {
|
||||
cx.add_action(|pane: &mut Pane, _: &CloseActiveItem, cx| {
|
||||
pane.close_active_item(cx);
|
||||
});
|
||||
cx.add_action("pane:close_item", |pane: &mut Pane, item_id: &usize, cx| {
|
||||
pane.close_item(*item_id, cx);
|
||||
cx.add_action(|pane: &mut Pane, action: &CloseItem, cx| {
|
||||
pane.close_item(action.0, cx);
|
||||
});
|
||||
cx.add_action("pane:split_up", |pane: &mut Pane, _: &(), cx| {
|
||||
pane.split(SplitDirection::Up, cx);
|
||||
});
|
||||
cx.add_action("pane:split_down", |pane: &mut Pane, _: &(), cx| {
|
||||
pane.split(SplitDirection::Down, cx);
|
||||
});
|
||||
cx.add_action("pane:split_left", |pane: &mut Pane, _: &(), cx| {
|
||||
pane.split(SplitDirection::Left, cx);
|
||||
});
|
||||
cx.add_action("pane:split_right", |pane: &mut Pane, _: &(), cx| {
|
||||
pane.split(SplitDirection::Right, cx);
|
||||
cx.add_action(|pane: &mut Pane, action: &Split, cx| {
|
||||
pane.split(action.0, cx);
|
||||
});
|
||||
|
||||
cx.add_bindings(vec![
|
||||
Binding::new("shift-cmd-{", "pane:activate_prev_item", Some("Pane")),
|
||||
Binding::new("shift-cmd-}", "pane:activate_next_item", Some("Pane")),
|
||||
Binding::new("cmd-w", "pane:close_active_item", Some("Pane")),
|
||||
Binding::new("cmd-k up", "pane:split_up", Some("Pane")),
|
||||
Binding::new("cmd-k down", "pane:split_down", Some("Pane")),
|
||||
Binding::new("cmd-k left", "pane:split_left", Some("Pane")),
|
||||
Binding::new("cmd-k right", "pane:split_right", Some("Pane")),
|
||||
Binding::new("shift-cmd-{", ActivatePrevItem, Some("Pane")),
|
||||
Binding::new("shift-cmd-}", ActivateNextItem, Some("Pane")),
|
||||
Binding::new("cmd-w", CloseActiveItem, Some("Pane")),
|
||||
Binding::new("cmd-k up", Split(SplitDirection::Up), Some("Pane")),
|
||||
Binding::new("cmd-k down", Split(SplitDirection::Down), Some("Pane")),
|
||||
Binding::new("cmd-k left", Split(SplitDirection::Left), Some("Pane")),
|
||||
Binding::new("cmd-k right", Split(SplitDirection::Right), Some("Pane")),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -253,7 +249,7 @@ impl Pane {
|
||||
ConstrainedBox::new(
|
||||
EventHandler::new(container.boxed())
|
||||
.on_mouse_down(move |cx| {
|
||||
cx.dispatch_action("pane:activate_item", ix);
|
||||
cx.dispatch_action(ActivateItem(ix));
|
||||
true
|
||||
})
|
||||
.boxed(),
|
||||
@@ -338,7 +334,7 @@ impl Pane {
|
||||
icon.boxed()
|
||||
}
|
||||
})
|
||||
.on_click(move |cx| cx.dispatch_action("pane:close_item", item_id))
|
||||
.on_click(move |cx| cx.dispatch_action(CloseItem(item_id)))
|
||||
.named("close-tab-icon")
|
||||
} else {
|
||||
let diameter = 8.;
|
||||
|
||||
@@ -184,7 +184,7 @@ impl PaneAxis {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum SplitDirection {
|
||||
Up,
|
||||
Down,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::Settings;
|
||||
use gpui::{
|
||||
action,
|
||||
elements::{
|
||||
Align, ConstrainedBox, Container, Flex, MouseEventHandler, ParentElement as _, Svg,
|
||||
},
|
||||
@@ -23,6 +24,14 @@ struct Item {
|
||||
view: AnyViewHandle,
|
||||
}
|
||||
|
||||
action!(ToggleSidebarItem, ToggleArg);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ToggleArg {
|
||||
side: Side,
|
||||
item_index: usize,
|
||||
}
|
||||
|
||||
impl Sidebar {
|
||||
pub fn new(side: Side) -> Self {
|
||||
Self {
|
||||
@@ -59,8 +68,8 @@ impl Sidebar {
|
||||
|
||||
Container::new(
|
||||
Flex::column()
|
||||
.with_children(self.items.iter().enumerate().map(|(item_ix, item)| {
|
||||
let theme = if Some(item_ix) == self.active_item_ix {
|
||||
.with_children(self.items.iter().enumerate().map(|(item_index, item)| {
|
||||
let theme = if Some(item_index) == self.active_item_ix {
|
||||
&settings.theme.active_sidebar_icon
|
||||
} else {
|
||||
&settings.theme.sidebar_icon
|
||||
@@ -81,7 +90,7 @@ impl Sidebar {
|
||||
.boxed()
|
||||
})
|
||||
.on_click(move |cx| {
|
||||
cx.dispatch_action("workspace:toggle_sidebar_item", (side, item_ix))
|
||||
cx.dispatch_action(ToggleSidebarItem(ToggleArg { side, item_index }))
|
||||
})
|
||||
.boxed()
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user