Add favorite limit settings
This commit is contained in:
@@ -2,9 +2,9 @@ use std::{collections::BTreeMap, time::SystemTime};
|
||||
|
||||
use ely_domain::{
|
||||
ArchivePolicy, ArchivedTab, BookmarkEntry, BrowserTab, DomainError, DownloadEntry,
|
||||
DownloadPolicy, HistoryEntry, HistoryRecordingPolicy, NewTabDestination, Profile, ProfileId,
|
||||
ProfileKind, ReadingListEntry, SearchEngine, SitePermissionAuditEvent, SitePermissionEntry,
|
||||
Space, SpaceId, SplitLayout, SyncStatus, TabId, UrlText,
|
||||
DownloadPolicy, FavoriteLimit, HistoryEntry, HistoryRecordingPolicy, NewTabDestination,
|
||||
Profile, ProfileId, ProfileKind, ReadingListEntry, SearchEngine, SitePermissionAuditEvent,
|
||||
SitePermissionEntry, Space, SpaceId, SplitLayout, SyncStatus, TabId, UrlText,
|
||||
};
|
||||
|
||||
use crate::{CoreError, navigation::tab_title};
|
||||
@@ -69,6 +69,7 @@ pub struct BrowserSnapshot {
|
||||
pub search_engine: SearchEngine,
|
||||
pub new_tab_destination: NewTabDestination,
|
||||
pub history_recording_policy: HistoryRecordingPolicy,
|
||||
pub favorite_limit: FavoriteLimit,
|
||||
pub command_query: String,
|
||||
}
|
||||
|
||||
@@ -96,6 +97,7 @@ pub struct BrowserCore {
|
||||
search_engine: SearchEngine,
|
||||
new_tab_destination: NewTabDestination,
|
||||
history_recording_policy: HistoryRecordingPolicy,
|
||||
favorite_limit: FavoriteLimit,
|
||||
command_query: String,
|
||||
}
|
||||
|
||||
@@ -131,6 +133,7 @@ impl BrowserCore {
|
||||
search_engine: SearchEngine::default(),
|
||||
new_tab_destination,
|
||||
history_recording_policy: HistoryRecordingPolicy::default(),
|
||||
favorite_limit: FavoriteLimit::default(),
|
||||
spaces: vec![space],
|
||||
profiles: vec![profile],
|
||||
tabs: vec![tab],
|
||||
@@ -253,6 +256,15 @@ impl BrowserCore {
|
||||
self.history_recording_policy
|
||||
}
|
||||
|
||||
pub fn set_favorite_limit(&mut self, favorite_limit: FavoriteLimit) {
|
||||
self.favorite_limit = favorite_limit;
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn favorite_limit(&self) -> FavoriteLimit {
|
||||
self.favorite_limit
|
||||
}
|
||||
|
||||
pub fn set_command_query(&mut self, query: impl Into<String>) {
|
||||
self.command_query = query.into();
|
||||
}
|
||||
@@ -292,6 +304,7 @@ impl BrowserCore {
|
||||
search_engine: self.search_engine,
|
||||
new_tab_destination: self.new_tab_destination,
|
||||
history_recording_policy: self.history_recording_policy,
|
||||
favorite_limit: self.favorite_limit,
|
||||
command_query: self.command_query.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,8 +11,6 @@ use crate::{
|
||||
|
||||
use super::BrowserCore;
|
||||
|
||||
const DEFAULT_FAVORITE_LIMIT: usize = 12;
|
||||
|
||||
impl BrowserCore {
|
||||
pub fn open_new_tab(&mut self) -> Result<TabId, CoreError> {
|
||||
let url = self.new_tab_url()?;
|
||||
@@ -227,9 +225,10 @@ impl BrowserCore {
|
||||
let favorite_count = self.tabs.iter().filter(|tab| tab.flags().favorite).count();
|
||||
let active_tab = self.tabs.get_mut(active_index).ok_or(CoreError::MissingActiveTab)?;
|
||||
let next_favorite = !active_tab.flags().favorite;
|
||||
let favorite_limit = self.favorite_limit.value();
|
||||
|
||||
if next_favorite && favorite_count >= DEFAULT_FAVORITE_LIMIT {
|
||||
return Err(CoreError::FavoriteLimitReached { limit: DEFAULT_FAVORITE_LIMIT });
|
||||
if next_favorite && favorite_count >= favorite_limit {
|
||||
return Err(CoreError::FavoriteLimitReached { limit: favorite_limit });
|
||||
}
|
||||
|
||||
active_tab.set_favorite(next_favorite);
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use std::error::Error;
|
||||
|
||||
use ely_browser_core::{BrowserCore, CoreError, InitialBrowserConfig};
|
||||
use ely_domain::{CommandIntent, CommandScope, NewTabDestination, SearchEngine, TabState, UrlText};
|
||||
use ely_domain::{
|
||||
CommandIntent, CommandScope, FavoriteLimit, NewTabDestination, SearchEngine, TabState, UrlText,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn opens_new_tab_below_active_tab() -> Result<(), Box<dyn Error>> {
|
||||
@@ -398,3 +400,27 @@ fn enforces_default_favorite_limit() -> Result<(), Box<dyn Error>> {
|
||||
assert_eq!(core.snapshot()?.favorites.len(), 12);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enforces_configured_favorite_limit() -> Result<(), Box<dyn Error>> {
|
||||
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
|
||||
core.set_favorite_limit(FavoriteLimit::Six);
|
||||
|
||||
core.toggle_active_tab_favorite()?;
|
||||
for index in 1..6 {
|
||||
core.open_tab(UrlText::parse(format!("https://example.com/{index}"))?);
|
||||
core.toggle_active_tab_favorite()?;
|
||||
}
|
||||
|
||||
core.open_tab(UrlText::parse("https://example.com/overflow")?);
|
||||
let error = match core.toggle_active_tab_favorite() {
|
||||
Err(error) => error,
|
||||
Ok(_) => return Err("configured favorite limit should apply".into()),
|
||||
};
|
||||
|
||||
let snapshot = core.snapshot()?;
|
||||
assert_eq!(error, CoreError::FavoriteLimitReached { limit: 6 });
|
||||
assert_eq!(snapshot.favorite_limit, FavoriteLimit::Six);
|
||||
assert_eq!(snapshot.favorites.len(), 6);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user