task_ui: Move status indicator into tab bar of terminal panel (#10846)

Piotr Osiewicz created

I'm not a huge fan of this change (& I expect the placement to change).
The plan is to have the button in a toolbar of terminal panel, but I'm
not sure if occupying a whole line of vertical space for a single button
is worth it; I suppose we might want to put more of tasks ui inside of
that toolbar.
Release Notes:

- Removed task status indicator and added "Spawn task" action to
terminal panel context menu.

Change summary

Cargo.lock                                 |  2 
crates/tasks_ui/Cargo.toml                 |  1 
crates/tasks_ui/src/lib.rs                 |  5 
crates/tasks_ui/src/modal.rs               |  3 
crates/tasks_ui/src/status_indicator.rs    | 98 ------------------------
crates/terminal_view/Cargo.toml            |  1 
crates/terminal_view/src/terminal_panel.rs | 50 +++++++++--
crates/workspace/src/pane.rs               |  4 
crates/zed/src/zed.rs                      |  2 
9 files changed, 46 insertions(+), 120 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -9765,7 +9765,6 @@ dependencies = [
  "serde_json",
  "settings",
  "task",
- "terminal",
  "tree-sitter-rust",
  "tree-sitter-typescript",
  "ui",
@@ -9863,6 +9862,7 @@ dependencies = [
  "shellexpand",
  "smol",
  "task",
+ "tasks_ui",
  "terminal",
  "theme",
  "ui",

crates/tasks_ui/Cargo.toml 🔗

@@ -22,7 +22,6 @@ serde.workspace = true
 settings.workspace = true
 ui.workspace = true
 util.workspace = true
-terminal.workspace = true
 workspace.workspace = true
 language.workspace = true
 

crates/tasks_ui/src/lib.rs 🔗

@@ -8,7 +8,7 @@ use anyhow::Context;
 use editor::Editor;
 use gpui::{AppContext, ViewContext, WindowContext};
 use language::{BasicContextProvider, ContextProvider, Language};
-use modal::{Spawn, TasksModal};
+use modal::TasksModal;
 use project::{Location, TaskSourceKind, WorktreeId};
 use task::{ResolvedTask, TaskContext, TaskTemplate, TaskVariables};
 use util::ResultExt;
@@ -16,9 +16,8 @@ use workspace::Workspace;
 
 mod modal;
 mod settings;
-mod status_indicator;
 
-pub use status_indicator::TaskStatusIndicator;
+pub use modal::Spawn;
 
 pub fn init(cx: &mut AppContext) {
     settings::TaskSettings::register(cx);

crates/tasks_ui/src/modal.rs 🔗

@@ -31,10 +31,11 @@ pub struct Spawn {
 }
 
 impl Spawn {
-    pub(crate) fn modal() -> Self {
+    pub fn modal() -> Self {
         Self { task_name: None }
     }
 }
+
 /// Rerun last task
 #[derive(PartialEq, Clone, Deserialize, Default)]
 pub struct Rerun {

crates/tasks_ui/src/status_indicator.rs 🔗

@@ -1,98 +0,0 @@
-use gpui::{IntoElement, Render, View, WeakView};
-use settings::Settings;
-use ui::{
-    div, ButtonCommon, Clickable, Color, FluentBuilder, IconButton, IconName, Tooltip,
-    VisualContext, WindowContext,
-};
-use workspace::{item::ItemHandle, StatusItemView, Workspace};
-
-use crate::{modal::Spawn, settings::TaskSettings};
-
-enum TaskStatus {
-    Failed,
-    Running,
-    Succeeded,
-}
-
-/// A status bar icon that surfaces the status of running tasks.
-/// It has a different color depending on the state of running tasks:
-/// - red if any open task tab failed
-/// - else, yellow if any open task tab is still running
-/// - else, green if there tasks tabs open, and they have all succeeded
-/// - else, no indicator if there are no open task tabs
-pub struct TaskStatusIndicator {
-    workspace: WeakView<Workspace>,
-}
-
-impl TaskStatusIndicator {
-    pub fn new(workspace: WeakView<Workspace>, cx: &mut WindowContext) -> View<Self> {
-        cx.new_view(|_| Self { workspace })
-    }
-    fn current_status(&self, cx: &mut WindowContext) -> Option<TaskStatus> {
-        self.workspace
-            .update(cx, |this, cx| {
-                let mut status = None;
-                let project = this.project().read(cx);
-
-                for handle in project.local_terminal_handles() {
-                    let Some(handle) = handle.upgrade() else {
-                        continue;
-                    };
-                    let handle = handle.read(cx);
-                    let task_state = handle.task();
-                    if let Some(state) = task_state {
-                        match state.status {
-                            terminal::TaskStatus::Running => {
-                                let _ = status.insert(TaskStatus::Running);
-                            }
-                            terminal::TaskStatus::Completed { success } => {
-                                if !success {
-                                    let _ = status.insert(TaskStatus::Failed);
-                                    return status;
-                                }
-                                status.get_or_insert(TaskStatus::Succeeded);
-                            }
-                            _ => {}
-                        };
-                    }
-                }
-                status
-            })
-            .ok()
-            .flatten()
-    }
-}
-
-impl Render for TaskStatusIndicator {
-    fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
-        if !TaskSettings::get_global(cx).show_status_indicator {
-            return div().into_any_element();
-        }
-        let current_status = self.current_status(cx);
-        let color = current_status.map(|status| match status {
-            TaskStatus::Failed => Color::Error,
-            TaskStatus::Running => Color::Warning,
-            TaskStatus::Succeeded => Color::Success,
-        });
-        IconButton::new("tasks-activity-indicator", IconName::Play)
-            .when_some(color, |this, color| this.icon_color(color))
-            .on_click(cx.listener(|this, _, cx| {
-                this.workspace
-                    .update(cx, |this, cx| {
-                        crate::spawn_task_or_modal(this, &Spawn::modal(), cx)
-                    })
-                    .ok();
-            }))
-            .tooltip(|cx| Tooltip::for_action("Spawn tasks", &Spawn { task_name: None }, cx))
-            .into_any_element()
-    }
-}
-
-impl StatusItemView for TaskStatusIndicator {
-    fn set_active_pane_item(
-        &mut self,
-        _: Option<&dyn ItemHandle>,
-        _: &mut ui::prelude::ViewContext<Self>,
-    ) {
-    }
-}

crates/terminal_view/Cargo.toml 🔗

@@ -24,6 +24,7 @@ itertools.workspace = true
 language.workspace = true
 project.workspace = true
 task.workspace = true
+tasks_ui.workspace = true
 search.workspace = true
 serde.workspace = true
 serde_json.workspace = true

crates/terminal_view/src/terminal_panel.rs 🔗

@@ -5,9 +5,9 @@ use collections::{HashMap, HashSet};
 use db::kvp::KEY_VALUE_STORE;
 use futures::future::join_all;
 use gpui::{
-    actions, Action, AppContext, AsyncWindowContext, Entity, EventEmitter, ExternalPaths,
-    FocusHandle, FocusableView, IntoElement, ParentElement, Pixels, Render, Styled, Subscription,
-    Task, View, ViewContext, VisualContext, WeakView, WindowContext,
+    actions, Action, AppContext, AsyncWindowContext, DismissEvent, Entity, EventEmitter,
+    ExternalPaths, FocusHandle, FocusableView, IntoElement, ParentElement, Pixels, Render, Styled,
+    Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowContext,
 };
 use itertools::Itertools;
 use project::{Fs, ProjectEntryId};
@@ -16,7 +16,10 @@ use serde::{Deserialize, Serialize};
 use settings::Settings;
 use task::{RevealStrategy, SpawnInTerminal, TaskId};
 use terminal::terminal_settings::{Shell, TerminalDockPosition, TerminalSettings};
-use ui::{h_flex, ButtonCommon, Clickable, IconButton, IconSize, Selectable, Tooltip};
+use ui::{
+    h_flex, ButtonCommon, Clickable, ContextMenu, FluentBuilder, IconButton, IconSize, Selectable,
+    Tooltip,
+};
 use util::{ResultExt, TryFutureExt};
 use workspace::{
     dock::{DockPosition, Panel, PanelEvent},
@@ -59,7 +62,6 @@ pub struct TerminalPanel {
 
 impl TerminalPanel {
     fn new(workspace: &Workspace, cx: &mut ViewContext<Self>) -> Self {
-        let terminal_panel = cx.view().downgrade();
         let pane = cx.new_view(|cx| {
             let mut pane = Pane::new(
                 workspace.weak_handle(),
@@ -73,19 +75,43 @@ impl TerminalPanel {
             pane.set_can_navigate(false, cx);
             pane.display_nav_history_buttons(None);
             pane.set_render_tab_bar_buttons(cx, move |pane, cx| {
-                let terminal_panel = terminal_panel.clone();
                 h_flex()
                     .gap_2()
                     .child(
                         IconButton::new("plus", IconName::Plus)
                             .icon_size(IconSize::Small)
-                            .on_click(move |_, cx| {
-                                terminal_panel
-                                    .update(cx, |panel, cx| panel.add_terminal(None, None, cx))
-                                    .log_err();
-                            })
-                            .tooltip(|cx| Tooltip::text("New Terminal", cx)),
+                            .on_click(cx.listener(|pane, _, cx| {
+                                let focus_handle = pane.focus_handle(cx);
+                                let menu = ContextMenu::build(cx, |menu, _| {
+                                    menu.action(
+                                        "New Terminal",
+                                        workspace::NewTerminal.boxed_clone(),
+                                    )
+                                    .entry(
+                                        "Spawn task",
+                                        Some(tasks_ui::Spawn::modal().boxed_clone()),
+                                        move |cx| {
+                                            // We want the focus to go back to terminal panel once task modal is dismissed,
+                                            // hence we focus that first. Otherwise, we'd end up without a focused element, as
+                                            // context menu will be gone the moment we spawn the modal.
+                                            cx.focus(&focus_handle);
+                                            cx.dispatch_action(
+                                                tasks_ui::Spawn::modal().boxed_clone(),
+                                            );
+                                        },
+                                    )
+                                });
+                                cx.subscribe(&menu, |pane, _, _: &DismissEvent, _| {
+                                    pane.new_item_menu = None;
+                                })
+                                .detach();
+                                pane.new_item_menu = Some(menu);
+                            }))
+                            .tooltip(|cx| Tooltip::text("New...", cx)),
                     )
+                    .when_some(pane.new_item_menu.as_ref(), |el, new_item_menu| {
+                        el.child(Pane::render_menu_overlay(new_item_menu))
+                    })
                     .child({
                         let zoomed = pane.is_zoomed();
                         IconButton::new("toggle_zoom", IconName::Maximize)

crates/workspace/src/pane.rs 🔗

@@ -193,7 +193,7 @@ pub struct Pane {
     last_focus_handle_by_item: HashMap<EntityId, WeakFocusHandle>,
     nav_history: NavHistory,
     toolbar: View<Toolbar>,
-    new_item_menu: Option<View<ContextMenu>>,
+    pub new_item_menu: Option<View<ContextMenu>>,
     split_item_menu: Option<View<ContextMenu>>,
     //     tab_context_menu: View<ContextMenu>,
     pub(crate) workspace: WeakView<Workspace>,
@@ -1747,7 +1747,7 @@ impl Pane {
             )
     }
 
-    fn render_menu_overlay(menu: &View<ContextMenu>) -> Div {
+    pub fn render_menu_overlay(menu: &View<ContextMenu>) -> Div {
         div().absolute().bottom_0().right_0().size_0().child(
             deferred(
                 anchored()

crates/zed/src/zed.rs 🔗

@@ -131,7 +131,6 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
             cx.new_view(|cx| diagnostics::items::DiagnosticIndicator::new(workspace, cx));
         let activity_indicator =
             activity_indicator::ActivityIndicator::new(workspace, app_state.languages.clone(), cx);
-        let tasks_indicator = tasks_ui::TaskStatusIndicator::new(workspace.weak_handle(), cx);
         let active_buffer_language =
             cx.new_view(|_| language_selector::ActiveBufferLanguage::new(workspace));
         let vim_mode_indicator = cx.new_view(|cx| vim::ModeIndicator::new(cx));
@@ -141,7 +140,6 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
             status_bar.add_left_item(diagnostic_summary, cx);
             status_bar.add_left_item(activity_indicator, cx);
             status_bar.add_right_item(copilot, cx);
-            status_bar.add_right_item(tasks_indicator, cx);
             status_bar.add_right_item(active_buffer_language, cx);
             status_bar.add_right_item(vim_mode_indicator, cx);
             status_bar.add_right_item(cursor_position, cx);