Split browser command handling

This commit is contained in:
2026-05-07 20:29:20 -04:00
parent dcc4883797
commit 73670bbd85
2 changed files with 96 additions and 84 deletions
+5 -84
View File
@@ -1,15 +1,17 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use ely_domain::{ use ely_domain::{
ArchiveSource, ArchivedTab, BrowserTab, CommandIntent, CommandScope, DomainError, Profile, ArchiveSource, ArchivedTab, BrowserTab, DomainError, Profile, ProfileId, ProfileKind, Space,
ProfileId, ProfileKind, Space, SpaceId, TabId, UrlText, SpaceId, TabId, UrlText,
}; };
use crate::{ use crate::{
CoreError, 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; const DEFAULT_FAVORITE_LIMIT: usize = 12;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@@ -311,55 +313,6 @@ impl BrowserCore {
&self.command_query &self.command_query
} }
pub fn submit_command(&mut self) -> Result<Option<CommandIntent>, 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<BrowserSnapshot, CoreError> { pub fn snapshot(&self) -> Result<BrowserSnapshot, CoreError> {
let active_space = self let active_space = self
.spaces .spaces
@@ -411,38 +364,6 @@ impl BrowserCore {
Ok(next_tab_id) Ok(next_tab_id)
} }
fn submit_named_command(&mut self, command: &str) -> Result<bool, CoreError> {
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<usize, CoreError> { fn active_tab_index(&self) -> Result<usize, CoreError> {
self.tabs self.tabs
.iter() .iter()
@@ -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<Option<CommandIntent>, 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<bool, CoreError> {
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),
}
}
}