Allow actions to be deserialized from JSON

Introduce separate macro for implementing 'internal' actions which
are not intended to be loaded from keymaps.
This commit is contained in:
Max Brunsfeld 2022-04-08 15:32:56 -07:00
parent 1778622960
commit fd4b81c8fc
26 changed files with 559 additions and 335 deletions

View file

@ -7,13 +7,14 @@ use gpui::{
actions,
elements::*,
geometry::{rect::RectF, vector::vec2f},
impl_actions,
impl_actions, impl_internal_actions,
keymap::Binding,
platform::{CursorStyle, NavigationDirection},
AppContext, Entity, MutableAppContext, PromptLevel, Quad, RenderContext, Task, View,
ViewContext, ViewHandle, WeakViewHandle,
};
use project::{ProjectEntryId, ProjectPath};
use serde::Deserialize;
use settings::Settings;
use std::{any::Any, cell::RefCell, cmp, mem, path::Path, rc::Rc};
use util::ResultExt;
@ -28,13 +29,16 @@ actions!(
]
);
#[derive(Clone)]
#[derive(Clone, Deserialize)]
pub struct Split(pub SplitDirection);
#[derive(Clone)]
pub struct CloseItem(pub CloseItemParams);
pub struct CloseItem {
pub item_id: usize,
pub pane: WeakViewHandle<Pane>,
}
#[derive(Clone)]
#[derive(Clone, Deserialize)]
pub struct ActivateItem(pub usize);
#[derive(Clone)]
@ -43,13 +47,8 @@ pub struct GoBack(pub Option<WeakViewHandle<Pane>>);
#[derive(Clone)]
pub struct GoForward(pub Option<WeakViewHandle<Pane>>);
impl_actions!(pane, [Split, CloseItem, ActivateItem, GoBack, GoForward,]);
#[derive(Clone)]
pub struct CloseItemParams {
pub item_id: usize,
pub pane: WeakViewHandle<Pane>,
}
impl_actions!(pane, [Split]);
impl_internal_actions!(pane, [CloseItem, ActivateItem, GoBack, GoForward]);
const MAX_NAVIGATION_HISTORY_LEN: usize = 1024;
@ -66,8 +65,8 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_async_action(Pane::close_active_item);
cx.add_async_action(Pane::close_inactive_items);
cx.add_async_action(|workspace: &mut Workspace, action: &CloseItem, cx| {
let pane = action.0.pane.upgrade(cx)?;
Some(Pane::close_item(workspace, pane, action.0.item_id, cx))
let pane = action.pane.upgrade(cx)?;
Some(Pane::close_item(workspace, pane, action.item_id, cx))
});
cx.add_action(|pane: &mut Pane, action: &Split, cx| {
pane.split(action.0, cx);
@ -747,10 +746,10 @@ impl Pane {
.on_click({
let pane = pane.clone();
move |cx| {
cx.dispatch_action(CloseItem(CloseItemParams {
cx.dispatch_action(CloseItem {
item_id,
pane: pane.clone(),
}))
})
}
})
.named("close-tab-icon")

View file

@ -4,6 +4,7 @@ use client::PeerId;
use collections::HashMap;
use gpui::{elements::*, Axis, Border, ViewHandle};
use project::Collaborator;
use serde::Deserialize;
use theme::Theme;
#[derive(Clone, Debug, Eq, PartialEq)]
@ -254,7 +255,8 @@ impl PaneAxis {
}
}
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SplitDirection {
Up,
Down,

View file

@ -1,5 +1,7 @@
use super::Workspace;
use gpui::{elements::*, impl_actions, platform::CursorStyle, AnyViewHandle, RenderContext};
use gpui::{
elements::*, impl_internal_actions, platform::CursorStyle, AnyViewHandle, RenderContext,
};
use std::{cell::RefCell, rc::Rc};
use theme::Theme;
@ -27,7 +29,7 @@ pub struct ToggleSidebarItem(pub SidebarItemId);
#[derive(Clone)]
pub struct ToggleSidebarItemFocus(pub SidebarItemId);
impl_actions!(workspace, [ToggleSidebarItem, ToggleSidebarItemFocus]);
impl_internal_actions!(workspace, [ToggleSidebarItem, ToggleSidebarItemFocus]);
#[derive(Clone)]
pub struct SidebarItemId {

View file

@ -17,7 +17,7 @@ use gpui::{
color::Color,
elements::*,
geometry::{rect::RectF, vector::vec2f, PathBuilder},
impl_actions,
impl_internal_actions,
json::{self, to_string_pretty, ToJson},
keymap::Binding,
platform::{CursorStyle, WindowOptions},
@ -101,7 +101,7 @@ pub struct ToggleFollow(pub PeerId);
#[derive(Clone)]
pub struct JoinProject(pub JoinProjectParams);
impl_actions!(
impl_internal_actions!(
workspace,
[Open, OpenNew, OpenPaths, ToggleFollow, JoinProject]
);
@ -630,6 +630,7 @@ pub struct WorkspaceParams {
pub client: Arc<Client>,
pub fs: Arc<dyn Fs>,
pub languages: Arc<LanguageRegistry>,
pub themes: Arc<ThemeRegistry>,
pub user_store: ModelHandle<UserStore>,
pub channel_list: ModelHandle<ChannelList>,
}
@ -659,6 +660,7 @@ impl WorkspaceParams {
channel_list: cx
.add_model(|cx| ChannelList::new(user_store.clone(), client.clone(), cx)),
client,
themes: ThemeRegistry::new((), cx.font_cache().clone()),
fs,
languages,
user_store,
@ -677,6 +679,7 @@ impl WorkspaceParams {
),
client: app_state.client.clone(),
fs: app_state.fs.clone(),
themes: app_state.themes.clone(),
languages: app_state.languages.clone(),
user_store: app_state.user_store.clone(),
channel_list: app_state.channel_list.clone(),
@ -694,6 +697,7 @@ pub struct Workspace {
user_store: ModelHandle<client::UserStore>,
remote_entity_subscription: Option<Subscription>,
fs: Arc<dyn Fs>,
themes: Arc<ThemeRegistry>,
modal: Option<AnyViewHandle>,
center: PaneGroup,
left_sidebar: Sidebar,
@ -802,6 +806,7 @@ impl Workspace {
remote_entity_subscription: None,
user_store: params.user_store.clone(),
fs: params.fs.clone(),
themes: params.themes.clone(),
left_sidebar: Sidebar::new(Side::Left),
right_sidebar: Sidebar::new(Side::Right),
project: params.project.clone(),
@ -834,6 +839,10 @@ impl Workspace {
&self.project
}
pub fn themes(&self) -> Arc<ThemeRegistry> {
self.themes.clone()
}
pub fn worktrees<'a>(
&self,
cx: &'a AppContext,