Merge pull request #1355 from zed-industries/better-tab-ordering

More intuitive tab ordering
This commit is contained in:
Keith Simmons 2022-07-14 12:26:46 -07:00 committed by GitHub
commit 17f52fb587
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 17 deletions

View file

@ -71,10 +71,10 @@ const MAX_NAVIGATION_HISTORY_LEN: usize = 1024;
pub fn init(cx: &mut MutableAppContext) { pub fn init(cx: &mut MutableAppContext) {
cx.add_action(|pane: &mut Pane, action: &ActivateItem, cx| { cx.add_action(|pane: &mut Pane, action: &ActivateItem, cx| {
pane.activate_item(action.0, true, true, cx); pane.activate_item(action.0, true, true, false, cx);
}); });
cx.add_action(|pane: &mut Pane, _: &ActivateLastItem, cx| { cx.add_action(|pane: &mut Pane, _: &ActivateLastItem, cx| {
pane.activate_item(pane.items.len() - 1, true, true, cx); pane.activate_item(pane.items.len() - 1, true, true, false, cx);
}); });
cx.add_action(|pane: &mut Pane, _: &ActivatePrevItem, cx| { cx.add_action(|pane: &mut Pane, _: &ActivatePrevItem, cx| {
pane.activate_prev_item(cx); pane.activate_prev_item(cx);
@ -288,7 +288,7 @@ impl Pane {
{ {
let prev_active_item_index = pane.active_item_index; let prev_active_item_index = pane.active_item_index;
pane.nav_history.borrow_mut().set_mode(mode); pane.nav_history.borrow_mut().set_mode(mode);
pane.activate_item(index, true, true, cx); pane.activate_item(index, true, true, false, cx);
pane.nav_history pane.nav_history
.borrow_mut() .borrow_mut()
.set_mode(NavigationMode::Normal); .set_mode(NavigationMode::Normal);
@ -380,7 +380,7 @@ impl Pane {
&& item.project_entry_ids(cx).as_slice() == &[project_entry_id] && item.project_entry_ids(cx).as_slice() == &[project_entry_id]
{ {
let item = item.boxed_clone(); let item = item.boxed_clone();
pane.activate_item(ix, true, focus_item, cx); pane.activate_item(ix, true, focus_item, true, cx);
return Some(item); return Some(item);
} }
} }
@ -404,9 +404,11 @@ impl Pane {
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) { ) {
// Prevent adding the same item to the pane more than once. // Prevent adding the same item to the pane more than once.
// If there is already an active item, reorder the desired item to be after it
// and activate it.
if let Some(item_ix) = pane.read(cx).items.iter().position(|i| i.id() == item.id()) { if let Some(item_ix) = pane.read(cx).items.iter().position(|i| i.id() == item.id()) {
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
pane.activate_item(item_ix, activate_pane, focus_item, cx) pane.activate_item(item_ix, activate_pane, focus_item, true, cx)
}); });
return; return;
} }
@ -426,7 +428,7 @@ impl Pane {
}; };
pane.items.insert(item_ix, item); pane.items.insert(item_ix, item);
pane.activate_item(item_ix, activate_pane, focus_item, cx); pane.activate_item(item_ix, activate_pane, focus_item, false, cx);
cx.notify(); cx.notify();
}); });
} }
@ -465,13 +467,31 @@ impl Pane {
pub fn activate_item( pub fn activate_item(
&mut self, &mut self,
index: usize, mut index: usize,
activate_pane: bool, activate_pane: bool,
focus_item: bool, focus_item: bool,
move_after_current_active: bool,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
use NavigationMode::{GoingBack, GoingForward}; use NavigationMode::{GoingBack, GoingForward};
if index < self.items.len() { if index < self.items.len() {
if move_after_current_active {
// If there is already an active item, reorder the desired item to be after it
// and activate it.
if self.active_item_index != index && self.active_item_index < self.items.len() {
let pane_to_activate = self.items.remove(index);
if self.active_item_index < index {
index = self.active_item_index + 1;
} else if self.active_item_index < self.items.len() + 1 {
index = self.active_item_index;
// Index is less than active_item_index. Reordering will decrement the
// active_item_index, so adjust it accordingly
self.active_item_index = index - 1;
}
self.items.insert(index, pane_to_activate);
}
}
let prev_active_item_ix = mem::replace(&mut self.active_item_index, index); let prev_active_item_ix = mem::replace(&mut self.active_item_index, index);
if prev_active_item_ix != self.active_item_index if prev_active_item_ix != self.active_item_index
|| matches!(self.nav_history.borrow().mode, GoingBack | GoingForward) || matches!(self.nav_history.borrow().mode, GoingBack | GoingForward)
@ -502,7 +522,7 @@ impl Pane {
} else if self.items.len() > 0 { } else if self.items.len() > 0 {
index = self.items.len() - 1; index = self.items.len() - 1;
} }
self.activate_item(index, true, true, cx); self.activate_item(index, true, true, false, cx);
} }
pub fn activate_next_item(&mut self, cx: &mut ViewContext<Self>) { pub fn activate_next_item(&mut self, cx: &mut ViewContext<Self>) {
@ -512,7 +532,7 @@ impl Pane {
} else { } else {
index = 0; index = 0;
} }
self.activate_item(index, true, true, cx); self.activate_item(index, true, true, false, cx);
} }
pub fn close_active_item( pub fn close_active_item(
@ -641,10 +661,13 @@ impl Pane {
pane.update(&mut cx, |pane, cx| { pane.update(&mut cx, |pane, cx| {
if let Some(item_ix) = pane.items.iter().position(|i| i.id() == item.id()) { if let Some(item_ix) = pane.items.iter().position(|i| i.id() == item.id()) {
if item_ix == pane.active_item_index { if item_ix == pane.active_item_index {
if item_ix + 1 < pane.items.len() { // Activate the previous item if possible.
pane.activate_next_item(cx); // This returns the user to the previously opened tab if they closed
} else if item_ix > 0 { // a ne item they just navigated to.
if item_ix > 0 {
pane.activate_prev_item(cx); pane.activate_prev_item(cx);
} else if item_ix + 1 < pane.items.len() {
pane.activate_next_item(cx);
} }
} }
@ -712,7 +735,7 @@ impl Pane {
if has_conflict && can_save { if has_conflict && can_save {
let mut answer = pane.update(cx, |pane, cx| { let mut answer = pane.update(cx, |pane, cx| {
pane.activate_item(item_ix, true, true, cx); pane.activate_item(item_ix, true, true, false, cx);
cx.prompt( cx.prompt(
PromptLevel::Warning, PromptLevel::Warning,
CONFLICT_MESSAGE, CONFLICT_MESSAGE,
@ -733,7 +756,7 @@ impl Pane {
}); });
let should_save = if should_prompt_for_save && !will_autosave { let should_save = if should_prompt_for_save && !will_autosave {
let mut answer = pane.update(cx, |pane, cx| { let mut answer = pane.update(cx, |pane, cx| {
pane.activate_item(item_ix, true, true, cx); pane.activate_item(item_ix, true, true, false, cx);
cx.prompt( cx.prompt(
PromptLevel::Warning, PromptLevel::Warning,
DIRTY_MESSAGE, DIRTY_MESSAGE,

View file

@ -562,7 +562,7 @@ impl<T: Item> ItemHandle for ViewHandle<T> {
if T::should_activate_item_on_event(event) { if T::should_activate_item_on_event(event) {
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
if let Some(ix) = pane.index_for_item(&item) { if let Some(ix) = pane.index_for_item(&item) {
pane.activate_item(ix, true, true, cx); pane.activate_item(ix, true, true, false, cx);
pane.activate(cx); pane.activate(cx);
} }
}); });
@ -1507,7 +1507,7 @@ impl Workspace {
}); });
if let Some((pane, ix)) = result { if let Some((pane, ix)) = result {
self.activate_pane(pane.clone(), cx); self.activate_pane(pane.clone(), cx);
pane.update(cx, |pane, cx| pane.activate_item(ix, true, true, cx)); pane.update(cx, |pane, cx| pane.activate_item(ix, true, true, false, cx));
true true
} else { } else {
false false
@ -2880,7 +2880,7 @@ mod tests {
let close_items = workspace.update(cx, |workspace, cx| { let close_items = workspace.update(cx, |workspace, cx| {
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
pane.activate_item(1, true, true, cx); pane.activate_item(1, true, true, false, cx);
assert_eq!(pane.active_item().unwrap().id(), item2.id()); assert_eq!(pane.active_item().unwrap().id(), item2.id());
}); });