Switch profiles from command scope
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
use ely_domain::{CommandIntent, CommandScope, SpaceId};
|
||||
use ely_domain::{CommandIntent, CommandScope, ProfileId, ProfileKind, SpaceId};
|
||||
|
||||
use crate::{
|
||||
CoreError,
|
||||
navigation::{move_tab_space_name, new_space_name, search_url, space_icon},
|
||||
navigation::{
|
||||
move_tab_space_name, new_profile_name, new_space_name, search_url, space_icon,
|
||||
switch_profile_name,
|
||||
},
|
||||
};
|
||||
|
||||
use super::BrowserCore;
|
||||
@@ -58,6 +61,10 @@ impl BrowserCore {
|
||||
self.create_space(name.to_string(), space_icon(name), 0xf54e00)?;
|
||||
return Ok(true);
|
||||
}
|
||||
if let Some(name) = new_profile_name(command) {
|
||||
self.create_profile(name.to_string(), 0xf54e00, ProfileKind::Standard)?;
|
||||
return Ok(true);
|
||||
}
|
||||
if let Some(name) = move_tab_space_name(command) {
|
||||
let Some(space_id) = self.find_space_match(name) else {
|
||||
return Ok(false);
|
||||
@@ -65,6 +72,13 @@ impl BrowserCore {
|
||||
self.move_active_tab_to_space(&space_id)?;
|
||||
return Ok(true);
|
||||
}
|
||||
if let Some(name) = switch_profile_name(command) {
|
||||
let Some(profile_id) = self.find_profile_match(name) else {
|
||||
return Ok(false);
|
||||
};
|
||||
self.select_profile(&profile_id)?;
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
match command.to_ascii_lowercase().as_str() {
|
||||
"new-tab" => {
|
||||
@@ -99,4 +113,12 @@ impl BrowserCore {
|
||||
.then(|| space.id().clone())
|
||||
})
|
||||
}
|
||||
|
||||
fn find_profile_match(&self, query: &str) -> Option<ProfileId> {
|
||||
let query = query.trim().to_lowercase();
|
||||
self.profiles
|
||||
.iter()
|
||||
.find(|profile| profile.name().to_lowercase().contains(&query))
|
||||
.map(|profile| profile.id().clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
use ely_domain::{Profile, ProfileId, ProfileKind, TabId};
|
||||
|
||||
use crate::CoreError;
|
||||
|
||||
use super::BrowserCore;
|
||||
|
||||
impl BrowserCore {
|
||||
pub fn create_profile(
|
||||
&mut self,
|
||||
name: impl Into<String>,
|
||||
color_hex: u32,
|
||||
kind: ProfileKind,
|
||||
) -> Result<ProfileId, CoreError> {
|
||||
let profile = Profile::new(name, color_hex, kind);
|
||||
let profile_id = profile.id().clone();
|
||||
self.profiles.push(profile);
|
||||
self.select_profile(&profile_id)?;
|
||||
Ok(profile_id)
|
||||
}
|
||||
|
||||
pub fn select_profile(&mut self, profile_id: &ProfileId) -> Result<TabId, CoreError> {
|
||||
if !self.profiles.iter().any(|profile| profile.id() == profile_id) {
|
||||
return Err(CoreError::ProfileNotFound { id: profile_id.clone() });
|
||||
}
|
||||
|
||||
let active_key = (self.active_space_id.clone(), profile_id.clone());
|
||||
if let Some(tab_id) = self
|
||||
.active_tabs_by_space_profile
|
||||
.get(&active_key)
|
||||
.filter(|tab_id| {
|
||||
self.tabs.iter().any(|tab| {
|
||||
tab.id() == *tab_id
|
||||
&& tab.space_id() == &active_key.0
|
||||
&& tab.profile_id() == &active_key.1
|
||||
})
|
||||
})
|
||||
.cloned()
|
||||
{
|
||||
self.select_tab(&tab_id)?;
|
||||
return Ok(tab_id);
|
||||
}
|
||||
|
||||
if let Some(tab_id) = self
|
||||
.tabs
|
||||
.iter()
|
||||
.rfind(|tab| tab.space_id() == &active_key.0 && tab.profile_id() == &active_key.1)
|
||||
.map(|tab| tab.id().clone())
|
||||
{
|
||||
self.select_tab(&tab_id)?;
|
||||
return Ok(tab_id);
|
||||
}
|
||||
|
||||
let tab = self.build_tab_for(
|
||||
self.active_space_id.clone(),
|
||||
profile_id.clone(),
|
||||
self.new_tab_url.clone(),
|
||||
);
|
||||
let tab_id = tab.id().clone();
|
||||
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.select_tab(&tab_id)?;
|
||||
Ok(tab_id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user