Remove ViewContext::dispatch_action
This commit is contained in:
parent
d815fc88ae
commit
c4472b0786
41 changed files with 574 additions and 670 deletions
|
@ -271,11 +271,11 @@ impl Dock {
|
|||
}
|
||||
}
|
||||
|
||||
fn focus_dock(workspace: &mut Workspace, _: &FocusDock, cx: &mut ViewContext<Workspace>) {
|
||||
pub fn focus_dock(workspace: &mut Workspace, _: &FocusDock, cx: &mut ViewContext<Workspace>) {
|
||||
Self::set_dock_position(workspace, workspace.dock.position.show(), true, cx);
|
||||
}
|
||||
|
||||
fn hide_dock(workspace: &mut Workspace, _: &HideDock, cx: &mut ViewContext<Workspace>) {
|
||||
pub fn hide_dock(workspace: &mut Workspace, _: &HideDock, cx: &mut ViewContext<Workspace>) {
|
||||
Self::set_dock_position(workspace, workspace.dock.position.hide(), true, cx);
|
||||
}
|
||||
|
||||
|
@ -374,8 +374,8 @@ impl Dock {
|
|||
.with_background_color(style.wash_color)
|
||||
})
|
||||
.capture_all()
|
||||
.on_down(MouseButton::Left, |_, _, cx| {
|
||||
cx.dispatch_action(HideDock);
|
||||
.on_down(MouseButton::Left, |_, workspace, cx| {
|
||||
Dock::hide_dock(workspace, &Default::default(), cx)
|
||||
})
|
||||
.with_cursor_style(CursorStyle::Arrow),
|
||||
)
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use super::{icon_for_dock_anchor, Dock, FocusDock, HideDock};
|
||||
use crate::{handle_dropped_item, StatusItemView, Workspace};
|
||||
use gpui::{
|
||||
elements::{Empty, MouseEventHandler, Svg},
|
||||
platform::CursorStyle,
|
||||
|
@ -6,10 +8,6 @@ use gpui::{
|
|||
};
|
||||
use settings::Settings;
|
||||
|
||||
use crate::{handle_dropped_item, StatusItemView, Workspace};
|
||||
|
||||
use super::{icon_for_dock_anchor, FocusDock, HideDock};
|
||||
|
||||
pub struct ToggleDockButton {
|
||||
workspace: WeakViewHandle<Workspace>,
|
||||
}
|
||||
|
@ -82,8 +80,12 @@ impl View for ToggleDockButton {
|
|||
|
||||
if dock_position.is_visible() {
|
||||
button
|
||||
.on_click(MouseButton::Left, |_, _, cx| {
|
||||
cx.dispatch_action(HideDock);
|
||||
.on_click(MouseButton::Left, |_, this, cx| {
|
||||
if let Some(workspace) = this.workspace.upgrade(cx) {
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
Dock::hide_dock(workspace, &Default::default(), cx)
|
||||
})
|
||||
}
|
||||
})
|
||||
.with_tooltip::<Self>(
|
||||
0,
|
||||
|
@ -94,8 +96,12 @@ impl View for ToggleDockButton {
|
|||
)
|
||||
} else {
|
||||
button
|
||||
.on_click(MouseButton::Left, |_, _, cx| {
|
||||
cx.dispatch_action(FocusDock);
|
||||
.on_click(MouseButton::Left, |_, this, cx| {
|
||||
if let Some(workspace) = this.workspace.upgrade(cx) {
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
Dock::focus_dock(workspace, &Default::default(), cx)
|
||||
})
|
||||
}
|
||||
})
|
||||
.with_tooltip::<Self>(
|
||||
0,
|
||||
|
|
|
@ -278,8 +278,8 @@ pub mod simple_message_notification {
|
|||
.with_height(style.button_width)
|
||||
})
|
||||
.with_padding(Padding::uniform(5.))
|
||||
.on_click(MouseButton::Left, move |_, _, cx| {
|
||||
cx.dispatch_action(CancelMessageNotification)
|
||||
.on_click(MouseButton::Left, move |_, this, cx| {
|
||||
this.dismiss(&Default::default(), cx);
|
||||
})
|
||||
.with_cursor_style(CursorStyle::PointingHand)
|
||||
.aligned()
|
||||
|
|
|
@ -2,7 +2,7 @@ mod dragged_item_receiver;
|
|||
|
||||
use super::{ItemHandle, SplitDirection};
|
||||
use crate::{
|
||||
dock::{icon_for_dock_anchor, AnchorDockBottom, AnchorDockRight, ExpandDock, HideDock},
|
||||
dock::{icon_for_dock_anchor, AnchorDockBottom, AnchorDockRight, Dock, ExpandDock},
|
||||
item::WeakItemHandle,
|
||||
toolbar::Toolbar,
|
||||
Item, NewFile, NewSearch, NewTerminal, Workspace,
|
||||
|
@ -259,6 +259,10 @@ impl Pane {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn workspace(&self) -> &WeakViewHandle<Workspace> {
|
||||
&self.workspace
|
||||
}
|
||||
|
||||
pub fn is_active(&self) -> bool {
|
||||
self.is_active
|
||||
}
|
||||
|
@ -1340,8 +1344,8 @@ impl Pane {
|
|||
cx,
|
||||
)
|
||||
})
|
||||
.on_down(MouseButton::Left, move |_, _, cx| {
|
||||
cx.dispatch_action(ActivateItem(ix));
|
||||
.on_down(MouseButton::Left, move |_, this, cx| {
|
||||
this.activate_item(ix, true, true, cx);
|
||||
})
|
||||
.on_click(MouseButton::Middle, {
|
||||
let item_id = item.id();
|
||||
|
@ -1639,7 +1643,13 @@ impl Pane {
|
|||
3,
|
||||
"icons/x_mark_8.svg",
|
||||
cx,
|
||||
|_, cx| cx.dispatch_action(HideDock),
|
||||
|this, cx| {
|
||||
if let Some(workspace) = this.workspace.upgrade(cx) {
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
Dock::hide_dock(workspace, &Default::default(), cx)
|
||||
})
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
}))
|
||||
|
@ -1693,8 +1703,8 @@ impl View for Pane {
|
|||
})
|
||||
.on_down(
|
||||
MouseButton::Left,
|
||||
move |_, _, cx| {
|
||||
cx.dispatch_action(ActivateItem(active_item_index));
|
||||
move |_, this, cx| {
|
||||
this.activate_item(active_item_index, true, true, cx);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
@ -1759,15 +1769,27 @@ impl View for Pane {
|
|||
})
|
||||
.on_down(
|
||||
MouseButton::Navigate(NavigationDirection::Back),
|
||||
move |_, _, cx| {
|
||||
let pane = cx.weak_handle();
|
||||
cx.dispatch_action(GoBack { pane: Some(pane) });
|
||||
move |_, pane, cx| {
|
||||
if let Some(workspace) = pane.workspace.upgrade(cx) {
|
||||
let pane = cx.weak_handle();
|
||||
cx.window_context().defer(move |cx| {
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
Pane::go_back(workspace, Some(pane), cx).detach_and_log_err(cx)
|
||||
})
|
||||
})
|
||||
}
|
||||
},
|
||||
)
|
||||
.on_down(MouseButton::Navigate(NavigationDirection::Forward), {
|
||||
move |_, _, cx| {
|
||||
let pane = cx.weak_handle();
|
||||
cx.dispatch_action(GoForward { pane: Some(pane) })
|
||||
move |_, pane, cx| {
|
||||
if let Some(workspace) = pane.workspace.upgrade(cx) {
|
||||
let pane = cx.weak_handle();
|
||||
cx.window_context().defer(move |cx| {
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
Pane::go_forward(workspace, Some(pane), cx).detach_and_log_err(cx)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
.into_any_named("pane")
|
||||
|
|
|
@ -279,9 +279,9 @@ impl View for SidebarButtons {
|
|||
.with_style(style.container)
|
||||
})
|
||||
.with_cursor_style(CursorStyle::PointingHand)
|
||||
.on_click(MouseButton::Left, {
|
||||
let action = action.clone();
|
||||
move |_, _, cx| cx.dispatch_action(action.clone())
|
||||
.on_click(MouseButton::Left, move |_, this, cx| {
|
||||
this.sidebar
|
||||
.update(cx, |sidebar, cx| sidebar.toggle_item(ix, cx));
|
||||
})
|
||||
.with_tooltip::<Self>(
|
||||
ix,
|
||||
|
|
|
@ -130,8 +130,20 @@ impl View for Toolbar {
|
|||
tooltip_style.clone(),
|
||||
enable_go_backward,
|
||||
spacing,
|
||||
super::GoBack {
|
||||
pane: Some(pane.clone()),
|
||||
{
|
||||
let pane = pane.clone();
|
||||
move |toolbar, cx| {
|
||||
if let Some(workspace) = toolbar
|
||||
.pane
|
||||
.upgrade(cx)
|
||||
.and_then(|pane| pane.read(cx).workspace().upgrade(cx))
|
||||
{
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
Pane::go_back(workspace, Some(pane.clone()), cx)
|
||||
.detach_and_log_err(cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
super::GoBack { pane: None },
|
||||
"Go Back",
|
||||
|
@ -143,7 +155,21 @@ impl View for Toolbar {
|
|||
tooltip_style,
|
||||
enable_go_forward,
|
||||
spacing,
|
||||
super::GoForward { pane: Some(pane) },
|
||||
{
|
||||
let pane = pane.clone();
|
||||
move |toolbar, cx| {
|
||||
if let Some(workspace) = toolbar
|
||||
.pane
|
||||
.upgrade(cx)
|
||||
.and_then(|pane| pane.read(cx).workspace().upgrade(cx))
|
||||
{
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
Pane::go_forward(workspace, Some(pane.clone()), cx)
|
||||
.detach_and_log_err(cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
super::GoForward { pane: None },
|
||||
"Go Forward",
|
||||
cx,
|
||||
|
@ -161,13 +187,13 @@ impl View for Toolbar {
|
|||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn nav_button<A: Action + Clone>(
|
||||
fn nav_button<A: Action, F: 'static + Fn(&mut Toolbar, &mut ViewContext<Toolbar>)>(
|
||||
svg_path: &'static str,
|
||||
style: theme::Interactive<theme::IconButton>,
|
||||
tooltip_style: TooltipStyle,
|
||||
enabled: bool,
|
||||
spacing: f32,
|
||||
action: A,
|
||||
on_click: F,
|
||||
tooltip_action: A,
|
||||
action_name: &str,
|
||||
cx: &mut ViewContext<Toolbar>,
|
||||
|
@ -195,8 +221,8 @@ fn nav_button<A: Action + Clone>(
|
|||
} else {
|
||||
CursorStyle::default()
|
||||
})
|
||||
.on_click(MouseButton::Left, move |_, _, cx| {
|
||||
cx.dispatch_action(action.clone())
|
||||
.on_click(MouseButton::Left, move |_, toolbar, cx| {
|
||||
on_click(toolbar, cx)
|
||||
})
|
||||
.with_tooltip::<A>(
|
||||
0,
|
||||
|
|
|
@ -208,48 +208,7 @@ pub fn init(app_state: Arc<AppState>, cx: &mut AppContext) {
|
|||
}
|
||||
}
|
||||
});
|
||||
cx.add_action({
|
||||
let app_state = Arc::downgrade(&app_state);
|
||||
move |_, _: &Open, cx: &mut ViewContext<Workspace>| {
|
||||
let mut paths = cx.prompt_for_paths(PathPromptOptions {
|
||||
files: true,
|
||||
directories: true,
|
||||
multiple: true,
|
||||
});
|
||||
|
||||
if let Some(app_state) = app_state.upgrade() {
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
if let Some(paths) = paths.recv().await.flatten() {
|
||||
if let Some(task) = this
|
||||
.update(&mut cx, |this, cx| {
|
||||
this.open_workspace_for_paths(paths, app_state, cx)
|
||||
})
|
||||
.log_err()
|
||||
{
|
||||
task.await.log_err();
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
});
|
||||
cx.add_global_action({
|
||||
let app_state = Arc::downgrade(&app_state);
|
||||
move |_: &NewWindow, cx: &mut AppContext| {
|
||||
if let Some(app_state) = app_state.upgrade() {
|
||||
open_new(&app_state, cx, |_, cx| cx.dispatch_action(NewFile)).detach();
|
||||
}
|
||||
}
|
||||
});
|
||||
cx.add_global_action({
|
||||
let app_state = Arc::downgrade(&app_state);
|
||||
move |_: &NewFile, cx: &mut AppContext| {
|
||||
if let Some(app_state) = app_state.upgrade() {
|
||||
open_new(&app_state, cx, |_, cx| cx.dispatch_action(NewFile)).detach();
|
||||
}
|
||||
}
|
||||
});
|
||||
cx.add_async_action(Workspace::open);
|
||||
|
||||
cx.add_async_action(Workspace::follow_next_collaborator);
|
||||
cx.add_async_action(Workspace::close);
|
||||
|
@ -913,7 +872,6 @@ impl Workspace {
|
|||
/// to the callback. Otherwise, a new empty window will be created.
|
||||
pub fn with_local_workspace<T, F>(
|
||||
&mut self,
|
||||
app_state: &Arc<AppState>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
callback: F,
|
||||
) -> Task<Result<T>>
|
||||
|
@ -924,7 +882,7 @@ impl Workspace {
|
|||
if self.project.read(cx).is_local() {
|
||||
Task::Ready(Some(Ok(callback(self, cx))))
|
||||
} else {
|
||||
let task = Self::new_local(Vec::new(), app_state.clone(), None, cx);
|
||||
let task = Self::new_local(Vec::new(), self.app_state.clone(), None, cx);
|
||||
cx.spawn(|_vh, mut cx| async move {
|
||||
let (workspace, _) = task.await;
|
||||
workspace.update(&mut cx, callback)
|
||||
|
@ -1093,10 +1051,29 @@ impl Workspace {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn open(&mut self, _: &Open, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> {
|
||||
let mut paths = cx.prompt_for_paths(PathPromptOptions {
|
||||
files: true,
|
||||
directories: true,
|
||||
multiple: true,
|
||||
});
|
||||
|
||||
Some(cx.spawn(|this, mut cx| async move {
|
||||
if let Some(paths) = paths.recv().await.flatten() {
|
||||
if let Some(task) = this
|
||||
.update(&mut cx, |this, cx| this.open_workspace_for_paths(paths, cx))
|
||||
.log_err()
|
||||
{
|
||||
task.await?
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn open_workspace_for_paths(
|
||||
&mut self,
|
||||
paths: Vec<PathBuf>,
|
||||
app_state: Arc<AppState>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Task<Result<()>> {
|
||||
let window_id = cx.window_id();
|
||||
|
@ -1108,6 +1085,7 @@ impl Workspace {
|
|||
} else {
|
||||
Some(self.prepare_to_close(false, cx))
|
||||
};
|
||||
let app_state = self.app_state.clone();
|
||||
|
||||
cx.spawn(|_, mut cx| async move {
|
||||
let window_id_to_replace = if let Some(close_task) = close_task {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue