diff --git a/packages/frontend/desktop/src/views_extra/journals.rs b/packages/frontend/desktop/src/views_extra/journals.rs index 888d056ad3..197815c3e5 100644 --- a/packages/frontend/desktop/src/views_extra/journals.rs +++ b/packages/frontend/desktop/src/views_extra/journals.rs @@ -5,7 +5,7 @@ use blocksuite_editor::{block_flow::civil_from_days, database::parse_date_input_ use ui::{ App, ClickEvent, Context, Div, Entity, FontWeight, InteractiveElement, IntoElement, ParentElement, RenderOnce, SharedString, Stateful, StatefulInteractiveElement, Styled, Window, - div, prelude::FluentBuilder, px, svg, + div, px, svg, }; use ui_tokens::FONT_SANS; @@ -329,15 +329,19 @@ impl RenderOnce for CalendarView { button(id.clone(), label, palette, enabled).snapshot(id, None) }; if let CalendarKind::Week { show_doc_today, .. } = self.kind { - let show_today = selected != today - && if placeholder { - width >= 120.0 - } else { - show_doc_today - }; - let cell_width = if width >= 300.0 { 34.0 } else { 24.0 }; - let reserve = if show_today { 108.0 } else { 54.0 }; - let count = (((width - reserve) / (cell_width + 2.0)) as i64).clamp(1, 7); + let show_today = if placeholder { + selected != today && width >= 120.0 + } else { + show_doc_today + }; + let size_factor = ((width - 180.0) / 180.0).clamp(0.0, 1.0); + let cell_width = (20.0 + 14.0 * size_factor).floor(); + let arrow_width = (14.0 + 10.0 * size_factor).floor(); + let today_width = (46.0 + 8.0 * size_factor).floor(); + let gap = 2.0; + let reserve = + 2.0 * arrow_width + gap + if show_today { today_width + gap } else { 0.0 }; + let count = (((width - reserve) / (cell_width + gap)) as i64).clamp(1, 7); let start = week_start(day); let first = (day - count / 2).clamp(start, start + 7 - count); let browse_step = |direction: i64| { @@ -356,11 +360,16 @@ impl RenderOnce for CalendarView { .snapshot(format!("{prefix}.journal_week"), None) .flex() .items_center() - .gap(px(2.0)) - .child(control("journal_previous", "‹", true).on_click(browse(browse_step(-1)))) + .gap(px(gap)) + .child( + control("journal_previous", "‹", true) + .px(px(0.0)) + .w(px(arrow_width)) + .on_click(browse(browse_step(-1))), + ) .children((first..first + count).map(|target| { let label = - &WEEKDAYS[weekday(target)][..if cell_width == 34.0 { 3 } else { 1 }]; + &WEEKDAYS[weekday(target)][..if cell_width >= 30.0 { 3 } else { 1 }]; let date = civil_from_days(target).2; day_button(&prefix, target, selected, today, true, palette) .w(px(cell_width)) @@ -369,13 +378,18 @@ impl RenderOnce for CalendarView { .child(date.to_string()) .on_click(select(target)) })) - .child(control("journal_next", "›", true).on_click(browse(browse_step(1)))); + .child( + control("journal_next", "›", true) + .px(px(0.0)) + .w(px(arrow_width)) + .on_click(browse(browse_step(1))), + ); let mut row = div() .min_w_0() .flex() .items_center() - .gap(px(2.0)) - .when(!show_today || width >= 128.0, |row| row.child(week)); + .gap(px(gap)) + .child(week); if show_today { let enabled = placeholder || !shell.state.store.is_readonly_sqlite() @@ -383,7 +397,9 @@ impl RenderOnce for CalendarView { .state .journal_doc_id(&shell.state.today_date) .is_some(); - let today_button = control("today", "Today", enabled); + let today_button = control("today", "Today", enabled) + .px(px(0.0)) + .w(px(today_width)); row = row.child(if enabled { today_button.on_click(journal_action(&self.shell, &location, move |state| { if placeholder { @@ -813,27 +829,165 @@ mod tests { } #[test] - fn document_header_hides_today_below_300_pixels() { - for (width, readonly) in [(299, false), (300, false), (300, true)] { + fn journal_header_controls_fit_the_pane_and_keep_actions_available() { + for readonly in [false, true] { + for (width, sidebar_width) in [ + (299, 0), + (300, 0), + (320, 0), + (359, 0), + (360, 0), + (390, 0), + (499, 0), + (500, 0), + (504, 0), + (524, 0), + (768, 240), + (768, 248), + (1280, 248), + ] { + let case = + format!("window {width}px, sidebar {sidebar_width}px, readonly {readonly}"); + let mut state = seeded(false); + if sidebar_width > 0 { + state.sidebar.width = sidebar_width as f32; + } + let yesterday = state.store.create_doc( + "Yesterday", + DocMode::Page, + Some(YESTERDAY.into()), + 1_000, + ); + if readonly { + mark_readonly(&mut state); + } + state.navigate_active_pane(DesktopRoute::Doc(yesterday)); + let shell = TestShell::open(state, width); + let frame = shell.frame(); + let elements = &frame.elements; + let pane = elements["desktop.workbench.topbar"].bounds; + assert_eq!(pane.x, sidebar_width as f32, "{case}"); + assert_eq!(pane.width, (width - sidebar_width) as f32, "{case}"); + + for id in [ + "desktop.topbar.journal_previous", + "desktop.topbar.journal_next", + "desktop.topbar.more", + "desktop.topbar.inspector_toggle", + ] { + assert!( + elements.get(id).is_some_and(|el| el.interactive), + "{id}: {case}" + ); + } + for (id, visible) in [ + ("desktop.topbar.today", width >= 300), + ("desktop.topbar.sidebar_toggle", sidebar_width == 0), + ("desktop.topbar.share", width - sidebar_width >= 500), + ] { + assert_eq!( + elements.get(id).map(|el| el.interactive), + visible.then_some(true), + "{id}: {case}" + ); + } + assert_eq!( + elements.contains_key("desktop.topbar.readonly_sqlite_badge"), + readonly, + "{case}" + ); + for id in ["desktop.topbar.favorite", "desktop.topbar.doc_info"] { + assert!(!elements.contains_key(id), "{id}: {case}"); + } + let fits = |outer: ui::SnapshotRect, inner: ui::SnapshotRect| { + inner.x >= outer.x - 0.5 && inner.right() <= outer.right() + 0.5 + }; + let week = elements["desktop.topbar.journal_week"].bounds; + let days: Vec<_> = elements + .values() + .filter(|el| el.id.starts_with("desktop.topbar.day.")) + .collect(); + assert!((1..=7).contains(&days.len()), "{case}"); + for day in days { + assert!( + day.interactive && fits(week, day.bounds), + "{}: {case}", + day.id + ); + } + let mut controls = Vec::new(); + for el in elements + .values() + .filter(|el| el.id.starts_with("desktop.topbar.")) + { + assert!(fits(pane, el.bounds), "{} {:?}: {case}", el.id, el.bounds); + assert!( + fits(el.content_mask, el.bounds), + "{} {:?} clipped by {:?}: {case}", + el.id, + el.bounds, + el.content_mask + ); + if !matches!( + el.id.as_str(), + "desktop.topbar.mode_switch" | "desktop.topbar.journal_week" + ) { + controls.push(el); + } + } + controls.sort_by(|a, b| a.bounds.x.total_cmp(&b.bounds.x)); + for pair in controls.windows(2) { + assert!( + pair[0].bounds.x + pair[0].bounds.width <= pair[1].bounds.x + 0.5, + "{} overlaps {}: {case}", + pair[0].id, + pair[1].id, + ); + } + + if width >= 300 { + shell.click("desktop.topbar.today"); + assert_doc(&shell, "doc-journal-today"); + } + shell.click("desktop.topbar.more"); + let menu = shell.frame(); + assert!( + menu.elements.contains_key("desktop.header.menu.favorite"), + "{case}" + ); + assert!( + menu.elements.contains_key("desktop.header.menu.doc_info"), + "{case}" + ); + } + } + } + + #[test] + fn today_opens_the_first_journal_from_another_document_on_the_same_date() { + for readonly in [false, true] { let mut state = seeded(false); - let yesterday = + let other = state .store - .create_doc("Yesterday", DocMode::Page, Some(YESTERDAY.into()), 1_000); + .create_doc("Another journal", DocMode::Page, Some(TODAY.into()), 1_000); + let mut ids = ["doc-journal-today", &other]; + ids.sort_unstable(); + assert_eq!(state.journal_doc_id(TODAY), Some(ids[0])); if readonly { mark_readonly(&mut state); } - state.navigate_active_pane(DesktopRoute::Doc(yesterday)); - let shell = TestShell::open(state, width); - let frame = shell.frame(); - assert_eq!( - frame.elements.contains_key("desktop.topbar.today"), - width == 300 - ); - if width == 300 { - shell.click("desktop.topbar.today"); - assert_doc(&shell, "doc-journal-today"); - } + state.navigate_active_pane(DesktopRoute::AllDocs); + state.navigate_active_pane(DesktopRoute::Doc(ids[1].into())); + let shell = TestShell::open(state, 500); + shell.click("desktop.topbar.today"); + assert_doc(&shell, ids[0]); + shell.click("desktop.topbar.today"); + assert_doc(&shell, ids[0]); + shell.state(|state| { + assert!(state.split.active_mut().go_back()); + assert_eq!(state.split.active().active_route(), &DesktopRoute::AllDocs); + }); } } diff --git a/packages/frontend/desktop/src/workbench/topbar/mod.rs b/packages/frontend/desktop/src/workbench/topbar/mod.rs index 9f4a20884b..f42cecdae3 100644 --- a/packages/frontend/desktop/src/workbench/topbar/mod.rs +++ b/packages/frontend/desktop/src/workbench/topbar/mod.rs @@ -108,13 +108,20 @@ fn icon_button( .child(glyph(icon, palette.text_secondary)) } -fn render_readonly_sqlite_badge(id: SharedString, palette: DesktopPalette) -> impl IntoElement { +fn render_readonly_sqlite_badge( + id: SharedString, + palette: DesktopPalette, + journal_width: Option, +) -> impl IntoElement { div() .id(id.clone()) .snapshot(id, Some("desktop.workbench.topbar")) .flex_shrink(0.0) .rounded(px(4.0)) .px(px(8.0)) + .when_some(journal_width, |badge, width| { + badge.w(px(width)).px(px(5.0)).flex().justify_center() + }) .py(px(2.0)) .bg(palette.bg_pill) .text_size(px(12.0)) @@ -170,13 +177,29 @@ pub(super) fn render_detail_header( let mode = pane.active_mode(); let route = pane.active_route(); let doc_entry = route.doc_id().and_then(|id| shell.state.store.get_doc(id)); - let journal = doc_entry.is_some_and(|entry| entry.meta.journal_date.is_some()); + let journal = + doc_entry.is_some_and(|entry| entry.meta.journal_date.is_some() && !entry.meta.trashed); let narrow = width < 500.0; let hide_share = split_visible || width < 500.0; - let compact_journal = journal && width < 360.0; + let compact_journal = journal && width < 420.0; + let tight_journal = journal && width < 340.0; let hide_doc_actions = width < 300.0; let show_sidebar_switch = first && !shell.state.sidebar.open; - let inset = if width < 360.0 { 8.0 } else { 16.0 }; + let inset = if compact_journal { + 5.0 + } else if width < 360.0 { + 8.0 + } else { + 16.0 + }; + let gap = if compact_journal { + 2.0 + } else if narrow { + 4.0 + } else { + 8.0 + }; + let icon_width = if tight_journal { 24.0 } else { 32.0 }; let id = |name: &str| -> SharedString { if index == 0 { format!("desktop.topbar.{name}").into() @@ -198,20 +221,15 @@ pub(super) fn render_detail_header( .w_full() .h(px(HEADER_HEIGHT)) .flex_shrink(0.0) - .gap(px(if compact_journal { - 3.0 - } else if narrow { - 4.0 - } else { - 8.0 - })) + .gap(px(gap)) .px(px(inset)) .bg(palette.bg_canvas) .text_color(palette.text_primary) .border_b(px(0.5)) .border_color(palette.border); if show_sidebar_switch { - header = header.child(sidebar_switch("desktop.topbar.sidebar_toggle", palette, cx)); + header = header + .child(sidebar_switch("desktop.topbar.sidebar_toggle", palette, cx).w(px(icon_width))); } let close_split = if split_visible { let pane_id = pane.id; @@ -253,26 +271,29 @@ pub(super) fn render_detail_header( .justify_center() .child(indicator), ); - Some(icon_button( - id("close_split"), - CLOSE_ICON_SVG, - false, - palette, - cx.listener(move |this, _, window, cx| { - if let Some(index) = this - .state - .split - .panes - .iter() - .position(|pane| pane.id == pane_id) - { - this.sync_editors(cx); - this.state.split.close_pane(index); - window.focus(&this.focus_handle, cx); - cx.notify(); - } - }), - )) + Some( + icon_button( + id("close_split"), + CLOSE_ICON_SVG, + false, + palette, + cx.listener(move |this, _, window, cx| { + if let Some(index) = this + .state + .split + .panes + .iter() + .position(|pane| pane.id == pane_id) + { + this.sync_editors(cx); + this.state.split.close_pane(index); + window.focus(&this.focus_handle, cx); + cx.notify(); + } + }), + ) + .w(px(icon_width)), + ) } else { None }; @@ -295,16 +316,24 @@ pub(super) fn render_detail_header( cx.notify(); }), ) + .w(px(icon_width)) }); let outer_controls = u8::from(show_sidebar_switch) + u8::from(split_visible) + u8::from(last); let control_width = if narrow { 36.0 } else { 40.0 }; let remaining = width - 2.0 * inset - control_width * f32::from(outer_controls); let readonly = shell.state.store.is_readonly_sqlite(); - let readonly_badge = || render_readonly_sqlite_badge(id("readonly_sqlite_badge"), palette); - let badge_width = if readonly { 84.0 } else { 0.0 }; + let journal_badge_width = if compact_journal { 64.0 } else { 76.0 }; + let readonly_badge = || { + render_readonly_sqlite_badge( + id("readonly_sqlite_badge"), + palette, + journal.then_some(journal_badge_width), + ) + }; let Some(entry) = doc_entry else { let content = div().min_w_0().flex_1().flex().items_center().gap(px(8.0)); let content = if route == &DesktopRoute::Journals { + let badge_width = if readonly { 84.0 } else { 0.0 }; content.children(readonly.then(readonly_badge)).child( div().min_w_0().flex_1().flex().justify_center().child( DesktopShellView::journal_week_picker( @@ -326,12 +355,11 @@ pub(super) fn render_detail_header( .children(inspector_toggle) .children(close_split); }; - let share_width = if hide_share { 0.0 } else { 76.0 }; - let mode_width = if compact_journal { 54.0 } else { 80.0 }; - let calendar_width = remaining - badge_width - share_width - mode_width; - let needs_today = journal && shell.state.journal_date(pane) != shell.state.today_date; - let show_doc_actions = !hide_doc_actions - && (!readonly || !needs_today || calendar_width >= 3.0 * control_width + 54.0); + let share_width = 72.0; + let mode_button_width = if tight_journal { 19.0 } else { 24.0 }; + let mode_padding = if compact_journal { 2.0 } else { 4.0 }; + let mode_gap = if compact_journal { 2.0 } else { 8.0 }; + let show_doc_actions = !journal && !hide_doc_actions; let target = DocTarget { workspace: shell.state.store.active_workspace().id.clone(), pane: pane.id, @@ -342,8 +370,8 @@ pub(super) fn render_detail_header( let mut modes = div() .id(id("mode_switch")) .snapshot(id("mode_switch"), Some("desktop.workbench.topbar")) - .p(px(if compact_journal { 2.0 } else { 4.0 })) - .gap(px(if compact_journal { 2.0 } else { 8.0 })) + .p(px(mode_padding)) + .gap(px(mode_gap)) .flex() .flex_shrink(0.0) .rounded(px(8.0)) @@ -369,7 +397,7 @@ pub(super) fn render_detail_header( } }), ) - .w(px(24.0)) + .w(px(mode_button_width)) .h(px(24.0)) .rounded(px(4.0)) .when(mode == choice, |el| el.bg(palette.bg_elevated)); @@ -415,11 +443,20 @@ pub(super) fn render_detail_header( } header = header.children(readonly.then(readonly_badge)); if journal { - let controls = control_width * if show_doc_actions { 3.0 } else { 1.0 }; + let icons_width = f32::from(outer_controls + 1) * (icon_width + gap); + let mode_width = 2.0 * (mode_button_width + mode_padding) + mode_gap + gap; + let badge_width = if readonly { + journal_badge_width + gap + } else { + 0.0 + }; + let share_width = if hide_share { 0.0 } else { share_width + gap }; + let calendar_width = + width - 2.0 * inset - icons_width - mode_width - badge_width - share_width; header = header.child(div().min_w_0().flex_1().flex().items_center().child( DesktopShellView::journal_week_picker( index, - calendar_width - controls, + calendar_width, !hide_doc_actions, palette, cx, @@ -452,27 +489,40 @@ pub(super) fn render_detail_header( if !journal { header = header.child(div().flex_1().min_w_0()); } - if !hide_share { + if !journal && !hide_share { header = header.child(share_button(id("share"), target.clone(), palette, cx)); } let menu_target = target.clone(); let controls_from_more = u8::from(show_doc_actions) + u8::from(last) + u8::from(split_visible); - let menu_right = right - inset - control_width * f32::from(controls_from_more); - header = header.child(icon_button( - id("more"), - ui_tokens::icons::MORE_HORIZONTAL.svg.as_bytes(), - false, - palette, - cx.listener(move |this, _, window, cx| { - if menu_target.activate(this) { - this.show_header_overlay( - HeaderOverlay::menu(menu_target.clone(), menu_right, bottom, hide_share), - window, - cx, - ); - } - }), - )); + let menu_offset = if journal { + f32::from(controls_from_more) * (icon_width + gap) + + if hide_share { 0.0 } else { share_width + gap } + } else { + f32::from(controls_from_more) * control_width + }; + let menu_right = right - inset - menu_offset; + header = header.child( + icon_button( + id("more"), + ui_tokens::icons::MORE_HORIZONTAL.svg.as_bytes(), + false, + palette, + cx.listener(move |this, _, window, cx| { + if menu_target.activate(this) { + this.show_header_overlay( + HeaderOverlay::menu(menu_target.clone(), menu_right, bottom, hide_share), + window, + cx, + ); + } + }), + ) + .w(px(icon_width)), + ); + if journal && !hide_share { + header = + header.child(share_button(id("share"), target.clone(), palette, cx).w(px(share_width))); + } if show_doc_actions { header = header.child(icon_button( id("doc_info"), @@ -539,58 +589,68 @@ mod tests { 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b) } - #[test] - fn edgeless_header_keeps_mode_controls_without_the_fullscreen_button() { - struct EdgelessHeader(Entity); + struct TestHeader(Entity); - impl Render for EdgelessHeader { - fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { - self.0.update(cx, |shell, cx| { - assert_eq!( - shell.state.split.active().active_mode(), - DocViewMode::Edgeless - ); - render_detail_header( - shell, - HeaderPosition { - index: 0, - first: true, - last: true, - split_visible: false, - width: 640.0, - right: 640.0, - bottom: HEADER_HEIGHT, - }, - crate::palette_for(ThemeMode::Light), - cx, - ) - }) - } + impl Render for TestHeader { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + self.0.update(cx, |shell, cx| { + render_detail_header( + shell, + HeaderPosition { + index: 0, + first: true, + last: true, + split_visible: false, + width: 640.0, + right: 640.0, + bottom: HEADER_HEIGHT, + }, + crate::palette_for(ThemeMode::Light), + cx, + ) + }) } + } + fn render_header(state: DesktopAppState) -> ui::FrameSnapshot { let platform = HeadlessPlatform::new(640, 80, 1.0); let app = Application::with_platform(platform.clone()).run_embedded(|_| {}); let header = app.update(|cx| { register_desktop_fonts(cx).unwrap(); cx.open_window(WindowOptions::default(), |_, cx| { - let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02"); - state.navigate_active_pane(DesktopRoute::Doc("doc-board".into())); let shell = cx.new(|cx| DesktopShellView::new(state, cx)); - cx.new(|_| EdgelessHeader(shell)) + cx.new(|_| TestHeader(shell)) }) .unwrap() }); platform.windows().pop().unwrap().request_frame(); app.update(|cx| { header - .update(cx, |_, window, _| { - let frame = window.frame_snapshot(); - assert!(frame.elements.contains_key("desktop.topbar.mode.edgeless")); - assert!(frame.elements.contains_key("desktop.topbar.more")); - assert!(!frame.elements.contains_key("desktop.topbar.present")); - }) - .unwrap(); - }); + .update(cx, |_, window, _| window.frame_snapshot().clone()) + .unwrap() + }) + } + + #[test] + fn edgeless_header_keeps_mode_controls_without_the_fullscreen_button() { + let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02"); + state.navigate_active_pane(DesktopRoute::Doc("doc-board".into())); + assert_eq!(state.split.active().active_mode(), DocViewMode::Edgeless); + let frame = render_header(state); + assert!(frame.elements.contains_key("desktop.topbar.mode.edgeless")); + assert!(frame.elements.contains_key("desktop.topbar.more")); + assert!(!frame.elements.contains_key("desktop.topbar.present")); + } + + #[test] + fn trashed_journal_uses_the_normal_document_header() { + let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02"); + state.navigate_active_pane(DesktopRoute::Doc("doc-journal-today".into())); + assert!(state.store.trash_doc("doc-journal-today", 1_000)); + let frame = render_header(state); + assert!(frame.elements.contains_key("desktop.topbar.doc_info")); + assert!(!frame.elements.contains_key("desktop.topbar.journal_week")); + assert!(!frame.elements.contains_key("desktop.topbar.today")); } #[test]