Go back to "create branch" in the list (#26433)

Closes #ISSUE

Release Notes:

- N/A
This commit is contained in:
Conrad Irwin 2025-03-10 22:24:52 -06:00 committed by GitHub
parent c2e4fdf63d
commit ff1d78df3b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,7 +12,7 @@ use project::git::Repository;
use std::sync::Arc; use std::sync::Arc;
use time::OffsetDateTime; use time::OffsetDateTime;
use time_format::format_local_timestamp; use time_format::format_local_timestamp;
use ui::{prelude::*, HighlightedLabel, KeyBinding, ListItem, ListItemSpacing, Tooltip}; use ui::{prelude::*, HighlightedLabel, ListItem, ListItemSpacing};
use util::ResultExt; use util::ResultExt;
use workspace::notifications::DetachAndPromptErr; use workspace::notifications::DetachAndPromptErr;
use workspace::{ModalView, Workspace}; use workspace::{ModalView, Workspace};
@ -169,6 +169,7 @@ impl Render for BranchList {
struct BranchEntry { struct BranchEntry {
branch: Branch, branch: Branch,
positions: Vec<usize>, positions: Vec<usize>,
is_new: bool,
} }
pub struct BranchListDelegate { pub struct BranchListDelegate {
@ -194,10 +195,6 @@ impl BranchListDelegate {
} }
} }
fn has_exact_match(&self, target: &str) -> bool {
self.matches.iter().any(|entry| entry.branch.name == target)
}
fn create_branch( fn create_branch(
&self, &self,
new_branch_name: SharedString, new_branch_name: SharedString,
@ -257,13 +254,14 @@ impl PickerDelegate for BranchListDelegate {
const RECENT_BRANCHES_COUNT: usize = 10; const RECENT_BRANCHES_COUNT: usize = 10;
cx.spawn_in(window, move |picker, mut cx| async move { cx.spawn_in(window, move |picker, mut cx| async move {
let matches = if query.is_empty() { let mut matches: Vec<BranchEntry> = if query.is_empty() {
all_branches all_branches
.into_iter() .into_iter()
.take(RECENT_BRANCHES_COUNT) .take(RECENT_BRANCHES_COUNT)
.map(|branch| BranchEntry { .map(|branch| BranchEntry {
branch, branch,
positions: Vec::new(), positions: Vec::new(),
is_new: false,
}) })
.collect() .collect()
} else { } else {
@ -286,11 +284,29 @@ impl PickerDelegate for BranchListDelegate {
.map(|candidate| BranchEntry { .map(|candidate| BranchEntry {
branch: all_branches[candidate.candidate_id].clone(), branch: all_branches[candidate.candidate_id].clone(),
positions: candidate.positions, positions: candidate.positions,
is_new: false,
}) })
.collect() .collect()
}; };
picker picker
.update(&mut cx, |picker, _| { .update(&mut cx, |picker, _| {
#[allow(clippy::nonminimal_bool)]
if !query.is_empty()
&& !matches
.first()
.is_some_and(|entry| entry.branch.name == query)
{
matches.push(BranchEntry {
branch: Branch {
name: query.clone().into(),
is_head: false,
upstream: None,
most_recent_commit: None,
},
positions: Vec::new(),
is_new: true,
})
}
let delegate = &mut picker.delegate; let delegate = &mut picker.delegate;
delegate.matches = matches; delegate.matches = matches;
if delegate.matches.is_empty() { if delegate.matches.is_empty() {
@ -305,19 +321,14 @@ impl PickerDelegate for BranchListDelegate {
}) })
} }
fn confirm(&mut self, secondary: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) { fn confirm(&mut self, _secondary: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) {
let new_branch_name = SharedString::from(self.last_query.trim().replace(" ", "-"));
if !new_branch_name.is_empty()
&& !self.has_exact_match(&new_branch_name)
&& ((self.selected_index == 0 && self.matches.len() == 0) || secondary)
{
self.create_branch(new_branch_name, window, cx);
return;
}
let Some(entry) = self.matches.get(self.selected_index()) else { let Some(entry) = self.matches.get(self.selected_index()) else {
return; return;
}; };
if entry.is_new {
self.create_branch(entry.branch.name.clone(), window, cx);
return;
}
let current_branch = self.repo.as_ref().map(|repo| { let current_branch = self.repo.as_ref().map(|repo| {
repo.update(cx, |repo, _| { repo.update(cx, |repo, _| {
@ -377,16 +388,16 @@ impl PickerDelegate for BranchListDelegate {
ix: usize, ix: usize,
selected: bool, selected: bool,
_window: &mut Window, _window: &mut Window,
_cx: &mut Context<Picker<Self>>, cx: &mut Context<Picker<Self>>,
) -> Option<Self::ListItem> { ) -> Option<Self::ListItem> {
let entry = &self.matches[ix]; let entry = &self.matches[ix];
let (commit_time, commit_message) = entry let (commit_time, subject) = entry
.branch .branch
.most_recent_commit .most_recent_commit
.as_ref() .as_ref()
.map(|commit| { .map(|commit| {
let commit_message = commit.subject.clone(); let subject = commit.subject.clone();
let commit_time = OffsetDateTime::from_unix_timestamp(commit.commit_timestamp) let commit_time = OffsetDateTime::from_unix_timestamp(commit.commit_timestamp)
.unwrap_or_else(|_| OffsetDateTime::now_utc()); .unwrap_or_else(|_| OffsetDateTime::now_utc());
let formatted_time = format_local_timestamp( let formatted_time = format_local_timestamp(
@ -394,14 +405,9 @@ impl PickerDelegate for BranchListDelegate {
OffsetDateTime::now_utc(), OffsetDateTime::now_utc(),
time_format::TimestampFormat::Relative, time_format::TimestampFormat::Relative,
); );
(formatted_time, commit_message) (Some(formatted_time), Some(subject))
}) })
.unwrap_or_else(|| { .unwrap_or_else(|| (None, None));
(
"Unknown Date".to_string(),
SharedString::from("No commit message available"),
)
});
Some( Some(
ListItem::new(SharedString::from(format!("vcs-menu-{ix}"))) ListItem::new(SharedString::from(format!("vcs-menu-{ix}")))
@ -422,92 +428,56 @@ impl PickerDelegate for BranchListDelegate {
.overflow_x_hidden() .overflow_x_hidden()
.gap_2() .gap_2()
.justify_between() .justify_between()
.child( .child(div().flex_shrink().overflow_x_hidden().child(
div().flex_shrink().overflow_x_hidden().child( if entry.is_new {
Label::new(format!(
"Create branch \"{}\"",
entry.branch.name
))
.into_any_element()
} else {
HighlightedLabel::new( HighlightedLabel::new(
entry.branch.name.clone(), entry.branch.name.clone(),
entry.positions.clone(), entry.positions.clone(),
) )
.truncate(), .truncate()
), .into_any_element()
) },
.child( ))
Label::new(commit_time) .when_some(commit_time, |el, commit_time| {
.size(LabelSize::Small) el.child(
.color(Color::Muted) Label::new(commit_time)
.into_element(), .size(LabelSize::Small)
), .color(Color::Muted)
.into_element(),
)
}),
) )
.when(self.style == BranchListStyle::Modal, |el| { .when(self.style == BranchListStyle::Modal, |el| {
el.child( el.child(div().max_w_96().child({
div().max_w_96().child( let message = if entry.is_new {
Label::new( if let Some(current_branch) =
commit_message self.repo.as_ref().and_then(|repo| {
.split('\n') repo.read(cx).current_branch().map(|b| b.name.clone())
.next() })
.unwrap_or_default() {
.to_string(), format!("based off {}", current_branch)
) } else {
"based off the current branch".to_string()
}
} else {
subject.unwrap_or("no commits found".into()).to_string()
};
Label::new(message)
.size(LabelSize::Small) .size(LabelSize::Small)
.truncate() .truncate()
.color(Color::Muted), .color(Color::Muted)
), }))
)
}), }),
), ),
) )
} }
fn render_footer(
&self,
window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Option<AnyElement> {
let new_branch_name = SharedString::from(self.last_query.trim().replace(" ", "-"));
let handle = cx.weak_entity();
Some(
h_flex()
.w_full()
.p_2()
.gap_2()
.border_t_1()
.border_color(cx.theme().colors().border_variant)
.when(
!new_branch_name.is_empty() && !self.has_exact_match(&new_branch_name),
|el| {
el.child(
Button::new(
"create-branch",
format!("Create branch '{new_branch_name}'",),
)
.key_binding(KeyBinding::for_action(
&menu::SecondaryConfirm,
window,
cx,
))
.toggle_state(
self.modifiers.secondary()
|| (self.selected_index == 0 && self.matches.len() == 0),
)
.tooltip(Tooltip::for_action_title(
"Create branch",
&menu::SecondaryConfirm,
))
.on_click(move |_, window, cx| {
let new_branch_name = new_branch_name.clone();
if let Some(picker) = handle.upgrade() {
picker.update(cx, |picker, cx| {
picker.delegate.create_branch(new_branch_name, window, cx)
});
}
}),
)
},
)
.into_any(),
)
}
fn no_matches_text(&self, _window: &mut Window, _cx: &mut App) -> Option<SharedString> { fn no_matches_text(&self, _window: &mut Window, _cx: &mut App) -> Option<SharedString> {
None None
} }