Add tab group sidebar support
This commit is contained in:
@@ -36,6 +36,7 @@ macro_rules! entity_id {
|
||||
entity_id!(TabId, "tab");
|
||||
entity_id!(SpaceId, "space");
|
||||
entity_id!(ProfileId, "profile");
|
||||
entity_id!(TabGroupId, "tab_group");
|
||||
entity_id!(SplitId, "split");
|
||||
entity_id!(WebViewId, "webview");
|
||||
entity_id!(DownloadId, "download");
|
||||
|
||||
@@ -18,6 +18,7 @@ mod space;
|
||||
mod split;
|
||||
mod sync;
|
||||
mod tab;
|
||||
mod tab_group;
|
||||
mod url_text;
|
||||
|
||||
pub use archive::{ArchiveSource, ArchivedTab};
|
||||
@@ -31,7 +32,8 @@ pub use error::DomainError;
|
||||
pub use favorite::FavoriteLimit;
|
||||
pub use history::HistoryEntry;
|
||||
pub use identifiers::{
|
||||
BookmarkId, DownloadId, NoteId, ProfileId, ReadingListId, SpaceId, SplitId, TabId, WebViewId,
|
||||
BookmarkId, DownloadId, NoteId, ProfileId, ReadingListId, SpaceId, SplitId, TabGroupId, TabId,
|
||||
WebViewId,
|
||||
};
|
||||
pub use new_tab::NewTabDestination;
|
||||
pub use note::{NoteEntry, NoteTarget};
|
||||
@@ -54,4 +56,5 @@ pub use sync::{
|
||||
SyncStatus,
|
||||
};
|
||||
pub use tab::{BrowserTab, TabFlags, TabState};
|
||||
pub use tab_group::TabGroup;
|
||||
pub use url_text::UrlText;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::time::SystemTime;
|
||||
|
||||
use crate::{DomainError, ProfileId, SpaceId, SplitId, TabId, UrlText};
|
||||
use crate::{DomainError, ProfileId, SpaceId, SplitId, TabGroupId, TabId, UrlText};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum TabState {
|
||||
@@ -30,6 +30,7 @@ pub struct BrowserTab {
|
||||
parent_tab_id: Option<TabId>,
|
||||
state: TabState,
|
||||
flags: TabFlags,
|
||||
group_id: Option<TabGroupId>,
|
||||
split_id: Option<SplitId>,
|
||||
sort_key: u64,
|
||||
sync_enabled: bool,
|
||||
@@ -57,6 +58,7 @@ impl BrowserTab {
|
||||
parent_tab_id: None,
|
||||
state: TabState::Ready,
|
||||
flags: TabFlags::default(),
|
||||
group_id: None,
|
||||
split_id: None,
|
||||
sort_key: 0,
|
||||
sync_enabled: true,
|
||||
@@ -176,6 +178,19 @@ impl BrowserTab {
|
||||
self.space_id = space_id;
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn group_id(&self) -> Option<&TabGroupId> {
|
||||
self.group_id.as_ref()
|
||||
}
|
||||
|
||||
pub fn set_group_id(&mut self, group_id: TabGroupId) {
|
||||
self.group_id = Some(group_id);
|
||||
}
|
||||
|
||||
pub fn clear_group_id(&mut self) {
|
||||
self.group_id = None;
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn split_id(&self) -> Option<&SplitId> {
|
||||
self.split_id.as_ref()
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
use crate::{DomainError, SpaceId, TabGroupId};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct TabGroup {
|
||||
id: TabGroupId,
|
||||
space_id: SpaceId,
|
||||
name: String,
|
||||
color_hex: u32,
|
||||
collapsed: bool,
|
||||
sort_key: u64,
|
||||
created_at: SystemTime,
|
||||
updated_at: SystemTime,
|
||||
}
|
||||
|
||||
impl TabGroup {
|
||||
pub fn new(
|
||||
space_id: SpaceId,
|
||||
name: impl Into<String>,
|
||||
color_hex: u32,
|
||||
sort_key: u64,
|
||||
) -> Result<Self, DomainError> {
|
||||
let name = normalized_name(name)?;
|
||||
let created_at = SystemTime::now();
|
||||
Ok(Self {
|
||||
id: TabGroupId::new(),
|
||||
space_id,
|
||||
name,
|
||||
color_hex,
|
||||
collapsed: false,
|
||||
sort_key,
|
||||
created_at,
|
||||
updated_at: created_at,
|
||||
})
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn id(&self) -> &TabGroupId {
|
||||
&self.id
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn space_id(&self) -> &SpaceId {
|
||||
&self.space_id
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
pub fn rename(&mut self, name: impl Into<String>) -> Result<(), DomainError> {
|
||||
self.name = normalized_name(name)?;
|
||||
self.record_update();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn color_hex(&self) -> u32 {
|
||||
self.color_hex
|
||||
}
|
||||
|
||||
pub fn set_color_hex(&mut self, color_hex: u32) {
|
||||
self.color_hex = color_hex;
|
||||
self.record_update();
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn collapsed(&self) -> bool {
|
||||
self.collapsed
|
||||
}
|
||||
|
||||
pub fn set_collapsed(&mut self, collapsed: bool) {
|
||||
self.collapsed = collapsed;
|
||||
self.record_update();
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn sort_key(&self) -> u64 {
|
||||
self.sort_key
|
||||
}
|
||||
|
||||
pub fn set_sort_key(&mut self, sort_key: u64) {
|
||||
self.sort_key = sort_key;
|
||||
self.record_update();
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn created_at(&self) -> SystemTime {
|
||||
self.created_at
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn updated_at(&self) -> SystemTime {
|
||||
self.updated_at
|
||||
}
|
||||
|
||||
fn record_update(&mut self) {
|
||||
let now = SystemTime::now();
|
||||
self.updated_at =
|
||||
if now > self.updated_at { now } else { self.updated_at + Duration::from_nanos(1) };
|
||||
}
|
||||
}
|
||||
|
||||
fn normalized_name(name: impl Into<String>) -> Result<String, DomainError> {
|
||||
let name = name.into();
|
||||
let name = name.trim();
|
||||
if name.is_empty() {
|
||||
return Err(DomainError::EmptyField { field: "tab_group_name" });
|
||||
}
|
||||
|
||||
Ok(name.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::TabGroup;
|
||||
use crate::{DomainError, SpaceId};
|
||||
|
||||
#[test]
|
||||
fn creates_tab_group_with_trimmed_name() -> Result<(), DomainError> {
|
||||
let space_id = SpaceId::new();
|
||||
let group = TabGroup::new(space_id.clone(), " Research ", 0xf54e00, 7)?;
|
||||
|
||||
assert_eq!(group.space_id(), &space_id);
|
||||
assert_eq!(group.name(), "Research");
|
||||
assert_eq!(group.color_hex(), 0xf54e00);
|
||||
assert_eq!(group.sort_key(), 7);
|
||||
assert!(!group.collapsed());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_empty_tab_group_name() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let error = match TabGroup::new(SpaceId::new(), " ", 0xf54e00, 0) {
|
||||
Err(error) => error,
|
||||
Ok(_) => return Err("empty tab group name was accepted".into()),
|
||||
};
|
||||
|
||||
assert_eq!(error, DomainError::EmptyField { field: "tab_group_name" });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn updates_mutable_tab_group_fields() -> Result<(), DomainError> {
|
||||
let mut group = TabGroup::new(SpaceId::new(), "Research", 0xf54e00, 0)?;
|
||||
|
||||
group.rename("Docs")?;
|
||||
group.set_color_hex(0x2f6fed);
|
||||
group.set_collapsed(true);
|
||||
group.set_sort_key(9);
|
||||
|
||||
assert_eq!(group.name(), "Docs");
|
||||
assert_eq!(group.color_hex(), 0x2f6fed);
|
||||
assert!(group.collapsed());
|
||||
assert_eq!(group.sort_key(), 9);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user