diff --git a/.mockery.yaml b/.mockery.yaml index dbce7c9a..2b5b16cd 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -65,6 +65,7 @@ packages: EntryInputTarget: {} HomepageFavorites: {} HomepageHistory: {} + HistorySidebarHistory: {} AllKeybindingsResetter: {} KeybindingResetter: {} KeybindingSetter: {} diff --git a/docs/reference/keybindings.md b/docs/reference/keybindings.md index b8c95970..3063aa93 100644 --- a/docs/reference/keybindings.md +++ b/docs/reference/keybindings.md @@ -83,7 +83,7 @@ These work outside modal modes: | Action | Keys | |--------|------| | Toggle floating pane | `Alt+F` | -| Toggle History system view in right split (may conflict with the browser's default History shortcut; behavior can vary by browser) | `Ctrl+H` | +| Toggle History sidebar (native GTK sidebar panel only). Ctrl+H may conflict with the browser's default History shortcut; behavior can vary by browser. | `Ctrl+H` | | Toggle Favorites system view in right split | unbound by default | | Toggle Config system view in right split | unbound by default | | Close pane (or release floating pane) | `Ctrl+W` | @@ -96,7 +96,7 @@ These work outside modal modes: - `Alt+F` is the only floating-pane shortcut enabled by default. - `Alt+F` toggles floating visibility and keeps floating pane state intact. -- `Ctrl+H` toggles `dumb://history`: it focuses an existing History pane, opens it in a right split if missing, or closes it when already active. Depending on the browser, the default History shortcut may intercept `Ctrl+H`, so behavior can vary. +- `Ctrl+H` toggles the native GTK history sidebar. The sidebar shows browsing history grouped by day with search/filter, keyboard navigation (arrows, Home/End, Ctrl+arrows for day jumps), and activation modes (Enter to navigate while keeping the sidebar open, Ctrl+Enter to navigate while keeping the sidebar open, Shift+Enter to open in a new split). If the native sidebar is unavailable, the shortcut returns an error instead of falling back to `dumb://history`. - `Ctrl+W` closes the active pane; when the floating pane is active, it fully releases that floating session. - Any URL shortcut (for example `Alt+G`) must be defined explicitly in `workspace.floating_pane.profiles`. - Floating profile shortcuts support modifier combos with `ctrl`, `shift`, and `alt` (for example `ctrl+shift+y` or `ctrl+alt+m`). diff --git a/internal/application/port/history_sidebar.go b/internal/application/port/history_sidebar.go new file mode 100644 index 00000000..4562fb83 --- /dev/null +++ b/internal/application/port/history_sidebar.go @@ -0,0 +1,16 @@ +package port + +import ( + "context" + + "github.com/bnema/dumber/internal/application/dto" + "github.com/bnema/dumber/internal/domain/entity" +) + +// HistorySidebarHistory provides the narrow history operations needed by the +// native GTK history sidebar. +type HistorySidebarHistory interface { + GetRecent(ctx context.Context, limit, offset int) ([]*entity.HistoryEntry, error) + Search(ctx context.Context, input dto.HistorySearchInput) (*dto.HistorySearchOutput, error) + Delete(ctx context.Context, id int64) error +} diff --git a/internal/application/usecase/search_history.go b/internal/application/usecase/search_history.go index 51a686ad..0da0682c 100644 --- a/internal/application/usecase/search_history.go +++ b/internal/application/usecase/search_history.go @@ -15,8 +15,10 @@ import ( "github.com/bnema/dumber/internal/logging" ) -// Compile-time check: SearchHistoryUseCase must satisfy port.HomepageHistory. +// Compile-time checks: SearchHistoryUseCase must satisfy the application +// history ports used by WebUI handlers and the native GTK history sidebar. var _ port.HomepageHistory = (*SearchHistoryUseCase)(nil) +var _ port.HistorySidebarHistory = (*SearchHistoryUseCase)(nil) const ( historyWindowDuration = 24 * time.Hour diff --git a/internal/infrastructure/config/defaults.go b/internal/infrastructure/config/defaults.go index a016368b..8f573961 100644 --- a/internal/infrastructure/config/defaults.go +++ b/internal/infrastructure/config/defaults.go @@ -21,7 +21,8 @@ const ( defaultMaxLogFiles = 100 // session log files // Appearance defaults - defaultFontSize = 16 // points + defaultSidebarWidth = 320 // px, clamped to [280, 380] + defaultFontSize = 16 // points defaultExternalThemeProvider = "noctalia" defaultExternalThemeFormat = "colors-json" defaultExternalThemeColorsFilename = "colors.json" @@ -251,8 +252,9 @@ func DefaultConfig() *Config { GLRenderingMode: GLRenderingModeAuto, }, }, - DefaultWebpageZoom: 1.2, // 120% default zoom for better readability - DefaultUIScale: defaultUIScale, // 1.0 = 100%, 2.0 = 200% + DefaultWebpageZoom: 1.2, // 120% default zoom for better readability + DefaultUIScale: defaultUIScale, // 1.0 = 100%, 2.0 = 200% + SidebarWidth: defaultSidebarWidth, // 320px, clamped to [280, 380] Workspace: WorkspaceConfig{ NewPaneURL: defaultNewPaneURL, SwitchToTabOnMove: true, diff --git a/internal/infrastructure/config/loader.go b/internal/infrastructure/config/loader.go index 648d905d..7530321e 100644 --- a/internal/infrastructure/config/loader.go +++ b/internal/infrastructure/config/loader.go @@ -664,6 +664,7 @@ func (m *Manager) setAppearanceDefaults(defaults *Config) { func (m *Manager) setZoomAndScaleDefaults(defaults *Config) { m.viper.SetDefault("default_webpage_zoom", defaults.DefaultWebpageZoom) m.viper.SetDefault("default_ui_scale", defaults.DefaultUIScale) + m.viper.SetDefault("sidebar_width", defaults.SidebarWidth) } func (m *Manager) setWorkspaceDefaults(defaults *Config) { diff --git a/internal/infrastructure/config/migrate.go b/internal/infrastructure/config/migrate.go index bdca4626..888e2ded 100644 --- a/internal/infrastructure/config/migrate.go +++ b/internal/infrastructure/config/migrate.go @@ -283,10 +283,10 @@ const ( // Config format constants. const ( - configFormatTOML = "toml" - configFormatYAML = "yaml" - configFormatJSON = "json" - databasePathKey = "database.path" + configFormatTOML = "toml" + configFormatYAML = "yaml" + configFormatJSON = "json" + databasePathKey = "database.path" ) type defaultActionMap struct { diff --git a/internal/infrastructure/config/schema.go b/internal/infrastructure/config/schema.go index 399d7008..58b1da3b 100644 --- a/internal/infrastructure/config/schema.go +++ b/internal/infrastructure/config/schema.go @@ -95,6 +95,9 @@ type Config struct { DefaultWebpageZoom float64 `mapstructure:"default_webpage_zoom" yaml:"default_webpage_zoom" toml:"default_webpage_zoom"` // DefaultUIScale sets the default UI scale for GTK widgets (1.0 = 100%, 2.0 = 200%) DefaultUIScale float64 `mapstructure:"default_ui_scale" yaml:"default_ui_scale" toml:"default_ui_scale"` + // SidebarWidth sets the preferred width (px) for the history sidebar. + // Clamped to [280, 380] at runtime. 0 means use default (320). + SidebarWidth int `mapstructure:"sidebar_width" yaml:"sidebar_width" toml:"sidebar_width"` // Workspace defines workspace, pane, and tab handling behavior. Workspace WorkspaceConfig `mapstructure:"workspace" yaml:"workspace" toml:"workspace"` // Session controls session persistence and restoration. diff --git a/internal/infrastructure/config/schema_provider.go b/internal/infrastructure/config/schema_provider.go index 80dfa19d..fab5de45 100644 --- a/internal/infrastructure/config/schema_provider.go +++ b/internal/infrastructure/config/schema_provider.go @@ -161,6 +161,14 @@ func (*SchemaProvider) getAppearanceKeys(defaults *Config) []entity.ConfigKeyInf Range: "0.5-3.0", Section: SectionAppearance, }, + { + Key: "sidebar_width", + Type: "int", + Default: fmt.Sprintf("%d", defaults.SidebarWidth), + Description: "Preferred width (px) for the history sidebar. 0 = default", + Range: "0 or 280-380", + Section: SectionAppearance, + }, { Key: "appearance.light_palette.*", Type: "string", diff --git a/internal/infrastructure/config/validation.go b/internal/infrastructure/config/validation.go index b8d4eaab..a7ba3c13 100644 --- a/internal/infrastructure/config/validation.go +++ b/internal/infrastructure/config/validation.go @@ -104,6 +104,9 @@ func validateAppearance(config *Config) []string { if config.DefaultUIScale < 0.5 || config.DefaultUIScale > 3.0 { validationErrors = append(validationErrors, "default_ui_scale must be between 0.5 and 3.0") } + if config.SidebarWidth != 0 && (config.SidebarWidth < 280 || config.SidebarWidth > 380) { + validationErrors = append(validationErrors, "sidebar_width must be between 280 and 380, or 0 for default") + } validationErrors = append(validationErrors, validateExternalTheme(config)...) return validationErrors } diff --git a/internal/ui/app.go b/internal/ui/app.go index 24a1c839..1b122686 100644 --- a/internal/ui/app.go +++ b/internal/ui/app.go @@ -2222,6 +2222,20 @@ func (a *App) activeWorkspaceViewForBrowserWindow(bw *browserWindow) *component. return a.workspaceViews[tab.ID] } +// hideAndRestoreFocusForBrowserWindow hides the sidebar and restores focus to +// the active content pane/webview of the given browser window. +func (a *App) hideAndRestoreFocusForBrowserWindow(bw *browserWindow) { + if bw == nil { + return + } + bw.hideHistorySidebar() + if wsView := a.activeWorkspaceViewForBrowserWindow(bw); wsView != nil { + if ws := a.activeWorkspaceForBrowserWindow(bw); ws != nil { + wsView.FocusPane(ws.ActivePaneID) + } + } +} + // tabTargetForBrowserWindow returns a coordinator.TabTarget scoped to the given browser window. // It does not allocate a TabList; callers that mutate tabs should use // ensureTabTargetForBrowserWindow instead. @@ -3508,6 +3522,7 @@ func (a *App) wireKeyboardActions() { } return a.EjectActivePaneToWindow(ctx, paneID) }) + a.kbDispatcher.SetOnToggleHistorySidebar(a.toggleHistorySidebarAction) a.kbDispatcher.SetOnToggleFloatingPane(func(ctx context.Context) error { return a.ToggleFloatingPane(ctx) }) @@ -5037,6 +5052,9 @@ func (a *App) initConfigWatcher(ctx context.Context) { if bw == nil { continue } + // Reapply sidebar width from live config (reloads after sidebar_width + // changes in the config file). + bw.applySidebarWidthConfig(a) if bw.keyboardHandler != nil { bw.keyboardHandler.ReloadShortcuts(ctx, &a.deps.Config.Workspace, &a.deps.Config.Session) } diff --git a/internal/ui/browser_window.go b/internal/ui/browser_window.go index ae9c8c71..326d2a2b 100644 --- a/internal/ui/browser_window.go +++ b/internal/ui/browser_window.go @@ -37,6 +37,8 @@ type browserWindow struct { globalShortcutHandler *input.GlobalShortcutHandler permissionDialog port.PermissionDialogPresenter webrtcIndicator *component.WebRTCPermissionIndicator + historySidebar *component.HistorySidebar + sidebarVisible bool } func (bw *browserWindow) detachInputForDestroy() { @@ -63,6 +65,12 @@ func (bw *browserWindow) clearShellState() { if bw == nil { return } + // Destroy the history sidebar before releasing the reference so its + // context, debounce timer, callbacks, and in-flight goroutines are + // cleaned up before the window itself is torn down. + if bw.historySidebar != nil { + bw.historySidebar.Destroy() + } bw.appToaster = nil bw.modeToaster = nil bw.borderMgr = nil @@ -76,6 +84,7 @@ func (bw *browserWindow) clearShellState() { bw.globalShortcutHandler = nil bw.permissionDialog = nil bw.webrtcIndicator = nil + bw.historySidebar = nil } func (bw *browserWindow) initChrome(ctx context.Context, a *App) { @@ -88,6 +97,7 @@ func (bw *browserWindow) initChrome(ctx context.Context, a *App) { bw.initAccentPicker(ctx, a) bw.initSessionManager(ctx, a) bw.initTabPicker(ctx, a) + bw.initHistorySidebar(ctx, a) } func (bw *browserWindow) initToasterOverlay(a *App) { @@ -259,6 +269,14 @@ func (bw *browserWindow) ensureTabs() { } } +func (a *App) hasBrowserWindow(target *browserWindow) bool { + if a == nil || target == nil || a.browserWindows == nil { + return false + } + bw, ok := a.browserWindows[target.id] + return ok && bw == target +} + func (a *App) registerBrowserWindow(bw *browserWindow) { if bw == nil { return diff --git a/internal/ui/browser_window_history_sidebar.go b/internal/ui/browser_window_history_sidebar.go new file mode 100644 index 00000000..a721d79b --- /dev/null +++ b/internal/ui/browser_window_history_sidebar.go @@ -0,0 +1,152 @@ +package ui + +import ( + "context" + "fmt" + + "github.com/bnema/dumber/internal/application/port" + "github.com/bnema/dumber/internal/application/usecase" + "github.com/bnema/dumber/internal/logging" + "github.com/bnema/dumber/internal/ui/component" + "github.com/bnema/dumber/internal/ui/window" +) + +// initHistorySidebar creates and mounts the history sidebar into the +// browser window's sidebar container. The sidebar is hidden by default. +func (bw *browserWindow) initHistorySidebar(ctx context.Context, a *App) { + if bw == nil || a == nil || bw.mainWindow == nil || a.deps == nil || a.deps.HistoryUC == nil { + return + } + log := logging.FromContext(ctx) + + cfg := a.buildHistorySidebarConfig(bw) + + sidebar := component.NewHistorySidebar(ctx, cfg) + if sidebar == nil { + log.Warn().Msg("failed to create history sidebar") + return + } + + bw.historySidebar = sidebar + + // Mount into the main window's sidebar box. + bw.mainWindow.SetSidebarWidget(sidebar.Widget()) + bw.historySidebar.Hide() + bw.mainWindow.SetSidebarVisible(false) + bw.sidebarVisible = false + + // Apply sidebar width from config, falling back to the default 320px. + // The width is clamped to [280, 380] by SetSidebarWidth internally. + bw.applySidebarWidthConfig(a) + + log.Debug().Msg("history sidebar initialized") +} + +// buildHistorySidebarConfig constructs the HistorySidebarConfig for the given +// browser window. Extracted from initHistorySidebar for testability. +func (a *App) buildHistorySidebarConfig(bw *browserWindow) component.HistorySidebarConfig { + var historyUC port.HistorySidebarHistory + if a.deps != nil { + historyUC = a.deps.HistoryUC + } + + return component.HistorySidebarConfig{ + HistoryUC: historyUC, + OnNavigate: func(navCtx context.Context, url string) error { + return a.navigateHistorySidebarSelection(navCtx, bw, url) + }, + OnNavigateKeepOpen: func(navCtx context.Context, url string) error { + return a.navigateHistorySidebarSelection(navCtx, bw, url) + }, + OnOpenInNewPane: func(splitCtx context.Context, url string) error { + if a.wsCoord == nil || !a.hasBrowserWindow(bw) { + return nil + } + a.activateBrowserWindow(bw) + return a.wsCoord.SplitWithURL(splitCtx, usecase.SplitRight, url) + }, + OnClose: func() { + a.hideAndRestoreFocusForBrowserWindow(bw) + }, + } +} + +func (a *App) navigateHistorySidebarSelection(ctx context.Context, bw *browserWindow, url string) error { + if a == nil || bw == nil || !a.hasBrowserWindow(bw) { + return nil + } + return a.navigateFromBrowserWindow(ctx, bw, url) +} + +// toggleHistorySidebar toggles sidebar visibility. +func (bw *browserWindow) toggleHistorySidebar() { + if bw == nil || bw.historySidebar == nil { + return + } + + if bw.sidebarVisible { + bw.hideHistorySidebar() + } else { + bw.showHistorySidebar() + } +} + +// showHistorySidebar makes the sidebar visible and grabs focus for the search +// entry. +func (bw *browserWindow) showHistorySidebar() { + if bw == nil || bw.historySidebar == nil || bw.mainWindow == nil { + return + } + bw.historySidebar.Show() + bw.mainWindow.SetSidebarVisible(true) + bw.sidebarVisible = true +} + +// hideHistorySidebar hides the sidebar. Callers should also restore focus +// to the active content pane after calling this. +func (bw *browserWindow) hideHistorySidebar() { + if bw == nil || bw.historySidebar == nil || bw.mainWindow == nil { + return + } + bw.historySidebar.Hide() + bw.mainWindow.SetSidebarVisible(false) + bw.sidebarVisible = false +} + +// historySidebarWidthConfig extracts the config-backed sidebar width and +// applies it via the MainWindow.SetSidebarWidth path. It is called during +// initialization and can be reused if config is reloaded at runtime. +func historySidebarWidthConfig(widthPx int) window.SidebarWidthConfig { + cfg := window.SidebarDefaultWidth() + if widthPx > 0 { + cfg.WidthPx = widthPx + } + return cfg +} + +// applySidebarWidthConfig applies the config-backed sidebar width to the +// main window's sidebar. +func (bw *browserWindow) applySidebarWidthConfig(a *App) { + if bw == nil || bw.mainWindow == nil || a == nil || a.deps == nil || a.deps.Config == nil { + return + } + bw.mainWindow.SetSidebarWidth(historySidebarWidthConfig(a.deps.Config.SidebarWidth)) +} + +// toggleHistorySidebarAction is the keyboard-action handler for toggling the +// history sidebar on the last focused browser window. +func (a *App) toggleHistorySidebarAction(ctx context.Context) error { + bw := a.lastFocusedBrowserWindow() + if bw == nil { + return fmt.Errorf("history sidebar unavailable: no focused browser window") + } + if bw.historySidebar == nil { + return fmt.Errorf("history sidebar unavailable: native sidebar not initialized") + } + if bw.sidebarVisible { + a.hideAndRestoreFocusForBrowserWindow(bw) + return nil + } + bw.toggleHistorySidebar() + return nil +} diff --git a/internal/ui/browser_window_history_sidebar_test.go b/internal/ui/browser_window_history_sidebar_test.go new file mode 100644 index 00000000..d475f071 --- /dev/null +++ b/internal/ui/browser_window_history_sidebar_test.go @@ -0,0 +1,891 @@ +package ui + +import ( + "context" + "testing" + + "github.com/bnema/dumber/internal/application/usecase" + "github.com/bnema/dumber/internal/domain/entity" + "github.com/bnema/dumber/internal/infrastructure/config" + "github.com/bnema/dumber/internal/ui/component" + "github.com/bnema/dumber/internal/ui/coordinator" + contentcoord "github.com/bnema/dumber/internal/ui/coordinator/content" + "github.com/bnema/dumber/internal/ui/dispatcher" + "github.com/bnema/dumber/internal/ui/input" + "github.com/bnema/dumber/internal/ui/window" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// ============================================================================= +// History sidebar integration tests +// ============================================================================= + +// TestHistorySidebarConfig_OnNavigateNavigatesActivePaneAndKeepsSidebar verifies +// that the default OnNavigate callback targets the owning browser window's +// active pane and keeps the sidebar visible. +func TestHistorySidebarConfig_OnNavigateNavigatesActivePaneAndKeepsSidebar(t *testing.T) { + ctx := t.Context() + + // Build two browser windows with independent tabs and panes. + tab1 := entity.NewTab(entity.TabID("tab-1"), entity.WorkspaceID("ws-1"), entity.NewPane(entity.PaneID("pane-1"))) + tab2 := entity.NewTab(entity.TabID("tab-2"), entity.WorkspaceID("ws-2"), entity.NewPane(entity.PaneID("pane-2"))) + + firstTabs := entity.NewTabList() + firstTabs.Add(tab1) + firstTabs.SetActive(tab1.ID) + first := &browserWindow{id: "window-1", tabs: firstTabs} + + secondTabs := entity.NewTabList() + secondTabs.Add(tab2) + secondTabs.SetActive(tab2.ID) + second := &browserWindow{id: "window-2", tabs: secondTabs} + + // Create fake webviews, one per pane. + fakeWv1 := &fakeRecordingWebView{id: 1} + fakeWv2 := &fakeRecordingWebView{id: 2} + + contentCoord := contentcoord.NewCoordinator(ctx, nil, nil, nil, nil, nil, nil, nil) + contentCoord.RegisterPopupWebView(entity.PaneID("pane-1"), fakeWv1) + contentCoord.RegisterPopupWebView(entity.PaneID("pane-2"), fakeWv2) + + navCoord := coordinator.NewNavigationCoordinator(ctx, nil, contentCoord) + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{first.id: first, second.id: second}, + lastFocusedWindowID: first.id, + contentCoord: contentCoord, + navCoord: navCoord, + workspaceViews: map[entity.TabID]*component.WorkspaceView{ + tab1.ID: {}, + tab2.ID: {}, + }, + } + app.tabs.Add(tab1) + app.tabs.Add(tab2) + + second.mainWindow = &window.MainWindow{} + second.historySidebar = &component.HistorySidebar{} + second.sidebarVisible = true + + cfg := app.buildHistorySidebarConfig(second) + navigateURL := "https://example.com" + err := cfg.OnNavigate(ctx, navigateURL) + require.NoError(t, err, "OnNavigate should succeed") + + // The second window's webview must have received the navigation. + assert.True(t, fakeWv2.loadURICalled, "second window webview should receive navigation") + assert.Equal(t, navigateURL, fakeWv2.loadURILastURI) + assert.True(t, second.sidebarVisible, "default history activation should keep the sidebar visible") + + // The first window's webview must NOT have been touched (stale-focus guard). + assert.False(t, fakeWv1.loadURICalled, "first window webview must not receive navigation from second window callback") +} + +// TestHistorySidebarConfig_OnNavigateKeepOpenNavigatesWithoutClosing verifies +// that the OnNavigateKeepOpen callback (Ctrl+Enter) navigates the owning window's +// active pane and keeps the sidebar visible. +func TestHistorySidebarConfig_OnNavigateKeepOpenNavigatesWithoutClosing(t *testing.T) { + ctx := t.Context() + + tab := entity.NewTab(entity.TabID("tab-1"), entity.WorkspaceID("ws-1"), entity.NewPane(entity.PaneID("pane-1"))) + bwTabs := entity.NewTabList() + bwTabs.Add(tab) + bwTabs.SetActive(tab.ID) + + bw := &browserWindow{ + id: "window-1", + tabs: bwTabs, + mainWindow: &window.MainWindow{}, + historySidebar: &component.HistorySidebar{}, + sidebarVisible: true, + } + + contentCoord := contentcoord.NewCoordinator(ctx, nil, nil, nil, nil, nil, nil, nil) + fakeWv := &fakeRecordingWebView{id: 1} + contentCoord.RegisterPopupWebView(entity.PaneID("pane-1"), fakeWv) + navCoord := coordinator.NewNavigationCoordinator(ctx, nil, contentCoord) + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{bw.id: bw}, + contentCoord: contentCoord, + navCoord: navCoord, + workspaceViews: map[entity.TabID]*component.WorkspaceView{ + tab.ID: {}, + }, + } + app.tabs.Add(tab) + + cfg := app.buildHistorySidebarConfig(bw) + navigateURL := "https://keep-open.com" + err := cfg.OnNavigateKeepOpen(ctx, navigateURL) + require.NoError(t, err) + assert.True(t, fakeWv.loadURICalled) + assert.Equal(t, navigateURL, fakeWv.loadURILastURI, "Ctrl+Enter navigation should go to the URL") + assert.True(t, bw.sidebarVisible, "Ctrl+Enter navigation should keep the sidebar visible") +} + +// TestHistorySidebar_OwnershipOnMultiWindowNavigation verifies that when +// multiple browser windows have history sidebars, navigation targets the +// correct owning window's active pane. This tests the stale-focus scenario +// where a different window is globally focused. +func TestHistorySidebar_OwnershipOnMultiWindowNavigation(t *testing.T) { + ctx := t.Context() + + // Two windows, each with their own tab and pane. + tab1 := entity.NewTab(entity.TabID("tab-1"), entity.WorkspaceID("ws-1"), entity.NewPane(entity.PaneID("pane-1"))) + tab2 := entity.NewTab(entity.TabID("tab-2"), entity.WorkspaceID("ws-2"), entity.NewPane(entity.PaneID("pane-2"))) + + firstTabs := entity.NewTabList() + firstTabs.Add(tab1) + firstTabs.SetActive(tab1.ID) + first := &browserWindow{id: "window-1", tabs: firstTabs} + + secondTabs := entity.NewTabList() + secondTabs.Add(tab2) + secondTabs.SetActive(tab2.ID) + second := &browserWindow{id: "window-2", tabs: secondTabs} + + fakeWv1 := &fakeRecordingWebView{id: 1} + fakeWv2 := &fakeRecordingWebView{id: 2} + + contentCoord := contentcoord.NewCoordinator(ctx, nil, nil, nil, nil, nil, nil, nil) + contentCoord.RegisterPopupWebView(entity.PaneID("pane-1"), fakeWv1) + contentCoord.RegisterPopupWebView(entity.PaneID("pane-2"), fakeWv2) + + navCoord := coordinator.NewNavigationCoordinator(ctx, nil, contentCoord) + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{first.id: first, second.id: second}, + lastFocusedWindowID: first.id, // STALE: first is globally focused + contentCoord: contentCoord, + navCoord: navCoord, + workspaceViews: map[entity.TabID]*component.WorkspaceView{ + tab1.ID: {}, + tab2.ID: {}, + }, + } + app.tabs.Add(tab1) + app.tabs.Add(tab2) + + // Navigation from the SECOND window should target pane-2 even though + // the first window is globally focused (stale focus). + err := app.navigateFromBrowserWindow(ctx, second, "https://second-window.com") + require.NoError(t, err) + + // Second window's webview must receive the navigation. + assert.True(t, fakeWv2.loadURICalled, "second window webview should receive navigation") + assert.Equal(t, "https://second-window.com", fakeWv2.loadURILastURI) + + // First window's webview must NOT have been touched. + assert.False(t, fakeWv1.loadURICalled, "first window webview should NOT receive navigation when second was targeted") +} + +// ============================================================================= +// History sidebar toggle state tests +// ============================================================================= + +// TestBrowserWindow_HistorySidebarToggle_NilIsNoOp verifies that when +// browserWindow.historySidebar is nil, toggleHistorySidebar is a safe +// no-op and sidebarVisible stays / remains false. +func TestBrowserWindow_HistorySidebarToggle_NilIsNoOp(t *testing.T) { + t.Parallel() + + bw := &browserWindow{id: "test-window", sidebarVisible: false} + require.Nil(t, bw.historySidebar, "historySidebar must be nil for this test") + + // Should not panic even though historySidebar is nil. + bw.toggleHistorySidebar() + assert.False(t, bw.sidebarVisible, "sidebarVisible must remain false when historySidebar is nil") + + // Calling again also safe. + bw.toggleHistorySidebar() + assert.False(t, bw.sidebarVisible, "sidebarVisible must remain false on second toggle") +} + +// TestBrowserWindow_HistorySidebarToggle_FlipsSidebarVisible verifies that +// toggleHistorySidebar correctly flips sidebarVisible when the sidebar +// has been set. Uses a zero-value HistorySidebar (all nil-checked methods +// are safe to call). +func TestBrowserWindow_HistorySidebarToggle_FlipsSidebarVisible(t *testing.T) { + t.Parallel() + + bw := &browserWindow{ + id: "test-window", + mainWindow: &window.MainWindow{}, + historySidebar: &component.HistorySidebar{}, + sidebarVisible: false, + } + + bw.toggleHistorySidebar() + assert.True(t, bw.sidebarVisible, "sidebarVisible must be true after first toggle") + + bw.toggleHistorySidebar() + assert.False(t, bw.sidebarVisible, "sidebarVisible must be false after second toggle") + + bw.toggleHistorySidebar() + assert.True(t, bw.sidebarVisible, "sidebarVisible must be true after third toggle") +} + +// TestBrowserWindow_HistorySidebarShowHide_TransitionsSidebarVisible +// verifies that showHistorySidebar and hideHistorySidebar independently +// set sidebarVisible to true and false respectively. +func TestBrowserWindow_HistorySidebarShowHide_TransitionsSidebarVisible(t *testing.T) { + t.Parallel() + + bw := &browserWindow{ + id: "test-window", + mainWindow: &window.MainWindow{}, + historySidebar: &component.HistorySidebar{}, + sidebarVisible: false, + } + + // Show sets visible + bw.showHistorySidebar() + assert.True(t, bw.sidebarVisible, "sidebarVisible must be true after show") + + // Hide clears visible + bw.hideHistorySidebar() + assert.False(t, bw.sidebarVisible, "sidebarVisible must be false after hide") + + // Redundant hide is idempotent + bw.hideHistorySidebar() + assert.False(t, bw.sidebarVisible, "sidebarVisible must remain false after redundant hide") + + // Show again + bw.showHistorySidebar() + assert.True(t, bw.sidebarVisible, "sidebarVisible must be true after second show") +} + +// TestBrowserWindow_HistorySidebarShowHide_NilSidebarIsNoOp verifies that +// show/hide do not panic when historySidebar is nil. +func TestBrowserWindow_HistorySidebarShowHide_NilSidebarIsNoOp(t *testing.T) { + t.Parallel() + + bw := &browserWindow{id: "test-window"} + require.Nil(t, bw.historySidebar) + + // Should not panic even though historySidebar is nil. + bw.showHistorySidebar() + assert.False(t, bw.sidebarVisible, "sidebarVisible must remain false when nil") + + bw.hideHistorySidebar() + assert.False(t, bw.sidebarVisible, "sidebarVisible must remain false when nil") +} + +// ============================================================================= +// App wiring: toggle handler uses lastFocusedBrowserWindow +// ============================================================================= + +// TestApp_HistorySidebarToggleHandlerUsesLastFocusedWindow verifies that +// the toggle handler wired in App.wireKeyboardActions picks the +// lastFocusedBrowserWindow and calls toggleHistorySidebar on it. +func TestApp_HistorySidebarToggleHandlerUsesLastFocusedWindow(t *testing.T) { + // Two windows, only the focused one has a history sidebar. + focusedBW := &browserWindow{ + id: "focused", + mainWindow: &window.MainWindow{}, + historySidebar: &component.HistorySidebar{}, + sidebarVisible: false, + } + otherBW := &browserWindow{ + id: "other", + mainWindow: &window.MainWindow{}, + } + + app := &App{ + browserWindows: map[string]*browserWindow{ + focusedBW.id: focusedBW, + otherBW.id: otherBW, + }, + lastFocusedWindowID: focusedBW.id, + } + + // Simulate the toggle handler that wireKeyboardActions registers: + // a.kbDispatcher.SetOnToggleHistorySidebar(func(ctx context.Context) error { + // bw := a.lastFocusedBrowserWindow() + // ... + // }) + bw := app.lastFocusedBrowserWindow() + require.NotNil(t, bw, "lastFocusedBrowserWindow must not be nil") + require.Equal(t, focusedBW.id, bw.id, "must return the focused window") + require.NotNil(t, bw.historySidebar, "focused window must have a history sidebar") + + // Toggle on the focused window. + bw.toggleHistorySidebar() + assert.True(t, bw.sidebarVisible, "sidebar on focused window must become visible") + + // The other window must remain untouched. + assert.False(t, otherBW.sidebarVisible, "other window sidebar must remain invisible") + + // Toggle again: focused window sidebar hides. + bw.toggleHistorySidebar() + assert.False(t, bw.sidebarVisible, "sidebar on focused window must hide on second toggle") +} + +// TestApp_HistorySidebarToggleHandler_NilFocusedWindowIsNoOp verifies +// that the toggle handler is safe when lastFocusedBrowserWindow returns nil. +func TestApp_HistorySidebarToggleHandler_NilFocusedWindowIsNoOp(t *testing.T) { + app := &App{ + browserWindows: make(map[string]*browserWindow), + lastFocusedWindowID: "missing", + } + + // The handler should return early without error when bw is nil. + bw := app.lastFocusedBrowserWindow() + require.Nil(t, bw, "lastFocusedBrowserWindow should return nil for missing window") +} + +// ============================================================================= +// History sidebar config callbacks: focus restoration on close +// ============================================================================= + +// TestHistorySidebarConfig_OnCloseHidesSidebar verifies that the OnClose +// callback hides the sidebar for the owning browser window. +func TestHistorySidebarConfig_OnCloseHidesSidebar(t *testing.T) { + tab := entity.NewTab(entity.TabID("tab-1"), entity.WorkspaceID("ws-1"), entity.NewPane(entity.PaneID("pane-1"))) + bwTabs := entity.NewTabList() + bwTabs.Add(tab) + bwTabs.SetActive(tab.ID) + + bw := &browserWindow{ + id: "window-1", + tabs: bwTabs, + mainWindow: &window.MainWindow{}, + historySidebar: &component.HistorySidebar{}, + sidebarVisible: true, + } + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{bw.id: bw}, + workspaceViews: map[entity.TabID]*component.WorkspaceView{tab.ID: {}}, + } + app.tabs.Add(tab) + + cfg := app.buildHistorySidebarConfig(bw) + cfg.OnClose() + + assert.False(t, bw.sidebarVisible, "sidebar must be hidden by OnClose") +} + +// ============================================================================= +// Dispatcher-backed Ctrl+H integration test +// ============================================================================= + +// TestApp_HistorySidebar_ToggleThroughKeyboardDispatcher wires the real toggle +// handler from App.wireKeyboardActions through the KeyboardDispatcher and +// dispatches ActionToggleHistorySystemView, asserting the focused browser +// window's sidebar visibility toggles. +func TestApp_HistorySidebar_ToggleThroughKeyboardDispatcher(t *testing.T) { + ctx := t.Context() + + focusedBW := &browserWindow{ + id: "focused", + mainWindow: &window.MainWindow{}, + historySidebar: &component.HistorySidebar{}, + sidebarVisible: false, + } + otherBW := &browserWindow{ + id: "other", + mainWindow: &window.MainWindow{}, + } + + app := &App{ + browserWindows: map[string]*browserWindow{ + focusedBW.id: focusedBW, + otherBW.id: otherBW, + }, + lastFocusedWindowID: focusedBW.id, + } + + // Create a KeyboardDispatcher and wire the production toggle handler. + kbDispatcher := dispatcher.NewKeyboardDispatcher( + ctx, + &coordinator.WorkspaceCoordinator{}, + &coordinator.NavigationCoordinator{}, + nil, nil, + dispatcher.KeyboardActions{}, + func(context.Context) entity.PaneID { return "" }, + ) + + kbDispatcher.SetOnToggleHistorySidebar(app.toggleHistorySidebarAction) + + // First dispatch: toggle ON. + err := kbDispatcher.Dispatch(ctx, input.ActionToggleHistorySystemView) + require.NoError(t, err) + assert.True(t, focusedBW.sidebarVisible, "focused window sidebar must be visible after toggle") + assert.False(t, otherBW.sidebarVisible, "other window sidebar must remain invisible") + + // Second dispatch: toggle OFF. + err = kbDispatcher.Dispatch(ctx, input.ActionToggleHistorySystemView) + require.NoError(t, err) + assert.False(t, focusedBW.sidebarVisible, "focused window sidebar must be hidden after second toggle") +} + +// TestApp_HistorySidebar_ToggleThroughDispatcher_UnavailableReturnsError verifies that +// when the focused window has no history sidebar, Ctrl+H returns a clean error. +func TestApp_HistorySidebar_ToggleThroughDispatcher_UnavailableReturnsError(t *testing.T) { + ctx := t.Context() + + bw := &browserWindow{ + id: "no-sidebar", + mainWindow: &window.MainWindow{}, + // historySidebar is nil + } + + app := &App{ + browserWindows: map[string]*browserWindow{bw.id: bw}, + lastFocusedWindowID: bw.id, + } + + kbDispatcher := dispatcher.NewKeyboardDispatcher( + ctx, + &coordinator.WorkspaceCoordinator{}, + &coordinator.NavigationCoordinator{}, + nil, nil, + dispatcher.KeyboardActions{}, + func(context.Context) entity.PaneID { return "" }, + ) + + kbDispatcher.SetOnToggleHistorySidebar(app.toggleHistorySidebarAction) + + err := kbDispatcher.Dispatch(ctx, input.ActionToggleHistorySystemView) + require.Error(t, err) + require.ErrorContains(t, err, "history sidebar unavailable") + assert.False(t, bw.sidebarVisible, "sidebar must remain invisible when not wired") +} + +// TestApp_HistorySidebar_ToggleThroughDispatcher_NilFocusedReturnsError verifies +// that the toggle handler returns a clean error when there is no focused window. +func TestApp_HistorySidebar_ToggleThroughDispatcher_NilFocusedReturnsError(t *testing.T) { + ctx := t.Context() + + app := &App{ + browserWindows: make(map[string]*browserWindow), + lastFocusedWindowID: "missing", + } + + kbDispatcher := dispatcher.NewKeyboardDispatcher( + ctx, + &coordinator.WorkspaceCoordinator{}, + &coordinator.NavigationCoordinator{}, + nil, nil, + dispatcher.KeyboardActions{}, + func(context.Context) entity.PaneID { return "" }, + ) + + kbDispatcher.SetOnToggleHistorySidebar(app.toggleHistorySidebarAction) + + err := kbDispatcher.Dispatch(ctx, input.ActionToggleHistorySystemView) + require.Error(t, err) + require.ErrorContains(t, err, "history sidebar unavailable") +} + +// ============================================================================= +// buildHistorySidebarConfig callback seam tests +// ============================================================================= + +// TestApp_HistorySidebarConfig_NavigateCallback verifies that the OnNavigate +// callback from buildHistorySidebarConfig navigates the owning browser window's +// active pane to the given URL. +func TestApp_HistorySidebarConfig_NavigateCallbackNavigates(t *testing.T) { + ctx := t.Context() + + paneID := entity.PaneID("pane-1") + tab := entity.NewTab(entity.TabID("tab-1"), entity.WorkspaceID("ws-1"), entity.NewPane(paneID)) + bwTabs := entity.NewTabList() + bwTabs.Add(tab) + bwTabs.SetActive(tab.ID) + + bw := &browserWindow{ + id: "window-1", + tabs: bwTabs, + mainWindow: &window.MainWindow{}, + historySidebar: &component.HistorySidebar{}, + sidebarVisible: true, + } + + fakeWv := &fakeRecordingWebView{id: 1} + contentCoord := contentcoord.NewCoordinator(ctx, nil, nil, nil, nil, nil, nil, nil) + contentCoord.RegisterPopupWebView(paneID, fakeWv) + navCoord := coordinator.NewNavigationCoordinator(ctx, nil, contentCoord) + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{bw.id: bw}, + contentCoord: contentCoord, + navCoord: navCoord, + workspaceViews: map[entity.TabID]*component.WorkspaceView{ + tab.ID: {}, + }, + } + app.tabs.Add(tab) + + // Build the config using the extracted seam. + cfg := app.buildHistorySidebarConfig(bw) + require.NotNil(t, cfg.OnNavigate, "OnNavigate callback must be non-nil") + + // Invoke the OnNavigate callback. + navigateURL := "https://navigated.com" + err := cfg.OnNavigate(ctx, navigateURL) + require.NoError(t, err) + + // Verify the navigation reached the correct webview. + assert.True(t, fakeWv.loadURICalled, "webview must receive navigation") + assert.Equal(t, navigateURL, fakeWv.loadURILastURI) + assert.True(t, bw.sidebarVisible, "default history navigation should keep the sidebar open") +} + +func TestApp_NavigateHistorySidebarSelection_KeepsSidebarVisible(t *testing.T) { + ctx := t.Context() + + paneID := entity.PaneID("pane-1") + tab := entity.NewTab(entity.TabID("tab-1"), entity.WorkspaceID("ws-1"), entity.NewPane(paneID)) + bwTabs := entity.NewTabList() + bwTabs.Add(tab) + bwTabs.SetActive(tab.ID) + + bw := &browserWindow{ + id: "window-1", + tabs: bwTabs, + mainWindow: &window.MainWindow{}, + historySidebar: &component.HistorySidebar{}, + sidebarVisible: true, + } + + fakeWv := &fakeRecordingWebView{id: 1} + contentCoord := contentcoord.NewCoordinator(ctx, nil, nil, nil, nil, nil, nil, nil) + contentCoord.RegisterPopupWebView(paneID, fakeWv) + navCoord := coordinator.NewNavigationCoordinator(ctx, nil, contentCoord) + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{bw.id: bw}, + contentCoord: contentCoord, + navCoord: navCoord, + workspaceViews: map[entity.TabID]*component.WorkspaceView{ + tab.ID: {}, + }, + } + app.tabs.Add(tab) + + err := app.navigateHistorySidebarSelection(ctx, bw, "https://open.com") + require.NoError(t, err) + assert.True(t, fakeWv.loadURICalled) + assert.Equal(t, "https://open.com", fakeWv.loadURILastURI) + assert.True(t, bw.sidebarVisible, "history selection navigation should not hide the sidebar") +} + +// TestApp_HistorySidebarConfig_NavigateCallbackOwnership verifies that +// OnNavigate targets the callback's owning window, not the globally focused +// window, when they differ. +func TestApp_HistorySidebarConfig_NavigateCallbackOwnership(t *testing.T) { + ctx := t.Context() + + tab1 := entity.NewTab(entity.TabID("tab-1"), entity.WorkspaceID("ws-1"), entity.NewPane(entity.PaneID("pane-1"))) + tab2 := entity.NewTab(entity.TabID("tab-2"), entity.WorkspaceID("ws-2"), entity.NewPane(entity.PaneID("pane-2"))) + + firstTabs := entity.NewTabList() + firstTabs.Add(tab1) + firstTabs.SetActive(tab1.ID) + first := &browserWindow{id: "window-1", tabs: firstTabs} + + secondTabs := entity.NewTabList() + secondTabs.Add(tab2) + secondTabs.SetActive(tab2.ID) + second := &browserWindow{id: "window-2", tabs: secondTabs} + + fakeWv1 := &fakeRecordingWebView{id: 1} + fakeWv2 := &fakeRecordingWebView{id: 2} + + contentCoord := contentcoord.NewCoordinator(ctx, nil, nil, nil, nil, nil, nil, nil) + contentCoord.RegisterPopupWebView(entity.PaneID("pane-1"), fakeWv1) + contentCoord.RegisterPopupWebView(entity.PaneID("pane-2"), fakeWv2) + navCoord := coordinator.NewNavigationCoordinator(ctx, nil, contentCoord) + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{first.id: first, second.id: second}, + lastFocusedWindowID: first.id, // STALE: first is globally focused + contentCoord: contentCoord, + navCoord: navCoord, + workspaceViews: map[entity.TabID]*component.WorkspaceView{ + tab1.ID: {}, + tab2.ID: {}, + }, + } + app.tabs.Add(tab1) + app.tabs.Add(tab2) + + // Build config for the SECOND window, even though first is globally focused. + cfg := app.buildHistorySidebarConfig(second) + + // Invoke OnNavigate — should navigate through second window's pane-2. + err := cfg.OnNavigate(ctx, "https://ownership.com") + require.NoError(t, err) + + // Second window's webview must receive navigation. + assert.True(t, fakeWv2.loadURICalled, "second window webview must receive navigation") + assert.Equal(t, "https://ownership.com", fakeWv2.loadURILastURI) + + // First window's webview must NOT be touched. + assert.False(t, fakeWv1.loadURICalled, "first window webview must not receive navigation") +} + +// TestApp_HistorySidebarConfig_KeepOpenCallback verifies that +// OnNavigateKeepOpen navigates the owning window without hiding the sidebar. +func TestApp_HistorySidebarConfig_KeepOpenCallback(t *testing.T) { + ctx := t.Context() + + paneID := entity.PaneID("pane-1") + tab := entity.NewTab(entity.TabID("tab-1"), entity.WorkspaceID("ws-1"), entity.NewPane(paneID)) + bwTabs := entity.NewTabList() + bwTabs.Add(tab) + bwTabs.SetActive(tab.ID) + + bw := &browserWindow{ + id: "window-1", + tabs: bwTabs, + mainWindow: &window.MainWindow{}, + historySidebar: &component.HistorySidebar{}, + sidebarVisible: true, + } + + fakeWv := &fakeRecordingWebView{id: 1} + contentCoord := contentcoord.NewCoordinator(ctx, nil, nil, nil, nil, nil, nil, nil) + contentCoord.RegisterPopupWebView(paneID, fakeWv) + navCoord := coordinator.NewNavigationCoordinator(ctx, nil, contentCoord) + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{bw.id: bw}, + contentCoord: contentCoord, + navCoord: navCoord, + workspaceViews: map[entity.TabID]*component.WorkspaceView{ + tab.ID: {}, + }, + } + app.tabs.Add(tab) + + cfg := app.buildHistorySidebarConfig(bw) + + // OnNavigateKeepOpen navigates but does NOT close the sidebar. + navigateURL := "https://keep-open.com" + err := cfg.OnNavigateKeepOpen(ctx, navigateURL) + require.NoError(t, err) + + assert.True(t, fakeWv.loadURICalled, "webview must receive navigation") + assert.Equal(t, navigateURL, fakeWv.loadURILastURI) + + // Sidebar must remain visible (keep-open contract). + assert.True(t, bw.sidebarVisible, "sidebar must stay visible after keep-open navigation") +} + +// TestApp_HistorySidebarConfig_OpenInNewPaneCallback verifies that +// OnOpenInNewPane activates the owning browser window and creates a split +// with the target URL. +func TestApp_HistorySidebarConfig_OpenInNewPaneCallback(t *testing.T) { + ctx := t.Context() + + paneID := entity.PaneID("pane-1") + tab := entity.NewTab(entity.TabID("tab-1"), entity.WorkspaceID("ws-1"), entity.NewPane(paneID)) + bwTabs := entity.NewTabList() + bwTabs.Add(tab) + bwTabs.SetActive(tab.ID) + + ws := entity.NewWorkspace("ws-1", entity.NewPane(paneID)) + ws.ActivePaneID = paneID + tab.Workspace = ws + + bw := &browserWindow{id: "window-1", tabs: bwTabs} + + panesUC := usecase.NewManagePanesUseCase(func() string { return "pane-2" }) + wsCoord := coordinator.NewWorkspaceCoordinator(ctx, coordinator.WorkspaceCoordinatorConfig{ + PanesUC: panesUC, + GetActiveWS: func() (*entity.Workspace, *component.WorkspaceView) { + return ws, nil + }, + }) + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{bw.id: bw}, + wsCoord: wsCoord, + workspaceViews: map[entity.TabID]*component.WorkspaceView{ + tab.ID: {}, + }, + } + app.tabs.Add(tab) + + cfg := app.buildHistorySidebarConfig(bw) + + // OnOpenInNewPane should activate the owning window and split with URL. + splitURL := "https://shift-enter.com" + err := cfg.OnOpenInNewPane(ctx, splitURL) + require.NoError(t, err) + + // After split, workspace should have 2 panes. + require.Equal(t, 2, ws.PaneCount(), "workspace should have 2 panes after split") + + // The new pane should have the split URL. + allPanes := ws.AllPanes() + var newPane *entity.Pane + for _, p := range allPanes { + if p != nil && p.ID != paneID { + newPane = p + break + } + } + require.NotNil(t, newPane, "new pane must exist after split") + assert.Equal(t, splitURL, newPane.URI, "new pane must have the split URL") +} + +// TestApp_HistorySidebarConfig_CloseCallback verifies that OnClose hides the +// sidebar for the owning browser window and restores focus to the active pane. +func TestApp_HistorySidebarConfig_CloseCallback(t *testing.T) { + paneID := entity.PaneID("pane-1") + tab := entity.NewTab(entity.TabID("tab-1"), entity.WorkspaceID("ws-1"), entity.NewPane(paneID)) + bwTabs := entity.NewTabList() + bwTabs.Add(tab) + bwTabs.SetActive(tab.ID) + + ws := entity.NewWorkspace("ws-1", entity.NewPane(paneID)) + ws.ActivePaneID = paneID + tab.Workspace = ws + + bw := &browserWindow{ + id: "window-1", + tabs: bwTabs, + mainWindow: &window.MainWindow{}, + historySidebar: &component.HistorySidebar{}, + sidebarVisible: true, + } + + // Create a minimal workspace view. + wsView := &component.WorkspaceView{} + // We set up the app so that hideAndRestoreFocusForBrowserWindow + // can find the wsView and call FocusPane on it. + // Since FocusPane is a method on WorkspaceView that requires GTK, + // we verify the state changes that happen before FocusPane: + // sidebarVisible must be toggled to false. + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{bw.id: bw}, + workspaceViews: map[entity.TabID]*component.WorkspaceView{ + tab.ID: wsView, + }, + } + cfg := app.buildHistorySidebarConfig(bw) + + // OnClose hides the sidebar. + cfg.OnClose() + + assert.False(t, bw.sidebarVisible, "sidebar must be hidden after close") +} + +// TestApp_HistorySidebarConfig_CloseWithNoSidebarIsSafe verifies that +// OnClose (hideAndRestoreFocusForBrowserWindow) is safe when the browser +// window has no sidebar or is nil. +func TestApp_HistorySidebarConfig_CloseWithNoSidebarIsSafe(t *testing.T) { + bw := &browserWindow{ + id: "no-sidebar", + mainWindow: &window.MainWindow{}, + // historySidebar is nil + } + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{bw.id: bw}, + } + + cfg := app.buildHistorySidebarConfig(bw) + + // Should not panic even with nil sidebar. + require.NotPanics(t, func() { cfg.OnClose() }) +} + +// TestApp_HideAndRestoreFocusForBrowserWindow_HidesSidebar verifies that +// hideAndRestoreFocusForBrowserWindow hides the sidebar. +func TestApp_HideAndRestoreFocusForBrowserWindow_HidesSidebar(t *testing.T) { + paneID := entity.PaneID("pane-1") + tab := entity.NewTab(entity.TabID("tab-1"), entity.WorkspaceID("ws-1"), entity.NewPane(paneID)) + bwTabs := entity.NewTabList() + bwTabs.Add(tab) + bwTabs.SetActive(tab.ID) + + ws := entity.NewWorkspace("ws-1", entity.NewPane(paneID)) + ws.ActivePaneID = paneID + tab.Workspace = ws + + bw := &browserWindow{ + id: "window-1", + tabs: bwTabs, + mainWindow: &window.MainWindow{}, + historySidebar: &component.HistorySidebar{}, + sidebarVisible: true, + } + + wsView := &component.WorkspaceView{} + + app := &App{ + tabs: entity.NewTabList(), + browserWindows: map[string]*browserWindow{bw.id: bw}, + workspaceViews: map[entity.TabID]*component.WorkspaceView{ + tab.ID: wsView, + }, + } + app.tabs.Add(tab) + + // Call hideAndRestoreFocusForBrowserWindow directly. + app.hideAndRestoreFocusForBrowserWindow(bw) + + // Sidebar must be hidden. + assert.False(t, bw.sidebarVisible, "sidebar must be hidden") +} + +// TestApp_HideAndRestoreFocusForBrowserWindow_NilBWIsSafe verifies that +// hideAndRestoreFocusForBrowserWindow handles nil browser window safely. +func TestApp_HideAndRestoreFocusForBrowserWindow_NilBWIsSafe(t *testing.T) { + app := &App{} + require.NotPanics(t, func() { app.hideAndRestoreFocusForBrowserWindow(nil) }) +} + +// ============================================================================= +// Sidebar width config tests +// ============================================================================= + +func TestHistorySidebarWidthConfig_ConfigValue(t *testing.T) { + cfg := historySidebarWidthConfig(350) + assert.Equal(t, 350, cfg.WidthPx, "should apply config-backed width of 350px") + assert.Equal(t, 280, cfg.MinPx, "should keep default min clamp") + assert.Equal(t, 380, cfg.MaxPx, "should keep default max clamp") +} + +func TestHistorySidebarWidthConfig_DefaultValue(t *testing.T) { + cfg := historySidebarWidthConfig(0) + assert.Equal(t, 320, cfg.WidthPx, "should use default width of 320px when config is 0") + assert.Equal(t, 280, cfg.MinPx, "should keep default min clamp") + assert.Equal(t, 380, cfg.MaxPx, "should keep default max clamp") +} + +// TestBrowserWindow_ApplySidebarWidthConfig_NilMainWindowIsSafe verifies +// that applySidebarWidthConfig handles nil mainWindow without panic. +func TestBrowserWindow_ApplySidebarWidthConfig_NilMainWindowIsSafe(t *testing.T) { + bw := &browserWindow{mainWindow: nil} + app := &App{deps: &Dependencies{Config: &config.Config{SidebarWidth: 300}}} + require.NotPanics(t, func() { bw.applySidebarWidthConfig(app) }) +} + +// TestBrowserWindow_ApplySidebarWidthConfig_NilDepsIsSafe verifies +// that applySidebarWidthConfig handles nil deps without panic. +func TestBrowserWindow_ApplySidebarWidthConfig_NilDepsIsSafe(t *testing.T) { + mw := &window.MainWindow{} + bw := &browserWindow{mainWindow: mw} + app := &App{deps: nil} + require.NotPanics(t, func() { bw.applySidebarWidthConfig(app) }) +} diff --git a/internal/ui/browser_window_test.go b/internal/ui/browser_window_test.go index 08538f06..ac4bcf1e 100644 --- a/internal/ui/browser_window_test.go +++ b/internal/ui/browser_window_test.go @@ -43,6 +43,7 @@ func TestBrowserWindow_RemoveBrowserWindowClearsShellState(t *testing.T) { setShellField(t, removed, "globalShortcutHandler", &input.GlobalShortcutHandler{}) setShellField(t, removed, "permissionDialog", (*testPermissionDialogPresenter)(nil)) setShellField(t, removed, "webrtcIndicator", &component.WebRTCPermissionIndicator{}) + setShellField(t, removed, "historySidebar", &component.HistorySidebar{}) app.removeBrowserWindow(removed.id) @@ -60,6 +61,7 @@ func TestBrowserWindow_RemoveBrowserWindowClearsShellState(t *testing.T) { "globalShortcutHandler", "permissionDialog", "webrtcIndicator", + "historySidebar", } { if !fieldIsZero(t, removed, name) { t.Fatalf("browserWindow.%s was not cleared", name) diff --git a/internal/ui/component/history_model.go b/internal/ui/component/history_model.go new file mode 100644 index 00000000..a6019969 --- /dev/null +++ b/internal/ui/component/history_model.go @@ -0,0 +1,304 @@ +// Package component provides UI components for the browser. +package component + +import ( + "sort" + "strconv" + "strings" + "time" + + "github.com/bnema/dumber/internal/domain/entity" +) + +// historyGroup represents a day-grouped section of history entries. +// Groups are ordered newest-first; entries within a group are newest-first. +type historyGroup struct { + Label string + Entries []*entity.HistoryEntry +} + +// dayKey identifies a unique calendar day for grouping. +type dayKey struct { + year int + month time.Month + day int +} + +const ( + dayLabelToday = "Today" + dayLabelYesterday = "Yesterday" + dayLabelOtherYearFormat = "January 2, 2006" + hoursPerDay = 24 +) + +// groupHistoryByDay groups entries by calendar day, newest-first. +// Days are labeled: "Today", "Yesterday", "Monday, January 2" (current year), +// "January 2, 2006" (other years). Within each group entries remain in +// the order they arrived (assumed most-recent-first). +func groupHistoryByDay(entries []*entity.HistoryEntry) []historyGroup { + if len(entries) == 0 { + return nil + } + + now := time.Now() + local := now.Location() + todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, local) + + groupsMap := make(map[dayKey]*historyGroup) + var keys []dayKey + + for _, entry := range entries { + t := entry.LastVisited.In(local) + key := dayKey{t.Year(), t.Month(), t.Day()} + + if _, ok := groupsMap[key]; !ok { + label := dayLabelForKey(key, todayStart, now) + groupsMap[key] = &historyGroup{Label: label} + keys = append(keys, key) + } + groupsMap[key].Entries = append(groupsMap[key].Entries, entry) + } + + // Sort keys newest-first + sort.Slice(keys, func(i, j int) bool { + ki, kj := keys[i], keys[j] + ti := time.Date(ki.year, ki.month, ki.day, 0, 0, 0, 0, local) + tj := time.Date(kj.year, kj.month, kj.day, 0, 0, 0, 0, local) + return ti.After(tj) + }) + + groups := make([]historyGroup, len(keys)) + for i, k := range keys { + groups[i] = *groupsMap[k] + } + return groups +} + +func dayLabelForKey(key dayKey, todayStart, now time.Time) string { + dayStart := time.Date(key.year, key.month, key.day, 0, 0, 0, 0, now.Location()) + switch { + case dayStart.Equal(todayStart): + return dayLabelToday + case dayStart.Equal(todayStart.AddDate(0, 0, -1)): + return dayLabelYesterday + case key.year == now.Year(): + return dayStart.Format("Monday, January 2") + default: + return dayStart.Format(dayLabelOtherYearFormat) + } +} + +// readableURL strips the protocol prefix and optional www. for display. +func readableURL(rawURL string) string { + u := rawURL + for _, prefix := range []string{"https://", "http://", "ftp://"} { + if strings.HasPrefix(u, prefix) { + u = u[len(prefix):] + break + } + } + u = strings.TrimPrefix(u, "www.") + // Remove trailing slash when there's no path beyond it + if slashIdx := strings.IndexByte(u, '/'); slashIdx == -1 || slashIdx == len(u)-1 { + u = strings.TrimSuffix(u, "/") + } + return u +} + +// relativeTime returns a compact relative time label for a timestamp. +// Examples: "2m ago", "1h ago", "3d ago", "Jan 2". +func relativeTime(t time.Time) string { + now := time.Now() + d := now.Sub(t) + + switch { + case d < time.Minute: + return "now" + case d < time.Hour: + m := int(d.Minutes()) + if m < 2 { + return "1m ago" + } + return strconv.Itoa(m) + "m ago" + case d < hoursPerDay*time.Hour: + h := int(d.Hours()) + if h < 2 { + return "1h ago" + } + return strconv.Itoa(h) + "h ago" + case d < 7*hoursPerDay*time.Hour: + days := int(d.Hours() / hoursPerDay) + if days < 2 { + return "1d ago" + } + return strconv.Itoa(days) + "d ago" + default: + if t.Year() == now.Year() { + return t.Format("Jan 2") + } + return t.Format("Jan 2, 2006") + } +} + +// historyDisplayRowKind identifies the type of row rendered in the sidebar. +type historyDisplayRowKind int + +const ( + historyDisplayRowHeader historyDisplayRowKind = iota + historyDisplayRowEntry +) + +// historyDisplayRow is the explicit row model rendered by the history sidebar. +// It is the only source used for row lookup, selection, activation, and delete. +type historyDisplayRow struct { + Kind historyDisplayRowKind + Label string + Entry *entity.HistoryEntry + GroupIndex int +} + +func buildHistoryDisplayRows(groups []historyGroup) []historyDisplayRow { + if len(groups) == 0 { + return nil + } + rows := make([]historyDisplayRow, 0) + for gi, group := range groups { + rows = append(rows, historyDisplayRow{Kind: historyDisplayRowHeader, Label: group.Label, GroupIndex: gi}) + for _, entry := range group.Entries { + rows = append(rows, historyDisplayRow{Kind: historyDisplayRowEntry, Entry: entry, GroupIndex: gi}) + } + } + return rows +} + +// keyboardNavModel provides pure functions for keyboard navigation over the +// explicit display rows. It has no GTK dependencies and can be tested directly. +type keyboardNavModel struct { + rows []historyDisplayRow +} + +// newKeyboardNavModel creates a keyboardNavModel over day-grouped history. +func newKeyboardNavModel(groups []historyGroup) keyboardNavModel { + return newKeyboardNavModelFromRows(buildHistoryDisplayRows(groups)) +} + +func newKeyboardNavModelFromRows(rows []historyDisplayRow) keyboardNavModel { + return keyboardNavModel{rows: rows} +} + +// totalRows returns the number of explicit display rows. +func (m keyboardNavModel) totalRows() int { return len(m.rows) } + +// isSelectable returns true when the display row corresponds to an entry. +func (m keyboardNavModel) isSelectable(index int) bool { + return index >= 0 && index < len(m.rows) && m.rows[index].Kind == historyDisplayRowEntry && m.rows[index].Entry != nil +} + +func (m keyboardNavModel) firstSelectableIndex() int { + for i := 0; i < m.totalRows(); i++ { + if m.isSelectable(i) { + return i + } + } + return -1 +} + +func (m keyboardNavModel) lastSelectableIndex() int { + for i := m.totalRows() - 1; i >= 0; i-- { + if m.isSelectable(i) { + return i + } + } + return -1 +} + +func (m keyboardNavModel) nextSelectableIndex(from, dir int) int { + if dir != -1 && dir != +1 { + return -1 + } + for i := from + dir; i >= 0 && i < m.totalRows(); i += dir { + if m.isSelectable(i) { + return i + } + } + return -1 +} + +func (m keyboardNavModel) groupIndexAt(index int) int { + if index < 0 || index >= len(m.rows) { + return -1 + } + return m.rows[index].GroupIndex +} + +func (m keyboardNavModel) maxGroupIndex() int { + maxIndex := -1 + for _, row := range m.rows { + if row.GroupIndex > maxIndex { + maxIndex = row.GroupIndex + } + } + return maxIndex +} + +func (m keyboardNavModel) cumulativeOffsetAtGroup(gi int) int { + if gi < 0 { + return -1 + } + for i, row := range m.rows { + if row.GroupIndex == gi && row.Kind == historyDisplayRowHeader { + return i + } + } + return -1 +} + +func (m keyboardNavModel) firstEntryOfGroup(gi int) int { + for i, row := range m.rows { + if row.GroupIndex == gi && row.Kind == historyDisplayRowEntry && row.Entry != nil { + return i + } + } + return -1 +} + +func (m keyboardNavModel) previousDayBoundary(from int) int { + gi := m.groupIndexAt(from) + if gi <= 0 { + return -1 + } + return m.firstEntryOfGroup(gi - 1) +} + +func (m keyboardNavModel) nextDayBoundary(from int) int { + gi := m.groupIndexAt(from) + if gi < 0 || gi >= m.maxGroupIndex() { + return -1 + } + return m.firstEntryOfGroup(gi + 1) +} + +func (m keyboardNavModel) entryAt(index int) *entity.HistoryEntry { + if !m.isSelectable(index) { + return nil + } + return m.rows[index].Entry +} + +func (m keyboardNavModel) entryURLAt(index int) string { + e := m.entryAt(index) + if e == nil { + return "" + } + return e.URL +} + +func (m keyboardNavModel) entryCount() int { + n := 0 + for i := range m.rows { + if m.isSelectable(i) { + n++ + } + } + return n +} diff --git a/internal/ui/component/history_model_test.go b/internal/ui/component/history_model_test.go new file mode 100644 index 00000000..cb179669 --- /dev/null +++ b/internal/ui/component/history_model_test.go @@ -0,0 +1,741 @@ +package component + +import ( + "fmt" + "testing" + "time" + + "github.com/bnema/dumber/internal/domain/entity" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGroupHistoryByDay_Empty(t *testing.T) { + assert.Nil(t, groupHistoryByDay(nil)) + assert.Nil(t, groupHistoryByDay([]*entity.HistoryEntry{})) +} + +func TestGroupHistoryByDay_SingleEntry(t *testing.T) { + now := time.Now() + entries := []*entity.HistoryEntry{ + {ID: 1, URL: "https://example.com", Title: "Example", LastVisited: now}, + } + groups := groupHistoryByDay(entries) + require.Len(t, groups, 1) + assert.Equal(t, "Today", groups[0].Label) + assert.Len(t, groups[0].Entries, 1) +} + +func TestGroupHistoryByDay_TodayYesterdayOlder(t *testing.T) { + now := time.Now() + today := now + yesterday := now.AddDate(0, 0, -1) + older := now.AddDate(0, 0, -5) + + entries := []*entity.HistoryEntry{ + {ID: 1, URL: "https://older.com", Title: "Older", LastVisited: older}, + {ID: 2, URL: "https://yesterday.com", Title: "Yesterday", LastVisited: yesterday}, + {ID: 3, URL: "https://today.com", Title: "Today", LastVisited: today}, + } + groups := groupHistoryByDay(entries) + require.Len(t, groups, 3) + assert.Equal(t, "Today", groups[0].Label) + assert.Equal(t, "Yesterday", groups[1].Label) + assert.NotEmpty(t, groups[2].Label, "older entry should have a formatted date label") + assert.Len(t, groups[2].Entries, 1) +} + +func TestGroupHistoryByDay_SameDayEntriesGrouped(t *testing.T) { + now := time.Now() + entries := []*entity.HistoryEntry{ + {ID: 1, URL: "https://a.com", Title: "A", LastVisited: now}, + {ID: 2, URL: "https://b.com", Title: "B", LastVisited: now.Add(-time.Hour)}, + } + groups := groupHistoryByDay(entries) + require.Len(t, groups, 1) + assert.Equal(t, "Today", groups[0].Label) + assert.Len(t, groups[0].Entries, 2) +} + +func TestReadableURL_StripsProtocol(t *testing.T) { + assert.Equal(t, "example.com", readableURL("https://example.com")) + assert.Equal(t, "example.com/page", readableURL("http://example.com/page")) + assert.Equal(t, "example.com", readableURL("https://www.example.com")) +} + +func TestReadableURL_KeepsPath(t *testing.T) { + assert.Equal(t, "example.com/path/to/page", readableURL("https://example.com/path/to/page")) +} + +func TestRelativeTime_Now(t *testing.T) { + assert.Equal(t, "now", relativeTime(time.Now())) +} + +func TestRelativeTime_Minutes(t *testing.T) { + assert.Equal(t, "1m ago", relativeTime(time.Now().Add(-1*time.Minute))) + assert.Equal(t, "5m ago", relativeTime(time.Now().Add(-5*time.Minute))) +} + +func TestRelativeTime_Hours(t *testing.T) { + assert.Equal(t, "1h ago", relativeTime(time.Now().Add(-1*time.Hour))) + assert.Equal(t, "3h ago", relativeTime(time.Now().Add(-3*time.Hour))) +} + +func TestRelativeTime_Days(t *testing.T) { + assert.Equal(t, "1d ago", relativeTime(time.Now().Add(-25*time.Hour))) + assert.Equal(t, "3d ago", relativeTime(time.Now().Add(-72*time.Hour))) +} + +func TestGroupHistoryByDay_CrossYearDifferentLabels(t *testing.T) { + now := time.Now() + thisYear := now + lastYear := now.AddDate(-1, 0, 0) + twoYearsAgo := now.AddDate(-2, 0, 0) + + entries := []*entity.HistoryEntry{ + {ID: 1, URL: "https://two-years-ago.com", Title: "Two Years Ago", LastVisited: twoYearsAgo}, + {ID: 2, URL: "https://last-year.com", Title: "Last Year", LastVisited: lastYear}, + {ID: 3, URL: "https://this-year.com", Title: "This Year", LastVisited: thisYear}, + } + groups := groupHistoryByDay(entries) + require.Len(t, groups, 3) + assert.Equal(t, "Today", groups[0].Label) + assert.Equal(t, lastYear.Format(dayLabelOtherYearFormat), groups[1].Label) + assert.Equal(t, twoYearsAgo.Format(dayLabelOtherYearFormat), groups[2].Label) + assert.Len(t, groups[2].Entries, 1) +} + +func TestGroupHistoryByDay_MaintainsInputOrderWithinDay(t *testing.T) { + now := time.Now() + entries := []*entity.HistoryEntry{ + {ID: 1, URL: "https://first.com", Title: "First", LastVisited: now}, + {ID: 2, URL: "https://second.com", Title: "Second", LastVisited: now.Add(-30 * time.Minute)}, + {ID: 3, URL: "https://third.com", Title: "Third", LastVisited: now.Add(-2 * time.Hour)}, + } + groups := groupHistoryByDay(entries) + require.Len(t, groups, 1) + require.Len(t, groups[0].Entries, 3) + // Entries within the same day maintain input order + assert.Equal(t, "https://first.com", groups[0].Entries[0].URL) + assert.Equal(t, "https://second.com", groups[0].Entries[1].URL) + assert.Equal(t, "https://third.com", groups[0].Entries[2].URL) +} + +func TestGroupHistoryByDay_LeapYearBoundary(t *testing.T) { + // Two consecutive days in a leap year should produce separate groups. + // Use local noon on a known leap year to avoid UTC-to-local date + // rollover, and check that labels include the year when not the + // current year. + loc := time.Now().Location() + leapYear := 2024 + feb28 := time.Date(leapYear, time.February, 28, 12, 0, 0, 0, loc) + feb29 := time.Date(leapYear, time.February, 29, 12, 0, 0, 0, loc) + + entries := []*entity.HistoryEntry{ + {ID: 1, URL: "https://feb29.com", Title: "Feb 29", LastVisited: feb29}, + {ID: 2, URL: "https://feb28.com", Title: "Feb 28", LastVisited: feb28}, + } + groups := groupHistoryByDay(entries) + require.Len(t, groups, 2, "Feb 28 and Feb 29 should be in separate groups") + // Verify order: newest first + // Since 2024 is likely not the current year, labels include year + assert.Equal(t, feb29.Format("January 2, 2006"), groups[0].Label) + assert.Equal(t, feb28.Format("January 2, 2006"), groups[1].Label) +} + +func TestGroupHistoryByDay_SameLocalDay(t *testing.T) { + // Two entries on the same calendar day in local timezone should be grouped. + loc := time.Now().Location() + sameDay := time.Date(2026, time.June, 10, 12, 0, 0, 0, loc) + earlier := time.Date(2026, time.June, 10, 8, 0, 0, 0, loc) + + entries := []*entity.HistoryEntry{ + {ID: 1, URL: "https://later.com", Title: "Later", LastVisited: sameDay}, + {ID: 2, URL: "https://earlier.com", Title: "Earlier", LastVisited: earlier}, + } + groups := groupHistoryByDay(entries) + require.Len(t, groups, 1, "entries on same calendar day should be in one group") + assert.Contains(t, groups[0].Label, "June") +} + +func TestReadableURL_StripsFTPAndWWW(t *testing.T) { + assert.Equal(t, "example.com", readableURL("ftp://example.com")) + assert.Equal(t, "example.com", readableURL("http://www.example.com")) + assert.Equal(t, "example.com", readableURL("https://www.example.com/")) +} + +func TestReadableURL_NoProtocol(t *testing.T) { + assert.Equal(t, "example.com", readableURL("example.com")) +} + +func TestReadableURL_TrailingSlash(t *testing.T) { + assert.Equal(t, "example.com", readableURL("https://example.com/")) + assert.Equal(t, "example.com/path/", readableURL("https://example.com/path/")) +} + +func TestReadableURL_EmptyOrRoot(t *testing.T) { + assert.Empty(t, readableURL("")) + assert.Empty(t, readableURL("/")) +} + +func TestReadableURL_PreservesPort(t *testing.T) { + assert.Equal(t, "example.com:8080", readableURL("https://example.com:8080")) + assert.Equal(t, "example.com:3000/path", readableURL("https://example.com:3000/path")) +} + +func TestRelativeTime_BoundaryEdgeCases(t *testing.T) { + // Just under 1 minute → "now" + assert.Equal(t, "now", relativeTime(time.Now().Add(-30*time.Second))) + // 59 seconds → "now" + assert.Equal(t, "now", relativeTime(time.Now().Add(-59*time.Second))) + // 59 minutes 59 seconds → "59m ago" + assert.Equal(t, "59m ago", relativeTime(time.Now().Add(-59*time.Minute).Add(-59*time.Second))) + // 23 hours 59 minutes → "23h ago" + assert.Equal(t, "23h ago", relativeTime(time.Now().Add(-23*time.Hour).Add(-59*time.Minute))) + // 6 days 23 hours → "6d ago" + assert.Equal(t, "6d ago", relativeTime(time.Now().Add(-6*24*time.Hour).Add(-23*time.Hour))) + // 7 days → "Jul 8" format (changes by date, just verify not in "Xd ago") + result := relativeTime(time.Now().Add(-7 * 24 * time.Hour)) + assert.NotContains(t, result, "d ago", "7+ days should not use day format") +} + +func TestRelativeTime_Future(t *testing.T) { + future := time.Now().Add(time.Hour) + result := relativeTime(future) + // Current implementation returns "now" for negative durations. + // This is a known limitation; the test documents the behavior. + assert.Equal(t, "now", result, "future times currently reported as 'now' (known limitation)") +} + +func TestRelativeTime_DifferentYear(t *testing.T) { + // An entry from a previous year should show a date with month abbreviation + // and year when not current year. + lastYear := time.Now().AddDate(-1, -1, 0) + result := relativeTime(lastYear) + _, err := time.Parse("Jan 2, 2006", result) + assert.NoError(t, err, "old entry should use the Jan 2, 2006 layout, got %q", result) +} + +func TestDayLabelForKey_MultiYearAgo(t *testing.T) { + // Produce a label for a dayKey that is multiple years ago + now := time.Now() + todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) + // Two years ago, same month/day + past := now.AddDate(-2, 0, -1) + key := dayKey{past.Year(), past.Month(), past.Day()} + label := dayLabelForKey(key, todayStart, now) + assert.Contains(t, label, past.Format("2006"), "multi-year-old day label should include year") +} + +func TestDayLabelForKey_WithinCurrentYear(t *testing.T) { + now := time.Now() + todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) + // Earlier this month + past := now.AddDate(0, 0, -10) + key := dayKey{past.Year(), past.Month(), past.Day()} + label := dayLabelForKey(key, todayStart, now) + // Should be weekday format without year + assert.NotContains(t, label, "2006", "within-year label should not include year") + assert.Contains(t, label, past.Weekday().String(), "within-year label should include weekday") +} + +// ============================================================================= +// keyboardNavModel tests +// ============================================================================= + +func makeGroups(dayCounts ...int) []historyGroup { + now := time.Now() + groups := make([]historyGroup, len(dayCounts)) + for i, count := range dayCounts { + entries := make([]*entity.HistoryEntry, count) + for j := 0; j < count; j++ { + entries[j] = &entity.HistoryEntry{ + ID: int64(i*100 + j), + URL: fmt.Sprintf("https://entry-%d-%d.com", i, j), + Title: fmt.Sprintf("Entry %d-%d", i, j), + LastVisited: now.Add(-time.Duration(i) * 24 * time.Hour).Add(-time.Duration(j) * time.Minute), + } + } + groups[i] = historyGroup{ + Label: fmt.Sprintf("Day %d", i), + Entries: entries, + } + } + return groups +} + +func TestKeyboardNavModel_EmptyGroups(t *testing.T) { + m := newKeyboardNavModel(nil) + assert.Equal(t, 0, m.totalRows()) + assert.Equal(t, -1, m.firstSelectableIndex()) + assert.Equal(t, -1, m.lastSelectableIndex()) + assert.Nil(t, m.entryAt(0)) + assert.Empty(t, m.entryURLAt(0)) + + m = newKeyboardNavModel([]historyGroup{}) + assert.Equal(t, 0, m.totalRows()) +} + +func TestKeyboardNavModel_SingleGroup(t *testing.T) { + groups := makeGroups(3) // 1 header + 3 entries = 4 rows + m := newKeyboardNavModel(groups) + + assert.Equal(t, 4, m.totalRows()) + + // Row 0: header (not selectable) + assert.False(t, m.isSelectable(0)) + assert.Nil(t, m.entryAt(0)) + assert.Empty(t, m.entryURLAt(0)) + + // Rows 1-3: entries (selectable) + for i := 1; i <= 3; i++ { + assert.True(t, m.isSelectable(i), "row %d should be selectable", i) + assert.NotNil(t, m.entryAt(i)) + assert.Contains(t, m.entryURLAt(i), "entry-0") + } + + // first / last + assert.Equal(t, 1, m.firstSelectableIndex()) + assert.Equal(t, 3, m.lastSelectableIndex()) +} + +func TestKeyboardNavModel_MultipleGroups(t *testing.T) { + groups := makeGroups(2, 3) // group0: header+e0+e1, group1: header+e0+e1+e2 + m := newKeyboardNavModel(groups) + + // Layout: [H0, E0-0, E0-1, H1, E1-0, E1-1, E1-2] = 7 rows + assert.Equal(t, 7, m.totalRows()) + + // Selectable: rows 1,2,4,5,6 + assert.False(t, m.isSelectable(0)) // H0 + assert.True(t, m.isSelectable(1)) // E0-0 + assert.True(t, m.isSelectable(2)) // E0-1 + assert.False(t, m.isSelectable(3)) // H1 + assert.True(t, m.isSelectable(4)) // E1-0 + assert.True(t, m.isSelectable(5)) // E1-1 + assert.True(t, m.isSelectable(6)) // E1-2 + + assert.Equal(t, 1, m.firstSelectableIndex()) + assert.Equal(t, 6, m.lastSelectableIndex()) +} + +func TestKeyboardNavModel_NextPreviousSelectable(t *testing.T) { + groups := makeGroups(2, 2) // rows: H0(0), E0-0(1), E0-1(2), H1(3), E1-0(4), E1-1(5) + m := newKeyboardNavModel(groups) + + // Next from 0 (header) → 1 + assert.Equal(t, 1, m.nextSelectableIndex(0, +1)) + // Next from 1 → 2 + assert.Equal(t, 2, m.nextSelectableIndex(1, +1)) + // Next from 2 → 4 (skip header H1 at 3) + assert.Equal(t, 4, m.nextSelectableIndex(2, +1)) + // Next from 5 → -1 (end) + assert.Equal(t, -1, m.nextSelectableIndex(5, +1)) + + // Previous from 5 (E1-1) → 4 + assert.Equal(t, 4, m.nextSelectableIndex(5, -1)) + // Previous from 4 → 2 (skip header H1 at 3) + assert.Equal(t, 2, m.nextSelectableIndex(4, -1)) + // Previous from 1 → -1 (beginning, only header before) + assert.Equal(t, -1, m.nextSelectableIndex(1, -1)) + // Previous from 0 (header) → -1 + assert.Equal(t, -1, m.nextSelectableIndex(0, -1)) +} + +func TestKeyboardNavModel_InvalidDirection(t *testing.T) { + groups := makeGroups(2) + m := newKeyboardNavModel(groups) + assert.Equal(t, -1, m.nextSelectableIndex(0, 0)) + assert.Equal(t, -1, m.nextSelectableIndex(0, 2)) + assert.Equal(t, -1, m.nextSelectableIndex(0, -2)) +} + +func TestKeyboardNavModel_DayBoundaries(t *testing.T) { + groups := makeGroups(2, 1, 2) + // Layout: H0(0), E0-0(1), E0-1(2), H1(3), E1-0(4), H2(5), E2-0(6), E2-1(7) + m := newKeyboardNavModel(groups) + + // Previous day from E0-1(2) → E0-1 is in group 0, no previous day. + assert.Equal(t, -1, m.previousDayBoundary(2)) + + // Previous day from E1-0(4) → first entry in group 0 = row 1 + assert.Equal(t, 1, m.previousDayBoundary(4)) + + // Previous day from first group's entry (E0-0 at 1) → no previous day + assert.Equal(t, -1, m.previousDayBoundary(1)) + + // Next day from E0-0(1) → first entry in group 1 = row 4 + assert.Equal(t, 4, m.nextDayBoundary(1)) + + // Next day from E0-1(2) → row 4 + assert.Equal(t, 4, m.nextDayBoundary(2)) + + // Next day from E1-0(4) → first entry in group 2 = row 6 + assert.Equal(t, 6, m.nextDayBoundary(4)) + + // Next day from E2-0(6, group 2) → last group, no next day + assert.Equal(t, -1, m.nextDayBoundary(6)) + + // Next day from E2-1(7, last entry) → no next day + assert.Equal(t, -1, m.nextDayBoundary(7)) +} + +func TestKeyboardNavModel_EntryAtURL(t *testing.T) { + groups := makeGroups(1, 1) + m := newKeyboardNavModel(groups) + + e0 := m.entryAt(1) + require.NotNil(t, e0) + assert.Equal(t, "https://entry-0-0.com", e0.URL) + assert.Equal(t, "https://entry-0-0.com", m.entryURLAt(1)) + + e1 := m.entryAt(3) + require.NotNil(t, e1) + assert.Equal(t, "https://entry-1-0.com", e1.URL) + + // Header rows return nil / "" + assert.Nil(t, m.entryAt(0)) // H0 + assert.Empty(t, m.entryURLAt(0)) + assert.Nil(t, m.entryAt(2)) // H1 + assert.Empty(t, m.entryURLAt(2)) + + // Out of range + assert.Nil(t, m.entryAt(99)) + assert.Empty(t, m.entryURLAt(99)) + assert.Nil(t, m.entryAt(-1)) +} + +func TestKeyboardNavModel_EntryCount(t *testing.T) { + assert.Equal(t, 0, newKeyboardNavModel(nil).entryCount()) + assert.Equal(t, 0, newKeyboardNavModel([]historyGroup{}).entryCount()) + assert.Equal(t, 5, newKeyboardNavModel(makeGroups(2, 3)).entryCount()) + assert.Equal(t, 10, newKeyboardNavModel(makeGroups(3, 3, 4)).entryCount()) +} + +func TestBuildHistoryDisplayRows_IncludesHeadersAndEntries(t *testing.T) { + now := time.Now() + entryA := &entity.HistoryEntry{ID: 1, URL: "https://a.com", Title: "A", LastVisited: now} + entryB := &entity.HistoryEntry{ID: 2, URL: "https://b.com", Title: "B", LastVisited: now.Add(-time.Hour)} + groups := []historyGroup{ + {Label: "Today", Entries: []*entity.HistoryEntry{entryA, entryB}}, + {Label: "Yesterday", Entries: nil}, + } + + rows := buildHistoryDisplayRows(groups) + require.Len(t, rows, 4) + assert.Equal(t, historyDisplayRowHeader, rows[0].Kind) + assert.Equal(t, "Today", rows[0].Label) + assert.Equal(t, 0, rows[0].GroupIndex) + assert.Equal(t, historyDisplayRowEntry, rows[1].Kind) + assert.Same(t, entryA, rows[1].Entry) + assert.Equal(t, 0, rows[1].GroupIndex) + assert.Equal(t, historyDisplayRowEntry, rows[2].Kind) + assert.Same(t, entryB, rows[2].Entry) + assert.Equal(t, historyDisplayRowHeader, rows[3].Kind) + assert.Equal(t, "Yesterday", rows[3].Label) + assert.Equal(t, 1, rows[3].GroupIndex) +} + +func TestKeyboardNavModelFromRows_UsesExplicitDisplayRows(t *testing.T) { + now := time.Now() + entryA := &entity.HistoryEntry{ID: 1, URL: "https://a.com", Title: "A", LastVisited: now} + entryB := &entity.HistoryEntry{ID: 2, URL: "https://b.com", Title: "B", LastVisited: now.Add(-24 * time.Hour)} + rows := buildHistoryDisplayRows([]historyGroup{ + {Label: "Today", Entries: []*entity.HistoryEntry{entryA}}, + {Label: "Yesterday", Entries: []*entity.HistoryEntry{entryB}}, + }) + + m := newKeyboardNavModelFromRows(rows) + assert.Equal(t, 4, m.totalRows()) + assert.False(t, m.isSelectable(0)) + assert.True(t, m.isSelectable(1)) + assert.False(t, m.isSelectable(2)) + assert.True(t, m.isSelectable(3)) + assert.Same(t, entryA, m.entryAt(1)) + assert.Equal(t, entryB.URL, m.entryURLAt(3)) + assert.Equal(t, 3, m.nextDayBoundary(1)) + assert.Equal(t, 1, m.previousDayBoundary(3)) +} + +// ============================================================================= +// Search state transition tests +// ============================================================================= + +func TestTransitionSearchState_EmptyQuery(t *testing.T) { + next := transitionSearchState("", 0) + assert.Empty(t, next.Query) + assert.False(t, next.HasSearchDone) + assert.False(t, next.HasResults) + assert.Equal(t, 0, next.ResultCount) +} + +func TestTransitionSearchState_NewQuery(t *testing.T) { + next := transitionSearchState("example", 5) + assert.Equal(t, "example", next.Query) + assert.True(t, next.HasSearchDone) + assert.True(t, next.HasResults) + assert.Equal(t, 5, next.ResultCount) +} + +func TestTransitionSearchState_QueryWithNoResults(t *testing.T) { + next := transitionSearchState("nonexistent", 0) + assert.Equal(t, "nonexistent", next.Query) + assert.True(t, next.HasSearchDone) + assert.False(t, next.HasResults) + assert.Equal(t, 0, next.ResultCount) +} + +func TestTransitionSearchState_QueryToQuery(t *testing.T) { + next := transitionSearchState("new", 7) + assert.Equal(t, "new", next.Query) + assert.True(t, next.HasSearchDone) + assert.True(t, next.HasResults) + assert.Equal(t, 7, next.ResultCount) +} + +func TestTransitionSearchState_QueryToEmpty(t *testing.T) { + next := transitionSearchState("", 0) + assert.Empty(t, next.Query) + assert.False(t, next.HasSearchDone) + assert.False(t, next.HasResults) + assert.Equal(t, 0, next.ResultCount) +} + +// ============================================================================= +// Reload preservation tests +// ============================================================================= + +func TestApplyReloadState_WithoutQuery(t *testing.T) { + s := applyReloadState("") + assert.Empty(t, s.PreservedQuery) + assert.True(t, s.ResetBrowse) + assert.False(t, s.ClearSearch) +} + +func TestApplyReloadState_WithQuery(t *testing.T) { + s := applyReloadState("search-term") + assert.Equal(t, "search-term", s.PreservedQuery) + assert.False(t, s.ResetBrowse) + assert.True(t, s.ClearSearch) +} + +// ============================================================================= +// keyboardNavModel edge cases +// ============================================================================= + +func TestKeyboardNavModel_SingleGroupZeroEntries(t *testing.T) { + // A group with a header but zero entries. + groups := []historyGroup{ + {Label: "Today", Entries: []*entity.HistoryEntry{}}, + } + m := newKeyboardNavModel(groups) + + assert.Equal(t, 1, m.totalRows()) // header only + assert.False(t, m.isSelectable(0)) + assert.Equal(t, -1, m.firstSelectableIndex()) + assert.Equal(t, -1, m.lastSelectableIndex()) + assert.Nil(t, m.entryAt(0)) + assert.Equal(t, 0, m.entryCount()) +} + +func TestKeyboardNavModel_MixedEmptyAndNonEmptyGroups(t *testing.T) { + // Groups: [A(0 entries), B(2 entries), C(0 entries)] + // Layout: H0(0), H1(1), E1-0(2), E1-1(3), H2(4) + groups := []historyGroup{ + {Label: "EmptyA", Entries: []*entity.HistoryEntry{}}, + {Label: "HasTwo", Entries: []*entity.HistoryEntry{ + {ID: 1, URL: "https://b1.com", Title: "B1", LastVisited: time.Now()}, + {ID: 2, URL: "https://b2.com", Title: "B2", LastVisited: time.Now()}, + }}, + {Label: "EmptyC", Entries: []*entity.HistoryEntry{}}, + } + m := newKeyboardNavModel(groups) + + assert.Equal(t, 5, m.totalRows()) + // All headers: 0, 1, 4 are not selectable + assert.False(t, m.isSelectable(0)) + assert.False(t, m.isSelectable(1)) + assert.False(t, m.isSelectable(4)) + // Entries: 2, 3 are selectable + assert.True(t, m.isSelectable(2)) + assert.True(t, m.isSelectable(3)) + + assert.Equal(t, 2, m.firstSelectableIndex()) + assert.Equal(t, 3, m.lastSelectableIndex()) + assert.Equal(t, 2, m.entryCount()) + + // next/previous day boundary from group B (index 2) should cross + // empty group A header to group C header + // previousDayBoundary from entry in B (row 2) → group before B is A (no entries) → -1 + assert.Equal(t, -1, m.previousDayBoundary(2)) + // nextDayBoundary from entry in B (row 2) → group after B is C (no entries) → -1 + assert.Equal(t, -1, m.nextDayBoundary(2)) +} + +func TestKeyboardNavModel_GroupIndexAt(t *testing.T) { + groups := makeGroups(2, 0, 3) // A:2 entries, B:0 entries, C:3 entries + // Layout: H0(0), E0-0(1), E0-1(2), H1(3), H2(4), E2-0(5), E2-1(6), E2-2(7) + // Groups: A at rows 0-2 (header+2 entries), B at row 3 (header only), C at rows 4-7 (header+3 entries) + m := newKeyboardNavModel(groups) + + assert.Equal(t, 0, m.groupIndexAt(0), "row 0 should be in group 0 (A header)") + assert.Equal(t, 0, m.groupIndexAt(1), "row 1 should be in group 0 (A entry)") + assert.Equal(t, 1, m.groupIndexAt(3), "row 3 should be in group 1 (B header)") + assert.Equal(t, 2, m.groupIndexAt(4), "row 4 should be in group 2 (C header)") + assert.Equal(t, 2, m.groupIndexAt(7), "row 7 should be in group 2 (last C entry)") + assert.Equal(t, -1, m.groupIndexAt(99), "out of range should return -1") +} + +func TestKeyboardNavModel_CumulativeOffsetAtGroup(t *testing.T) { + groups := makeGroups(2, 0, 3) + m := newKeyboardNavModel(groups) + + assert.Equal(t, 0, m.cumulativeOffsetAtGroup(0), "group 0 offset should be 0") + // Group 0: 1 header + 2 entries = 3 rows + assert.Equal(t, 3, m.cumulativeOffsetAtGroup(1), "group 1 offset should skip group 0") + // Group 1: 1 header + 0 entries = 1 row, so group 2 offset = 3 + 1 = 4 + assert.Equal(t, 4, m.cumulativeOffsetAtGroup(2), "group 2 offset should skip groups 0 and 1") + assert.Equal(t, -1, m.cumulativeOffsetAtGroup(99), "out of range should return -1") +} + +func TestKeyboardNavModel_FirstEntryOfGroup(t *testing.T) { + groups := makeGroups(2, 0, 3) + m := newKeyboardNavModel(groups) + + // Group 0: offset=0, first entry = 1 + assert.Equal(t, 1, m.firstEntryOfGroup(0)) + // Group 1: offset=3, first entry = 4 (but group has 0 entries -> -1) + assert.Equal(t, -1, m.firstEntryOfGroup(1)) + // Group 2: offset=4, first entry = 5 + assert.Equal(t, 5, m.firstEntryOfGroup(2)) +} + +func TestKeyboardNavModel_NextPreviousSelectable_EmptyGroups(t *testing.T) { + // All groups have zero entries. + groups := []historyGroup{ + {Label: "A", Entries: []*entity.HistoryEntry{}}, + {Label: "B", Entries: []*entity.HistoryEntry{}}, + } + m := newKeyboardNavModel(groups) + + assert.Equal(t, -1, m.nextSelectableIndex(0, 1)) + assert.Equal(t, -1, m.nextSelectableIndex(0, -1)) +} + +func TestKeyboardNavModel_EntryCountWithEmptyGroups(t *testing.T) { + groups := []historyGroup{ + {Label: "A", Entries: []*entity.HistoryEntry{{ + ID: 1, URL: "https://a.com", Title: "A", LastVisited: time.Now(), + }}}, + {Label: "B", Entries: []*entity.HistoryEntry{}}, + {Label: "C", Entries: []*entity.HistoryEntry{{ + ID: 2, URL: "https://c.com", Title: "C", LastVisited: time.Now(), + }}}, + } + m := newKeyboardNavModel(groups) + assert.Equal(t, 2, m.entryCount(), "only non-empty groups' entries should count") +} + +// ============================================================================= +// Search generation / stale-result suppression (pure transition model) +// ============================================================================= + +// TestTransitionSearchState_StaleGenBehavior documents that the search +// generation counter (searchGen) is a production-side concern for stale +// result suppression. The transitionSearchState pure function only models +// query/result transitions; the actual generation guard is enforced by +// the GTK idle callback comparing gen vs hs.searchGen. +// +// We test the callback pattern here by simulating two sequential searches +// where a later result arrives after the gen has moved on. +func TestTransitionSearchState_SequentialSearches(t *testing.T) { + // Search 1: "foo" -> 5 results + next1 := transitionSearchState("foo", 5) + assert.Equal(t, "foo", next1.Query) + assert.True(t, next1.HasSearchDone) + assert.True(t, next1.HasResults) + assert.Equal(t, 5, next1.ResultCount) + + // Search 2: "foobar" -> 3 results (supersedes search 1) + next2 := transitionSearchState("foobar", 3) + assert.Equal(t, "foobar", next2.Query) + assert.True(t, next2.HasSearchDone) + assert.True(t, next2.HasResults) + assert.Equal(t, 3, next2.ResultCount) +} + +// TestTransitionSearchState_SearchThenClearThenReSearch verifies the +// transition from search -> empty -> new search produces correct state. +func TestTransitionSearchState_SearchThenClearThenReSearch(t *testing.T) { + // Search "term" -> 2 results + s := transitionSearchState("term", 2) + assert.Equal(t, "term", s.Query) + assert.True(t, s.HasSearchDone) + assert.True(t, s.HasResults) + assert.Equal(t, 2, s.ResultCount) + + // Clear: query becomes "" + s = transitionSearchState("", 0) + assert.Empty(t, s.Query) + assert.False(t, s.HasSearchDone) + assert.False(t, s.HasResults) + assert.Equal(t, 0, s.ResultCount) + + // Re-search: new term + s = transitionSearchState("other", 7) + assert.Equal(t, "other", s.Query) + assert.True(t, s.HasSearchDone) + assert.True(t, s.HasResults) + assert.Equal(t, 7, s.ResultCount) +} + +// TestApplyReloadState_EmptyAndNonEmpty verifies all reload preservation +// states are correctly modeled by the pure function. +func TestApplyReloadState_EmptyAndNonEmpty(t *testing.T) { + tt := []struct { + name string + query string + wantQuery string + wantReset bool + wantClear bool + }{ + {name: "empty query", query: "", wantQuery: "", wantReset: true, wantClear: false}, + {name: "non-empty", query: "search", wantQuery: "search", wantReset: false, wantClear: true}, + {name: "whitespace only", query: " ", wantQuery: " ", wantReset: false, wantClear: true}, + {name: "long query", query: "very long search query with many terms", wantQuery: "very long search query with many terms", wantReset: false, wantClear: true}, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + s := applyReloadState(tc.query) + assert.Equal(t, tc.wantQuery, s.PreservedQuery) + assert.Equal(t, tc.wantReset, s.ResetBrowse) + assert.Equal(t, tc.wantClear, s.ClearSearch) + }) + } +} + +// TestDayLabelForKey_DifferentYears verifies multi-year scenarios produce +// the expected label format. +func TestDayLabelForKey_DifferentYears(t *testing.T) { + now := time.Now() + todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) + + // Same year, not today/yesterday + future := now.AddDate(0, 0, -3) + key := dayKey{future.Year(), future.Month(), future.Day()} + label := dayLabelForKey(key, todayStart, now) + assert.NotContains(t, label, "2006", "within-current-year label should not contain year") + + // Previous year (now.Year()-1) + lastYear := now.AddDate(-1, 0, 0) + key = dayKey{lastYear.Year(), lastYear.Month(), lastYear.Day()} + label = dayLabelForKey(key, todayStart, now) + assert.Equal(t, lastYear.Format(dayLabelOtherYearFormat), label) + + // Multiple years ago + twoYearsAgo := now.AddDate(-2, 0, 0) + key = dayKey{twoYearsAgo.Year(), twoYearsAgo.Month(), twoYearsAgo.Day()} + label = dayLabelForKey(key, todayStart, now) + assert.Contains(t, label, twoYearsAgo.Format("2006"), "multi-year-old label should include year") +} diff --git a/internal/ui/component/history_sidebar.go b/internal/ui/component/history_sidebar.go new file mode 100644 index 00000000..fd94f07c --- /dev/null +++ b/internal/ui/component/history_sidebar.go @@ -0,0 +1,357 @@ +package component + +import ( + "context" + "sync" + + "github.com/bnema/puregotk/v4/glib" + "github.com/bnema/puregotk/v4/gtk" + + "github.com/bnema/dumber/internal/application/port" + "github.com/bnema/dumber/internal/domain/entity" + "github.com/bnema/dumber/internal/logging" + "github.com/bnema/dumber/internal/ui/layout" + "github.com/rs/zerolog" +) + +// sidebar constants +const ( + sidebarMinWidth = 280 + sidebarDefaultWidth = 320 + sidebarSearchDebounceMs = 150 + sidebarPageSize = 100 // entries fetched per page + sidebarSearchLimit = 100 // max FTS search results +) + +// HistorySidebarKeyboardAction enumerates the possible activation actions +// triggered by keyboard Enter variants. +type HistorySidebarKeyboardAction int + +const ( + // SidebarActionCloseOnActivate is the default activation path. + // The host currently keeps the sidebar visible for default activation. + SidebarActionCloseOnActivate HistorySidebarKeyboardAction = iota + // SidebarActionKeepOpenOnActivate navigates but leaves the sidebar visible. + SidebarActionKeepOpenOnActivate + // SidebarActionNewPaneOnActivate navigates by opening the URL in a new pane/split. + SidebarActionNewPaneOnActivate +) + +// HistorySidebar is a GTK sidebar component that displays browsing history +// grouped by day, with search/filter support and keyboard navigation. +// History is loaded page by page with background goroutines to avoid +// blocking the GTK main thread. +type HistorySidebar struct { + // GTK widgets + outerBox *gtk.Box + searchBox *gtk.Box + searchEntry *gtk.SearchEntry + scrolledWin *gtk.ScrolledWindow + listBox *gtk.ListBox + + // Dependencies + historyUC port.HistorySidebarHistory + onURL func(ctx context.Context, url string) + onOpenInNewPane func(ctx context.Context, url string) error + onNavigateKeepOpen func(ctx context.Context, url string) + onClose func() + + // Data + allEntries []*entity.HistoryEntry // Flat list, most-recent-first + groups []historyGroup // Current display groups (filtered) + displayRows []historyDisplayRow // Explicit rows currently rendered + + // Paging state (browse mode only) + totalLoaded int // how many entries have been fetched so far + hasMore bool + isLoading bool + loadGen uint64 // incremented each new browse load; used for stale-result protection + + // State + visible bool + currentQuery string + loadStarted bool + loadDone bool + destroyed bool + mu sync.RWMutex + logger zerolog.Logger + + // Search state + searchResults []*entity.HistoryEntry // non-nil when a search has completed + searchGen uint64 // incremented each search; used for stale-result protection + searchDone bool // true when the last search completed + searchErr error // last search error (if any) + + // Scroll/selection preservation + prevScrollValue float64 + prevSelectedURL string + + // Search debounce timer + debounceTimer uint + + // Retained callbacks + retainedCallbacks []interface{} + + // idleScheduler dispatches work onto the GTK main thread. + // Tests may override it to exercise scheduled callbacks deterministically. + idleScheduler func(glib.SourceFunc) + + // Context + ctx context.Context + cancel context.CancelFunc +} + +// HistorySidebarConfig holds configuration for creating a HistorySidebar. +type HistorySidebarConfig struct { + // HistoryUC provides history query and delete operations. + HistoryUC port.HistorySidebarHistory + + // OnNavigate is called when the user activates a history entry. + // The default Enter / click behavior keeps the sidebar open after navigating. + OnNavigate func(ctx context.Context, url string) error + + // OnOpenInNewPane is called when Shift+Enter activates a URL. + // Should open the URL in a new pane/split. If nil, Shift+Enter + // falls back to the default plain-Enter behavior. + OnOpenInNewPane func(ctx context.Context, url string) error + + // OnNavigateKeepOpen is called when Ctrl+Enter activates a URL. + // Unlike OnNavigate, the sidebar must NOT be closed after this + // callback returns. If nil, Ctrl+Enter falls back to OnNavigate. + OnNavigateKeepOpen func(ctx context.Context, url string) error + + // OnClose is called when the sidebar should close itself (e.g. Escape + // with empty search). The host should hide the sidebar and restore + // focus to the active content pane/webview. + OnClose func() +} + +// NewHistorySidebar creates a new HistorySidebar component. +func NewHistorySidebar(ctx context.Context, cfg HistorySidebarConfig) *HistorySidebar { + ctx, cancel := context.WithCancel(ctx) + log := logging.FromContext(ctx).With().Str("component", "history-sidebar").Logger() + + hs := &HistorySidebar{ + historyUC: cfg.HistoryUC, + onURL: func(callCtx context.Context, url string) { + if cfg.OnNavigate != nil { + if err := cfg.OnNavigate(callCtx, url); err != nil { + log.Error().Err(err).Str("url", url).Msg("history sidebar navigate failed") + } + } + }, + onOpenInNewPane: func(callCtx context.Context, url string) error { + if cfg.OnOpenInNewPane == nil { + // Fall back to regular navigation when no new-pane handler set + if cfg.OnNavigate != nil { + return cfg.OnNavigate(callCtx, url) + } + return nil + } + return cfg.OnOpenInNewPane(callCtx, url) + }, + onNavigateKeepOpen: func(callCtx context.Context, url string) { + if cfg.OnNavigateKeepOpen != nil { + if err := cfg.OnNavigateKeepOpen(callCtx, url); err != nil { + log.Error().Err(err).Str("url", url).Msg("history sidebar keep-open navigate failed") + } + } else if cfg.OnNavigate != nil { + // Fall back to the default navigate path when no dedicated + // keep-open callback is configured. + if err := cfg.OnNavigate(callCtx, url); err != nil { + log.Error().Err(err).Str("url", url).Msg("history sidebar keep-open fallback failed") + } + } + }, + onClose: func() { + if cfg.OnClose != nil { + cfg.OnClose() + } + }, + logger: log, + ctx: ctx, + cancel: cancel, + hasMore: cfg.HistoryUC != nil, + } + + if err := hs.createWidgets(); err != nil { + log.Error().Err(err).Msg("failed to create history sidebar widgets") + cancel() + return nil + } + + hs.setupSearchHandler() + hs.setupScrollLoadMore() + hs.setupKeyboardNavigation() + + log.Debug().Msg("history sidebar created") + + // Start loading history asynchronously (background goroutine) + hs.startLoadHistory() + + return hs +} + +// Destroy cleans up the sidebar and releases resources. +func (hs *HistorySidebar) Destroy() { + hs.mu.Lock() + if hs.destroyed { + hs.mu.Unlock() + return + } + hs.destroyed = true + timerID := hs.debounceTimer + hs.debounceTimer = 0 + hs.mu.Unlock() + + if hs.cancel != nil { + hs.cancel() + } + + if timerID != 0 { + glib.SourceRemove(timerID) + } +} + +// WidgetAsLayout returns the sidebar's outer widget for embedding. +func (hs *HistorySidebar) WidgetAsLayout(factory layout.WidgetFactory) layout.Widget { + if hs.outerBox == nil { + return nil + } + return factory.WrapWidget(&hs.outerBox.Widget) +} + +// Widget returns the raw GTK widget. +func (hs *HistorySidebar) Widget() *gtk.Widget { + if hs.outerBox == nil { + return nil + } + return &hs.outerBox.Widget +} + +// Show displays the sidebar and focuses the search entry. +// History data is refreshed asynchronously so Ctrl+H always shows +// current recent visits, not stale data from initialization. +func (hs *HistorySidebar) Show() { + hs.mu.Lock() + if hs.outerBox == nil || hs.destroyed { + hs.mu.Unlock() + return + } + + hs.outerBox.SetVisible(true) + hs.visible = true + hs.mu.Unlock() + + // Schedule a background reload so the sidebar shows fresh data + // when it becomes visible, not stale data captured at init time. + reloadCb := glib.SourceFunc(func(uintptr) bool { + hs.mu.RLock() + destroyed := hs.destroyed + hs.mu.RUnlock() + if destroyed { + return false + } + hs.Reload() + return false + }) + hs.scheduleIdle(reloadCb) + + // Focus search entry via idle callback to ensure layout is stable. + cb := glib.SourceFunc(func(uintptr) bool { + hs.mu.RLock() + destroyed := hs.destroyed + entry := hs.searchEntry + hs.mu.RUnlock() + if destroyed || entry == nil { + return false + } + entry.GrabFocus() + return false + }) + hs.scheduleIdle(cb) +} + +// Hide hides the sidebar. +func (hs *HistorySidebar) Hide() { + hs.mu.Lock() + defer hs.mu.Unlock() + + if hs.outerBox == nil || hs.destroyed { + return + } + + hs.outerBox.SetVisible(false) + hs.visible = false +} + +// IsVisible returns whether the sidebar is visible. +func (hs *HistorySidebar) IsVisible() bool { + hs.mu.RLock() + defer hs.mu.RUnlock() + return hs.visible +} + +// Reload triggers a fresh load of history data. Preserves the current search +// query, scroll position, and selection. +func (hs *HistorySidebar) Reload() { + hs.mu.Lock() + if hs.destroyed { + hs.mu.Unlock() + return + } + hs.preserveScrollAndSelection() + savedQuery := hs.currentQuery + + // Reset all data state but keep the query preserved so the search entry + // text and internal state remain consistent. + hs.loadDone = false + hs.loadStarted = false + hs.totalLoaded = 0 + hs.hasMore = hs.historyUC != nil + hs.isLoading = false + hs.allEntries = nil + hs.setDisplayGroupsLocked(nil) + hs.searchResults = nil + hs.searchDone = false + hs.searchErr = nil + + if savedQuery == "" { + hs.currentQuery = "" + hs.mu.Unlock() + hs.startLoadHistory() + } else { + hs.currentQuery = savedQuery + hs.searchGen++ // Invalidate any in-flight search + gen := hs.searchGen + hs.mu.Unlock() + hs.scheduleClearList() + hs.doFTSearch(savedQuery, gen) + } +} + +// SetSearchQuery externally sets the search text and triggers filtering. +func (hs *HistorySidebar) SetSearchQuery(query string) { + hs.mu.RLock() + entry := hs.searchEntry + hs.mu.RUnlock() + if entry != nil { + entry.SetText(query) + } +} + +// ClearSearch clears the search entry text. +func (hs *HistorySidebar) ClearSearch() { + hs.SetSearchQuery("") +} + +// ============================================================================= +// Helpers +// ============================================================================= + +func safeSidebarString(s, fallback string) string { + if s == "" { + return fallback + } + return s +} diff --git a/internal/ui/component/history_sidebar_keyboard.go b/internal/ui/component/history_sidebar_keyboard.go new file mode 100644 index 00000000..6c03c4dd --- /dev/null +++ b/internal/ui/component/history_sidebar_keyboard.go @@ -0,0 +1,598 @@ +package component + +import ( + "github.com/bnema/puregotk/v4/gdk" + "github.com/bnema/puregotk/v4/glib" + "github.com/bnema/puregotk/v4/gtk" + + "github.com/bnema/dumber/internal/domain/entity" +) + +// ============================================================================= +// Keyboard navigation +// ============================================================================= + +func (hs *HistorySidebar) setupKeyboardNavigation() { + if hs.outerBox == nil { + return + } + + // The ListBox already supports Up/Down arrow navigation natively. + // We add a PhaseCapture key controller on the outerBox to intercept + // keys before the ListBox processes them. + keyController := gtk.NewEventControllerKey() + if keyController == nil { + return + } + keyController.SetPropagationPhase(gtk.PhaseCaptureValue) + + keyPressedCb := func(_ gtk.EventControllerKey, keyval uint, _ uint, state gdk.ModifierType) bool { + switch keyval { + // --- Escape: clear search or close sidebar --- + case uint(gdk.KEY_Escape): + if hs.searchEntry != nil && hs.searchEntry.GetText() != "" { + hs.searchEntry.SetText("") + return true + } + // Close sidebar explicitly and restore focus + hs.closeSidebar() + return true + + // --- Enter variants --- + case uint(gdk.KEY_Return), uint(gdk.KEY_KP_Enter): + return hs.handleEnterKey(state) + + // --- Delete: remove selected entry --- + case uint(gdk.KEY_Delete), uint(gdk.KEY_KP_Delete): + if hs.searchEntry != nil && hs.searchEntry.GetText() != "" { + return false + } + return hs.handleDeleteKey() + + // --- PageUp / PageDown: scroll by page --- + case uint(gdk.KEY_Page_Up): + hs.scrollByPage(-1) + return true + case uint(gdk.KEY_Page_Down): + hs.scrollByPage(1) + return true + + // --- Home / End: jump to first/last selectable row --- + case uint(gdk.KEY_Home): + hs.jumpToFirstSelectable() + return true + case uint(gdk.KEY_End): + hs.jumpToLastSelectable() + return true + + // --- Up / Down: previous / next selectable row --- + // Ctrl+Up / Ctrl+Down: previous / next day group jump --- + case uint(gdk.KEY_Up): + if state&gdk.ControlMaskValue != 0 { + hs.jumpToPreviousDay() + return true + } + hs.selectPreviousRow() + return true + case uint(gdk.KEY_Down): + if state&gdk.ControlMaskValue != 0 { + hs.jumpToNextDay() + return true + } + hs.selectNextRow() + return true + } + + return false + } + + hs.retainedCallbacks = append(hs.retainedCallbacks, keyPressedCb) + keyController.ConnectKeyPressed(&keyPressedCb) + + hs.outerBox.AddController(&keyController.EventController) +} + +// handleEnterKey processes Enter, Ctrl+Enter, and Shift+Enter on a selected row. +// Returns true if the key was consumed. +func (hs *HistorySidebar) handleEnterKey(state gdk.ModifierType) bool { + // Determine activation mode from modifiers + var action HistorySidebarKeyboardAction + + switch { + case state&gdk.ControlMaskValue != 0: + // Ctrl+Enter: navigate but keep sidebar open + action = SidebarActionKeepOpenOnActivate + case state&gdk.ShiftMaskValue != 0: + // Shift+Enter: navigate in new pane + action = SidebarActionNewPaneOnActivate + default: + // Plain Enter: navigate using the default activation behavior. + action = SidebarActionCloseOnActivate + } + + // Find the selected row and its URL + row := hs.listBox.GetSelectedRow() + if row == nil || !row.GetSelectable() { + return false + } + + hs.mu.RLock() + url := hs.entryURLAtIndex(row.GetIndex()) + hs.mu.RUnlock() + if url == "" { + return false + } + + // Schedule activation on the GTK main thread + switch action { + case SidebarActionKeepOpenOnActivate: + hs.navigateWithoutClosing(url) + case SidebarActionNewPaneOnActivate: + hs.navigateToNewPane(url) + default: + hs.navigateToURL(url) + } + + // Consume the key event + return true +} + +// handleDeleteKey removes the selected history entry and updates the selection. +// Returns true if the key was consumed. +func (hs *HistorySidebar) handleDeleteKey() bool { + row := hs.listBox.GetSelectedRow() + if row == nil || !row.GetSelectable() { + return false + } + if hs.historyUC == nil { + return false + } + + idx := row.GetIndex() + + hs.mu.RLock() + url := hs.entryURLAtIndex(idx) + entryID := hs.findEntryIDByIndex(idx) + nextSelectedURL := "" + if nextRow := hs.findNextSelectableAfter(idx); nextRow != -1 { + nextSelectedURL = hs.entryURLAtIndex(nextRow) + } + hs.mu.RUnlock() + + if url == "" || entryID <= 0 { + return false + } + + go func() { + if err := hs.historyUC.Delete(hs.ctx, entryID); err != nil { + hs.logger.Error().Err(err).Int64("entry_id", entryID).Msg("failed to delete history entry") + return + } + + cb := glib.SourceFunc(func(uintptr) bool { + hs.mu.Lock() + if hs.destroyed { + hs.mu.Unlock() + return false + } + hs.applyDeletedEntryLocked(entryID, nextSelectedURL) + hs.mu.Unlock() + + hs.rebuildList() + return false + }) + hs.scheduleIdle(cb) + }() + + return true +} + +// applyDeletedEntryLocked updates local sidebar state after a successful +// history delete. Must be called with hs.mu write lock held. +func (hs *HistorySidebar) applyDeletedEntryLocked(entryID int64, nextSelectedURL string) { + hs.preserveScrollAndSelection() + hs.prevSelectedURL = nextSelectedURL + hs.searchGen++ + hs.loadGen++ + hs.isLoading = false + hs.loadStarted = false + hs.removeFromAllEntries(entryID) + hs.totalLoaded = len(hs.allEntries) + hs.removeFromSearchResults(entryID) + hs.rebuildLocalGroups() +} + +// findEntryIDByIndex returns the entry ID for the linear ListBox index. +// Must be called with hs.mu read lock held. +func (hs *HistorySidebar) findEntryIDByIndex(index int) int64 { + entry := newKeyboardNavModelFromRows(hs.displayRows).entryAt(index) + if entry == nil { + return 0 + } + return entry.ID +} + +// rebuildLocalGroups rebuilds hs.groups from the current allEntries and query. +// Must be called with hs.mu write lock held. +func (hs *HistorySidebar) rebuildLocalGroups() { + if hs.currentQuery == "" { + hs.setDisplayGroupsLocked(groupHistoryByDay(hs.allEntries)) + } else if hs.searchResults != nil { + hs.setDisplayGroupsLocked(groupHistoryByDay(hs.searchResults)) + } else { + // For search mode, the search results are handled by doFTSearch. + // Removing an entry while in search mode would need a re-search. + // Fall back to grouping searchResults if they exist. + hs.setDisplayGroupsLocked(nil) + } +} + +// removeFromAllEntries removes all history entries matching the given URL or ID +// from hs.allEntries. Must be called with hs.mu write lock held. +func (hs *HistorySidebar) removeFromAllEntries(id int64) { + filtered := make([]*entity.HistoryEntry, 0, len(hs.allEntries)) + for _, e := range hs.allEntries { + if e != nil && e.ID == id { + continue + } + filtered = append(filtered, e) + } + hs.allEntries = filtered +} + +// removeFromSearchResults removes all history entries matching the given ID +// from hs.searchResults. Must be called with hs.mu write lock held. +func (hs *HistorySidebar) removeFromSearchResults(id int64) { + if hs.searchResults == nil { + return + } + filtered := make([]*entity.HistoryEntry, 0, len(hs.searchResults)) + for _, e := range hs.searchResults { + if e != nil && e.ID == id { + continue + } + filtered = append(filtered, e) + } + hs.searchResults = filtered +} + +// findNextSelectableAfter returns the ListBox index of the next selectable +// row after the given index, falling back to the previous selectable row. +// Must be called with hs.mu read lock held. +func (hs *HistorySidebar) findNextSelectableAfter(idx int) int { + model := newKeyboardNavModelFromRows(hs.displayRows) + if next := model.nextSelectableIndex(idx, +1); next != -1 { + return next + } + if prev := model.nextSelectableIndex(idx, -1); prev != -1 { + return prev + } + return -1 +} + +// scrollByPage scrolls the list by one page up or down, +// keeping the selection visible. +func (hs *HistorySidebar) scrollByPage(direction int) { + if hs.scrolledWin == nil { + return + } + vadj := hs.scrolledWin.GetVadjustment() + if vadj == nil { + return + } + pageSize := vadj.GetPageSize() + current := vadj.GetValue() + newVal := current + float64(direction)*(pageSize*0.9) // 90% page for overlap + if newVal < 0 { + newVal = 0 + } + upper := vadj.GetUpper() - pageSize + if newVal > upper { + newVal = upper + } + if newVal >= 0 { + vadj.SetValue(newVal) + } +} + +// jumpToPreviousDay selects the first entry in the previous day group +// relative to the currently selected row. +func (hs *HistorySidebar) jumpToPreviousDay() { + currentIdx := -1 + if row := hs.listBox.GetSelectedRow(); row != nil { + currentIdx = row.GetIndex() + } + + hs.mu.RLock() + targetIdx := newKeyboardNavModelFromRows(hs.displayRows).previousDayBoundary(currentIdx) + hs.mu.RUnlock() + if targetIdx == -1 { + hs.jumpToFirstSelectable() + return + } + if row := hs.listBox.GetRowAtIndex(targetIdx); row != nil && row.GetSelectable() { + hs.listBox.SelectRow(row) + hs.scrollRowIntoView(targetIdx) + return + } + hs.jumpToFirstSelectable() +} + +// jumpToNextDay selects the first entry in the next day group +// relative to the currently selected row. +func (hs *HistorySidebar) jumpToNextDay() { + currentIdx := -1 + if row := hs.listBox.GetSelectedRow(); row != nil { + currentIdx = row.GetIndex() + } + + hs.mu.RLock() + targetIdx := newKeyboardNavModelFromRows(hs.displayRows).nextDayBoundary(currentIdx) + hs.mu.RUnlock() + if targetIdx == -1 { + hs.jumpToLastSelectable() + return + } + if row := hs.listBox.GetRowAtIndex(targetIdx); row != nil && row.GetSelectable() { + hs.listBox.SelectRow(row) + hs.scrollRowIntoView(targetIdx) + return + } + hs.jumpToLastSelectable() +} + +// scrollRowIntoView scrolls the scrolled window to ensure the row at +// the given ListBox index is visible. +func (hs *HistorySidebar) scrollRowIntoView(index int) { + hs.ensureRowVisible(index) +} + +// jumpToFirstSelectable selects the first selectable row in the list. +func (hs *HistorySidebar) jumpToFirstSelectable() { + for i := 0; ; i++ { + row := hs.listBox.GetRowAtIndex(i) + if row == nil { + break + } + if row.GetSelectable() { + hs.listBox.SelectRow(row) + hs.ensureRowVisible(i) + return + } + } +} + +// jumpToLastSelectable selects the last selectable row in the list. +func (hs *HistorySidebar) jumpToLastSelectable() { + // Walk backwards through the rows + maxIdx := 0 + var lastRow *gtk.ListBoxRow + for i := 0; ; i++ { + row := hs.listBox.GetRowAtIndex(i) + if row == nil { + break + } + maxIdx = i + if row.GetSelectable() { + lastRow = row + } + } + // GetRowAtIndex returns nil once the index is past the end of the list, + // so this scan terminates naturally at the first missing row. + // If we found a selectable row, try it. Otherwise fall back to last row. + if lastRow != nil { + hs.listBox.SelectRow(lastRow) + hs.ensureRowVisible(lastRow.GetIndex()) + return + } + // Fallback: last row regardless of selectability + if maxIdx > 0 { + if row := hs.listBox.GetRowAtIndex(maxIdx); row != nil { + hs.listBox.SelectRow(row) + hs.ensureRowVisible(maxIdx) + } + } +} + +// ============================================================================= +// Up/Down row selection (with search entry focus preserved) +// ============================================================================= + +// selectPreviousRow selects the previous selectable row (skipping headers). +// Focus remains in the search entry; the ListBox selection is updated +// programmatically and scrolled into view. +func (hs *HistorySidebar) selectPreviousRow() { + hs.selectAdjacentRow(-1) +} + +// selectNextRow selects the next selectable row (skipping headers). +// Focus remains in the search entry; the ListBox selection is updated +// programmatically and scrolled into view. +func (hs *HistorySidebar) selectNextRow() { + hs.selectAdjacentRow(1) +} + +// selectAdjacentRow moves selection by direction (-1 or +1) to the next +// selectable row, skipping non-selectable (header) rows. If nothing is +// currently selected, it selects the first (down) or last (up) selectable +// row. Focus remains in the search entry. +func (hs *HistorySidebar) selectAdjacentRow(direction int) { + if hs.listBox == nil { + return + } + + current := -1 + if row := hs.listBox.GetSelectedRow(); row != nil { + current = row.GetIndex() + } + + hs.mu.RLock() + model := newKeyboardNavModelFromRows(hs.displayRows) + var target int + if current < 0 { + if direction > 0 { + target = model.firstSelectableIndex() + } else { + target = model.lastSelectableIndex() + } + } else { + target = model.nextSelectableIndex(current, direction) + } + hs.mu.RUnlock() + + if target == -1 { + return + } + if row := hs.listBox.GetRowAtIndex(target); row != nil && row.GetSelectable() { + hs.listBox.SelectRow(row) + hs.ensureRowVisible(target) + } +} + +// ensureRowVisible adjusts the scrolled window so the row at index is +// visible, WITHOUT calling GrabFocus (preserving search entry focus). +// The Y position is computed by summing the allocated heights of all +// preceding rows. +func (hs *HistorySidebar) ensureRowVisible(index int) { + if hs.scrolledWin == nil || hs.listBox == nil { + return + } + vadj := hs.scrolledWin.GetVadjustment() + if vadj == nil { + return + } + row := hs.listBox.GetRowAtIndex(index) + if row == nil { + return + } + + // Sum allocated heights of all preceding rows to estimate Y position. + var yPos int + for i := 0; i < index; i++ { + r := hs.listBox.GetRowAtIndex(i) + if r == nil { + continue + } + yPos += r.GetAllocatedHeight() + } + + rowHeight := row.GetAllocatedHeight() + if rowHeight <= 0 { + return + } + + pageSize := vadj.GetPageSize() + current := vadj.GetValue() + rowTop := float64(yPos) + rowBottom := rowTop + float64(rowHeight) + + if rowTop < current { + // Row is above the visible area — scroll up. + vadj.SetValue(rowTop) + } else if rowBottom > current+pageSize { + // Row is below the visible area — scroll down. + vadj.SetValue(rowBottom - pageSize) + } +} + +// ============================================================================= +// Row activation (Enter / click) +// ============================================================================= + +func (hs *HistorySidebar) onRowActivated(row *gtk.ListBoxRow) { + if row == nil || !row.GetSelectable() { + return + } + + hs.mu.RLock() + // Allow activation when browse is loaded or when search results are available. + // This prevents a race where the user searches before the initial browse page + // finishes loading — browse may be left unloaded, but search results should + // still be activatable. + hasSearchResults := hs.searchDone && hs.searchResults != nil + if (!hs.loadDone && !hasSearchResults) || len(hs.groups) == 0 { + hs.mu.RUnlock() + return + } + entry := newKeyboardNavModelFromRows(hs.displayRows).entryAt(row.GetIndex()) + hs.mu.RUnlock() + if entry == nil || entry.URL == "" { + return + } + + hs.navigateToURL(entry.URL) +} + +func (hs *HistorySidebar) navigateToURL(url string) { + if hs.onURL == nil || url == "" { + return + } + + navigateCb := glib.SourceFunc(func(uintptr) bool { + hs.mu.RLock() + destroyed := hs.destroyed + hs.mu.RUnlock() + if destroyed { + return false + } + hs.onURL(hs.ctx, url) + return false + }) + hs.scheduleIdle(navigateCb) +} + +// navigateWithoutClosing navigates to the URL but does NOT close the sidebar. +// Used by Ctrl+Enter activation. +func (hs *HistorySidebar) navigateWithoutClosing(url string) { + if hs.onNavigateKeepOpen == nil || url == "" { + return + } + hs.doNavigateWithoutClose(url) +} + +// doNavigateWithoutClose schedules navigation without closing the sidebar. +// Uses the dedicated OnNavigateKeepOpen path so hosts can override the +// default activation behavior when they need a distinct keep-open action. +func (hs *HistorySidebar) doNavigateWithoutClose(url string) { + navigateCb := glib.SourceFunc(func(uintptr) bool { + hs.mu.RLock() + destroyed := hs.destroyed + hs.mu.RUnlock() + if destroyed { + return false + } + hs.onNavigateKeepOpen(hs.ctx, url) + return false + }) + hs.scheduleIdle(navigateCb) +} + +// navigateToNewPane navigates to the URL by opening it in a new pane. +// The sidebar stays open. Used by Shift+Enter activation. +func (hs *HistorySidebar) navigateToNewPane(url string) { + if hs.onOpenInNewPane == nil || url == "" { + return + } + + navigateCb := glib.SourceFunc(func(uintptr) bool { + hs.mu.RLock() + destroyed := hs.destroyed + hs.mu.RUnlock() + if destroyed { + return false + } + if err := hs.onOpenInNewPane(hs.ctx, url); err != nil { + hs.logger.Error().Err(err).Str("url", url).Msg("history sidebar new-pane navigation failed") + } + return false + }) + hs.scheduleIdle(navigateCb) +} + +// closeSidebar calls the configured OnClose callback to tell the host to +// hide the sidebar and restore focus to the active content pane/webview. +func (hs *HistorySidebar) closeSidebar() { + if hs.onClose != nil { + hs.onClose() + } +} diff --git a/internal/ui/component/history_sidebar_loading_search.go b/internal/ui/component/history_sidebar_loading_search.go new file mode 100644 index 00000000..03ce40c1 --- /dev/null +++ b/internal/ui/component/history_sidebar_loading_search.go @@ -0,0 +1,442 @@ +package component + +import ( + "github.com/bnema/puregotk/v4/glib" + "github.com/bnema/puregotk/v4/gtk" + + "github.com/bnema/dumber/internal/application/dto" + "github.com/bnema/dumber/internal/domain/entity" +) + +// ============================================================================= +// Data loading — background goroutine with paging +// ============================================================================= + +func (hs *HistorySidebar) startLoadHistory() { + hs.mu.Lock() + hs.loadGen++ + gen := hs.loadGen + hs.loadStarted = true + hs.isLoading = true + hs.mu.Unlock() + + // Fetch first page in a background goroutine + go hs.fetchPage(0, gen) +} + +// fetchPage fetches a page of history entries in a background goroutine +// and schedules the UI update on the GTK main thread. +func (hs *HistorySidebar) fetchPage(offset int, gen uint64) { + hs.mu.RLock() + uc := hs.historyUC + ctx := hs.ctx + hs.mu.RUnlock() + + if uc == nil || ctx == nil { + // No provider; show empty state + cb := glib.SourceFunc(func(uintptr) bool { + hs.mu.Lock() + if hs.destroyed { + hs.mu.Unlock() + return false + } + hs.loadStarted = false + hs.isLoading = false + hs.loadDone = true + hs.hasMore = false + hs.mu.Unlock() + hs.scheduleRebuild() + return false + }) + hs.scheduleIdle(cb) + return + } + + entries, err := uc.GetRecent(ctx, sidebarPageSize, offset) + if err != nil { + hs.logger.Error().Err(err).Int("offset", offset).Msg("failed to load history page") + } + + if entries == nil { + entries = []*entity.HistoryEntry{} + } + + hasMore := len(entries) >= sidebarPageSize + + hs.mu.Lock() + + // If a newer load was started since this fetch began, drop stale results. + // Must NOT mutate isLoading/loadStarted — they belong to the current + // generation set by startLoadHistory or LoadMore. + if gen != hs.loadGen { + hs.mu.Unlock() + return + } + + // If search is active, don't update browse state with stale page data + // and don't overwrite search results. + if hs.currentQuery != "" { + hs.isLoading = false + hs.loadStarted = false + hs.mu.Unlock() + return + } + + hs.totalLoaded = offset + len(entries) + hs.hasMore = hasMore + hs.isLoading = false + hs.loadStarted = false + hs.loadDone = true + + if offset == 0 { + // First page: replace all entries + hs.allEntries = entries + } else { + // Subsequent page: append + hs.allEntries = append(hs.allEntries, entries...) + } + + // Group for display + hs.setDisplayGroupsLocked(groupHistoryByDay(hs.allEntries)) + hs.mu.Unlock() + + // Schedule UI rebuild on GTK main thread + cb := glib.SourceFunc(func(uintptr) bool { + hs.rebuildList() + return false + }) + hs.scheduleIdle(cb) +} + +// LoadMore fetches the next page and appends it to the existing entries. +func (hs *HistorySidebar) LoadMore() { + hs.mu.Lock() + if hs.isLoading || !hs.hasMore || hs.destroyed || hs.currentQuery != "" { + hs.mu.Unlock() + return + } + hs.isLoading = true + offset := hs.totalLoaded + gen := hs.loadGen + hs.mu.Unlock() + + hs.logger.Debug().Int("offset", offset).Msg("loading more history entries") + go hs.fetchPage(offset, gen) +} + +// ============================================================================= +// Scroll-aware load-more: detects when the user reaches the bottom +// ============================================================================= + +func (hs *HistorySidebar) setupScrollLoadMore() { + if hs.scrolledWin == nil { + return + } + + vadj := hs.scrolledWin.GetVadjustment() + if vadj == nil { + return + } + + changedCb := func(_ gtk.Adjustment) { + hs.mu.RLock() + if hs.destroyed || !hs.hasMore || hs.isLoading { + hs.mu.RUnlock() + return + } + value := vadj.GetValue() + upper := vadj.GetUpper() + pageSize := vadj.GetPageSize() + hs.mu.RUnlock() + + // Trigger load-more when within 200px of the bottom + if pageSize > 0 && value+pageSize >= upper-200.0 { + hs.LoadMore() + } + } + hs.retainedCallbacks = append(hs.retainedCallbacks, changedCb) + vadj.ConnectValueChanged(&changedCb) +} + +// ============================================================================= +// Scroll/selection preservation +// ============================================================================= + +// preserveScrollAndSelection saves the current scroll position and selected row +// URL before a rebuild. Must be called with hs.mu write lock held. +func (hs *HistorySidebar) preserveScrollAndSelection() { + hs.prevScrollValue = 0 + hs.prevSelectedURL = "" + + if hs.scrolledWin != nil { + if vadj := hs.scrolledWin.GetVadjustment(); vadj != nil { + hs.prevScrollValue = vadj.GetValue() + } + } + if hs.listBox != nil { + if selected := hs.listBox.GetSelectedRow(); selected != nil { + if url := hs.entryURLAtIndex(selected.GetIndex()); url != "" { + hs.prevSelectedURL = url + } + } + } +} + +// restoreScrollAndSelection restores the previously saved scroll position and +// selection after a rebuild. Called on the GTK main thread. +func (hs *HistorySidebar) restoreScrollAndSelection() { + // Restore selection first (changes scroll position) + if hs.prevSelectedURL != "" { + hs.selectRowByURL(hs.prevSelectedURL) + } + + // Then restore scroll position if we have one + if hs.prevScrollValue > 0 && hs.scrolledWin != nil { + if vadj := hs.scrolledWin.GetVadjustment(); vadj != nil { + maxVal := vadj.GetUpper() - vadj.GetPageSize() + if hs.prevScrollValue > maxVal { + hs.prevScrollValue = maxVal + } + vadj.SetValue(hs.prevScrollValue) + } + } + + hs.prevScrollValue = 0 + hs.prevSelectedURL = "" +} + +// getRowURL extracts the URL stored in a list box row. +func (hs *HistorySidebar) getRowURL(row *gtk.ListBoxRow) string { + if row == nil || !row.GetSelectable() { + return "" + } + child := row.GetChild() + if child == nil { + return "" + } + + // The child is the vertical box. Walk children to find our stored URL. + // We store the URL directly on the row as data. + // Actually, let's use a simpler approach: walk the list box to find the entry. + idx := row.GetIndex() + hs.mu.RLock() + defer hs.mu.RUnlock() + + return hs.entryURLAtIndex(idx) +} + +// entryURLAtIndex returns the URL of the history entry at the given +// linear list index (including group headers which return ""). +func (hs *HistorySidebar) entryURLAtIndex(index int) string { + return newKeyboardNavModelFromRows(hs.displayRows).entryURLAt(index) +} + +// selectRowByURL finds and selects a row whose URL matches. +func (hs *HistorySidebar) selectRowByURL(url string) { + if url == "" || hs.listBox == nil { + return + } + for i := 0; ; i++ { + row := hs.listBox.GetRowAtIndex(i) + if row == nil { + break + } + if !row.GetSelectable() { + continue + } + if hs.getRowURL(row) == url { + hs.listBox.SelectRow(row) + return + } + } +} + +// ============================================================================= +// Search / filtering +// ============================================================================= + +func (hs *HistorySidebar) setupSearchHandler() { + if hs.searchEntry == nil { + return + } + + changedCb := func(_ gtk.SearchEntry) { + hs.onSearchChanged() + } + hs.retainedCallbacks = append(hs.retainedCallbacks, changedCb) + hs.searchEntry.ConnectSearchChanged(&changedCb) +} + +func (hs *HistorySidebar) onSearchChanged() { + hs.mu.Lock() + if hs.destroyed { + hs.mu.Unlock() + return + } + hs.currentQuery = hs.searchEntry.GetText() + hs.preserveScrollAndSelection() + oldTimer := hs.debounceTimer + hs.debounceTimer = 0 + hs.mu.Unlock() + + if oldTimer != 0 { + glib.SourceRemove(oldTimer) + } + + filterCb := glib.SourceFunc(func(uintptr) bool { + hs.applyFilter() + return false + }) + timerID := glib.TimeoutAdd(uint(sidebarSearchDebounceMs), &filterCb, 0) + + hs.mu.Lock() + if hs.destroyed { + hs.mu.Unlock() + if timerID != 0 { + glib.SourceRemove(timerID) + } + return + } + hs.debounceTimer = timerID + hs.mu.Unlock() +} + +func (hs *HistorySidebar) applyFilter() { + hs.mu.Lock() + hs.debounceTimer = 0 + query := hs.currentQuery + + if query == "" { + // Empty query: use in-memory browse entries (paged getRecent). + // Clear search state and invalidate any in-flight search so a late + // search result doesn't overwrite browse state. + hs.searchResults = nil + hs.searchDone = false + hs.searchGen++ + hs.setDisplayGroupsLocked(nil) + if !hs.loadDone { + // Browse was never fully loaded (e.g., a search superseded the + // initial page fetch). Clear the list, show a loading indicator, + // and restart loading history in the background. + hs.mu.Unlock() + hs.scheduleRebuild() // Shows "Loading history…" while fetch runs + hs.startLoadHistory() + return + } + hs.setDisplayGroupsLocked(groupHistoryByDay(hs.allEntries)) + hs.mu.Unlock() + hs.scheduleRebuild() + return + } + + // Non-empty query: use real FTS search via the provider. + // Cancel any stale in-flight search via generation counter. + hs.searchGen++ + gen := hs.searchGen + hs.searchDone = false + hs.searchResults = nil + hs.setDisplayGroupsLocked(nil) + hs.mu.Unlock() + + // Clear the list immediately to avoid showing stale browse results + // while the search is in flight. + hs.scheduleClearList() + + hs.doFTSearch(query, gen) +} + +// doFTSearch runs a history FTS search in a background goroutine and +// updates the display when results arrive. Stale results (from a superseded +// search generation) are silently dropped. +func (hs *HistorySidebar) doFTSearch(query string, gen uint64) { + hs.mu.RLock() + uc := hs.historyUC + ctx := hs.ctx + hs.mu.RUnlock() + + if uc == nil || ctx == nil { + return + } + + go func() { + out, err := uc.Search(ctx, dto.HistorySearchInput{Query: query, Limit: sidebarSearchLimit}) + var entries []*entity.HistoryEntry + if out != nil { + entries = make([]*entity.HistoryEntry, len(out.Matches)) + for i, m := range out.Matches { + entries[i] = m.Entry + } + } + if err != nil { + hs.logger.Error().Err(err).Str("query", query).Msg("history FTS search failed") + } + if entries == nil { + entries = []*entity.HistoryEntry{} + } + + // Apply results on the GTK main thread with stale-result protection + cb := glib.SourceFunc(func(uintptr) bool { + if hs.applySearchResults(entries, gen, err) { + hs.scheduleRebuild() + } + return false + }) + hs.scheduleIdle(cb) + }() +} + +// applySearchResults applies search results under the generation guard. +// Returns true if results were applied (non-stale), false if the generation +// had moved on and the results were dropped. +func (hs *HistorySidebar) applySearchResults(entries []*entity.HistoryEntry, gen uint64, err error) bool { + hs.mu.Lock() + defer hs.mu.Unlock() + if hs.destroyed || gen != hs.searchGen { + return false + } + hs.searchResults = entries + hs.searchDone = true + hs.searchErr = err + hs.setDisplayGroupsLocked(groupHistoryByDay(entries)) + return true +} + +func (hs *HistorySidebar) scheduleIdle(cb glib.SourceFunc) { + if hs != nil && hs.idleScheduler != nil { + hs.idleScheduler(cb) + return + } + glib.IdleAdd(&cb, 0) +} + +// scheduleClearList clears the list box on the GTK main thread. +func (hs *HistorySidebar) scheduleClearList() { + cb := glib.SourceFunc(func(uintptr) bool { + hs.mu.RLock() + destroyed := hs.destroyed + listBox := hs.listBox + hs.mu.RUnlock() + if destroyed || listBox == nil { + return false + } + listBox.RemoveAll() + return false + }) + hs.scheduleIdle(cb) +} + +// scheduleRebuild schedules a list rebuild on the GTK main thread. +func (hs *HistorySidebar) scheduleRebuild() { + cb := glib.SourceFunc(func(uintptr) bool { + hs.rebuildList() + return false + }) + hs.scheduleIdle(cb) +} + +// setDisplayGroupsLocked updates grouped history and the explicit display-row +// model. Caller must hold hs.mu for writing. +func (hs *HistorySidebar) setDisplayGroupsLocked(groups []historyGroup) { + hs.groups = groups + hs.displayRows = buildHistoryDisplayRows(groups) +} diff --git a/internal/ui/component/history_sidebar_rendering.go b/internal/ui/component/history_sidebar_rendering.go new file mode 100644 index 00000000..4281455d --- /dev/null +++ b/internal/ui/component/history_sidebar_rendering.go @@ -0,0 +1,234 @@ +package component + +import ( + "github.com/bnema/puregotk/v4/gtk" + "github.com/bnema/puregotk/v4/pango" + + "github.com/bnema/dumber/internal/domain/entity" +) + +// ============================================================================= +// List rendering +// ============================================================================= + +// rebuildList clears and repopulates the list box from current groups. +// Must be called on the GTK main thread. Preserves scroll and selection. +func (hs *HistorySidebar) rebuildList() { + hs.mu.RLock() + if hs.destroyed || hs.listBox == nil { + hs.mu.RUnlock() + return + } + rows := hs.displayRows + query := hs.currentQuery + hasSearchResults := hs.searchResults != nil + searchDone := hs.searchDone + totalLoaded := hs.totalLoaded + hs.mu.RUnlock() + + // Remove all rows + hs.listBox.RemoveAll() + + if len(rows) == 0 { + if !hasSearchResults && totalLoaded == 0 { + // Browse has not loaded yet AND no search has completed. + hs.showLoadingOrEmpty(query, searchDone) + return + } + // Search completed with 0 results, or browse loaded but empty (no history). + hs.showEmptyState(query) + hs.restoreScrollAndSelection() + return + } + + for _, row := range rows { + switch row.Kind { + case historyDisplayRowHeader: + hs.appendGroupHeader(row.Label) + case historyDisplayRowEntry: + hs.appendEntryRow(row.Entry) + } + } + + hs.listBox.Show() + + // Restore previous scroll position and selection + hs.restoreScrollAndSelection() + + // If no selection was restored and this is the first load, select first entry + hs.ensureAtLeastOneSelection() +} + +func (hs *HistorySidebar) showLoadingOrEmpty(query string, searchDone bool) { + label := gtk.NewLabel(nil) + if label == nil { + return + } + label.AddCssClass("history-sidebar-loading") + + hs.mu.RLock() + isLoading := hs.isLoading + hs.mu.RUnlock() + + switch { + case isLoading && query == "": + label.SetText("Loading history...") + case query != "" && !searchDone: + label.SetText("Searching...") + case query != "": + label.SetText(noResultsText(query)) + default: + label.SetText("No browsing history") + } + + label.SetWrap(false) + label.SetXalign(0.0) + + row := gtk.NewListBoxRow() + if row == nil { + return + } + row.SetSelectable(false) + row.SetCanFocus(false) + row.SetActivatable(false) + row.SetChild(&label.Widget) + hs.listBox.Append(&row.Widget) +} + +func noResultsText(query string) string { + return "No results for \"" + query + "\"" +} + +func (hs *HistorySidebar) showEmptyState(query string) { + label := gtk.NewLabel(nil) + if label == nil { + return + } + label.AddCssClass("history-sidebar-empty") + + if query != "" { + label.SetText(noResultsText(query)) + } else { + label.SetText("No browsing history") + } + + label.SetWrap(false) + label.SetXalign(0.0) + + row := gtk.NewListBoxRow() + if row == nil { + return + } + row.SetSelectable(false) + row.SetCanFocus(false) + row.SetActivatable(false) + row.SetChild(&label.Widget) + hs.listBox.Append(&row.Widget) +} + +// appendGroupHeader adds a non-selectable group header label to the list. +func (hs *HistorySidebar) appendGroupHeader(labelText string) { + label := gtk.NewLabel(&labelText) + if label == nil { + return + } + label.AddCssClass("history-sidebar-group-header") + label.SetXalign(0.0) + label.SetHexpand(true) + + row := gtk.NewListBoxRow() + if row == nil { + return + } + row.SetSelectable(false) + row.SetCanFocus(false) + row.SetActivatable(false) + row.SetChild(&label.Widget) + hs.listBox.Append(&row.Widget) +} + +// appendEntryRow adds a selectable two-line entry row to the list. +func (hs *HistorySidebar) appendEntryRow(entry *entity.HistoryEntry) { + if entry == nil { + return + } + // Outer vertical box for two-line layout + rowBox := gtk.NewBox(gtk.OrientationVerticalValue, 1) + if rowBox == nil { + return + } + rowBox.SetHexpand(true) + + // Title line (first line) + titleLabel := gtk.NewLabel(nil) + if titleLabel == nil { + return + } + titleLabel.AddCssClass("history-sidebar-row-title") + titleLabel.SetText(safeSidebarString(entry.Title, entry.URL)) + titleLabel.SetXalign(0.0) + titleLabel.SetHexpand(true) + titleLabel.SetEllipsize(pango.EllipsizeEndValue) + + // Subtitle line with URL and time + subBox := gtk.NewBox(gtk.OrientationHorizontalValue, 0) + if subBox == nil { + return + } + subBox.SetHexpand(true) + + urlLabel := gtk.NewLabel(nil) + if urlLabel == nil { + return + } + urlLabel.AddCssClass("history-sidebar-row-subtitle") + urlLabel.SetText(readableURL(entry.URL)) + urlLabel.SetXalign(0.0) + urlLabel.SetHexpand(true) + urlLabel.SetEllipsize(pango.EllipsizeEndValue) + + timeLabel := gtk.NewLabel(nil) + if timeLabel == nil { + return + } + timeLabel.AddCssClass("history-sidebar-row-time") + timeLabel.SetText(relativeTime(entry.LastVisited)) + timeLabel.SetXalign(1.0) + + subBox.Append(&urlLabel.Widget) + subBox.Append(&timeLabel.Widget) + + rowBox.Append(&titleLabel.Widget) + rowBox.Append(&subBox.Widget) + + // Create the list box row + row := gtk.NewListBoxRow() + if row == nil { + return + } + row.AddCssClass("history-sidebar-row") + row.SetSelectable(true) + row.SetActivatable(true) + row.SetCanFocus(true) + row.SetFocusOnClick(true) + row.SetChild(&rowBox.Widget) + + hs.listBox.Append(&row.Widget) +} + +// ensureAtLeastOneSelection selects the first selectable row if nothing is selected. +func (hs *HistorySidebar) ensureAtLeastOneSelection() { + if hs.listBox.GetSelectedRow() != nil { + return + } + for i := 0; ; i++ { + row := hs.listBox.GetRowAtIndex(i) + if row == nil { + break + } + if row.GetSelectable() { + hs.listBox.SelectRow(row) + return + } + } +} diff --git a/internal/ui/component/history_sidebar_search_test.go b/internal/ui/component/history_sidebar_search_test.go new file mode 100644 index 00000000..caeed5cc --- /dev/null +++ b/internal/ui/component/history_sidebar_search_test.go @@ -0,0 +1,391 @@ +package component + +import ( + "context" + "errors" + "fmt" + "sync" + "testing" + "time" + + "github.com/bnema/dumber/internal/application/dto" + "github.com/bnema/dumber/internal/domain/entity" + "github.com/bnema/puregotk/v4/glib" + "github.com/rs/zerolog" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// ============================================================================= +// applySearchResults seam tests +// ============================================================================= + +// newTestSidebarSearchHarness creates a minimal HistorySidebar with only the +// fields needed by applySearchResults, avoiding GTK widget construction. +func newTestSidebarSearchHarness() *HistorySidebar { + return &HistorySidebar{ + mu: sync.RWMutex{}, + logger: zerolog.Nop(), + } +} + +func TestApplySearchResults_NonStaleApplied(t *testing.T) { + hs := newTestSidebarSearchHarness() + hs.searchGen = 1 + + entries := []*entity.HistoryEntry{ + {ID: 1, URL: "https://example.com", Title: "Example", LastVisited: time.Now()}, + } + + applied := hs.applySearchResults(entries, 1, nil) + assert.True(t, applied, "non-stale results must be applied") + assert.True(t, hs.searchDone) + require.NoError(t, hs.searchErr) + require.NotNil(t, hs.searchResults) + assert.Len(t, hs.searchResults, 1) + assert.Equal(t, "https://example.com", hs.searchResults[0].URL) + require.NotNil(t, hs.groups, "groups must be built from results") + assert.Equal(t, "Today", hs.groups[0].Label) +} + +func TestApplySearchResults_StaleGenRejected(t *testing.T) { + hs := newTestSidebarSearchHarness() + hs.searchGen = 2 // Gen has moved on + + entries := []*entity.HistoryEntry{ + {ID: 1, URL: "https://stale.com", Title: "Stale", LastVisited: time.Now()}, + } + + applied := hs.applySearchResults(entries, 1, nil) // gen=1 is stale + assert.False(t, applied, "stale results must be rejected") + assert.False(t, hs.searchDone, "search must not be marked done for stale result") + assert.Nil(t, hs.searchResults, "searchResults must remain nil") + assert.Nil(t, hs.groups, "groups must remain nil") +} + +func TestApplySearchResults_StaleAfterIncrement(t *testing.T) { + hs := newTestSidebarSearchHarness() + hs.searchGen = 1 + + // First result applied as gen=1 + firstEntries := []*entity.HistoryEntry{ + {ID: 1, URL: "https://first.com", Title: "First", LastVisited: time.Now()}, + } + applied := hs.applySearchResults(firstEntries, 1, nil) + assert.True(t, applied) + + // Second search starts, gen advances + hs.searchGen = 2 + + // Stale result from gen=1 arrives + staleEntries := []*entity.HistoryEntry{ + {ID: 2, URL: "https://stale.com", Title: "Stale", LastVisited: time.Now()}, + } + applied = hs.applySearchResults(staleEntries, 1, nil) + assert.False(t, applied, "stale result must be rejected") + + // The original results must be preserved + require.NotNil(t, hs.searchResults) + assert.Len(t, hs.searchResults, 1) + assert.Equal(t, "https://first.com", hs.searchResults[0].URL) +} + +func TestApplySearchResults_ErrorStored(t *testing.T) { + hs := newTestSidebarSearchHarness() + hs.searchGen = 1 + + wantErr := errors.New("search failed") + entries := []*entity.HistoryEntry{} // empty but not nil + + applied := hs.applySearchResults(entries, 1, wantErr) + assert.True(t, applied) + assert.True(t, hs.searchDone) + require.ErrorIs(t, hs.searchErr, wantErr) + require.NotNil(t, hs.searchResults) + assert.Empty(t, hs.searchResults) +} + +func TestApplySearchResults_EmptyResultsApplied(t *testing.T) { + hs := newTestSidebarSearchHarness() + hs.searchGen = 1 + + applied := hs.applySearchResults([]*entity.HistoryEntry{}, 1, nil) + assert.True(t, applied) + assert.True(t, hs.searchDone) + require.NoError(t, hs.searchErr) + require.NotNil(t, hs.searchResults) + assert.Empty(t, hs.searchResults) + // Groups from empty entries should be nil or empty + assert.Nil(t, hs.groups, "empty entries should produce nil groups") +} + +func TestApplyDeletedEntryLocked_RecomputesBrowseState(t *testing.T) { + now := time.Now() + keepA := &entity.HistoryEntry{ID: 1, URL: "https://keep-a.com", Title: "Keep A", LastVisited: now} + deleteMe := &entity.HistoryEntry{ID: 2, URL: "https://delete-me.com", Title: "Delete Me", LastVisited: now.Add(-time.Minute)} + keepB := &entity.HistoryEntry{ID: 3, URL: "https://keep-b.com", Title: "Keep B", LastVisited: now.Add(-2 * time.Minute)} + + hs := newTestSidebarSearchHarness() + hs.allEntries = []*entity.HistoryEntry{keepA, deleteMe, keepB} + hs.totalLoaded = len(hs.allEntries) + hs.loadGen = 7 + hs.isLoading = true + hs.loadStarted = true + hs.searchResults = []*entity.HistoryEntry{deleteMe, keepB} + hs.setDisplayGroupsLocked(groupHistoryByDay(hs.allEntries)) + + hs.mu.Lock() + hs.applyDeletedEntryLocked(deleteMe.ID, keepB.URL) + hs.mu.Unlock() + + assert.Equal(t, uint64(8), hs.loadGen, "deletes must invalidate in-flight browse loads") + assert.False(t, hs.isLoading, "delete should clear stale loading state") + assert.False(t, hs.loadStarted, "delete should clear stale loadStarted state") + assert.Equal(t, 2, hs.totalLoaded, "browse pagination offset must track remaining loaded entries") + assert.Equal(t, keepB.URL, hs.prevSelectedURL) + assert.Len(t, hs.allEntries, 2) + assert.Equal(t, []string{keepA.URL, keepB.URL}, []string{hs.allEntries[0].URL, hs.allEntries[1].URL}) + assert.Len(t, hs.searchResults, 1) + assert.Equal(t, keepB.URL, hs.searchResults[0].URL) + require.Len(t, hs.groups, 1) + require.Len(t, hs.groups[0].Entries, 2) + require.Len(t, hs.displayRows, 3, "one header plus two remaining entries") + assert.Equal(t, historyDisplayRowHeader, hs.displayRows[0].Kind) + assert.Equal(t, historyDisplayRowEntry, hs.displayRows[1].Kind) + assert.Equal(t, historyDisplayRowEntry, hs.displayRows[2].Kind) +} + +// ============================================================================= +// Async search seam: controllable history port fake +// ============================================================================= + +type fakeHistorySidebarHistory struct { + getRecentFn func(ctx context.Context, limit, offset int) ([]*entity.HistoryEntry, error) + searchFn func(ctx context.Context, input dto.HistorySearchInput) (*dto.HistorySearchOutput, error) + deleteFn func(ctx context.Context, id int64) error +} + +func (f *fakeHistorySidebarHistory) GetRecent(ctx context.Context, limit, offset int) ([]*entity.HistoryEntry, error) { + if f.getRecentFn != nil { + return f.getRecentFn(ctx, limit, offset) + } + return nil, fmt.Errorf("unexpected GetRecent call in fakeHistorySidebarHistory") +} + +func (f *fakeHistorySidebarHistory) Search(ctx context.Context, input dto.HistorySearchInput) (*dto.HistorySearchOutput, error) { + if f.searchFn != nil { + return f.searchFn(ctx, input) + } + return &dto.HistorySearchOutput{Matches: []entity.HistoryMatch{}}, nil +} + +func (f *fakeHistorySidebarHistory) Delete(ctx context.Context, id int64) error { + if f.deleteFn != nil { + return f.deleteFn(ctx, id) + } + return nil +} + +func TestApplySearchResults_StaleGenerationDropsResultsAfterSearch(t *testing.T) { + searchCalled := make(chan struct{}, 1) + idleCalled := make(chan glib.SourceFunc, 1) + + history := &fakeHistorySidebarHistory{ + searchFn: func(context.Context, dto.HistorySearchInput) (*dto.HistorySearchOutput, error) { + searchCalled <- struct{}{} + return &dto.HistorySearchOutput{Matches: []entity.HistoryMatch{ + {Entry: &entity.HistoryEntry{ID: 1, URL: "https://result.com", Title: "Result", LastVisited: time.Now()}}, + }}, nil + }, + } + + hs := newTestSidebarSearchHarness() + hs.ctx = t.Context() + hs.historyUC = history + hs.searchGen = 1 + hs.idleScheduler = func(cb glib.SourceFunc) { + idleCalled <- cb + } + + // Start search with gen=1. + hs.doFTSearch("test", 1) + select { + case <-searchCalled: + case <-time.After(time.Second): + t.Fatal("timed out waiting for search use case to be invoked") + } + + // Advance gen before the queued UI callback applies results. + hs.mu.Lock() + hs.searchGen = 2 + hs.mu.Unlock() + + select { + case cb := <-idleCalled: + cb(0) + case <-time.After(time.Second): + t.Fatal("timed out waiting for scheduled idle callback") + } + + hs.mu.RLock() + defer hs.mu.RUnlock() + assert.Nil(t, hs.searchResults) + assert.False(t, hs.searchDone) + assert.Nil(t, hs.groups) +} + +func TestApplySearchResults_CurrentGenerationAppliedAfterSearch(t *testing.T) { + searchCalled := make(chan struct{}, 1) + idleCalled := make(chan glib.SourceFunc, 1) + + history := &fakeHistorySidebarHistory{ + searchFn: func(context.Context, dto.HistorySearchInput) (*dto.HistorySearchOutput, error) { + searchCalled <- struct{}{} + return &dto.HistorySearchOutput{Matches: []entity.HistoryMatch{ + {Entry: &entity.HistoryEntry{ID: 1, URL: "https://live.com", Title: "Live", LastVisited: time.Now()}}, + }}, nil + }, + } + + hs := newTestSidebarSearchHarness() + hs.ctx = t.Context() + hs.historyUC = history + hs.searchGen = 1 + hs.idleScheduler = func(cb glib.SourceFunc) { + idleCalled <- cb + } + + // Start the search and wait for the use case to be invoked. + hs.doFTSearch("live", 1) + select { + case <-searchCalled: + case <-time.After(time.Second): + t.Fatal("timed out waiting for search use case to be invoked") + } + + select { + case cb := <-idleCalled: + cb(0) + case <-time.After(time.Second): + t.Fatal("timed out waiting for scheduled idle callback") + } + + hs.mu.RLock() + require.NotNil(t, hs.searchResults) + assert.True(t, hs.searchDone) + assert.Len(t, hs.searchResults, 1) + assert.Equal(t, "https://live.com", hs.searchResults[0].URL) + hs.mu.RUnlock() +} + +// ============================================================================= +// Reload with query preservation +// ============================================================================= + +// TestHistorySidebar_ReloadPreservesQuery verifies that Reload preserves the +// active query while resetting browse/search state before the refreshed load. +func TestHistorySidebar_ReloadPreservesQuery(t *testing.T) { + searchCalled := make(chan struct{}, 1) + history := &fakeHistorySidebarHistory{ + searchFn: func(context.Context, dto.HistorySearchInput) (*dto.HistorySearchOutput, error) { + searchCalled <- struct{}{} + return &dto.HistorySearchOutput{Matches: []entity.HistoryMatch{}}, nil + }, + } + + hs := newTestSidebarSearchHarness() + hs.currentQuery = "preserved" + hs.historyUC = history + hs.ctx = t.Context() + + hs.mu.Lock() + oldGen := hs.searchGen + hs.loadDone = true + hs.allEntries = []*entity.HistoryEntry{ + {ID: 1, URL: "https://old.com", Title: "Old", LastVisited: time.Now()}, + } + hs.groups = groupHistoryByDay(hs.allEntries) + hs.searchResults = []*entity.HistoryEntry{{ID: 2, URL: "https://stale.com", Title: "Stale", LastVisited: time.Now()}} + hs.searchDone = true + hs.mu.Unlock() + + hs.Reload() + + select { + case <-searchCalled: + case <-time.After(time.Second): + t.Fatal("timed out waiting for reload search to be invoked") + } + + hs.mu.RLock() + assert.Equal(t, "preserved", hs.currentQuery, "query must be preserved after Reload") + assert.False(t, hs.loadDone, "loadDone must be reset") + assert.False(t, hs.loadStarted, "loadStarted must be reset") + assert.Nil(t, hs.allEntries, "entries must be cleared") + assert.Nil(t, hs.groups, "groups must be cleared") + assert.Nil(t, hs.searchResults, "searchResults must be cleared before refreshed search applies") + assert.False(t, hs.searchDone, "searchDone must be reset") + assert.Equal(t, oldGen+1, hs.searchGen, "searchGen must be incremented") + hs.mu.RUnlock() +} + +// ============================================================================= +// fetchPage generation guard: stale browse results must not clear loading state +// ============================================================================= + +func TestFetchPage_StaleGenerationDoesNotMutateLoadingState(t *testing.T) { + t.Parallel() + + // Repo blocks GetRecent until test signals proceed, so we can control + // when a stale fetch completes relative to generation changes. + getRecentCalled := make(chan struct{}) + proceed := make(chan struct{}) + + history := &fakeHistorySidebarHistory{ + getRecentFn: func(context.Context, int, int) ([]*entity.HistoryEntry, error) { + close(getRecentCalled) + <-proceed + return []*entity.HistoryEntry{}, nil + }, + } + + hs := newTestSidebarSearchHarness() + hs.ctx = t.Context() + hs.historyUC = history + + // Simulate: gen 1 started, then gen 2 started (e.g. by Reload/Show). + // The second call incremented loadGen and set isLoading/loadStarted. + hs.mu.Lock() + hs.loadGen = 2 + hs.isLoading = true + hs.loadStarted = true + hs.totalLoaded = 0 + hs.hasMore = true + hs.mu.Unlock() + + // Start stale gen 1 fetch in background + fetchDone := make(chan struct{}) + go func() { + hs.fetchPage(0, 1) // gen=1 is stale — loadGen is 2 + close(fetchDone) + }() + + // Wait until GetRecent is entered (fetchPage is blocked inside the repo call) + <-getRecentCalled + + // Let GetRecent return — this simulates the gen 1 fetch completing + // after gen 2 has already taken over. + close(proceed) + + // Wait for fetchPage to finish processing the stale return + <-fetchDone + + // Verify: stale completion must NOT clear the new generation's loading state + hs.mu.RLock() + assert.True(t, hs.isLoading, "isLoading must remain true when stale gen completes") + assert.True(t, hs.loadStarted, "loadStarted must remain true when stale gen completes") + assert.Equal(t, uint64(2), hs.loadGen, "loadGen must remain unchanged") + assert.False(t, hs.loadDone, "loadDone must remain false; gen 2 hasn't completed") + assert.Equal(t, 0, hs.totalLoaded, "stale results must not update totalLoaded") + hs.mu.RUnlock() +} diff --git a/internal/ui/component/history_sidebar_widgets.go b/internal/ui/component/history_sidebar_widgets.go new file mode 100644 index 00000000..d9cc28fa --- /dev/null +++ b/internal/ui/component/history_sidebar_widgets.go @@ -0,0 +1,93 @@ +package component + +import ( + "fmt" + + "github.com/bnema/puregotk/v4/gtk" +) + +// ============================================================================= +// Widget creation +// ============================================================================= + +func (hs *HistorySidebar) createWidgets() error { + if err := hs.initOuterBox(); err != nil { + return err + } + if err := hs.initSearchBox(); err != nil { + return err + } + if err := hs.initListArea(); err != nil { + return err + } + return nil +} + +func (hs *HistorySidebar) initOuterBox() error { + hs.outerBox = gtk.NewBox(gtk.OrientationVerticalValue, 0) + if hs.outerBox == nil { + return fmt.Errorf("history sidebar: outer box creation failed") + } + hs.outerBox.AddCssClass("history-sidebar-outer") + hs.outerBox.SetSizeRequest(sidebarMinWidth, -1) + hs.outerBox.SetHexpand(false) + hs.outerBox.SetVexpand(true) + hs.outerBox.SetVisible(false) + return nil +} + +func (hs *HistorySidebar) initSearchBox() error { + hs.searchBox = gtk.NewBox(gtk.OrientationHorizontalValue, 4) + if hs.searchBox == nil { + return fmt.Errorf("history sidebar: search box creation failed") + } + hs.searchBox.AddCssClass("history-sidebar-search-box") + hs.searchBox.SetHexpand(true) + + hs.searchEntry = gtk.NewSearchEntry() + if hs.searchEntry == nil { + return fmt.Errorf("history sidebar: search entry creation failed") + } + hs.searchEntry.AddCssClass("history-sidebar-search") + hs.searchEntry.SetHexpand(true) + placeholder := "Search history..." + hs.searchEntry.SetPlaceholderText(&placeholder) + + hs.searchBox.Append(&hs.searchEntry.Widget) + hs.outerBox.Append(&hs.searchBox.Widget) + return nil +} + +func (hs *HistorySidebar) initListArea() error { + hs.scrolledWin = gtk.NewScrolledWindow() + if hs.scrolledWin == nil { + return fmt.Errorf("history sidebar: scrolled window creation failed") + } + hs.scrolledWin.SetVexpand(true) + hs.scrolledWin.SetHexpand(true) + hs.scrolledWin.SetPolicy(gtk.PolicyNeverValue, gtk.PolicyAutomaticValue) + hs.scrolledWin.AddCssClass("history-sidebar-groups") + + hs.listBox = gtk.NewListBox() + if hs.listBox == nil { + return fmt.Errorf("history sidebar: list box creation failed") + } + hs.listBox.AddCssClass("history-sidebar-groups") + hs.listBox.SetActivateOnSingleClick(true) + hs.listBox.SetSelectionMode(gtk.SelectionSingleValue) + + // Connect row activation (single-click and keyboard activation) + rowActivatedCb := func(_ gtk.ListBox, rowPtr uintptr) { + row := gtk.ListBoxRowNewFromInternalPtr(rowPtr) + if row == nil { + return + } + hs.onRowActivated(row) + } + hs.retainedCallbacks = append(hs.retainedCallbacks, rowActivatedCb) + hs.listBox.ConnectRowActivated(&rowActivatedCb) + + hs.scrolledWin.SetChild(&hs.listBox.Widget) + hs.outerBox.Append(&hs.scrolledWin.Widget) + return nil +} diff --git a/internal/ui/component/history_test_helpers_test.go b/internal/ui/component/history_test_helpers_test.go new file mode 100644 index 00000000..9a7866fe --- /dev/null +++ b/internal/ui/component/history_test_helpers_test.go @@ -0,0 +1,39 @@ +package component + +// searchStateSnapshot captures the observable state of a history search +// at a point in time. This is test-only data for pure transition checks. +type searchStateSnapshot struct { + Query string + HasSearchDone bool + HasResults bool + ResultCount int +} + +// transitionSearchState models a search state transition for tests without GTK. +func transitionSearchState(newQuery string, resultCount int) searchStateSnapshot { + return searchStateSnapshot{ + Query: newQuery, + HasSearchDone: newQuery != "", + HasResults: newQuery != "" && resultCount > 0, + ResultCount: resultCount, + } +} + +// reloadPreservationSnapshot captures the preserved state during a reload. +type reloadPreservationSnapshot struct { + PreservedQuery string + ResetBrowse bool + ClearSearch bool +} + +// applyReloadState computes the expected state after a reload for test-only +// transition checks. +func applyReloadState(currentQuery string) reloadPreservationSnapshot { + s := reloadPreservationSnapshot{PreservedQuery: currentQuery} + if currentQuery == "" { + s.ResetBrowse = true + } else { + s.ClearSearch = true + } + return s +} diff --git a/internal/ui/dispatcher/keyboard.go b/internal/ui/dispatcher/keyboard.go index a6b7cf38..ef99424c 100644 --- a/internal/ui/dispatcher/keyboard.go +++ b/internal/ui/dispatcher/keyboard.go @@ -15,6 +15,10 @@ import ( ) const ( + // historySystemViewURL is the full-page/systemview history surface. + // It remains reachable by direct navigation (for example typing + // dumb://history in the omnibox), but Ctrl+H no longer falls back to it. + // Ctrl+H is reserved for the native GTK history sidebar only. historySystemViewURL = "dumb://history" favoritesSystemViewURL = "dumb://favorites" configSystemViewURL = "dumb://config" @@ -32,24 +36,25 @@ type KeyboardActions struct { // KeyboardDispatcher routes keyboard actions to appropriate coordinators. type KeyboardDispatcher struct { - actions KeyboardActions - wsCoord *coordinator.WorkspaceCoordinator - navCoord *coordinator.NavigationCoordinator - zoomUC *usecase.ManageZoomUseCase - copyURLUC *usecase.CopyURLUseCase - actionHandlers map[input.Action]func(ctx context.Context) error - onQuit func() - onFindOpen func(ctx context.Context) error - onFindNext func(ctx context.Context) error - onFindPrev func(ctx context.Context) error - onFindClose func(ctx context.Context) error - activePaneID func(ctx context.Context) entity.PaneID - onSessionOpen func(ctx context.Context, paneID entity.PaneID) error - onMovePaneToTab func(ctx context.Context, paneID entity.PaneID) error - onMovePaneToNext func(ctx context.Context, paneID entity.PaneID) error - onEjectPaneToWindow func(ctx context.Context, paneID entity.PaneID) error - onToggleFloating func(ctx context.Context) error - onOpenFloating func(ctx context.Context, target input.FloatingProfileTarget) error + actions KeyboardActions + wsCoord *coordinator.WorkspaceCoordinator + navCoord *coordinator.NavigationCoordinator + zoomUC *usecase.ManageZoomUseCase + copyURLUC *usecase.CopyURLUseCase + actionHandlers map[input.Action]func(ctx context.Context) error + onQuit func() + onFindOpen func(ctx context.Context) error + onFindNext func(ctx context.Context) error + onFindPrev func(ctx context.Context) error + onFindClose func(ctx context.Context) error + activePaneID func(ctx context.Context) entity.PaneID + onSessionOpen func(ctx context.Context, paneID entity.PaneID) error + onMovePaneToTab func(ctx context.Context, paneID entity.PaneID) error + onMovePaneToNext func(ctx context.Context, paneID entity.PaneID) error + onEjectPaneToWindow func(ctx context.Context, paneID entity.PaneID) error + onToggleHistorySidebar func(ctx context.Context) error + onToggleFloating func(ctx context.Context) error + onOpenFloating func(ctx context.Context, target input.FloatingProfileTarget) error } // NewKeyboardDispatcher creates a new KeyboardDispatcher. @@ -119,6 +124,10 @@ func (d *KeyboardDispatcher) SetOnEjectPaneToWindow(fn func(ctx context.Context, d.onEjectPaneToWindow = fn } +func (d *KeyboardDispatcher) SetOnToggleHistorySidebar(fn func(ctx context.Context) error) { + d.onToggleHistorySidebar = fn +} + func (d *KeyboardDispatcher) SetOnToggleFloatingPane(fn func(ctx context.Context) error) { d.onToggleFloating = fn } @@ -243,8 +252,14 @@ func (d *KeyboardDispatcher) initActionHandlers() { } return d.logNoop(ctx, "toggle floating pane action (no handler)") }, + // ActionToggleHistorySystemView (default Ctrl+H) is intentionally bound + // to the native GTK history sidebar only. The dumb://history systemview + // remains available through direct navigation, not shortcut fallback. input.ActionToggleHistorySystemView: func(ctx context.Context) error { - return d.wsCoord.ToggleSystemViewRight(ctx, historySystemViewURL) + if d.onToggleHistorySidebar == nil { + return fmt.Errorf("history sidebar unavailable: toggle handler not wired") + } + return d.onToggleHistorySidebar(ctx) }, input.ActionToggleFavoritesSystemView: func(ctx context.Context) error { return d.wsCoord.ToggleSystemViewRight(ctx, favoritesSystemViewURL) diff --git a/internal/ui/dispatcher/keyboard_test.go b/internal/ui/dispatcher/keyboard_test.go index 3ad8516b..48c4c998 100644 --- a/internal/ui/dispatcher/keyboard_test.go +++ b/internal/ui/dispatcher/keyboard_test.go @@ -2,11 +2,10 @@ package dispatcher import ( "context" + "fmt" "testing" - "github.com/bnema/dumber/internal/application/usecase" "github.com/bnema/dumber/internal/domain/entity" - "github.com/bnema/dumber/internal/ui/component" "github.com/bnema/dumber/internal/ui/coordinator" "github.com/bnema/dumber/internal/ui/input" "github.com/stretchr/testify/assert" @@ -64,37 +63,56 @@ func TestKeyboardDispatcher_TabActionsUseInjectedKeyboardActions(t *testing.T) { assert.Equal(t, 4, switchedIndex) } -func TestKeyboardDispatcher_ToggleHistorySystemViewOpensRightSplit(t *testing.T) { +func TestKeyboardDispatcher_ToggleHistorySidebarCallsCallback(t *testing.T) { ctx := context.Background() - ids := []string{"pane-2", "split-1"} - idx := 0 - panesUC := usecase.NewManagePanesUseCase(func() string { - id := ids[idx] - idx++ - return id - }) + d := NewKeyboardDispatcher(ctx, &coordinator.WorkspaceCoordinator{}, &coordinator.NavigationCoordinator{}, nil, nil, KeyboardActions{}, func(context.Context) entity.PaneID { return "" }) - initialPane := entity.NewPane("pane-1") - initialPane.URI = "https://example.com" - ws := entity.NewWorkspace("ws-1", initialPane) - wsCoord := coordinator.NewWorkspaceCoordinator(ctx, coordinator.WorkspaceCoordinatorConfig{ - PanesUC: panesUC, - GetActiveWS: func() (*entity.Workspace, *component.WorkspaceView) { - return ws, nil - }, + var called bool + d.SetOnToggleHistorySidebar(func(context.Context) error { + called = true + return nil }) - d := NewKeyboardDispatcher(ctx, wsCoord, &coordinator.NavigationCoordinator{}, nil, nil, KeyboardActions{}, func(context.Context) entity.PaneID { return "" }) - err := d.Dispatch(ctx, input.ActionToggleHistorySystemView) require.NoError(t, err) + assert.True(t, called, "onToggleHistorySidebar should have been called") +} + +func TestKeyboardDispatcher_ToggleHistorySystemViewReturnsErrorWhenHandlerMissing(t *testing.T) { + ctx := context.Background() + d := NewKeyboardDispatcher(ctx, &coordinator.WorkspaceCoordinator{}, &coordinator.NavigationCoordinator{}, nil, nil, KeyboardActions{}, func(context.Context) entity.PaneID { return "" }) + + err := d.Dispatch(ctx, input.ActionToggleHistorySystemView) + require.Error(t, err) + assert.ErrorContains(t, err, "history sidebar unavailable") +} + +func TestKeyboardDispatcher_ToggleHistorySidebarErrorPropagation(t *testing.T) { + ctx := context.Background() + d := NewKeyboardDispatcher(ctx, &coordinator.WorkspaceCoordinator{}, &coordinator.NavigationCoordinator{}, nil, nil, KeyboardActions{}, func(context.Context) entity.PaneID { return "" }) - require.Equal(t, 2, ws.PaneCount()) - active := ws.ActivePane() - require.NotNil(t, active) - require.NotNil(t, active.Pane) - assert.Equal(t, entity.PaneID("pane-2"), active.Pane.ID) - assert.Equal(t, "dumb://history", active.Pane.URI) + wantErr := fmt.Errorf("sidebar toggle failed") + d.SetOnToggleHistorySidebar(func(context.Context) error { + return wantErr + }) + + err := d.Dispatch(ctx, input.ActionToggleHistorySystemView) + require.Error(t, err) + assert.ErrorIs(t, err, wantErr, "onToggleHistorySidebar error should propagate") +} + +func TestKeyboardDispatcher_ToggleHistorySidebarSetThenUnsetReturnsError(t *testing.T) { + ctx := context.Background() + d := NewKeyboardDispatcher(ctx, &coordinator.WorkspaceCoordinator{}, &coordinator.NavigationCoordinator{}, nil, nil, KeyboardActions{}, func(context.Context) entity.PaneID { return "" }) + + d.SetOnToggleHistorySidebar(func(context.Context) error { + return nil + }) + d.SetOnToggleHistorySidebar(nil) + + err := d.Dispatch(ctx, input.ActionToggleHistorySystemView) + require.Error(t, err) + assert.ErrorContains(t, err, "history sidebar unavailable") } func TestKeyboardDispatcher_PassesActivePaneIDToShellCallbacks(t *testing.T) { diff --git a/internal/ui/theme/css.go b/internal/ui/theme/css.go index bf0f0f39..67195b18 100644 --- a/internal/ui/theme/css.go +++ b/internal/ui/theme/css.go @@ -140,6 +140,10 @@ func GenerateCSSFull(p Palette, _ float64, fonts FontConfig, modeColors ModeColo // Accent picker styling sb.WriteString(generateAccentPickerCSS(p)) + sb.WriteString("\n") + + // History sidebar styling + sb.WriteString(generateHistorySidebarCSS(p)) return sb.String() } diff --git a/internal/ui/theme/history_sidebar_css.go b/internal/ui/theme/history_sidebar_css.go new file mode 100644 index 00000000..196d351b --- /dev/null +++ b/internal/ui/theme/history_sidebar_css.go @@ -0,0 +1,90 @@ +package theme + +// generateHistorySidebarCSS creates GTK4 CSS for the history sidebar component. +func generateHistorySidebarCSS(_ Palette) string { + return `/* ===== History Sidebar Styling ===== */ + +.history-sidebar-outer { + background-color: var(--surface); + border-left: 0.0625em solid var(--border); +} + +.history-sidebar-search-box { + padding: 0.375em 0.5em; + border-bottom: 0.0625em solid var(--border); + background-color: var(--surface); +} + +.history-sidebar-search { + padding: 0.125em 0.375em; + font-size: 0.85em; +} + +.history-sidebar-groups { + background-color: var(--surface); +} + +.history-sidebar-group-header { + padding: 0.25em 0.625em; + padding-top: 0.375em; + font-size: 0.75em; + font-weight: 600; + color: var(--muted); + text-transform: uppercase; + letter-spacing: 0.04em; + background-color: var(--surface-variant); + border-bottom: 0.0625em solid var(--border); +} + +.history-sidebar-row { + padding: 0.1875em 0.625em; + min-height: 0; + border-bottom: 0.0625em solid alpha(var(--border), 0.4); + background-color: var(--surface); + transition: background-color 100ms ease; +} + +.history-sidebar-row:hover { + background-color: alpha(var(--accent), 0.18); +} + +.history-sidebar-row:selected { + background-color: alpha(var(--accent), 0.18); +} + +.history-sidebar-row:focus { + background-color: alpha(var(--accent), 0.18); +} + +.history-sidebar-row-title { + font-size: 0.82em; + color: var(--text); + font-weight: 500; +} + +.history-sidebar-row-subtitle { + font-size: 0.72em; + color: var(--muted); +} + +.history-sidebar-row-time { + font-size: 0.68em; + color: var(--muted); + padding-left: 0.5em; + opacity: 0.75; +} + +.history-sidebar-empty { + padding: 1.5em 0.75em; + font-size: 0.82em; + color: var(--muted); + font-style: italic; +} + +.history-sidebar-loading { + padding: 1.5em 0.75em; + font-size: 0.82em; + color: var(--muted); +} +` +} diff --git a/internal/ui/theme/history_sidebar_css_test.go b/internal/ui/theme/history_sidebar_css_test.go new file mode 100644 index 00000000..231bba1a --- /dev/null +++ b/internal/ui/theme/history_sidebar_css_test.go @@ -0,0 +1,333 @@ +package theme + +import ( + "crypto/sha256" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGenerateHistorySidebarCSS_ContainsExpectedSelectors(t *testing.T) { + css := generateHistorySidebarCSS(DefaultDarkPalette()) + + selectors := []string{ + ".history-sidebar-outer", + ".history-sidebar-search-box", + ".history-sidebar-search", + ".history-sidebar-groups", + ".history-sidebar-group-header", + ".history-sidebar-row", + ".history-sidebar-row:hover", + ".history-sidebar-row:selected", + ".history-sidebar-row:focus", + ".history-sidebar-row-title", + ".history-sidebar-row-subtitle", + ".history-sidebar-row-time", + ".history-sidebar-empty", + ".history-sidebar-loading", + } + + for _, sel := range selectors { + assert.Contains(t, css, sel+" {", + "expected selector %s { in generated CSS", sel) + } +} + +func TestGenerateHistorySidebarCSS_AccentAlphaInterpolation(t *testing.T) { + css := generateHistorySidebarCSS(DefaultDarkPalette()) + + expectedAlpha := "alpha(var(--accent), 0.18)" + assert.Contains(t, css, expectedAlpha, + "expected accent alpha variable usage %q in generated CSS", expectedAlpha) + assert.NotContains(t, css, "alpha(#", + "history sidebar CSS should not inline hardcoded accent hex values") + + // The accent alpha should appear in hover, selected, and focus blocks. + assert.GreaterOrEqual(t, strings.Count(css, expectedAlpha), 3, + "expected accent alpha to appear at least 3 times (hover/selected/focus)") +} + +func TestGenerateHistorySidebarCSS_DeterministicOutput(t *testing.T) { + palette := DefaultDarkPalette() + + css1 := generateHistorySidebarCSS(palette) + css2 := generateHistorySidebarCSS(palette) + + hash1 := sha256.Sum256([]byte(css1)) + hash2 := sha256.Sum256([]byte(css2)) + + assert.Equal(t, hash1, hash2, "CSS output must be deterministic for the same palette") +} + +func TestGenerateHistorySidebarCSS_UsesPaletteVariablesInsteadOfInliningColors(t *testing.T) { + darkCSS := generateHistorySidebarCSS(DefaultDarkPalette()) + lightCSS := generateHistorySidebarCSS(DefaultLightPalette()) + + assert.Equal(t, darkCSS, lightCSS, + "history sidebar fragment should be palette-independent when it relies on CSS variables") + assert.Contains(t, darkCSS, "alpha(var(--accent), 0.18)") + assert.NotContains(t, darkCSS, "alpha(#") +} + +func TestGenerateHistorySidebarCSS_ThroughGenerateCSS(t *testing.T) { + fullCSS := GenerateCSS(DefaultDarkPalette()) + + assert.Contains(t, fullCSS, ".history-sidebar-outer {", + "full CSS must contain history sidebar selectors") + assert.Contains(t, fullCSS, ".history-sidebar-group-header {", + "full CSS must contain group header selector") + assert.Contains(t, fullCSS, ".history-sidebar-row-title {", + "full CSS must contain row title selector") + assert.Contains(t, fullCSS, "History Sidebar Styling", + "full CSS must contain the history sidebar comment marker") +} + +func TestGenerateHistorySidebarCSS_NoEmptyCSSBlocks(t *testing.T) { + css := generateHistorySidebarCSS(DefaultDarkPalette()) + + sections := strings.Split(css, "{") + for i, section := range sections { + if i == 0 { + continue + } + closeIdx := strings.Index(section, "}") + if closeIdx < 0 { + continue + } + blockContent := strings.TrimSpace(section[:closeIdx]) + assert.NotEmpty(t, blockContent, "CSS block must not be empty: section %d", i) + } +} + +func TestGenerateHistorySidebarCSS_CustomPaletteValuesInterpolated(t *testing.T) { + customPalette := Palette{ + Background: "#111111", + Surface: "#222222", + SurfaceVariant: "#333333", + Text: "#eeeeee", + Muted: "#aaaaaa", + Accent: "#ff6600", + Border: "#444444", + Success: "#00cc44", + Warning: "#ffaa00", + Destructive: "#cc2222", + } + + css := generateHistorySidebarCSS(customPalette) + + assert.Contains(t, css, "alpha(var(--accent), 0.18)", + "sidebar CSS should use the shared accent variable") + assert.NotContains(t, css, "#ff6600", + "sidebar fragment should not inline palette-specific accent values") + + assert.Contains(t, css, ".history-sidebar-outer {") + assert.Contains(t, css, ".history-sidebar-row-title {") + assert.Contains(t, css, ".history-sidebar-row-subtitle {") + assert.Contains(t, css, ".history-sidebar-row-time {") + assert.Contains(t, css, ".history-sidebar-empty {") +} + +func TestGenerateHistorySidebarCSS_ContainsTransition(t *testing.T) { + css := generateHistorySidebarCSS(DefaultDarkPalette()) + + assert.Contains(t, css, "transition:", + "CSS should include transition property for smooth hover effects") + assert.Contains(t, css, "background-color 100ms ease", + "row transition should specify background-color 100ms ease") +} + +func TestGenerateHistorySidebarCSS_ContainsUppercaseGroupHeader(t *testing.T) { + css := generateHistorySidebarCSS(DefaultDarkPalette()) + + assert.Contains(t, css, "text-transform: uppercase;", + "group header should use text-transform uppercase") + assert.Contains(t, css, "letter-spacing: 0.04em;", + "group header should have letter-spacing") +} + +func TestGenerateHistorySidebarCSS_InGenerateCSSFull_DarkPalette(t *testing.T) { + fullCSS := GenerateCSSFull(DefaultDarkPalette(), 1.0, DefaultFontConfig(), DefaultModeColors()) + + assert.Contains(t, fullCSS, ".history-sidebar-outer {", + "GenerateCSSFull must include history sidebar outer selector for dark palette") + assert.Contains(t, fullCSS, ".history-sidebar-row-title {", + "GenerateCSSFull must include history sidebar row title selector for dark palette") + assert.Contains(t, fullCSS, ".history-sidebar-search {", + "GenerateCSSFull must include history sidebar search selector for dark palette") + assert.Contains(t, fullCSS, ".history-sidebar-empty {", + "GenerateCSSFull must include history sidebar empty selector for dark palette") +} + +func TestGenerateHistorySidebarCSS_InGenerateCSSFull_LightPalette(t *testing.T) { + fullCSS := GenerateCSSFull(DefaultLightPalette(), 1.0, DefaultFontConfig(), DefaultModeColors()) + + assert.Contains(t, fullCSS, ".history-sidebar-outer {", + "GenerateCSSFull must include history sidebar outer selector for light palette") + assert.Contains(t, fullCSS, ".history-sidebar-row-title {", + "GenerateCSSFull must include history sidebar row title selector for light palette") + assert.Contains(t, fullCSS, ".history-sidebar-search {", + "GenerateCSSFull must include history sidebar search selector for light palette") + assert.Contains(t, fullCSS, ".history-sidebar-empty {", + "GenerateCSSFull must include history sidebar empty selector for light palette") +} + +func TestGenerateHistorySidebarCSS_LiveReloadSeamIncludesUpdatedCSS(t *testing.T) { + // GenerateCSSFull is the live theme reload entry point called when + // the palette changes at runtime. This test proves that switching + // palettes produces different history sidebar CSS, confirming the + // full generation path includes and re-generates the sidebar styles. + darkCSS := GenerateCSSFull(DefaultDarkPalette(), 1.0, DefaultFontConfig(), DefaultModeColors()) + lightCSS := GenerateCSSFull(DefaultLightPalette(), 1.0, DefaultFontConfig(), DefaultModeColors()) + + // Both must contain history sidebar selectors. + assert.Contains(t, darkCSS, ".history-sidebar-outer {", "dark CSS must have sidebar styles") + assert.Contains(t, lightCSS, ".history-sidebar-outer {", "light CSS must have sidebar styles") + + // The CSS must differ between palettes (different color values). + if darkCSS == lightCSS { + t.Fatal("dark and light palette should produce different CSS when history sidebar is included") + } + + // The dark and light outputs must both be parseable: each '{' has '}'. + assert.Equal(t, strings.Count(darkCSS, "{"), strings.Count(darkCSS, "}"), + "dark CSS braces must be balanced") + assert.Equal(t, strings.Count(lightCSS, "{"), strings.Count(lightCSS, "}"), + "light CSS braces must be balanced") +} + +func TestGenerateHistorySidebarCSS_InGenerateCSSFull_WithCustomScaleAndFonts(t *testing.T) { + customFonts := FontConfig{ + SansFont: "Noto Sans", + MonospaceFont: "JetBrains Mono", + GtkFont: "Noto Sans", + } + customModeColors := ModeColors{ + PaneMode: "#ff0000", + TabMode: "#00ff00", + SessionMode: "#0000ff", + ResizeMode: "#ffff00", + } + + fullCSS := GenerateCSSFull(DefaultDarkPalette(), 2.0, customFonts, customModeColors) + + assert.Contains(t, fullCSS, ".history-sidebar-outer {", + "GenerateCSSFull with custom scale/fonts must include history sidebar selectors") + assert.Contains(t, fullCSS, ".history-sidebar-row:hover {", + "GenerateCSSFull must include hover state for history rows") + assert.Contains(t, fullCSS, ".history-sidebar-row:selected {", + "GenerateCSSFull must include selected state for history rows") + assert.Contains(t, fullCSS, ".history-sidebar-row:focus {", + "GenerateCSSFull must include focus state for history rows") +} + +// TestGenerateHistorySidebarCSS_LiveReloadFullPath verifies that the +// GenerateCSSFull entry point (used by live theme reload) includes the +// history sidebar CSS with correct palette values. This is the path that +// would be called when the user changes themes at runtime. +func TestGenerateHistorySidebarCSS_LiveReloadFullPath(t *testing.T) { + darkPalette := DefaultDarkPalette() + lightPalette := DefaultLightPalette() + customPalette := Palette{ + Background: "#111111", + Surface: "#222222", + SurfaceVariant: "#333333", + Text: "#eeeeee", + Muted: "#aaaaaa", + Accent: "#ff6600", + Border: "#444444", + Success: "#00cc44", + Warning: "#ffaa00", + Destructive: "#cc2222", + } + + // GenerateCSSFull is the exact function called by the live theme reload + // path. It generates ALL CSS including the history sidebar selectors. + darkCSS := GenerateCSSFull(darkPalette, 1.0, DefaultFontConfig(), DefaultModeColors()) + lightCSS := GenerateCSSFull(lightPalette, 1.0, DefaultFontConfig(), DefaultModeColors()) + customCSS := GenerateCSSFull(customPalette, 1.0, DefaultFontConfig(), DefaultModeColors()) + + // All three must contain the full set of history sidebar selectors. + sidebarSelectors := []string{ + ".history-sidebar-outer", + ".history-sidebar-search-box", + ".history-sidebar-search", + ".history-sidebar-groups", + ".history-sidebar-group-header", + ".history-sidebar-row", + ".history-sidebar-row:hover", + ".history-sidebar-row:selected", + ".history-sidebar-row:focus", + ".history-sidebar-row-title", + ".history-sidebar-row-subtitle", + ".history-sidebar-row-time", + ".history-sidebar-empty", + ".history-sidebar-loading", + } + + for _, sel := range sidebarSelectors { + assert.Contains(t, darkCSS, sel+" {", "dark CSS must have selector %s", sel) + assert.Contains(t, lightCSS, sel+" {", "light CSS must have selector %s", sel) + assert.Contains(t, customCSS, sel+" {", "custom CSS must have selector %s", sel) + } + + // Palette-specific values must differ. + assert.NotEqual(t, darkCSS, lightCSS, "dark and light GenerateCSSFull output must differ") + assert.NotEqual(t, darkCSS, customCSS, "dark and custom GenerateCSSFull output must differ") + assert.NotEqual(t, lightCSS, customCSS, "light and custom GenerateCSSFull output must differ") + + // Sidebar CSS should always use the accent variable, while the :root block + // carries the palette-specific value that changes on live reload. + assert.Contains(t, darkCSS, "alpha(var(--accent), 0.18)") + assert.Contains(t, lightCSS, "alpha(var(--accent), 0.18)") + assert.Contains(t, customCSS, "alpha(var(--accent), 0.18)") + assert.Contains(t, darkCSS, "\n --accent: "+darkPalette.Accent+";\n") + assert.Contains(t, lightCSS, "\n --accent: "+lightPalette.Accent+";\n") + assert.Contains(t, customCSS, "\n --accent: "+customPalette.Accent+";\n") + + // All three outputs must be syntactically valid: every '{' has a matching '}'. + for name, css := range map[string]string{ + "dark": darkCSS, + "light": lightCSS, + "custom": customCSS, + } { + assert.Equal(t, strings.Count(css, "{"), strings.Count(css, "}"), + "%s CSS braces must be balanced", name) + } +} + +// TestGenerateHistorySidebarCSS_LiveReloadPaletteSwitch verifies that +// switching palettes at GenerateCSSFull level updates the history sidebar +// accent values. This simulates what happens when the theme system applies +// a new palette at runtime: the full CSS (including sidebar) is regenerated. +func TestGenerateHistorySidebarCSS_LiveReloadPaletteSwitch(t *testing.T) { + // Start with dark palette. + current := DefaultDarkPalette() + cssBefore := GenerateCSSFull(current, 1.0, DefaultFontConfig(), DefaultModeColors()) + + // Verify the sidebar section uses the shared accent variable. + assert.Contains(t, cssBefore, "alpha(var(--accent), 0.18)", + "initial palette should render sidebar CSS through the accent variable") + assert.Contains(t, cssBefore, "\n --accent: "+current.Accent+";\n") + + // Switch to light palette (simulating runtime theme change). + current = DefaultLightPalette() + cssAfter := GenerateCSSFull(current, 1.0, DefaultFontConfig(), DefaultModeColors()) + + assert.Contains(t, cssAfter, "alpha(var(--accent), 0.18)", + "updated palette should still use the shared accent variable") + assert.Contains(t, cssAfter, "\n --accent: "+current.Accent+";\n") + + // The old palette variable definition must not remain after switching. + darkAccent := DefaultDarkPalette().Accent + if current.Accent != darkAccent { + assert.NotContains(t, cssAfter, "\n --accent: "+darkAccent+";\n") + } + + // History sidebar selectors must be present in both. + assert.Contains(t, cssBefore, ".history-sidebar-row:selected {") + assert.Contains(t, cssAfter, ".history-sidebar-row:selected {") + + // The before and after CSS must be different (palette changed). + assert.NotEqual(t, cssBefore, cssAfter, "CSS must change when palette switches") +} diff --git a/internal/ui/window/main_window.go b/internal/ui/window/main_window.go index a6fa87f0..c8a59457 100644 --- a/internal/ui/window/main_window.go +++ b/internal/ui/window/main_window.go @@ -18,12 +18,24 @@ const ( ) // MainWindow represents the main browser window. +// +// Layout hierarchy: +// +// window +// rootBox (vertical) +// contentOverlay +// contentAreaHBox (horizontal) +// mainContentBox (vertical, expand) ← workspace tab content +// sidebarBox (vertical, fixed width) ← optional sidebar +// tabBar (overlay, not measured) type MainWindow struct { window *gtk.ApplicationWindow rootBox *gtk.Box // Vertical: tab bar + content tabBar *component.TabBar contentOverlay *gtk.Overlay // Overlay for content + omnibox - contentArea *gtk.Box // Container for workspace content + contentAreaBox *gtk.Box // Horizontal: main content + sidebar + mainContentBox *gtk.Box // Vertical container for workspace content + sidebarBox *gtk.Box // Vertical container for sidebar (hidden by default) currentContent *gtk.Widget // Track current content for removal on tab switch tabBarPosition string // "top" or "bottom" @@ -49,10 +61,26 @@ func New(ctx context.Context, app *gtk.Application, tabBarPosition string) (*Mai mw.window.SetTitle(&title) mw.window.SetDefaultSize(defaultWidth, defaultHeight) + if err := mw.initLayoutWidgets(); err != nil { + return nil, err + } + + mw.contentAreaBox.Append(&mw.mainContentBox.Widget) + mw.contentAreaBox.Append(&mw.sidebarBox.Widget) + mw.contentOverlay.SetChild(&mw.contentAreaBox.Widget) + + mw.assembleLayout() + + mw.window.SetChild(&mw.rootBox.Widget) + + return mw, nil +} + +func (mw *MainWindow) initLayoutWidgets() error { mw.rootBox = gtk.NewBox(gtk.OrientationVerticalValue, 0) if mw.rootBox == nil { mw.window.Unref() - return nil, ErrWidgetCreationFailed("rootBox") + return ErrWidgetCreationFailed("rootBox") } mw.rootBox.SetHexpand(true) mw.rootBox.SetVexpand(true) @@ -62,40 +90,77 @@ func New(ctx context.Context, app *gtk.Application, tabBarPosition string) (*Mai if mw.tabBar == nil { mw.rootBox.Unref() mw.window.Unref() - return nil, ErrWidgetCreationFailed("tabBar") + return ErrWidgetCreationFailed("tabBar") + } + + if err := mw.initContentWidgets(); err != nil { + return err } + return nil +} +func (mw *MainWindow) initContentWidgets() error { mw.contentOverlay = gtk.NewOverlay() if mw.contentOverlay == nil { mw.tabBar.Destroy() mw.rootBox.Unref() mw.window.Unref() - return nil, ErrWidgetCreationFailed("contentOverlay") + return ErrWidgetCreationFailed("contentOverlay") } mw.contentOverlay.SetHexpand(true) mw.contentOverlay.SetVexpand(true) mw.contentOverlay.SetVisible(true) - mw.contentArea = gtk.NewBox(gtk.OrientationVerticalValue, 0) - if mw.contentArea == nil { + // Horizontal box to hold both main content and sidebar side by side. + mw.contentAreaBox = gtk.NewBox(gtk.OrientationHorizontalValue, 0) + if mw.contentAreaBox == nil { mw.contentOverlay.Unref() mw.tabBar.Destroy() mw.rootBox.Unref() mw.window.Unref() - return nil, ErrWidgetCreationFailed("contentArea") + return ErrWidgetCreationFailed("contentAreaBox") } - mw.contentArea.SetHexpand(true) - mw.contentArea.SetVexpand(true) - mw.contentArea.SetVisible(true) - mw.contentArea.AddCssClass("content-area") + mw.contentAreaBox.SetHexpand(true) + mw.contentAreaBox.SetVexpand(true) + mw.contentAreaBox.SetVisible(true) - mw.contentOverlay.SetChild(&mw.contentArea.Widget) - - mw.assembleLayout() + if err := mw.initContentBoxes(); err != nil { + return err + } + return nil +} - mw.window.SetChild(&mw.rootBox.Widget) +func (mw *MainWindow) initContentBoxes() error { + // Main content box (vertical) for workspace content. + mw.mainContentBox = gtk.NewBox(gtk.OrientationVerticalValue, 0) + if mw.mainContentBox == nil { + mw.contentAreaBox.Unref() + mw.contentOverlay.Unref() + mw.tabBar.Destroy() + mw.rootBox.Unref() + mw.window.Unref() + return ErrWidgetCreationFailed("mainContentBox") + } + mw.mainContentBox.SetHexpand(true) + mw.mainContentBox.SetVexpand(true) + mw.mainContentBox.SetVisible(true) + + // Sidebar box (hidden by default). + mw.sidebarBox = gtk.NewBox(gtk.OrientationVerticalValue, 0) + if mw.sidebarBox == nil { + mw.mainContentBox.Unref() + mw.contentAreaBox.Unref() + mw.contentOverlay.Unref() + mw.tabBar.Destroy() + mw.rootBox.Unref() + mw.window.Unref() + return ErrWidgetCreationFailed("sidebarBox") + } + mw.sidebarBox.SetHexpand(false) + mw.sidebarBox.SetVexpand(true) + mw.sidebarBox.SetVisible(false) - return mw, nil + return nil } func (mw *MainWindow) assembleLayout() { @@ -147,21 +212,26 @@ func (mw *MainWindow) TabBar() *component.TabBar { return mw.tabBar } -// ContentArea returns the main content container widget. +// ContentArea returns the main content container widget (the vertical +// box where workspace content is placed). func (mw *MainWindow) ContentArea() *gtk.Box { - return mw.contentArea + return mw.mainContentBox } -// SetContent replaces the current content widget with the given widget. +// SetContent replaces the current content widget in the main content area +// (the vertical box that holds workspace tab content). func (mw *MainWindow) SetContent(widget *gtk.Widget) { + if mw == nil || mw.mainContentBox == nil { + return + } if mw.currentContent != nil { - mw.contentArea.Remove(mw.currentContent) + mw.mainContentBox.Remove(mw.currentContent) mw.currentContent = nil } if widget != nil { widget.SetVisible(true) - mw.contentArea.Append(widget) + mw.mainContentBox.Append(widget) mw.currentContent = widget } } @@ -201,30 +271,30 @@ func (mw *MainWindow) ContentOverlay() *gtk.Overlay { } // SetTabBarContentInsetVisible adds or removes the tab bar inset CSS class -// on the content area. Avoids duplicate add/remove if already in the desired state. +// on the main content area. Avoids duplicate add/remove if already in the desired state. func (mw *MainWindow) SetTabBarContentInsetVisible(visible bool) { - if mw.contentArea == nil { + if mw.mainContentBox == nil { return } class := mw.tabBarContentInsetClass() if visible { - if !mw.contentArea.HasCssClass(class) { - mw.contentArea.AddCssClass(class) + if !mw.mainContentBox.HasCssClass(class) { + mw.mainContentBox.AddCssClass(class) } } else { - if mw.contentArea.HasCssClass(class) { - mw.contentArea.RemoveCssClass(class) + if mw.mainContentBox.HasCssClass(class) { + mw.mainContentBox.RemoveCssClass(class) } } } // HasTabBarContentInset returns whether the tab bar inset CSS class is currently -// applied to the content area. +// applied to the main content area. func (mw *MainWindow) HasTabBarContentInset() bool { - if mw.contentArea == nil { + if mw.mainContentBox == nil { return false } - return mw.contentArea.HasCssClass(mw.tabBarContentInsetClass()) + return mw.mainContentBox.HasCssClass(mw.tabBarContentInsetClass()) } func (mw *MainWindow) tabBarContentInsetClass() string { @@ -247,9 +317,17 @@ func (mw *MainWindow) Destroy() { mw.tabBar.Destroy() mw.tabBar = nil } - if mw.contentArea != nil { - mw.contentArea.Unref() - mw.contentArea = nil + if mw.sidebarBox != nil { + mw.sidebarBox.Unref() + mw.sidebarBox = nil + } + if mw.mainContentBox != nil { + mw.mainContentBox.Unref() + mw.mainContentBox = nil + } + if mw.contentAreaBox != nil { + mw.contentAreaBox.Unref() + mw.contentAreaBox = nil } if mw.rootBox != nil { mw.rootBox.Unref() diff --git a/internal/ui/window/main_window_sidebar.go b/internal/ui/window/main_window_sidebar.go new file mode 100644 index 00000000..70c62b4b --- /dev/null +++ b/internal/ui/window/main_window_sidebar.go @@ -0,0 +1,93 @@ +package window + +import ( + "github.com/bnema/puregotk/v4/gtk" +) + +// SidebarWidthConfig defines the initial/recommended width range for the sidebar. +type SidebarWidthConfig struct { + // WidthPx is the preferred sidebar width. + WidthPx int + // MinPx is the minimum clamped width (default 280). + MinPx int + // MaxPx is the maximum clamping bound (default 380). + MaxPx int +} + +// SidebarDefaultWidth returns a sensible default width configuration: +// preferred 320px, clamped to [280, 380]. +func SidebarDefaultWidth() SidebarWidthConfig { + return SidebarWidthConfig{ + WidthPx: 320, + MinPx: 280, + MaxPx: 380, + } +} + +// SetSidebarWidth sets the sidebar box width to widthPx, clamped to the +// config's [MinPx, MaxPx] bounds. Using the zero-value SidebarWidthConfig{} +// sets sensible defaults (320px clamped to [280, 380]). +func (mw *MainWindow) SetSidebarWidth(cfg SidebarWidthConfig) { + if mw.sidebarBox == nil { + return + } + defaults := SidebarDefaultWidth() + if cfg.MinPx == 0 { + cfg.MinPx = defaults.MinPx + } + if cfg.MaxPx == 0 { + cfg.MaxPx = defaults.MaxPx + } + if cfg.WidthPx == 0 { + cfg.WidthPx = defaults.WidthPx + } + if cfg.MinPx > cfg.MaxPx { + mw.logger.Warn().Int("min_px", cfg.MinPx).Int("max_px", cfg.MaxPx).Msg("invalid sidebar width bounds; swapping") + cfg.MinPx, cfg.MaxPx = cfg.MaxPx, cfg.MinPx + } + clamped := cfg.WidthPx + if clamped < cfg.MinPx { + clamped = cfg.MinPx + } + if clamped > cfg.MaxPx { + clamped = cfg.MaxPx + } + mw.sidebarBox.SetSizeRequest(clamped, -1) + mw.logger.Debug().Int("sidebar_width", clamped).Msg("sidebar width set") +} + +// SetSidebarVisible shows or hides the sidebar pane. +func (mw *MainWindow) SetSidebarVisible(visible bool) { + if mw.sidebarBox == nil { + return + } + mw.sidebarBox.SetVisible(visible) + mw.logger.Debug().Bool("sidebar_visible", visible).Msg("sidebar visibility changed") +} + +// IsSidebarVisible returns whether the sidebar pane is currently visible. +func (mw *MainWindow) IsSidebarVisible() bool { + if mw.sidebarBox == nil { + return false + } + return mw.sidebarBox.GetVisible() +} + +// SetSidebarWidget replaces the current sidebar content widget. +func (mw *MainWindow) SetSidebarWidget(widget *gtk.Widget) { + if mw.sidebarBox == nil { + return + } + // Remove existing children + for { + child := mw.sidebarBox.GetFirstChild() + if child == nil { + break + } + mw.sidebarBox.Remove(child) + } + if widget != nil { + widget.SetVisible(true) + mw.sidebarBox.Append(widget) + } +}