Give focus to the selected terminal
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
parent
baa9e271d5
commit
ad7e49ed06
6 changed files with 43 additions and 20 deletions
|
@ -1,145 +0,0 @@
|
|||
use context_menu::{ContextMenu, ContextMenuItem};
|
||||
use gpui::{
|
||||
actions, elements::*, geometry::vector::vec2f, CursorStyle, Element, ElementBox, Entity,
|
||||
MouseButton, MutableAppContext, RenderContext, View, ViewContext, ViewHandle, WeakViewHandle,
|
||||
};
|
||||
use settings::Settings;
|
||||
|
||||
use crate::{dock::FocusDock, item::ItemHandle, NewTerminal, StatusItemView, Workspace};
|
||||
|
||||
// #[derive(Clone, PartialEq)]
|
||||
// pub struct DeployTerminalMenu {
|
||||
// position: Vector2F,
|
||||
// }
|
||||
|
||||
actions!(terminal, [DeployTerminalMenu]);
|
||||
|
||||
pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_action(TerminalButton::deploy_terminal_menu);
|
||||
}
|
||||
|
||||
pub struct TerminalButton {
|
||||
workspace: WeakViewHandle<Workspace>,
|
||||
popup_menu: ViewHandle<ContextMenu>,
|
||||
}
|
||||
|
||||
impl Entity for TerminalButton {
|
||||
type Event = ();
|
||||
}
|
||||
|
||||
impl View for TerminalButton {
|
||||
fn ui_name() -> &'static str {
|
||||
"TerminalButton"
|
||||
}
|
||||
|
||||
fn render(&mut self, cx: &mut RenderContext<'_, Self>) -> ElementBox {
|
||||
let workspace = self.workspace.upgrade(cx);
|
||||
let project = match workspace {
|
||||
Some(workspace) => workspace.read(cx).project().read(cx),
|
||||
None => return Empty::new().boxed(),
|
||||
};
|
||||
let has_terminals = !project.local_terminal_handles().is_empty();
|
||||
let terminal_count = project.local_terminal_handles().iter().count();
|
||||
let theme = cx.global::<Settings>().theme.clone();
|
||||
|
||||
Stack::new()
|
||||
.with_child(
|
||||
MouseEventHandler::<Self>::new(0, cx, {
|
||||
let theme = theme.clone();
|
||||
move |state, _| {
|
||||
let style = theme
|
||||
.workspace
|
||||
.status_bar
|
||||
.sidebar_buttons
|
||||
.item
|
||||
.style_for(state, true);
|
||||
|
||||
Svg::new("icons/terminal_12.svg")
|
||||
.with_color(style.icon_color)
|
||||
.constrained()
|
||||
.with_width(style.icon_size)
|
||||
.with_height(style.icon_size)
|
||||
.contained()
|
||||
.with_style(style.container)
|
||||
.boxed()
|
||||
}
|
||||
})
|
||||
.with_cursor_style(CursorStyle::PointingHand)
|
||||
.on_up(MouseButton::Left, move |_, _| {
|
||||
// TODO: Do we need this stuff?
|
||||
// let dock_pane = workspace.read(cx.app).dock_pane();
|
||||
// let drop_index = dock_pane.read(cx.app).items_len() + 1;
|
||||
// handle_dropped_item(event, &dock_pane.downgrade(), drop_index, false, None, cx);
|
||||
})
|
||||
.on_click(MouseButton::Left, move |_, cx| {
|
||||
if has_terminals {
|
||||
cx.dispatch_action(DeployTerminalMenu);
|
||||
println!("Yes, has_terminals {}", terminal_count);
|
||||
} else {
|
||||
cx.dispatch_action(FocusDock);
|
||||
};
|
||||
})
|
||||
.with_tooltip::<Self, _>(
|
||||
0,
|
||||
"Show Terminal".into(),
|
||||
Some(Box::new(FocusDock)),
|
||||
theme.tooltip.clone(),
|
||||
cx,
|
||||
)
|
||||
.boxed(),
|
||||
)
|
||||
.with_child(ChildView::new(&self.popup_menu, cx).boxed())
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Rename this to `DeployTerminalButton`
|
||||
impl TerminalButton {
|
||||
pub fn new(workspace: ViewHandle<Workspace>, cx: &mut ViewContext<Self>) -> Self {
|
||||
// When terminal moves, redraw so that the icon and toggle status matches.
|
||||
cx.subscribe(&workspace, |_, _, _, cx| cx.notify()).detach();
|
||||
Self {
|
||||
workspace: workspace.downgrade(),
|
||||
popup_menu: cx.add_view(|cx| {
|
||||
let mut menu = ContextMenu::new(cx);
|
||||
menu.set_position_mode(OverlayPositionMode::Local);
|
||||
menu
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deploy_terminal_menu(
|
||||
&mut self,
|
||||
_action: &DeployTerminalMenu,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
let mut menu_options = vec![ContextMenuItem::item("New Terminal", NewTerminal)];
|
||||
|
||||
if let Some(workspace) = self.workspace.upgrade(cx) {
|
||||
let project = workspace.read(cx).project().read(cx);
|
||||
let local_terminal_handles = project.local_terminal_handles();
|
||||
|
||||
if !local_terminal_handles.is_empty() {
|
||||
menu_options.push(ContextMenuItem::Separator)
|
||||
}
|
||||
|
||||
for local_terminal_handle in local_terminal_handles {
|
||||
if let Some(terminal) = local_terminal_handle.upgrade(cx) {
|
||||
// TODO: Replace the `NewTerminal` action with an action that instead focuses the selected terminal
|
||||
menu_options.push(ContextMenuItem::item(
|
||||
terminal.read(cx).title(),
|
||||
NewTerminal,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.popup_menu.update(cx, |menu, cx| {
|
||||
menu.show(vec2f(0., 0.), AnchorCorner::TopRight, menu_options, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl StatusItemView for TerminalButton {
|
||||
fn set_active_pane_item(&mut self, _: Option<&dyn ItemHandle>, _: &mut ViewContext<Self>) {}
|
||||
}
|
|
@ -12,7 +12,6 @@ pub mod searchable;
|
|||
pub mod shared_screen;
|
||||
pub mod sidebar;
|
||||
mod status_bar;
|
||||
pub mod terminal_button;
|
||||
mod toolbar;
|
||||
|
||||
pub use smallvec;
|
||||
|
@ -57,7 +56,6 @@ use std::{
|
|||
sync::Arc,
|
||||
time::Duration,
|
||||
};
|
||||
use terminal_button::TerminalButton;
|
||||
|
||||
use crate::{
|
||||
notifications::simple_message_notification::{MessageNotification, OsOpen},
|
||||
|
@ -83,7 +81,7 @@ use status_bar::StatusBar;
|
|||
pub use status_bar::StatusItemView;
|
||||
use theme::{Theme, ThemeRegistry};
|
||||
pub use toolbar::{ToolbarItemLocation, ToolbarItemView};
|
||||
use util::{ResultExt, StaffMode};
|
||||
use util::ResultExt;
|
||||
|
||||
lazy_static! {
|
||||
static ref ZED_WINDOW_SIZE: Option<Vector2F> = env::var("ZED_WINDOW_SIZE")
|
||||
|
@ -185,7 +183,6 @@ impl_actions!(workspace, [ActivatePane]);
|
|||
pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
|
||||
pane::init(cx);
|
||||
dock::init(cx);
|
||||
terminal_button::init(cx);
|
||||
notifications::init(cx);
|
||||
|
||||
cx.add_global_action(open);
|
||||
|
@ -587,7 +584,6 @@ impl Workspace {
|
|||
let left_sidebar = cx.add_view(|_| Sidebar::new(SidebarSide::Left));
|
||||
let right_sidebar = cx.add_view(|_| Sidebar::new(SidebarSide::Right));
|
||||
let left_sidebar_buttons = cx.add_view(|cx| SidebarButtons::new(left_sidebar.clone(), cx));
|
||||
let toggle_terminal = cx.add_view(|cx| TerminalButton::new(handle.clone(), cx));
|
||||
let toggle_dock = cx.add_view(|cx| ToggleDockButton::new(handle, cx));
|
||||
let right_sidebar_buttons =
|
||||
cx.add_view(|cx| SidebarButtons::new(right_sidebar.clone(), cx));
|
||||
|
@ -596,10 +592,6 @@ impl Workspace {
|
|||
status_bar.add_left_item(left_sidebar_buttons, cx);
|
||||
status_bar.add_right_item(right_sidebar_buttons, cx);
|
||||
status_bar.add_right_item(toggle_dock, cx);
|
||||
// TOOD: Remove this when things are done
|
||||
if **cx.default_global::<StaffMode>() {
|
||||
status_bar.add_right_item(toggle_terminal, cx);
|
||||
}
|
||||
status_bar
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue