Skip to content

Commit 7ed834b

Browse files
authored
terminal: Fix unresponsive buttons on load until center pane is clicked + Auto-focus docked terminal on load if no other item is focused (#23039)
Closes #23006 This PR should have been split into two, but since the changes are related, I merged them into one. 1. On load, the title bar actions and bottom bar toggles are unresponsive until the center pane is clicked. This happens because the terminal captures focus (even if it's closed) long after the workspace sets focus to itself during loading. The issue was in the `focus_view` call used in the `new` method of `TerminalPanel`. Since new terminal views can be created behind the scenes (i.e., without the terminal being visible to the user), we shouldn't handle focus for the terminal in this case. Removing `focus_view` from the `new` method has no impact on the existing terminal focusing logic. I've tested scenarios such as creating new terminals, splitting terminals, zooming, etc., and everything works as expected. 2. Currently, on load, docked terminals do not automatically focus when they are only visible item to the user. This PR implements it. Before/After: 1. When only the dock terminal is visible on load. Terminal is focused. <img src="https://github.com/user-attachments/assets/af8848aa-ccb5-4a3b-b2c6-486e8d588f09" alt="image" height="280px" /> <img src="https://github.com/user-attachments/assets/8f76ca2e-de29-4cc0-979b-749b50a00bbd" alt="image" height="280px" /> 2. When other items are visible along with the dock terminal on load. Editor is focused. <img src="https://github.com/user-attachments/assets/d3248272-a75d-4763-9e99-defb8a369b68" alt="image" height="280px" /> <img src="https://github.com/user-attachments/assets/fba5184e-1ab2-406c-9669-b141aaf1c32f" alt="image" height="280px" /> 3. Multiple tabs along with split panes. Last terminal is focused. <img src="https://github.com/user-attachments/assets/7a10c3cf-8bb3-4b88-aacc-732b678bee19" alt="image" height="270px" /> <img src="https://github.com/user-attachments/assets/4d16e98f-9d7a-45f6-8701-d6652e411d3b" alt="image" height="270px" /> Future: When a docked terminal is in a zoomed state and Zed is loaded, we should prioritize focusing on the terminal over the active item (e.g., an editor) behind it. This hasn't been implemented in this PR because the zoomed state during the load function is stale. The correct state is received later via the workspace. I'm still investigating where exactly this should be handled, so this will be a separate PR. cc: @SomeoneToIgnore Release Notes: - Fixed unresponsive buttons on load until the center pane is clicked. - Added auto-focus for the docked terminal on load when no other item is focused.
1 parent 13405ed commit 7ed834b

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

crates/terminal_view/src/persistence.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,16 @@ fn populate_pane_items(
146146
cx: &mut ViewContext<Pane>,
147147
) {
148148
let mut item_index = pane.items_len();
149+
let mut active_item_index = None;
149150
for item in items {
150-
let activate_item = Some(item.item_id().as_u64()) == active_item;
151+
if Some(item.item_id().as_u64()) == active_item {
152+
active_item_index = Some(item_index);
153+
}
151154
pane.add_item(Box::new(item), false, false, None, cx);
152155
item_index += 1;
153-
if activate_item {
154-
pane.activate_item(item_index, false, false, cx);
155-
}
156+
}
157+
if let Some(index) = active_item_index {
158+
pane.activate_item(index, false, false, cx);
156159
}
157160
}
158161

crates/terminal_view/src/terminal_panel.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use ui::{
3131
};
3232
use util::{ResultExt, TryFutureExt};
3333
use workspace::{
34-
dock::{DockPosition, Panel, PanelEvent},
34+
dock::{DockPosition, Panel, PanelEvent, PanelHandle},
3535
item::SerializableItem,
3636
move_active_item, move_item, pane,
3737
ui::IconName,
@@ -83,7 +83,6 @@ impl TerminalPanel {
8383
let project = workspace.project();
8484
let pane = new_terminal_pane(workspace.weak_handle(), project.clone(), false, cx);
8585
let center = PaneGroup::new(pane.clone());
86-
cx.focus_view(&pane);
8786
let terminal_panel = Self {
8887
center,
8988
active_pane: pane,
@@ -283,6 +282,25 @@ impl TerminalPanel {
283282
}
284283
}
285284

285+
if let Some(workspace) = workspace.upgrade() {
286+
let should_focus = workspace
287+
.update(&mut cx, |workspace, cx| {
288+
workspace.active_item(cx).is_none()
289+
&& workspace.is_dock_at_position_open(terminal_panel.position(cx), cx)
290+
})
291+
.unwrap_or(false);
292+
293+
if should_focus {
294+
terminal_panel
295+
.update(&mut cx, |panel, cx| {
296+
panel.active_pane.update(cx, |pane, cx| {
297+
pane.focus_active_item(cx);
298+
});
299+
})
300+
.ok();
301+
}
302+
}
303+
286304
Ok(terminal_panel)
287305
}
288306

crates/workspace/src/workspace.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,19 @@ impl Workspace {
22982298
}
22992299
}
23002300

2301+
pub fn is_dock_at_position_open(
2302+
&self,
2303+
position: DockPosition,
2304+
cx: &mut ViewContext<Self>,
2305+
) -> bool {
2306+
let dock = match position {
2307+
DockPosition::Left => &self.left_dock,
2308+
DockPosition::Bottom => &self.bottom_dock,
2309+
DockPosition::Right => &self.right_dock,
2310+
};
2311+
dock.read(cx).is_open()
2312+
}
2313+
23012314
pub fn toggle_dock(&mut self, dock_side: DockPosition, cx: &mut ViewContext<Self>) {
23022315
let dock = match dock_side {
23032316
DockPosition::Left => &self.left_dock,

0 commit comments

Comments
 (0)