diff --git a/crates/ely_browser_core/src/state.rs b/crates/ely_browser_core/src/state.rs index c2d5a3c..3219f22 100644 --- a/crates/ely_browser_core/src/state.rs +++ b/crates/ely_browser_core/src/state.rs @@ -1,15 +1,17 @@ use std::collections::BTreeMap; use ely_domain::{ - ArchiveSource, ArchivedTab, BrowserTab, CommandIntent, CommandScope, DomainError, Profile, - ProfileId, ProfileKind, Space, SpaceId, TabId, UrlText, + ArchiveSource, ArchivedTab, BrowserTab, DomainError, Profile, ProfileId, ProfileKind, Space, + SpaceId, TabId, UrlText, }; use crate::{ CoreError, - navigation::{new_space_name, search_url, space_icon, tab_matches_query, tab_title}, + navigation::{tab_matches_query, tab_title}, }; +mod commands; + const DEFAULT_FAVORITE_LIMIT: usize = 12; #[derive(Clone, Debug)] @@ -311,55 +313,6 @@ impl BrowserCore { &self.command_query } - pub fn submit_command(&mut self) -> Result, CoreError> { - let query = self.command_query.trim(); - if query.is_empty() { - return Ok(None); - } - - let intent = CommandIntent::parse(query)?; - match &intent { - CommandIntent::Navigate(url) => { - self.open_tab(url.clone()); - self.command_query.clear(); - } - CommandIntent::Search(query) => { - let url = search_url(query)?; - self.open_tab(url); - self.command_query.clear(); - } - CommandIntent::Command(command) if self.submit_named_command(command)? => { - self.command_query.clear(); - } - CommandIntent::Command(_) => {} - CommandIntent::ScopedSearch { scope: CommandScope::Tabs, query } => { - if let Some(tab_id) = self.find_tab_match(query) { - self.select_tab(&tab_id)?; - self.command_query.clear(); - } - } - CommandIntent::ScopedSearch { scope: CommandScope::Spaces, query } => { - let query = query.trim().to_lowercase(); - if let Some(space_id) = self.spaces.iter().find_map(|space| { - (space.name().to_lowercase().contains(&query) - || space.icon().to_lowercase().contains(&query)) - .then(|| space.id().clone()) - }) { - self.select_space(&space_id)?; - self.command_query.clear(); - } - } - CommandIntent::ScopedSearch { scope: CommandScope::Archive, query } - if self.restore_archived_tab_match(query)?.is_some() => - { - self.command_query.clear(); - } - _ => {} - } - - Ok(Some(intent)) - } - pub fn snapshot(&self) -> Result { let active_space = self .spaces @@ -411,38 +364,6 @@ impl BrowserCore { Ok(next_tab_id) } - fn submit_named_command(&mut self, command: &str) -> Result { - let command = command.trim(); - if let Some(name) = new_space_name(command) { - self.create_space(name.to_string(), space_icon(name), 0xf54e00)?; - return Ok(true); - } - - match command.to_ascii_lowercase().as_str() { - "new-tab" => { - self.open_tab(self.new_tab_url.clone()); - Ok(true) - } - "close-tab" => { - self.close_active_tab()?; - Ok(true) - } - "favorite" | "toggle-favorite" => { - self.toggle_active_tab_favorite()?; - Ok(true) - } - "pin" | "pin-tab" | "toggle-pin" => { - self.toggle_active_tab_pinned()?; - Ok(true) - } - "restore-tab" | "reopen-tab" => { - self.restore_last_archived_tab()?; - Ok(true) - } - _ => Ok(false), - } - } - fn active_tab_index(&self) -> Result { self.tabs .iter() diff --git a/crates/ely_browser_core/src/state/commands.rs b/crates/ely_browser_core/src/state/commands.rs new file mode 100644 index 0000000..a9dfe66 --- /dev/null +++ b/crates/ely_browser_core/src/state/commands.rs @@ -0,0 +1,91 @@ +use ely_domain::{CommandIntent, CommandScope}; + +use crate::{ + CoreError, + navigation::{new_space_name, search_url, space_icon}, +}; + +use super::BrowserCore; + +impl BrowserCore { + pub fn submit_command(&mut self) -> Result, CoreError> { + let query = self.command_query.trim(); + if query.is_empty() { + return Ok(None); + } + + let intent = CommandIntent::parse(query)?; + match &intent { + CommandIntent::Navigate(url) => { + self.open_tab(url.clone()); + self.command_query.clear(); + } + CommandIntent::Search(query) => { + let url = search_url(query)?; + self.open_tab(url); + self.command_query.clear(); + } + CommandIntent::Command(command) if self.submit_named_command(command)? => { + self.command_query.clear(); + } + CommandIntent::Command(_) => {} + CommandIntent::ScopedSearch { scope: CommandScope::Tabs, query } => { + if let Some(tab_id) = self.find_tab_match(query) { + self.select_tab(&tab_id)?; + self.command_query.clear(); + } + } + CommandIntent::ScopedSearch { scope: CommandScope::Spaces, query } => { + let query = query.trim().to_lowercase(); + if let Some(space_id) = self.spaces.iter().find_map(|space| { + (space.name().to_lowercase().contains(&query) + || space.icon().to_lowercase().contains(&query)) + .then(|| space.id().clone()) + }) { + self.select_space(&space_id)?; + self.command_query.clear(); + } + } + CommandIntent::ScopedSearch { scope: CommandScope::Archive, query } + if self.restore_archived_tab_match(query)?.is_some() => + { + self.command_query.clear(); + } + _ => {} + } + + Ok(Some(intent)) + } + + fn submit_named_command(&mut self, command: &str) -> Result { + let command = command.trim(); + if let Some(name) = new_space_name(command) { + self.create_space(name.to_string(), space_icon(name), 0xf54e00)?; + return Ok(true); + } + + match command.to_ascii_lowercase().as_str() { + "new-tab" => { + self.open_tab(self.new_tab_url.clone()); + Ok(true) + } + "close-tab" => { + self.close_active_tab()?; + Ok(true) + } + "favorite" | "toggle-favorite" => { + self.toggle_active_tab_favorite()?; + Ok(true) + } + "pin" | "pin-tab" | "toggle-pin" => { + self.toggle_active_tab_pinned()?; + Ok(true) + } + "restore-tab" | "reopen-tab" => { + self.restore_last_archived_tab()?; + Ok(true) + } + _ => Ok(false), + } + } +}