Address some issues where panes don't get focused properly, make the focused pane more obvious, and prevent splitting of the pane with no items

This commit is contained in:
K Simmons 2022-07-20 18:52:32 -07:00
parent d76cdb01be
commit 225055ed5d
13 changed files with 154 additions and 70 deletions

View File

@ -355,7 +355,7 @@ impl ContextMenu {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_mouse_down_out(MouseButton::Left, |_, cx| cx.dispatch_action(Cancel)) .on_down_out(MouseButton::Left, |_, cx| cx.dispatch_action(Cancel))
.on_mouse_down_out(MouseButton::Right, |_, cx| cx.dispatch_action(Cancel)) .on_down_out(MouseButton::Right, |_, cx| cx.dispatch_action(Cancel))
} }
} }

View File

@ -707,7 +707,7 @@ impl CompletionsMenu {
}, },
) )
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_mouse_down(MouseButton::Left, move |_, cx| { .on_down(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ConfirmCompletion { cx.dispatch_action(ConfirmCompletion {
item_ix: Some(item_ix), item_ix: Some(item_ix),
}); });
@ -840,7 +840,7 @@ impl CodeActionsMenu {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_mouse_down(MouseButton::Left, move |_, cx| { .on_down(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ConfirmCodeAction { cx.dispatch_action(ConfirmCodeAction {
item_ix: Some(item_ix), item_ix: Some(item_ix),
}); });
@ -2674,7 +2674,7 @@ impl Editor {
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.with_padding(Padding::uniform(3.)) .with_padding(Padding::uniform(3.))
.on_mouse_down(MouseButton::Left, |_, cx| { .on_down(MouseButton::Left, |_, cx| {
cx.dispatch_action(ToggleCodeActions { cx.dispatch_action(ToggleCodeActions {
deployed_from_indicator: true, deployed_from_indicator: true,
}); });

View File

@ -7,7 +7,9 @@ use settings::Settings;
use util::TryFutureExt; use util::TryFutureExt;
use workspace::Workspace; use workspace::Workspace;
use crate::{Anchor, DisplayPoint, Editor, EditorSnapshot, GoToDefinition, Select, SelectPhase}; use crate::{
Anchor, DisplayPoint, Editor, EditorSnapshot, Event, GoToDefinition, Select, SelectPhase,
};
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub struct UpdateGoToDefinitionLink { pub struct UpdateGoToDefinitionLink {
@ -276,6 +278,13 @@ pub fn go_to_fetched_definition(
}); });
if !definitions.is_empty() { if !definitions.is_empty() {
editor_handle.update(cx, |editor, cx| {
if !editor.focused {
cx.focus_self();
cx.emit(Event::Activate);
}
});
Editor::navigate_to_definitions(workspace, editor_handle, definitions, cx); Editor::navigate_to_definitions(workspace, editor_handle, definitions, cx);
} else { } else {
editor_handle.update(cx, |editor, cx| { editor_handle.update(cx, |editor, cx| {

View File

@ -2,7 +2,7 @@ use context_menu::ContextMenuItem;
use gpui::{geometry::vector::Vector2F, impl_internal_actions, MutableAppContext, ViewContext}; use gpui::{geometry::vector::Vector2F, impl_internal_actions, MutableAppContext, ViewContext};
use crate::{ use crate::{
DisplayPoint, Editor, EditorMode, FindAllReferences, GoToDefinition, Rename, SelectMode, DisplayPoint, Editor, EditorMode, Event, FindAllReferences, GoToDefinition, Rename, SelectMode,
ToggleCodeActions, ToggleCodeActions,
}; };
@ -23,6 +23,11 @@ pub fn deploy_context_menu(
&DeployMouseContextMenu { position, point }: &DeployMouseContextMenu, &DeployMouseContextMenu { position, point }: &DeployMouseContextMenu,
cx: &mut ViewContext<Editor>, cx: &mut ViewContext<Editor>,
) { ) {
if !editor.focused {
cx.focus_self();
cx.emit(Event::Activate);
}
// Don't show context menu for inline editors // Don't show context menu for inline editors
if editor.mode() != EditorMode::Full { if editor.mode() != EditorMode::Full {
return; return;

View File

@ -43,7 +43,7 @@ impl MouseEventHandler {
self self
} }
pub fn on_mouse_down( pub fn on_down(
mut self, mut self,
button: MouseButton, button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static, handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
@ -61,7 +61,7 @@ impl MouseEventHandler {
self self
} }
pub fn on_mouse_down_out( pub fn on_down_out(
mut self, mut self,
button: MouseButton, button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static, handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,

View File

@ -90,7 +90,7 @@ impl<D: PickerDelegate> View for Picker<D> {
.read(cx) .read(cx)
.render_match(ix, state, ix == selected_ix, cx) .render_match(ix, state, ix == selected_ix, cx)
}) })
.on_mouse_down(MouseButton::Left, move |_, cx| { .on_down(MouseButton::Left, move |_, cx| {
cx.dispatch_action(SelectIndex(ix)) cx.dispatch_action(SelectIndex(ix))
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)

View File

@ -1082,7 +1082,7 @@ impl ProjectPanel {
} }
}, },
) )
.on_mouse_down( .on_down(
MouseButton::Right, MouseButton::Right,
move |MouseButtonEvent { position, .. }, cx| { move |MouseButtonEvent { position, .. }, cx| {
cx.dispatch_action(DeployContextMenu { entry_id, position }) cx.dispatch_action(DeployContextMenu { entry_id, position })
@ -1134,7 +1134,7 @@ impl View for ProjectPanel {
.expanded() .expanded()
.boxed() .boxed()
}) })
.on_mouse_down( .on_down(
MouseButton::Right, MouseButton::Right,
move |MouseButtonEvent { position, .. }, cx| { move |MouseButtonEvent { position, .. }, cx| {
// When deploying the context menu anywhere below the last project entry, // When deploying the context menu anywhere below the last project entry,

View File

@ -147,6 +147,7 @@ impl ProjectSearch {
pub enum ViewEvent { pub enum ViewEvent {
UpdateTab, UpdateTab,
Activate,
EditorEvent(editor::Event), EditorEvent(editor::Event),
} }
@ -162,7 +163,9 @@ impl View for ProjectSearchView {
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox { fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
let model = &self.model.read(cx); let model = &self.model.read(cx);
if model.match_ranges.is_empty() { if model.match_ranges.is_empty() {
let theme = &cx.global::<Settings>().theme; enum Status {}
let theme = cx.global::<Settings>().theme.clone();
let text = if self.query_editor.read(cx).text(cx).is_empty() { let text = if self.query_editor.read(cx).text(cx).is_empty() {
"" ""
} else if model.pending_search.is_some() { } else if model.pending_search.is_some() {
@ -170,12 +173,18 @@ impl View for ProjectSearchView {
} else { } else {
"No results" "No results"
}; };
Label::new(text.to_string(), theme.search.results_status.clone()) MouseEventHandler::new::<Status, _, _>(0, cx, |_, _| {
.aligned() Label::new(text.to_string(), theme.search.results_status.clone())
.contained() .aligned()
.with_background_color(theme.editor.background) .contained()
.flex(1., true) .with_background_color(theme.editor.background)
.boxed() .flex(1., true)
.boxed()
})
.on_down(MouseButton::Left, |_, cx| {
cx.focus_parent_view();
})
.boxed()
} else { } else {
ChildView::new(&self.results_editor).flex(1., true).boxed() ChildView::new(&self.results_editor).flex(1., true).boxed()
} }

View File

@ -38,8 +38,10 @@ pub struct Theme {
pub struct Workspace { pub struct Workspace {
pub background: Color, pub background: Color,
pub titlebar: Titlebar, pub titlebar: Titlebar,
pub tab: Tab, pub focused_active_tab: Tab,
pub active_tab: Tab, pub focused_inactive_tab: Tab,
pub unfocused_active_tab: Tab,
pub unfocused_inactive_tab: Tab,
pub pane_button: Interactive<IconButton>, pub pane_button: Interactive<IconButton>,
pub pane_divider: Border, pub pane_divider: Border,
pub leader_border_opacity: f32, pub leader_border_opacity: f32,

View File

@ -1,5 +1,5 @@
use super::{ItemHandle, SplitDirection}; use super::{ItemHandle, SplitDirection};
use crate::{toolbar::Toolbar, Item, WeakItemHandle, Workspace}; use crate::{toolbar::Toolbar, Item, NewFile, WeakItemHandle, Workspace};
use anyhow::Result; use anyhow::Result;
use collections::{HashMap, HashSet, VecDeque}; use collections::{HashMap, HashSet, VecDeque};
use context_menu::{ContextMenu, ContextMenuItem}; use context_menu::{ContextMenu, ContextMenuItem};
@ -136,6 +136,7 @@ pub enum Event {
pub struct Pane { pub struct Pane {
items: Vec<Box<dyn ItemHandle>>, items: Vec<Box<dyn ItemHandle>>,
is_active: bool,
active_item_index: usize, active_item_index: usize,
autoscroll: bool, autoscroll: bool,
nav_history: Rc<RefCell<NavHistory>>, nav_history: Rc<RefCell<NavHistory>>,
@ -184,6 +185,7 @@ impl Pane {
let split_menu = cx.add_view(|cx| ContextMenu::new(cx)); let split_menu = cx.add_view(|cx| ContextMenu::new(cx));
Self { Self {
items: Vec::new(), items: Vec::new(),
is_active: true,
active_item_index: 0, active_item_index: 0,
autoscroll: false, autoscroll: false,
nav_history: Rc::new(RefCell::new(NavHistory { nav_history: Rc::new(RefCell::new(NavHistory {
@ -199,6 +201,11 @@ impl Pane {
} }
} }
pub fn set_active(&mut self, is_active: bool, cx: &mut ViewContext<Self>) {
self.is_active = is_active;
cx.notify();
}
pub fn nav_history_for_item<T: Item>(&self, item: &ViewHandle<T>) -> ItemNavHistory { pub fn nav_history_for_item<T: Item>(&self, item: &ViewHandle<T>) -> ItemNavHistory {
ItemNavHistory { ItemNavHistory {
history: self.nav_history.clone(), history: self.nav_history.clone(),
@ -865,26 +872,23 @@ impl Pane {
None None
}; };
let is_pane_active = self.is_active;
let mut row = Flex::row().scrollable::<Tabs, _>(1, autoscroll, cx); let mut row = Flex::row().scrollable::<Tabs, _>(1, autoscroll, cx);
for (ix, (item, detail)) in self.items.iter().zip(self.tab_details(cx)).enumerate() { for (ix, (item, detail)) in self.items.iter().zip(self.tab_details(cx)).enumerate() {
let detail = if detail == 0 { None } else { Some(detail) }; let detail = if detail == 0 { None } else { Some(detail) };
let is_active = ix == self.active_item_index; let is_tab_active = ix == self.active_item_index;
row.add_child({ row.add_child({
let tab_style = if is_active { let mut tab_style = match (is_pane_active, is_tab_active) {
theme.workspace.active_tab.clone() (true, true) => theme.workspace.focused_active_tab.clone(),
} else { (true, false) => theme.workspace.focused_inactive_tab.clone(),
theme.workspace.tab.clone() (false, true) => theme.workspace.unfocused_active_tab.clone(),
(false, false) => theme.workspace.unfocused_inactive_tab.clone(),
}; };
let title = item.tab_content(detail, &tab_style, cx); let title = item.tab_content(detail, &tab_style, cx);
let mut style = if is_active {
theme.workspace.active_tab.clone()
} else {
theme.workspace.tab.clone()
};
if ix == 0 { if ix == 0 {
style.container.border.left = false; tab_style.container.border.left = false;
} }
MouseEventHandler::new::<Tab, _, _>(ix, cx, |_, cx| { MouseEventHandler::new::<Tab, _, _>(ix, cx, |_, cx| {
@ -894,9 +898,9 @@ impl Pane {
Align::new({ Align::new({
let diameter = 7.0; let diameter = 7.0;
let icon_color = if item.has_conflict(cx) { let icon_color = if item.has_conflict(cx) {
Some(style.icon_conflict) Some(tab_style.icon_conflict)
} else if item.is_dirty(cx) { } else if item.is_dirty(cx) {
Some(style.icon_dirty) Some(tab_style.icon_dirty)
} else { } else {
None None
}; };
@ -928,8 +932,8 @@ impl Pane {
Container::new(Align::new(title).boxed()) Container::new(Align::new(title).boxed())
.with_style(ContainerStyle { .with_style(ContainerStyle {
margin: Margin { margin: Margin {
left: style.spacing, left: tab_style.spacing,
right: style.spacing, right: tab_style.spacing,
..Default::default() ..Default::default()
}, },
..Default::default() ..Default::default()
@ -947,10 +951,11 @@ impl Pane {
cx, cx,
|mouse_state, _| { |mouse_state, _| {
if mouse_state.hovered { if mouse_state.hovered {
icon.with_color(style.icon_close_active) icon.with_color(tab_style.icon_close_active)
.boxed() .boxed()
} else { } else {
icon.with_color(style.icon_close).boxed() icon.with_color(tab_style.icon_close)
.boxed()
} }
}, },
) )
@ -969,27 +974,39 @@ impl Pane {
} else { } else {
Empty::new().boxed() Empty::new().boxed()
}) })
.with_width(style.icon_width) .with_width(tab_style.icon_width)
.boxed(), .boxed(),
) )
.boxed(), .boxed(),
) )
.boxed(), .boxed(),
) )
.with_style(style.container) .with_style(tab_style.container)
.boxed() .boxed()
}) })
.on_mouse_down(MouseButton::Left, move |_, cx| { .with_cursor_style(if is_tab_active && is_pane_active {
CursorStyle::Arrow
} else {
CursorStyle::PointingHand
})
.on_down(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ActivateItem(ix)); cx.dispatch_action(ActivateItem(ix));
}) })
.boxed() .boxed()
}) })
} }
let filler_style = if is_pane_active {
&theme.workspace.focused_inactive_tab
} else {
&theme.workspace.unfocused_inactive_tab
};
row.add_child( row.add_child(
Empty::new() Empty::new()
.contained() .contained()
.with_border(theme.workspace.tab.container.border) .with_style(filler_style.container)
.with_border(theme.workspace.focused_active_tab.container.border)
.flex(0., true) .flex(0., true)
.named("filler"), .named("filler"),
); );
@ -1054,10 +1071,12 @@ impl View for Pane {
.with_child( .with_child(
EventHandler::new(if let Some(active_item) = self.active_item() { EventHandler::new(if let Some(active_item) = self.active_item() {
Flex::column() Flex::column()
.with_child( .with_child({
Flex::row() let mut tab_row = Flex::row()
.with_child(self.render_tabs(cx).flex(1., true).named("tabs")) .with_child(self.render_tabs(cx).flex(1., true).named("tabs"));
.with_child(
if self.is_active {
tab_row.add_child(
MouseEventHandler::new::<SplitIcon, _, _>( MouseEventHandler::new::<SplitIcon, _, _>(
0, 0,
cx, cx,
@ -1080,7 +1099,7 @@ impl View for Pane {
}, },
) )
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_mouse_down( .on_down(
MouseButton::Left, MouseButton::Left,
|MouseButtonEvent { position, .. }, cx| { |MouseButtonEvent { position, .. }, cx| {
cx.dispatch_action(DeploySplitMenu { position }); cx.dispatch_action(DeploySplitMenu { position });
@ -1088,15 +1107,36 @@ impl View for Pane {
) )
.boxed(), .boxed(),
) )
}
tab_row
.constrained() .constrained()
.with_height(cx.global::<Settings>().theme.workspace.tab.height) .with_height(
.boxed(), cx.global::<Settings>()
) .theme
.workspace
.focused_active_tab
.height,
)
.boxed()
})
.with_child(ChildView::new(&self.toolbar).boxed()) .with_child(ChildView::new(&self.toolbar).boxed())
.with_child(ChildView::new(active_item).flex(1., true).boxed()) .with_child(ChildView::new(active_item).flex(1., true).boxed())
.boxed() .boxed()
} else { } else {
Empty::new().boxed() enum EmptyPane {}
let theme = cx.global::<Settings>().theme.clone();
MouseEventHandler::new::<EmptyPane, _, _>(0, cx, |_, _| {
Empty::new()
.contained()
.with_background_color(theme.editor.background)
.boxed()
})
.on_down(MouseButton::Left, |_, cx| {
cx.focus_parent_view();
})
.boxed()
}) })
.on_navigate_mouse_down(move |direction, cx| { .on_navigate_mouse_down(move |direction, cx| {
let this = this.clone(); let this = this.clone();

View File

@ -187,7 +187,7 @@ impl Sidebar {
..Default::default() ..Default::default()
}) })
.with_cursor_style(CursorStyle::ResizeLeftRight) .with_cursor_style(CursorStyle::ResizeLeftRight)
.on_mouse_down(MouseButton::Left, |_, _| {}) // This prevents the mouse down event from being propagated elsewhere .on_down(MouseButton::Left, |_, _| {}) // This prevents the mouse down event from being propagated elsewhere
.on_drag( .on_drag(
MouseButton::Left, MouseButton::Left,
move |old_position, move |old_position,

View File

@ -1566,7 +1566,11 @@ impl Workspace {
fn activate_pane(&mut self, pane: ViewHandle<Pane>, cx: &mut ViewContext<Self>) { fn activate_pane(&mut self, pane: ViewHandle<Pane>, cx: &mut ViewContext<Self>) {
if self.active_pane != pane { if self.active_pane != pane {
self.active_pane
.update(cx, |pane, cx| pane.set_active(false, cx));
self.active_pane = pane.clone(); self.active_pane = pane.clone();
self.active_pane
.update(cx, |pane, cx| pane.set_active(true, cx));
self.status_bar.update(cx, |status_bar, cx| { self.status_bar.update(cx, |status_bar, cx| {
status_bar.set_active_pane(&self.active_pane, cx); status_bar.set_active_pane(&self.active_pane, cx);
}); });
@ -1629,17 +1633,17 @@ impl Workspace {
pane: ViewHandle<Pane>, pane: ViewHandle<Pane>,
direction: SplitDirection, direction: SplitDirection,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> ViewHandle<Pane> { ) -> Option<ViewHandle<Pane>> {
let new_pane = self.add_pane(cx); pane.read(cx).active_item().map(|item| {
self.activate_pane(new_pane.clone(), cx); let new_pane = self.add_pane(cx);
if let Some(item) = pane.read(cx).active_item() { self.activate_pane(new_pane.clone(), cx);
if let Some(clone) = item.clone_on_split(cx.as_mut()) { if let Some(clone) = item.clone_on_split(cx.as_mut()) {
Pane::add_item(self, new_pane.clone(), clone, true, true, cx); Pane::add_item(self, new_pane.clone(), clone, true, true, cx);
} }
} self.center.split(&pane, &new_pane, direction).unwrap();
self.center.split(&pane, &new_pane, direction).unwrap(); cx.notify();
cx.notify(); new_pane
new_pane })
} }
fn remove_pane(&mut self, pane: ViewHandle<Pane>, cx: &mut ViewContext<Self>) { fn remove_pane(&mut self, pane: ViewHandle<Pane>, cx: &mut ViewContext<Self>) {
@ -3045,7 +3049,9 @@ mod tests {
// multi-entry items: (3, 4) // multi-entry items: (3, 4)
let left_pane = workspace.update(cx, |workspace, cx| { let left_pane = workspace.update(cx, |workspace, cx| {
let left_pane = workspace.active_pane().clone(); let left_pane = workspace.active_pane().clone();
let right_pane = workspace.split_pane(left_pane.clone(), SplitDirection::Right, cx); let right_pane = workspace
.split_pane(left_pane.clone(), SplitDirection::Right, cx)
.unwrap();
workspace.activate_pane(left_pane.clone(), cx); workspace.activate_pane(left_pane.clone(), cx);
workspace.add_item(Box::new(cx.add_view(|_| item_2_3.clone())), cx); workspace.add_item(Box::new(cx.add_view(|_| item_2_3.clone())), cx);

View File

@ -14,7 +14,7 @@ export function workspaceBackground(theme: Theme) {
} }
export default function workspace(theme: Theme) { export default function workspace(theme: Theme) {
const tab = { const focusedInactiveTab = {
height: 32, height: 32,
background: workspaceBackground(theme), background: workspaceBackground(theme),
iconClose: iconColor(theme, "muted"), iconClose: iconColor(theme, "muted"),
@ -39,16 +39,27 @@ export default function workspace(theme: Theme) {
} }
}; };
const activeTab = { const focusedActiveTab = {
...tab, ...focusedInactiveTab,
background: backgroundColor(theme, 500), background: backgroundColor(theme, 500),
text: text(theme, "sans", "active", { size: "sm" }), text: text(theme, "sans", "active", { size: "sm" }),
border: { border: {
...tab.border, ...focusedInactiveTab.border,
bottom: false, bottom: false,
}, },
}; };
const unfocusedInactiveTab = {
...focusedInactiveTab,
background: backgroundColor(theme, 100),
text: text(theme, "sans", "placeholder", { size: "sm" }),
};
const unfocusedActiveTab = {
...focusedInactiveTab,
text: text(theme, "sans", "placeholder", { size: "sm" }),
}
const titlebarPadding = 6; const titlebarPadding = 6;
return { return {
@ -63,15 +74,17 @@ export default function workspace(theme: Theme) {
}, },
leaderBorderOpacity: 0.7, leaderBorderOpacity: 0.7,
leaderBorderWidth: 2.0, leaderBorderWidth: 2.0,
tab, focusedActiveTab,
activeTab, focusedInactiveTab,
unfocusedActiveTab,
unfocusedInactiveTab,
paneButton: { paneButton: {
color: iconColor(theme, "secondary"), color: iconColor(theme, "secondary"),
border: { border: {
...tab.border, ...focusedActiveTab.border,
}, },
iconWidth: 12, iconWidth: 12,
buttonWidth: tab.height, buttonWidth: focusedActiveTab.height,
hover: { hover: {
color: iconColor(theme, "active"), color: iconColor(theme, "active"),
background: backgroundColor(theme, 300), background: backgroundColor(theme, 300),