Insert new tabs below active tab

This commit is contained in:
2026-05-07 18:53:24 -04:00
parent b787486f8e
commit e9ef92c41b
4 changed files with 60 additions and 12 deletions
+1 -6
View File
@@ -257,12 +257,7 @@ impl ElyShell {
.text_color(rgb(colors::INK))
.child(tab.title().to_string()),
)
.child(
div()
.text_xs()
.text_color(rgb(colors::MUTED))
.child(tab.url().as_str().to_string()),
)
.child(div().text_xs().text_color(rgb(colors::MUTED)).child(tab.display_url()))
.into_any_element()
}
}
+41 -6
View File
@@ -77,18 +77,27 @@ impl BrowserCore {
url,
);
let tab_id = tab.id().clone();
self.tabs.push(tab);
let insert_index = self
.tabs
.iter()
.position(|existing| existing.id() == &self.active_tab_id)
.map_or(self.tabs.len(), |index| index + 1);
self.tabs.insert(insert_index, tab);
self.active_tab_id = tab_id.clone();
tab_id
}
pub fn select_tab(&mut self, tab_id: &TabId) -> Result<(), CoreError> {
if self.tabs.iter().any(|tab| tab.id() == tab_id) {
self.active_tab_id = tab_id.clone();
return Ok(());
}
let tab = self
.tabs
.iter()
.find(|tab| tab.id() == tab_id)
.ok_or_else(|| CoreError::TabNotFound { id: tab_id.clone() })?;
Err(CoreError::TabNotFound { id: tab_id.clone() })
self.active_tab_id = tab.id().clone();
self.active_space_id = tab.space_id().clone();
self.active_profile_id = tab.profile_id().clone();
Ok(())
}
pub fn set_command_query(&mut self, query: impl Into<String>) {
@@ -146,3 +155,29 @@ fn tab_title(url: &UrlText) -> String {
url.display_host()
}
#[cfg(test)]
mod tests {
use std::error::Error;
use ely_domain::UrlText;
use super::{BrowserCore, InitialBrowserConfig};
#[test]
fn opens_new_tab_below_active_tab() -> Result<(), Box<dyn Error>> {
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
let first_tab_id = core.active_tab()?.id().clone();
let second_tab_id = core.open_tab(UrlText::parse("https://example.com")?);
core.select_tab(&first_tab_id)?;
let third_tab_id = core.open_tab(UrlText::parse("https://servo.org")?);
let snapshot = core.snapshot()?;
let ordered_ids = snapshot.tabs.iter().map(|tab| tab.id().clone()).collect::<Vec<_>>();
assert_eq!(ordered_ids, vec![first_tab_id, third_tab_id.clone(), second_tab_id]);
assert_eq!(snapshot.active_tab_id, third_tab_id);
Ok(())
}
}
+5
View File
@@ -75,6 +75,11 @@ impl BrowserTab {
&self.url
}
#[must_use]
pub fn display_url(&self) -> String {
self.url.display_url()
}
#[must_use]
pub fn state(&self) -> &TabState {
&self.state
+13
View File
@@ -51,6 +51,19 @@ impl UrlText {
.and_then(|url| url.host_str().map(str::to_string))
.unwrap_or_else(|| self.value.clone())
}
#[must_use]
pub fn display_url(&self) -> String {
let Ok(url) = Url::parse(&self.value) else {
return self.value.clone();
};
if url.scheme() == "ely" {
return self.value.clone();
}
url.host_str().map(str::to_string).unwrap_or_else(|| self.value.clone())
}
}
impl fmt::Display for UrlText {