main_window_sidebar.go RawHistoryFolder
1package window
2
3import (
4 "github.com/bnema/puregotk/v4/gtk"
5)
6
7// SidebarWidthConfig defines the initial/recommended width range for the sidebar.
8type SidebarWidthConfig struct {
9 // WidthPx is the preferred sidebar width.
10 WidthPx int
11 // MinPx is the minimum clamped width (default 280).
12 MinPx int
13 // MaxPx is the maximum clamping bound (default 380).
14 MaxPx int
15}
16
17// SidebarDefaultWidth returns a sensible default width configuration:
18// preferred 320px, clamped to [280, 380].
19func SidebarDefaultWidth() SidebarWidthConfig {
20 return SidebarWidthConfig{
21 WidthPx: 320,
22 MinPx: 280,
23 MaxPx: 380,
24 }
25}
26
27// SetSidebarWidth sets the sidebar box width to widthPx, clamped to the
28// config's [MinPx, MaxPx] bounds. Using the zero-value SidebarWidthConfig{}
29// sets sensible defaults (320px clamped to [280, 380]).
30func (mw *MainWindow) SetSidebarWidth(cfg SidebarWidthConfig) {
31 if mw.sidebarBox == nil {
32 return
33 }
34 defaults := SidebarDefaultWidth()
35 if cfg.MinPx == 0 {
36 cfg.MinPx = defaults.MinPx
37 }
38 if cfg.MaxPx == 0 {
39 cfg.MaxPx = defaults.MaxPx
40 }
41 if cfg.WidthPx == 0 {
42 cfg.WidthPx = defaults.WidthPx
43 }
44 if cfg.MinPx > cfg.MaxPx {
45 mw.logger.Warn().Int("min_px", cfg.MinPx).Int("max_px", cfg.MaxPx).Msg("invalid sidebar width bounds; swapping")
46 cfg.MinPx, cfg.MaxPx = cfg.MaxPx, cfg.MinPx
47 }
48 clamped := cfg.WidthPx
49 if clamped < cfg.MinPx {
50 clamped = cfg.MinPx
51 }
52 if clamped > cfg.MaxPx {
53 clamped = cfg.MaxPx
54 }
55 mw.sidebarBox.SetSizeRequest(clamped, -1)
56 mw.logger.Debug().Int("sidebar_width", clamped).Msg("sidebar width set")
57}
58
59// SetSidebarVisible shows or hides the sidebar pane.
60func (mw *MainWindow) SetSidebarVisible(visible bool) {
61 if mw.sidebarBox == nil {
62 return
63 }
64 mw.sidebarBox.SetVisible(visible)
65 mw.logger.Debug().Bool("sidebar_visible", visible).Msg("sidebar visibility changed")
66}
67
68// IsSidebarVisible returns whether the sidebar pane is currently visible.
69func (mw *MainWindow) IsSidebarVisible() bool {
70 if mw.sidebarBox == nil {
71 return false
72 }
73 return mw.sidebarBox.GetVisible()
74}
75
76// SetSidebarWidget replaces the current sidebar content widget.
77func (mw *MainWindow) SetSidebarWidget(widget *gtk.Widget) {
78 if mw.sidebarBox == nil {
79 return
80 }
81 // Remove existing children
82 for {
83 child := mw.sidebarBox.GetFirstChild()
84 if child == nil {
85 break
86 }
87 mw.sidebarBox.Remove(child)
88 }
89 if widget != nil {
90 widget.SetVisible(true)
91 mw.sidebarBox.Append(widget)
92 }
93}