Bind spaces to default profiles

This commit is contained in:
2026-05-08 05:21:42 -04:00
parent aed3a4154e
commit d45dced4a7
4 changed files with 120 additions and 28 deletions
+32 -13
View File
@@ -106,13 +106,14 @@ pub struct BrowserCore {
impl BrowserCore {
pub fn new(config: InitialBrowserConfig) -> Result<Self, CoreError> {
let space = Space::new(config.space_name, config.space_icon, 0xf54e00);
let profile = Profile::new(config.profile_name, 0x26251e, ProfileKind::Standard);
let active_profile_id = profile.id().clone();
let space =
Space::new(config.space_name, config.space_icon, 0xf54e00, active_profile_id.clone());
let new_tab_destination = config.new_tab_destination;
let new_tab_url = new_tab_destination.url()?;
let new_tab_title = tab_title(&new_tab_url);
let active_space_id = space.id().clone();
let active_profile_id = profile.id().clone();
let tab = BrowserTab::new(
TabId::new(),
active_space_id.clone(),
@@ -162,13 +163,10 @@ impl BrowserCore {
icon: impl Into<String>,
accent_hex: u32,
) -> Result<SpaceId, CoreError> {
let space = Space::new(name, icon, accent_hex);
let space = Space::new(name, icon, accent_hex, self.active_profile_id.clone());
let space_id = space.id().clone();
let tab = self.build_tab_for(
space_id.clone(),
self.active_profile_id.clone(),
self.new_tab_url()?,
);
let default_profile_id = space.default_profile_id().clone();
let tab = self.build_tab_for(space_id.clone(), default_profile_id, self.new_tab_url()?);
let tab_id = tab.id().clone();
self.spaces.push(space);
@@ -200,11 +198,14 @@ impl BrowserCore {
return Ok(tab_id);
}
let tab = self.build_tab_for(
space_id.clone(),
self.active_profile_id.clone(),
self.new_tab_url()?,
);
let default_profile_id = self
.spaces
.iter()
.find(|space| space.id() == space_id)
.ok_or_else(|| CoreError::SpaceNotFound { id: space_id.clone() })?
.default_profile_id()
.clone();
let tab = self.build_tab_for(space_id.clone(), default_profile_id, self.new_tab_url()?);
let tab_id = tab.id().clone();
self.tabs.push(tab);
self.select_tab(&tab_id)?;
@@ -233,6 +234,24 @@ impl BrowserCore {
Ok(())
}
pub fn set_space_default_profile(
&mut self,
space_id: &SpaceId,
profile_id: &ProfileId,
) -> Result<(), CoreError> {
if !self.profiles.iter().any(|profile| profile.id() == profile_id) {
return Err(CoreError::ProfileNotFound { id: profile_id.clone() });
}
let space = self
.spaces
.iter_mut()
.find(|space| space.id() == space_id)
.ok_or_else(|| CoreError::SpaceNotFound { id: space_id.clone() })?;
space.set_default_profile_id(profile_id.clone());
Ok(())
}
pub fn set_search_engine(&mut self, search_engine: SearchEngine) {
self.search_engine = search_engine;
}
+46
View File
@@ -0,0 +1,46 @@
use std::error::Error;
use ely_browser_core::{BrowserCore, CoreError, InitialBrowserConfig};
use ely_domain::{ProfileId, ProfileKind};
#[test]
fn created_space_binds_current_profile_as_default() -> Result<(), Box<dyn Error>> {
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
let research_profile_id = core.create_profile("Research", 0x9fc9a2, ProfileKind::Standard)?;
let research_space_id = core.create_space("Research", "R", 0x9fc9a2)?;
let snapshot = core.snapshot()?;
let Some(research_space) =
snapshot.spaces.iter().find(|space| space.id() == &research_space_id)
else {
return Err("missing research space".into());
};
assert_eq!(research_space.default_profile_id(), &research_profile_id);
assert_eq!(snapshot.active_profile_id, research_profile_id);
Ok(())
}
#[test]
fn space_default_profile_updates_with_profile_validation() -> Result<(), Box<dyn Error>> {
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
let work_space_id = core.snapshot()?.active_space_id;
let research_profile_id = core.create_profile("Research", 0x9fc9a2, ProfileKind::Standard)?;
core.set_space_default_profile(&work_space_id, &research_profile_id)?;
let snapshot = core.snapshot()?;
let Some(work_space) = snapshot.spaces.iter().find(|space| space.id() == &work_space_id) else {
return Err("missing work space".into());
};
assert_eq!(work_space.default_profile_id(), &research_profile_id);
let missing_profile_id = ProfileId::new();
let error = match core.set_space_default_profile(&work_space_id, &missing_profile_id) {
Err(error) => error,
Ok(_) => return Err("space default profile should require an existing profile".into()),
};
assert_eq!(error, CoreError::ProfileNotFound { id: missing_profile_id });
Ok(())
}