Insert new tabs below active tab
This commit is contained in:
@@ -257,12 +257,7 @@ impl ElyShell {
|
|||||||
.text_color(rgb(colors::INK))
|
.text_color(rgb(colors::INK))
|
||||||
.child(tab.title().to_string()),
|
.child(tab.title().to_string()),
|
||||||
)
|
)
|
||||||
.child(
|
.child(div().text_xs().text_color(rgb(colors::MUTED)).child(tab.display_url()))
|
||||||
div()
|
|
||||||
.text_xs()
|
|
||||||
.text_color(rgb(colors::MUTED))
|
|
||||||
.child(tab.url().as_str().to_string()),
|
|
||||||
)
|
|
||||||
.into_any_element()
|
.into_any_element()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,18 +77,27 @@ impl BrowserCore {
|
|||||||
url,
|
url,
|
||||||
);
|
);
|
||||||
let tab_id = tab.id().clone();
|
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();
|
self.active_tab_id = tab_id.clone();
|
||||||
tab_id
|
tab_id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_tab(&mut self, tab_id: &TabId) -> Result<(), CoreError> {
|
pub fn select_tab(&mut self, tab_id: &TabId) -> Result<(), CoreError> {
|
||||||
if self.tabs.iter().any(|tab| tab.id() == tab_id) {
|
let tab = self
|
||||||
self.active_tab_id = tab_id.clone();
|
.tabs
|
||||||
return Ok(());
|
.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>) {
|
pub fn set_command_query(&mut self, query: impl Into<String>) {
|
||||||
@@ -146,3 +155,29 @@ fn tab_title(url: &UrlText) -> String {
|
|||||||
|
|
||||||
url.display_host()
|
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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -75,6 +75,11 @@ impl BrowserTab {
|
|||||||
&self.url
|
&self.url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn display_url(&self) -> String {
|
||||||
|
self.url.display_url()
|
||||||
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn state(&self) -> &TabState {
|
pub fn state(&self) -> &TabState {
|
||||||
&self.state
|
&self.state
|
||||||
|
|||||||
@@ -51,6 +51,19 @@ impl UrlText {
|
|||||||
.and_then(|url| url.host_str().map(str::to_string))
|
.and_then(|url| url.host_str().map(str::to_string))
|
||||||
.unwrap_or_else(|| self.value.clone())
|
.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 {
|
impl fmt::Display for UrlText {
|
||||||
|
|||||||
Reference in New Issue
Block a user