Add search engine settings
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
use ely_domain::{BrowserTab, DomainError, PluginId, SiteOrigin, UrlText};
|
||||
use ely_domain::{BrowserTab, PluginId, SearchEngine, SiteOrigin, UrlText};
|
||||
use url::Url;
|
||||
|
||||
use crate::CoreError;
|
||||
|
||||
const DEFAULT_SEARCH_URL: &str = "https://duckduckgo.com/";
|
||||
|
||||
pub(crate) fn tab_title(url: &UrlText) -> String {
|
||||
if let Some(title) = internal_page_title(url.as_str()) {
|
||||
return title.to_string();
|
||||
@@ -28,6 +26,7 @@ fn internal_page_title(url: &str) -> Option<&'static str> {
|
||||
"ely://about" => Some("About ELY Browser"),
|
||||
"ely://settings" => Some("Settings"),
|
||||
"ely://settings/sidebar-tabs" => Some("Sidebar & Tabs Settings"),
|
||||
"ely://settings/search" => Some("Search Settings"),
|
||||
"ely://settings/spaces" => Some("Space Settings"),
|
||||
"ely://settings/shortcuts" => Some("Shortcut Settings"),
|
||||
"ely://settings/plugins" => Some("Plugin Settings"),
|
||||
@@ -91,11 +90,8 @@ pub(crate) fn space_icon(name: &str) -> String {
|
||||
name.chars().next().map_or_else(String::new, |value| value.to_string())
|
||||
}
|
||||
|
||||
pub(crate) fn search_url(query: &str) -> Result<UrlText, CoreError> {
|
||||
let mut url = Url::parse(DEFAULT_SEARCH_URL)
|
||||
.map_err(|_| DomainError::InvalidUrl { value: DEFAULT_SEARCH_URL.to_string() })?;
|
||||
url.query_pairs_mut().append_pair("q", query);
|
||||
UrlText::parse(url.to_string()).map_err(CoreError::from)
|
||||
pub(crate) fn search_url(query: &str, search_engine: SearchEngine) -> Result<UrlText, CoreError> {
|
||||
search_engine.search_url(query).map_err(CoreError::from)
|
||||
}
|
||||
|
||||
pub(crate) fn downloads_url() -> Result<UrlText, CoreError> {
|
||||
@@ -163,6 +159,9 @@ fn settings_page_route(query: &str) -> Option<&'static str> {
|
||||
"sidebar" | "tabs" | "sidebar tabs" | "sidebar & tabs" => {
|
||||
Some("ely://settings/sidebar-tabs")
|
||||
}
|
||||
"search" | "search engine" | "default search" | "default search engine" => {
|
||||
Some("ely://settings/search")
|
||||
}
|
||||
"space" | "spaces" | "space settings" | "spaces settings" => Some("ely://settings/spaces"),
|
||||
"shortcut" | "shortcuts" | "keyboard" | "keyboard shortcuts" => {
|
||||
Some("ely://settings/shortcuts")
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::{collections::BTreeMap, time::SystemTime};
|
||||
|
||||
use ely_domain::{
|
||||
ArchivePolicy, ArchivedTab, BookmarkEntry, BrowserTab, DomainError, DownloadEntry,
|
||||
DownloadPolicy, HistoryEntry, Profile, ProfileId, ProfileKind, ReadingListEntry,
|
||||
DownloadPolicy, HistoryEntry, Profile, ProfileId, ProfileKind, ReadingListEntry, SearchEngine,
|
||||
SitePermissionAuditEvent, SitePermissionEntry, Space, SpaceId, SplitLayout, SyncStatus, TabId,
|
||||
UrlText,
|
||||
};
|
||||
@@ -66,6 +66,7 @@ pub struct BrowserSnapshot {
|
||||
pub active_space_name: String,
|
||||
pub active_profile_name: String,
|
||||
pub active_download_policy: DownloadPolicy,
|
||||
pub search_engine: SearchEngine,
|
||||
pub command_query: String,
|
||||
}
|
||||
|
||||
@@ -90,6 +91,7 @@ pub struct BrowserCore {
|
||||
active_tab_id: TabId,
|
||||
active_tabs_by_space: BTreeMap<SpaceId, TabId>,
|
||||
active_tabs_by_space_profile: BTreeMap<(SpaceId, ProfileId), TabId>,
|
||||
search_engine: SearchEngine,
|
||||
command_query: String,
|
||||
new_tab_url: UrlText,
|
||||
}
|
||||
@@ -121,6 +123,7 @@ impl BrowserCore {
|
||||
active_tab_id,
|
||||
active_tabs_by_space,
|
||||
active_tabs_by_space_profile,
|
||||
search_engine: SearchEngine::default(),
|
||||
spaces: vec![space],
|
||||
profiles: vec![profile],
|
||||
tabs: vec![tab],
|
||||
@@ -217,6 +220,15 @@ impl BrowserCore {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_search_engine(&mut self, search_engine: SearchEngine) {
|
||||
self.search_engine = search_engine;
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn search_engine(&self) -> SearchEngine {
|
||||
self.search_engine
|
||||
}
|
||||
|
||||
pub fn set_command_query(&mut self, query: impl Into<String>) {
|
||||
self.command_query = query.into();
|
||||
}
|
||||
@@ -253,6 +265,7 @@ impl BrowserCore {
|
||||
active_space_name: active_space.name().to_string(),
|
||||
active_profile_name: active_profile.name().to_string(),
|
||||
active_download_policy: active_profile.download_policy().clone(),
|
||||
search_engine: self.search_engine,
|
||||
command_query: self.command_query.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ impl BrowserCore {
|
||||
self.command_query.clear();
|
||||
}
|
||||
CommandIntent::Search(query) => {
|
||||
let url = search_url(query)?;
|
||||
let url = search_url(query, self.search_engine)?;
|
||||
self.open_tab(url);
|
||||
self.command_query.clear();
|
||||
}
|
||||
|
||||
@@ -65,3 +65,24 @@ fn settings_scoped_search_opens_spaces_page() -> Result<(), Box<dyn Error>> {
|
||||
assert_eq!(core.snapshot()?.command_query, "");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_scoped_search_opens_search_page() -> Result<(), Box<dyn Error>> {
|
||||
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
|
||||
|
||||
core.set_command_query("@settings search");
|
||||
let intent = core.submit_command()?;
|
||||
let active_tab = core.active_tab()?;
|
||||
|
||||
assert_eq!(
|
||||
intent,
|
||||
Some(CommandIntent::ScopedSearch {
|
||||
scope: CommandScope::Settings,
|
||||
query: "search".to_string(),
|
||||
})
|
||||
);
|
||||
assert_eq!(active_tab.title(), "Search Settings");
|
||||
assert_eq!(active_tab.url().as_str(), "ely://settings/search");
|
||||
assert_eq!(core.snapshot()?.command_query, "");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::error::Error;
|
||||
|
||||
use ely_browser_core::{BrowserCore, CoreError, InitialBrowserConfig};
|
||||
use ely_domain::{CommandIntent, CommandScope, TabState, UrlText};
|
||||
use ely_domain::{CommandIntent, CommandScope, SearchEngine, TabState, UrlText};
|
||||
|
||||
#[test]
|
||||
fn opens_new_tab_below_active_tab() -> Result<(), Box<dyn Error>> {
|
||||
@@ -244,6 +244,22 @@ fn search_command_opens_default_search_url() -> Result<(), Box<dyn Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_command_uses_selected_search_engine() -> Result<(), Box<dyn Error>> {
|
||||
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
|
||||
core.set_search_engine(SearchEngine::Google);
|
||||
|
||||
core.set_command_query("? rust async book");
|
||||
let intent = core.submit_command()?;
|
||||
|
||||
let active_tab = core.active_tab()?;
|
||||
assert_eq!(intent, Some(CommandIntent::Search("rust async book".to_string())));
|
||||
assert_eq!(active_tab.url().as_str(), "https://www.google.com/search?q=rust+async+book");
|
||||
assert_eq!(core.snapshot()?.search_engine, SearchEngine::Google);
|
||||
assert_eq!(core.command_query(), "");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn switching_spaces_restores_each_space_active_tab() -> Result<(), Box<dyn Error>> {
|
||||
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
|
||||
|
||||
Reference in New Issue
Block a user