diff --git a/crates/ely_app/src/shell/internal_pages/reading_list.rs b/crates/ely_app/src/shell/internal_pages/reading_list.rs index 3910dc4..ca85d8d 100644 --- a/crates/ely_app/src/shell/internal_pages/reading_list.rs +++ b/crates/ely_app/src/shell/internal_pages/reading_list.rs @@ -1,12 +1,16 @@ use ely_browser_core::BrowserSnapshot; use ely_design_system::colors; -use ely_domain::{ReadingListEntry, ReadingProgress}; +use ely_domain::{ReadingListEntry, ReadingListId, ReadingProgress}; use gpui::prelude::FluentBuilder; use gpui::{ AnyElement, Context, InteractiveElement, IntoElement, ParentElement, SharedString, StatefulInteractiveElement, Styled, div, px, rgb, }; -use gpui_component::{IconName, StyledExt, scroll::ScrollableElement}; +use gpui_component::{ + IconName, Sizable, StyledExt, + button::{Button, ButtonVariants}, + scroll::ScrollableElement, +}; use super::{ElyShell, render_canvas_surface}; @@ -58,19 +62,23 @@ impl ElyShell { .reading_list .iter() .rev() - .map(|entry| self.render_reading_list_row(snapshot, entry, cx)), + .enumerate() + .map(|(index, entry)| self.render_reading_list_row(index, snapshot, entry, cx)), ) .into_any_element() } fn render_reading_list_row( &mut self, + index: usize, snapshot: &BrowserSnapshot, entry: &ReadingListEntry, cx: &mut Context, ) -> AnyElement { let url = entry.source_url().clone(); let space_name = reading_list_space_name(snapshot, entry); + let entry_id = entry.id().clone(); + let progress = *entry.progress(); div() .id(SharedString::from(format!("reading-{}", entry.id().as_str()))) @@ -81,18 +89,20 @@ impl ElyShell { .items_center() .justify_between() .gap_4() - .cursor_pointer() - .hover(|style| style.bg(rgb(colors::CANVAS_SOFT))) - .active(|style| style.opacity(0.82)) - .on_click(cx.listener(move |shell, _, window, cx| { - shell.open_url(url.clone(), window, cx); - })) .child( div() + .id(SharedString::from(format!("reading-open-{}", entry.id().as_str()))) .min_w_0() + .flex_1() .flex() .items_center() .gap_3() + .cursor_pointer() + .hover(|style| style.bg(rgb(colors::CANVAS_SOFT))) + .active(|style| style.opacity(0.82)) + .on_click(cx.listener(move |shell, _, window, cx| { + shell.open_url(url.clone(), window, cx); + })) .child(div().text_color(rgb(colors::MUTED_SOFT)).child(IconName::Inbox)) .child( div() @@ -132,6 +142,7 @@ impl ElyShell { }) .child(progress_label(entry.progress())), ) + .child(render_progress_action(index, entry_id, progress, cx)) .into_any_element() } } @@ -172,9 +183,31 @@ fn reading_list_space_name(snapshot: &BrowserSnapshot, entry: &ReadingListEntry) } fn progress_label(progress: &ReadingProgress) -> &'static str { - match progress { - ReadingProgress::Unread => "Unread", - } + progress.label() +} + +fn render_progress_action( + index: usize, + entry_id: ReadingListId, + progress: ReadingProgress, + cx: &mut Context, +) -> AnyElement { + let next_progress = progress.toggled(); + let icon = match progress { + ReadingProgress::Unread => IconName::CircleCheck, + ReadingProgress::Finished => IconName::Undo2, + }; + + Button::new(("reading-progress", index)) + .ghost() + .xsmall() + .icon(icon) + .label(progress.action_label()) + .tooltip(progress.action_label()) + .on_click(cx.listener(move |shell, _, _, cx| { + shell.set_reading_list_progress(&entry_id, next_progress, cx); + })) + .into_any_element() } fn reading_list_count_label(count: usize) -> String { diff --git a/crates/ely_app/src/shell/mod.rs b/crates/ely_app/src/shell/mod.rs index 87e8583..e2b3b34 100644 --- a/crates/ely_app/src/shell/mod.rs +++ b/crates/ely_app/src/shell/mod.rs @@ -3,6 +3,7 @@ mod downloads; mod history; mod internal_pages; mod plugins; +mod reading_list; mod render; mod site_permissions; mod splits; diff --git a/crates/ely_app/src/shell/reading_list.rs b/crates/ely_app/src/shell/reading_list.rs new file mode 100644 index 0000000..c2ed2d8 --- /dev/null +++ b/crates/ely_app/src/shell/reading_list.rs @@ -0,0 +1,19 @@ +use ely_domain::{ReadingListId, ReadingProgress}; +use gpui::Context; + +use super::{ElyShell, ShellState}; + +impl ElyShell { + pub(super) fn set_reading_list_progress( + &mut self, + entry_id: &ReadingListId, + progress: ReadingProgress, + cx: &mut Context, + ) { + if let ShellState::Ready(core) = &mut self.state + && core.set_reading_list_progress(entry_id, progress).is_ok() + { + cx.notify(); + } + } +} diff --git a/crates/ely_browser_core/src/error.rs b/crates/ely_browser_core/src/error.rs index 3196313..6f5a4d0 100644 --- a/crates/ely_browser_core/src/error.rs +++ b/crates/ely_browser_core/src/error.rs @@ -1,5 +1,6 @@ use ely_domain::{ - BookmarkId, DomainError, DownloadId, PluginId, ProfileId, SpaceId, SplitId, TabId, + BookmarkId, DomainError, DownloadId, PluginId, ProfileId, ReadingListId, SpaceId, SplitId, + TabId, }; use thiserror::Error; @@ -26,6 +27,9 @@ pub enum CoreError { #[error("download not found: {id}")] DownloadNotFound { id: DownloadId }, + #[error("reading list entry not found: {id}")] + ReadingListEntryNotFound { id: ReadingListId }, + #[error("download target path is unavailable: {id}")] DownloadTargetPathUnavailable { id: DownloadId }, diff --git a/crates/ely_browser_core/src/state/reading_list.rs b/crates/ely_browser_core/src/state/reading_list.rs index 8d09f21..5f9cb22 100644 --- a/crates/ely_browser_core/src/state/reading_list.rs +++ b/crates/ely_browser_core/src/state/reading_list.rs @@ -1,6 +1,6 @@ use std::time::SystemTime; -use ely_domain::{ReadingListEntry, ReadingListId, UrlText}; +use ely_domain::{ReadingListEntry, ReadingListId, ReadingProgress, UrlText}; use crate::CoreError; @@ -27,6 +27,15 @@ impl BrowserCore { Ok(entry_id) } + pub fn set_reading_list_progress( + &mut self, + entry_id: &ReadingListId, + progress: ReadingProgress, + ) -> Result<(), CoreError> { + self.reading_list_entry_mut(entry_id)?.set_progress(progress); + Ok(()) + } + pub(super) fn find_reading_list_match(&self, query: &str) -> Option { let normalized_query = query.trim().to_lowercase(); if normalized_query.is_empty() { @@ -48,6 +57,16 @@ impl BrowserCore { .cloned() .collect() } + + fn reading_list_entry_mut( + &mut self, + entry_id: &ReadingListId, + ) -> Result<&mut ReadingListEntry, CoreError> { + self.reading_list + .iter_mut() + .find(|entry| entry.id() == entry_id) + .ok_or_else(|| CoreError::ReadingListEntryNotFound { id: entry_id.clone() }) + } } fn reading_list_entry_matches_query(entry: &ReadingListEntry, normalized_query: &str) -> bool { diff --git a/crates/ely_browser_core/tests/reading_list.rs b/crates/ely_browser_core/tests/reading_list.rs index aa5e644..ed02454 100644 --- a/crates/ely_browser_core/tests/reading_list.rs +++ b/crates/ely_browser_core/tests/reading_list.rs @@ -1,7 +1,9 @@ use std::error::Error; use ely_browser_core::{BrowserCore, InitialBrowserConfig}; -use ely_domain::{CommandIntent, CommandScope, ProfileKind, ReadingProgress, UrlText}; +use ely_domain::{ + CommandIntent, CommandScope, ProfileKind, ReadingListId, ReadingProgress, UrlText, +}; #[test] fn save_active_tab_records_reading_list_context() -> Result<(), Box> { @@ -41,6 +43,33 @@ fn save_active_tab_reuses_existing_reading_list_entry() -> Result<(), Box Result<(), Box> { + let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?; + core.open_tab(UrlText::parse("https://example.com/long-read")?); + let entry_id = core.save_active_tab_to_reading_list()?; + + core.set_reading_list_progress(&entry_id, ReadingProgress::Finished)?; + let snapshot = core.snapshot()?; + + assert_eq!(snapshot.reading_list[0].id(), &entry_id); + assert_eq!(snapshot.reading_list[0].progress(), &ReadingProgress::Finished); + Ok(()) +} + +#[test] +fn missing_reading_list_progress_update_returns_error() -> Result<(), Box> { + let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?; + let missing_id = ReadingListId::new(); + + let Err(error) = core.set_reading_list_progress(&missing_id, ReadingProgress::Finished) else { + return Err("expected missing reading list entry error".into()); + }; + + assert_eq!(error, ely_browser_core::CoreError::ReadingListEntryNotFound { id: missing_id }); + Ok(()) +} + #[test] fn reading_list_scoped_search_opens_matching_entry() -> Result<(), Box> { let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?; diff --git a/crates/ely_domain/src/reading_list.rs b/crates/ely_domain/src/reading_list.rs index 76e3283..c9d346b 100644 --- a/crates/ely_domain/src/reading_list.rs +++ b/crates/ely_domain/src/reading_list.rs @@ -2,9 +2,36 @@ use std::time::SystemTime; use crate::{DomainError, ProfileId, ReadingListId, SpaceId, UrlText}; -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum ReadingProgress { Unread, + Finished, +} + +impl ReadingProgress { + #[must_use] + pub fn label(self) -> &'static str { + match self { + Self::Unread => "Unread", + Self::Finished => "Read", + } + } + + #[must_use] + pub fn action_label(self) -> &'static str { + match self { + Self::Unread => "Mark Read", + Self::Finished => "Mark Unread", + } + } + + #[must_use] + pub fn toggled(self) -> Self { + match self { + Self::Unread => Self::Finished, + Self::Finished => Self::Unread, + } + } } #[derive(Clone, Debug, Eq, PartialEq)] @@ -74,6 +101,10 @@ impl ReadingListEntry { &self.progress } + pub fn set_progress(&mut self, progress: ReadingProgress) { + self.progress = progress; + } + #[must_use] pub fn added_at(&self) -> SystemTime { self.added_at