pane: Disable the "Close..." controls depending on certain scenarios (#25037)

Closes https://github.com/zed-industries/zed/issues/12471

- Disables "Close Others" if there's just one tab
- Disables "Close Left"/"Close Right" if the above is true or if there's
no tabs to the left/right side of the active tab

Release Notes:

- N/A
This commit is contained in:
Danilo Leal 2025-02-17 18:01:13 -03:00 committed by GitHub
parent dc11a61ff8
commit a99696b95f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -40,11 +40,11 @@ use std::{
}; };
use theme::ThemeSettings; use theme::ThemeSettings;
use ui::{ use ui::{
prelude::*, right_click_menu, ButtonSize, Color, DecoratedIcon, IconButton, IconButtonShape, prelude::*, right_click_menu, ButtonSize, Color, ContextMenu, ContextMenuEntry,
IconDecoration, IconDecorationKind, IconName, IconSize, Indicator, Label, PopoverMenu, ContextMenuItem, DecoratedIcon, IconButton, IconButtonShape, IconDecoration,
PopoverMenuHandle, Tab, TabBar, TabPosition, Tooltip, IconDecorationKind, IconName, IconSize, Indicator, Label, PopoverMenu, PopoverMenuHandle, Tab,
TabBar, TabPosition, Tooltip,
}; };
use ui::{v_flex, ContextMenu};
use util::{debug_panic, maybe, truncate_and_remove_front, ResultExt}; use util::{debug_panic, maybe, truncate_and_remove_front, ResultExt};
/// A selected entry in e.g. project panel. /// A selected entry in e.g. project panel.
@ -2395,6 +2395,9 @@ impl Pane {
} }
}; };
let total_items = self.items.len();
let has_items_to_left = ix > 0;
let has_items_to_right = ix < total_items - 1;
let is_pinned = self.is_tab_pinned(ix); let is_pinned = self.is_tab_pinned(ix);
let pane = cx.entity().downgrade(); let pane = cx.entity().downgrade();
let menu_context = item.item_focus_handle(cx); let menu_context = item.item_focus_handle(cx);
@ -2415,24 +2418,28 @@ impl Pane {
.detach_and_log_err(cx); .detach_and_log_err(cx);
}), }),
) )
.entry( .item(ContextMenuItem::Entry(
"Close Others", ContextMenuEntry::new("Close Others")
Some(Box::new(CloseInactiveItems { .action(Some(Box::new(CloseInactiveItems {
save_intent: None, save_intent: None,
close_pinned: false, close_pinned: false,
})), })))
window.handler_for(&pane, move |pane, window, cx| { .disabled(total_items == 1)
pane.close_items(window, cx, SaveIntent::Close, |id| id != item_id) .handler(window.handler_for(&pane, move |pane, window, cx| {
pane.close_items(window, cx, SaveIntent::Close, |id| {
id != item_id
})
.detach_and_log_err(cx); .detach_and_log_err(cx);
}),
)
.separator()
.entry(
"Close Left",
Some(Box::new(CloseItemsToTheLeft {
close_pinned: false,
})), })),
window.handler_for(&pane, move |pane, window, cx| { ))
.separator()
.item(ContextMenuItem::Entry(
ContextMenuEntry::new("Close Left")
.action(Some(Box::new(CloseItemsToTheLeft {
close_pinned: false,
})))
.disabled(!has_items_to_left)
.handler(window.handler_for(&pane, move |pane, window, cx| {
pane.close_items_to_the_left_by_id( pane.close_items_to_the_left_by_id(
item_id, item_id,
&CloseItemsToTheLeft { &CloseItemsToTheLeft {
@ -2443,14 +2450,15 @@ impl Pane {
cx, cx,
) )
.detach_and_log_err(cx); .detach_and_log_err(cx);
}),
)
.entry(
"Close Right",
Some(Box::new(CloseItemsToTheRight {
close_pinned: false,
})), })),
window.handler_for(&pane, move |pane, window, cx| { ))
.item(ContextMenuItem::Entry(
ContextMenuEntry::new("Close Right")
.action(Some(Box::new(CloseItemsToTheRight {
close_pinned: false,
})))
.disabled(!has_items_to_right)
.handler(window.handler_for(&pane, move |pane, window, cx| {
pane.close_items_to_the_right_by_id( pane.close_items_to_the_right_by_id(
item_id, item_id,
&CloseItemsToTheRight { &CloseItemsToTheRight {
@ -2461,8 +2469,8 @@ impl Pane {
cx, cx,
) )
.detach_and_log_err(cx); .detach_and_log_err(cx);
}), })),
) ))
.separator() .separator()
.entry( .entry(
"Close Clean", "Close Clean",