refactor: improve focus management and stacked pane stability
- Replace workspace.active with currentlyFocused for cleaner focus tracking - Add dedicated FocusManager and StackedPaneManager for separation of concerns - Implement focus-aware navigation preventing focus stealing in stacked panes - Enhance WebView title updates to refresh stacked pane title bars - Add GTK4 widget validation and proper lifecycle management - Improve widget parenting/unparenting with automatic cleanup - Add WidgetWaitForDraw for better widget operation synchronization - Update all tests to use new currentlyFocused field - Add .pnpm-store to gitignore
- commit
- def2355babcea485b122017b686cc5de2595daa3
- author
- bamen · · 2025-09-24 20:32 UTC
- committer
- bamen · · 2025-09-24 21:53 UTC
- parent
- e2cc33e
- signature
- unsigned
14 files · +3948 -354
M .gitignore +2 -0
| @@ -66,6 +66,8 @@ assets/gui | |||
| 66 | 66 | ||
| 67 | 67 | # Node/TypeScript related | |
| 68 | 68 | node_modules/ | |
| 69 | + | .pnpm-store/ | |
| 70 | + | ||
| 69 | 71 | ||
| 70 | 72 | # Development/tooling files | |
| 71 | 73 | CLAUDE.md | |
| @@ -66,6 +66,8 @@ assets/gui | |||||
| 66 | 66 | ||||
| 67 | # Node/TypeScript related | 67 | # Node/TypeScript related | ||
| 68 | node_modules/ | 68 | node_modules/ | ||
| 69 | + | .pnpm-store/ | |||
| 70 | + | ||||
| 69 | 71 | ||||
| 70 | # Development/tooling files | 72 | # Development/tooling files | ||
| 71 | CLAUDE.md | 73 | CLAUDE.md | ||
M CHANGELOG.md +4 -0
| @@ -16,6 +16,10 @@ All notable changes to this project will be documented in this file. | |||
| 16 | 16 | ### Fixed | |
| 17 | 17 | - **Mobile URL overflow**: Fixed long URLs breaking layout on narrow screens by adding proper width constraints and CSS fixes | |
| 18 | 18 | - **GTK shortcut GLib-CRITICAL error**: Fixed NULL string assertion in shortcut callback system | |
| 19 | + | - **Focus management architecture**: Refactored workspace focus system from `active` field to `currentlyFocused` with dedicated FocusManager for improved reliability and consistency across pane operations | |
| 20 | + | - **Stacked pane focus conflicts**: Fixed focus stealing issues in stacked panes by implementing focus-aware navigation that preserves active pane when spawning siblings | |
| 21 | + | - **WebView title updates**: Enhanced webview title change handling to properly update stacked pane title bars in addition to database storage | |
| 22 | + | - **GTK4 widget lifecycle**: Improved widget parenting/unparenting operations with proper GTK4 validation and automatic cleanup to prevent critical warnings | |
| 19 | 23 | ||
| 20 | 24 | ## [0.10.0] - 2025-09-24 | |
| 21 | 25 | ||
| @@ -16,6 +16,10 @@ All notable changes to this project will be documented in this file. | |||||
| 16 | ### Fixed | 16 | ### Fixed | ||
| 17 | - **Mobile URL overflow**: Fixed long URLs breaking layout on narrow screens by adding proper width constraints and CSS fixes | 17 | - **Mobile URL overflow**: Fixed long URLs breaking layout on narrow screens by adding proper width constraints and CSS fixes | ||
| 18 | - **GTK shortcut GLib-CRITICAL error**: Fixed NULL string assertion in shortcut callback system | 18 | - **GTK shortcut GLib-CRITICAL error**: Fixed NULL string assertion in shortcut callback system | ||
| 19 | + | - **Focus management architecture**: Refactored workspace focus system from `active` field to `currentlyFocused` with dedicated FocusManager for improved reliability and consistency across pane operations | |||
| 20 | + | - **Stacked pane focus conflicts**: Fixed focus stealing issues in stacked panes by implementing focus-aware navigation that preserves active pane when spawning siblings | |||
| 21 | + | - **WebView title updates**: Enhanced webview title change handling to properly update stacked pane title bars in addition to database storage | |||
| 22 | + | - **GTK4 widget lifecycle**: Improved widget parenting/unparenting operations with proper GTK4 validation and automatic cleanup to prevent critical warnings | |||
| 19 | 23 | ||||
| 20 | ## [0.10.0] - 2025-09-24 | 24 | ## [0.10.0] - 2025-09-24 | ||
| 21 | 25 | ||||
A internal/app/browser/focus_manager.go +244 -0
| @@ -0,0 +1,244 @@ | |||
| 1 | + | package browser | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "log" | |
| 5 | + | ||
| 6 | + | "github.com/bnema/dumber/pkg/webkit" | |
| 7 | + | ) | |
| 8 | + | ||
| 9 | + | // FocusManager centralizes all focus/active pane management logic | |
| 10 | + | // This is the Single Source of Truth for pane activation | |
| 11 | + | type FocusManager struct { | |
| 12 | + | wm *WorkspaceManager | |
| 13 | + | ||
| 14 | + | // Callbacks for side-effects | |
| 15 | + | onFocusChanged []FocusChangeCallback | |
| 16 | + | } | |
| 17 | + | ||
| 18 | + | // FocusChangeCallback is called when focus changes between panes | |
| 19 | + | type FocusChangeCallback func(oldPane, newPane *paneNode) | |
| 20 | + | ||
| 21 | + | // NewFocusManager creates a new centralized focus manager | |
| 22 | + | func NewFocusManager(wm *WorkspaceManager) *FocusManager { | |
| 23 | + | return &FocusManager{ | |
| 24 | + | wm: wm, | |
| 25 | + | onFocusChanged: make([]FocusChangeCallback, 0), | |
| 26 | + | } | |
| 27 | + | } | |
| 28 | + | ||
| 29 | + | // AddFocusChangeCallback registers a callback for focus changes | |
| 30 | + | func (fm *FocusManager) AddFocusChangeCallback(callback FocusChangeCallback) { | |
| 31 | + | fm.onFocusChanged = append(fm.onFocusChanged, callback) | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | // SetActivePane is the ONLY method to change the active pane | |
| 35 | + | // All other methods (focusNode, JS bridges, etc.) should call this | |
| 36 | + | func (fm *FocusManager) SetActivePane(node *paneNode) { | |
| 37 | + | if fm == nil { | |
| 38 | + | log.Printf("[focus-manager] WARNING: SetActivePane called on nil FocusManager") | |
| 39 | + | return | |
| 40 | + | } | |
| 41 | + | ||
| 42 | + | if node == nil { | |
| 43 | + | log.Printf("[focus-manager] WARNING: SetActivePane called with nil node") | |
| 44 | + | return | |
| 45 | + | } | |
| 46 | + | ||
| 47 | + | // Validate node exists in the tree | |
| 48 | + | if !fm.isValidNode(node) { | |
| 49 | + | log.Printf("[focus-manager] WARNING: SetActivePane called with invalid node %p", node) | |
| 50 | + | return | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | oldPane := fm.getCurrentActivePane() | |
| 54 | + | ||
| 55 | + | // No-op if already active | |
| 56 | + | if oldPane == node { | |
| 57 | + | log.Printf("[focus-manager] SetActivePane: %p already active", node) | |
| 58 | + | return | |
| 59 | + | } | |
| 60 | + | ||
| 61 | + | log.Printf("[focus-manager] SetActivePane: %p -> %p", oldPane, node) | |
| 62 | + | ||
| 63 | + | // Step 1: GTK4 Focus (Single Source of Truth) | |
| 64 | + | fm.setGTKFocus(node) | |
| 65 | + | ||
| 66 | + | // Step 2: Update visual indicators | |
| 67 | + | fm.updateVisualState(oldPane, node) | |
| 68 | + | ||
| 69 | + | // Step 3: Notify JavaScript bridge | |
| 70 | + | fm.notifyJavaScript(node) | |
| 71 | + | ||
| 72 | + | // Step 4: Execute callbacks | |
| 73 | + | fm.executeCallbacks(oldPane, node) | |
| 74 | + | } | |
| 75 | + | ||
| 76 | + | // GetActivePane returns the currently active pane | |
| 77 | + | // This is derived from GTK4 focus state, not stored separately | |
| 78 | + | func (fm *FocusManager) GetActivePane() *paneNode { | |
| 79 | + | return fm.getCurrentActivePane() | |
| 80 | + | } | |
| 81 | + | ||
| 82 | + | // ===== INTERNAL METHODS ===== | |
| 83 | + | ||
| 84 | + | // setGTKFocus sets the GTK4 focus on the pane's webview | |
| 85 | + | func (fm *FocusManager) setGTKFocus(node *paneNode) { | |
| 86 | + | if node.pane == nil || node.pane.webView == nil { | |
| 87 | + | log.Printf("[focus-manager] ERROR: Cannot focus node with nil pane/webView") | |
| 88 | + | return | |
| 89 | + | } | |
| 90 | + | ||
| 91 | + | // Get the widget handle for the webview | |
| 92 | + | viewWidget := node.pane.webView.Widget() | |
| 93 | + | if viewWidget == 0 { | |
| 94 | + | log.Printf("[focus-manager] ERROR: Cannot focus node with invalid widget") | |
| 95 | + | return | |
| 96 | + | } | |
| 97 | + | ||
| 98 | + | // This is the Single Source of Truth for focus | |
| 99 | + | webkit.WidgetGrabFocus(viewWidget) | |
| 100 | + | } | |
| 101 | + | ||
| 102 | + | // updateVisualState manages CSS classes and visibility | |
| 103 | + | func (fm *FocusManager) updateVisualState(oldPane, newPane *paneNode) { | |
| 104 | + | activePaneClass := "workspace-pane-active" | |
| 105 | + | ||
| 106 | + | // Remove active class from old pane | |
| 107 | + | if oldPane != nil && oldPane.container != 0 { | |
| 108 | + | webkit.WidgetRemoveCSSClass(oldPane.container, activePaneClass) | |
| 109 | + | } | |
| 110 | + | ||
| 111 | + | // Add active class to new pane | |
| 112 | + | if newPane.container != 0 { | |
| 113 | + | webkit.WidgetAddCSSClass(newPane.container, activePaneClass) | |
| 114 | + | } | |
| 115 | + | ||
| 116 | + | // Handle stacked panes visibility | |
| 117 | + | fm.updateStackedPaneVisibility(newPane) | |
| 118 | + | } | |
| 119 | + | ||
| 120 | + | // updateStackedPaneVisibility handles special case for stacked panes | |
| 121 | + | func (fm *FocusManager) updateStackedPaneVisibility(node *paneNode) { | |
| 122 | + | // Find if this node is part of a stack | |
| 123 | + | stackNode := fm.findStackContainer(node) | |
| 124 | + | if stackNode == nil { | |
| 125 | + | return // Not in a stack | |
| 126 | + | } | |
| 127 | + | ||
| 128 | + | // Update stack visibility based on the focused pane | |
| 129 | + | activeIndex := -1 | |
| 130 | + | for i, stackedPane := range stackNode.stackedPanes { | |
| 131 | + | if stackedPane == node { | |
| 132 | + | activeIndex = i | |
| 133 | + | break | |
| 134 | + | } | |
| 135 | + | } | |
| 136 | + | ||
| 137 | + | if activeIndex >= 0 { | |
| 138 | + | stackNode.activeStackIndex = activeIndex | |
| 139 | + | fm.updateStackVisibility(stackNode) | |
| 140 | + | } | |
| 141 | + | } | |
| 142 | + | ||
| 143 | + | // updateStackVisibility updates visibility of panes in a stack | |
| 144 | + | func (fm *FocusManager) updateStackVisibility(stackNode *paneNode) { | |
| 145 | + | activeIndex := stackNode.activeStackIndex | |
| 146 | + | if activeIndex < 0 || activeIndex >= len(stackNode.stackedPanes) { | |
| 147 | + | return | |
| 148 | + | } | |
| 149 | + | ||
| 150 | + | // Hide all panes in stack | |
| 151 | + | for i, pane := range stackNode.stackedPanes { | |
| 152 | + | if i != activeIndex { | |
| 153 | + | webkit.WidgetSetVisible(pane.container, false) | |
| 154 | + | webkit.WidgetSetVisible(pane.titleBar, true) | |
| 155 | + | webkit.WidgetRemoveCSSClass(pane.container, "stacked-pane-active") | |
| 156 | + | webkit.WidgetAddCSSClass(pane.container, "stacked-pane-collapsed") | |
| 157 | + | } | |
| 158 | + | } | |
| 159 | + | ||
| 160 | + | // Show active pane | |
| 161 | + | activePaneNode := stackNode.stackedPanes[activeIndex] | |
| 162 | + | webkit.WidgetSetVisible(activePaneNode.container, true) | |
| 163 | + | webkit.WidgetSetVisible(activePaneNode.titleBar, false) | |
| 164 | + | webkit.WidgetAddCSSClass(activePaneNode.container, "stacked-pane-active") | |
| 165 | + | webkit.WidgetRemoveCSSClass(activePaneNode.container, "stacked-pane-collapsed") | |
| 166 | + | } | |
| 167 | + | ||
| 168 | + | // notifyJavaScript dispatches focus events to the JavaScript bridge | |
| 169 | + | func (fm *FocusManager) notifyJavaScript(node *paneNode) { | |
| 170 | + | if node.pane == nil || node.pane.webView == nil { | |
| 171 | + | return | |
| 172 | + | } | |
| 173 | + | ||
| 174 | + | detail := map[string]any{ | |
| 175 | + | "hasFocus": true, | |
| 176 | + | "paneId": fm.getPaneID(node), | |
| 177 | + | } | |
| 178 | + | ||
| 179 | + | if err := node.pane.webView.DispatchCustomEvent("dumber:workspace-focus", detail); err != nil { | |
| 180 | + | log.Printf("[focus-manager] Failed to dispatch focus event: %v", err) | |
| 181 | + | } | |
| 182 | + | } | |
| 183 | + | ||
| 184 | + | // executeCallbacks runs all registered focus change callbacks | |
| 185 | + | func (fm *FocusManager) executeCallbacks(oldPane, newPane *paneNode) { | |
| 186 | + | for _, callback := range fm.onFocusChanged { | |
| 187 | + | callback(oldPane, newPane) | |
| 188 | + | } | |
| 189 | + | } | |
| 190 | + | ||
| 191 | + | // ===== HELPER METHODS ===== | |
| 192 | + | ||
| 193 | + | // getCurrentActivePane determines the currently active pane from GTK4 focus | |
| 194 | + | func (fm *FocusManager) getCurrentActivePane() *paneNode { | |
| 195 | + | // TODO: Implement proper GTK4 focus traversal | |
| 196 | + | // For now, we'll track this internally until GTK4 focus callbacks are implemented | |
| 197 | + | return fm.wm.currentlyFocused | |
| 198 | + | } | |
| 199 | + | ||
| 200 | + | // isValidNode checks if a node exists in the current tree | |
| 201 | + | func (fm *FocusManager) isValidNode(node *paneNode) bool { | |
| 202 | + | if fm == nil || fm.wm == nil || node == nil || node.pane == nil || node.pane.webView == nil { | |
| 203 | + | return false | |
| 204 | + | } | |
| 205 | + | return fm.wm.viewToNode[node.pane.webView] == node | |
| 206 | + | } | |
| 207 | + | ||
| 208 | + | // findStackContainer finds the stack container that contains this node | |
| 209 | + | func (fm *FocusManager) findStackContainer(node *paneNode) *paneNode { | |
| 210 | + | // Traverse up the tree looking for a stack container | |
| 211 | + | current := node | |
| 212 | + | for current != nil { | |
| 213 | + | if current.stackedPanes != nil { | |
| 214 | + | // This is a stack container, check if it contains our node | |
| 215 | + | for _, stackedPane := range current.stackedPanes { | |
| 216 | + | if stackedPane == node { | |
| 217 | + | return current | |
| 218 | + | } | |
| 219 | + | } | |
| 220 | + | } | |
| 221 | + | // Move up the tree (we'd need parent pointers for this) | |
| 222 | + | // For now, traverse the entire tree | |
| 223 | + | break | |
| 224 | + | } | |
| 225 | + | return nil | |
| 226 | + | } | |
| 227 | + | ||
| 228 | + | // getPaneID gets a unique identifier for a pane (for JS bridge) | |
| 229 | + | func (fm *FocusManager) getPaneID(node *paneNode) string { | |
| 230 | + | if node.pane == nil || node.pane.webView == nil { | |
| 231 | + | return "" | |
| 232 | + | } | |
| 233 | + | // Use the webview ID as a unique identifier | |
| 234 | + | return node.pane.webView.ID() | |
| 235 | + | } | |
| 236 | + | ||
| 237 | + | // SetActivePaneByView sets active pane by WebView reference | |
| 238 | + | func (fm *FocusManager) SetActivePaneByView(view *webkit.WebView) { | |
| 239 | + | if node := fm.wm.viewToNode[view]; node != nil { | |
| 240 | + | fm.SetActivePane(node) | |
| 241 | + | } else { | |
| 242 | + | log.Printf("[focus-manager] WARNING: SetActivePaneByView called with unknown view %p", view) | |
| 243 | + | } | |
| 244 | + | } | |
| @@ -0,0 +1,244 @@ | |||||
| 1 | + | package browser | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "log" | |||
| 5 | + | ||||
| 6 | + | "github.com/bnema/dumber/pkg/webkit" | |||
| 7 | + | ) | |||
| 8 | + | ||||
| 9 | + | // FocusManager centralizes all focus/active pane management logic | |||
| 10 | + | // This is the Single Source of Truth for pane activation | |||
| 11 | + | type FocusManager struct { | |||
| 12 | + | wm *WorkspaceManager | |||
| 13 | + | ||||
| 14 | + | // Callbacks for side-effects | |||
| 15 | + | onFocusChanged []FocusChangeCallback | |||
| 16 | + | } | |||
| 17 | + | ||||
| 18 | + | // FocusChangeCallback is called when focus changes between panes | |||
| 19 | + | type FocusChangeCallback func(oldPane, newPane *paneNode) | |||
| 20 | + | ||||
| 21 | + | // NewFocusManager creates a new centralized focus manager | |||
| 22 | + | func NewFocusManager(wm *WorkspaceManager) *FocusManager { | |||
| 23 | + | return &FocusManager{ | |||
| 24 | + | wm: wm, | |||
| 25 | + | onFocusChanged: make([]FocusChangeCallback, 0), | |||
| 26 | + | } | |||
| 27 | + | } | |||
| 28 | + | ||||
| 29 | + | // AddFocusChangeCallback registers a callback for focus changes | |||
| 30 | + | func (fm *FocusManager) AddFocusChangeCallback(callback FocusChangeCallback) { | |||
| 31 | + | fm.onFocusChanged = append(fm.onFocusChanged, callback) | |||
| 32 | + | } | |||
| 33 | + | ||||
| 34 | + | // SetActivePane is the ONLY method to change the active pane | |||
| 35 | + | // All other methods (focusNode, JS bridges, etc.) should call this | |||
| 36 | + | func (fm *FocusManager) SetActivePane(node *paneNode) { | |||
| 37 | + | if fm == nil { | |||
| 38 | + | log.Printf("[focus-manager] WARNING: SetActivePane called on nil FocusManager") | |||
| 39 | + | return | |||
| 40 | + | } | |||
| 41 | + | ||||
| 42 | + | if node == nil { | |||
| 43 | + | log.Printf("[focus-manager] WARNING: SetActivePane called with nil node") | |||
| 44 | + | return | |||
| 45 | + | } | |||
| 46 | + | ||||
| 47 | + | // Validate node exists in the tree | |||
| 48 | + | if !fm.isValidNode(node) { | |||
| 49 | + | log.Printf("[focus-manager] WARNING: SetActivePane called with invalid node %p", node) | |||
| 50 | + | return | |||
| 51 | + | } | |||
| 52 | + | ||||
| 53 | + | oldPane := fm.getCurrentActivePane() | |||
| 54 | + | ||||
| 55 | + | // No-op if already active | |||
| 56 | + | if oldPane == node { | |||
| 57 | + | log.Printf("[focus-manager] SetActivePane: %p already active", node) | |||
| 58 | + | return | |||
| 59 | + | } | |||
| 60 | + | ||||
| 61 | + | log.Printf("[focus-manager] SetActivePane: %p -> %p", oldPane, node) | |||
| 62 | + | ||||
| 63 | + | // Step 1: GTK4 Focus (Single Source of Truth) | |||
| 64 | + | fm.setGTKFocus(node) | |||
| 65 | + | ||||
| 66 | + | // Step 2: Update visual indicators | |||
| 67 | + | fm.updateVisualState(oldPane, node) | |||
| 68 | + | ||||
| 69 | + | // Step 3: Notify JavaScript bridge | |||
| 70 | + | fm.notifyJavaScript(node) | |||
| 71 | + | ||||
| 72 | + | // Step 4: Execute callbacks | |||
| 73 | + | fm.executeCallbacks(oldPane, node) | |||
| 74 | + | } | |||
| 75 | + | ||||
| 76 | + | // GetActivePane returns the currently active pane | |||
| 77 | + | // This is derived from GTK4 focus state, not stored separately | |||
| 78 | + | func (fm *FocusManager) GetActivePane() *paneNode { | |||
| 79 | + | return fm.getCurrentActivePane() | |||
| 80 | + | } | |||
| 81 | + | ||||
| 82 | + | // ===== INTERNAL METHODS ===== | |||
| 83 | + | ||||
| 84 | + | // setGTKFocus sets the GTK4 focus on the pane's webview | |||
| 85 | + | func (fm *FocusManager) setGTKFocus(node *paneNode) { | |||
| 86 | + | if node.pane == nil || node.pane.webView == nil { | |||
| 87 | + | log.Printf("[focus-manager] ERROR: Cannot focus node with nil pane/webView") | |||
| 88 | + | return | |||
| 89 | + | } | |||
| 90 | + | ||||
| 91 | + | // Get the widget handle for the webview | |||
| 92 | + | viewWidget := node.pane.webView.Widget() | |||
| 93 | + | if viewWidget == 0 { | |||
| 94 | + | log.Printf("[focus-manager] ERROR: Cannot focus node with invalid widget") | |||
| 95 | + | return | |||
| 96 | + | } | |||
| 97 | + | ||||
| 98 | + | // This is the Single Source of Truth for focus | |||
| 99 | + | webkit.WidgetGrabFocus(viewWidget) | |||
| 100 | + | } | |||
| 101 | + | ||||
| 102 | + | // updateVisualState manages CSS classes and visibility | |||
| 103 | + | func (fm *FocusManager) updateVisualState(oldPane, newPane *paneNode) { | |||
| 104 | + | activePaneClass := "workspace-pane-active" | |||
| 105 | + | ||||
| 106 | + | // Remove active class from old pane | |||
| 107 | + | if oldPane != nil && oldPane.container != 0 { | |||
| 108 | + | webkit.WidgetRemoveCSSClass(oldPane.container, activePaneClass) | |||
| 109 | + | } | |||
| 110 | + | ||||
| 111 | + | // Add active class to new pane | |||
| 112 | + | if newPane.container != 0 { | |||
| 113 | + | webkit.WidgetAddCSSClass(newPane.container, activePaneClass) | |||
| 114 | + | } | |||
| 115 | + | ||||
| 116 | + | // Handle stacked panes visibility | |||
| 117 | + | fm.updateStackedPaneVisibility(newPane) | |||
| 118 | + | } | |||
| 119 | + | ||||
| 120 | + | // updateStackedPaneVisibility handles special case for stacked panes | |||
| 121 | + | func (fm *FocusManager) updateStackedPaneVisibility(node *paneNode) { | |||
| 122 | + | // Find if this node is part of a stack | |||
| 123 | + | stackNode := fm.findStackContainer(node) | |||
| 124 | + | if stackNode == nil { | |||
| 125 | + | return // Not in a stack | |||
| 126 | + | } | |||
| 127 | + | ||||
| 128 | + | // Update stack visibility based on the focused pane | |||
| 129 | + | activeIndex := -1 | |||
| 130 | + | for i, stackedPane := range stackNode.stackedPanes { | |||
| 131 | + | if stackedPane == node { | |||
| 132 | + | activeIndex = i | |||
| 133 | + | break | |||
| 134 | + | } | |||
| 135 | + | } | |||
| 136 | + | ||||
| 137 | + | if activeIndex >= 0 { | |||
| 138 | + | stackNode.activeStackIndex = activeIndex | |||
| 139 | + | fm.updateStackVisibility(stackNode) | |||
| 140 | + | } | |||
| 141 | + | } | |||
| 142 | + | ||||
| 143 | + | // updateStackVisibility updates visibility of panes in a stack | |||
| 144 | + | func (fm *FocusManager) updateStackVisibility(stackNode *paneNode) { | |||
| 145 | + | activeIndex := stackNode.activeStackIndex | |||
| 146 | + | if activeIndex < 0 || activeIndex >= len(stackNode.stackedPanes) { | |||
| 147 | + | return | |||
| 148 | + | } | |||
| 149 | + | ||||
| 150 | + | // Hide all panes in stack | |||
| 151 | + | for i, pane := range stackNode.stackedPanes { | |||
| 152 | + | if i != activeIndex { | |||
| 153 | + | webkit.WidgetSetVisible(pane.container, false) | |||
| 154 | + | webkit.WidgetSetVisible(pane.titleBar, true) | |||
| 155 | + | webkit.WidgetRemoveCSSClass(pane.container, "stacked-pane-active") | |||
| 156 | + | webkit.WidgetAddCSSClass(pane.container, "stacked-pane-collapsed") | |||
| 157 | + | } | |||
| 158 | + | } | |||
| 159 | + | ||||
| 160 | + | // Show active pane | |||
| 161 | + | activePaneNode := stackNode.stackedPanes[activeIndex] | |||
| 162 | + | webkit.WidgetSetVisible(activePaneNode.container, true) | |||
| 163 | + | webkit.WidgetSetVisible(activePaneNode.titleBar, false) | |||
| 164 | + | webkit.WidgetAddCSSClass(activePaneNode.container, "stacked-pane-active") | |||
| 165 | + | webkit.WidgetRemoveCSSClass(activePaneNode.container, "stacked-pane-collapsed") | |||
| 166 | + | } | |||
| 167 | + | ||||
| 168 | + | // notifyJavaScript dispatches focus events to the JavaScript bridge | |||
| 169 | + | func (fm *FocusManager) notifyJavaScript(node *paneNode) { | |||
| 170 | + | if node.pane == nil || node.pane.webView == nil { | |||
| 171 | + | return | |||
| 172 | + | } | |||
| 173 | + | ||||
| 174 | + | detail := map[string]any{ | |||
| 175 | + | "hasFocus": true, | |||
| 176 | + | "paneId": fm.getPaneID(node), | |||
| 177 | + | } | |||
| 178 | + | ||||
| 179 | + | if err := node.pane.webView.DispatchCustomEvent("dumber:workspace-focus", detail); err != nil { | |||
| 180 | + | log.Printf("[focus-manager] Failed to dispatch focus event: %v", err) | |||
| 181 | + | } | |||
| 182 | + | } | |||
| 183 | + | ||||
| 184 | + | // executeCallbacks runs all registered focus change callbacks | |||
| 185 | + | func (fm *FocusManager) executeCallbacks(oldPane, newPane *paneNode) { | |||
| 186 | + | for _, callback := range fm.onFocusChanged { | |||
| 187 | + | callback(oldPane, newPane) | |||
| 188 | + | } | |||
| 189 | + | } | |||
| 190 | + | ||||
| 191 | + | // ===== HELPER METHODS ===== | |||
| 192 | + | ||||
| 193 | + | // getCurrentActivePane determines the currently active pane from GTK4 focus | |||
| 194 | + | func (fm *FocusManager) getCurrentActivePane() *paneNode { | |||
| 195 | + | // TODO: Implement proper GTK4 focus traversal | |||
| 196 | + | // For now, we'll track this internally until GTK4 focus callbacks are implemented | |||
| 197 | + | return fm.wm.currentlyFocused | |||
| 198 | + | } | |||
| 199 | + | ||||
| 200 | + | // isValidNode checks if a node exists in the current tree | |||
| 201 | + | func (fm *FocusManager) isValidNode(node *paneNode) bool { | |||
| 202 | + | if fm == nil || fm.wm == nil || node == nil || node.pane == nil || node.pane.webView == nil { | |||
| 203 | + | return false | |||
| 204 | + | } | |||
| 205 | + | return fm.wm.viewToNode[node.pane.webView] == node | |||
| 206 | + | } | |||
| 207 | + | ||||
| 208 | + | // findStackContainer finds the stack container that contains this node | |||
| 209 | + | func (fm *FocusManager) findStackContainer(node *paneNode) *paneNode { | |||
| 210 | + | // Traverse up the tree looking for a stack container | |||
| 211 | + | current := node | |||
| 212 | + | for current != nil { | |||
| 213 | + | if current.stackedPanes != nil { | |||
| 214 | + | // This is a stack container, check if it contains our node | |||
| 215 | + | for _, stackedPane := range current.stackedPanes { | |||
| 216 | + | if stackedPane == node { | |||
| 217 | + | return current | |||
| 218 | + | } | |||
| 219 | + | } | |||
| 220 | + | } | |||
| 221 | + | // Move up the tree (we'd need parent pointers for this) | |||
| 222 | + | // For now, traverse the entire tree | |||
| 223 | + | break | |||
| 224 | + | } | |||
| 225 | + | return nil | |||
| 226 | + | } | |||
| 227 | + | ||||
| 228 | + | // getPaneID gets a unique identifier for a pane (for JS bridge) | |||
| 229 | + | func (fm *FocusManager) getPaneID(node *paneNode) string { | |||
| 230 | + | if node.pane == nil || node.pane.webView == nil { | |||
| 231 | + | return "" | |||
| 232 | + | } | |||
| 233 | + | // Use the webview ID as a unique identifier | |||
| 234 | + | return node.pane.webView.ID() | |||
| 235 | + | } | |||
| 236 | + | ||||
| 237 | + | // SetActivePaneByView sets active pane by WebView reference | |||
| 238 | + | func (fm *FocusManager) SetActivePaneByView(view *webkit.WebView) { | |||
| 239 | + | if node := fm.wm.viewToNode[view]; node != nil { | |||
| 240 | + | fm.SetActivePane(node) | |||
| 241 | + | } else { | |||
| 242 | + | log.Printf("[focus-manager] WARNING: SetActivePaneByView called with unknown view %p", view) | |||
| 243 | + | } | |||
| 244 | + | } | |||
M internal/app/browser/shortcuts.go +2 -2
| @@ -44,7 +44,7 @@ func (s *ShortcutHandler) isActivePane() bool { | |||
| 44 | 44 | ||
| 45 | 45 | // For related popups, they're active if parent chain contains the active node | |
| 46 | 46 | if node.isRelated && node.parentPane != nil { | |
| 47 | - | activeNode := s.app.workspace.active | |
| 47 | + | activeNode := s.app.workspace.currentlyFocused | |
| 48 | 48 | cur := node | |
| 49 | 49 | for cur != nil { | |
| 50 | 50 | if cur == activeNode { | |
| @@ -56,7 +56,7 @@ func (s *ShortcutHandler) isActivePane() bool { | |||
| 56 | 56 | } | |
| 57 | 57 | ||
| 58 | 58 | // Independent panes: equal to active node | |
| 59 | - | return node == s.app.workspace.active | |
| 59 | + | return node == s.app.workspace.currentlyFocused | |
| 60 | 60 | } | |
| 61 | 61 | ||
| 62 | 62 | // RegisterShortcuts registers pane-specific keyboard shortcuts with focus guards | |
| @@ -44,7 +44,7 @@ func (s *ShortcutHandler) isActivePane() bool { | |||||
| 44 | 44 | ||||
| 45 | // For related popups, they're active if parent chain contains the active node | 45 | // For related popups, they're active if parent chain contains the active node | ||
| 46 | if node.isRelated && node.parentPane != nil { | 46 | if node.isRelated && node.parentPane != nil { | ||
| 47 | - | activeNode := s.app.workspace.active | 47 | + | activeNode := s.app.workspace.currentlyFocused |
| 48 | cur := node | 48 | cur := node | ||
| 49 | for cur != nil { | 49 | for cur != nil { | ||
| 50 | if cur == activeNode { | 50 | if cur == activeNode { | ||
| @@ -56,7 +56,7 @@ func (s *ShortcutHandler) isActivePane() bool { | |||||
| 56 | } | 56 | } | ||
| 57 | 57 | ||||
| 58 | // Independent panes: equal to active node | 58 | // Independent panes: equal to active node | ||
| 59 | - | return node == s.app.workspace.active | 59 | + | return node == s.app.workspace.currentlyFocused |
| 60 | } | 60 | } | ||
| 61 | 61 | ||||
| 62 | // RegisterShortcuts registers pane-specific keyboard shortcuts with focus guards | 62 | // RegisterShortcuts registers pane-specific keyboard shortcuts with focus guards | ||
A internal/app/browser/stacked_panes.go +677 -0
| @@ -0,0 +1,677 @@ | |||
| 1 | + | package browser | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "errors" | |
| 5 | + | "fmt" | |
| 6 | + | "log" | |
| 7 | + | "time" | |
| 8 | + | ||
| 9 | + | "github.com/bnema/dumber/pkg/webkit" | |
| 10 | + | ) | |
| 11 | + | ||
| 12 | + | // StackedPaneManager handles all stacked pane operations | |
| 13 | + | type StackedPaneManager struct { | |
| 14 | + | wm *WorkspaceManager | |
| 15 | + | } | |
| 16 | + | ||
| 17 | + | // NewStackedPaneManager creates a new stacked pane manager | |
| 18 | + | func NewStackedPaneManager(wm *WorkspaceManager) *StackedPaneManager { | |
| 19 | + | return &StackedPaneManager{ | |
| 20 | + | wm: wm, | |
| 21 | + | } | |
| 22 | + | } | |
| 23 | + | ||
| 24 | + | // StackPane creates a new pane stacked on top of the target pane. | |
| 25 | + | // This is the main entry point for creating stacked panes. | |
| 26 | + | func (spm *StackedPaneManager) StackPane(target *paneNode) (*paneNode, error) { | |
| 27 | + | if target == nil || !target.isLeaf || target.pane == nil { | |
| 28 | + | return nil, errors.New("stack target must be a leaf pane") | |
| 29 | + | } | |
| 30 | + | ||
| 31 | + | // Create the new pane first | |
| 32 | + | newLeaf, err := spm.prepareNewStackedPane() | |
| 33 | + | if err != nil { | |
| 34 | + | return nil, fmt.Errorf("failed to prepare new stacked pane: %v", err) | |
| 35 | + | } | |
| 36 | + | ||
| 37 | + | var stackNode *paneNode | |
| 38 | + | var insertIndex int | |
| 39 | + | ||
| 40 | + | // Check if target is already in a stack | |
| 41 | + | if target.parent != nil && target.parent.isStacked { | |
| 42 | + | // Target is already in a stack - add to existing stack | |
| 43 | + | stackNode, insertIndex, err = spm.addPaneToExistingStack(target, newLeaf) | |
| 44 | + | if err != nil { | |
| 45 | + | return nil, fmt.Errorf("failed to add pane to existing stack: %v", err) | |
| 46 | + | } | |
| 47 | + | } else { | |
| 48 | + | // Target is not stacked - create initial stack | |
| 49 | + | stackNode, insertIndex, err = spm.convertToStackedContainer(target, newLeaf) | |
| 50 | + | if err != nil { | |
| 51 | + | return nil, fmt.Errorf("failed to convert to stacked container: %v", err) | |
| 52 | + | } | |
| 53 | + | } | |
| 54 | + | ||
| 55 | + | // Finalize the stack creation | |
| 56 | + | return spm.finalizeStackCreation(stackNode, newLeaf, insertIndex) | |
| 57 | + | } | |
| 58 | + | ||
| 59 | + | // prepareNewStackedPane creates a new pane and its container | |
| 60 | + | func (spm *StackedPaneManager) prepareNewStackedPane() (*paneNode, error) { | |
| 61 | + | newView, err := spm.wm.createWebView() | |
| 62 | + | if err != nil { | |
| 63 | + | return nil, err | |
| 64 | + | } | |
| 65 | + | ||
| 66 | + | newPane, err := spm.wm.createPane(newView) | |
| 67 | + | if err != nil { | |
| 68 | + | return nil, err | |
| 69 | + | } | |
| 70 | + | ||
| 71 | + | if handler := newPane.MessageHandler(); handler != nil { | |
| 72 | + | handler.SetWorkspaceObserver(spm.wm) | |
| 73 | + | } | |
| 74 | + | ||
| 75 | + | newContainer := newPane.webView.RootWidget() | |
| 76 | + | if newContainer == 0 { | |
| 77 | + | return nil, errors.New("new pane missing container") | |
| 78 | + | } | |
| 79 | + | webkit.WidgetSetHExpand(newContainer, true) | |
| 80 | + | webkit.WidgetSetVExpand(newContainer, true) | |
| 81 | + | webkit.WidgetRealizeInContainer(newContainer) | |
| 82 | + | ||
| 83 | + | // Create the new leaf node | |
| 84 | + | newLeaf := &paneNode{ | |
| 85 | + | pane: newPane, | |
| 86 | + | container: newContainer, | |
| 87 | + | isLeaf: true, | |
| 88 | + | } | |
| 89 | + | ||
| 90 | + | // Create title bar for the new pane | |
| 91 | + | newTitleBar := spm.createTitleBar(newLeaf) | |
| 92 | + | newLeaf.titleBar = newTitleBar | |
| 93 | + | ||
| 94 | + | // Keep new container hidden initially - will be shown after transition | |
| 95 | + | webkit.WidgetSetVisible(newContainer, false) | |
| 96 | + | webkit.WidgetSetVisible(newTitleBar, false) | |
| 97 | + | ||
| 98 | + | return newLeaf, nil | |
| 99 | + | } | |
| 100 | + | ||
| 101 | + | // addPaneToExistingStack adds a new pane to an existing stack | |
| 102 | + | func (spm *StackedPaneManager) addPaneToExistingStack(target, newLeaf *paneNode) (*paneNode, int, error) { | |
| 103 | + | stackNode := target.parent | |
| 104 | + | ||
| 105 | + | // Find the current position of the target pane in the stack | |
| 106 | + | currentIndex := -1 | |
| 107 | + | for i, pane := range stackNode.stackedPanes { | |
| 108 | + | if pane == target { | |
| 109 | + | currentIndex = i | |
| 110 | + | break | |
| 111 | + | } | |
| 112 | + | } | |
| 113 | + | ||
| 114 | + | if currentIndex == -1 { | |
| 115 | + | return nil, 0, errors.New("target pane not found in stack") | |
| 116 | + | } | |
| 117 | + | ||
| 118 | + | // Insert the new pane right after the current pane | |
| 119 | + | insertIndex := currentIndex + 1 | |
| 120 | + | log.Printf("[workspace] adding to existing stack: currentIndex=%d insertIndex=%d stackSize=%d", | |
| 121 | + | currentIndex, insertIndex, len(stackNode.stackedPanes)) | |
| 122 | + | ||
| 123 | + | // Set parent relationship | |
| 124 | + | newLeaf.parent = stackNode | |
| 125 | + | ||
| 126 | + | // Insert the new pane at the correct position in the slice | |
| 127 | + | stackNode.stackedPanes = append(stackNode.stackedPanes, nil) // Expand slice | |
| 128 | + | copy(stackNode.stackedPanes[insertIndex+1:], stackNode.stackedPanes[insertIndex:]) // Shift elements | |
| 129 | + | stackNode.stackedPanes[insertIndex] = newLeaf // Insert new pane | |
| 130 | + | ||
| 131 | + | // Unparent the new container before adding it to the stack (only if it has a parent) | |
| 132 | + | if webkit.WidgetGetParent(newLeaf.container) != 0 { | |
| 133 | + | webkit.WidgetUnparent(newLeaf.container) | |
| 134 | + | } | |
| 135 | + | ||
| 136 | + | // Add the new widgets to the internal stack box (not the wrapper) | |
| 137 | + | webkit.BoxAppend(stackNode.stackWrapper, newLeaf.titleBar) | |
| 138 | + | webkit.BoxAppend(stackNode.stackWrapper, newLeaf.container) | |
| 139 | + | ||
| 140 | + | return stackNode, insertIndex, nil | |
| 141 | + | } | |
| 142 | + | ||
| 143 | + | // convertToStackedContainer converts a simple pane to a stacked container | |
| 144 | + | func (spm *StackedPaneManager) convertToStackedContainer(target, newLeaf *paneNode) (*paneNode, int, error) { | |
| 145 | + | log.Printf("[workspace] converting pane to stacked: %p", target) | |
| 146 | + | ||
| 147 | + | // Create the wrapper container - this is what will be used by splitNode | |
| 148 | + | stackWrapperContainer := webkit.NewBox(webkit.OrientationVertical, 0) | |
| 149 | + | if stackWrapperContainer == 0 { | |
| 150 | + | return nil, 0, errors.New("failed to create stack wrapper container") | |
| 151 | + | } | |
| 152 | + | webkit.WidgetSetHExpand(stackWrapperContainer, true) | |
| 153 | + | webkit.WidgetSetVExpand(stackWrapperContainer, true) | |
| 154 | + | ||
| 155 | + | // Create the internal box for the actual stacked widgets (titles + webviews) | |
| 156 | + | stackInternalBox := webkit.NewBox(webkit.OrientationVertical, 0) | |
| 157 | + | if stackInternalBox == 0 { | |
| 158 | + | return nil, 0, errors.New("failed to create stack internal box") | |
| 159 | + | } | |
| 160 | + | webkit.WidgetSetHExpand(stackInternalBox, true) | |
| 161 | + | webkit.WidgetSetVExpand(stackInternalBox, true) | |
| 162 | + | ||
| 163 | + | // The internal box goes inside the wrapper | |
| 164 | + | webkit.BoxAppend(stackWrapperContainer, stackInternalBox) | |
| 165 | + | ||
| 166 | + | // Get the existing container and parent info | |
| 167 | + | existingContainer := target.container | |
| 168 | + | parent := target.parent | |
| 169 | + | ||
| 170 | + | // Create title bar for the existing pane | |
| 171 | + | titleBar := spm.createTitleBar(target) | |
| 172 | + | ||
| 173 | + | // Keep existing container visible during transition to prevent rendering glitch | |
| 174 | + | webkit.WidgetSetVisible(titleBar, false) | |
| 175 | + | ||
| 176 | + | // Detach existing container from its current parent first | |
| 177 | + | spm.detachFromParent(target, parent) | |
| 178 | + | ||
| 179 | + | // Build the complete stack structure with hidden widgets | |
| 180 | + | webkit.BoxAppend(stackInternalBox, titleBar) | |
| 181 | + | webkit.BoxAppend(stackInternalBox, existingContainer) | |
| 182 | + | ||
| 183 | + | // Immediately reattach the stack wrapper to minimize visibility gap | |
| 184 | + | spm.reattachToParent(stackWrapperContainer, target, parent) | |
| 185 | + | ||
| 186 | + | // Convert target to a stacked leaf node | |
| 187 | + | target.isStacked = true | |
| 188 | + | target.isLeaf = true | |
| 189 | + | target.container = existingContainer | |
| 190 | + | target.titleBar = titleBar | |
| 191 | + | ||
| 192 | + | // Create the stack container node - container points to wrapper, stackWrapper points to internal box | |
| 193 | + | stackNode := &paneNode{ | |
| 194 | + | isStacked: true, | |
| 195 | + | isLeaf: false, | |
| 196 | + | container: stackWrapperContainer, // Wrapper for GTK operations (splits, etc.) | |
| 197 | + | stackWrapper: stackInternalBox, // Internal box for stack operations | |
| 198 | + | stackedPanes: []*paneNode{target}, | |
| 199 | + | activeStackIndex: 0, // KEEP CURRENT PANE ACTIVE during transition (index 0) | |
| 200 | + | parent: parent, | |
| 201 | + | } | |
| 202 | + | ||
| 203 | + | // Update target's parent to be the stack node | |
| 204 | + | target.parent = stackNode | |
| 205 | + | ||
| 206 | + | // Update parent references (GTK operations already done above) | |
| 207 | + | if parent == nil { | |
| 208 | + | spm.wm.root = stackNode | |
| 209 | + | } else { | |
| 210 | + | if parent.left == target { | |
| 211 | + | parent.left = stackNode | |
| 212 | + | } else if parent.right == target { | |
| 213 | + | parent.right = stackNode | |
| 214 | + | } | |
| 215 | + | } | |
| 216 | + | ||
| 217 | + | // Set up the new pane | |
| 218 | + | newLeaf.parent = stackNode | |
| 219 | + | ||
| 220 | + | // Insert the new pane at index 1 (after the original pane) | |
| 221 | + | insertIndex := 1 | |
| 222 | + | stackNode.stackedPanes = append(stackNode.stackedPanes, newLeaf) | |
| 223 | + | ||
| 224 | + | // Unparent the new container before adding it to the stack | |
| 225 | + | if webkit.WidgetGetParent(newLeaf.container) != 0 { | |
| 226 | + | webkit.WidgetUnparent(newLeaf.container) | |
| 227 | + | } | |
| 228 | + | ||
| 229 | + | // Add the new widgets to the internal stack box | |
| 230 | + | webkit.BoxAppend(stackNode.stackWrapper, newLeaf.titleBar) | |
| 231 | + | webkit.BoxAppend(stackNode.stackWrapper, newLeaf.container) | |
| 232 | + | ||
| 233 | + | return stackNode, insertIndex, nil | |
| 234 | + | } | |
| 235 | + | ||
| 236 | + | // detachFromParent removes a pane from its current parent | |
| 237 | + | func (spm *StackedPaneManager) detachFromParent(target *paneNode, parent *paneNode) { | |
| 238 | + | if parent == nil { | |
| 239 | + | // Target is the root - remove it from window | |
| 240 | + | if spm.wm.window != nil { | |
| 241 | + | spm.wm.window.SetChild(0) | |
| 242 | + | } | |
| 243 | + | // Unparent if it has a GTK parent | |
| 244 | + | if webkit.WidgetGetParent(target.container) != 0 { | |
| 245 | + | webkit.WidgetUnparent(target.container) | |
| 246 | + | } | |
| 247 | + | } else if parent.container != 0 { | |
| 248 | + | // Target has a parent paned - remove it (automatically unparents in GTK4) | |
| 249 | + | if parent.left == target { | |
| 250 | + | webkit.PanedSetStartChild(parent.container, 0) | |
| 251 | + | } else if parent.right == target { | |
| 252 | + | webkit.PanedSetEndChild(parent.container, 0) | |
| 253 | + | } | |
| 254 | + | } | |
| 255 | + | } | |
| 256 | + | ||
| 257 | + | // reattachToParent attaches a container to the parent | |
| 258 | + | func (spm *StackedPaneManager) reattachToParent(container uintptr, target *paneNode, parent *paneNode) { | |
| 259 | + | if parent == nil { | |
| 260 | + | if spm.wm.window != nil { | |
| 261 | + | spm.wm.window.SetChild(container) | |
| 262 | + | } | |
| 263 | + | } else if parent.container != 0 { | |
| 264 | + | if parent.left == target { | |
| 265 | + | webkit.PanedSetStartChild(parent.container, container) | |
| 266 | + | } else if parent.right == target { | |
| 267 | + | webkit.PanedSetEndChild(parent.container, container) | |
| 268 | + | } | |
| 269 | + | } | |
| 270 | + | } | |
| 271 | + | ||
| 272 | + | // finalizeStackCreation completes the stack creation process | |
| 273 | + | func (spm *StackedPaneManager) finalizeStackCreation(stackNode, newLeaf *paneNode, insertIndex int) (*paneNode, error) { | |
| 274 | + | // Update workspace state first | |
| 275 | + | spm.wm.viewToNode[newLeaf.pane.webView] = newLeaf | |
| 276 | + | spm.wm.ensureHover(newLeaf) | |
| 277 | + | spm.wm.app.panes = append(spm.wm.app.panes, newLeaf.pane) | |
| 278 | + | if newLeaf.pane.zoomController != nil { | |
| 279 | + | newLeaf.pane.zoomController.ApplyInitialZoom() | |
| 280 | + | } | |
| 281 | + | ||
| 282 | + | // Update CSS classes | |
| 283 | + | spm.wm.ensurePaneBaseClasses() | |
| 284 | + | ||
| 285 | + | // Mark stack operation timestamp to prevent focus conflicts | |
| 286 | + | spm.wm.lastStackOperation = time.Now() | |
| 287 | + | ||
| 288 | + | // Update stack visibility to show current state (existing pane still active) | |
| 289 | + | spm.UpdateStackVisibility(stackNode) | |
| 290 | + | ||
| 291 | + | // Transition to the new pane after a brief delay to avoid rendering conflicts | |
| 292 | + | webkit.IdleAdd(func() bool { | |
| 293 | + | // Now switch to the new pane | |
| 294 | + | stackNode.activeStackIndex = insertIndex | |
| 295 | + | spm.UpdateStackVisibility(stackNode) | |
| 296 | + | spm.wm.currentlyFocused = newLeaf | |
| 297 | + | spm.wm.focusManager.SetActivePane(newLeaf) | |
| 298 | + | return false // Remove idle callback | |
| 299 | + | }) | |
| 300 | + | ||
| 301 | + | log.Printf("[workspace] stacked new pane: stackNode=%p newLeaf=%p stackSize=%d activeIndex=%d insertIndex=%d", | |
| 302 | + | stackNode, newLeaf, len(stackNode.stackedPanes), stackNode.activeStackIndex, insertIndex) | |
| 303 | + | return newLeaf, nil | |
| 304 | + | } | |
| 305 | + | ||
| 306 | + | // UpdateStackVisibility updates the visibility of panes in a stack | |
| 307 | + | func (spm *StackedPaneManager) UpdateStackVisibility(stackNode *paneNode) { | |
| 308 | + | log.Printf("[workspace] updateStackVisibility called: stackNode=%p", stackNode) | |
| 309 | + | ||
| 310 | + | if stackNode == nil { | |
| 311 | + | log.Printf("[workspace] updateStackVisibility aborted: stackNode is nil") | |
| 312 | + | return | |
| 313 | + | } | |
| 314 | + | ||
| 315 | + | if !stackNode.isStacked { | |
| 316 | + | log.Printf("[workspace] updateStackVisibility aborted: stackNode.isStacked=%v", stackNode.isStacked) | |
| 317 | + | return | |
| 318 | + | } | |
| 319 | + | ||
| 320 | + | if len(stackNode.stackedPanes) == 0 { | |
| 321 | + | log.Printf("[workspace] updateStackVisibility aborted: stackedPanes empty, len=%d", len(stackNode.stackedPanes)) | |
| 322 | + | return | |
| 323 | + | } | |
| 324 | + | ||
| 325 | + | activeIndex := stackNode.activeStackIndex | |
| 326 | + | if activeIndex < 0 || activeIndex >= len(stackNode.stackedPanes) { | |
| 327 | + | log.Printf("[workspace] updateStackVisibility: correcting activeIndex from %d to 0", activeIndex) | |
| 328 | + | activeIndex = 0 | |
| 329 | + | stackNode.activeStackIndex = activeIndex | |
| 330 | + | } | |
| 331 | + | ||
| 332 | + | log.Printf("[workspace] updating stack visibility: activeIndex=%d stackSize=%d", activeIndex, len(stackNode.stackedPanes)) | |
| 333 | + | ||
| 334 | + | // First, ensure the active pane is visible (prevents rendering gaps) | |
| 335 | + | activePane := stackNode.stackedPanes[activeIndex] | |
| 336 | + | webkit.WidgetSetVisible(activePane.container, true) | |
| 337 | + | webkit.WidgetSetVisible(activePane.titleBar, false) | |
| 338 | + | webkit.WidgetAddCSSClass(activePane.container, "stacked-pane-active") | |
| 339 | + | webkit.WidgetRemoveCSSClass(activePane.container, "stacked-pane-collapsed") | |
| 340 | + | ||
| 341 | + | // Then hide other panes and show their title bars | |
| 342 | + | for i, pane := range stackNode.stackedPanes { | |
| 343 | + | if i != activeIndex { | |
| 344 | + | webkit.WidgetSetVisible(pane.container, false) | |
| 345 | + | webkit.WidgetSetVisible(pane.titleBar, true) | |
| 346 | + | webkit.WidgetAddCSSClass(pane.container, "stacked-pane-collapsed") | |
| 347 | + | webkit.WidgetRemoveCSSClass(pane.container, "stacked-pane-active") | |
| 348 | + | } | |
| 349 | + | } | |
| 350 | + | } | |
| 351 | + | ||
| 352 | + | // NavigateStack handles navigation within a stacked pane container | |
| 353 | + | func (spm *StackedPaneManager) NavigateStack(direction string) bool { | |
| 354 | + | if spm.wm.currentlyFocused == nil { | |
| 355 | + | return false | |
| 356 | + | } | |
| 357 | + | ||
| 358 | + | // Find the stack container this pane belongs to | |
| 359 | + | var stackNode *paneNode | |
| 360 | + | current := spm.wm.currentlyFocused | |
| 361 | + | ||
| 362 | + | // Check if current pane is directly in a stack | |
| 363 | + | if current.parent != nil && current.parent.isStacked { | |
| 364 | + | stackNode = current.parent | |
| 365 | + | } else { | |
| 366 | + | // Current pane might be the stack container itself if it was the first pane converted to stack | |
| 367 | + | if current.isStacked { | |
| 368 | + | stackNode = current | |
| 369 | + | } | |
| 370 | + | } | |
| 371 | + | ||
| 372 | + | if stackNode == nil || !stackNode.isStacked || len(stackNode.stackedPanes) <= 1 { | |
| 373 | + | return false // Not in a stack or stack has only one pane | |
| 374 | + | } | |
| 375 | + | ||
| 376 | + | // Use the stackNode's activeStackIndex instead of searching for current pane | |
| 377 | + | // This is more reliable than trying to match panes | |
| 378 | + | currentIndex := stackNode.activeStackIndex | |
| 379 | + | if currentIndex < 0 || currentIndex >= len(stackNode.stackedPanes) { | |
| 380 | + | log.Printf("[workspace] navigateStack: invalid activeStackIndex=%d, resetting to 0", currentIndex) | |
| 381 | + | currentIndex = 0 | |
| 382 | + | stackNode.activeStackIndex = 0 | |
| 383 | + | } | |
| 384 | + | ||
| 385 | + | // Calculate new index based on direction | |
| 386 | + | var newIndex int | |
| 387 | + | switch direction { | |
| 388 | + | case "up": | |
| 389 | + | newIndex = currentIndex - 1 | |
| 390 | + | if newIndex < 0 { | |
| 391 | + | newIndex = len(stackNode.stackedPanes) - 1 // Wrap to last | |
| 392 | + | } | |
| 393 | + | case "down": | |
| 394 | + | newIndex = currentIndex + 1 | |
| 395 | + | if newIndex >= len(stackNode.stackedPanes) { | |
| 396 | + | newIndex = 0 // Wrap to first | |
| 397 | + | } | |
| 398 | + | default: | |
| 399 | + | return false | |
| 400 | + | } | |
| 401 | + | ||
| 402 | + | if newIndex == currentIndex { | |
| 403 | + | return false // No change | |
| 404 | + | } | |
| 405 | + | ||
| 406 | + | // Update active stack index and visibility | |
| 407 | + | stackNode.activeStackIndex = newIndex | |
| 408 | + | spm.UpdateStackVisibility(stackNode) | |
| 409 | + | ||
| 410 | + | // Focus the new active pane | |
| 411 | + | newActivePane := stackNode.stackedPanes[newIndex] | |
| 412 | + | spm.wm.currentlyFocused = newActivePane | |
| 413 | + | spm.wm.focusManager.SetActivePane(newActivePane) | |
| 414 | + | ||
| 415 | + | log.Printf("[workspace] navigated stack: direction=%s from=%d to=%d stackSize=%d", | |
| 416 | + | direction, currentIndex, newIndex, len(stackNode.stackedPanes)) | |
| 417 | + | return true | |
| 418 | + | } | |
| 419 | + | ||
| 420 | + | // createTitleBar creates a title bar widget for a pane in a stack | |
| 421 | + | func (spm *StackedPaneManager) createTitleBar(pane *paneNode) uintptr { | |
| 422 | + | titleBox := webkit.NewBox(webkit.OrientationHorizontal, 8) | |
| 423 | + | if titleBox == 0 { | |
| 424 | + | log.Printf("[workspace] failed to create title box") | |
| 425 | + | return 0 | |
| 426 | + | } | |
| 427 | + | ||
| 428 | + | webkit.WidgetAddCSSClass(titleBox, "stacked-pane-title") | |
| 429 | + | webkit.WidgetSetHExpand(titleBox, true) | |
| 430 | + | webkit.WidgetSetVExpand(titleBox, false) | |
| 431 | + | ||
| 432 | + | // Get the actual title from the WebView instead of hardcoding "New Tab" | |
| 433 | + | var titleText string | |
| 434 | + | if pane.pane != nil && pane.pane.webView != nil { | |
| 435 | + | titleText = pane.pane.webView.GetTitle() | |
| 436 | + | if titleText == "" { | |
| 437 | + | titleText = "New Tab" // Fallback only when title is actually empty | |
| 438 | + | } | |
| 439 | + | } else { | |
| 440 | + | titleText = "New Tab" | |
| 441 | + | } | |
| 442 | + | ||
| 443 | + | // Create title label with the actual title | |
| 444 | + | titleLabel := webkit.NewLabel(titleText) | |
| 445 | + | if titleLabel == 0 { | |
| 446 | + | log.Printf("[workspace] failed to create title label") | |
| 447 | + | return titleBox | |
| 448 | + | } | |
| 449 | + | ||
| 450 | + | webkit.WidgetAddCSSClass(titleLabel, "stacked-pane-title-text") | |
| 451 | + | webkit.LabelSetMaxWidthChars(titleLabel, 50) | |
| 452 | + | webkit.LabelSetEllipsize(titleLabel, webkit.EllipsizeEnd) | |
| 453 | + | ||
| 454 | + | webkit.BoxAppend(titleBox, titleLabel) | |
| 455 | + | webkit.WidgetShow(titleBox) | |
| 456 | + | webkit.WidgetShow(titleLabel) | |
| 457 | + | ||
| 458 | + | return titleBox | |
| 459 | + | } | |
| 460 | + | ||
| 461 | + | // UpdateTitleBar updates the title bar label for a WebView in stacked panes | |
| 462 | + | func (spm *StackedPaneManager) UpdateTitleBar(webView *webkit.WebView, title string) { | |
| 463 | + | if spm.wm == nil || webView == nil || title == "" { | |
| 464 | + | return | |
| 465 | + | } | |
| 466 | + | ||
| 467 | + | // Find the pane node for this WebView | |
| 468 | + | node, exists := spm.wm.viewToNode[webView] | |
| 469 | + | if !exists || node == nil || !node.isLeaf { | |
| 470 | + | return | |
| 471 | + | } | |
| 472 | + | ||
| 473 | + | // Check if this pane is in a stack and has a title bar | |
| 474 | + | if node.parent != nil && node.parent.isStacked && node.titleBar != 0 { | |
| 475 | + | // Update the title bar label with the new title | |
| 476 | + | spm.updateTitleBarLabel(node, title) | |
| 477 | + | log.Printf("[workspace] updated title bar for WebView %s: %s", webView.ID(), title) | |
| 478 | + | } | |
| 479 | + | } | |
| 480 | + | ||
| 481 | + | // updateTitleBarLabel updates the label widget within a title bar by recreating it | |
| 482 | + | func (spm *StackedPaneManager) updateTitleBarLabel(node *paneNode, title string) { | |
| 483 | + | if node == nil || node.titleBar == 0 || node.parent == nil || !node.parent.isStacked { | |
| 484 | + | return | |
| 485 | + | } | |
| 486 | + | ||
| 487 | + | // Format the title for display | |
| 488 | + | displayTitle := title | |
| 489 | + | if len(displayTitle) > 50 { | |
| 490 | + | displayTitle = displayTitle[:47] + "..." | |
| 491 | + | } | |
| 492 | + | ||
| 493 | + | // Create new title bar with updated title | |
| 494 | + | newTitleBar := spm.createTitleBarWithTitle(node, displayTitle) | |
| 495 | + | if newTitleBar == 0 { | |
| 496 | + | log.Printf("[workspace] failed to create new title bar") | |
| 497 | + | return | |
| 498 | + | } | |
| 499 | + | ||
| 500 | + | // Replace the old title bar in the stack | |
| 501 | + | if node.parent != nil && node.parent.isStacked && node.parent.stackWrapper != 0 { | |
| 502 | + | // Remove old title bar | |
| 503 | + | webkit.WidgetUnparent(node.titleBar) | |
| 504 | + | ||
| 505 | + | // Update the node's title bar reference | |
| 506 | + | node.titleBar = newTitleBar | |
| 507 | + | ||
| 508 | + | // Add new title bar to the stack (need to find correct position) | |
| 509 | + | webkit.BoxPrepend(node.parent.stackWrapper, newTitleBar) | |
| 510 | + | ||
| 511 | + | log.Printf("[workspace] replaced title bar for pane: %p", node) | |
| 512 | + | } | |
| 513 | + | } | |
| 514 | + | ||
| 515 | + | // createTitleBarWithTitle creates a title bar with a specific title | |
| 516 | + | func (spm *StackedPaneManager) createTitleBarWithTitle(pane *paneNode, title string) uintptr { | |
| 517 | + | titleBox := webkit.NewBox(webkit.OrientationHorizontal, 8) | |
| 518 | + | if titleBox == 0 { | |
| 519 | + | return 0 | |
| 520 | + | } | |
| 521 | + | ||
| 522 | + | webkit.WidgetAddCSSClass(titleBox, "stacked-pane-title") | |
| 523 | + | webkit.WidgetSetHExpand(titleBox, true) | |
| 524 | + | webkit.WidgetSetVExpand(titleBox, false) | |
| 525 | + | ||
| 526 | + | // Create title label with the specified title | |
| 527 | + | titleLabel := webkit.NewLabel(title) | |
| 528 | + | if titleLabel == 0 { | |
| 529 | + | return titleBox | |
| 530 | + | } | |
| 531 | + | ||
| 532 | + | webkit.WidgetAddCSSClass(titleLabel, "stacked-pane-title-text") | |
| 533 | + | webkit.LabelSetMaxWidthChars(titleLabel, 50) | |
| 534 | + | webkit.LabelSetEllipsize(titleLabel, webkit.EllipsizeEnd) | |
| 535 | + | ||
| 536 | + | webkit.BoxAppend(titleBox, titleLabel) | |
| 537 | + | webkit.WidgetShow(titleBox) | |
| 538 | + | webkit.WidgetShow(titleLabel) | |
| 539 | + | ||
| 540 | + | return titleBox | |
| 541 | + | } | |
| 542 | + | ||
| 543 | + | // CloseStackedPane handles closing a pane that is part of a stack | |
| 544 | + | func (spm *StackedPaneManager) CloseStackedPane(node *paneNode) error { | |
| 545 | + | if node.parent == nil || !node.parent.isStacked { | |
| 546 | + | return errors.New("node is not part of a stacked pane") | |
| 547 | + | } | |
| 548 | + | ||
| 549 | + | stackNode := node.parent | |
| 550 | + | ||
| 551 | + | // Find the index of the node to be closed | |
| 552 | + | nodeIndex := -1 | |
| 553 | + | for i, stackedPane := range stackNode.stackedPanes { | |
| 554 | + | if stackedPane == node { | |
| 555 | + | nodeIndex = i | |
| 556 | + | break | |
| 557 | + | } | |
| 558 | + | } | |
| 559 | + | ||
| 560 | + | if nodeIndex == -1 { | |
| 561 | + | return errors.New("node not found in stack") | |
| 562 | + | } | |
| 563 | + | ||
| 564 | + | log.Printf("[workspace] closing stacked pane: index=%d stackSize=%d", nodeIndex, len(stackNode.stackedPanes)) | |
| 565 | + | ||
| 566 | + | // Clean up the pane | |
| 567 | + | spm.wm.detachHover(node) | |
| 568 | + | delete(spm.wm.viewToNode, node.pane.webView) | |
| 569 | + | ||
| 570 | + | // Remove from app.panes | |
| 571 | + | for i, pane := range spm.wm.app.panes { | |
| 572 | + | if pane == node.pane { | |
| 573 | + | spm.wm.app.panes = append(spm.wm.app.panes[:i], spm.wm.app.panes[i+1:]...) | |
| 574 | + | break | |
| 575 | + | } | |
| 576 | + | } | |
| 577 | + | ||
| 578 | + | // Remove the pane's widgets from the stack container | |
| 579 | + | stackBox := stackNode.stackWrapper | |
| 580 | + | if stackBox == 0 { | |
| 581 | + | stackBox = stackNode.container | |
| 582 | + | } | |
| 583 | + | ||
| 584 | + | if node.titleBar != 0 { | |
| 585 | + | webkit.BoxRemove(stackBox, node.titleBar) | |
| 586 | + | } | |
| 587 | + | if node.container != 0 { | |
| 588 | + | webkit.BoxRemove(stackBox, node.container) | |
| 589 | + | } | |
| 590 | + | ||
| 591 | + | // Clean up the pane | |
| 592 | + | node.pane.Cleanup() | |
| 593 | + | ||
| 594 | + | // Remove from the stack | |
| 595 | + | stackNode.stackedPanes = append(stackNode.stackedPanes[:nodeIndex], stackNode.stackedPanes[nodeIndex+1:]...) | |
| 596 | + | ||
| 597 | + | // Handle the remaining panes in the stack | |
| 598 | + | if len(stackNode.stackedPanes) == 0 { | |
| 599 | + | // No panes left in stack - this should not happen normally | |
| 600 | + | log.Printf("[workspace] warning: empty stack after closing pane") | |
| 601 | + | return errors.New("stack became empty") | |
| 602 | + | } else if len(stackNode.stackedPanes) == 1 { | |
| 603 | + | // Only one pane left - convert back to a regular pane | |
| 604 | + | lastPane := stackNode.stackedPanes[0] | |
| 605 | + | parent := stackNode.parent | |
| 606 | + | lastPaneContainer := lastPane.container | |
| 607 | + | ||
| 608 | + | // Remove the title bar since we're going back to single pane | |
| 609 | + | if lastPane.titleBar != 0 { | |
| 610 | + | webkit.BoxRemove(stackBox, lastPane.titleBar) | |
| 611 | + | } | |
| 612 | + | ||
| 613 | + | // Unparent the pane container from stack wrapper | |
| 614 | + | webkit.BoxRemove(stackBox, lastPaneContainer) | |
| 615 | + | ||
| 616 | + | // CRITICAL FIX: Replace the stackNode completely with lastPane | |
| 617 | + | // Update parent child references FIRST | |
| 618 | + | if parent == nil { | |
| 619 | + | spm.wm.root = lastPane | |
| 620 | + | if spm.wm.window != nil { | |
| 621 | + | spm.wm.window.SetChild(lastPaneContainer) | |
| 622 | + | } | |
| 623 | + | } else { | |
| 624 | + | if parent.left == stackNode { | |
| 625 | + | parent.left = lastPane | |
| 626 | + | webkit.PanedSetStartChild(parent.container, lastPaneContainer) | |
| 627 | + | } else if parent.right == stackNode { | |
| 628 | + | parent.right = lastPane | |
| 629 | + | webkit.PanedSetEndChild(parent.container, lastPaneContainer) | |
| 630 | + | } | |
| 631 | + | } | |
| 632 | + | ||
| 633 | + | // Convert lastPane back to regular pane | |
| 634 | + | lastPane.parent = parent | |
| 635 | + | lastPane.isStacked = false | |
| 636 | + | lastPane.stackedPanes = nil | |
| 637 | + | lastPane.titleBar = 0 | |
| 638 | + | ||
| 639 | + | // Update the viewToNode mapping to point to the lastPane, not stackNode | |
| 640 | + | spm.wm.viewToNode[lastPane.pane.webView] = lastPane | |
| 641 | + | ||
| 642 | + | // Focus the remaining pane if it was the active one being closed | |
| 643 | + | if spm.wm.currentlyFocused == node { | |
| 644 | + | spm.wm.currentlyFocused = lastPane | |
| 645 | + | spm.wm.focusManager.SetActivePane(lastPane) | |
| 646 | + | } | |
| 647 | + | ||
| 648 | + | log.Printf("[workspace] converted single-pane stack back to regular pane") | |
| 649 | + | } else { | |
| 650 | + | // Multiple panes remain in stack - adjust active index | |
| 651 | + | if stackNode.activeStackIndex >= nodeIndex && stackNode.activeStackIndex > 0 { | |
| 652 | + | stackNode.activeStackIndex-- | |
| 653 | + | } | |
| 654 | + | if stackNode.activeStackIndex >= len(stackNode.stackedPanes) { | |
| 655 | + | stackNode.activeStackIndex = len(stackNode.stackedPanes) - 1 | |
| 656 | + | } | |
| 657 | + | ||
| 658 | + | // Update visibility for remaining panes | |
| 659 | + | spm.UpdateStackVisibility(stackNode) | |
| 660 | + | ||
| 661 | + | // Focus the new active pane if we closed the currently active one | |
| 662 | + | if spm.wm.currentlyFocused == node { | |
| 663 | + | newActivePaneInStack := stackNode.stackedPanes[stackNode.activeStackIndex] | |
| 664 | + | spm.wm.currentlyFocused = newActivePaneInStack | |
| 665 | + | spm.wm.focusManager.SetActivePane(newActivePaneInStack) | |
| 666 | + | } | |
| 667 | + | ||
| 668 | + | log.Printf("[workspace] closed pane from stack: remaining=%d activeIndex=%d", | |
| 669 | + | len(stackNode.stackedPanes), stackNode.activeStackIndex) | |
| 670 | + | } | |
| 671 | + | ||
| 672 | + | // Update CSS classes after pane count changes | |
| 673 | + | spm.wm.ensurePaneBaseClasses() | |
| 674 | + | log.Printf("[workspace] stacked pane closed; panes remaining=%d", len(spm.wm.app.panes)) | |
| 675 | + | ||
| 676 | + | return nil | |
| 677 | + | } | |
| @@ -0,0 +1,677 @@ | |||||
| 1 | + | package browser | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "errors" | |||
| 5 | + | "fmt" | |||
| 6 | + | "log" | |||
| 7 | + | "time" | |||
| 8 | + | ||||
| 9 | + | "github.com/bnema/dumber/pkg/webkit" | |||
| 10 | + | ) | |||
| 11 | + | ||||
| 12 | + | // StackedPaneManager handles all stacked pane operations | |||
| 13 | + | type StackedPaneManager struct { | |||
| 14 | + | wm *WorkspaceManager | |||
| 15 | + | } | |||
| 16 | + | ||||
| 17 | + | // NewStackedPaneManager creates a new stacked pane manager | |||
| 18 | + | func NewStackedPaneManager(wm *WorkspaceManager) *StackedPaneManager { | |||
| 19 | + | return &StackedPaneManager{ | |||
| 20 | + | wm: wm, | |||
| 21 | + | } | |||
| 22 | + | } | |||
| 23 | + | ||||
| 24 | + | // StackPane creates a new pane stacked on top of the target pane. | |||
| 25 | + | // This is the main entry point for creating stacked panes. | |||
| 26 | + | func (spm *StackedPaneManager) StackPane(target *paneNode) (*paneNode, error) { | |||
| 27 | + | if target == nil || !target.isLeaf || target.pane == nil { | |||
| 28 | + | return nil, errors.New("stack target must be a leaf pane") | |||
| 29 | + | } | |||
| 30 | + | ||||
| 31 | + | // Create the new pane first | |||
| 32 | + | newLeaf, err := spm.prepareNewStackedPane() | |||
| 33 | + | if err != nil { | |||
| 34 | + | return nil, fmt.Errorf("failed to prepare new stacked pane: %v", err) | |||
| 35 | + | } | |||
| 36 | + | ||||
| 37 | + | var stackNode *paneNode | |||
| 38 | + | var insertIndex int | |||
| 39 | + | ||||
| 40 | + | // Check if target is already in a stack | |||
| 41 | + | if target.parent != nil && target.parent.isStacked { | |||
| 42 | + | // Target is already in a stack - add to existing stack | |||
| 43 | + | stackNode, insertIndex, err = spm.addPaneToExistingStack(target, newLeaf) | |||
| 44 | + | if err != nil { | |||
| 45 | + | return nil, fmt.Errorf("failed to add pane to existing stack: %v", err) | |||
| 46 | + | } | |||
| 47 | + | } else { | |||
| 48 | + | // Target is not stacked - create initial stack | |||
| 49 | + | stackNode, insertIndex, err = spm.convertToStackedContainer(target, newLeaf) | |||
| 50 | + | if err != nil { | |||
| 51 | + | return nil, fmt.Errorf("failed to convert to stacked container: %v", err) | |||
| 52 | + | } | |||
| 53 | + | } | |||
| 54 | + | ||||
| 55 | + | // Finalize the stack creation | |||
| 56 | + | return spm.finalizeStackCreation(stackNode, newLeaf, insertIndex) | |||
| 57 | + | } | |||
| 58 | + | ||||
| 59 | + | // prepareNewStackedPane creates a new pane and its container | |||
| 60 | + | func (spm *StackedPaneManager) prepareNewStackedPane() (*paneNode, error) { | |||
| 61 | + | newView, err := spm.wm.createWebView() | |||
| 62 | + | if err != nil { | |||
| 63 | + | return nil, err | |||
| 64 | + | } | |||
| 65 | + | ||||
| 66 | + | newPane, err := spm.wm.createPane(newView) | |||
| 67 | + | if err != nil { | |||
| 68 | + | return nil, err | |||
| 69 | + | } | |||
| 70 | + | ||||
| 71 | + | if handler := newPane.MessageHandler(); handler != nil { | |||
| 72 | + | handler.SetWorkspaceObserver(spm.wm) | |||
| 73 | + | } | |||
| 74 | + | ||||
| 75 | + | newContainer := newPane.webView.RootWidget() | |||
| 76 | + | if newContainer == 0 { | |||
| 77 | + | return nil, errors.New("new pane missing container") | |||
| 78 | + | } | |||
| 79 | + | webkit.WidgetSetHExpand(newContainer, true) | |||
| 80 | + | webkit.WidgetSetVExpand(newContainer, true) | |||
| 81 | + | webkit.WidgetRealizeInContainer(newContainer) | |||
| 82 | + | ||||
| 83 | + | // Create the new leaf node | |||
| 84 | + | newLeaf := &paneNode{ | |||
| 85 | + | pane: newPane, | |||
| 86 | + | container: newContainer, | |||
| 87 | + | isLeaf: true, | |||
| 88 | + | } | |||
| 89 | + | ||||
| 90 | + | // Create title bar for the new pane | |||
| 91 | + | newTitleBar := spm.createTitleBar(newLeaf) | |||
| 92 | + | newLeaf.titleBar = newTitleBar | |||
| 93 | + | ||||
| 94 | + | // Keep new container hidden initially - will be shown after transition | |||
| 95 | + | webkit.WidgetSetVisible(newContainer, false) | |||
| 96 | + | webkit.WidgetSetVisible(newTitleBar, false) | |||
| 97 | + | ||||
| 98 | + | return newLeaf, nil | |||
| 99 | + | } | |||
| 100 | + | ||||
| 101 | + | // addPaneToExistingStack adds a new pane to an existing stack | |||
| 102 | + | func (spm *StackedPaneManager) addPaneToExistingStack(target, newLeaf *paneNode) (*paneNode, int, error) { | |||
| 103 | + | stackNode := target.parent | |||
| 104 | + | ||||
| 105 | + | // Find the current position of the target pane in the stack | |||
| 106 | + | currentIndex := -1 | |||
| 107 | + | for i, pane := range stackNode.stackedPanes { | |||
| 108 | + | if pane == target { | |||
| 109 | + | currentIndex = i | |||
| 110 | + | break | |||
| 111 | + | } | |||
| 112 | + | } | |||
| 113 | + | ||||
| 114 | + | if currentIndex == -1 { | |||
| 115 | + | return nil, 0, errors.New("target pane not found in stack") | |||
| 116 | + | } | |||
| 117 | + | ||||
| 118 | + | // Insert the new pane right after the current pane | |||
| 119 | + | insertIndex := currentIndex + 1 | |||
| 120 | + | log.Printf("[workspace] adding to existing stack: currentIndex=%d insertIndex=%d stackSize=%d", | |||
| 121 | + | currentIndex, insertIndex, len(stackNode.stackedPanes)) | |||
| 122 | + | ||||
| 123 | + | // Set parent relationship | |||
| 124 | + | newLeaf.parent = stackNode | |||
| 125 | + | ||||
| 126 | + | // Insert the new pane at the correct position in the slice | |||
| 127 | + | stackNode.stackedPanes = append(stackNode.stackedPanes, nil) // Expand slice | |||
| 128 | + | copy(stackNode.stackedPanes[insertIndex+1:], stackNode.stackedPanes[insertIndex:]) // Shift elements | |||
| 129 | + | stackNode.stackedPanes[insertIndex] = newLeaf // Insert new pane | |||
| 130 | + | ||||
| 131 | + | // Unparent the new container before adding it to the stack (only if it has a parent) | |||
| 132 | + | if webkit.WidgetGetParent(newLeaf.container) != 0 { | |||
| 133 | + | webkit.WidgetUnparent(newLeaf.container) | |||
| 134 | + | } | |||
| 135 | + | ||||
| 136 | + | // Add the new widgets to the internal stack box (not the wrapper) | |||
| 137 | + | webkit.BoxAppend(stackNode.stackWrapper, newLeaf.titleBar) | |||
| 138 | + | webkit.BoxAppend(stackNode.stackWrapper, newLeaf.container) | |||
| 139 | + | ||||
| 140 | + | return stackNode, insertIndex, nil | |||
| 141 | + | } | |||
| 142 | + | ||||
| 143 | + | // convertToStackedContainer converts a simple pane to a stacked container | |||
| 144 | + | func (spm *StackedPaneManager) convertToStackedContainer(target, newLeaf *paneNode) (*paneNode, int, error) { | |||
| 145 | + | log.Printf("[workspace] converting pane to stacked: %p", target) | |||
| 146 | + | ||||
| 147 | + | // Create the wrapper container - this is what will be used by splitNode | |||
| 148 | + | stackWrapperContainer := webkit.NewBox(webkit.OrientationVertical, 0) | |||
| 149 | + | if stackWrapperContainer == 0 { | |||
| 150 | + | return nil, 0, errors.New("failed to create stack wrapper container") | |||
| 151 | + | } | |||
| 152 | + | webkit.WidgetSetHExpand(stackWrapperContainer, true) | |||
| 153 | + | webkit.WidgetSetVExpand(stackWrapperContainer, true) | |||
| 154 | + | ||||
| 155 | + | // Create the internal box for the actual stacked widgets (titles + webviews) | |||
| 156 | + | stackInternalBox := webkit.NewBox(webkit.OrientationVertical, 0) | |||
| 157 | + | if stackInternalBox == 0 { | |||
| 158 | + | return nil, 0, errors.New("failed to create stack internal box") | |||
| 159 | + | } | |||
| 160 | + | webkit.WidgetSetHExpand(stackInternalBox, true) | |||
| 161 | + | webkit.WidgetSetVExpand(stackInternalBox, true) | |||
| 162 | + | ||||
| 163 | + | // The internal box goes inside the wrapper | |||
| 164 | + | webkit.BoxAppend(stackWrapperContainer, stackInternalBox) | |||
| 165 | + | ||||
| 166 | + | // Get the existing container and parent info | |||
| 167 | + | existingContainer := target.container | |||
| 168 | + | parent := target.parent | |||
| 169 | + | ||||
| 170 | + | // Create title bar for the existing pane | |||
| 171 | + | titleBar := spm.createTitleBar(target) | |||
| 172 | + | ||||
| 173 | + | // Keep existing container visible during transition to prevent rendering glitch | |||
| 174 | + | webkit.WidgetSetVisible(titleBar, false) | |||
| 175 | + | ||||
| 176 | + | // Detach existing container from its current parent first | |||
| 177 | + | spm.detachFromParent(target, parent) | |||
| 178 | + | ||||
| 179 | + | // Build the complete stack structure with hidden widgets | |||
| 180 | + | webkit.BoxAppend(stackInternalBox, titleBar) | |||
| 181 | + | webkit.BoxAppend(stackInternalBox, existingContainer) | |||
| 182 | + | ||||
| 183 | + | // Immediately reattach the stack wrapper to minimize visibility gap | |||
| 184 | + | spm.reattachToParent(stackWrapperContainer, target, parent) | |||
| 185 | + | ||||
| 186 | + | // Convert target to a stacked leaf node | |||
| 187 | + | target.isStacked = true | |||
| 188 | + | target.isLeaf = true | |||
| 189 | + | target.container = existingContainer | |||
| 190 | + | target.titleBar = titleBar | |||
| 191 | + | ||||
| 192 | + | // Create the stack container node - container points to wrapper, stackWrapper points to internal box | |||
| 193 | + | stackNode := &paneNode{ | |||
| 194 | + | isStacked: true, | |||
| 195 | + | isLeaf: false, | |||
| 196 | + | container: stackWrapperContainer, // Wrapper for GTK operations (splits, etc.) | |||
| 197 | + | stackWrapper: stackInternalBox, // Internal box for stack operations | |||
| 198 | + | stackedPanes: []*paneNode{target}, | |||
| 199 | + | activeStackIndex: 0, // KEEP CURRENT PANE ACTIVE during transition (index 0) | |||
| 200 | + | parent: parent, | |||
| 201 | + | } | |||
| 202 | + | ||||
| 203 | + | // Update target's parent to be the stack node | |||
| 204 | + | target.parent = stackNode | |||
| 205 | + | ||||
| 206 | + | // Update parent references (GTK operations already done above) | |||
| 207 | + | if parent == nil { | |||
| 208 | + | spm.wm.root = stackNode | |||
| 209 | + | } else { | |||
| 210 | + | if parent.left == target { | |||
| 211 | + | parent.left = stackNode | |||
| 212 | + | } else if parent.right == target { | |||
| 213 | + | parent.right = stackNode | |||
| 214 | + | } | |||
| 215 | + | } | |||
| 216 | + | ||||
| 217 | + | // Set up the new pane | |||
| 218 | + | newLeaf.parent = stackNode | |||
| 219 | + | ||||
| 220 | + | // Insert the new pane at index 1 (after the original pane) | |||
| 221 | + | insertIndex := 1 | |||
| 222 | + | stackNode.stackedPanes = append(stackNode.stackedPanes, newLeaf) | |||
| 223 | + | ||||
| 224 | + | // Unparent the new container before adding it to the stack | |||
| 225 | + | if webkit.WidgetGetParent(newLeaf.container) != 0 { | |||
| 226 | + | webkit.WidgetUnparent(newLeaf.container) | |||
| 227 | + | } | |||
| 228 | + | ||||
| 229 | + | // Add the new widgets to the internal stack box | |||
| 230 | + | webkit.BoxAppend(stackNode.stackWrapper, newLeaf.titleBar) | |||
| 231 | + | webkit.BoxAppend(stackNode.stackWrapper, newLeaf.container) | |||
| 232 | + | ||||
| 233 | + | return stackNode, insertIndex, nil | |||
| 234 | + | } | |||
| 235 | + | ||||
| 236 | + | // detachFromParent removes a pane from its current parent | |||
| 237 | + | func (spm *StackedPaneManager) detachFromParent(target *paneNode, parent *paneNode) { | |||
| 238 | + | if parent == nil { | |||
| 239 | + | // Target is the root - remove it from window | |||
| 240 | + | if spm.wm.window != nil { | |||
| 241 | + | spm.wm.window.SetChild(0) | |||
| 242 | + | } | |||
| 243 | + | // Unparent if it has a GTK parent | |||
| 244 | + | if webkit.WidgetGetParent(target.container) != 0 { | |||
| 245 | + | webkit.WidgetUnparent(target.container) | |||
| 246 | + | } | |||
| 247 | + | } else if parent.container != 0 { | |||
| 248 | + | // Target has a parent paned - remove it (automatically unparents in GTK4) | |||
| 249 | + | if parent.left == target { | |||
| 250 | + | webkit.PanedSetStartChild(parent.container, 0) | |||
| 251 | + | } else if parent.right == target { | |||
| 252 | + | webkit.PanedSetEndChild(parent.container, 0) | |||
| 253 | + | } | |||
| 254 | + | } | |||
| 255 | + | } | |||
| 256 | + | ||||
| 257 | + | // reattachToParent attaches a container to the parent | |||
| 258 | + | func (spm *StackedPaneManager) reattachToParent(container uintptr, target *paneNode, parent *paneNode) { | |||
| 259 | + | if parent == nil { | |||
| 260 | + | if spm.wm.window != nil { | |||
| 261 | + | spm.wm.window.SetChild(container) | |||
| 262 | + | } | |||
| 263 | + | } else if parent.container != 0 { | |||
| 264 | + | if parent.left == target { | |||
| 265 | + | webkit.PanedSetStartChild(parent.container, container) | |||
| 266 | + | } else if parent.right == target { | |||
| 267 | + | webkit.PanedSetEndChild(parent.container, container) | |||
| 268 | + | } | |||
| 269 | + | } | |||
| 270 | + | } | |||
| 271 | + | ||||
| 272 | + | // finalizeStackCreation completes the stack creation process | |||
| 273 | + | func (spm *StackedPaneManager) finalizeStackCreation(stackNode, newLeaf *paneNode, insertIndex int) (*paneNode, error) { | |||
| 274 | + | // Update workspace state first | |||
| 275 | + | spm.wm.viewToNode[newLeaf.pane.webView] = newLeaf | |||
| 276 | + | spm.wm.ensureHover(newLeaf) | |||
| 277 | + | spm.wm.app.panes = append(spm.wm.app.panes, newLeaf.pane) | |||
| 278 | + | if newLeaf.pane.zoomController != nil { | |||
| 279 | + | newLeaf.pane.zoomController.ApplyInitialZoom() | |||
| 280 | + | } | |||
| 281 | + | ||||
| 282 | + | // Update CSS classes | |||
| 283 | + | spm.wm.ensurePaneBaseClasses() | |||
| 284 | + | ||||
| 285 | + | // Mark stack operation timestamp to prevent focus conflicts | |||
| 286 | + | spm.wm.lastStackOperation = time.Now() | |||
| 287 | + | ||||
| 288 | + | // Update stack visibility to show current state (existing pane still active) | |||
| 289 | + | spm.UpdateStackVisibility(stackNode) | |||
| 290 | + | ||||
| 291 | + | // Transition to the new pane after a brief delay to avoid rendering conflicts | |||
| 292 | + | webkit.IdleAdd(func() bool { | |||
| 293 | + | // Now switch to the new pane | |||
| 294 | + | stackNode.activeStackIndex = insertIndex | |||
| 295 | + | spm.UpdateStackVisibility(stackNode) | |||
| 296 | + | spm.wm.currentlyFocused = newLeaf | |||
| 297 | + | spm.wm.focusManager.SetActivePane(newLeaf) | |||
| 298 | + | return false // Remove idle callback | |||
| 299 | + | }) | |||
| 300 | + | ||||
| 301 | + | log.Printf("[workspace] stacked new pane: stackNode=%p newLeaf=%p stackSize=%d activeIndex=%d insertIndex=%d", | |||
| 302 | + | stackNode, newLeaf, len(stackNode.stackedPanes), stackNode.activeStackIndex, insertIndex) | |||
| 303 | + | return newLeaf, nil | |||
| 304 | + | } | |||
| 305 | + | ||||
| 306 | + | // UpdateStackVisibility updates the visibility of panes in a stack | |||
| 307 | + | func (spm *StackedPaneManager) UpdateStackVisibility(stackNode *paneNode) { | |||
| 308 | + | log.Printf("[workspace] updateStackVisibility called: stackNode=%p", stackNode) | |||
| 309 | + | ||||
| 310 | + | if stackNode == nil { | |||
| 311 | + | log.Printf("[workspace] updateStackVisibility aborted: stackNode is nil") | |||
| 312 | + | return | |||
| 313 | + | } | |||
| 314 | + | ||||
| 315 | + | if !stackNode.isStacked { | |||
| 316 | + | log.Printf("[workspace] updateStackVisibility aborted: stackNode.isStacked=%v", stackNode.isStacked) | |||
| 317 | + | return | |||
| 318 | + | } | |||
| 319 | + | ||||
| 320 | + | if len(stackNode.stackedPanes) == 0 { | |||
| 321 | + | log.Printf("[workspace] updateStackVisibility aborted: stackedPanes empty, len=%d", len(stackNode.stackedPanes)) | |||
| 322 | + | return | |||
| 323 | + | } | |||
| 324 | + | ||||
| 325 | + | activeIndex := stackNode.activeStackIndex | |||
| 326 | + | if activeIndex < 0 || activeIndex >= len(stackNode.stackedPanes) { | |||
| 327 | + | log.Printf("[workspace] updateStackVisibility: correcting activeIndex from %d to 0", activeIndex) | |||
| 328 | + | activeIndex = 0 | |||
| 329 | + | stackNode.activeStackIndex = activeIndex | |||
| 330 | + | } | |||
| 331 | + | ||||
| 332 | + | log.Printf("[workspace] updating stack visibility: activeIndex=%d stackSize=%d", activeIndex, len(stackNode.stackedPanes)) | |||
| 333 | + | ||||
| 334 | + | // First, ensure the active pane is visible (prevents rendering gaps) | |||
| 335 | + | activePane := stackNode.stackedPanes[activeIndex] | |||
| 336 | + | webkit.WidgetSetVisible(activePane.container, true) | |||
| 337 | + | webkit.WidgetSetVisible(activePane.titleBar, false) | |||
| 338 | + | webkit.WidgetAddCSSClass(activePane.container, "stacked-pane-active") | |||
| 339 | + | webkit.WidgetRemoveCSSClass(activePane.container, "stacked-pane-collapsed") | |||
| 340 | + | ||||
| 341 | + | // Then hide other panes and show their title bars | |||
| 342 | + | for i, pane := range stackNode.stackedPanes { | |||
| 343 | + | if i != activeIndex { | |||
| 344 | + | webkit.WidgetSetVisible(pane.container, false) | |||
| 345 | + | webkit.WidgetSetVisible(pane.titleBar, true) | |||
| 346 | + | webkit.WidgetAddCSSClass(pane.container, "stacked-pane-collapsed") | |||
| 347 | + | webkit.WidgetRemoveCSSClass(pane.container, "stacked-pane-active") | |||
| 348 | + | } | |||
| 349 | + | } | |||
| 350 | + | } | |||
| 351 | + | ||||
| 352 | + | // NavigateStack handles navigation within a stacked pane container | |||
| 353 | + | func (spm *StackedPaneManager) NavigateStack(direction string) bool { | |||
| 354 | + | if spm.wm.currentlyFocused == nil { | |||
| 355 | + | return false | |||
| 356 | + | } | |||
| 357 | + | ||||
| 358 | + | // Find the stack container this pane belongs to | |||
| 359 | + | var stackNode *paneNode | |||
| 360 | + | current := spm.wm.currentlyFocused | |||
| 361 | + | ||||
| 362 | + | // Check if current pane is directly in a stack | |||
| 363 | + | if current.parent != nil && current.parent.isStacked { | |||
| 364 | + | stackNode = current.parent | |||
| 365 | + | } else { | |||
| 366 | + | // Current pane might be the stack container itself if it was the first pane converted to stack | |||
| 367 | + | if current.isStacked { | |||
| 368 | + | stackNode = current | |||
| 369 | + | } | |||
| 370 | + | } | |||
| 371 | + | ||||
| 372 | + | if stackNode == nil || !stackNode.isStacked || len(stackNode.stackedPanes) <= 1 { | |||
| 373 | + | return false // Not in a stack or stack has only one pane | |||
| 374 | + | } | |||
| 375 | + | ||||
| 376 | + | // Use the stackNode's activeStackIndex instead of searching for current pane | |||
| 377 | + | // This is more reliable than trying to match panes | |||
| 378 | + | currentIndex := stackNode.activeStackIndex | |||
| 379 | + | if currentIndex < 0 || currentIndex >= len(stackNode.stackedPanes) { | |||
| 380 | + | log.Printf("[workspace] navigateStack: invalid activeStackIndex=%d, resetting to 0", currentIndex) | |||
| 381 | + | currentIndex = 0 | |||
| 382 | + | stackNode.activeStackIndex = 0 | |||
| 383 | + | } | |||
| 384 | + | ||||
| 385 | + | // Calculate new index based on direction | |||
| 386 | + | var newIndex int | |||
| 387 | + | switch direction { | |||
| 388 | + | case "up": | |||
| 389 | + | newIndex = currentIndex - 1 | |||
| 390 | + | if newIndex < 0 { | |||
| 391 | + | newIndex = len(stackNode.stackedPanes) - 1 // Wrap to last | |||
| 392 | + | } | |||
| 393 | + | case "down": | |||
| 394 | + | newIndex = currentIndex + 1 | |||
| 395 | + | if newIndex >= len(stackNode.stackedPanes) { | |||
| 396 | + | newIndex = 0 // Wrap to first | |||
| 397 | + | } | |||
| 398 | + | default: | |||
| 399 | + | return false | |||
| 400 | + | } | |||
| 401 | + | ||||
| 402 | + | if newIndex == currentIndex { | |||
| 403 | + | return false // No change | |||
| 404 | + | } | |||
| 405 | + | ||||
| 406 | + | // Update active stack index and visibility | |||
| 407 | + | stackNode.activeStackIndex = newIndex | |||
| 408 | + | spm.UpdateStackVisibility(stackNode) | |||
| 409 | + | ||||
| 410 | + | // Focus the new active pane | |||
| 411 | + | newActivePane := stackNode.stackedPanes[newIndex] | |||
| 412 | + | spm.wm.currentlyFocused = newActivePane | |||
| 413 | + | spm.wm.focusManager.SetActivePane(newActivePane) | |||
| 414 | + | ||||
| 415 | + | log.Printf("[workspace] navigated stack: direction=%s from=%d to=%d stackSize=%d", | |||
| 416 | + | direction, currentIndex, newIndex, len(stackNode.stackedPanes)) | |||
| 417 | + | return true | |||
| 418 | + | } | |||
| 419 | + | ||||
| 420 | + | // createTitleBar creates a title bar widget for a pane in a stack | |||
| 421 | + | func (spm *StackedPaneManager) createTitleBar(pane *paneNode) uintptr { | |||
| 422 | + | titleBox := webkit.NewBox(webkit.OrientationHorizontal, 8) | |||
| 423 | + | if titleBox == 0 { | |||
| 424 | + | log.Printf("[workspace] failed to create title box") | |||
| 425 | + | return 0 | |||
| 426 | + | } | |||
| 427 | + | ||||
| 428 | + | webkit.WidgetAddCSSClass(titleBox, "stacked-pane-title") | |||
| 429 | + | webkit.WidgetSetHExpand(titleBox, true) | |||
| 430 | + | webkit.WidgetSetVExpand(titleBox, false) | |||
| 431 | + | ||||
| 432 | + | // Get the actual title from the WebView instead of hardcoding "New Tab" | |||
| 433 | + | var titleText string | |||
| 434 | + | if pane.pane != nil && pane.pane.webView != nil { | |||
| 435 | + | titleText = pane.pane.webView.GetTitle() | |||
| 436 | + | if titleText == "" { | |||
| 437 | + | titleText = "New Tab" // Fallback only when title is actually empty | |||
| 438 | + | } | |||
| 439 | + | } else { | |||
| 440 | + | titleText = "New Tab" | |||
| 441 | + | } | |||
| 442 | + | ||||
| 443 | + | // Create title label with the actual title | |||
| 444 | + | titleLabel := webkit.NewLabel(titleText) | |||
| 445 | + | if titleLabel == 0 { | |||
| 446 | + | log.Printf("[workspace] failed to create title label") | |||
| 447 | + | return titleBox | |||
| 448 | + | } | |||
| 449 | + | ||||
| 450 | + | webkit.WidgetAddCSSClass(titleLabel, "stacked-pane-title-text") | |||
| 451 | + | webkit.LabelSetMaxWidthChars(titleLabel, 50) | |||
| 452 | + | webkit.LabelSetEllipsize(titleLabel, webkit.EllipsizeEnd) | |||
| 453 | + | ||||
| 454 | + | webkit.BoxAppend(titleBox, titleLabel) | |||
| 455 | + | webkit.WidgetShow(titleBox) | |||
| 456 | + | webkit.WidgetShow(titleLabel) | |||
| 457 | + | ||||
| 458 | + | return titleBox | |||
| 459 | + | } | |||
| 460 | + | ||||
| 461 | + | // UpdateTitleBar updates the title bar label for a WebView in stacked panes | |||
| 462 | + | func (spm *StackedPaneManager) UpdateTitleBar(webView *webkit.WebView, title string) { | |||
| 463 | + | if spm.wm == nil || webView == nil || title == "" { | |||
| 464 | + | return | |||
| 465 | + | } | |||
| 466 | + | ||||
| 467 | + | // Find the pane node for this WebView | |||
| 468 | + | node, exists := spm.wm.viewToNode[webView] | |||
| 469 | + | if !exists || node == nil || !node.isLeaf { | |||
| 470 | + | return | |||
| 471 | + | } | |||
| 472 | + | ||||
| 473 | + | // Check if this pane is in a stack and has a title bar | |||
| 474 | + | if node.parent != nil && node.parent.isStacked && node.titleBar != 0 { | |||
| 475 | + | // Update the title bar label with the new title | |||
| 476 | + | spm.updateTitleBarLabel(node, title) | |||
| 477 | + | log.Printf("[workspace] updated title bar for WebView %s: %s", webView.ID(), title) | |||
| 478 | + | } | |||
| 479 | + | } | |||
| 480 | + | ||||
| 481 | + | // updateTitleBarLabel updates the label widget within a title bar by recreating it | |||
| 482 | + | func (spm *StackedPaneManager) updateTitleBarLabel(node *paneNode, title string) { | |||
| 483 | + | if node == nil || node.titleBar == 0 || node.parent == nil || !node.parent.isStacked { | |||
| 484 | + | return | |||
| 485 | + | } | |||
| 486 | + | ||||
| 487 | + | // Format the title for display | |||
| 488 | + | displayTitle := title | |||
| 489 | + | if len(displayTitle) > 50 { | |||
| 490 | + | displayTitle = displayTitle[:47] + "..." | |||
| 491 | + | } | |||
| 492 | + | ||||
| 493 | + | // Create new title bar with updated title | |||
| 494 | + | newTitleBar := spm.createTitleBarWithTitle(node, displayTitle) | |||
| 495 | + | if newTitleBar == 0 { | |||
| 496 | + | log.Printf("[workspace] failed to create new title bar") | |||
| 497 | + | return | |||
| 498 | + | } | |||
| 499 | + | ||||
| 500 | + | // Replace the old title bar in the stack | |||
| 501 | + | if node.parent != nil && node.parent.isStacked && node.parent.stackWrapper != 0 { | |||
| 502 | + | // Remove old title bar | |||
| 503 | + | webkit.WidgetUnparent(node.titleBar) | |||
| 504 | + | ||||
| 505 | + | // Update the node's title bar reference | |||
| 506 | + | node.titleBar = newTitleBar | |||
| 507 | + | ||||
| 508 | + | // Add new title bar to the stack (need to find correct position) | |||
| 509 | + | webkit.BoxPrepend(node.parent.stackWrapper, newTitleBar) | |||
| 510 | + | ||||
| 511 | + | log.Printf("[workspace] replaced title bar for pane: %p", node) | |||
| 512 | + | } | |||
| 513 | + | } | |||
| 514 | + | ||||
| 515 | + | // createTitleBarWithTitle creates a title bar with a specific title | |||
| 516 | + | func (spm *StackedPaneManager) createTitleBarWithTitle(pane *paneNode, title string) uintptr { | |||
| 517 | + | titleBox := webkit.NewBox(webkit.OrientationHorizontal, 8) | |||
| 518 | + | if titleBox == 0 { | |||
| 519 | + | return 0 | |||
| 520 | + | } | |||
| 521 | + | ||||
| 522 | + | webkit.WidgetAddCSSClass(titleBox, "stacked-pane-title") | |||
| 523 | + | webkit.WidgetSetHExpand(titleBox, true) | |||
| 524 | + | webkit.WidgetSetVExpand(titleBox, false) | |||
| 525 | + | ||||
| 526 | + | // Create title label with the specified title | |||
| 527 | + | titleLabel := webkit.NewLabel(title) | |||
| 528 | + | if titleLabel == 0 { | |||
| 529 | + | return titleBox | |||
| 530 | + | } | |||
| 531 | + | ||||
| 532 | + | webkit.WidgetAddCSSClass(titleLabel, "stacked-pane-title-text") | |||
| 533 | + | webkit.LabelSetMaxWidthChars(titleLabel, 50) | |||
| 534 | + | webkit.LabelSetEllipsize(titleLabel, webkit.EllipsizeEnd) | |||
| 535 | + | ||||
| 536 | + | webkit.BoxAppend(titleBox, titleLabel) | |||
| 537 | + | webkit.WidgetShow(titleBox) | |||
| 538 | + | webkit.WidgetShow(titleLabel) | |||
| 539 | + | ||||
| 540 | + | return titleBox | |||
| 541 | + | } | |||
| 542 | + | ||||
| 543 | + | // CloseStackedPane handles closing a pane that is part of a stack | |||
| 544 | + | func (spm *StackedPaneManager) CloseStackedPane(node *paneNode) error { | |||
| 545 | + | if node.parent == nil || !node.parent.isStacked { | |||
| 546 | + | return errors.New("node is not part of a stacked pane") | |||
| 547 | + | } | |||
| 548 | + | ||||
| 549 | + | stackNode := node.parent | |||
| 550 | + | ||||
| 551 | + | // Find the index of the node to be closed | |||
| 552 | + | nodeIndex := -1 | |||
| 553 | + | for i, stackedPane := range stackNode.stackedPanes { | |||
| 554 | + | if stackedPane == node { | |||
| 555 | + | nodeIndex = i | |||
| 556 | + | break | |||
| 557 | + | } | |||
| 558 | + | } | |||
| 559 | + | ||||
| 560 | + | if nodeIndex == -1 { | |||
| 561 | + | return errors.New("node not found in stack") | |||
| 562 | + | } | |||
| 563 | + | ||||
| 564 | + | log.Printf("[workspace] closing stacked pane: index=%d stackSize=%d", nodeIndex, len(stackNode.stackedPanes)) | |||
| 565 | + | ||||
| 566 | + | // Clean up the pane | |||
| 567 | + | spm.wm.detachHover(node) | |||
| 568 | + | delete(spm.wm.viewToNode, node.pane.webView) | |||
| 569 | + | ||||
| 570 | + | // Remove from app.panes | |||
| 571 | + | for i, pane := range spm.wm.app.panes { | |||
| 572 | + | if pane == node.pane { | |||
| 573 | + | spm.wm.app.panes = append(spm.wm.app.panes[:i], spm.wm.app.panes[i+1:]...) | |||
| 574 | + | break | |||
| 575 | + | } | |||
| 576 | + | } | |||
| 577 | + | ||||
| 578 | + | // Remove the pane's widgets from the stack container | |||
| 579 | + | stackBox := stackNode.stackWrapper | |||
| 580 | + | if stackBox == 0 { | |||
| 581 | + | stackBox = stackNode.container | |||
| 582 | + | } | |||
| 583 | + | ||||
| 584 | + | if node.titleBar != 0 { | |||
| 585 | + | webkit.BoxRemove(stackBox, node.titleBar) | |||
| 586 | + | } | |||
| 587 | + | if node.container != 0 { | |||
| 588 | + | webkit.BoxRemove(stackBox, node.container) | |||
| 589 | + | } | |||
| 590 | + | ||||
| 591 | + | // Clean up the pane | |||
| 592 | + | node.pane.Cleanup() | |||
| 593 | + | ||||
| 594 | + | // Remove from the stack | |||
| 595 | + | stackNode.stackedPanes = append(stackNode.stackedPanes[:nodeIndex], stackNode.stackedPanes[nodeIndex+1:]...) | |||
| 596 | + | ||||
| 597 | + | // Handle the remaining panes in the stack | |||
| 598 | + | if len(stackNode.stackedPanes) == 0 { | |||
| 599 | + | // No panes left in stack - this should not happen normally | |||
| 600 | + | log.Printf("[workspace] warning: empty stack after closing pane") | |||
| 601 | + | return errors.New("stack became empty") | |||
| 602 | + | } else if len(stackNode.stackedPanes) == 1 { | |||
| 603 | + | // Only one pane left - convert back to a regular pane | |||
| 604 | + | lastPane := stackNode.stackedPanes[0] | |||
| 605 | + | parent := stackNode.parent | |||
| 606 | + | lastPaneContainer := lastPane.container | |||
| 607 | + | ||||
| 608 | + | // Remove the title bar since we're going back to single pane | |||
| 609 | + | if lastPane.titleBar != 0 { | |||
| 610 | + | webkit.BoxRemove(stackBox, lastPane.titleBar) | |||
| 611 | + | } | |||
| 612 | + | ||||
| 613 | + | // Unparent the pane container from stack wrapper | |||
| 614 | + | webkit.BoxRemove(stackBox, lastPaneContainer) | |||
| 615 | + | ||||
| 616 | + | // CRITICAL FIX: Replace the stackNode completely with lastPane | |||
| 617 | + | // Update parent child references FIRST | |||
| 618 | + | if parent == nil { | |||
| 619 | + | spm.wm.root = lastPane | |||
| 620 | + | if spm.wm.window != nil { | |||
| 621 | + | spm.wm.window.SetChild(lastPaneContainer) | |||
| 622 | + | } | |||
| 623 | + | } else { | |||
| 624 | + | if parent.left == stackNode { | |||
| 625 | + | parent.left = lastPane | |||
| 626 | + | webkit.PanedSetStartChild(parent.container, lastPaneContainer) | |||
| 627 | + | } else if parent.right == stackNode { | |||
| 628 | + | parent.right = lastPane | |||
| 629 | + | webkit.PanedSetEndChild(parent.container, lastPaneContainer) | |||
| 630 | + | } | |||
| 631 | + | } | |||
| 632 | + | ||||
| 633 | + | // Convert lastPane back to regular pane | |||
| 634 | + | lastPane.parent = parent | |||
| 635 | + | lastPane.isStacked = false | |||
| 636 | + | lastPane.stackedPanes = nil | |||
| 637 | + | lastPane.titleBar = 0 | |||
| 638 | + | ||||
| 639 | + | // Update the viewToNode mapping to point to the lastPane, not stackNode | |||
| 640 | + | spm.wm.viewToNode[lastPane.pane.webView] = lastPane | |||
| 641 | + | ||||
| 642 | + | // Focus the remaining pane if it was the active one being closed | |||
| 643 | + | if spm.wm.currentlyFocused == node { | |||
| 644 | + | spm.wm.currentlyFocused = lastPane | |||
| 645 | + | spm.wm.focusManager.SetActivePane(lastPane) | |||
| 646 | + | } | |||
| 647 | + | ||||
| 648 | + | log.Printf("[workspace] converted single-pane stack back to regular pane") | |||
| 649 | + | } else { | |||
| 650 | + | // Multiple panes remain in stack - adjust active index | |||
| 651 | + | if stackNode.activeStackIndex >= nodeIndex && stackNode.activeStackIndex > 0 { | |||
| 652 | + | stackNode.activeStackIndex-- | |||
| 653 | + | } | |||
| 654 | + | if stackNode.activeStackIndex >= len(stackNode.stackedPanes) { | |||
| 655 | + | stackNode.activeStackIndex = len(stackNode.stackedPanes) - 1 | |||
| 656 | + | } | |||
| 657 | + | ||||
| 658 | + | // Update visibility for remaining panes | |||
| 659 | + | spm.UpdateStackVisibility(stackNode) | |||
| 660 | + | ||||
| 661 | + | // Focus the new active pane if we closed the currently active one | |||
| 662 | + | if spm.wm.currentlyFocused == node { | |||
| 663 | + | newActivePaneInStack := stackNode.stackedPanes[stackNode.activeStackIndex] | |||
| 664 | + | spm.wm.currentlyFocused = newActivePaneInStack | |||
| 665 | + | spm.wm.focusManager.SetActivePane(newActivePaneInStack) | |||
| 666 | + | } | |||
| 667 | + | ||||
| 668 | + | log.Printf("[workspace] closed pane from stack: remaining=%d activeIndex=%d", | |||
| 669 | + | len(stackNode.stackedPanes), stackNode.activeStackIndex) | |||
| 670 | + | } | |||
| 671 | + | ||||
| 672 | + | // Update CSS classes after pane count changes | |||
| 673 | + | spm.wm.ensurePaneBaseClasses() | |||
| 674 | + | log.Printf("[workspace] stacked pane closed; panes remaining=%d", len(spm.wm.app.panes)) | |||
| 675 | + | ||||
| 676 | + | return nil | |||
| 677 | + | } | |||
M internal/app/browser/webview.go +7 -0
| @@ -157,12 +157,19 @@ func (app *BrowserApp) attachPaneHandlers(pane *BrowserPane) { | |||
| 157 | 157 | if url == "" { | |
| 158 | 158 | return | |
| 159 | 159 | } | |
| 160 | + | ||
| 161 | + | // Update database with new title | |
| 160 | 162 | go func(url, title string) { | |
| 161 | 163 | ctx := context.Background() | |
| 162 | 164 | if err := app.browserService.UpdatePageTitle(ctx, url, title); err != nil { | |
| 163 | 165 | log.Printf("Warning: failed to update page title: %v", err) | |
| 164 | 166 | } | |
| 165 | 167 | }(url, title) | |
| 168 | + | ||
| 169 | + | // Update stacked pane title bar if this pane is in a stack | |
| 170 | + | if app.workspace != nil { | |
| 171 | + | app.workspace.UpdateTitleBar(pane.webView, title) | |
| 172 | + | } | |
| 166 | 173 | }) | |
| 167 | 174 | ||
| 168 | 175 | pane.webView.RegisterScriptMessageHandler(func(payload string) { | |
| @@ -157,12 +157,19 @@ func (app *BrowserApp) attachPaneHandlers(pane *BrowserPane) { | |||||
| 157 | if url == "" { | 157 | if url == "" { | ||
| 158 | return | 158 | return | ||
| 159 | } | 159 | } | ||
| 160 | + | ||||
| 161 | + | // Update database with new title | |||
| 160 | go func(url, title string) { | 162 | go func(url, title string) { | ||
| 161 | ctx := context.Background() | 163 | ctx := context.Background() | ||
| 162 | if err := app.browserService.UpdatePageTitle(ctx, url, title); err != nil { | 164 | if err := app.browserService.UpdatePageTitle(ctx, url, title); err != nil { | ||
| 163 | log.Printf("Warning: failed to update page title: %v", err) | 165 | log.Printf("Warning: failed to update page title: %v", err) | ||
| 164 | } | 166 | } | ||
| 165 | }(url, title) | 167 | }(url, title) | ||
| 168 | + | ||||
| 169 | + | // Update stacked pane title bar if this pane is in a stack | |||
| 170 | + | if app.workspace != nil { | |||
| 171 | + | app.workspace.UpdateTitleBar(pane.webView, title) | |||
| 172 | + | } | |||
| 166 | }) | 173 | }) | ||
| 167 | 174 | ||||
| 168 | pane.webView.RegisterScriptMessageHandler(func(payload string) { | 175 | pane.webView.RegisterScriptMessageHandler(func(payload string) { | ||
M internal/app/browser/workspace_manager.go +462 -321
| @@ -19,7 +19,7 @@ type paneNode struct { | |||
| 19 | 19 | parent *paneNode | |
| 20 | 20 | left *paneNode | |
| 21 | 21 | right *paneNode | |
| 22 | - | container uintptr // GtkPaned for branch nodes, GtkBox for stacked nodes, stable WebView container for leaves | |
| 22 | + | container uintptr // GtkPaned for branch nodes, wrapper GtkBox for stacked nodes, stable WebView container for leaves | |
| 23 | 23 | orientation webkit.Orientation | |
| 24 | 24 | isLeaf bool | |
| 25 | 25 | isPopup bool // Deprecated: use windowType instead | |
| @@ -35,6 +35,7 @@ type paneNode struct { | |||
| 35 | 35 | stackedPanes []*paneNode // List of stacked panes (if isStacked) | |
| 36 | 36 | activeStackIndex int // Index of currently visible pane in stack | |
| 37 | 37 | titleBar uintptr // GtkBox for title bar (when collapsed) | |
| 38 | + | stackWrapper uintptr // Internal GtkBox containing the actual stacked widgets (titles + webviews) | |
| 38 | 39 | } | |
| 39 | 40 | ||
| 40 | 41 | // WorkspaceManager coordinates Zellij-style pane operations. | |
| @@ -42,7 +43,6 @@ type WorkspaceManager struct { | |||
| 42 | 43 | app *BrowserApp | |
| 43 | 44 | window *webkit.Window | |
| 44 | 45 | root *paneNode | |
| 45 | - | active *paneNode | |
| 46 | 46 | mainPane *paneNode | |
| 47 | 47 | viewToNode map[*webkit.WebView]*paneNode | |
| 48 | 48 | lastSplitMsg map[*webkit.WebView]time.Time | |
| @@ -62,8 +62,18 @@ type WorkspaceManager struct { | |||
| 62 | 62 | lastFocusChange time.Time // When focus was last changed | |
| 63 | 63 | focusThrottleMutex sync.Mutex // Protects focus throttling state | |
| 64 | 64 | ||
| 65 | + | // Stack operation timing to prevent focus conflicts | |
| 66 | + | lastStackOperation time.Time // When a stack operation was last performed | |
| 67 | + | ||
| 65 | 68 | // NEW: Pane creation deduplicator | |
| 66 | 69 | paneDeduplicator *messaging.PaneRequestDeduplicator | |
| 70 | + | ||
| 71 | + | // Specialized managers for different pane operations | |
| 72 | + | stackedPaneManager *StackedPaneManager | |
| 73 | + | focusManager *FocusManager | |
| 74 | + | ||
| 75 | + | // Current focused pane (Single Source of Truth for focus) | |
| 76 | + | currentlyFocused *paneNode | |
| 67 | 77 | } | |
| 68 | 78 | ||
| 69 | 79 | const ( | |
| @@ -84,6 +94,10 @@ func NewWorkspaceManager(app *BrowserApp, rootPane *BrowserPane) *WorkspaceManag | |||
| 84 | 94 | lastExitMsg: make(map[*webkit.WebView]time.Time), | |
| 85 | 95 | paneDeduplicator: messaging.NewPaneRequestDeduplicator(), // NEW: Initialize deduplicator | |
| 86 | 96 | } | |
| 97 | + | ||
| 98 | + | // Initialize specialized managers | |
| 99 | + | manager.stackedPaneManager = NewStackedPaneManager(manager) | |
| 100 | + | manager.focusManager = NewFocusManager(manager) | |
| 87 | 101 | manager.createWebViewFn = func() (*webkit.WebView, error) { | |
| 88 | 102 | if manager.app == nil { | |
| 89 | 103 | return nil, errors.New("workspace manager missing app reference") | |
| @@ -112,7 +126,6 @@ func NewWorkspaceManager(app *BrowserApp, rootPane *BrowserPane) *WorkspaceManag | |||
| 112 | 126 | isLeaf: true, | |
| 113 | 127 | } | |
| 114 | 128 | manager.root = root | |
| 115 | - | manager.active = root | |
| 116 | 129 | manager.mainPane = root | |
| 117 | 130 | manager.viewToNode[rootPane.webView] = root | |
| 118 | 131 | manager.ensureHover(root) | |
| @@ -122,7 +135,8 @@ func NewWorkspaceManager(app *BrowserApp, rootPane *BrowserPane) *WorkspaceManag | |||
| 122 | 135 | } | |
| 123 | 136 | ||
| 124 | 137 | app.workspace = manager | |
| 125 | - | manager.focusNode(root) | |
| 138 | + | manager.currentlyFocused = root | |
| 139 | + | manager.focusManager.SetActivePane(root) | |
| 126 | 140 | // Workspace navigation shortcuts are now handled globally by WindowShortcutHandler | |
| 127 | 141 | // Ensure initial CSS classes are applied | |
| 128 | 142 | manager.ensurePaneBaseClasses() | |
| @@ -161,7 +175,8 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||
| 161 | 175 | wm.paneModeActive = true | |
| 162 | 176 | wm.paneModeSource = source | |
| 163 | 177 | wm.lastPaneModeEntry = time.Now() | |
| 164 | - | wm.focusNode(node) | |
| 178 | + | wm.currentlyFocused = node | |
| 179 | + | wm.focusManager.SetActivePane(node) | |
| 165 | 180 | case "pane-confirmed", "pane-cancelled", "pane-mode-exited": | |
| 166 | 181 | // Debounce pane-mode-exited events to prevent duplicate focus calls | |
| 167 | 182 | if event == "pane-mode-exited" { | |
| @@ -179,7 +194,7 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||
| 179 | 194 | wm.paneModeSource = nil | |
| 180 | 195 | wm.paneMutex.Unlock() | |
| 181 | 196 | ||
| 182 | - | wm.focusNode(node) | |
| 197 | + | wm.focusAfterPaneMode(node) | |
| 183 | 198 | case "pane-close": | |
| 184 | 199 | if !wm.paneModeActive { | |
| 185 | 200 | log.Printf("[workspace] close requested outside pane mode; ignoring") | |
| @@ -194,7 +209,8 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||
| 194 | 209 | // Don't focus the node that's about to be closed - closeCurrentPane() will handle focus | |
| 195 | 210 | wm.closeCurrentPane() | |
| 196 | 211 | case "pane-split": | |
| 197 | - | wm.focusNode(node) | |
| 212 | + | wm.currentlyFocused = node | |
| 213 | + | wm.focusManager.SetActivePane(node) | |
| 198 | 214 | direction := strings.ToLower(msg.Direction) | |
| 199 | 215 | if direction == "" { | |
| 200 | 216 | direction = "right" | |
| @@ -220,7 +236,8 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||
| 220 | 236 | wm.clonePaneState(node, newNode) | |
| 221 | 237 | wm.splitting = false | |
| 222 | 238 | case "pane-stack": | |
| 223 | - | wm.focusNode(node) | |
| 239 | + | wm.currentlyFocused = node | |
| 240 | + | wm.focusManager.SetActivePane(node) | |
| 224 | 241 | if last, ok := wm.lastSplitMsg[source]; ok { | |
| 225 | 242 | if time.Since(last) < 200*time.Millisecond { | |
| 226 | 243 | log.Printf("[workspace] stack ignored: debounce (%.0fms)", time.Since(last).Seconds()*1000) | |
| @@ -233,7 +250,7 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||
| 233 | 250 | } | |
| 234 | 251 | wm.splitting = true | |
| 235 | 252 | wm.lastSplitMsg[source] = time.Now() | |
| 236 | - | newNode, err := wm.stackPane(node) | |
| 253 | + | newNode, err := wm.stackedPaneManager.StackPane(node) | |
| 237 | 254 | if err != nil { | |
| 238 | 255 | log.Printf("[workspace] stack failed: %v", err) | |
| 239 | 256 | wm.splitting = false | |
| @@ -338,7 +355,7 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||
| 338 | 355 | ||
| 339 | 356 | // GetActiveNode returns the currently active pane node | |
| 340 | 357 | func (wm *WorkspaceManager) GetActiveNode() *paneNode { | |
| 341 | - | return wm.active | |
| 358 | + | return wm.currentlyFocused | |
| 342 | 359 | } | |
| 343 | 360 | ||
| 344 | 361 | // GetNodeForWebView returns the pane node associated with a WebView | |
| @@ -376,123 +393,36 @@ func (wm *WorkspaceManager) DispatchPaneFocusEvent(node *paneNode, active bool) | |||
| 376 | 393 | } | |
| 377 | 394 | } | |
| 378 | 395 | ||
| 379 | - | func (wm *WorkspaceManager) focusNode(node *paneNode) { | |
| 380 | - | if node == nil || !node.isLeaf || node.pane == nil || node.pane.webView == nil { | |
| 381 | - | return | |
| 382 | - | } | |
| 396 | + | // DEPRECATED: Moved to FocusManager | |
| 397 | + | ||
| 398 | + | // focusAfterPaneMode restores focus when a pane mode operation completes without | |
| 399 | + | // stealing focus from the active pane inside a stack that just spawned a sibling. | |
| 400 | + | func (wm *WorkspaceManager) focusAfterPaneMode(node *paneNode) { | |
| 401 | + | wm.focusRespectingStack(node, "focus-after-pane-mode") | |
| 402 | + | } | |
| 383 | 403 | ||
| 384 | - | // Don't focus a node that's not in our viewToNode map (likely destroyed) | |
| 385 | - | if _, exists := wm.viewToNode[node.pane.webView]; !exists { | |
| 386 | - | log.Printf("[workspace] focusNode: skipping destroyed/unknown webView=%p", node.pane.webView) | |
| 404 | + | func (wm *WorkspaceManager) focusRespectingStack(node *paneNode, reason string) { | |
| 405 | + | if node == nil { | |
| 387 | 406 | return | |
| 388 | 407 | } | |
| 389 | 408 | ||
| 390 | - | // Handle stacked pane focus: ensure the pane is active in its stack | |
| 391 | - | if node.parent != nil && node.parent.isStacked { | |
| 392 | - | stackNode := node.parent | |
| 393 | - | // Find the index of this node in the stack | |
| 394 | - | for i, stackedPane := range stackNode.stackedPanes { | |
| 395 | - | if stackedPane == node { | |
| 396 | - | if stackNode.activeStackIndex != i { | |
| 397 | - | stackNode.activeStackIndex = i | |
| 398 | - | wm.updateStackVisibility(stackNode) | |
| 399 | - | log.Printf("[workspace] updated stack focus: activeIndex=%d for pane=%p", i, node) | |
| 409 | + | if stack := node.parent; stack != nil && stack.isStacked { | |
| 410 | + | activeIndex := stack.activeStackIndex | |
| 411 | + | if activeIndex >= 0 && activeIndex < len(stack.stackedPanes) { | |
| 412 | + | activePane := stack.stackedPanes[activeIndex] | |
| 413 | + | if activePane != nil && activePane != node { | |
| 414 | + | if reason != "" { | |
| 415 | + | log.Printf("[workspace] %s: preserving active stacked pane index=%d", reason, activeIndex) | |
| 400 | 416 | } | |
| 401 | - | break | |
| 417 | + | wm.currentlyFocused = activePane | |
| 418 | + | wm.focusManager.SetActivePane(activePane) | |
| 419 | + | return | |
| 402 | 420 | } | |
| 403 | 421 | } | |
| 404 | 422 | } | |
| 405 | 423 | ||
| 406 | - | previous := wm.active | |
| 407 | - | var previousContainer uintptr | |
| 408 | - | var previousWebView *webkit.WebView | |
| 409 | - | if previous != nil && previous != node && previous.pane != nil && previous.pane.webView != nil { | |
| 410 | - | previousContainer = previous.container | |
| 411 | - | previousWebView = previous.pane.webView | |
| 412 | - | wm.DispatchPaneFocusEvent(previous, false) | |
| 413 | - | } | |
| 414 | - | ||
| 415 | - | // Set previously active WebView as inactive | |
| 416 | - | if previousWebView != nil && previousWebView != node.pane.webView { | |
| 417 | - | previousWebView.SetActive(false) | |
| 418 | - | } | |
| 419 | - | ||
| 420 | - | wm.active = node | |
| 421 | - | wm.app.activePane = node.pane | |
| 422 | - | wm.app.webView = node.pane.webView | |
| 423 | - | wm.app.zoomController = node.pane.zoomController | |
| 424 | - | wm.app.navigationController = node.pane.navigationController | |
| 425 | - | wm.app.clipboardController = node.pane.clipboardController | |
| 426 | - | wm.app.messageHandler = node.pane.messageHandler | |
| 427 | - | wm.app.shortcutHandler = node.pane.shortcutHandler | |
| 428 | - | ||
| 429 | - | // Set this WebView as active | |
| 430 | - | if node.pane != nil && node.pane.webView != nil { | |
| 431 | - | node.pane.webView.SetActive(true) | |
| 432 | - | } | |
| 433 | - | ||
| 434 | - | // Shortcuts are registered once during webView creation, no need to re-register on focus | |
| 435 | - | ||
| 436 | - | if handler := node.pane.messageHandler; handler != nil { | |
| 437 | - | handler.SetWorkspaceObserver(wm) | |
| 438 | - | } | |
| 439 | - | ||
| 440 | - | if wm.app.browserService != nil { | |
| 441 | - | wm.app.browserService.AttachWebView(node.pane.webView) | |
| 442 | - | } | |
| 443 | - | ||
| 444 | - | // Ensure all panes have proper base classes | |
| 445 | - | wm.ensurePaneBaseClasses() | |
| 446 | - | ||
| 447 | - | container := node.container | |
| 448 | - | viewWidget := node.pane.webView.Widget() | |
| 449 | - | if container != 0 && container != previousContainer { | |
| 450 | - | // Remove active class from previous container to ensure only one pane is visually active | |
| 451 | - | if previousContainer != 0 { | |
| 452 | - | webkit.WidgetRemoveCSSClass(previousContainer, activePaneClass) | |
| 453 | - | } | |
| 454 | - | ||
| 455 | - | // Add active class to current container (border visibility controlled by CSS multi-pane rules) | |
| 456 | - | webkit.WidgetAddCSSClass(container, activePaneClass) | |
| 457 | - | if !webkit.WidgetIsValid(container) { | |
| 458 | - | log.Printf("[workspace] focus aborted: container invalid widget=%#x", container) | |
| 459 | - | return | |
| 460 | - | } | |
| 461 | - | webkit.IdleAdd(func() bool { | |
| 462 | - | if !webkit.WidgetIsValid(container) { | |
| 463 | - | log.Printf("[workspace] focus aborted during idle: container invalid widget=%#x", container) | |
| 464 | - | return false | |
| 465 | - | } | |
| 466 | - | parent := webkit.WidgetGetParent(container) | |
| 467 | - | if parent != 0 { | |
| 468 | - | webkit.WidgetRealizeInContainer(container) | |
| 469 | - | if viewWidget != 0 { | |
| 470 | - | webkit.WidgetGrabFocus(viewWidget) | |
| 471 | - | } | |
| 472 | - | // Consolidated focus operation log | |
| 473 | - | if wm.app.config != nil && wm.app.config.Debug.EnableWorkspaceDebug { | |
| 474 | - | log.Printf("[workspace] focus operations completed: container=%#x parent=%#x viewWidget=%#x", container, parent, viewWidget) | |
| 475 | - | } | |
| 476 | - | } else { | |
| 477 | - | log.Printf("[workspace] focus deferred: widget not parented") | |
| 478 | - | } | |
| 479 | - | return false // Remove idle callback | |
| 480 | - | }) | |
| 481 | - | } | |
| 482 | - | ||
| 483 | - | if node.pane != nil && node.pane.webView != nil { | |
| 484 | - | // Update pane focus time | |
| 485 | - | node.pane.UpdateLastFocus() | |
| 486 | - | ||
| 487 | - | // Always dispatch focus event to ensure JavaScript side knows the active pane | |
| 488 | - | // This fixes the issue where navigation within the same pane doesn't update JS state | |
| 489 | - | wm.DispatchPaneFocusEvent(node, true) | |
| 490 | - | ||
| 491 | - | // Lazy-load GUI components if first focus | |
| 492 | - | if !node.pane.HasGUI() { | |
| 493 | - | wm.ensureGUIInPane(node.pane) | |
| 494 | - | } | |
| 495 | - | } | |
| 424 | + | wm.currentlyFocused = node | |
| 425 | + | wm.focusManager.SetActivePane(node) | |
| 496 | 426 | } | |
| 497 | 427 | ||
| 498 | 428 | // Helper functions for stacked pane CSS colors | |
| @@ -662,8 +592,8 @@ func (wm *WorkspaceManager) focusByView(view *webkit.WebView) { | |||
| 662 | 592 | wm.focusThrottleMutex.Unlock() | |
| 663 | 593 | ||
| 664 | 594 | if node, ok := wm.viewToNode[view]; ok { | |
| 665 | - | if wm.active != node { | |
| 666 | - | wm.focusNode(node) | |
| 595 | + | if wm.currentlyFocused != node { | |
| 596 | + | wm.focusRespectingStack(node, "focus-by-view") | |
| 667 | 597 | } | |
| 668 | 598 | } | |
| 669 | 599 | } | |
| @@ -680,7 +610,8 @@ func (wm *WorkspaceManager) ensureHover(node *paneNode) { | |||
| 680 | 610 | if wm == nil { | |
| 681 | 611 | return | |
| 682 | 612 | } | |
| 683 | - | wm.focusNode(node) | |
| 613 | + | wm.currentlyFocused = node | |
| 614 | + | wm.focusManager.SetActivePane(node) | |
| 684 | 615 | }) | |
| 685 | 616 | node.hoverToken = token | |
| 686 | 617 | if token == 0 { | |
| @@ -706,7 +637,7 @@ func (wm *WorkspaceManager) FocusNeighbor(direction string) bool { | |||
| 706 | 637 | switch strings.ToLower(direction) { | |
| 707 | 638 | case "up", "down": | |
| 708 | 639 | // Check if current pane is part of a stack and handle stack navigation | |
| 709 | - | if wm.navigateStack(strings.ToLower(direction)) { | |
| 640 | + | if wm.stackedPaneManager.NavigateStack(strings.ToLower(direction)) { | |
| 710 | 641 | return true | |
| 711 | 642 | } | |
| 712 | 643 | // Fall back to regular adjacency navigation | |
| @@ -720,13 +651,13 @@ func (wm *WorkspaceManager) FocusNeighbor(direction string) bool { | |||
| 720 | 651 | ||
| 721 | 652 | // navigateStack handles navigation within a stacked pane container. | |
| 722 | 653 | func (wm *WorkspaceManager) navigateStack(direction string) bool { | |
| 723 | - | if wm.active == nil { | |
| 654 | + | if wm.currentlyFocused == nil { | |
| 724 | 655 | return false | |
| 725 | 656 | } | |
| 726 | 657 | ||
| 727 | 658 | // Find the stack container this pane belongs to | |
| 728 | 659 | var stackNode *paneNode | |
| 729 | - | current := wm.active | |
| 660 | + | current := wm.currentlyFocused | |
| 730 | 661 | ||
| 731 | 662 | // Check if current pane is directly in a stack | |
| 732 | 663 | if current.parent != nil && current.parent.isStacked { | |
| @@ -779,11 +710,12 @@ func (wm *WorkspaceManager) navigateStack(direction string) bool { | |||
| 779 | 710 | ||
| 780 | 711 | // Update active stack index and visibility | |
| 781 | 712 | stackNode.activeStackIndex = newIndex | |
| 782 | - | wm.updateStackVisibility(stackNode) | |
| 713 | + | wm.stackedPaneManager.UpdateStackVisibility(stackNode) | |
| 783 | 714 | ||
| 784 | 715 | // Focus the new active pane | |
| 785 | 716 | newActivePane := stackNode.stackedPanes[newIndex] | |
| 786 | - | wm.focusNode(newActivePane) | |
| 717 | + | wm.currentlyFocused = newActivePane | |
| 718 | + | wm.focusManager.SetActivePane(newActivePane) | |
| 787 | 719 | ||
| 788 | 720 | log.Printf("[workspace] navigated stack: direction=%s from=%d to=%d stackSize=%d", | |
| 789 | 721 | direction, currentIndex, newIndex, len(stackNode.stackedPanes)) | |
| @@ -793,16 +725,17 @@ func (wm *WorkspaceManager) navigateStack(direction string) bool { | |||
| 793 | 725 | const focusEpsilon = 1e-3 | |
| 794 | 726 | ||
| 795 | 727 | func (wm *WorkspaceManager) focusAdjacent(direction string) bool { | |
| 796 | - | if wm.active == nil || !wm.active.isLeaf || wm.active.container == 0 { | |
| 728 | + | if wm.currentlyFocused == nil || !wm.currentlyFocused.isLeaf || wm.currentlyFocused.container == 0 { | |
| 797 | 729 | return false | |
| 798 | 730 | } | |
| 799 | 731 | ||
| 800 | - | if neighbor := wm.structuralNeighbor(wm.active, direction); neighbor != nil { | |
| 801 | - | wm.focusNode(neighbor) | |
| 732 | + | if neighbor := wm.structuralNeighbor(wm.currentlyFocused, direction); neighbor != nil { | |
| 733 | + | wm.currentlyFocused = neighbor | |
| 734 | + | wm.focusManager.SetActivePane(neighbor) | |
| 802 | 735 | return true | |
| 803 | 736 | } | |
| 804 | 737 | ||
| 805 | - | currentBounds, ok := webkit.WidgetGetBounds(wm.active.container) | |
| 738 | + | currentBounds, ok := webkit.WidgetGetBounds(wm.currentlyFocused.container) | |
| 806 | 739 | if !ok { | |
| 807 | 740 | log.Printf("[workspace] unable to compute bounds for active pane") | |
| 808 | 741 | return false | |
| @@ -817,7 +750,7 @@ func (wm *WorkspaceManager) focusAdjacent(direction string) bool { | |||
| 817 | 750 | var debugCandidates []string | |
| 818 | 751 | ||
| 819 | 752 | for _, candidate := range leaves { | |
| 820 | - | if candidate == nil || candidate == wm.active || candidate.container == 0 { | |
| 753 | + | if candidate == nil || candidate == wm.currentlyFocused || candidate.container == 0 { | |
| 821 | 754 | continue | |
| 822 | 755 | } | |
| 823 | 756 | bounds, ok := webkit.WidgetGetBounds(candidate.container) | |
| @@ -865,12 +798,13 @@ func (wm *WorkspaceManager) focusAdjacent(direction string) bool { | |||
| 865 | 798 | } | |
| 866 | 799 | ||
| 867 | 800 | if best != nil { | |
| 868 | - | wm.focusNode(best) | |
| 801 | + | wm.currentlyFocused = best | |
| 802 | + | wm.focusManager.SetActivePane(best) | |
| 869 | 803 | return true | |
| 870 | 804 | } | |
| 871 | 805 | ||
| 872 | 806 | if len(debugCandidates) > 0 { | |
| 873 | - | log.Printf("[workspace] focusAdjacent no candidate direction=%s current=%#x candidates=%s", direction, wm.active.container, strings.Join(debugCandidates, "; ")) | |
| 807 | + | log.Printf("[workspace] focusAdjacent no candidate direction=%s current=%#x candidates=%s", direction, wm.currentlyFocused.container, strings.Join(debugCandidates, "; ")) | |
| 874 | 808 | } | |
| 875 | 809 | return false | |
| 876 | 810 | } | |
| @@ -1041,6 +975,16 @@ func (wm *WorkspaceManager) collectLeavesFrom(node *paneNode) []*paneNode { | |||
| 1041 | 975 | leaves = append(leaves, n) | |
| 1042 | 976 | return | |
| 1043 | 977 | } | |
| 978 | + | ||
| 979 | + | // Handle stacked panes - traverse the stacked panes list | |
| 980 | + | if n.isStacked && len(n.stackedPanes) > 0 { | |
| 981 | + | for _, stackedPane := range n.stackedPanes { | |
| 982 | + | walk(stackedPane, depth+1) | |
| 983 | + | } | |
| 984 | + | return | |
| 985 | + | } | |
| 986 | + | ||
| 987 | + | // Handle regular split nodes | |
| 1044 | 988 | walk(n.left, depth+1) | |
| 1045 | 989 | walk(n.right, depth+1) | |
| 1046 | 990 | } | |
| @@ -1067,6 +1011,32 @@ func (wm *WorkspaceManager) splitNode(target *paneNode, direction string) (*pane | |||
| 1067 | 1011 | return nil, errors.New("split target must be a leaf pane") | |
| 1068 | 1012 | } | |
| 1069 | 1013 | ||
| 1014 | + | // IMPORTANT: If the target is inside a stacked pane, we need to split AROUND the stack, | |
| 1015 | + | // not the individual pane or the stack itself. We create a new split at the stack's parent level. | |
| 1016 | + | originalTarget := target | |
| 1017 | + | var stackContainer *paneNode | |
| 1018 | + | if target.parent != nil && target.parent.isStacked { | |
| 1019 | + | // Target is in a stack - we'll create the split around the entire stack | |
| 1020 | + | stackContainer = target.parent | |
| 1021 | + | log.Printf("[workspace] target is in stack, will split around the stack: originalTarget=%p stackContainer=%p", originalTarget, stackContainer) | |
| 1022 | + | } | |
| 1023 | + | ||
| 1024 | + | // Determine what we're actually splitting | |
| 1025 | + | var splitTarget *paneNode | |
| 1026 | + | var splitTargetContainer uintptr | |
| 1027 | + | ||
| 1028 | + | if stackContainer != nil { | |
| 1029 | + | // Case: splitting from inside a stack - we split around the entire stack | |
| 1030 | + | splitTarget = stackContainer | |
| 1031 | + | splitTargetContainer = stackContainer.container | |
| 1032 | + | log.Printf("[workspace] splitting around stack: stackContainer=%p container=%#x", splitTarget, splitTargetContainer) | |
| 1033 | + | } else { | |
| 1034 | + | // Case: normal split from a simple pane | |
| 1035 | + | splitTarget = target | |
| 1036 | + | splitTargetContainer = target.container | |
| 1037 | + | log.Printf("[workspace] normal split from pane: target=%p container=%#x", splitTarget, splitTargetContainer) | |
| 1038 | + | } | |
| 1039 | + | ||
| 1070 | 1040 | newView, err := wm.createWebView() | |
| 1071 | 1041 | if err != nil { | |
| 1072 | 1042 | return nil, err | |
| @@ -1089,16 +1059,25 @@ func (wm *WorkspaceManager) splitNode(target *paneNode, direction string) (*pane | |||
| 1089 | 1059 | webkit.WidgetSetVExpand(newContainer, true) | |
| 1090 | 1060 | webkit.WidgetRealizeInContainer(newContainer) | |
| 1091 | 1061 | ||
| 1092 | - | existingContainer := target.container | |
| 1093 | - | if existingContainer == 0 { | |
| 1094 | - | return nil, errors.New("existing pane missing container") | |
| 1062 | + | // Use the determined split target container | |
| 1063 | + | if splitTargetContainer == 0 { | |
| 1064 | + | return nil, errors.New("split target missing container") | |
| 1065 | + | } | |
| 1066 | + | ||
| 1067 | + | // Only apply GTK operations if this is a simple widget (leaf pane) | |
| 1068 | + | // For stacked containers, we need to be more careful | |
| 1069 | + | if splitTarget.isLeaf { | |
| 1070 | + | webkit.WidgetSetHExpand(splitTargetContainer, true) | |
| 1071 | + | webkit.WidgetSetVExpand(splitTargetContainer, true) | |
| 1072 | + | webkit.WidgetRealizeInContainer(splitTargetContainer) | |
| 1073 | + | } else if splitTarget.isStacked { | |
| 1074 | + | // For stacked containers, the wrapper should already be properly configured | |
| 1075 | + | log.Printf("[workspace] skipping GTK setup for stack wrapper: %#x", splitTargetContainer) | |
| 1095 | 1076 | } | |
| 1096 | - | webkit.WidgetSetHExpand(existingContainer, true) | |
| 1097 | - | webkit.WidgetSetVExpand(existingContainer, true) | |
| 1098 | - | webkit.WidgetRealizeInContainer(existingContainer) | |
| 1099 | 1077 | ||
| 1100 | 1078 | orientation, existingFirst := mapDirection(direction) | |
| 1101 | - | log.Printf("[workspace] splitting direction=%s orientation=%v existingFirst=%v target.parent=%p", direction, orientation, existingFirst, target.parent) | |
| 1079 | + | log.Printf("[workspace] splitting direction=%s orientation=%v existingFirst=%v splitTarget.parent=%p splitTarget.isStacked=%v", | |
| 1080 | + | direction, orientation, existingFirst, splitTarget.parent, splitTarget.isStacked) | |
| 1102 | 1081 | ||
| 1103 | 1082 | paned := webkit.NewPaned(orientation) | |
| 1104 | 1083 | if paned == 0 { | |
| @@ -1115,7 +1094,7 @@ func (wm *WorkspaceManager) splitNode(target *paneNode, direction string) (*pane | |||
| 1115 | 1094 | isLeaf: true, | |
| 1116 | 1095 | } | |
| 1117 | 1096 | split := &paneNode{ | |
| 1118 | - | parent: target.parent, | |
| 1097 | + | parent: splitTarget.parent, | |
| 1119 | 1098 | container: paned, | |
| 1120 | 1099 | orientation: orientation, | |
| 1121 | 1100 | isLeaf: false, | |
| @@ -1123,90 +1102,97 @@ func (wm *WorkspaceManager) splitNode(target *paneNode, direction string) (*pane | |||
| 1123 | 1102 | ||
| 1124 | 1103 | parent := split.parent | |
| 1125 | 1104 | ||
| 1126 | - | // Detach existing container from its current GTK parent before inserting into new paned. | |
| 1105 | + | // Detach split target container from its current GTK parent before inserting into new paned. | |
| 1127 | 1106 | if parent == nil { | |
| 1128 | - | // Target is the root - remove it from the window and unparent it | |
| 1129 | - | log.Printf("[workspace] removing existing container=%#x from window", existingContainer) | |
| 1107 | + | // Split target is the root - remove it from the window | |
| 1108 | + | log.Printf("[workspace] removing split target container=%#x from window", splitTargetContainer) | |
| 1130 | 1109 | if wm.window != nil { | |
| 1131 | 1110 | wm.window.SetChild(0) | |
| 1132 | 1111 | } | |
| 1112 | + | // For root widgets, only unparent if they actually have a GTK parent | |
| 1113 | + | if webkit.WidgetGetParent(splitTargetContainer) != 0 { | |
| 1114 | + | webkit.WidgetUnparent(splitTargetContainer) | |
| 1115 | + | } | |
| 1133 | 1116 | } else if parent.container != 0 { | |
| 1134 | - | // Target has a parent paned - unparent it from there | |
| 1135 | - | log.Printf("[workspace] unparenting existing container=%#x from parent paned=%#x", existingContainer, parent.container) | |
| 1136 | - | if parent.left == target { | |
| 1117 | + | // Split target has a parent paned - remove it (automatically unparents in GTK4) | |
| 1118 | + | log.Printf("[workspace] unparenting split target container=%#x from parent paned=%#x", splitTargetContainer, parent.container) | |
| 1119 | + | if parent.left == splitTarget { | |
| 1137 | 1120 | webkit.PanedSetStartChild(parent.container, 0) | |
| 1138 | - | } else if parent.right == target { | |
| 1121 | + | } else if parent.right == splitTarget { | |
| 1139 | 1122 | webkit.PanedSetEndChild(parent.container, 0) | |
| 1140 | 1123 | } | |
| 1141 | - | } | |
| 1142 | - | ||
| 1143 | - | // Properly unparent the widget from GTK's perspective | |
| 1144 | - | webkit.WidgetUnparent(existingContainer) | |
| 1145 | - | if parent != nil && parent.container != 0 { | |
| 1124 | + | // GTK4 automatically unparents when we set paned child to 0 - no manual unparent needed | |
| 1146 | 1125 | webkit.WidgetQueueAllocate(parent.container) | |
| 1147 | 1126 | } | |
| 1148 | 1127 | ||
| 1149 | 1128 | // Set up the tree structure first | |
| 1150 | 1129 | if existingFirst { | |
| 1151 | - | split.left = target | |
| 1130 | + | split.left = splitTarget | |
| 1152 | 1131 | split.right = newLeaf | |
| 1153 | 1132 | } else { | |
| 1154 | 1133 | split.left = newLeaf | |
| 1155 | - | split.right = target | |
| 1134 | + | split.right = splitTarget | |
| 1156 | 1135 | } | |
| 1157 | 1136 | ||
| 1158 | - | target.parent = split | |
| 1137 | + | splitTarget.parent = split | |
| 1159 | 1138 | newLeaf.parent = split | |
| 1160 | 1139 | ||
| 1161 | 1140 | // Update tree root/parent references | |
| 1162 | 1141 | if parent == nil { | |
| 1163 | 1142 | wm.root = split | |
| 1164 | 1143 | } else { | |
| 1165 | - | if parent.left == target { | |
| 1144 | + | if parent.left == splitTarget { | |
| 1166 | 1145 | parent.left = split | |
| 1167 | - | } else if parent.right == target { | |
| 1146 | + | } else if parent.right == splitTarget { | |
| 1168 | 1147 | parent.right = split | |
| 1169 | 1148 | } | |
| 1170 | 1149 | } | |
| 1171 | 1150 | ||
| 1172 | - | // Use idle callback to ensure widget unparenting is complete before re-parenting | |
| 1173 | - | webkit.IdleAdd(func() bool { | |
| 1174 | - | // Add both containers to the new paned | |
| 1175 | - | if existingFirst { | |
| 1176 | - | webkit.PanedSetStartChild(paned, existingContainer) | |
| 1177 | - | webkit.PanedSetEndChild(paned, newContainer) | |
| 1178 | - | log.Printf("[workspace] added existing=%#x as start child, new=%#x as end child", existingContainer, newContainer) | |
| 1179 | - | } else { | |
| 1180 | - | webkit.PanedSetStartChild(paned, newContainer) | |
| 1181 | - | webkit.PanedSetEndChild(paned, existingContainer) | |
| 1182 | - | log.Printf("[workspace] added new=%#x as start child, existing=%#x as end child", newContainer, existingContainer) | |
| 1183 | - | } | |
| 1151 | + | // Ensure widget operations are complete before re-parenting (GTK test pattern) | |
| 1152 | + | webkit.WidgetWaitForDraw(splitTargetContainer) | |
| 1153 | + | webkit.WidgetWaitForDraw(newContainer) | |
| 1184 | 1154 | ||
| 1185 | - | // Attach the new paned to its parent | |
| 1186 | - | if parent == nil { | |
| 1187 | - | if wm.window != nil { | |
| 1188 | - | wm.window.SetChild(paned) | |
| 1189 | - | webkit.WidgetQueueAllocate(paned) | |
| 1190 | - | log.Printf("[workspace] paned set as window child: paned=%#x", paned) | |
| 1191 | - | } | |
| 1192 | - | } else { | |
| 1193 | - | if parent.left == split { | |
| 1194 | - | webkit.PanedSetStartChild(parent.container, paned) | |
| 1195 | - | } else if parent.right == split { | |
| 1196 | - | webkit.PanedSetEndChild(parent.container, paned) | |
| 1197 | - | } | |
| 1198 | - | webkit.WidgetQueueAllocate(parent.container) | |
| 1155 | + | // Add both containers to the new paned | |
| 1156 | + | if existingFirst { | |
| 1157 | + | webkit.PanedSetStartChild(paned, splitTargetContainer) | |
| 1158 | + | webkit.PanedSetEndChild(paned, newContainer) | |
| 1159 | + | log.Printf("[workspace] added splitTarget=%#x as start child, new=%#x as end child", splitTargetContainer, newContainer) | |
| 1160 | + | } else { | |
| 1161 | + | webkit.PanedSetStartChild(paned, newContainer) | |
| 1162 | + | webkit.PanedSetEndChild(paned, splitTargetContainer) | |
| 1163 | + | log.Printf("[workspace] added new=%#x as start child, splitTarget=%#x as end child", newContainer, splitTargetContainer) | |
| 1164 | + | } | |
| 1165 | + | ||
| 1166 | + | // Attach the new paned to its parent | |
| 1167 | + | if parent == nil { | |
| 1168 | + | if wm.window != nil { | |
| 1169 | + | wm.window.SetChild(paned) | |
| 1199 | 1170 | webkit.WidgetQueueAllocate(paned) | |
| 1200 | - | log.Printf("[workspace] paned inserted into parent=%#x", parent.container) | |
| 1171 | + | log.Printf("[workspace] paned set as window child: paned=%#x", paned) | |
| 1201 | 1172 | } | |
| 1173 | + | } else { | |
| 1174 | + | if parent.left == split { | |
| 1175 | + | webkit.PanedSetStartChild(parent.container, paned) | |
| 1176 | + | } else if parent.right == split { | |
| 1177 | + | webkit.PanedSetEndChild(parent.container, paned) | |
| 1178 | + | } | |
| 1179 | + | webkit.WidgetQueueAllocate(parent.container) | |
| 1180 | + | webkit.WidgetQueueAllocate(paned) | |
| 1181 | + | log.Printf("[workspace] paned inserted into parent=%#x", parent.container) | |
| 1182 | + | } | |
| 1202 | 1183 | ||
| 1203 | - | webkit.WidgetShow(paned) | |
| 1204 | - | return false // Run once | |
| 1205 | - | }) | |
| 1184 | + | webkit.WidgetShow(paned) | |
| 1206 | 1185 | ||
| 1207 | 1186 | wm.viewToNode[newPane.webView] = newLeaf | |
| 1208 | 1187 | wm.ensureHover(newLeaf) | |
| 1209 | - | wm.ensureHover(target) | |
| 1188 | + | ||
| 1189 | + | // For stacked panes, we need to ensure hover on the original target, not the stack container | |
| 1190 | + | if originalTarget != target { | |
| 1191 | + | wm.ensureHover(originalTarget) | |
| 1192 | + | } else { | |
| 1193 | + | wm.ensureHover(target) | |
| 1194 | + | } | |
| 1195 | + | ||
| 1210 | 1196 | wm.app.panes = append(wm.app.panes, newPane) | |
| 1211 | 1197 | if newPane.zoomController != nil { | |
| 1212 | 1198 | newPane.zoomController.ApplyInitialZoom() | |
| @@ -1219,7 +1205,8 @@ func (wm *WorkspaceManager) splitNode(target *paneNode, direction string) (*pane | |||
| 1219 | 1205 | if newContainer != 0 { | |
| 1220 | 1206 | webkit.WidgetShow(newContainer) | |
| 1221 | 1207 | } | |
| 1222 | - | wm.focusNode(newLeaf) | |
| 1208 | + | wm.currentlyFocused = newLeaf | |
| 1209 | + | wm.focusManager.SetActivePane(newLeaf) | |
| 1223 | 1210 | return false | |
| 1224 | 1211 | }) | |
| 1225 | 1212 | ||
| @@ -1255,30 +1242,81 @@ func (wm *WorkspaceManager) stackPane(target *paneNode) (*paneNode, error) { | |||
| 1255 | 1242 | webkit.WidgetSetVExpand(newContainer, true) | |
| 1256 | 1243 | webkit.WidgetRealizeInContainer(newContainer) | |
| 1257 | 1244 | ||
| 1258 | - | // If target is not already stacked, convert it to a stacked node | |
| 1259 | - | if !target.isStacked { | |
| 1245 | + | var stackNode *paneNode | |
| 1246 | + | var insertIndex int | |
| 1247 | + | ||
| 1248 | + | // Check if target is already in a stack | |
| 1249 | + | if target.parent != nil && target.parent.isStacked { | |
| 1250 | + | // Target is already in a stack - find the stack container and insertion point | |
| 1251 | + | stackNode = target.parent | |
| 1252 | + | ||
| 1253 | + | // Find the current position of the target pane in the stack | |
| 1254 | + | currentIndex := -1 | |
| 1255 | + | for i, pane := range stackNode.stackedPanes { | |
| 1256 | + | if pane == target { | |
| 1257 | + | currentIndex = i | |
| 1258 | + | break | |
| 1259 | + | } | |
| 1260 | + | } | |
| 1261 | + | ||
| 1262 | + | if currentIndex == -1 { | |
| 1263 | + | return nil, errors.New("target pane not found in stack") | |
| 1264 | + | } | |
| 1265 | + | ||
| 1266 | + | // Insert the new pane right after the current pane | |
| 1267 | + | insertIndex = currentIndex + 1 | |
| 1268 | + | log.Printf("[workspace] adding to existing stack: currentIndex=%d insertIndex=%d stackSize=%d", | |
| 1269 | + | currentIndex, insertIndex, len(stackNode.stackedPanes)) | |
| 1270 | + | } else if target.isStacked { | |
| 1271 | + | // This shouldn't happen with current structure, but handle it | |
| 1272 | + | return nil, errors.New("target pane has inconsistent stacking state") | |
| 1273 | + | } else { | |
| 1274 | + | // Target is not stacked - create initial stack | |
| 1260 | 1275 | log.Printf("[workspace] converting pane to stacked: %p", target) | |
| 1261 | 1276 | ||
| 1262 | - | // Create a vertical box container for the stack | |
| 1263 | - | stackContainer := webkit.NewBox(webkit.OrientationVertical, 0) | |
| 1264 | - | if stackContainer == 0 { | |
| 1265 | - | return nil, errors.New("failed to create stack container") | |
| 1277 | + | // Create the wrapper container - this is what will be used by splitNode | |
| 1278 | + | stackWrapperContainer := webkit.NewBox(webkit.OrientationVertical, 0) | |
| 1279 | + | if stackWrapperContainer == 0 { | |
| 1280 | + | return nil, errors.New("failed to create stack wrapper container") | |
| 1281 | + | } | |
| 1282 | + | webkit.WidgetSetHExpand(stackWrapperContainer, true) | |
| 1283 | + | webkit.WidgetSetVExpand(stackWrapperContainer, true) | |
| 1284 | + | ||
| 1285 | + | // Create the internal box for the actual stacked widgets (titles + webviews) | |
| 1286 | + | stackInternalBox := webkit.NewBox(webkit.OrientationVertical, 0) | |
| 1287 | + | if stackInternalBox == 0 { | |
| 1288 | + | return nil, errors.New("failed to create stack internal box") | |
| 1266 | 1289 | } | |
| 1267 | - | webkit.WidgetSetHExpand(stackContainer, true) | |
| 1268 | - | webkit.WidgetSetVExpand(stackContainer, true) | |
| 1290 | + | webkit.WidgetSetHExpand(stackInternalBox, true) | |
| 1291 | + | webkit.WidgetSetVExpand(stackInternalBox, true) | |
| 1292 | + | ||
| 1293 | + | // The internal box goes inside the wrapper | |
| 1294 | + | webkit.BoxAppend(stackWrapperContainer, stackInternalBox) | |
| 1269 | 1295 | ||
| 1270 | 1296 | // Get the existing container and parent info | |
| 1271 | 1297 | existingContainer := target.container | |
| 1272 | 1298 | parent := target.parent | |
| 1273 | 1299 | ||
| 1274 | - | // Detach existing container from its parent | |
| 1300 | + | // Create title bar for the existing pane | |
| 1301 | + | titleBar := wm.createTitleBar(target) | |
| 1302 | + | ||
| 1303 | + | // Keep existing container visible during transition to prevent rendering glitch | |
| 1304 | + | // webkit.WidgetSetVisible(existingContainer, false) // REMOVED - causes rendering block | |
| 1305 | + | webkit.WidgetSetVisible(titleBar, false) | |
| 1306 | + | ||
| 1307 | + | // Detach existing container from its current parent first | |
| 1308 | + | // This is necessary before we can add it to the stack | |
| 1275 | 1309 | if parent == nil { | |
| 1276 | 1310 | // Target is the root - remove it from window | |
| 1277 | 1311 | if wm.window != nil { | |
| 1278 | 1312 | wm.window.SetChild(0) | |
| 1279 | 1313 | } | |
| 1314 | + | // Unparent if it has a GTK parent | |
| 1315 | + | if webkit.WidgetGetParent(existingContainer) != 0 { | |
| 1316 | + | webkit.WidgetUnparent(existingContainer) | |
| 1317 | + | } | |
| 1280 | 1318 | } else if parent.container != 0 { | |
| 1281 | - | // Target has a parent - unparent it | |
| 1319 | + | // Target has a parent paned - remove it (automatically unparents in GTK4) | |
| 1282 | 1320 | if parent.left == target { | |
| 1283 | 1321 | webkit.PanedSetStartChild(parent.container, 0) | |
| 1284 | 1322 | } else if parent.right == target { | |
| @@ -1286,59 +1324,62 @@ func (wm *WorkspaceManager) stackPane(target *paneNode) (*paneNode, error) { | |||
| 1286 | 1324 | } | |
| 1287 | 1325 | } | |
| 1288 | 1326 | ||
| 1289 | - | webkit.WidgetUnparent(existingContainer) | |
| 1327 | + | // Build the complete stack structure with hidden widgets | |
| 1328 | + | webkit.BoxAppend(stackInternalBox, titleBar) | |
| 1329 | + | webkit.BoxAppend(stackInternalBox, existingContainer) | |
| 1290 | 1330 | ||
| 1291 | - | // Create title bar for the existing pane | |
| 1292 | - | titleBar := wm.createTitleBar(target) | |
| 1293 | - | ||
| 1294 | - | // Add ONLY the title bar for the first pane to the stack container initially | |
| 1295 | - | // The webview content will be added when this pane becomes active | |
| 1296 | - | webkit.BoxAppend(stackContainer, titleBar) | |
| 1297 | - | webkit.BoxAppend(stackContainer, existingContainer) | |
| 1331 | + | // Immediately reattach the stack wrapper to minimize visibility gap | |
| 1332 | + | if parent == nil { | |
| 1333 | + | if wm.window != nil { | |
| 1334 | + | wm.window.SetChild(stackWrapperContainer) | |
| 1335 | + | } | |
| 1336 | + | } else if parent.container != 0 { | |
| 1337 | + | if parent.left == target { | |
| 1338 | + | webkit.PanedSetStartChild(parent.container, stackWrapperContainer) | |
| 1339 | + | } else if parent.right == target { | |
| 1340 | + | webkit.PanedSetEndChild(parent.container, stackWrapperContainer) | |
| 1341 | + | } | |
| 1342 | + | } | |
| 1298 | 1343 | ||
| 1299 | - | // Convert target to a stacked node but keep it as a leaf for content management | |
| 1344 | + | // Convert target to a stacked leaf node | |
| 1300 | 1345 | target.isStacked = true | |
| 1301 | - | target.isLeaf = true // Keep as leaf so updateStackVisibility works correctly | |
| 1302 | - | target.container = existingContainer // Keep original container for visibility control | |
| 1346 | + | target.isLeaf = true | |
| 1347 | + | target.container = existingContainer | |
| 1303 | 1348 | target.titleBar = titleBar | |
| 1304 | 1349 | ||
| 1305 | - | // Create a parent stacked container node | |
| 1306 | - | stackNode := &paneNode{ | |
| 1350 | + | // Create the stack container node - container points to wrapper, stackWrapper points to internal box | |
| 1351 | + | stackNode = &paneNode{ | |
| 1307 | 1352 | isStacked: true, | |
| 1308 | 1353 | isLeaf: false, | |
| 1309 | - | container: stackContainer, | |
| 1354 | + | container: stackWrapperContainer, // Wrapper for GTK operations (splits, etc.) | |
| 1355 | + | stackWrapper: stackInternalBox, // Internal box for stack operations | |
| 1310 | 1356 | stackedPanes: []*paneNode{target}, | |
| 1311 | - | activeStackIndex: 1, // NEW PANE WILL BE ACTIVE (index 1) | |
| 1357 | + | activeStackIndex: 0, // KEEP CURRENT PANE ACTIVE during transition (index 0) | |
| 1312 | 1358 | parent: parent, | |
| 1313 | 1359 | } | |
| 1314 | 1360 | ||
| 1315 | 1361 | // Update target's parent to be the stack node | |
| 1316 | 1362 | target.parent = stackNode | |
| 1317 | 1363 | ||
| 1318 | - | // Update parent references | |
| 1364 | + | // Update parent references (GTK operations already done above) | |
| 1319 | 1365 | if parent == nil { | |
| 1320 | 1366 | wm.root = stackNode | |
| 1321 | - | if wm.window != nil { | |
| 1322 | - | wm.window.SetChild(stackContainer) | |
| 1323 | - | } | |
| 1324 | 1367 | } else { | |
| 1325 | 1368 | if parent.left == target { | |
| 1326 | 1369 | parent.left = stackNode | |
| 1327 | - | webkit.PanedSetStartChild(parent.container, stackContainer) | |
| 1328 | 1370 | } else if parent.right == target { | |
| 1329 | 1371 | parent.right = stackNode | |
| 1330 | - | webkit.PanedSetEndChild(parent.container, stackContainer) | |
| 1331 | 1372 | } | |
| 1332 | 1373 | } | |
| 1333 | 1374 | ||
| 1334 | - | // Update target to point to the stack node for further operations | |
| 1335 | - | target = stackNode | |
| 1375 | + | // New pane will be inserted at index 1 (after the original pane) | |
| 1376 | + | insertIndex = 1 | |
| 1336 | 1377 | } | |
| 1337 | 1378 | ||
| 1338 | 1379 | // Create new leaf node for the new pane | |
| 1339 | 1380 | newLeaf := &paneNode{ | |
| 1340 | 1381 | pane: newPane, | |
| 1341 | - | parent: target, | |
| 1382 | + | parent: stackNode, | |
| 1342 | 1383 | container: newContainer, | |
| 1343 | 1384 | isLeaf: true, | |
| 1344 | 1385 | } | |
| @@ -1347,18 +1388,26 @@ func (wm *WorkspaceManager) stackPane(target *paneNode) (*paneNode, error) { | |||
| 1347 | 1388 | newTitleBar := wm.createTitleBar(newLeaf) | |
| 1348 | 1389 | newLeaf.titleBar = newTitleBar | |
| 1349 | 1390 | ||
| 1350 | - | // Add new pane to the stack | |
| 1351 | - | target.stackedPanes = append(target.stackedPanes, newLeaf) | |
| 1352 | - | webkit.BoxAppend(target.container, newTitleBar) | |
| 1391 | + | // Keep new container hidden initially - will be shown after transition | |
| 1392 | + | webkit.WidgetSetVisible(newContainer, false) | |
| 1393 | + | webkit.WidgetSetVisible(newTitleBar, false) | |
| 1394 | + | ||
| 1395 | + | // Insert the new pane at the correct position in the slice | |
| 1396 | + | stackNode.stackedPanes = append(stackNode.stackedPanes, nil) // Expand slice | |
| 1397 | + | copy(stackNode.stackedPanes[insertIndex+1:], stackNode.stackedPanes[insertIndex:]) // Shift elements | |
| 1398 | + | stackNode.stackedPanes[insertIndex] = newLeaf // Insert new pane | |
| 1353 | 1399 | ||
| 1354 | - | // Unparent the new container before adding it to the stack | |
| 1355 | - | webkit.WidgetUnparent(newContainer) | |
| 1356 | - | webkit.BoxAppend(target.container, newContainer) | |
| 1400 | + | // Unparent the new container before adding it to the stack (only if it has a parent) | |
| 1401 | + | if webkit.WidgetGetParent(newContainer) != 0 { | |
| 1402 | + | webkit.WidgetUnparent(newContainer) | |
| 1403 | + | } | |
| 1357 | 1404 | ||
| 1358 | - | // Ensure the activeStackIndex is correct (should be 1 for the new pane) | |
| 1359 | - | target.activeStackIndex = len(target.stackedPanes) - 1 | |
| 1405 | + | // Add the new widgets to the internal stack box (not the wrapper) | |
| 1406 | + | // GTK will handle the visual order correctly as long as we manage visibility properly | |
| 1407 | + | webkit.BoxAppend(stackNode.stackWrapper, newTitleBar) | |
| 1408 | + | webkit.BoxAppend(stackNode.stackWrapper, newContainer) | |
| 1360 | 1409 | ||
| 1361 | - | // Update workspace state FIRST | |
| 1410 | + | // Update workspace state first | |
| 1362 | 1411 | wm.viewToNode[newPane.webView] = newLeaf | |
| 1363 | 1412 | wm.ensureHover(newLeaf) | |
| 1364 | 1413 | wm.app.panes = append(wm.app.panes, newPane) | |
| @@ -1369,19 +1418,112 @@ func (wm *WorkspaceManager) stackPane(target *paneNode) (*paneNode, error) { | |||
| 1369 | 1418 | // Update CSS classes | |
| 1370 | 1419 | wm.ensurePaneBaseClasses() | |
| 1371 | 1420 | ||
| 1372 | - | // Show the new container immediately | |
| 1373 | - | webkit.WidgetShow(newContainer) | |
| 1421 | + | // Mark stack operation timestamp to prevent focus conflicts | |
| 1422 | + | wm.lastStackOperation = time.Now() | |
| 1374 | 1423 | ||
| 1375 | - | // Update stack visibility to show the new pane as active | |
| 1376 | - | wm.updateStackVisibility(target) | |
| 1424 | + | // Update stack visibility to show current state (existing pane still active) | |
| 1425 | + | wm.stackedPaneManager.UpdateStackVisibility(stackNode) | |
| 1377 | 1426 | ||
| 1378 | - | // Focus the new pane IMMEDIATELY (not in IdleAdd) | |
| 1379 | - | wm.focusNode(newLeaf) | |
| 1427 | + | // Transition to the new pane after a brief delay to avoid rendering conflicts | |
| 1428 | + | webkit.IdleAdd(func() bool { | |
| 1429 | + | // Now switch to the new pane | |
| 1430 | + | stackNode.activeStackIndex = insertIndex | |
| 1431 | + | wm.stackedPaneManager.UpdateStackVisibility(stackNode) | |
| 1432 | + | wm.currentlyFocused = newLeaf | |
| 1433 | + | wm.focusManager.SetActivePane(newLeaf) | |
| 1434 | + | return false // Remove idle callback | |
| 1435 | + | }) | |
| 1380 | 1436 | ||
| 1381 | - | log.Printf("[workspace] stacked new pane: target=%p newLeaf=%p stackSize=%d activeIndex=%d", target, newLeaf, len(target.stackedPanes), target.activeStackIndex) | |
| 1437 | + | log.Printf("[workspace] stacked new pane: stackNode=%p newLeaf=%p stackSize=%d activeIndex=%d insertIndex=%d", | |
| 1438 | + | stackNode, newLeaf, len(stackNode.stackedPanes), stackNode.activeStackIndex, insertIndex) | |
| 1382 | 1439 | return newLeaf, nil | |
| 1383 | 1440 | } | |
| 1384 | 1441 | ||
| 1442 | + | // UpdateTitleBar updates the title bar label for a WebView in stacked panes | |
| 1443 | + | func (wm *WorkspaceManager) UpdateTitleBar(webView *webkit.WebView, title string) { | |
| 1444 | + | if wm == nil || webView == nil || title == "" { | |
| 1445 | + | return | |
| 1446 | + | } | |
| 1447 | + | ||
| 1448 | + | // Find the pane node for this WebView | |
| 1449 | + | node, exists := wm.viewToNode[webView] | |
| 1450 | + | if !exists || node == nil || !node.isLeaf { | |
| 1451 | + | return | |
| 1452 | + | } | |
| 1453 | + | ||
| 1454 | + | // Check if this pane is in a stack and has a title bar | |
| 1455 | + | if node.parent != nil && node.parent.isStacked && node.titleBar != 0 { | |
| 1456 | + | // Update the title bar label with the new title | |
| 1457 | + | wm.updateTitleBarLabel(node, title) | |
| 1458 | + | log.Printf("[workspace] updated title bar for WebView %s: %s", webView.ID(), title) | |
| 1459 | + | } | |
| 1460 | + | } | |
| 1461 | + | ||
| 1462 | + | // updateTitleBarLabel updates the label widget within a title bar by recreating it | |
| 1463 | + | func (wm *WorkspaceManager) updateTitleBarLabel(node *paneNode, title string) { | |
| 1464 | + | if node == nil || node.titleBar == 0 || node.parent == nil || !node.parent.isStacked { | |
| 1465 | + | return | |
| 1466 | + | } | |
| 1467 | + | ||
| 1468 | + | // Format the title for display | |
| 1469 | + | displayTitle := title | |
| 1470 | + | if len(displayTitle) > 50 { | |
| 1471 | + | displayTitle = displayTitle[:47] + "..." | |
| 1472 | + | } | |
| 1473 | + | ||
| 1474 | + | // Recreate the title bar with the new title | |
| 1475 | + | stackNode := node.parent | |
| 1476 | + | oldTitleBar := node.titleBar | |
| 1477 | + | ||
| 1478 | + | // Create new title bar with updated title | |
| 1479 | + | newTitleBar := wm.createTitleBarWithTitle(node, displayTitle) | |
| 1480 | + | if newTitleBar == 0 { | |
| 1481 | + | log.Printf("[workspace] failed to create new title bar") | |
| 1482 | + | return | |
| 1483 | + | } | |
| 1484 | + | ||
| 1485 | + | // Replace the old title bar in the internal stack box (not the wrapper) | |
| 1486 | + | // Insert the new one after the old one, then remove the old one | |
| 1487 | + | webkit.BoxInsertChildAfter(stackNode.stackWrapper, newTitleBar, oldTitleBar) | |
| 1488 | + | webkit.BoxRemove(stackNode.stackWrapper, oldTitleBar) | |
| 1489 | + | ||
| 1490 | + | // Update the node reference | |
| 1491 | + | node.titleBar = newTitleBar | |
| 1492 | + | ||
| 1493 | + | log.Printf("[workspace] updated title bar label: %s", displayTitle) | |
| 1494 | + | } | |
| 1495 | + | ||
| 1496 | + | // createTitleBarWithTitle creates a title bar with a specific title | |
| 1497 | + | func (wm *WorkspaceManager) createTitleBarWithTitle(pane *paneNode, title string) uintptr { | |
| 1498 | + | titleBox := webkit.NewBox(webkit.OrientationHorizontal, 8) | |
| 1499 | + | if titleBox == 0 { | |
| 1500 | + | log.Printf("[workspace] failed to create title bar box") | |
| 1501 | + | return 0 | |
| 1502 | + | } | |
| 1503 | + | webkit.WidgetSetHExpand(titleBox, true) | |
| 1504 | + | webkit.WidgetSetVExpand(titleBox, false) | |
| 1505 | + | ||
| 1506 | + | // Use the provided title | |
| 1507 | + | displayTitle := title | |
| 1508 | + | if displayTitle == "" { | |
| 1509 | + | displayTitle = "New Tab" | |
| 1510 | + | } | |
| 1511 | + | ||
| 1512 | + | label := webkit.NewLabel(displayTitle) | |
| 1513 | + | if label != 0 { | |
| 1514 | + | webkit.LabelSetEllipsize(label, webkit.EllipsizeEnd) | |
| 1515 | + | webkit.WidgetSetHExpand(label, true) | |
| 1516 | + | webkit.BoxAppend(titleBox, label) | |
| 1517 | + | } | |
| 1518 | + | ||
| 1519 | + | // Add CSS classes | |
| 1520 | + | webkit.WidgetAddCSSClass(titleBox, "stacked-pane-title") | |
| 1521 | + | webkit.WidgetAddCSSClass(label, "stacked-pane-title-text") | |
| 1522 | + | ||
| 1523 | + | log.Printf("[workspace] created title bar with custom title: box=%#x label=%#x title=%s", titleBox, label, displayTitle) | |
| 1524 | + | return titleBox | |
| 1525 | + | } | |
| 1526 | + | ||
| 1385 | 1527 | // createTitleBar creates a title bar widget for a pane in a stack. | |
| 1386 | 1528 | func (wm *WorkspaceManager) createTitleBar(pane *paneNode) uintptr { | |
| 1387 | 1529 | titleBox := webkit.NewBox(webkit.OrientationHorizontal, 8) | |
| @@ -1417,35 +1559,7 @@ func (wm *WorkspaceManager) createTitleBar(pane *paneNode) uintptr { | |||
| 1417 | 1559 | } | |
| 1418 | 1560 | ||
| 1419 | 1561 | // updateStackVisibility updates the visibility of panes in a stack. | |
| 1420 | - | func (wm *WorkspaceManager) updateStackVisibility(stackNode *paneNode) { | |
| 1421 | - | if !stackNode.isStacked || len(stackNode.stackedPanes) == 0 { | |
| 1422 | - | return | |
| 1423 | - | } | |
| 1424 | - | ||
| 1425 | - | activeIndex := stackNode.activeStackIndex | |
| 1426 | - | if activeIndex < 0 || activeIndex >= len(stackNode.stackedPanes) { | |
| 1427 | - | activeIndex = 0 | |
| 1428 | - | stackNode.activeStackIndex = activeIndex | |
| 1429 | - | } | |
| 1430 | - | ||
| 1431 | - | log.Printf("[workspace] updating stack visibility: activeIndex=%d stackSize=%d", activeIndex, len(stackNode.stackedPanes)) | |
| 1432 | - | ||
| 1433 | - | for i, pane := range stackNode.stackedPanes { | |
| 1434 | - | if i == activeIndex { | |
| 1435 | - | // Show active pane's content, hide its title bar | |
| 1436 | - | webkit.WidgetSetVisible(pane.container, true) | |
| 1437 | - | webkit.WidgetSetVisible(pane.titleBar, false) | |
| 1438 | - | webkit.WidgetAddCSSClass(pane.container, "stacked-pane-active") | |
| 1439 | - | webkit.WidgetRemoveCSSClass(pane.container, "stacked-pane-collapsed") | |
| 1440 | - | } else { | |
| 1441 | - | // Hide inactive pane's content, show its title bar | |
| 1442 | - | webkit.WidgetSetVisible(pane.container, false) | |
| 1443 | - | webkit.WidgetSetVisible(pane.titleBar, true) | |
| 1444 | - | webkit.WidgetAddCSSClass(pane.container, "stacked-pane-collapsed") | |
| 1445 | - | webkit.WidgetRemoveCSSClass(pane.container, "stacked-pane-active") | |
| 1446 | - | } | |
| 1447 | - | } | |
| 1448 | - | } | |
| 1562 | + | // DEPRECATED: Moved to StackedPaneManager | |
| 1449 | 1563 | ||
| 1450 | 1564 | func (wm *WorkspaceManager) clonePaneState(_ *paneNode, target *paneNode) { | |
| 1451 | 1565 | if wm == nil || target == nil { | |
| @@ -1476,10 +1590,10 @@ func (wm *WorkspaceManager) clonePaneState(_ *paneNode, target *paneNode) { | |||
| 1476 | 1590 | } | |
| 1477 | 1591 | ||
| 1478 | 1592 | func (wm *WorkspaceManager) closeCurrentPane() { | |
| 1479 | - | if wm == nil || wm.active == nil { | |
| 1593 | + | if wm == nil || wm.currentlyFocused == nil { | |
| 1480 | 1594 | return | |
| 1481 | 1595 | } | |
| 1482 | - | if err := wm.closePane(wm.active); err != nil { | |
| 1596 | + | if err := wm.closePane(wm.currentlyFocused); err != nil { | |
| 1483 | 1597 | log.Printf("[workspace] close current pane failed: %v", err) | |
| 1484 | 1598 | } | |
| 1485 | 1599 | } | |
| @@ -1494,19 +1608,22 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||
| 1494 | 1608 | ||
| 1495 | 1609 | // Handle closing panes in a stack | |
| 1496 | 1610 | if node.parent != nil && node.parent.isStacked { | |
| 1497 | - | return wm.closeStackedPane(node) | |
| 1611 | + | return wm.stackedPaneManager.CloseStackedPane(node) | |
| 1498 | 1612 | } | |
| 1499 | 1613 | ||
| 1500 | 1614 | if node.parent != nil && node.parent.container != 0 { | |
| 1501 | 1615 | if node.parent.left == node { | |
| 1502 | 1616 | webkit.PanedSetStartChild(node.parent.container, 0) | |
| 1617 | + | // GTK4 automatically unparents when setting child to 0 | |
| 1503 | 1618 | } else if node.parent.right == node { | |
| 1504 | 1619 | webkit.PanedSetEndChild(node.parent.container, 0) | |
| 1620 | + | // GTK4 automatically unparents when setting child to 0 | |
| 1621 | + | } | |
| 1622 | + | } else if node.container != 0 { | |
| 1623 | + | // Only manually unparent if widget wasn't auto-unparented above | |
| 1624 | + | if webkit.WidgetGetParent(node.container) != 0 { | |
| 1625 | + | webkit.WidgetUnparent(node.container) | |
| 1505 | 1626 | } | |
| 1506 | - | } | |
| 1507 | - | ||
| 1508 | - | if node.container != 0 { | |
| 1509 | - | webkit.WidgetUnparent(node.container) | |
| 1510 | 1627 | } | |
| 1511 | 1628 | ||
| 1512 | 1629 | remaining := len(wm.app.panes) | |
| @@ -1586,7 +1703,7 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||
| 1586 | 1703 | } | |
| 1587 | 1704 | ||
| 1588 | 1705 | wm.root = nil | |
| 1589 | - | wm.active = nil | |
| 1706 | + | wm.currentlyFocused = nil | |
| 1590 | 1707 | wm.mainPane = nil | |
| 1591 | 1708 | ||
| 1592 | 1709 | webkit.QuitMainLoop() | |
| @@ -1646,8 +1763,8 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||
| 1646 | 1763 | } | |
| 1647 | 1764 | ||
| 1648 | 1765 | // Clear current active if it's the node being closed | |
| 1649 | - | if wm.active == node { | |
| 1650 | - | wm.active = nil | |
| 1766 | + | if wm.currentlyFocused == node { | |
| 1767 | + | wm.currentlyFocused = nil | |
| 1651 | 1768 | } | |
| 1652 | 1769 | ||
| 1653 | 1770 | // Set replacement as new root | |
| @@ -1666,7 +1783,8 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||
| 1666 | 1783 | // Focus a suitable pane | |
| 1667 | 1784 | focusTarget := wm.leftmostLeaf(replacement) | |
| 1668 | 1785 | if focusTarget != nil { | |
| 1669 | - | wm.focusNode(focusTarget) | |
| 1786 | + | wm.currentlyFocused = focusTarget | |
| 1787 | + | wm.focusManager.SetActivePane(focusTarget) | |
| 1670 | 1788 | } | |
| 1671 | 1789 | ||
| 1672 | 1790 | // Destroy the webview and detach hover AFTER rearranging hierarchy | |
| @@ -1731,8 +1849,8 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||
| 1731 | 1849 | } | |
| 1732 | 1850 | ||
| 1733 | 1851 | // Clear current active if it's the node being closed | |
| 1734 | - | if wm.active == node { | |
| 1735 | - | wm.active = nil | |
| 1852 | + | if wm.currentlyFocused == node { | |
| 1853 | + | wm.currentlyFocused = nil | |
| 1736 | 1854 | } | |
| 1737 | 1855 | ||
| 1738 | 1856 | grand := parent.parent | |
| @@ -1771,6 +1889,12 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||
| 1771 | 1889 | } | |
| 1772 | 1890 | ||
| 1773 | 1891 | // Now safely reparent the sibling to the grandparent | |
| 1892 | + | // Ensure GTK focus state is synchronized before reparenting to prevent | |
| 1893 | + | // GTK-WARNING: "Error finding last focus widget of GtkPaned, gtk_paned_set_focus_child | |
| 1894 | + | // was called on widget (nil) which is not child" | |
| 1895 | + | webkit.WidgetWaitForDraw(sibling.container) | |
| 1896 | + | webkit.WidgetWaitForDraw(grand.container) | |
| 1897 | + | ||
| 1774 | 1898 | sibling.parent = grand | |
| 1775 | 1899 | if grand.container != 0 && !grand.isLeaf { | |
| 1776 | 1900 | if grand.left == parent { | |
| @@ -1796,7 +1920,8 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||
| 1796 | 1920 | ||
| 1797 | 1921 | // Focus the target pane | |
| 1798 | 1922 | if focusTarget != nil && focusTarget != node { | |
| 1799 | - | wm.focusNode(focusTarget) | |
| 1923 | + | wm.currentlyFocused = focusTarget | |
| 1924 | + | wm.focusManager.SetActivePane(focusTarget) | |
| 1800 | 1925 | } | |
| 1801 | 1926 | ||
| 1802 | 1927 | // Destroy the webview and detach hover AFTER all hierarchy changes are complete | |
| @@ -1913,12 +2038,17 @@ func (wm *WorkspaceManager) closeStackedPane(node *paneNode) error { | |||
| 1913 | 2038 | } | |
| 1914 | 2039 | ||
| 1915 | 2040 | // Remove the pane's widgets from the stack container | |
| 2041 | + | stackBox := stackNode.stackWrapper | |
| 2042 | + | if stackBox == 0 { | |
| 2043 | + | stackBox = stackNode.container | |
| 2044 | + | } | |
| 2045 | + | ||
| 1916 | 2046 | if node.titleBar != 0 { | |
| 1917 | - | webkit.BoxRemove(stackNode.container, node.titleBar) | |
| 2047 | + | webkit.BoxRemove(stackBox, node.titleBar) | |
| 1918 | 2048 | } | |
| 1919 | 2049 | if node.container != 0 { | |
| 1920 | - | webkit.BoxRemove(stackNode.container, node.container) | |
| 1921 | - | webkit.WidgetUnparent(node.container) | |
| 2050 | + | webkit.BoxRemove(stackBox, node.container) | |
| 2051 | + | // BoxRemove automatically unparents in GTK4, no manual unparent needed | |
| 1922 | 2052 | } | |
| 1923 | 2053 | ||
| 1924 | 2054 | // Clean up the pane | |
| @@ -1935,51 +2065,47 @@ func (wm *WorkspaceManager) closeStackedPane(node *paneNode) error { | |||
| 1935 | 2065 | } else if len(stackNode.stackedPanes) == 1 { | |
| 1936 | 2066 | // Only one pane left - convert back to a regular pane | |
| 1937 | 2067 | lastPane := stackNode.stackedPanes[0] | |
| 2068 | + | parent := stackNode.parent | |
| 2069 | + | lastPaneContainer := lastPane.container | |
| 1938 | 2070 | ||
| 1939 | 2071 | // Remove the title bar since we're going back to single pane | |
| 1940 | 2072 | if lastPane.titleBar != 0 { | |
| 1941 | - | webkit.BoxRemove(stackNode.container, lastPane.titleBar) | |
| 2073 | + | webkit.BoxRemove(stackBox, lastPane.titleBar) | |
| 1942 | 2074 | } | |
| 1943 | 2075 | ||
| 1944 | - | // Replace the stack container with the pane's container | |
| 1945 | - | parent := stackNode.parent | |
| 1946 | - | lastPaneContainer := lastPane.container | |
| 1947 | - | ||
| 1948 | - | // Unparent from stack container | |
| 1949 | - | webkit.BoxRemove(stackNode.container, lastPaneContainer) | |
| 1950 | - | webkit.WidgetUnparent(lastPaneContainer) | |
| 1951 | - | ||
| 1952 | - | // Convert back to regular pane structure | |
| 1953 | - | lastPane.parent = parent | |
| 1954 | - | lastPane.isStacked = false | |
| 1955 | - | lastPane.stackedPanes = nil | |
| 1956 | - | lastPane.titleBar = 0 | |
| 1957 | - | stackNode.isStacked = false | |
| 1958 | - | stackNode.stackedPanes = nil | |
| 1959 | - | stackNode.container = lastPaneContainer | |
| 1960 | - | stackNode.isLeaf = true | |
| 1961 | - | stackNode.pane = lastPane.pane | |
| 2076 | + | // Unparent the pane container from stack wrapper | |
| 2077 | + | webkit.BoxRemove(stackBox, lastPaneContainer) | |
| 1962 | 2078 | ||
| 1963 | - | // Update parent references | |
| 2079 | + | // CRITICAL FIX: Replace the stackNode completely with lastPane | |
| 2080 | + | // Update parent child references FIRST | |
| 1964 | 2081 | if parent == nil { | |
| 1965 | - | wm.root = stackNode | |
| 2082 | + | wm.root = lastPane | |
| 1966 | 2083 | if wm.window != nil { | |
| 1967 | 2084 | wm.window.SetChild(lastPaneContainer) | |
| 1968 | 2085 | } | |
| 1969 | 2086 | } else { | |
| 1970 | 2087 | if parent.left == stackNode { | |
| 2088 | + | parent.left = lastPane | |
| 1971 | 2089 | webkit.PanedSetStartChild(parent.container, lastPaneContainer) | |
| 1972 | 2090 | } else if parent.right == stackNode { | |
| 2091 | + | parent.right = lastPane | |
| 1973 | 2092 | webkit.PanedSetEndChild(parent.container, lastPaneContainer) | |
| 1974 | 2093 | } | |
| 1975 | 2094 | } | |
| 1976 | 2095 | ||
| 1977 | - | // Update the viewToNode mapping | |
| 1978 | - | wm.viewToNode[lastPane.pane.webView] = stackNode | |
| 2096 | + | // Convert lastPane back to regular pane | |
| 2097 | + | lastPane.parent = parent | |
| 2098 | + | lastPane.isStacked = false | |
| 2099 | + | lastPane.stackedPanes = nil | |
| 2100 | + | lastPane.titleBar = 0 | |
| 2101 | + | ||
| 2102 | + | // Update the viewToNode mapping to point to the lastPane, not stackNode | |
| 2103 | + | wm.viewToNode[lastPane.pane.webView] = lastPane | |
| 1979 | 2104 | ||
| 1980 | 2105 | // Focus the remaining pane if it was the active one being closed | |
| 1981 | - | if wm.active == node { | |
| 1982 | - | wm.focusNode(stackNode) | |
| 2106 | + | if wm.currentlyFocused == node { | |
| 2107 | + | wm.currentlyFocused = lastPane | |
| 2108 | + | wm.focusManager.SetActivePane(lastPane) | |
| 1983 | 2109 | } | |
| 1984 | 2110 | ||
| 1985 | 2111 | log.Printf("[workspace] converted single-pane stack back to regular pane") | |
| @@ -1993,12 +2119,13 @@ func (wm *WorkspaceManager) closeStackedPane(node *paneNode) error { | |||
| 1993 | 2119 | } | |
| 1994 | 2120 | ||
| 1995 | 2121 | // Update visibility for remaining panes | |
| 1996 | - | wm.updateStackVisibility(stackNode) | |
| 2122 | + | wm.stackedPaneManager.UpdateStackVisibility(stackNode) | |
| 1997 | 2123 | ||
| 1998 | 2124 | // Focus the new active pane if we closed the currently active one | |
| 1999 | - | if wm.active == node { | |
| 2125 | + | if wm.currentlyFocused == node { | |
| 2000 | 2126 | newActivePaneInStack := stackNode.stackedPanes[stackNode.activeStackIndex] | |
| 2001 | - | wm.focusNode(newActivePaneInStack) | |
| 2127 | + | wm.currentlyFocused = newActivePaneInStack | |
| 2128 | + | wm.focusManager.SetActivePane(newActivePaneInStack) | |
| 2002 | 2129 | } | |
| 2003 | 2130 | ||
| 2004 | 2131 | log.Printf("[workspace] closed pane from stack: remaining=%d activeIndex=%d", | |
| @@ -2021,8 +2148,8 @@ func (wm *WorkspaceManager) updateMainPane() { | |||
| 2021 | 2148 | } | |
| 2022 | 2149 | ||
| 2023 | 2150 | if wm.mainPane == nil || !wm.mainPane.isLeaf { | |
| 2024 | - | if wm.active != nil && wm.active.isLeaf { | |
| 2025 | - | wm.mainPane = wm.active | |
| 2151 | + | if wm.currentlyFocused != nil && wm.currentlyFocused.isLeaf { | |
| 2152 | + | wm.mainPane = wm.currentlyFocused | |
| 2026 | 2153 | } | |
| 2027 | 2154 | } | |
| 2028 | 2155 | } | |
| @@ -2139,8 +2266,8 @@ func (wm *WorkspaceManager) HandlePopup(source *webkit.WebView, url string) *web | |||
| 2139 | 2266 | ||
| 2140 | 2267 | // Determine target node for splitting | |
| 2141 | 2268 | target := node | |
| 2142 | - | if !popCfg.FollowPaneContext && wm.active != nil { | |
| 2143 | - | target = wm.active | |
| 2269 | + | if !popCfg.FollowPaneContext && wm.currentlyFocused != nil { | |
| 2270 | + | target = wm.currentlyFocused | |
| 2144 | 2271 | } | |
| 2145 | 2272 | ||
| 2146 | 2273 | // Add the popup pane to the workspace using manual pane insertion | |
| @@ -2621,10 +2748,23 @@ func (wm *WorkspaceManager) handleDetectedWindowType(sourceNode *paneNode, webVi | |||
| 2621 | 2748 | ||
| 2622 | 2749 | // insertPopupPane inserts a pre-created popup pane into the workspace | |
| 2623 | 2750 | func (wm *WorkspaceManager) insertPopupPane(target *paneNode, newPane *BrowserPane, direction string) error { | |
| 2624 | - | if target == nil || !target.isLeaf || target.pane == nil { | |
| 2625 | - | return errors.New("insert target must be a leaf pane") | |
| 2751 | + | if target == nil { | |
| 2752 | + | return errors.New("insert target cannot be nil") | |
| 2753 | + | } | |
| 2754 | + | ||
| 2755 | + | // Accept both leaf panes and stacked panes as valid targets | |
| 2756 | + | if !target.isLeaf && !target.isStacked { | |
| 2757 | + | return errors.New("insert target must be a leaf pane or stacked pane") | |
| 2626 | 2758 | } | |
| 2627 | 2759 | ||
| 2760 | + | // For leaf panes, require a pane | |
| 2761 | + | if target.isLeaf && target.pane == nil { | |
| 2762 | + | return errors.New("leaf target missing pane") | |
| 2763 | + | } | |
| 2764 | + | ||
| 2765 | + | // For stacked panes, we trust the caller - production stacks have varied structures | |
| 2766 | + | // Do not validate internal stack structure here | |
| 2767 | + | ||
| 2628 | 2768 | if newPane == nil || newPane.webView == nil { | |
| 2629 | 2769 | return errors.New("new pane missing webview") | |
| 2630 | 2770 | } | |
| @@ -2752,7 +2892,8 @@ func (wm *WorkspaceManager) insertPopupPane(target *paneNode, newPane *BrowserPa | |||
| 2752 | 2892 | if newContainer != 0 { | |
| 2753 | 2893 | webkit.WidgetShow(newContainer) | |
| 2754 | 2894 | } | |
| 2755 | - | wm.focusNode(newLeaf) | |
| 2895 | + | wm.currentlyFocused = newLeaf | |
| 2896 | + | wm.focusManager.SetActivePane(newLeaf) | |
| 2756 | 2897 | return false | |
| 2757 | 2898 | }) | |
| 2758 | 2899 | ||
| @@ -19,7 +19,7 @@ type paneNode struct { | |||||
| 19 | parent *paneNode | 19 | parent *paneNode | ||
| 20 | left *paneNode | 20 | left *paneNode | ||
| 21 | right *paneNode | 21 | right *paneNode | ||
| 22 | - | container uintptr // GtkPaned for branch nodes, GtkBox for stacked nodes, stable WebView container for leaves | 22 | + | container uintptr // GtkPaned for branch nodes, wrapper GtkBox for stacked nodes, stable WebView container for leaves |
| 23 | orientation webkit.Orientation | 23 | orientation webkit.Orientation | ||
| 24 | isLeaf bool | 24 | isLeaf bool | ||
| 25 | isPopup bool // Deprecated: use windowType instead | 25 | isPopup bool // Deprecated: use windowType instead | ||
| @@ -35,6 +35,7 @@ type paneNode struct { | |||||
| 35 | stackedPanes []*paneNode // List of stacked panes (if isStacked) | 35 | stackedPanes []*paneNode // List of stacked panes (if isStacked) | ||
| 36 | activeStackIndex int // Index of currently visible pane in stack | 36 | activeStackIndex int // Index of currently visible pane in stack | ||
| 37 | titleBar uintptr // GtkBox for title bar (when collapsed) | 37 | titleBar uintptr // GtkBox for title bar (when collapsed) | ||
| 38 | + | stackWrapper uintptr // Internal GtkBox containing the actual stacked widgets (titles + webviews) | |||
| 38 | } | 39 | } | ||
| 39 | 40 | ||||
| 40 | // WorkspaceManager coordinates Zellij-style pane operations. | 41 | // WorkspaceManager coordinates Zellij-style pane operations. | ||
| @@ -42,7 +43,6 @@ type WorkspaceManager struct { | |||||
| 42 | app *BrowserApp | 43 | app *BrowserApp | ||
| 43 | window *webkit.Window | 44 | window *webkit.Window | ||
| 44 | root *paneNode | 45 | root *paneNode | ||
| 45 | - | active *paneNode | |||
| 46 | mainPane *paneNode | 46 | mainPane *paneNode | ||
| 47 | viewToNode map[*webkit.WebView]*paneNode | 47 | viewToNode map[*webkit.WebView]*paneNode | ||
| 48 | lastSplitMsg map[*webkit.WebView]time.Time | 48 | lastSplitMsg map[*webkit.WebView]time.Time | ||
| @@ -62,8 +62,18 @@ type WorkspaceManager struct { | |||||
| 62 | lastFocusChange time.Time // When focus was last changed | 62 | lastFocusChange time.Time // When focus was last changed | ||
| 63 | focusThrottleMutex sync.Mutex // Protects focus throttling state | 63 | focusThrottleMutex sync.Mutex // Protects focus throttling state | ||
| 64 | 64 | ||||
| 65 | + | // Stack operation timing to prevent focus conflicts | |||
| 66 | + | lastStackOperation time.Time // When a stack operation was last performed | |||
| 67 | + | ||||
| 65 | // NEW: Pane creation deduplicator | 68 | // NEW: Pane creation deduplicator | ||
| 66 | paneDeduplicator *messaging.PaneRequestDeduplicator | 69 | paneDeduplicator *messaging.PaneRequestDeduplicator | ||
| 70 | + | ||||
| 71 | + | // Specialized managers for different pane operations | |||
| 72 | + | stackedPaneManager *StackedPaneManager | |||
| 73 | + | focusManager *FocusManager | |||
| 74 | + | ||||
| 75 | + | // Current focused pane (Single Source of Truth for focus) | |||
| 76 | + | currentlyFocused *paneNode | |||
| 67 | } | 77 | } | ||
| 68 | 78 | ||||
| 69 | const ( | 79 | const ( | ||
| @@ -84,6 +94,10 @@ func NewWorkspaceManager(app *BrowserApp, rootPane *BrowserPane) *WorkspaceManag | |||||
| 84 | lastExitMsg: make(map[*webkit.WebView]time.Time), | 94 | lastExitMsg: make(map[*webkit.WebView]time.Time), | ||
| 85 | paneDeduplicator: messaging.NewPaneRequestDeduplicator(), // NEW: Initialize deduplicator | 95 | paneDeduplicator: messaging.NewPaneRequestDeduplicator(), // NEW: Initialize deduplicator | ||
| 86 | } | 96 | } | ||
| 97 | + | ||||
| 98 | + | // Initialize specialized managers | |||
| 99 | + | manager.stackedPaneManager = NewStackedPaneManager(manager) | |||
| 100 | + | manager.focusManager = NewFocusManager(manager) | |||
| 87 | manager.createWebViewFn = func() (*webkit.WebView, error) { | 101 | manager.createWebViewFn = func() (*webkit.WebView, error) { | ||
| 88 | if manager.app == nil { | 102 | if manager.app == nil { | ||
| 89 | return nil, errors.New("workspace manager missing app reference") | 103 | return nil, errors.New("workspace manager missing app reference") | ||
| @@ -112,7 +126,6 @@ func NewWorkspaceManager(app *BrowserApp, rootPane *BrowserPane) *WorkspaceManag | |||||
| 112 | isLeaf: true, | 126 | isLeaf: true, | ||
| 113 | } | 127 | } | ||
| 114 | manager.root = root | 128 | manager.root = root | ||
| 115 | - | manager.active = root | |||
| 116 | manager.mainPane = root | 129 | manager.mainPane = root | ||
| 117 | manager.viewToNode[rootPane.webView] = root | 130 | manager.viewToNode[rootPane.webView] = root | ||
| 118 | manager.ensureHover(root) | 131 | manager.ensureHover(root) | ||
| @@ -122,7 +135,8 @@ func NewWorkspaceManager(app *BrowserApp, rootPane *BrowserPane) *WorkspaceManag | |||||
| 122 | } | 135 | } | ||
| 123 | 136 | ||||
| 124 | app.workspace = manager | 137 | app.workspace = manager | ||
| 125 | - | manager.focusNode(root) | 138 | + | manager.currentlyFocused = root |
| 139 | + | manager.focusManager.SetActivePane(root) | |||
| 126 | // Workspace navigation shortcuts are now handled globally by WindowShortcutHandler | 140 | // Workspace navigation shortcuts are now handled globally by WindowShortcutHandler | ||
| 127 | // Ensure initial CSS classes are applied | 141 | // Ensure initial CSS classes are applied | ||
| 128 | manager.ensurePaneBaseClasses() | 142 | manager.ensurePaneBaseClasses() | ||
| @@ -161,7 +175,8 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||||
| 161 | wm.paneModeActive = true | 175 | wm.paneModeActive = true | ||
| 162 | wm.paneModeSource = source | 176 | wm.paneModeSource = source | ||
| 163 | wm.lastPaneModeEntry = time.Now() | 177 | wm.lastPaneModeEntry = time.Now() | ||
| 164 | - | wm.focusNode(node) | 178 | + | wm.currentlyFocused = node |
| 179 | + | wm.focusManager.SetActivePane(node) | |||
| 165 | case "pane-confirmed", "pane-cancelled", "pane-mode-exited": | 180 | case "pane-confirmed", "pane-cancelled", "pane-mode-exited": | ||
| 166 | // Debounce pane-mode-exited events to prevent duplicate focus calls | 181 | // Debounce pane-mode-exited events to prevent duplicate focus calls | ||
| 167 | if event == "pane-mode-exited" { | 182 | if event == "pane-mode-exited" { | ||
| @@ -179,7 +194,7 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||||
| 179 | wm.paneModeSource = nil | 194 | wm.paneModeSource = nil | ||
| 180 | wm.paneMutex.Unlock() | 195 | wm.paneMutex.Unlock() | ||
| 181 | 196 | ||||
| 182 | - | wm.focusNode(node) | 197 | + | wm.focusAfterPaneMode(node) |
| 183 | case "pane-close": | 198 | case "pane-close": | ||
| 184 | if !wm.paneModeActive { | 199 | if !wm.paneModeActive { | ||
| 185 | log.Printf("[workspace] close requested outside pane mode; ignoring") | 200 | log.Printf("[workspace] close requested outside pane mode; ignoring") | ||
| @@ -194,7 +209,8 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||||
| 194 | // Don't focus the node that's about to be closed - closeCurrentPane() will handle focus | 209 | // Don't focus the node that's about to be closed - closeCurrentPane() will handle focus | ||
| 195 | wm.closeCurrentPane() | 210 | wm.closeCurrentPane() | ||
| 196 | case "pane-split": | 211 | case "pane-split": | ||
| 197 | - | wm.focusNode(node) | 212 | + | wm.currentlyFocused = node |
| 213 | + | wm.focusManager.SetActivePane(node) | |||
| 198 | direction := strings.ToLower(msg.Direction) | 214 | direction := strings.ToLower(msg.Direction) | ||
| 199 | if direction == "" { | 215 | if direction == "" { | ||
| 200 | direction = "right" | 216 | direction = "right" | ||
| @@ -220,7 +236,8 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||||
| 220 | wm.clonePaneState(node, newNode) | 236 | wm.clonePaneState(node, newNode) | ||
| 221 | wm.splitting = false | 237 | wm.splitting = false | ||
| 222 | case "pane-stack": | 238 | case "pane-stack": | ||
| 223 | - | wm.focusNode(node) | 239 | + | wm.currentlyFocused = node |
| 240 | + | wm.focusManager.SetActivePane(node) | |||
| 224 | if last, ok := wm.lastSplitMsg[source]; ok { | 241 | if last, ok := wm.lastSplitMsg[source]; ok { | ||
| 225 | if time.Since(last) < 200*time.Millisecond { | 242 | if time.Since(last) < 200*time.Millisecond { | ||
| 226 | log.Printf("[workspace] stack ignored: debounce (%.0fms)", time.Since(last).Seconds()*1000) | 243 | log.Printf("[workspace] stack ignored: debounce (%.0fms)", time.Since(last).Seconds()*1000) | ||
| @@ -233,7 +250,7 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||||
| 233 | } | 250 | } | ||
| 234 | wm.splitting = true | 251 | wm.splitting = true | ||
| 235 | wm.lastSplitMsg[source] = time.Now() | 252 | wm.lastSplitMsg[source] = time.Now() | ||
| 236 | - | newNode, err := wm.stackPane(node) | 253 | + | newNode, err := wm.stackedPaneManager.StackPane(node) |
| 237 | if err != nil { | 254 | if err != nil { | ||
| 238 | log.Printf("[workspace] stack failed: %v", err) | 255 | log.Printf("[workspace] stack failed: %v", err) | ||
| 239 | wm.splitting = false | 256 | wm.splitting = false | ||
| @@ -338,7 +355,7 @@ func (wm *WorkspaceManager) OnWorkspaceMessage(source *webkit.WebView, msg messa | |||||
| 338 | 355 | ||||
| 339 | // GetActiveNode returns the currently active pane node | 356 | // GetActiveNode returns the currently active pane node | ||
| 340 | func (wm *WorkspaceManager) GetActiveNode() *paneNode { | 357 | func (wm *WorkspaceManager) GetActiveNode() *paneNode { | ||
| 341 | - | return wm.active | 358 | + | return wm.currentlyFocused |
| 342 | } | 359 | } | ||
| 343 | 360 | ||||
| 344 | // GetNodeForWebView returns the pane node associated with a WebView | 361 | // GetNodeForWebView returns the pane node associated with a WebView | ||
| @@ -376,123 +393,36 @@ func (wm *WorkspaceManager) DispatchPaneFocusEvent(node *paneNode, active bool) | |||||
| 376 | } | 393 | } | ||
| 377 | } | 394 | } | ||
| 378 | 395 | ||||
| 379 | - | func (wm *WorkspaceManager) focusNode(node *paneNode) { | 396 | + | // DEPRECATED: Moved to FocusManager |
| 380 | - | if node == nil || !node.isLeaf || node.pane == nil || node.pane.webView == nil { | 397 | + | |
| 381 | - | return | 398 | + | // focusAfterPaneMode restores focus when a pane mode operation completes without |
| 382 | - | } | 399 | + | // stealing focus from the active pane inside a stack that just spawned a sibling. |
| 400 | + | func (wm *WorkspaceManager) focusAfterPaneMode(node *paneNode) { | |||
| 401 | + | wm.focusRespectingStack(node, "focus-after-pane-mode") | |||
| 402 | + | } | |||
| 383 | 403 | ||||
| 384 | - | // Don't focus a node that's not in our viewToNode map (likely destroyed) | 404 | + | func (wm *WorkspaceManager) focusRespectingStack(node *paneNode, reason string) { |
| 385 | - | if _, exists := wm.viewToNode[node.pane.webView]; !exists { | 405 | + | if node == nil { |
| 386 | - | log.Printf("[workspace] focusNode: skipping destroyed/unknown webView=%p", node.pane.webView) | |||
| 387 | return | 406 | return | ||
| 388 | } | 407 | } | ||
| 389 | 408 | ||||
| 390 | - | // Handle stacked pane focus: ensure the pane is active in its stack | 409 | + | if stack := node.parent; stack != nil && stack.isStacked { |
| 391 | - | if node.parent != nil && node.parent.isStacked { | 410 | + | activeIndex := stack.activeStackIndex |
| 392 | - | stackNode := node.parent | 411 | + | if activeIndex >= 0 && activeIndex < len(stack.stackedPanes) { |
| 393 | - | // Find the index of this node in the stack | 412 | + | activePane := stack.stackedPanes[activeIndex] |
| 394 | - | for i, stackedPane := range stackNode.stackedPanes { | 413 | + | if activePane != nil && activePane != node { |
| 395 | - | if stackedPane == node { | 414 | + | if reason != "" { |
| 396 | - | if stackNode.activeStackIndex != i { | 415 | + | log.Printf("[workspace] %s: preserving active stacked pane index=%d", reason, activeIndex) |
| 397 | - | stackNode.activeStackIndex = i | |||
| 398 | - | wm.updateStackVisibility(stackNode) | |||
| 399 | - | log.Printf("[workspace] updated stack focus: activeIndex=%d for pane=%p", i, node) | |||
| 400 | } | 416 | } | ||
| 401 | - | break | 417 | + | wm.currentlyFocused = activePane |
| 418 | + | wm.focusManager.SetActivePane(activePane) | |||
| 419 | + | return | |||
| 402 | } | 420 | } | ||
| 403 | } | 421 | } | ||
| 404 | } | 422 | } | ||
| 405 | 423 | ||||
| 406 | - | previous := wm.active | 424 | + | wm.currentlyFocused = node |
| 407 | - | var previousContainer uintptr | 425 | + | wm.focusManager.SetActivePane(node) |
| 408 | - | var previousWebView *webkit.WebView | |||
| 409 | - | if previous != nil && previous != node && previous.pane != nil && previous.pane.webView != nil { | |||
| 410 | - | previousContainer = previous.container | |||
| 411 | - | previousWebView = previous.pane.webView | |||
| 412 | - | wm.DispatchPaneFocusEvent(previous, false) | |||
| 413 | - | } | |||
| 414 | - | ||||
| 415 | - | // Set previously active WebView as inactive | |||
| 416 | - | if previousWebView != nil && previousWebView != node.pane.webView { | |||
| 417 | - | previousWebView.SetActive(false) | |||
| 418 | - | } | |||
| 419 | - | ||||
| 420 | - | wm.active = node | |||
| 421 | - | wm.app.activePane = node.pane | |||
| 422 | - | wm.app.webView = node.pane.webView | |||
| 423 | - | wm.app.zoomController = node.pane.zoomController | |||
| 424 | - | wm.app.navigationController = node.pane.navigationController | |||
| 425 | - | wm.app.clipboardController = node.pane.clipboardController | |||
| 426 | - | wm.app.messageHandler = node.pane.messageHandler | |||
| 427 | - | wm.app.shortcutHandler = node.pane.shortcutHandler | |||
| 428 | - | ||||
| 429 | - | // Set this WebView as active | |||
| 430 | - | if node.pane != nil && node.pane.webView != nil { | |||
| 431 | - | node.pane.webView.SetActive(true) | |||
| 432 | - | } | |||
| 433 | - | ||||
| 434 | - | // Shortcuts are registered once during webView creation, no need to re-register on focus | |||
| 435 | - | ||||
| 436 | - | if handler := node.pane.messageHandler; handler != nil { | |||
| 437 | - | handler.SetWorkspaceObserver(wm) | |||
| 438 | - | } | |||
| 439 | - | ||||
| 440 | - | if wm.app.browserService != nil { | |||
| 441 | - | wm.app.browserService.AttachWebView(node.pane.webView) | |||
| 442 | - | } | |||
| 443 | - | ||||
| 444 | - | // Ensure all panes have proper base classes | |||
| 445 | - | wm.ensurePaneBaseClasses() | |||
| 446 | - | ||||
| 447 | - | container := node.container | |||
| 448 | - | viewWidget := node.pane.webView.Widget() | |||
| 449 | - | if container != 0 && container != previousContainer { | |||
| 450 | - | // Remove active class from previous container to ensure only one pane is visually active | |||
| 451 | - | if previousContainer != 0 { | |||
| 452 | - | webkit.WidgetRemoveCSSClass(previousContainer, activePaneClass) | |||
| 453 | - | } | |||
| 454 | - | ||||
| 455 | - | // Add active class to current container (border visibility controlled by CSS multi-pane rules) | |||
| 456 | - | webkit.WidgetAddCSSClass(container, activePaneClass) | |||
| 457 | - | if !webkit.WidgetIsValid(container) { | |||
| 458 | - | log.Printf("[workspace] focus aborted: container invalid widget=%#x", container) | |||
| 459 | - | return | |||
| 460 | - | } | |||
| 461 | - | webkit.IdleAdd(func() bool { | |||
| 462 | - | if !webkit.WidgetIsValid(container) { | |||
| 463 | - | log.Printf("[workspace] focus aborted during idle: container invalid widget=%#x", container) | |||
| 464 | - | return false | |||
| 465 | - | } | |||
| 466 | - | parent := webkit.WidgetGetParent(container) | |||
| 467 | - | if parent != 0 { | |||
| 468 | - | webkit.WidgetRealizeInContainer(container) | |||
| 469 | - | if viewWidget != 0 { | |||
| 470 | - | webkit.WidgetGrabFocus(viewWidget) | |||
| 471 | - | } | |||
| 472 | - | // Consolidated focus operation log | |||
| 473 | - | if wm.app.config != nil && wm.app.config.Debug.EnableWorkspaceDebug { | |||
| 474 | - | log.Printf("[workspace] focus operations completed: container=%#x parent=%#x viewWidget=%#x", container, parent, viewWidget) | |||
| 475 | - | } | |||
| 476 | - | } else { | |||
| 477 | - | log.Printf("[workspace] focus deferred: widget not parented") | |||
| 478 | - | } | |||
| 479 | - | return false // Remove idle callback | |||
| 480 | - | }) | |||
| 481 | - | } | |||
| 482 | - | ||||
| 483 | - | if node.pane != nil && node.pane.webView != nil { | |||
| 484 | - | // Update pane focus time | |||
| 485 | - | node.pane.UpdateLastFocus() | |||
| 486 | - | ||||
| 487 | - | // Always dispatch focus event to ensure JavaScript side knows the active pane | |||
| 488 | - | // This fixes the issue where navigation within the same pane doesn't update JS state | |||
| 489 | - | wm.DispatchPaneFocusEvent(node, true) | |||
| 490 | - | ||||
| 491 | - | // Lazy-load GUI components if first focus | |||
| 492 | - | if !node.pane.HasGUI() { | |||
| 493 | - | wm.ensureGUIInPane(node.pane) | |||
| 494 | - | } | |||
| 495 | - | } | |||
| 496 | } | 426 | } | ||
| 497 | 427 | ||||
| 498 | // Helper functions for stacked pane CSS colors | 428 | // Helper functions for stacked pane CSS colors | ||
| @@ -662,8 +592,8 @@ func (wm *WorkspaceManager) focusByView(view *webkit.WebView) { | |||||
| 662 | wm.focusThrottleMutex.Unlock() | 592 | wm.focusThrottleMutex.Unlock() | ||
| 663 | 593 | ||||
| 664 | if node, ok := wm.viewToNode[view]; ok { | 594 | if node, ok := wm.viewToNode[view]; ok { | ||
| 665 | - | if wm.active != node { | 595 | + | if wm.currentlyFocused != node { |
| 666 | - | wm.focusNode(node) | 596 | + | wm.focusRespectingStack(node, "focus-by-view") |
| 667 | } | 597 | } | ||
| 668 | } | 598 | } | ||
| 669 | } | 599 | } | ||
| @@ -680,7 +610,8 @@ func (wm *WorkspaceManager) ensureHover(node *paneNode) { | |||||
| 680 | if wm == nil { | 610 | if wm == nil { | ||
| 681 | return | 611 | return | ||
| 682 | } | 612 | } | ||
| 683 | - | wm.focusNode(node) | 613 | + | wm.currentlyFocused = node |
| 614 | + | wm.focusManager.SetActivePane(node) | |||
| 684 | }) | 615 | }) | ||
| 685 | node.hoverToken = token | 616 | node.hoverToken = token | ||
| 686 | if token == 0 { | 617 | if token == 0 { | ||
| @@ -706,7 +637,7 @@ func (wm *WorkspaceManager) FocusNeighbor(direction string) bool { | |||||
| 706 | switch strings.ToLower(direction) { | 637 | switch strings.ToLower(direction) { | ||
| 707 | case "up", "down": | 638 | case "up", "down": | ||
| 708 | // Check if current pane is part of a stack and handle stack navigation | 639 | // Check if current pane is part of a stack and handle stack navigation | ||
| 709 | - | if wm.navigateStack(strings.ToLower(direction)) { | 640 | + | if wm.stackedPaneManager.NavigateStack(strings.ToLower(direction)) { |
| 710 | return true | 641 | return true | ||
| 711 | } | 642 | } | ||
| 712 | // Fall back to regular adjacency navigation | 643 | // Fall back to regular adjacency navigation | ||
| @@ -720,13 +651,13 @@ func (wm *WorkspaceManager) FocusNeighbor(direction string) bool { | |||||
| 720 | 651 | ||||
| 721 | // navigateStack handles navigation within a stacked pane container. | 652 | // navigateStack handles navigation within a stacked pane container. | ||
| 722 | func (wm *WorkspaceManager) navigateStack(direction string) bool { | 653 | func (wm *WorkspaceManager) navigateStack(direction string) bool { | ||
| 723 | - | if wm.active == nil { | 654 | + | if wm.currentlyFocused == nil { |
| 724 | return false | 655 | return false | ||
| 725 | } | 656 | } | ||
| 726 | 657 | ||||
| 727 | // Find the stack container this pane belongs to | 658 | // Find the stack container this pane belongs to | ||
| 728 | var stackNode *paneNode | 659 | var stackNode *paneNode | ||
| 729 | - | current := wm.active | 660 | + | current := wm.currentlyFocused |
| 730 | 661 | ||||
| 731 | // Check if current pane is directly in a stack | 662 | // Check if current pane is directly in a stack | ||
| 732 | if current.parent != nil && current.parent.isStacked { | 663 | if current.parent != nil && current.parent.isStacked { | ||
| @@ -779,11 +710,12 @@ func (wm *WorkspaceManager) navigateStack(direction string) bool { | |||||
| 779 | 710 | ||||
| 780 | // Update active stack index and visibility | 711 | // Update active stack index and visibility | ||
| 781 | stackNode.activeStackIndex = newIndex | 712 | stackNode.activeStackIndex = newIndex | ||
| 782 | - | wm.updateStackVisibility(stackNode) | 713 | + | wm.stackedPaneManager.UpdateStackVisibility(stackNode) |
| 783 | 714 | ||||
| 784 | // Focus the new active pane | 715 | // Focus the new active pane | ||
| 785 | newActivePane := stackNode.stackedPanes[newIndex] | 716 | newActivePane := stackNode.stackedPanes[newIndex] | ||
| 786 | - | wm.focusNode(newActivePane) | 717 | + | wm.currentlyFocused = newActivePane |
| 718 | + | wm.focusManager.SetActivePane(newActivePane) | |||
| 787 | 719 | ||||
| 788 | log.Printf("[workspace] navigated stack: direction=%s from=%d to=%d stackSize=%d", | 720 | log.Printf("[workspace] navigated stack: direction=%s from=%d to=%d stackSize=%d", | ||
| 789 | direction, currentIndex, newIndex, len(stackNode.stackedPanes)) | 721 | direction, currentIndex, newIndex, len(stackNode.stackedPanes)) | ||
| @@ -793,16 +725,17 @@ func (wm *WorkspaceManager) navigateStack(direction string) bool { | |||||
| 793 | const focusEpsilon = 1e-3 | 725 | const focusEpsilon = 1e-3 | ||
| 794 | 726 | ||||
| 795 | func (wm *WorkspaceManager) focusAdjacent(direction string) bool { | 727 | func (wm *WorkspaceManager) focusAdjacent(direction string) bool { | ||
| 796 | - | if wm.active == nil || !wm.active.isLeaf || wm.active.container == 0 { | 728 | + | if wm.currentlyFocused == nil || !wm.currentlyFocused.isLeaf || wm.currentlyFocused.container == 0 { |
| 797 | return false | 729 | return false | ||
| 798 | } | 730 | } | ||
| 799 | 731 | ||||
| 800 | - | if neighbor := wm.structuralNeighbor(wm.active, direction); neighbor != nil { | 732 | + | if neighbor := wm.structuralNeighbor(wm.currentlyFocused, direction); neighbor != nil { |
| 801 | - | wm.focusNode(neighbor) | 733 | + | wm.currentlyFocused = neighbor |
| 734 | + | wm.focusManager.SetActivePane(neighbor) | |||
| 802 | return true | 735 | return true | ||
| 803 | } | 736 | } | ||
| 804 | 737 | ||||
| 805 | - | currentBounds, ok := webkit.WidgetGetBounds(wm.active.container) | 738 | + | currentBounds, ok := webkit.WidgetGetBounds(wm.currentlyFocused.container) |
| 806 | if !ok { | 739 | if !ok { | ||
| 807 | log.Printf("[workspace] unable to compute bounds for active pane") | 740 | log.Printf("[workspace] unable to compute bounds for active pane") | ||
| 808 | return false | 741 | return false | ||
| @@ -817,7 +750,7 @@ func (wm *WorkspaceManager) focusAdjacent(direction string) bool { | |||||
| 817 | var debugCandidates []string | 750 | var debugCandidates []string | ||
| 818 | 751 | ||||
| 819 | for _, candidate := range leaves { | 752 | for _, candidate := range leaves { | ||
| 820 | - | if candidate == nil || candidate == wm.active || candidate.container == 0 { | 753 | + | if candidate == nil || candidate == wm.currentlyFocused || candidate.container == 0 { |
| 821 | continue | 754 | continue | ||
| 822 | } | 755 | } | ||
| 823 | bounds, ok := webkit.WidgetGetBounds(candidate.container) | 756 | bounds, ok := webkit.WidgetGetBounds(candidate.container) | ||
| @@ -865,12 +798,13 @@ func (wm *WorkspaceManager) focusAdjacent(direction string) bool { | |||||
| 865 | } | 798 | } | ||
| 866 | 799 | ||||
| 867 | if best != nil { | 800 | if best != nil { | ||
| 868 | - | wm.focusNode(best) | 801 | + | wm.currentlyFocused = best |
| 802 | + | wm.focusManager.SetActivePane(best) | |||
| 869 | return true | 803 | return true | ||
| 870 | } | 804 | } | ||
| 871 | 805 | ||||
| 872 | if len(debugCandidates) > 0 { | 806 | if len(debugCandidates) > 0 { | ||
| 873 | - | log.Printf("[workspace] focusAdjacent no candidate direction=%s current=%#x candidates=%s", direction, wm.active.container, strings.Join(debugCandidates, "; ")) | 807 | + | log.Printf("[workspace] focusAdjacent no candidate direction=%s current=%#x candidates=%s", direction, wm.currentlyFocused.container, strings.Join(debugCandidates, "; ")) |
| 874 | } | 808 | } | ||
| 875 | return false | 809 | return false | ||
| 876 | } | 810 | } | ||
| @@ -1041,6 +975,16 @@ func (wm *WorkspaceManager) collectLeavesFrom(node *paneNode) []*paneNode { | |||||
| 1041 | leaves = append(leaves, n) | 975 | leaves = append(leaves, n) | ||
| 1042 | return | 976 | return | ||
| 1043 | } | 977 | } | ||
| 978 | + | ||||
| 979 | + | // Handle stacked panes - traverse the stacked panes list | |||
| 980 | + | if n.isStacked && len(n.stackedPanes) > 0 { | |||
| 981 | + | for _, stackedPane := range n.stackedPanes { | |||
| 982 | + | walk(stackedPane, depth+1) | |||
| 983 | + | } | |||
| 984 | + | return | |||
| 985 | + | } | |||
| 986 | + | ||||
| 987 | + | // Handle regular split nodes | |||
| 1044 | walk(n.left, depth+1) | 988 | walk(n.left, depth+1) | ||
| 1045 | walk(n.right, depth+1) | 989 | walk(n.right, depth+1) | ||
| 1046 | } | 990 | } | ||
| @@ -1067,6 +1011,32 @@ func (wm *WorkspaceManager) splitNode(target *paneNode, direction string) (*pane | |||||
| 1067 | return nil, errors.New("split target must be a leaf pane") | 1011 | return nil, errors.New("split target must be a leaf pane") | ||
| 1068 | } | 1012 | } | ||
| 1069 | 1013 | ||||
| 1014 | + | // IMPORTANT: If the target is inside a stacked pane, we need to split AROUND the stack, | |||
| 1015 | + | // not the individual pane or the stack itself. We create a new split at the stack's parent level. | |||
| 1016 | + | originalTarget := target | |||
| 1017 | + | var stackContainer *paneNode | |||
| 1018 | + | if target.parent != nil && target.parent.isStacked { | |||
| 1019 | + | // Target is in a stack - we'll create the split around the entire stack | |||
| 1020 | + | stackContainer = target.parent | |||
| 1021 | + | log.Printf("[workspace] target is in stack, will split around the stack: originalTarget=%p stackContainer=%p", originalTarget, stackContainer) | |||
| 1022 | + | } | |||
| 1023 | + | ||||
| 1024 | + | // Determine what we're actually splitting | |||
| 1025 | + | var splitTarget *paneNode | |||
| 1026 | + | var splitTargetContainer uintptr | |||
| 1027 | + | ||||
| 1028 | + | if stackContainer != nil { | |||
| 1029 | + | // Case: splitting from inside a stack - we split around the entire stack | |||
| 1030 | + | splitTarget = stackContainer | |||
| 1031 | + | splitTargetContainer = stackContainer.container | |||
| 1032 | + | log.Printf("[workspace] splitting around stack: stackContainer=%p container=%#x", splitTarget, splitTargetContainer) | |||
| 1033 | + | } else { | |||
| 1034 | + | // Case: normal split from a simple pane | |||
| 1035 | + | splitTarget = target | |||
| 1036 | + | splitTargetContainer = target.container | |||
| 1037 | + | log.Printf("[workspace] normal split from pane: target=%p container=%#x", splitTarget, splitTargetContainer) | |||
| 1038 | + | } | |||
| 1039 | + | ||||
| 1070 | newView, err := wm.createWebView() | 1040 | newView, err := wm.createWebView() | ||
| 1071 | if err != nil { | 1041 | if err != nil { | ||
| 1072 | return nil, err | 1042 | return nil, err | ||
| @@ -1089,16 +1059,25 @@ func (wm *WorkspaceManager) splitNode(target *paneNode, direction string) (*pane | |||||
| 1089 | webkit.WidgetSetVExpand(newContainer, true) | 1059 | webkit.WidgetSetVExpand(newContainer, true) | ||
| 1090 | webkit.WidgetRealizeInContainer(newContainer) | 1060 | webkit.WidgetRealizeInContainer(newContainer) | ||
| 1091 | 1061 | ||||
| 1092 | - | existingContainer := target.container | 1062 | + | // Use the determined split target container |
| 1093 | - | if existingContainer == 0 { | 1063 | + | if splitTargetContainer == 0 { |
| 1094 | - | return nil, errors.New("existing pane missing container") | 1064 | + | return nil, errors.New("split target missing container") |
| 1065 | + | } | |||
| 1066 | + | ||||
| 1067 | + | // Only apply GTK operations if this is a simple widget (leaf pane) | |||
| 1068 | + | // For stacked containers, we need to be more careful | |||
| 1069 | + | if splitTarget.isLeaf { | |||
| 1070 | + | webkit.WidgetSetHExpand(splitTargetContainer, true) | |||
| 1071 | + | webkit.WidgetSetVExpand(splitTargetContainer, true) | |||
| 1072 | + | webkit.WidgetRealizeInContainer(splitTargetContainer) | |||
| 1073 | + | } else if splitTarget.isStacked { | |||
| 1074 | + | // For stacked containers, the wrapper should already be properly configured | |||
| 1075 | + | log.Printf("[workspace] skipping GTK setup for stack wrapper: %#x", splitTargetContainer) | |||
| 1095 | } | 1076 | } | ||
| 1096 | - | webkit.WidgetSetHExpand(existingContainer, true) | |||
| 1097 | - | webkit.WidgetSetVExpand(existingContainer, true) | |||
| 1098 | - | webkit.WidgetRealizeInContainer(existingContainer) | |||
| 1099 | 1077 | ||||
| 1100 | orientation, existingFirst := mapDirection(direction) | 1078 | orientation, existingFirst := mapDirection(direction) | ||
| 1101 | - | log.Printf("[workspace] splitting direction=%s orientation=%v existingFirst=%v target.parent=%p", direction, orientation, existingFirst, target.parent) | 1079 | + | log.Printf("[workspace] splitting direction=%s orientation=%v existingFirst=%v splitTarget.parent=%p splitTarget.isStacked=%v", |
| 1080 | + | direction, orientation, existingFirst, splitTarget.parent, splitTarget.isStacked) | |||
| 1102 | 1081 | ||||
| 1103 | paned := webkit.NewPaned(orientation) | 1082 | paned := webkit.NewPaned(orientation) | ||
| 1104 | if paned == 0 { | 1083 | if paned == 0 { | ||
| @@ -1115,7 +1094,7 @@ func (wm *WorkspaceManager) splitNode(target *paneNode, direction string) (*pane | |||||
| 1115 | isLeaf: true, | 1094 | isLeaf: true, | ||
| 1116 | } | 1095 | } | ||
| 1117 | split := &paneNode{ | 1096 | split := &paneNode{ | ||
| 1118 | - | parent: target.parent, | 1097 | + | parent: splitTarget.parent, |
| 1119 | container: paned, | 1098 | container: paned, | ||
| 1120 | orientation: orientation, | 1099 | orientation: orientation, | ||
| 1121 | isLeaf: false, | 1100 | isLeaf: false, | ||
| @@ -1123,90 +1102,97 @@ func (wm *WorkspaceManager) splitNode(target *paneNode, direction string) (*pane | |||||
| 1123 | 1102 | ||||
| 1124 | parent := split.parent | 1103 | parent := split.parent | ||
| 1125 | 1104 | ||||
| 1126 | - | // Detach existing container from its current GTK parent before inserting into new paned. | 1105 | + | // Detach split target container from its current GTK parent before inserting into new paned. |
| 1127 | if parent == nil { | 1106 | if parent == nil { | ||
| 1128 | - | // Target is the root - remove it from the window and unparent it | 1107 | + | // Split target is the root - remove it from the window |
| 1129 | - | log.Printf("[workspace] removing existing container=%#x from window", existingContainer) | 1108 | + | log.Printf("[workspace] removing split target container=%#x from window", splitTargetContainer) |
| 1130 | if wm.window != nil { | 1109 | if wm.window != nil { | ||
| 1131 | wm.window.SetChild(0) | 1110 | wm.window.SetChild(0) | ||
| 1132 | } | 1111 | } | ||
| 1112 | + | // For root widgets, only unparent if they actually have a GTK parent | |||
| 1113 | + | if webkit.WidgetGetParent(splitTargetContainer) != 0 { | |||
| 1114 | + | webkit.WidgetUnparent(splitTargetContainer) | |||
| 1115 | + | } | |||
| 1133 | } else if parent.container != 0 { | 1116 | } else if parent.container != 0 { | ||
| 1134 | - | // Target has a parent paned - unparent it from there | 1117 | + | // Split target has a parent paned - remove it (automatically unparents in GTK4) |
| 1135 | - | log.Printf("[workspace] unparenting existing container=%#x from parent paned=%#x", existingContainer, parent.container) | 1118 | + | log.Printf("[workspace] unparenting split target container=%#x from parent paned=%#x", splitTargetContainer, parent.container) |
| 1136 | - | if parent.left == target { | 1119 | + | if parent.left == splitTarget { |
| 1137 | webkit.PanedSetStartChild(parent.container, 0) | 1120 | webkit.PanedSetStartChild(parent.container, 0) | ||
| 1138 | - | } else if parent.right == target { | 1121 | + | } else if parent.right == splitTarget { |
| 1139 | webkit.PanedSetEndChild(parent.container, 0) | 1122 | webkit.PanedSetEndChild(parent.container, 0) | ||
| 1140 | } | 1123 | } | ||
| 1141 | - | } | 1124 | + | // GTK4 automatically unparents when we set paned child to 0 - no manual unparent needed |
| 1142 | - | ||||
| 1143 | - | // Properly unparent the widget from GTK's perspective | |||
| 1144 | - | webkit.WidgetUnparent(existingContainer) | |||
| 1145 | - | if parent != nil && parent.container != 0 { | |||
| 1146 | webkit.WidgetQueueAllocate(parent.container) | 1125 | webkit.WidgetQueueAllocate(parent.container) | ||
| 1147 | } | 1126 | } | ||
| 1148 | 1127 | ||||
| 1149 | // Set up the tree structure first | 1128 | // Set up the tree structure first | ||
| 1150 | if existingFirst { | 1129 | if existingFirst { | ||
| 1151 | - | split.left = target | 1130 | + | split.left = splitTarget |
| 1152 | split.right = newLeaf | 1131 | split.right = newLeaf | ||
| 1153 | } else { | 1132 | } else { | ||
| 1154 | split.left = newLeaf | 1133 | split.left = newLeaf | ||
| 1155 | - | split.right = target | 1134 | + | split.right = splitTarget |
| 1156 | } | 1135 | } | ||
| 1157 | 1136 | ||||
| 1158 | - | target.parent = split | 1137 | + | splitTarget.parent = split |
| 1159 | newLeaf.parent = split | 1138 | newLeaf.parent = split | ||
| 1160 | 1139 | ||||
| 1161 | // Update tree root/parent references | 1140 | // Update tree root/parent references | ||
| 1162 | if parent == nil { | 1141 | if parent == nil { | ||
| 1163 | wm.root = split | 1142 | wm.root = split | ||
| 1164 | } else { | 1143 | } else { | ||
| 1165 | - | if parent.left == target { | 1144 | + | if parent.left == splitTarget { |
| 1166 | parent.left = split | 1145 | parent.left = split | ||
| 1167 | - | } else if parent.right == target { | 1146 | + | } else if parent.right == splitTarget { |
| 1168 | parent.right = split | 1147 | parent.right = split | ||
| 1169 | } | 1148 | } | ||
| 1170 | } | 1149 | } | ||
| 1171 | 1150 | ||||
| 1172 | - | // Use idle callback to ensure widget unparenting is complete before re-parenting | 1151 | + | // Ensure widget operations are complete before re-parenting (GTK test pattern) |
| 1173 | - | webkit.IdleAdd(func() bool { | 1152 | + | webkit.WidgetWaitForDraw(splitTargetContainer) |
| 1174 | - | // Add both containers to the new paned | 1153 | + | webkit.WidgetWaitForDraw(newContainer) |
| 1175 | - | if existingFirst { | |||
| 1176 | - | webkit.PanedSetStartChild(paned, existingContainer) | |||
| 1177 | - | webkit.PanedSetEndChild(paned, newContainer) | |||
| 1178 | - | log.Printf("[workspace] added existing=%#x as start child, new=%#x as end child", existingContainer, newContainer) | |||
| 1179 | - | } else { | |||
| 1180 | - | webkit.PanedSetStartChild(paned, newContainer) | |||
| 1181 | - | webkit.PanedSetEndChild(paned, existingContainer) | |||
| 1182 | - | log.Printf("[workspace] added new=%#x as start child, existing=%#x as end child", newContainer, existingContainer) | |||
| 1183 | - | } | |||
| 1184 | 1154 | ||||
| 1185 | - | // Attach the new paned to its parent | 1155 | + | // Add both containers to the new paned |
| 1186 | - | if parent == nil { | 1156 | + | if existingFirst { |
| 1187 | - | if wm.window != nil { | 1157 | + | webkit.PanedSetStartChild(paned, splitTargetContainer) |
| 1188 | - | wm.window.SetChild(paned) | 1158 | + | webkit.PanedSetEndChild(paned, newContainer) |
| 1189 | - | webkit.WidgetQueueAllocate(paned) | 1159 | + | log.Printf("[workspace] added splitTarget=%#x as start child, new=%#x as end child", splitTargetContainer, newContainer) |
| 1190 | - | log.Printf("[workspace] paned set as window child: paned=%#x", paned) | 1160 | + | } else { |
| 1191 | - | } | 1161 | + | webkit.PanedSetStartChild(paned, newContainer) |
| 1192 | - | } else { | 1162 | + | webkit.PanedSetEndChild(paned, splitTargetContainer) |
| 1193 | - | if parent.left == split { | 1163 | + | log.Printf("[workspace] added new=%#x as start child, splitTarget=%#x as end child", newContainer, splitTargetContainer) |
| 1194 | - | webkit.PanedSetStartChild(parent.container, paned) | 1164 | + | } |
| 1195 | - | } else if parent.right == split { | 1165 | + | |
| 1196 | - | webkit.PanedSetEndChild(parent.container, paned) | 1166 | + | // Attach the new paned to its parent |
| 1197 | - | } | 1167 | + | if parent == nil { |
| 1198 | - | webkit.WidgetQueueAllocate(parent.container) | 1168 | + | if wm.window != nil { |
| 1169 | + | wm.window.SetChild(paned) | |||
| 1199 | webkit.WidgetQueueAllocate(paned) | 1170 | webkit.WidgetQueueAllocate(paned) | ||
| 1200 | - | log.Printf("[workspace] paned inserted into parent=%#x", parent.container) | 1171 | + | log.Printf("[workspace] paned set as window child: paned=%#x", paned) |
| 1201 | } | 1172 | } | ||
| 1173 | + | } else { | |||
| 1174 | + | if parent.left == split { | |||
| 1175 | + | webkit.PanedSetStartChild(parent.container, paned) | |||
| 1176 | + | } else if parent.right == split { | |||
| 1177 | + | webkit.PanedSetEndChild(parent.container, paned) | |||
| 1178 | + | } | |||
| 1179 | + | webkit.WidgetQueueAllocate(parent.container) | |||
| 1180 | + | webkit.WidgetQueueAllocate(paned) | |||
| 1181 | + | log.Printf("[workspace] paned inserted into parent=%#x", parent.container) | |||
| 1182 | + | } | |||
| 1202 | 1183 | ||||
| 1203 | - | webkit.WidgetShow(paned) | 1184 | + | webkit.WidgetShow(paned) |
| 1204 | - | return false // Run once | |||
| 1205 | - | }) | |||
| 1206 | 1185 | ||||
| 1207 | wm.viewToNode[newPane.webView] = newLeaf | 1186 | wm.viewToNode[newPane.webView] = newLeaf | ||
| 1208 | wm.ensureHover(newLeaf) | 1187 | wm.ensureHover(newLeaf) | ||
| 1209 | - | wm.ensureHover(target) | 1188 | + | |
| 1189 | + | // For stacked panes, we need to ensure hover on the original target, not the stack container | |||
| 1190 | + | if originalTarget != target { | |||
| 1191 | + | wm.ensureHover(originalTarget) | |||
| 1192 | + | } else { | |||
| 1193 | + | wm.ensureHover(target) | |||
| 1194 | + | } | |||
| 1195 | + | ||||
| 1210 | wm.app.panes = append(wm.app.panes, newPane) | 1196 | wm.app.panes = append(wm.app.panes, newPane) | ||
| 1211 | if newPane.zoomController != nil { | 1197 | if newPane.zoomController != nil { | ||
| 1212 | newPane.zoomController.ApplyInitialZoom() | 1198 | newPane.zoomController.ApplyInitialZoom() | ||
| @@ -1219,7 +1205,8 @@ func (wm *WorkspaceManager) splitNode(target *paneNode, direction string) (*pane | |||||
| 1219 | if newContainer != 0 { | 1205 | if newContainer != 0 { | ||
| 1220 | webkit.WidgetShow(newContainer) | 1206 | webkit.WidgetShow(newContainer) | ||
| 1221 | } | 1207 | } | ||
| 1222 | - | wm.focusNode(newLeaf) | 1208 | + | wm.currentlyFocused = newLeaf |
| 1209 | + | wm.focusManager.SetActivePane(newLeaf) | |||
| 1223 | return false | 1210 | return false | ||
| 1224 | }) | 1211 | }) | ||
| 1225 | 1212 | ||||
| @@ -1255,30 +1242,81 @@ func (wm *WorkspaceManager) stackPane(target *paneNode) (*paneNode, error) { | |||||
| 1255 | webkit.WidgetSetVExpand(newContainer, true) | 1242 | webkit.WidgetSetVExpand(newContainer, true) | ||
| 1256 | webkit.WidgetRealizeInContainer(newContainer) | 1243 | webkit.WidgetRealizeInContainer(newContainer) | ||
| 1257 | 1244 | ||||
| 1258 | - | // If target is not already stacked, convert it to a stacked node | 1245 | + | var stackNode *paneNode |
| 1259 | - | if !target.isStacked { | 1246 | + | var insertIndex int |
| 1247 | + | ||||
| 1248 | + | // Check if target is already in a stack | |||
| 1249 | + | if target.parent != nil && target.parent.isStacked { | |||
| 1250 | + | // Target is already in a stack - find the stack container and insertion point | |||
| 1251 | + | stackNode = target.parent | |||
| 1252 | + | ||||
| 1253 | + | // Find the current position of the target pane in the stack | |||
| 1254 | + | currentIndex := -1 | |||
| 1255 | + | for i, pane := range stackNode.stackedPanes { | |||
| 1256 | + | if pane == target { | |||
| 1257 | + | currentIndex = i | |||
| 1258 | + | break | |||
| 1259 | + | } | |||
| 1260 | + | } | |||
| 1261 | + | ||||
| 1262 | + | if currentIndex == -1 { | |||
| 1263 | + | return nil, errors.New("target pane not found in stack") | |||
| 1264 | + | } | |||
| 1265 | + | ||||
| 1266 | + | // Insert the new pane right after the current pane | |||
| 1267 | + | insertIndex = currentIndex + 1 | |||
| 1268 | + | log.Printf("[workspace] adding to existing stack: currentIndex=%d insertIndex=%d stackSize=%d", | |||
| 1269 | + | currentIndex, insertIndex, len(stackNode.stackedPanes)) | |||
| 1270 | + | } else if target.isStacked { | |||
| 1271 | + | // This shouldn't happen with current structure, but handle it | |||
| 1272 | + | return nil, errors.New("target pane has inconsistent stacking state") | |||
| 1273 | + | } else { | |||
| 1274 | + | // Target is not stacked - create initial stack | |||
| 1260 | log.Printf("[workspace] converting pane to stacked: %p", target) | 1275 | log.Printf("[workspace] converting pane to stacked: %p", target) | ||
| 1261 | 1276 | ||||
| 1262 | - | // Create a vertical box container for the stack | 1277 | + | // Create the wrapper container - this is what will be used by splitNode |
| 1263 | - | stackContainer := webkit.NewBox(webkit.OrientationVertical, 0) | 1278 | + | stackWrapperContainer := webkit.NewBox(webkit.OrientationVertical, 0) |
| 1264 | - | if stackContainer == 0 { | 1279 | + | if stackWrapperContainer == 0 { |
| 1265 | - | return nil, errors.New("failed to create stack container") | 1280 | + | return nil, errors.New("failed to create stack wrapper container") |
| 1281 | + | } | |||
| 1282 | + | webkit.WidgetSetHExpand(stackWrapperContainer, true) | |||
| 1283 | + | webkit.WidgetSetVExpand(stackWrapperContainer, true) | |||
| 1284 | + | ||||
| 1285 | + | // Create the internal box for the actual stacked widgets (titles + webviews) | |||
| 1286 | + | stackInternalBox := webkit.NewBox(webkit.OrientationVertical, 0) | |||
| 1287 | + | if stackInternalBox == 0 { | |||
| 1288 | + | return nil, errors.New("failed to create stack internal box") | |||
| 1266 | } | 1289 | } | ||
| 1267 | - | webkit.WidgetSetHExpand(stackContainer, true) | 1290 | + | webkit.WidgetSetHExpand(stackInternalBox, true) |
| 1268 | - | webkit.WidgetSetVExpand(stackContainer, true) | 1291 | + | webkit.WidgetSetVExpand(stackInternalBox, true) |
| 1292 | + | ||||
| 1293 | + | // The internal box goes inside the wrapper | |||
| 1294 | + | webkit.BoxAppend(stackWrapperContainer, stackInternalBox) | |||
| 1269 | 1295 | ||||
| 1270 | // Get the existing container and parent info | 1296 | // Get the existing container and parent info | ||
| 1271 | existingContainer := target.container | 1297 | existingContainer := target.container | ||
| 1272 | parent := target.parent | 1298 | parent := target.parent | ||
| 1273 | 1299 | ||||
| 1274 | - | // Detach existing container from its parent | 1300 | + | // Create title bar for the existing pane |
| 1301 | + | titleBar := wm.createTitleBar(target) | |||
| 1302 | + | ||||
| 1303 | + | // Keep existing container visible during transition to prevent rendering glitch | |||
| 1304 | + | // webkit.WidgetSetVisible(existingContainer, false) // REMOVED - causes rendering block | |||
| 1305 | + | webkit.WidgetSetVisible(titleBar, false) | |||
| 1306 | + | ||||
| 1307 | + | // Detach existing container from its current parent first | |||
| 1308 | + | // This is necessary before we can add it to the stack | |||
| 1275 | if parent == nil { | 1309 | if parent == nil { | ||
| 1276 | // Target is the root - remove it from window | 1310 | // Target is the root - remove it from window | ||
| 1277 | if wm.window != nil { | 1311 | if wm.window != nil { | ||
| 1278 | wm.window.SetChild(0) | 1312 | wm.window.SetChild(0) | ||
| 1279 | } | 1313 | } | ||
| 1314 | + | // Unparent if it has a GTK parent | |||
| 1315 | + | if webkit.WidgetGetParent(existingContainer) != 0 { | |||
| 1316 | + | webkit.WidgetUnparent(existingContainer) | |||
| 1317 | + | } | |||
| 1280 | } else if parent.container != 0 { | 1318 | } else if parent.container != 0 { | ||
| 1281 | - | // Target has a parent - unparent it | 1319 | + | // Target has a parent paned - remove it (automatically unparents in GTK4) |
| 1282 | if parent.left == target { | 1320 | if parent.left == target { | ||
| 1283 | webkit.PanedSetStartChild(parent.container, 0) | 1321 | webkit.PanedSetStartChild(parent.container, 0) | ||
| 1284 | } else if parent.right == target { | 1322 | } else if parent.right == target { | ||
| @@ -1286,59 +1324,62 @@ func (wm *WorkspaceManager) stackPane(target *paneNode) (*paneNode, error) { | |||||
| 1286 | } | 1324 | } | ||
| 1287 | } | 1325 | } | ||
| 1288 | 1326 | ||||
| 1289 | - | webkit.WidgetUnparent(existingContainer) | 1327 | + | // Build the complete stack structure with hidden widgets |
| 1328 | + | webkit.BoxAppend(stackInternalBox, titleBar) | |||
| 1329 | + | webkit.BoxAppend(stackInternalBox, existingContainer) | |||
| 1290 | 1330 | ||||
| 1291 | - | // Create title bar for the existing pane | 1331 | + | // Immediately reattach the stack wrapper to minimize visibility gap |
| 1292 | - | titleBar := wm.createTitleBar(target) | 1332 | + | if parent == nil { |
| 1293 | - | 1333 | + | if wm.window != nil { | |
| 1294 | - | // Add ONLY the title bar for the first pane to the stack container initially | 1334 | + | wm.window.SetChild(stackWrapperContainer) |
| 1295 | - | // The webview content will be added when this pane becomes active | 1335 | + | } |
| 1296 | - | webkit.BoxAppend(stackContainer, titleBar) | 1336 | + | } else if parent.container != 0 { |
| 1297 | - | webkit.BoxAppend(stackContainer, existingContainer) | 1337 | + | if parent.left == target { |
| 1338 | + | webkit.PanedSetStartChild(parent.container, stackWrapperContainer) | |||
| 1339 | + | } else if parent.right == target { | |||
| 1340 | + | webkit.PanedSetEndChild(parent.container, stackWrapperContainer) | |||
| 1341 | + | } | |||
| 1342 | + | } | |||
| 1298 | 1343 | ||||
| 1299 | - | // Convert target to a stacked node but keep it as a leaf for content management | 1344 | + | // Convert target to a stacked leaf node |
| 1300 | target.isStacked = true | 1345 | target.isStacked = true | ||
| 1301 | - | target.isLeaf = true // Keep as leaf so updateStackVisibility works correctly | 1346 | + | target.isLeaf = true |
| 1302 | - | target.container = existingContainer // Keep original container for visibility control | 1347 | + | target.container = existingContainer |
| 1303 | target.titleBar = titleBar | 1348 | target.titleBar = titleBar | ||
| 1304 | 1349 | ||||
| 1305 | - | // Create a parent stacked container node | 1350 | + | // Create the stack container node - container points to wrapper, stackWrapper points to internal box |
| 1306 | - | stackNode := &paneNode{ | 1351 | + | stackNode = &paneNode{ |
| 1307 | isStacked: true, | 1352 | isStacked: true, | ||
| 1308 | isLeaf: false, | 1353 | isLeaf: false, | ||
| 1309 | - | container: stackContainer, | 1354 | + | container: stackWrapperContainer, // Wrapper for GTK operations (splits, etc.) |
| 1355 | + | stackWrapper: stackInternalBox, // Internal box for stack operations | |||
| 1310 | stackedPanes: []*paneNode{target}, | 1356 | stackedPanes: []*paneNode{target}, | ||
| 1311 | - | activeStackIndex: 1, // NEW PANE WILL BE ACTIVE (index 1) | 1357 | + | activeStackIndex: 0, // KEEP CURRENT PANE ACTIVE during transition (index 0) |
| 1312 | parent: parent, | 1358 | parent: parent, | ||
| 1313 | } | 1359 | } | ||
| 1314 | 1360 | ||||
| 1315 | // Update target's parent to be the stack node | 1361 | // Update target's parent to be the stack node | ||
| 1316 | target.parent = stackNode | 1362 | target.parent = stackNode | ||
| 1317 | 1363 | ||||
| 1318 | - | // Update parent references | 1364 | + | // Update parent references (GTK operations already done above) |
| 1319 | if parent == nil { | 1365 | if parent == nil { | ||
| 1320 | wm.root = stackNode | 1366 | wm.root = stackNode | ||
| 1321 | - | if wm.window != nil { | |||
| 1322 | - | wm.window.SetChild(stackContainer) | |||
| 1323 | - | } | |||
| 1324 | } else { | 1367 | } else { | ||
| 1325 | if parent.left == target { | 1368 | if parent.left == target { | ||
| 1326 | parent.left = stackNode | 1369 | parent.left = stackNode | ||
| 1327 | - | webkit.PanedSetStartChild(parent.container, stackContainer) | |||
| 1328 | } else if parent.right == target { | 1370 | } else if parent.right == target { | ||
| 1329 | parent.right = stackNode | 1371 | parent.right = stackNode | ||
| 1330 | - | webkit.PanedSetEndChild(parent.container, stackContainer) | |||
| 1331 | } | 1372 | } | ||
| 1332 | } | 1373 | } | ||
| 1333 | 1374 | ||||
| 1334 | - | // Update target to point to the stack node for further operations | 1375 | + | // New pane will be inserted at index 1 (after the original pane) |
| 1335 | - | target = stackNode | 1376 | + | insertIndex = 1 |
| 1336 | } | 1377 | } | ||
| 1337 | 1378 | ||||
| 1338 | // Create new leaf node for the new pane | 1379 | // Create new leaf node for the new pane | ||
| 1339 | newLeaf := &paneNode{ | 1380 | newLeaf := &paneNode{ | ||
| 1340 | pane: newPane, | 1381 | pane: newPane, | ||
| 1341 | - | parent: target, | 1382 | + | parent: stackNode, |
| 1342 | container: newContainer, | 1383 | container: newContainer, | ||
| 1343 | isLeaf: true, | 1384 | isLeaf: true, | ||
| 1344 | } | 1385 | } | ||
| @@ -1347,18 +1388,26 @@ func (wm *WorkspaceManager) stackPane(target *paneNode) (*paneNode, error) { | |||||
| 1347 | newTitleBar := wm.createTitleBar(newLeaf) | 1388 | newTitleBar := wm.createTitleBar(newLeaf) | ||
| 1348 | newLeaf.titleBar = newTitleBar | 1389 | newLeaf.titleBar = newTitleBar | ||
| 1349 | 1390 | ||||
| 1350 | - | // Add new pane to the stack | 1391 | + | // Keep new container hidden initially - will be shown after transition |
| 1351 | - | target.stackedPanes = append(target.stackedPanes, newLeaf) | 1392 | + | webkit.WidgetSetVisible(newContainer, false) |
| 1352 | - | webkit.BoxAppend(target.container, newTitleBar) | 1393 | + | webkit.WidgetSetVisible(newTitleBar, false) |
| 1394 | + | ||||
| 1395 | + | // Insert the new pane at the correct position in the slice | |||
| 1396 | + | stackNode.stackedPanes = append(stackNode.stackedPanes, nil) // Expand slice | |||
| 1397 | + | copy(stackNode.stackedPanes[insertIndex+1:], stackNode.stackedPanes[insertIndex:]) // Shift elements | |||
| 1398 | + | stackNode.stackedPanes[insertIndex] = newLeaf // Insert new pane | |||
| 1353 | 1399 | ||||
| 1354 | - | // Unparent the new container before adding it to the stack | 1400 | + | // Unparent the new container before adding it to the stack (only if it has a parent) |
| 1355 | - | webkit.WidgetUnparent(newContainer) | 1401 | + | if webkit.WidgetGetParent(newContainer) != 0 { |
| 1356 | - | webkit.BoxAppend(target.container, newContainer) | 1402 | + | webkit.WidgetUnparent(newContainer) |
| 1403 | + | } | |||
| 1357 | 1404 | ||||
| 1358 | - | // Ensure the activeStackIndex is correct (should be 1 for the new pane) | 1405 | + | // Add the new widgets to the internal stack box (not the wrapper) |
| 1359 | - | target.activeStackIndex = len(target.stackedPanes) - 1 | 1406 | + | // GTK will handle the visual order correctly as long as we manage visibility properly |
| 1407 | + | webkit.BoxAppend(stackNode.stackWrapper, newTitleBar) | |||
| 1408 | + | webkit.BoxAppend(stackNode.stackWrapper, newContainer) | |||
| 1360 | 1409 | ||||
| 1361 | - | // Update workspace state FIRST | 1410 | + | // Update workspace state first |
| 1362 | wm.viewToNode[newPane.webView] = newLeaf | 1411 | wm.viewToNode[newPane.webView] = newLeaf | ||
| 1363 | wm.ensureHover(newLeaf) | 1412 | wm.ensureHover(newLeaf) | ||
| 1364 | wm.app.panes = append(wm.app.panes, newPane) | 1413 | wm.app.panes = append(wm.app.panes, newPane) | ||
| @@ -1369,19 +1418,112 @@ func (wm *WorkspaceManager) stackPane(target *paneNode) (*paneNode, error) { | |||||
| 1369 | // Update CSS classes | 1418 | // Update CSS classes | ||
| 1370 | wm.ensurePaneBaseClasses() | 1419 | wm.ensurePaneBaseClasses() | ||
| 1371 | 1420 | ||||
| 1372 | - | // Show the new container immediately | 1421 | + | // Mark stack operation timestamp to prevent focus conflicts |
| 1373 | - | webkit.WidgetShow(newContainer) | 1422 | + | wm.lastStackOperation = time.Now() |
| 1374 | 1423 | ||||
| 1375 | - | // Update stack visibility to show the new pane as active | 1424 | + | // Update stack visibility to show current state (existing pane still active) |
| 1376 | - | wm.updateStackVisibility(target) | 1425 | + | wm.stackedPaneManager.UpdateStackVisibility(stackNode) |
| 1377 | 1426 | ||||
| 1378 | - | // Focus the new pane IMMEDIATELY (not in IdleAdd) | 1427 | + | // Transition to the new pane after a brief delay to avoid rendering conflicts |
| 1379 | - | wm.focusNode(newLeaf) | 1428 | + | webkit.IdleAdd(func() bool { |
| 1429 | + | // Now switch to the new pane | |||
| 1430 | + | stackNode.activeStackIndex = insertIndex | |||
| 1431 | + | wm.stackedPaneManager.UpdateStackVisibility(stackNode) | |||
| 1432 | + | wm.currentlyFocused = newLeaf | |||
| 1433 | + | wm.focusManager.SetActivePane(newLeaf) | |||
| 1434 | + | return false // Remove idle callback | |||
| 1435 | + | }) | |||
| 1380 | 1436 | ||||
| 1381 | - | log.Printf("[workspace] stacked new pane: target=%p newLeaf=%p stackSize=%d activeIndex=%d", target, newLeaf, len(target.stackedPanes), target.activeStackIndex) | 1437 | + | log.Printf("[workspace] stacked new pane: stackNode=%p newLeaf=%p stackSize=%d activeIndex=%d insertIndex=%d", |
| 1438 | + | stackNode, newLeaf, len(stackNode.stackedPanes), stackNode.activeStackIndex, insertIndex) | |||
| 1382 | return newLeaf, nil | 1439 | return newLeaf, nil | ||
| 1383 | } | 1440 | } | ||
| 1384 | 1441 | ||||
| 1442 | + | // UpdateTitleBar updates the title bar label for a WebView in stacked panes | |||
| 1443 | + | func (wm *WorkspaceManager) UpdateTitleBar(webView *webkit.WebView, title string) { | |||
| 1444 | + | if wm == nil || webView == nil || title == "" { | |||
| 1445 | + | return | |||
| 1446 | + | } | |||
| 1447 | + | ||||
| 1448 | + | // Find the pane node for this WebView | |||
| 1449 | + | node, exists := wm.viewToNode[webView] | |||
| 1450 | + | if !exists || node == nil || !node.isLeaf { | |||
| 1451 | + | return | |||
| 1452 | + | } | |||
| 1453 | + | ||||
| 1454 | + | // Check if this pane is in a stack and has a title bar | |||
| 1455 | + | if node.parent != nil && node.parent.isStacked && node.titleBar != 0 { | |||
| 1456 | + | // Update the title bar label with the new title | |||
| 1457 | + | wm.updateTitleBarLabel(node, title) | |||
| 1458 | + | log.Printf("[workspace] updated title bar for WebView %s: %s", webView.ID(), title) | |||
| 1459 | + | } | |||
| 1460 | + | } | |||
| 1461 | + | ||||
| 1462 | + | // updateTitleBarLabel updates the label widget within a title bar by recreating it | |||
| 1463 | + | func (wm *WorkspaceManager) updateTitleBarLabel(node *paneNode, title string) { | |||
| 1464 | + | if node == nil || node.titleBar == 0 || node.parent == nil || !node.parent.isStacked { | |||
| 1465 | + | return | |||
| 1466 | + | } | |||
| 1467 | + | ||||
| 1468 | + | // Format the title for display | |||
| 1469 | + | displayTitle := title | |||
| 1470 | + | if len(displayTitle) > 50 { | |||
| 1471 | + | displayTitle = displayTitle[:47] + "..." | |||
| 1472 | + | } | |||
| 1473 | + | ||||
| 1474 | + | // Recreate the title bar with the new title | |||
| 1475 | + | stackNode := node.parent | |||
| 1476 | + | oldTitleBar := node.titleBar | |||
| 1477 | + | ||||
| 1478 | + | // Create new title bar with updated title | |||
| 1479 | + | newTitleBar := wm.createTitleBarWithTitle(node, displayTitle) | |||
| 1480 | + | if newTitleBar == 0 { | |||
| 1481 | + | log.Printf("[workspace] failed to create new title bar") | |||
| 1482 | + | return | |||
| 1483 | + | } | |||
| 1484 | + | ||||
| 1485 | + | // Replace the old title bar in the internal stack box (not the wrapper) | |||
| 1486 | + | // Insert the new one after the old one, then remove the old one | |||
| 1487 | + | webkit.BoxInsertChildAfter(stackNode.stackWrapper, newTitleBar, oldTitleBar) | |||
| 1488 | + | webkit.BoxRemove(stackNode.stackWrapper, oldTitleBar) | |||
| 1489 | + | ||||
| 1490 | + | // Update the node reference | |||
| 1491 | + | node.titleBar = newTitleBar | |||
| 1492 | + | ||||
| 1493 | + | log.Printf("[workspace] updated title bar label: %s", displayTitle) | |||
| 1494 | + | } | |||
| 1495 | + | ||||
| 1496 | + | // createTitleBarWithTitle creates a title bar with a specific title | |||
| 1497 | + | func (wm *WorkspaceManager) createTitleBarWithTitle(pane *paneNode, title string) uintptr { | |||
| 1498 | + | titleBox := webkit.NewBox(webkit.OrientationHorizontal, 8) | |||
| 1499 | + | if titleBox == 0 { | |||
| 1500 | + | log.Printf("[workspace] failed to create title bar box") | |||
| 1501 | + | return 0 | |||
| 1502 | + | } | |||
| 1503 | + | webkit.WidgetSetHExpand(titleBox, true) | |||
| 1504 | + | webkit.WidgetSetVExpand(titleBox, false) | |||
| 1505 | + | ||||
| 1506 | + | // Use the provided title | |||
| 1507 | + | displayTitle := title | |||
| 1508 | + | if displayTitle == "" { | |||
| 1509 | + | displayTitle = "New Tab" | |||
| 1510 | + | } | |||
| 1511 | + | ||||
| 1512 | + | label := webkit.NewLabel(displayTitle) | |||
| 1513 | + | if label != 0 { | |||
| 1514 | + | webkit.LabelSetEllipsize(label, webkit.EllipsizeEnd) | |||
| 1515 | + | webkit.WidgetSetHExpand(label, true) | |||
| 1516 | + | webkit.BoxAppend(titleBox, label) | |||
| 1517 | + | } | |||
| 1518 | + | ||||
| 1519 | + | // Add CSS classes | |||
| 1520 | + | webkit.WidgetAddCSSClass(titleBox, "stacked-pane-title") | |||
| 1521 | + | webkit.WidgetAddCSSClass(label, "stacked-pane-title-text") | |||
| 1522 | + | ||||
| 1523 | + | log.Printf("[workspace] created title bar with custom title: box=%#x label=%#x title=%s", titleBox, label, displayTitle) | |||
| 1524 | + | return titleBox | |||
| 1525 | + | } | |||
| 1526 | + | ||||
| 1385 | // createTitleBar creates a title bar widget for a pane in a stack. | 1527 | // createTitleBar creates a title bar widget for a pane in a stack. | ||
| 1386 | func (wm *WorkspaceManager) createTitleBar(pane *paneNode) uintptr { | 1528 | func (wm *WorkspaceManager) createTitleBar(pane *paneNode) uintptr { | ||
| 1387 | titleBox := webkit.NewBox(webkit.OrientationHorizontal, 8) | 1529 | titleBox := webkit.NewBox(webkit.OrientationHorizontal, 8) | ||
| @@ -1417,35 +1559,7 @@ func (wm *WorkspaceManager) createTitleBar(pane *paneNode) uintptr { | |||||
| 1417 | } | 1559 | } | ||
| 1418 | 1560 | ||||
| 1419 | // updateStackVisibility updates the visibility of panes in a stack. | 1561 | // updateStackVisibility updates the visibility of panes in a stack. | ||
| 1420 | - | func (wm *WorkspaceManager) updateStackVisibility(stackNode *paneNode) { | 1562 | + | // DEPRECATED: Moved to StackedPaneManager |
| 1421 | - | if !stackNode.isStacked || len(stackNode.stackedPanes) == 0 { | |||
| 1422 | - | return | |||
| 1423 | - | } | |||
| 1424 | - | ||||
| 1425 | - | activeIndex := stackNode.activeStackIndex | |||
| 1426 | - | if activeIndex < 0 || activeIndex >= len(stackNode.stackedPanes) { | |||
| 1427 | - | activeIndex = 0 | |||
| 1428 | - | stackNode.activeStackIndex = activeIndex | |||
| 1429 | - | } | |||
| 1430 | - | ||||
| 1431 | - | log.Printf("[workspace] updating stack visibility: activeIndex=%d stackSize=%d", activeIndex, len(stackNode.stackedPanes)) | |||
| 1432 | - | ||||
| 1433 | - | for i, pane := range stackNode.stackedPanes { | |||
| 1434 | - | if i == activeIndex { | |||
| 1435 | - | // Show active pane's content, hide its title bar | |||
| 1436 | - | webkit.WidgetSetVisible(pane.container, true) | |||
| 1437 | - | webkit.WidgetSetVisible(pane.titleBar, false) | |||
| 1438 | - | webkit.WidgetAddCSSClass(pane.container, "stacked-pane-active") | |||
| 1439 | - | webkit.WidgetRemoveCSSClass(pane.container, "stacked-pane-collapsed") | |||
| 1440 | - | } else { | |||
| 1441 | - | // Hide inactive pane's content, show its title bar | |||
| 1442 | - | webkit.WidgetSetVisible(pane.container, false) | |||
| 1443 | - | webkit.WidgetSetVisible(pane.titleBar, true) | |||
| 1444 | - | webkit.WidgetAddCSSClass(pane.container, "stacked-pane-collapsed") | |||
| 1445 | - | webkit.WidgetRemoveCSSClass(pane.container, "stacked-pane-active") | |||
| 1446 | - | } | |||
| 1447 | - | } | |||
| 1448 | - | } | |||
| 1449 | 1563 | ||||
| 1450 | func (wm *WorkspaceManager) clonePaneState(_ *paneNode, target *paneNode) { | 1564 | func (wm *WorkspaceManager) clonePaneState(_ *paneNode, target *paneNode) { | ||
| 1451 | if wm == nil || target == nil { | 1565 | if wm == nil || target == nil { | ||
| @@ -1476,10 +1590,10 @@ func (wm *WorkspaceManager) clonePaneState(_ *paneNode, target *paneNode) { | |||||
| 1476 | } | 1590 | } | ||
| 1477 | 1591 | ||||
| 1478 | func (wm *WorkspaceManager) closeCurrentPane() { | 1592 | func (wm *WorkspaceManager) closeCurrentPane() { | ||
| 1479 | - | if wm == nil || wm.active == nil { | 1593 | + | if wm == nil || wm.currentlyFocused == nil { |
| 1480 | return | 1594 | return | ||
| 1481 | } | 1595 | } | ||
| 1482 | - | if err := wm.closePane(wm.active); err != nil { | 1596 | + | if err := wm.closePane(wm.currentlyFocused); err != nil { |
| 1483 | log.Printf("[workspace] close current pane failed: %v", err) | 1597 | log.Printf("[workspace] close current pane failed: %v", err) | ||
| 1484 | } | 1598 | } | ||
| 1485 | } | 1599 | } | ||
| @@ -1494,19 +1608,22 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||||
| 1494 | 1608 | ||||
| 1495 | // Handle closing panes in a stack | 1609 | // Handle closing panes in a stack | ||
| 1496 | if node.parent != nil && node.parent.isStacked { | 1610 | if node.parent != nil && node.parent.isStacked { | ||
| 1497 | - | return wm.closeStackedPane(node) | 1611 | + | return wm.stackedPaneManager.CloseStackedPane(node) |
| 1498 | } | 1612 | } | ||
| 1499 | 1613 | ||||
| 1500 | if node.parent != nil && node.parent.container != 0 { | 1614 | if node.parent != nil && node.parent.container != 0 { | ||
| 1501 | if node.parent.left == node { | 1615 | if node.parent.left == node { | ||
| 1502 | webkit.PanedSetStartChild(node.parent.container, 0) | 1616 | webkit.PanedSetStartChild(node.parent.container, 0) | ||
| 1617 | + | // GTK4 automatically unparents when setting child to 0 | |||
| 1503 | } else if node.parent.right == node { | 1618 | } else if node.parent.right == node { | ||
| 1504 | webkit.PanedSetEndChild(node.parent.container, 0) | 1619 | webkit.PanedSetEndChild(node.parent.container, 0) | ||
| 1620 | + | // GTK4 automatically unparents when setting child to 0 | |||
| 1621 | + | } | |||
| 1622 | + | } else if node.container != 0 { | |||
| 1623 | + | // Only manually unparent if widget wasn't auto-unparented above | |||
| 1624 | + | if webkit.WidgetGetParent(node.container) != 0 { | |||
| 1625 | + | webkit.WidgetUnparent(node.container) | |||
| 1505 | } | 1626 | } | ||
| 1506 | - | } | |||
| 1507 | - | ||||
| 1508 | - | if node.container != 0 { | |||
| 1509 | - | webkit.WidgetUnparent(node.container) | |||
| 1510 | } | 1627 | } | ||
| 1511 | 1628 | ||||
| 1512 | remaining := len(wm.app.panes) | 1629 | remaining := len(wm.app.panes) | ||
| @@ -1586,7 +1703,7 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||||
| 1586 | } | 1703 | } | ||
| 1587 | 1704 | ||||
| 1588 | wm.root = nil | 1705 | wm.root = nil | ||
| 1589 | - | wm.active = nil | 1706 | + | wm.currentlyFocused = nil |
| 1590 | wm.mainPane = nil | 1707 | wm.mainPane = nil | ||
| 1591 | 1708 | ||||
| 1592 | webkit.QuitMainLoop() | 1709 | webkit.QuitMainLoop() | ||
| @@ -1646,8 +1763,8 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||||
| 1646 | } | 1763 | } | ||
| 1647 | 1764 | ||||
| 1648 | // Clear current active if it's the node being closed | 1765 | // Clear current active if it's the node being closed | ||
| 1649 | - | if wm.active == node { | 1766 | + | if wm.currentlyFocused == node { |
| 1650 | - | wm.active = nil | 1767 | + | wm.currentlyFocused = nil |
| 1651 | } | 1768 | } | ||
| 1652 | 1769 | ||||
| 1653 | // Set replacement as new root | 1770 | // Set replacement as new root | ||
| @@ -1666,7 +1783,8 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||||
| 1666 | // Focus a suitable pane | 1783 | // Focus a suitable pane | ||
| 1667 | focusTarget := wm.leftmostLeaf(replacement) | 1784 | focusTarget := wm.leftmostLeaf(replacement) | ||
| 1668 | if focusTarget != nil { | 1785 | if focusTarget != nil { | ||
| 1669 | - | wm.focusNode(focusTarget) | 1786 | + | wm.currentlyFocused = focusTarget |
| 1787 | + | wm.focusManager.SetActivePane(focusTarget) | |||
| 1670 | } | 1788 | } | ||
| 1671 | 1789 | ||||
| 1672 | // Destroy the webview and detach hover AFTER rearranging hierarchy | 1790 | // Destroy the webview and detach hover AFTER rearranging hierarchy | ||
| @@ -1731,8 +1849,8 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||||
| 1731 | } | 1849 | } | ||
| 1732 | 1850 | ||||
| 1733 | // Clear current active if it's the node being closed | 1851 | // Clear current active if it's the node being closed | ||
| 1734 | - | if wm.active == node { | 1852 | + | if wm.currentlyFocused == node { |
| 1735 | - | wm.active = nil | 1853 | + | wm.currentlyFocused = nil |
| 1736 | } | 1854 | } | ||
| 1737 | 1855 | ||||
| 1738 | grand := parent.parent | 1856 | grand := parent.parent | ||
| @@ -1771,6 +1889,12 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||||
| 1771 | } | 1889 | } | ||
| 1772 | 1890 | ||||
| 1773 | // Now safely reparent the sibling to the grandparent | 1891 | // Now safely reparent the sibling to the grandparent | ||
| 1892 | + | // Ensure GTK focus state is synchronized before reparenting to prevent | |||
| 1893 | + | // GTK-WARNING: "Error finding last focus widget of GtkPaned, gtk_paned_set_focus_child | |||
| 1894 | + | // was called on widget (nil) which is not child" | |||
| 1895 | + | webkit.WidgetWaitForDraw(sibling.container) | |||
| 1896 | + | webkit.WidgetWaitForDraw(grand.container) | |||
| 1897 | + | ||||
| 1774 | sibling.parent = grand | 1898 | sibling.parent = grand | ||
| 1775 | if grand.container != 0 && !grand.isLeaf { | 1899 | if grand.container != 0 && !grand.isLeaf { | ||
| 1776 | if grand.left == parent { | 1900 | if grand.left == parent { | ||
| @@ -1796,7 +1920,8 @@ func (wm *WorkspaceManager) closePane(node *paneNode) error { | |||||
| 1796 | 1920 | ||||
| 1797 | // Focus the target pane | 1921 | // Focus the target pane | ||
| 1798 | if focusTarget != nil && focusTarget != node { | 1922 | if focusTarget != nil && focusTarget != node { | ||
| 1799 | - | wm.focusNode(focusTarget) | 1923 | + | wm.currentlyFocused = focusTarget |
| 1924 | + | wm.focusManager.SetActivePane(focusTarget) | |||
| 1800 | } | 1925 | } | ||
| 1801 | 1926 | ||||
| 1802 | // Destroy the webview and detach hover AFTER all hierarchy changes are complete | 1927 | // Destroy the webview and detach hover AFTER all hierarchy changes are complete | ||
| @@ -1913,12 +2038,17 @@ func (wm *WorkspaceManager) closeStackedPane(node *paneNode) error { | |||||
| 1913 | } | 2038 | } | ||
| 1914 | 2039 | ||||
| 1915 | // Remove the pane's widgets from the stack container | 2040 | // Remove the pane's widgets from the stack container | ||
| 2041 | + | stackBox := stackNode.stackWrapper | |||
| 2042 | + | if stackBox == 0 { | |||
| 2043 | + | stackBox = stackNode.container | |||
| 2044 | + | } | |||
| 2045 | + | ||||
| 1916 | if node.titleBar != 0 { | 2046 | if node.titleBar != 0 { | ||
| 1917 | - | webkit.BoxRemove(stackNode.container, node.titleBar) | 2047 | + | webkit.BoxRemove(stackBox, node.titleBar) |
| 1918 | } | 2048 | } | ||
| 1919 | if node.container != 0 { | 2049 | if node.container != 0 { | ||
| 1920 | - | webkit.BoxRemove(stackNode.container, node.container) | 2050 | + | webkit.BoxRemove(stackBox, node.container) |
| 1921 | - | webkit.WidgetUnparent(node.container) | 2051 | + | // BoxRemove automatically unparents in GTK4, no manual unparent needed |
| 1922 | } | 2052 | } | ||
| 1923 | 2053 | ||||
| 1924 | // Clean up the pane | 2054 | // Clean up the pane | ||
| @@ -1935,51 +2065,47 @@ func (wm *WorkspaceManager) closeStackedPane(node *paneNode) error { | |||||
| 1935 | } else if len(stackNode.stackedPanes) == 1 { | 2065 | } else if len(stackNode.stackedPanes) == 1 { | ||
| 1936 | // Only one pane left - convert back to a regular pane | 2066 | // Only one pane left - convert back to a regular pane | ||
| 1937 | lastPane := stackNode.stackedPanes[0] | 2067 | lastPane := stackNode.stackedPanes[0] | ||
| 2068 | + | parent := stackNode.parent | |||
| 2069 | + | lastPaneContainer := lastPane.container | |||
| 1938 | 2070 | ||||
| 1939 | // Remove the title bar since we're going back to single pane | 2071 | // Remove the title bar since we're going back to single pane | ||
| 1940 | if lastPane.titleBar != 0 { | 2072 | if lastPane.titleBar != 0 { | ||
| 1941 | - | webkit.BoxRemove(stackNode.container, lastPane.titleBar) | 2073 | + | webkit.BoxRemove(stackBox, lastPane.titleBar) |
| 1942 | } | 2074 | } | ||
| 1943 | 2075 | ||||
| 1944 | - | // Replace the stack container with the pane's container | 2076 | + | // Unparent the pane container from stack wrapper |
| 1945 | - | parent := stackNode.parent | 2077 | + | webkit.BoxRemove(stackBox, lastPaneContainer) |
| 1946 | - | lastPaneContainer := lastPane.container | |||
| 1947 | - | ||||
| 1948 | - | // Unparent from stack container | |||
| 1949 | - | webkit.BoxRemove(stackNode.container, lastPaneContainer) | |||
| 1950 | - | webkit.WidgetUnparent(lastPaneContainer) | |||
| 1951 | - | ||||
| 1952 | - | // Convert back to regular pane structure | |||
| 1953 | - | lastPane.parent = parent | |||
| 1954 | - | lastPane.isStacked = false | |||
| 1955 | - | lastPane.stackedPanes = nil | |||
| 1956 | - | lastPane.titleBar = 0 | |||
| 1957 | - | stackNode.isStacked = false | |||
| 1958 | - | stackNode.stackedPanes = nil | |||
| 1959 | - | stackNode.container = lastPaneContainer | |||
| 1960 | - | stackNode.isLeaf = true | |||
| 1961 | - | stackNode.pane = lastPane.pane | |||
| 1962 | 2078 | ||||
| 1963 | - | // Update parent references | 2079 | + | // CRITICAL FIX: Replace the stackNode completely with lastPane |
| 2080 | + | // Update parent child references FIRST | |||
| 1964 | if parent == nil { | 2081 | if parent == nil { | ||
| 1965 | - | wm.root = stackNode | 2082 | + | wm.root = lastPane |
| 1966 | if wm.window != nil { | 2083 | if wm.window != nil { | ||
| 1967 | wm.window.SetChild(lastPaneContainer) | 2084 | wm.window.SetChild(lastPaneContainer) | ||
| 1968 | } | 2085 | } | ||
| 1969 | } else { | 2086 | } else { | ||
| 1970 | if parent.left == stackNode { | 2087 | if parent.left == stackNode { | ||
| 2088 | + | parent.left = lastPane | |||
| 1971 | webkit.PanedSetStartChild(parent.container, lastPaneContainer) | 2089 | webkit.PanedSetStartChild(parent.container, lastPaneContainer) | ||
| 1972 | } else if parent.right == stackNode { | 2090 | } else if parent.right == stackNode { | ||
| 2091 | + | parent.right = lastPane | |||
| 1973 | webkit.PanedSetEndChild(parent.container, lastPaneContainer) | 2092 | webkit.PanedSetEndChild(parent.container, lastPaneContainer) | ||
| 1974 | } | 2093 | } | ||
| 1975 | } | 2094 | } | ||
| 1976 | 2095 | ||||
| 1977 | - | // Update the viewToNode mapping | 2096 | + | // Convert lastPane back to regular pane |
| 1978 | - | wm.viewToNode[lastPane.pane.webView] = stackNode | 2097 | + | lastPane.parent = parent |
| 2098 | + | lastPane.isStacked = false | |||
| 2099 | + | lastPane.stackedPanes = nil | |||
| 2100 | + | lastPane.titleBar = 0 | |||
| 2101 | + | ||||
| 2102 | + | // Update the viewToNode mapping to point to the lastPane, not stackNode | |||
| 2103 | + | wm.viewToNode[lastPane.pane.webView] = lastPane | |||
| 1979 | 2104 | ||||
| 1980 | // Focus the remaining pane if it was the active one being closed | 2105 | // Focus the remaining pane if it was the active one being closed | ||
| 1981 | - | if wm.active == node { | 2106 | + | if wm.currentlyFocused == node { |
| 1982 | - | wm.focusNode(stackNode) | 2107 | + | wm.currentlyFocused = lastPane |
| 2108 | + | wm.focusManager.SetActivePane(lastPane) | |||
| 1983 | } | 2109 | } | ||
| 1984 | 2110 | ||||
| 1985 | log.Printf("[workspace] converted single-pane stack back to regular pane") | 2111 | log.Printf("[workspace] converted single-pane stack back to regular pane") | ||
| @@ -1993,12 +2119,13 @@ func (wm *WorkspaceManager) closeStackedPane(node *paneNode) error { | |||||
| 1993 | } | 2119 | } | ||
| 1994 | 2120 | ||||
| 1995 | // Update visibility for remaining panes | 2121 | // Update visibility for remaining panes | ||
| 1996 | - | wm.updateStackVisibility(stackNode) | 2122 | + | wm.stackedPaneManager.UpdateStackVisibility(stackNode) |
| 1997 | 2123 | ||||
| 1998 | // Focus the new active pane if we closed the currently active one | 2124 | // Focus the new active pane if we closed the currently active one | ||
| 1999 | - | if wm.active == node { | 2125 | + | if wm.currentlyFocused == node { |
| 2000 | newActivePaneInStack := stackNode.stackedPanes[stackNode.activeStackIndex] | 2126 | newActivePaneInStack := stackNode.stackedPanes[stackNode.activeStackIndex] | ||
| 2001 | - | wm.focusNode(newActivePaneInStack) | 2127 | + | wm.currentlyFocused = newActivePaneInStack |
| 2128 | + | wm.focusManager.SetActivePane(newActivePaneInStack) | |||
| 2002 | } | 2129 | } | ||
| 2003 | 2130 | ||||
| 2004 | log.Printf("[workspace] closed pane from stack: remaining=%d activeIndex=%d", | 2131 | log.Printf("[workspace] closed pane from stack: remaining=%d activeIndex=%d", | ||
| @@ -2021,8 +2148,8 @@ func (wm *WorkspaceManager) updateMainPane() { | |||||
| 2021 | } | 2148 | } | ||
| 2022 | 2149 | ||||
| 2023 | if wm.mainPane == nil || !wm.mainPane.isLeaf { | 2150 | if wm.mainPane == nil || !wm.mainPane.isLeaf { | ||
| 2024 | - | if wm.active != nil && wm.active.isLeaf { | 2151 | + | if wm.currentlyFocused != nil && wm.currentlyFocused.isLeaf { |
| 2025 | - | wm.mainPane = wm.active | 2152 | + | wm.mainPane = wm.currentlyFocused |
| 2026 | } | 2153 | } | ||
| 2027 | } | 2154 | } | ||
| 2028 | } | 2155 | } | ||
| @@ -2139,8 +2266,8 @@ func (wm *WorkspaceManager) HandlePopup(source *webkit.WebView, url string) *web | |||||
| 2139 | 2266 | ||||
| 2140 | // Determine target node for splitting | 2267 | // Determine target node for splitting | ||
| 2141 | target := node | 2268 | target := node | ||
| 2142 | - | if !popCfg.FollowPaneContext && wm.active != nil { | 2269 | + | if !popCfg.FollowPaneContext && wm.currentlyFocused != nil { |
| 2143 | - | target = wm.active | 2270 | + | target = wm.currentlyFocused |
| 2144 | } | 2271 | } | ||
| 2145 | 2272 | ||||
| 2146 | // Add the popup pane to the workspace using manual pane insertion | 2273 | // Add the popup pane to the workspace using manual pane insertion | ||
| @@ -2621,10 +2748,23 @@ func (wm *WorkspaceManager) handleDetectedWindowType(sourceNode *paneNode, webVi | |||||
| 2621 | 2748 | ||||
| 2622 | // insertPopupPane inserts a pre-created popup pane into the workspace | 2749 | // insertPopupPane inserts a pre-created popup pane into the workspace | ||
| 2623 | func (wm *WorkspaceManager) insertPopupPane(target *paneNode, newPane *BrowserPane, direction string) error { | 2750 | func (wm *WorkspaceManager) insertPopupPane(target *paneNode, newPane *BrowserPane, direction string) error { | ||
| 2624 | - | if target == nil || !target.isLeaf || target.pane == nil { | 2751 | + | if target == nil { |
| 2625 | - | return errors.New("insert target must be a leaf pane") | 2752 | + | return errors.New("insert target cannot be nil") |
| 2753 | + | } | |||
| 2754 | + | ||||
| 2755 | + | // Accept both leaf panes and stacked panes as valid targets | |||
| 2756 | + | if !target.isLeaf && !target.isStacked { | |||
| 2757 | + | return errors.New("insert target must be a leaf pane or stacked pane") | |||
| 2626 | } | 2758 | } | ||
| 2627 | 2759 | ||||
| 2760 | + | // For leaf panes, require a pane | |||
| 2761 | + | if target.isLeaf && target.pane == nil { | |||
| 2762 | + | return errors.New("leaf target missing pane") | |||
| 2763 | + | } | |||
| 2764 | + | ||||
| 2765 | + | // For stacked panes, we trust the caller - production stacks have varied structures | |||
| 2766 | + | // Do not validate internal stack structure here | |||
| 2767 | + | ||||
| 2628 | if newPane == nil || newPane.webView == nil { | 2768 | if newPane == nil || newPane.webView == nil { | ||
| 2629 | return errors.New("new pane missing webview") | 2769 | return errors.New("new pane missing webview") | ||
| 2630 | } | 2770 | } | ||
| @@ -2752,7 +2892,8 @@ func (wm *WorkspaceManager) insertPopupPane(target *paneNode, newPane *BrowserPa | |||||
| 2752 | if newContainer != 0 { | 2892 | if newContainer != 0 { | ||
| 2753 | webkit.WidgetShow(newContainer) | 2893 | webkit.WidgetShow(newContainer) | ||
| 2754 | } | 2894 | } | ||
| 2755 | - | wm.focusNode(newLeaf) | 2895 | + | wm.currentlyFocused = newLeaf |
| 2896 | + | wm.focusManager.SetActivePane(newLeaf) | |||
| 2756 | return false | 2897 | return false | ||
| 2757 | }) | 2898 | }) | ||
| 2758 | 2899 | ||||
M internal/app/browser/workspace_manager_bench_test.go +2 -2
| @@ -144,7 +144,7 @@ func BenchmarkFocusAdjacent(b *testing.B) { | |||
| 144 | 144 | webkit.SetWidgetBoundsForTesting(right.container, webkit.WidgetBounds{X: 120, Y: 0, Width: 100, Height: 100}) | |
| 145 | 145 | webkit.SetWidgetBoundsForTesting(bottom.container, webkit.WidgetBounds{X: 0, Y: 120, Width: 100, Height: 100}) | |
| 146 | 146 | ||
| 147 | - | wm.active = wm.root | |
| 147 | + | wm.currentlyFocused = wm.root | |
| 148 | 148 | ||
| 149 | 149 | b.ResetTimer() | |
| 150 | 150 | for i := 0; i < b.N; i++ { | |
| @@ -336,7 +336,7 @@ func newTestWorkspaceManagerWithMocks(tb testing.TB) *WorkspaceManager { | |||
| 336 | 336 | isLeaf: true, | |
| 337 | 337 | } | |
| 338 | 338 | wm.root = root | |
| 339 | - | wm.active = root | |
| 339 | + | wm.currentlyFocused = root | |
| 340 | 340 | wm.mainPane = root | |
| 341 | 341 | wm.viewToNode[rootView] = root | |
| 342 | 342 | ||
| @@ -144,7 +144,7 @@ func BenchmarkFocusAdjacent(b *testing.B) { | |||||
| 144 | webkit.SetWidgetBoundsForTesting(right.container, webkit.WidgetBounds{X: 120, Y: 0, Width: 100, Height: 100}) | 144 | webkit.SetWidgetBoundsForTesting(right.container, webkit.WidgetBounds{X: 120, Y: 0, Width: 100, Height: 100}) | ||
| 145 | webkit.SetWidgetBoundsForTesting(bottom.container, webkit.WidgetBounds{X: 0, Y: 120, Width: 100, Height: 100}) | 145 | webkit.SetWidgetBoundsForTesting(bottom.container, webkit.WidgetBounds{X: 0, Y: 120, Width: 100, Height: 100}) | ||
| 146 | 146 | ||||
| 147 | - | wm.active = wm.root | 147 | + | wm.currentlyFocused = wm.root |
| 148 | 148 | ||||
| 149 | b.ResetTimer() | 149 | b.ResetTimer() | ||
| 150 | for i := 0; i < b.N; i++ { | 150 | for i := 0; i < b.N; i++ { | ||
| @@ -336,7 +336,7 @@ func newTestWorkspaceManagerWithMocks(tb testing.TB) *WorkspaceManager { | |||||
| 336 | isLeaf: true, | 336 | isLeaf: true, | ||
| 337 | } | 337 | } | ||
| 338 | wm.root = root | 338 | wm.root = root | ||
| 339 | - | wm.active = root | 339 | + | wm.currentlyFocused = root |
| 340 | wm.mainPane = root | 340 | wm.mainPane = root | ||
| 341 | wm.viewToNode[rootView] = root | 341 | wm.viewToNode[rootView] = root | ||
| 342 | 342 | ||||
M internal/app/browser/workspace_manager_test.go +11 -9
| @@ -53,7 +53,7 @@ func TestSplitNodeCreatesExpectedTree(t *testing.T) { | |||
| 53 | 53 | for _, tc := range cases { | |
| 54 | 54 | t.Run(tc.name, func(t *testing.T) { | |
| 55 | 55 | wm := newTestWorkspaceManager(t) | |
| 56 | - | original := wm.active | |
| 56 | + | original := wm.currentlyFocused | |
| 57 | 57 | ||
| 58 | 58 | newLeaf, err := wm.splitNode(original, tc.direction) | |
| 59 | 59 | if err != nil { | |
| @@ -103,7 +103,7 @@ func TestSplitNodeCreatesExpectedTree(t *testing.T) { | |||
| 103 | 103 | ||
| 104 | 104 | func TestClosePanePromotesSibling(t *testing.T) { | |
| 105 | 105 | wm := newTestWorkspaceManager(t) | |
| 106 | - | original := wm.active | |
| 106 | + | original := wm.currentlyFocused | |
| 107 | 107 | ||
| 108 | 108 | newLeaf, err := wm.splitNode(original, "right") | |
| 109 | 109 | if err != nil { | |
| @@ -130,14 +130,14 @@ func TestClosePanePromotesSibling(t *testing.T) { | |||
| 130 | 130 | t.Fatalf("expected remaining pane to match original") | |
| 131 | 131 | } | |
| 132 | 132 | ||
| 133 | - | if wm.active != original { | |
| 133 | + | if wm.currentlyFocused != original { | |
| 134 | 134 | t.Fatalf("expected focus to move to promoted pane") | |
| 135 | 135 | } | |
| 136 | 136 | } | |
| 137 | 137 | ||
| 138 | 138 | func TestFocusNeighborWithBounds(t *testing.T) { | |
| 139 | 139 | wm := newTestWorkspaceManager(t) | |
| 140 | - | left := wm.active | |
| 140 | + | left := wm.currentlyFocused | |
| 141 | 141 | ||
| 142 | 142 | right, err := wm.splitNode(left, "right") | |
| 143 | 143 | if err != nil { | |
| @@ -156,7 +156,7 @@ func TestFocusNeighborWithBounds(t *testing.T) { | |||
| 156 | 156 | t.Fatalf("expected focus neighbor left to succeed") | |
| 157 | 157 | } | |
| 158 | 158 | ||
| 159 | - | if wm.active != left { | |
| 159 | + | if wm.currentlyFocused != left { | |
| 160 | 160 | t.Fatalf("expected focus to move to left pane") | |
| 161 | 161 | } | |
| 162 | 162 | ||
| @@ -172,7 +172,7 @@ func TestFocusNeighborWithBounds(t *testing.T) { | |||
| 172 | 172 | webkit.SetWidgetBoundsForTesting(bottom.container, webkit.WidgetBounds{X: 0, Y: 120, Width: 100, Height: 90}) | |
| 173 | 173 | ||
| 174 | 174 | // Active pane should be bottom after split. Move focus up. | |
| 175 | - | if wm.active != bottom { | |
| 175 | + | if wm.currentlyFocused != bottom { | |
| 176 | 176 | t.Fatalf("expected bottom pane to be active after split") | |
| 177 | 177 | } | |
| 178 | 178 | ||
| @@ -180,7 +180,7 @@ func TestFocusNeighborWithBounds(t *testing.T) { | |||
| 180 | 180 | t.Fatalf("expected focus neighbor up to succeed") | |
| 181 | 181 | } | |
| 182 | 182 | ||
| 183 | - | if wm.active != top { | |
| 183 | + | if wm.currentlyFocused != top { | |
| 184 | 184 | t.Fatalf("expected focus to move to top pane") | |
| 185 | 185 | } | |
| 186 | 186 | } | |
| @@ -238,7 +238,8 @@ func TestWorkspaceNavigationAfterFocusChanges(t *testing.T) { | |||
| 238 | 238 | // works consistently (the old bug was per-webview shortcut registration) | |
| 239 | 239 | for i := 0; i < 3; i++ { | |
| 240 | 240 | // Move focus left | |
| 241 | - | wm.focusNode(left) | |
| 241 | + | wm.currentlyFocused = left | |
| 242 | + | wm.focusManager.SetActivePane(left) | |
| 242 | 243 | if wm.GetActiveNode() != left { | |
| 243 | 244 | t.Fatalf("expected left pane to be active after focusNode call %d", i) | |
| 244 | 245 | } | |
| @@ -249,7 +250,8 @@ func TestWorkspaceNavigationAfterFocusChanges(t *testing.T) { | |||
| 249 | 250 | } | |
| 250 | 251 | ||
| 251 | 252 | // Move focus right | |
| 252 | - | wm.focusNode(right) | |
| 253 | + | wm.currentlyFocused = right | |
| 254 | + | wm.focusManager.SetActivePane(right) | |
| 253 | 255 | if wm.GetActiveNode() != right { | |
| 254 | 256 | t.Fatalf("expected right pane to be active after focusNode call %d", i) | |
| 255 | 257 | } | |
| @@ -53,7 +53,7 @@ func TestSplitNodeCreatesExpectedTree(t *testing.T) { | |||||
| 53 | for _, tc := range cases { | 53 | for _, tc := range cases { | ||
| 54 | t.Run(tc.name, func(t *testing.T) { | 54 | t.Run(tc.name, func(t *testing.T) { | ||
| 55 | wm := newTestWorkspaceManager(t) | 55 | wm := newTestWorkspaceManager(t) | ||
| 56 | - | original := wm.active | 56 | + | original := wm.currentlyFocused |
| 57 | 57 | ||||
| 58 | newLeaf, err := wm.splitNode(original, tc.direction) | 58 | newLeaf, err := wm.splitNode(original, tc.direction) | ||
| 59 | if err != nil { | 59 | if err != nil { | ||
| @@ -103,7 +103,7 @@ func TestSplitNodeCreatesExpectedTree(t *testing.T) { | |||||
| 103 | 103 | ||||
| 104 | func TestClosePanePromotesSibling(t *testing.T) { | 104 | func TestClosePanePromotesSibling(t *testing.T) { | ||
| 105 | wm := newTestWorkspaceManager(t) | 105 | wm := newTestWorkspaceManager(t) | ||
| 106 | - | original := wm.active | 106 | + | original := wm.currentlyFocused |
| 107 | 107 | ||||
| 108 | newLeaf, err := wm.splitNode(original, "right") | 108 | newLeaf, err := wm.splitNode(original, "right") | ||
| 109 | if err != nil { | 109 | if err != nil { | ||
| @@ -130,14 +130,14 @@ func TestClosePanePromotesSibling(t *testing.T) { | |||||
| 130 | t.Fatalf("expected remaining pane to match original") | 130 | t.Fatalf("expected remaining pane to match original") | ||
| 131 | } | 131 | } | ||
| 132 | 132 | ||||
| 133 | - | if wm.active != original { | 133 | + | if wm.currentlyFocused != original { |
| 134 | t.Fatalf("expected focus to move to promoted pane") | 134 | t.Fatalf("expected focus to move to promoted pane") | ||
| 135 | } | 135 | } | ||
| 136 | } | 136 | } | ||
| 137 | 137 | ||||
| 138 | func TestFocusNeighborWithBounds(t *testing.T) { | 138 | func TestFocusNeighborWithBounds(t *testing.T) { | ||
| 139 | wm := newTestWorkspaceManager(t) | 139 | wm := newTestWorkspaceManager(t) | ||
| 140 | - | left := wm.active | 140 | + | left := wm.currentlyFocused |
| 141 | 141 | ||||
| 142 | right, err := wm.splitNode(left, "right") | 142 | right, err := wm.splitNode(left, "right") | ||
| 143 | if err != nil { | 143 | if err != nil { | ||
| @@ -156,7 +156,7 @@ func TestFocusNeighborWithBounds(t *testing.T) { | |||||
| 156 | t.Fatalf("expected focus neighbor left to succeed") | 156 | t.Fatalf("expected focus neighbor left to succeed") | ||
| 157 | } | 157 | } | ||
| 158 | 158 | ||||
| 159 | - | if wm.active != left { | 159 | + | if wm.currentlyFocused != left { |
| 160 | t.Fatalf("expected focus to move to left pane") | 160 | t.Fatalf("expected focus to move to left pane") | ||
| 161 | } | 161 | } | ||
| 162 | 162 | ||||
| @@ -172,7 +172,7 @@ func TestFocusNeighborWithBounds(t *testing.T) { | |||||
| 172 | webkit.SetWidgetBoundsForTesting(bottom.container, webkit.WidgetBounds{X: 0, Y: 120, Width: 100, Height: 90}) | 172 | webkit.SetWidgetBoundsForTesting(bottom.container, webkit.WidgetBounds{X: 0, Y: 120, Width: 100, Height: 90}) | ||
| 173 | 173 | ||||
| 174 | // Active pane should be bottom after split. Move focus up. | 174 | // Active pane should be bottom after split. Move focus up. | ||
| 175 | - | if wm.active != bottom { | 175 | + | if wm.currentlyFocused != bottom { |
| 176 | t.Fatalf("expected bottom pane to be active after split") | 176 | t.Fatalf("expected bottom pane to be active after split") | ||
| 177 | } | 177 | } | ||
| 178 | 178 | ||||
| @@ -180,7 +180,7 @@ func TestFocusNeighborWithBounds(t *testing.T) { | |||||
| 180 | t.Fatalf("expected focus neighbor up to succeed") | 180 | t.Fatalf("expected focus neighbor up to succeed") | ||
| 181 | } | 181 | } | ||
| 182 | 182 | ||||
| 183 | - | if wm.active != top { | 183 | + | if wm.currentlyFocused != top { |
| 184 | t.Fatalf("expected focus to move to top pane") | 184 | t.Fatalf("expected focus to move to top pane") | ||
| 185 | } | 185 | } | ||
| 186 | } | 186 | } | ||
| @@ -238,7 +238,8 @@ func TestWorkspaceNavigationAfterFocusChanges(t *testing.T) { | |||||
| 238 | // works consistently (the old bug was per-webview shortcut registration) | 238 | // works consistently (the old bug was per-webview shortcut registration) | ||
| 239 | for i := 0; i < 3; i++ { | 239 | for i := 0; i < 3; i++ { | ||
| 240 | // Move focus left | 240 | // Move focus left | ||
| 241 | - | wm.focusNode(left) | 241 | + | wm.currentlyFocused = left |
| 242 | + | wm.focusManager.SetActivePane(left) | |||
| 242 | if wm.GetActiveNode() != left { | 243 | if wm.GetActiveNode() != left { | ||
| 243 | t.Fatalf("expected left pane to be active after focusNode call %d", i) | 244 | t.Fatalf("expected left pane to be active after focusNode call %d", i) | ||
| 244 | } | 245 | } | ||
| @@ -249,7 +250,8 @@ func TestWorkspaceNavigationAfterFocusChanges(t *testing.T) { | |||||
| 249 | } | 250 | } | ||
| 250 | 251 | ||||
| 251 | // Move focus right | 252 | // Move focus right | ||
| 252 | - | wm.focusNode(right) | 253 | + | wm.currentlyFocused = right |
| 254 | + | wm.focusManager.SetActivePane(right) | |||
| 253 | if wm.GetActiveNode() != right { | 255 | if wm.GetActiveNode() != right { | ||
| 254 | t.Fatalf("expected right pane to be active after focusNode call %d", i) | 256 | t.Fatalf("expected right pane to be active after focusNode call %d", i) | ||
| 255 | } | 257 | } | ||
M internal/app/browser/workspace_manager_tree_test.go +5 -5
| @@ -449,7 +449,7 @@ func TestFocusManagement(t *testing.T) { | |||
| 449 | 449 | newNode, _ := wm.splitNode(original, "right") | |
| 450 | 450 | ||
| 451 | 451 | // Set new node as active | |
| 452 | - | wm.active = newNode | |
| 452 | + | wm.currentlyFocused = newNode | |
| 453 | 453 | ||
| 454 | 454 | // Close the active node | |
| 455 | 455 | err := wm.closePane(newNode) | |
| @@ -458,7 +458,7 @@ func TestFocusManagement(t *testing.T) { | |||
| 458 | 458 | } | |
| 459 | 459 | ||
| 460 | 460 | // Focus should transfer to original | |
| 461 | - | if wm.active != original { | |
| 461 | + | if wm.currentlyFocused != original { | |
| 462 | 462 | t.Error("Focus should transfer to remaining pane") | |
| 463 | 463 | } | |
| 464 | 464 | }) | |
| @@ -470,7 +470,7 @@ func TestFocusManagement(t *testing.T) { | |||
| 470 | 470 | newNode, _ := wm.splitNode(original, "right") | |
| 471 | 471 | ||
| 472 | 472 | // Keep original as active | |
| 473 | - | wm.active = original | |
| 473 | + | wm.currentlyFocused = original | |
| 474 | 474 | ||
| 475 | 475 | // Close the non-active node | |
| 476 | 476 | err := wm.closePane(newNode) | |
| @@ -479,7 +479,7 @@ func TestFocusManagement(t *testing.T) { | |||
| 479 | 479 | } | |
| 480 | 480 | ||
| 481 | 481 | // Focus should remain on original | |
| 482 | - | if wm.active != original { | |
| 482 | + | if wm.currentlyFocused != original { | |
| 483 | 483 | t.Error("Focus should remain on original pane") | |
| 484 | 484 | } | |
| 485 | 485 | }) | |
| @@ -925,7 +925,7 @@ func newTestWorkspaceManagerWithMocksForTree(t *testing.T) *WorkspaceManager { | |||
| 925 | 925 | isLeaf: true, | |
| 926 | 926 | } | |
| 927 | 927 | wm.root = root | |
| 928 | - | wm.active = root | |
| 928 | + | wm.currentlyFocused = root | |
| 929 | 929 | wm.mainPane = root | |
| 930 | 930 | wm.viewToNode[rootView] = root | |
| 931 | 931 | ||
| @@ -449,7 +449,7 @@ func TestFocusManagement(t *testing.T) { | |||||
| 449 | newNode, _ := wm.splitNode(original, "right") | 449 | newNode, _ := wm.splitNode(original, "right") | ||
| 450 | 450 | ||||
| 451 | // Set new node as active | 451 | // Set new node as active | ||
| 452 | - | wm.active = newNode | 452 | + | wm.currentlyFocused = newNode |
| 453 | 453 | ||||
| 454 | // Close the active node | 454 | // Close the active node | ||
| 455 | err := wm.closePane(newNode) | 455 | err := wm.closePane(newNode) | ||
| @@ -458,7 +458,7 @@ func TestFocusManagement(t *testing.T) { | |||||
| 458 | } | 458 | } | ||
| 459 | 459 | ||||
| 460 | // Focus should transfer to original | 460 | // Focus should transfer to original | ||
| 461 | - | if wm.active != original { | 461 | + | if wm.currentlyFocused != original { |
| 462 | t.Error("Focus should transfer to remaining pane") | 462 | t.Error("Focus should transfer to remaining pane") | ||
| 463 | } | 463 | } | ||
| 464 | }) | 464 | }) | ||
| @@ -470,7 +470,7 @@ func TestFocusManagement(t *testing.T) { | |||||
| 470 | newNode, _ := wm.splitNode(original, "right") | 470 | newNode, _ := wm.splitNode(original, "right") | ||
| 471 | 471 | ||||
| 472 | // Keep original as active | 472 | // Keep original as active | ||
| 473 | - | wm.active = original | 473 | + | wm.currentlyFocused = original |
| 474 | 474 | ||||
| 475 | // Close the non-active node | 475 | // Close the non-active node | ||
| 476 | err := wm.closePane(newNode) | 476 | err := wm.closePane(newNode) | ||
| @@ -479,7 +479,7 @@ func TestFocusManagement(t *testing.T) { | |||||
| 479 | } | 479 | } | ||
| 480 | 480 | ||||
| 481 | // Focus should remain on original | 481 | // Focus should remain on original | ||
| 482 | - | if wm.active != original { | 482 | + | if wm.currentlyFocused != original { |
| 483 | t.Error("Focus should remain on original pane") | 483 | t.Error("Focus should remain on original pane") | ||
| 484 | } | 484 | } | ||
| 485 | }) | 485 | }) | ||
| @@ -925,7 +925,7 @@ func newTestWorkspaceManagerWithMocksForTree(t *testing.T) *WorkspaceManager { | |||||
| 925 | isLeaf: true, | 925 | isLeaf: true, | ||
| 926 | } | 926 | } | ||
| 927 | wm.root = root | 927 | wm.root = root | ||
| 928 | - | wm.active = root | 928 | + | wm.currentlyFocused = root |
| 929 | wm.mainPane = root | 929 | wm.mainPane = root | ||
| 930 | wm.viewToNode[rootView] = root | 930 | wm.viewToNode[rootView] = root | ||
| 931 | 931 | ||||
A internal/app/browser/workspace_test.go +2210 -0 large diff (2210 lines changed): collapsed
M pkg/webkit/window.go +35 -1
| @@ -2,10 +2,14 @@ | |||
| 2 | 2 | ||
| 3 | 3 | package webkit | |
| 4 | 4 | ||
| 5 | + | import "sync" | |
| 6 | + | ||
| 5 | 7 | // Window represents a top-level application window hosting a WebView. | |
| 6 | 8 | // Stub for future GTK application window management. | |
| 7 | 9 | type Window struct { | |
| 8 | 10 | Title string | |
| 11 | + | child uintptr | |
| 12 | + | mu sync.Mutex | |
| 9 | 13 | } | |
| 10 | 14 | ||
| 11 | 15 | // NewWindow constructs a Window. In real implementation, this would create a GTK window. | |
| @@ -20,6 +24,36 @@ func (w *Window) SetTitle(title string) { | |||
| 20 | 24 | } | |
| 21 | 25 | ||
| 22 | 26 | // SetChild is a stubbed window child setter for non-CGO builds. | |
| 27 | + | // Simulates GTK4 behavior where setting child to 0 unparents the current child. | |
| 23 | 28 | func (w *Window) SetChild(child uintptr) { | |
| 24 | - | _ = child | |
| 29 | + | if w == nil { | |
| 30 | + | return | |
| 31 | + | } | |
| 32 | + | ||
| 33 | + | w.mu.Lock() | |
| 34 | + | defer w.mu.Unlock() | |
| 35 | + | ||
| 36 | + | // GTK4 behavior: Setting child to 0 removes and potentially invalidates the current child | |
| 37 | + | if child == 0 && w.child != 0 { | |
| 38 | + | // GTK4 automatically unparents the current child but keeps the widget alive | |
| 39 | + | widgetMu.Lock() | |
| 40 | + | if stub, ok := widgetState[w.child]; ok { | |
| 41 | + | stub.parent = 0 | |
| 42 | + | } | |
| 43 | + | widgetMu.Unlock() | |
| 44 | + | } | |
| 45 | + | ||
| 46 | + | // GTK4 validation: if child is not 0, it must be a valid widget | |
| 47 | + | if child != 0 { | |
| 48 | + | widgetMu.Lock() | |
| 49 | + | childStub, childExists := widgetState[child] | |
| 50 | + | if !childExists || childStub.isDestroyed { | |
| 51 | + | widgetMu.Unlock() | |
| 52 | + | panic("GTK-CRITICAL simulation: gtk_window_set_child: assertion 'GTK_IS_WIDGET (widget)' failed - child is invalid") | |
| 53 | + | } | |
| 54 | + | childStub.parent = 1 // Simplified window handle | |
| 55 | + | widgetMu.Unlock() | |
| 56 | + | } | |
| 57 | + | ||
| 58 | + | w.child = child | |
| 25 | 59 | } | |
| @@ -2,10 +2,14 @@ | |||||
| 2 | 2 | ||||
| 3 | package webkit | 3 | package webkit | ||
| 4 | 4 | ||||
| 5 | + | import "sync" | |||
| 6 | + | ||||
| 5 | // Window represents a top-level application window hosting a WebView. | 7 | // Window represents a top-level application window hosting a WebView. | ||
| 6 | // Stub for future GTK application window management. | 8 | // Stub for future GTK application window management. | ||
| 7 | type Window struct { | 9 | type Window struct { | ||
| 8 | Title string | 10 | Title string | ||
| 11 | + | child uintptr | |||
| 12 | + | mu sync.Mutex | |||
| 9 | } | 13 | } | ||
| 10 | 14 | ||||
| 11 | // NewWindow constructs a Window. In real implementation, this would create a GTK window. | 15 | // NewWindow constructs a Window. In real implementation, this would create a GTK window. | ||
| @@ -20,6 +24,36 @@ func (w *Window) SetTitle(title string) { | |||||
| 20 | } | 24 | } | ||
| 21 | 25 | ||||
| 22 | // SetChild is a stubbed window child setter for non-CGO builds. | 26 | // SetChild is a stubbed window child setter for non-CGO builds. | ||
| 27 | + | // Simulates GTK4 behavior where setting child to 0 unparents the current child. | |||
| 23 | func (w *Window) SetChild(child uintptr) { | 28 | func (w *Window) SetChild(child uintptr) { | ||
| 24 | - | _ = child | 29 | + | if w == nil { |
| 30 | + | return | |||
| 31 | + | } | |||
| 32 | + | ||||
| 33 | + | w.mu.Lock() | |||
| 34 | + | defer w.mu.Unlock() | |||
| 35 | + | ||||
| 36 | + | // GTK4 behavior: Setting child to 0 removes and potentially invalidates the current child | |||
| 37 | + | if child == 0 && w.child != 0 { | |||
| 38 | + | // GTK4 automatically unparents the current child but keeps the widget alive | |||
| 39 | + | widgetMu.Lock() | |||
| 40 | + | if stub, ok := widgetState[w.child]; ok { | |||
| 41 | + | stub.parent = 0 | |||
| 42 | + | } | |||
| 43 | + | widgetMu.Unlock() | |||
| 44 | + | } | |||
| 45 | + | ||||
| 46 | + | // GTK4 validation: if child is not 0, it must be a valid widget | |||
| 47 | + | if child != 0 { | |||
| 48 | + | widgetMu.Lock() | |||
| 49 | + | childStub, childExists := widgetState[child] | |||
| 50 | + | if !childExists || childStub.isDestroyed { | |||
| 51 | + | widgetMu.Unlock() | |||
| 52 | + | panic("GTK-CRITICAL simulation: gtk_window_set_child: assertion 'GTK_IS_WIDGET (widget)' failed - child is invalid") | |||
| 53 | + | } | |||
| 54 | + | childStub.parent = 1 // Simplified window handle | |||
| 55 | + | widgetMu.Unlock() | |||
| 56 | + | } | |||
| 57 | + | ||||
| 58 | + | w.child = child | |||
| 25 | } | 59 | } | ||
M pkg/webkit/workspace_widgets_cgo.go +43 -0
| @@ -5,6 +5,7 @@ package webkit | |||
| 5 | 5 | /* | |
| 6 | 6 | #cgo pkg-config: gtk4 | |
| 7 | 7 | #include <gtk/gtk.h> | |
| 8 | + | #include <gtk/gtktestutils.h> | |
| 8 | 9 | #include <gdk/gdk.h> | |
| 9 | 10 | #include <glib.h> | |
| 10 | 11 | #include <glib-object.h> | |
| @@ -179,6 +180,7 @@ static void widget_set_visible(GtkWidget* widget, gboolean visible) { | |||
| 179 | 180 | gtk_widget_set_visible(widget, visible); | |
| 180 | 181 | } | |
| 181 | 182 | ||
| 183 | + | ||
| 182 | 184 | static gboolean widget_get_visible(GtkWidget* widget) { | |
| 183 | 185 | if (!widget) return FALSE; | |
| 184 | 186 | return gtk_widget_get_visible(widget); | |
| @@ -194,6 +196,18 @@ static void widget_show(GtkWidget* widget) { | |||
| 194 | 196 | gtk_widget_set_visible(widget, TRUE); | |
| 195 | 197 | } | |
| 196 | 198 | ||
| 199 | + | static void workspace_gtk_test_init(void) { | |
| 200 | + | // Create dummy argc/argv for GTK test initialization | |
| 201 | + | // GTK test framework requires valid argc/argv pointers, not NULL | |
| 202 | + | static int test_argc = 1; | |
| 203 | + | static char test_prog[] = "dumber-test"; | |
| 204 | + | static char* test_argv[] = { test_prog, NULL }; | |
| 205 | + | static char** test_argv_ptr = test_argv; | |
| 206 | + | ||
| 207 | + | // Initialize GTK test framework with proper arguments | |
| 208 | + | gtk_test_init(&test_argc, &test_argv_ptr, NULL); | |
| 209 | + | } | |
| 210 | + | ||
| 197 | 211 | // Label helpers for title bars | |
| 198 | 212 | static GtkWidget* label_new(const char* text) { | |
| 199 | 213 | return gtk_label_new(text); | |
| @@ -213,6 +227,11 @@ static void label_set_ellipsize(GtkWidget* label, int mode) { | |||
| 213 | 227 | if (!label) return; | |
| 214 | 228 | gtk_label_set_ellipsize(GTK_LABEL(label), (PangoEllipsizeMode)mode); | |
| 215 | 229 | } | |
| 230 | + | ||
| 231 | + | static void label_set_max_width_chars(GtkWidget* label, int n_chars) { | |
| 232 | + | if (!label) return; | |
| 233 | + | gtk_label_set_max_width_chars(GTK_LABEL(label), n_chars); | |
| 234 | + | } | |
| 216 | 235 | */ | |
| 217 | 236 | import "C" | |
| 218 | 237 | ||
| @@ -235,6 +254,7 @@ var ( | |||
| 235 | 254 | hoverCallbacks = make(map[uintptr]func()) | |
| 236 | 255 | hoverControllers = make(map[uintptr]uintptr) | |
| 237 | 256 | nextHoverID uintptr = 1 | |
| 257 | + | gtkTestInitOnce sync.Once | |
| 238 | 258 | ) | |
| 239 | 259 | ||
| 240 | 260 | func widgetIsValid(widget uintptr) bool { | |
| @@ -734,3 +754,26 @@ func LabelSetEllipsize(label uintptr, mode EllipsizeMode) { | |||
| 734 | 754 | } | |
| 735 | 755 | C.label_set_ellipsize((*C.GtkWidget)(unsafe.Pointer(label)), C.int(mode)) | |
| 736 | 756 | } | |
| 757 | + | ||
| 758 | + | // LabelSetMaxWidthChars sets the maximum width in characters for a GtkLabel. | |
| 759 | + | func LabelSetMaxWidthChars(label uintptr, nChars int) { | |
| 760 | + | if label == 0 { | |
| 761 | + | return | |
| 762 | + | } | |
| 763 | + | C.label_set_max_width_chars((*C.GtkWidget)(unsafe.Pointer(label)), C.int(nChars)) | |
| 764 | + | } | |
| 765 | + | ||
| 766 | + | // WidgetWaitForDraw ensures widget operations are complete (GTK test pattern) | |
| 767 | + | // This replaces the problematic IdleAdd pattern with proper widget synchronization | |
| 768 | + | func WidgetWaitForDraw(widget uintptr) { | |
| 769 | + | if widget == 0 { | |
| 770 | + | return | |
| 771 | + | } | |
| 772 | + | if !widgetIsValid(widget) { | |
| 773 | + | log.Panicf("gtk_test_widget_wait_for_draw: widget=%#x invalid", widget) | |
| 774 | + | } | |
| 775 | + | gtkTestInitOnce.Do(func() { | |
| 776 | + | C.workspace_gtk_test_init() | |
| 777 | + | }) | |
| 778 | + | C.gtk_test_widget_wait_for_draw((*C.GtkWidget)(unsafe.Pointer(widget))) | |
| 779 | + | } | |
| @@ -5,6 +5,7 @@ package webkit | |||||
| 5 | /* | 5 | /* | ||
| 6 | #cgo pkg-config: gtk4 | 6 | #cgo pkg-config: gtk4 | ||
| 7 | #include <gtk/gtk.h> | 7 | #include <gtk/gtk.h> | ||
| 8 | + | #include <gtk/gtktestutils.h> | |||
| 8 | #include <gdk/gdk.h> | 9 | #include <gdk/gdk.h> | ||
| 9 | #include <glib.h> | 10 | #include <glib.h> | ||
| 10 | #include <glib-object.h> | 11 | #include <glib-object.h> | ||
| @@ -179,6 +180,7 @@ static void widget_set_visible(GtkWidget* widget, gboolean visible) { | |||||
| 179 | gtk_widget_set_visible(widget, visible); | 180 | gtk_widget_set_visible(widget, visible); | ||
| 180 | } | 181 | } | ||
| 181 | 182 | ||||
| 183 | + | ||||
| 182 | static gboolean widget_get_visible(GtkWidget* widget) { | 184 | static gboolean widget_get_visible(GtkWidget* widget) { | ||
| 183 | if (!widget) return FALSE; | 185 | if (!widget) return FALSE; | ||
| 184 | return gtk_widget_get_visible(widget); | 186 | return gtk_widget_get_visible(widget); | ||
| @@ -194,6 +196,18 @@ static void widget_show(GtkWidget* widget) { | |||||
| 194 | gtk_widget_set_visible(widget, TRUE); | 196 | gtk_widget_set_visible(widget, TRUE); | ||
| 195 | } | 197 | } | ||
| 196 | 198 | ||||
| 199 | + | static void workspace_gtk_test_init(void) { | |||
| 200 | + | // Create dummy argc/argv for GTK test initialization | |||
| 201 | + | // GTK test framework requires valid argc/argv pointers, not NULL | |||
| 202 | + | static int test_argc = 1; | |||
| 203 | + | static char test_prog[] = "dumber-test"; | |||
| 204 | + | static char* test_argv[] = { test_prog, NULL }; | |||
| 205 | + | static char** test_argv_ptr = test_argv; | |||
| 206 | + | ||||
| 207 | + | // Initialize GTK test framework with proper arguments | |||
| 208 | + | gtk_test_init(&test_argc, &test_argv_ptr, NULL); | |||
| 209 | + | } | |||
| 210 | + | ||||
| 197 | // Label helpers for title bars | 211 | // Label helpers for title bars | ||
| 198 | static GtkWidget* label_new(const char* text) { | 212 | static GtkWidget* label_new(const char* text) { | ||
| 199 | return gtk_label_new(text); | 213 | return gtk_label_new(text); | ||
| @@ -213,6 +227,11 @@ static void label_set_ellipsize(GtkWidget* label, int mode) { | |||||
| 213 | if (!label) return; | 227 | if (!label) return; | ||
| 214 | gtk_label_set_ellipsize(GTK_LABEL(label), (PangoEllipsizeMode)mode); | 228 | gtk_label_set_ellipsize(GTK_LABEL(label), (PangoEllipsizeMode)mode); | ||
| 215 | } | 229 | } | ||
| 230 | + | ||||
| 231 | + | static void label_set_max_width_chars(GtkWidget* label, int n_chars) { | |||
| 232 | + | if (!label) return; | |||
| 233 | + | gtk_label_set_max_width_chars(GTK_LABEL(label), n_chars); | |||
| 234 | + | } | |||
| 216 | */ | 235 | */ | ||
| 217 | import "C" | 236 | import "C" | ||
| 218 | 237 | ||||
| @@ -235,6 +254,7 @@ var ( | |||||
| 235 | hoverCallbacks = make(map[uintptr]func()) | 254 | hoverCallbacks = make(map[uintptr]func()) | ||
| 236 | hoverControllers = make(map[uintptr]uintptr) | 255 | hoverControllers = make(map[uintptr]uintptr) | ||
| 237 | nextHoverID uintptr = 1 | 256 | nextHoverID uintptr = 1 | ||
| 257 | + | gtkTestInitOnce sync.Once | |||
| 238 | ) | 258 | ) | ||
| 239 | 259 | ||||
| 240 | func widgetIsValid(widget uintptr) bool { | 260 | func widgetIsValid(widget uintptr) bool { | ||
| @@ -734,3 +754,26 @@ func LabelSetEllipsize(label uintptr, mode EllipsizeMode) { | |||||
| 734 | } | 754 | } | ||
| 735 | C.label_set_ellipsize((*C.GtkWidget)(unsafe.Pointer(label)), C.int(mode)) | 755 | C.label_set_ellipsize((*C.GtkWidget)(unsafe.Pointer(label)), C.int(mode)) | ||
| 736 | } | 756 | } | ||
| 757 | + | ||||
| 758 | + | // LabelSetMaxWidthChars sets the maximum width in characters for a GtkLabel. | |||
| 759 | + | func LabelSetMaxWidthChars(label uintptr, nChars int) { | |||
| 760 | + | if label == 0 { | |||
| 761 | + | return | |||
| 762 | + | } | |||
| 763 | + | C.label_set_max_width_chars((*C.GtkWidget)(unsafe.Pointer(label)), C.int(nChars)) | |||
| 764 | + | } | |||
| 765 | + | ||||
| 766 | + | // WidgetWaitForDraw ensures widget operations are complete (GTK test pattern) | |||
| 767 | + | // This replaces the problematic IdleAdd pattern with proper widget synchronization | |||
| 768 | + | func WidgetWaitForDraw(widget uintptr) { | |||
| 769 | + | if widget == 0 { | |||
| 770 | + | return | |||
| 771 | + | } | |||
| 772 | + | if !widgetIsValid(widget) { | |||
| 773 | + | log.Panicf("gtk_test_widget_wait_for_draw: widget=%#x invalid", widget) | |||
| 774 | + | } | |||
| 775 | + | gtkTestInitOnce.Do(func() { | |||
| 776 | + | C.workspace_gtk_test_init() | |||
| 777 | + | }) | |||
| 778 | + | C.gtk_test_widget_wait_for_draw((*C.GtkWidget)(unsafe.Pointer(widget))) | |||
| 779 | + | } | |||
M pkg/webkit/workspace_widgets_stub.go +244 -14
| @@ -26,6 +26,14 @@ type widgetStub struct { | |||
| 26 | 26 | bounds WidgetBounds | |
| 27 | 27 | hasBounds bool | |
| 28 | 28 | hover map[uintptr]func() | |
| 29 | + | // GTK4 lifecycle simulation | |
| 30 | + | parent uintptr | |
| 31 | + | isDestroyed bool | |
| 32 | + | refCount uint | |
| 33 | + | // GTK focus management simulation | |
| 34 | + | hasFocus bool | |
| 35 | + | canFocus bool | |
| 36 | + | focusedChild uintptr | |
| 29 | 37 | } | |
| 30 | 38 | ||
| 31 | 39 | var ( | |
| @@ -33,6 +41,10 @@ var ( | |||
| 33 | 41 | widgetState = map[uintptr]*widgetStub{} | |
| 34 | 42 | nextWidgetID uintptr = 1 | |
| 35 | 43 | nextHoverToken uintptr = 1 | |
| 44 | + | // GTK focus management simulation | |
| 45 | + | globalFocusedWidget uintptr | |
| 46 | + | focusWarningEnabled bool = true | |
| 47 | + | gtkTestInitOnce sync.Once | |
| 36 | 48 | ) | |
| 37 | 49 | ||
| 38 | 50 | func newWidgetHandle() uintptr { | |
| @@ -40,7 +52,10 @@ func newWidgetHandle() uintptr { | |||
| 40 | 52 | defer widgetMu.Unlock() | |
| 41 | 53 | id := nextWidgetID | |
| 42 | 54 | nextWidgetID++ | |
| 43 | - | widgetState[id] = &widgetStub{} | |
| 55 | + | widgetState[id] = &widgetStub{ | |
| 56 | + | refCount: 1, // GTK widgets start with ref count 1 | |
| 57 | + | canFocus: true, // Most widgets can receive focus | |
| 58 | + | } | |
| 44 | 59 | return id | |
| 45 | 60 | } | |
| 46 | 61 | ||
| @@ -76,16 +91,86 @@ func NewPaned(orientation Orientation) uintptr { | |||
| 76 | 91 | func PanedSetStartChild(paned uintptr, child uintptr) { | |
| 77 | 92 | widgetMu.Lock() | |
| 78 | 93 | defer widgetMu.Unlock() | |
| 79 | - | if stub, ok := widgetState[paned]; ok { | |
| 80 | - | stub.startChild = child | |
| 94 | + | ||
| 95 | + | // GTK4 validation: paned must be a valid widget | |
| 96 | + | if paned == 0 { | |
| 97 | + | panic("GTK-CRITICAL simulation: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed - paned is NULL") | |
| 98 | + | } | |
| 99 | + | ||
| 100 | + | stub, ok := widgetState[paned] | |
| 101 | + | if !ok || stub.isDestroyed { | |
| 102 | + | panic("GTK-CRITICAL simulation: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed - paned is invalid") | |
| 103 | + | } | |
| 104 | + | ||
| 105 | + | // GTK4 validation: if child is not 0, it must be a valid widget | |
| 106 | + | if child != 0 { | |
| 107 | + | childStub, childExists := widgetState[child] | |
| 108 | + | if !childExists || childStub.isDestroyed { | |
| 109 | + | panic("GTK-CRITICAL simulation: gtk_widget_insert_before: assertion 'GTK_IS_WIDGET (widget)' failed - child is invalid") | |
| 110 | + | } | |
| 111 | + | } | |
| 112 | + | ||
| 113 | + | // GTK4 behavior: setting child to 0 automatically unparents the old child | |
| 114 | + | if stub.startChild != 0 && child == 0 { | |
| 115 | + | if oldChild, exists := widgetState[stub.startChild]; exists { | |
| 116 | + | oldChild.parent = 0 // Automatically unparent | |
| 117 | + | } | |
| 118 | + | } | |
| 119 | + | // Set new child and establish parent relationship | |
| 120 | + | stub.startChild = child | |
| 121 | + | if child != 0 { | |
| 122 | + | if childStub, exists := widgetState[child]; exists { | |
| 123 | + | // Simulate the exact GTK focus management issue from production | |
| 124 | + | // When reparenting a widget with focus hierarchy to a new parent, | |
| 125 | + | // GTK's focus management calls gtk_paned_set_focus_child with (nil) | |
| 126 | + | if focusWarningEnabled && (childStub.hasFocus || childStub.focusedChild != 0) { | |
| 127 | + | panic("GTK-WARNING simulation: Error finding last focus widget of GtkPaned, gtk_paned_set_focus_child was called on widget (nil) which is not child") | |
| 128 | + | } | |
| 129 | + | childStub.parent = paned | |
| 130 | + | } | |
| 81 | 131 | } | |
| 82 | 132 | } | |
| 83 | 133 | ||
| 84 | 134 | func PanedSetEndChild(paned uintptr, child uintptr) { | |
| 85 | 135 | widgetMu.Lock() | |
| 86 | 136 | defer widgetMu.Unlock() | |
| 87 | - | if stub, ok := widgetState[paned]; ok { | |
| 88 | - | stub.endChild = child | |
| 137 | + | ||
| 138 | + | // GTK4 validation: paned must be a valid widget | |
| 139 | + | if paned == 0 { | |
| 140 | + | panic("GTK-CRITICAL simulation: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed - paned is NULL") | |
| 141 | + | } | |
| 142 | + | ||
| 143 | + | stub, ok := widgetState[paned] | |
| 144 | + | if !ok || stub.isDestroyed { | |
| 145 | + | panic("GTK-CRITICAL simulation: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed - paned is invalid") | |
| 146 | + | } | |
| 147 | + | ||
| 148 | + | // GTK4 validation: if child is not 0, it must be a valid widget | |
| 149 | + | if child != 0 { | |
| 150 | + | childStub, childExists := widgetState[child] | |
| 151 | + | if !childExists || childStub.isDestroyed { | |
| 152 | + | panic("GTK-CRITICAL simulation: gtk_widget_insert_before: assertion 'GTK_IS_WIDGET (widget)' failed - child is invalid") | |
| 153 | + | } | |
| 154 | + | } | |
| 155 | + | ||
| 156 | + | // GTK4 behavior: setting child to 0 automatically unparents the old child | |
| 157 | + | if stub.endChild != 0 && child == 0 { | |
| 158 | + | if oldChild, exists := widgetState[stub.endChild]; exists { | |
| 159 | + | oldChild.parent = 0 // Automatically unparent | |
| 160 | + | } | |
| 161 | + | } | |
| 162 | + | // Set new child and establish parent relationship | |
| 163 | + | stub.endChild = child | |
| 164 | + | if child != 0 { | |
| 165 | + | if childStub, exists := widgetState[child]; exists { | |
| 166 | + | // Simulate the exact GTK focus management issue from production | |
| 167 | + | // When reparenting a widget with focus hierarchy to a new parent, | |
| 168 | + | // GTK's focus management calls gtk_paned_set_focus_child with (nil) | |
| 169 | + | if focusWarningEnabled && (childStub.hasFocus || childStub.focusedChild != 0) { | |
| 170 | + | panic("GTK-WARNING simulation: Error finding last focus widget of GtkPaned, gtk_paned_set_focus_child was called on widget (nil) which is not child") | |
| 171 | + | } | |
| 172 | + | childStub.parent = paned | |
| 173 | + | } | |
| 89 | 174 | } | |
| 90 | 175 | } | |
| 91 | 176 | ||
| @@ -96,17 +181,82 @@ func PanedSetResizeEnd(paned uintptr, resize bool) {} | |||
| 96 | 181 | // PanedSetPosition is a no-op in stub builds. | |
| 97 | 182 | func PanedSetPosition(paned uintptr, pos int) {} | |
| 98 | 183 | ||
| 99 | - | func WidgetUnparent(widget uintptr) {} | |
| 184 | + | func WidgetUnparent(widget uintptr) { | |
| 185 | + | widgetMu.Lock() | |
| 186 | + | defer widgetMu.Unlock() | |
| 187 | + | if stub, ok := widgetState[widget]; ok { | |
| 188 | + | // GTK4 CRITICAL simulation: unparenting widget with no parent is illegal | |
| 189 | + | if stub.parent == 0 { | |
| 190 | + | // In real GTK4, this would cause: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed | |
| 191 | + | // We'll simulate this by marking the widget as destroyed | |
| 192 | + | stub.isDestroyed = true | |
| 193 | + | panic("GTK-CRITICAL simulation: gtk_widget_unparent called on widget with no parent") | |
| 194 | + | } | |
| 195 | + | // Remove from parent's child list | |
| 196 | + | if parentStub, exists := widgetState[stub.parent]; exists { | |
| 197 | + | if parentStub.startChild == widget { | |
| 198 | + | parentStub.startChild = 0 | |
| 199 | + | } | |
| 200 | + | if parentStub.endChild == widget { | |
| 201 | + | parentStub.endChild = 0 | |
| 202 | + | } | |
| 203 | + | } | |
| 204 | + | stub.parent = 0 | |
| 205 | + | } | |
| 206 | + | } | |
| 100 | 207 | ||
| 101 | 208 | func WidgetSetHExpand(widget uintptr, expand bool) {} | |
| 102 | 209 | ||
| 103 | 210 | func WidgetSetVExpand(widget uintptr, expand bool) {} | |
| 104 | 211 | ||
| 105 | - | func WidgetGetParent(widget uintptr) uintptr { return 0 } | |
| 212 | + | func WidgetGetParent(widget uintptr) uintptr { | |
| 213 | + | widgetMu.Lock() | |
| 214 | + | defer widgetMu.Unlock() | |
| 215 | + | if stub, ok := widgetState[widget]; ok && !stub.isDestroyed { | |
| 216 | + | return stub.parent | |
| 217 | + | } | |
| 218 | + | return 0 | |
| 219 | + | } | |
| 106 | 220 | ||
| 107 | 221 | func WidgetShow(widget uintptr) {} | |
| 108 | 222 | ||
| 109 | - | func WidgetGrabFocus(widget uintptr) {} | |
| 223 | + | func WidgetGrabFocus(widget uintptr) { | |
| 224 | + | widgetMu.Lock() | |
| 225 | + | defer widgetMu.Unlock() | |
| 226 | + | ||
| 227 | + | if widget == 0 { | |
| 228 | + | return | |
| 229 | + | } | |
| 230 | + | ||
| 231 | + | stub, ok := widgetState[widget] | |
| 232 | + | if !ok || stub.isDestroyed || !stub.canFocus { | |
| 233 | + | return | |
| 234 | + | } | |
| 235 | + | ||
| 236 | + | // Clear previous focus | |
| 237 | + | if globalFocusedWidget != 0 { | |
| 238 | + | if prevStub, exists := widgetState[globalFocusedWidget]; exists { | |
| 239 | + | prevStub.hasFocus = false | |
| 240 | + | } | |
| 241 | + | } | |
| 242 | + | ||
| 243 | + | // Set new focus | |
| 244 | + | stub.hasFocus = true | |
| 245 | + | globalFocusedWidget = widget | |
| 246 | + | ||
| 247 | + | // Simulate focus in parent hierarchy - each parent should point to its direct focused child | |
| 248 | + | currentChild := widget | |
| 249 | + | parent := stub.parent | |
| 250 | + | for parent != 0 { | |
| 251 | + | if parentStub, exists := widgetState[parent]; exists { | |
| 252 | + | parentStub.focusedChild = currentChild // Direct child that contains focus | |
| 253 | + | currentChild = parent // Move up the hierarchy | |
| 254 | + | parent = parentStub.parent | |
| 255 | + | } else { | |
| 256 | + | break | |
| 257 | + | } | |
| 258 | + | } | |
| 259 | + | } | |
| 110 | 260 | ||
| 111 | 261 | func WidgetRef(widget uintptr) bool { | |
| 112 | 262 | widgetMu.Lock() | |
| @@ -126,8 +276,13 @@ func WidgetHookDestroy(widget uintptr) {} | |||
| 126 | 276 | func WidgetIsValid(widget uintptr) bool { | |
| 127 | 277 | widgetMu.Lock() | |
| 128 | 278 | defer widgetMu.Unlock() | |
| 129 | - | _, ok := widgetState[widget] | |
| 130 | - | return ok && widget != 0 | |
| 279 | + | if widget == 0 { | |
| 280 | + | return false | |
| 281 | + | } | |
| 282 | + | if stub, ok := widgetState[widget]; ok { | |
| 283 | + | return !stub.isDestroyed | |
| 284 | + | } | |
| 285 | + | return false | |
| 131 | 286 | } | |
| 132 | 287 | ||
| 133 | 288 | func WidgetRefCount(widget uintptr) uint { | |
| @@ -175,6 +330,25 @@ func WidgetGetBounds(widget uintptr) (WidgetBounds, bool) { | |||
| 175 | 330 | return stub.bounds, true | |
| 176 | 331 | } | |
| 177 | 332 | ||
| 333 | + | // WidgetWaitForDraw ensures widget operations are complete (GTK test pattern) | |
| 334 | + | // This replaces the problematic IdleAdd pattern with proper widget synchronization | |
| 335 | + | func WidgetWaitForDraw(widget uintptr) { | |
| 336 | + | gtkTestInitOnce.Do(func() {}) | |
| 337 | + | widgetMu.Lock() | |
| 338 | + | defer widgetMu.Unlock() | |
| 339 | + | ||
| 340 | + | // Validate widget is still valid after any recent operations | |
| 341 | + | if widget != 0 { | |
| 342 | + | stub, ok := widgetState[widget] | |
| 343 | + | if !ok || stub.isDestroyed { | |
| 344 | + | panic("GTK-CRITICAL simulation: gtk_test_widget_wait_for_draw: widget is invalid") | |
| 345 | + | } | |
| 346 | + | } | |
| 347 | + | // In real GTK, this would wait for all pending draw operations to complete | |
| 348 | + | // In our stub, we just validate the widget state | |
| 349 | + | } | |
| 350 | + | ||
| 351 | + | // IdleAdd simulates GTK's idle callback system - DEPRECATED, use WidgetWaitForDraw instead | |
| 178 | 352 | func IdleAdd(fn func() bool) { | |
| 179 | 353 | if fn != nil { | |
| 180 | 354 | fn() | |
| @@ -187,19 +361,68 @@ func WidgetAddCSSClass(widget uintptr, class string) {} | |||
| 187 | 361 | ||
| 188 | 362 | func WidgetRemoveCSSClass(widget uintptr, class string) {} | |
| 189 | 363 | ||
| 364 | + | // SimulateFocusForTesting gives focus to a widget for test scenarios | |
| 365 | + | func SimulateFocusForTesting(widget uintptr) { | |
| 366 | + | WidgetGrabFocus(widget) | |
| 367 | + | } | |
| 368 | + | ||
| 190 | 369 | // GtkBox functions for stacked panes (stub implementations) | |
| 191 | 370 | ||
| 192 | 371 | func NewBox(orientation Orientation, spacing int) uintptr { | |
| 193 | 372 | return newWidgetHandle() | |
| 194 | 373 | } | |
| 195 | 374 | ||
| 196 | - | func BoxAppend(box uintptr, child uintptr) {} | |
| 375 | + | func BoxAppend(box uintptr, child uintptr) { | |
| 376 | + | widgetMu.Lock() | |
| 377 | + | defer widgetMu.Unlock() | |
| 197 | 378 | ||
| 198 | - | func BoxPrepend(box uintptr, child uintptr) {} | |
| 379 | + | if box == 0 || child == 0 { | |
| 380 | + | return | |
| 381 | + | } | |
| 199 | 382 | ||
| 200 | - | func BoxRemove(box uintptr, child uintptr) {} | |
| 383 | + | boxStub, ok := widgetState[box] | |
| 384 | + | if !ok || boxStub.isDestroyed { | |
| 385 | + | panic("GTK-CRITICAL simulation: gtk_box_append: box is invalid") | |
| 386 | + | } | |
| 201 | 387 | ||
| 202 | - | func BoxInsertChildAfter(box uintptr, child uintptr, sibling uintptr) {} | |
| 388 | + | childStub, ok := widgetState[child] | |
| 389 | + | if !ok || childStub.isDestroyed { | |
| 390 | + | panic("GTK-CRITICAL simulation: gtk_box_append: child is invalid") | |
| 391 | + | } | |
| 392 | + | ||
| 393 | + | if childStub.parent != 0 && childStub.parent != box { | |
| 394 | + | panic("GTK-CRITICAL simulation: gtk_box_append: child already has a different parent") | |
| 395 | + | } | |
| 396 | + | childStub.parent = box | |
| 397 | + | } | |
| 398 | + | ||
| 399 | + | func BoxPrepend(box uintptr, child uintptr) { | |
| 400 | + | BoxAppend(box, child) | |
| 401 | + | } | |
| 402 | + | ||
| 403 | + | func BoxRemove(box uintptr, child uintptr) { | |
| 404 | + | widgetMu.Lock() | |
| 405 | + | defer widgetMu.Unlock() | |
| 406 | + | ||
| 407 | + | if box == 0 || child == 0 { | |
| 408 | + | return | |
| 409 | + | } | |
| 410 | + | ||
| 411 | + | childStub, ok := widgetState[child] | |
| 412 | + | if !ok || childStub.isDestroyed { | |
| 413 | + | panic("GTK-CRITICAL simulation: gtk_box_remove: child is invalid") | |
| 414 | + | } | |
| 415 | + | ||
| 416 | + | if childStub.parent != box { | |
| 417 | + | panic("GTK-CRITICAL simulation: gtk_box_remove: child is not parented to the specified box") | |
| 418 | + | } | |
| 419 | + | ||
| 420 | + | childStub.parent = 0 | |
| 421 | + | } | |
| 422 | + | ||
| 423 | + | func BoxInsertChildAfter(box uintptr, child uintptr, sibling uintptr) { | |
| 424 | + | BoxAppend(box, child) | |
| 425 | + | } | |
| 203 | 426 | ||
| 204 | 427 | // Widget visibility functions for stacked panes (stub implementations) | |
| 205 | 428 | ||
| @@ -230,3 +453,10 @@ const ( | |||
| 230 | 453 | ) | |
| 231 | 454 | ||
| 232 | 455 | func LabelSetEllipsize(label uintptr, mode EllipsizeMode) {} | |
| 456 | + | ||
| 457 | + | func LabelSetMaxWidthChars(label uintptr, nChars int) {} | |
| 458 | + | ||
| 459 | + | // SetTestContainer is a testing helper to set the container field for WebView in stub builds | |
| 460 | + | func (w *WebView) SetTestContainer(container uintptr) { | |
| 461 | + | w.container = container | |
| 462 | + | } | |
| @@ -26,6 +26,14 @@ type widgetStub struct { | |||||
| 26 | bounds WidgetBounds | 26 | bounds WidgetBounds | ||
| 27 | hasBounds bool | 27 | hasBounds bool | ||
| 28 | hover map[uintptr]func() | 28 | hover map[uintptr]func() | ||
| 29 | + | // GTK4 lifecycle simulation | |||
| 30 | + | parent uintptr | |||
| 31 | + | isDestroyed bool | |||
| 32 | + | refCount uint | |||
| 33 | + | // GTK focus management simulation | |||
| 34 | + | hasFocus bool | |||
| 35 | + | canFocus bool | |||
| 36 | + | focusedChild uintptr | |||
| 29 | } | 37 | } | ||
| 30 | 38 | ||||
| 31 | var ( | 39 | var ( | ||
| @@ -33,6 +41,10 @@ var ( | |||||
| 33 | widgetState = map[uintptr]*widgetStub{} | 41 | widgetState = map[uintptr]*widgetStub{} | ||
| 34 | nextWidgetID uintptr = 1 | 42 | nextWidgetID uintptr = 1 | ||
| 35 | nextHoverToken uintptr = 1 | 43 | nextHoverToken uintptr = 1 | ||
| 44 | + | // GTK focus management simulation | |||
| 45 | + | globalFocusedWidget uintptr | |||
| 46 | + | focusWarningEnabled bool = true | |||
| 47 | + | gtkTestInitOnce sync.Once | |||
| 36 | ) | 48 | ) | ||
| 37 | 49 | ||||
| 38 | func newWidgetHandle() uintptr { | 50 | func newWidgetHandle() uintptr { | ||
| @@ -40,7 +52,10 @@ func newWidgetHandle() uintptr { | |||||
| 40 | defer widgetMu.Unlock() | 52 | defer widgetMu.Unlock() | ||
| 41 | id := nextWidgetID | 53 | id := nextWidgetID | ||
| 42 | nextWidgetID++ | 54 | nextWidgetID++ | ||
| 43 | - | widgetState[id] = &widgetStub{} | 55 | + | widgetState[id] = &widgetStub{ |
| 56 | + | refCount: 1, // GTK widgets start with ref count 1 | |||
| 57 | + | canFocus: true, // Most widgets can receive focus | |||
| 58 | + | } | |||
| 44 | return id | 59 | return id | ||
| 45 | } | 60 | } | ||
| 46 | 61 | ||||
| @@ -76,16 +91,86 @@ func NewPaned(orientation Orientation) uintptr { | |||||
| 76 | func PanedSetStartChild(paned uintptr, child uintptr) { | 91 | func PanedSetStartChild(paned uintptr, child uintptr) { | ||
| 77 | widgetMu.Lock() | 92 | widgetMu.Lock() | ||
| 78 | defer widgetMu.Unlock() | 93 | defer widgetMu.Unlock() | ||
| 79 | - | if stub, ok := widgetState[paned]; ok { | 94 | + | |
| 80 | - | stub.startChild = child | 95 | + | // GTK4 validation: paned must be a valid widget |
| 96 | + | if paned == 0 { | |||
| 97 | + | panic("GTK-CRITICAL simulation: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed - paned is NULL") | |||
| 98 | + | } | |||
| 99 | + | ||||
| 100 | + | stub, ok := widgetState[paned] | |||
| 101 | + | if !ok || stub.isDestroyed { | |||
| 102 | + | panic("GTK-CRITICAL simulation: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed - paned is invalid") | |||
| 103 | + | } | |||
| 104 | + | ||||
| 105 | + | // GTK4 validation: if child is not 0, it must be a valid widget | |||
| 106 | + | if child != 0 { | |||
| 107 | + | childStub, childExists := widgetState[child] | |||
| 108 | + | if !childExists || childStub.isDestroyed { | |||
| 109 | + | panic("GTK-CRITICAL simulation: gtk_widget_insert_before: assertion 'GTK_IS_WIDGET (widget)' failed - child is invalid") | |||
| 110 | + | } | |||
| 111 | + | } | |||
| 112 | + | ||||
| 113 | + | // GTK4 behavior: setting child to 0 automatically unparents the old child | |||
| 114 | + | if stub.startChild != 0 && child == 0 { | |||
| 115 | + | if oldChild, exists := widgetState[stub.startChild]; exists { | |||
| 116 | + | oldChild.parent = 0 // Automatically unparent | |||
| 117 | + | } | |||
| 118 | + | } | |||
| 119 | + | // Set new child and establish parent relationship | |||
| 120 | + | stub.startChild = child | |||
| 121 | + | if child != 0 { | |||
| 122 | + | if childStub, exists := widgetState[child]; exists { | |||
| 123 | + | // Simulate the exact GTK focus management issue from production | |||
| 124 | + | // When reparenting a widget with focus hierarchy to a new parent, | |||
| 125 | + | // GTK's focus management calls gtk_paned_set_focus_child with (nil) | |||
| 126 | + | if focusWarningEnabled && (childStub.hasFocus || childStub.focusedChild != 0) { | |||
| 127 | + | panic("GTK-WARNING simulation: Error finding last focus widget of GtkPaned, gtk_paned_set_focus_child was called on widget (nil) which is not child") | |||
| 128 | + | } | |||
| 129 | + | childStub.parent = paned | |||
| 130 | + | } | |||
| 81 | } | 131 | } | ||
| 82 | } | 132 | } | ||
| 83 | 133 | ||||
| 84 | func PanedSetEndChild(paned uintptr, child uintptr) { | 134 | func PanedSetEndChild(paned uintptr, child uintptr) { | ||
| 85 | widgetMu.Lock() | 135 | widgetMu.Lock() | ||
| 86 | defer widgetMu.Unlock() | 136 | defer widgetMu.Unlock() | ||
| 87 | - | if stub, ok := widgetState[paned]; ok { | 137 | + | |
| 88 | - | stub.endChild = child | 138 | + | // GTK4 validation: paned must be a valid widget |
| 139 | + | if paned == 0 { | |||
| 140 | + | panic("GTK-CRITICAL simulation: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed - paned is NULL") | |||
| 141 | + | } | |||
| 142 | + | ||||
| 143 | + | stub, ok := widgetState[paned] | |||
| 144 | + | if !ok || stub.isDestroyed { | |||
| 145 | + | panic("GTK-CRITICAL simulation: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed - paned is invalid") | |||
| 146 | + | } | |||
| 147 | + | ||||
| 148 | + | // GTK4 validation: if child is not 0, it must be a valid widget | |||
| 149 | + | if child != 0 { | |||
| 150 | + | childStub, childExists := widgetState[child] | |||
| 151 | + | if !childExists || childStub.isDestroyed { | |||
| 152 | + | panic("GTK-CRITICAL simulation: gtk_widget_insert_before: assertion 'GTK_IS_WIDGET (widget)' failed - child is invalid") | |||
| 153 | + | } | |||
| 154 | + | } | |||
| 155 | + | ||||
| 156 | + | // GTK4 behavior: setting child to 0 automatically unparents the old child | |||
| 157 | + | if stub.endChild != 0 && child == 0 { | |||
| 158 | + | if oldChild, exists := widgetState[stub.endChild]; exists { | |||
| 159 | + | oldChild.parent = 0 // Automatically unparent | |||
| 160 | + | } | |||
| 161 | + | } | |||
| 162 | + | // Set new child and establish parent relationship | |||
| 163 | + | stub.endChild = child | |||
| 164 | + | if child != 0 { | |||
| 165 | + | if childStub, exists := widgetState[child]; exists { | |||
| 166 | + | // Simulate the exact GTK focus management issue from production | |||
| 167 | + | // When reparenting a widget with focus hierarchy to a new parent, | |||
| 168 | + | // GTK's focus management calls gtk_paned_set_focus_child with (nil) | |||
| 169 | + | if focusWarningEnabled && (childStub.hasFocus || childStub.focusedChild != 0) { | |||
| 170 | + | panic("GTK-WARNING simulation: Error finding last focus widget of GtkPaned, gtk_paned_set_focus_child was called on widget (nil) which is not child") | |||
| 171 | + | } | |||
| 172 | + | childStub.parent = paned | |||
| 173 | + | } | |||
| 89 | } | 174 | } | ||
| 90 | } | 175 | } | ||
| 91 | 176 | ||||
| @@ -96,17 +181,82 @@ func PanedSetResizeEnd(paned uintptr, resize bool) {} | |||||
| 96 | // PanedSetPosition is a no-op in stub builds. | 181 | // PanedSetPosition is a no-op in stub builds. | ||
| 97 | func PanedSetPosition(paned uintptr, pos int) {} | 182 | func PanedSetPosition(paned uintptr, pos int) {} | ||
| 98 | 183 | ||||
| 99 | - | func WidgetUnparent(widget uintptr) {} | 184 | + | func WidgetUnparent(widget uintptr) { |
| 185 | + | widgetMu.Lock() | |||
| 186 | + | defer widgetMu.Unlock() | |||
| 187 | + | if stub, ok := widgetState[widget]; ok { | |||
| 188 | + | // GTK4 CRITICAL simulation: unparenting widget with no parent is illegal | |||
| 189 | + | if stub.parent == 0 { | |||
| 190 | + | // In real GTK4, this would cause: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed | |||
| 191 | + | // We'll simulate this by marking the widget as destroyed | |||
| 192 | + | stub.isDestroyed = true | |||
| 193 | + | panic("GTK-CRITICAL simulation: gtk_widget_unparent called on widget with no parent") | |||
| 194 | + | } | |||
| 195 | + | // Remove from parent's child list | |||
| 196 | + | if parentStub, exists := widgetState[stub.parent]; exists { | |||
| 197 | + | if parentStub.startChild == widget { | |||
| 198 | + | parentStub.startChild = 0 | |||
| 199 | + | } | |||
| 200 | + | if parentStub.endChild == widget { | |||
| 201 | + | parentStub.endChild = 0 | |||
| 202 | + | } | |||
| 203 | + | } | |||
| 204 | + | stub.parent = 0 | |||
| 205 | + | } | |||
| 206 | + | } | |||
| 100 | 207 | ||||
| 101 | func WidgetSetHExpand(widget uintptr, expand bool) {} | 208 | func WidgetSetHExpand(widget uintptr, expand bool) {} | ||
| 102 | 209 | ||||
| 103 | func WidgetSetVExpand(widget uintptr, expand bool) {} | 210 | func WidgetSetVExpand(widget uintptr, expand bool) {} | ||
| 104 | 211 | ||||
| 105 | - | func WidgetGetParent(widget uintptr) uintptr { return 0 } | 212 | + | func WidgetGetParent(widget uintptr) uintptr { |
| 213 | + | widgetMu.Lock() | |||
| 214 | + | defer widgetMu.Unlock() | |||
| 215 | + | if stub, ok := widgetState[widget]; ok && !stub.isDestroyed { | |||
| 216 | + | return stub.parent | |||
| 217 | + | } | |||
| 218 | + | return 0 | |||
| 219 | + | } | |||
| 106 | 220 | ||||
| 107 | func WidgetShow(widget uintptr) {} | 221 | func WidgetShow(widget uintptr) {} | ||
| 108 | 222 | ||||
| 109 | - | func WidgetGrabFocus(widget uintptr) {} | 223 | + | func WidgetGrabFocus(widget uintptr) { |
| 224 | + | widgetMu.Lock() | |||
| 225 | + | defer widgetMu.Unlock() | |||
| 226 | + | ||||
| 227 | + | if widget == 0 { | |||
| 228 | + | return | |||
| 229 | + | } | |||
| 230 | + | ||||
| 231 | + | stub, ok := widgetState[widget] | |||
| 232 | + | if !ok || stub.isDestroyed || !stub.canFocus { | |||
| 233 | + | return | |||
| 234 | + | } | |||
| 235 | + | ||||
| 236 | + | // Clear previous focus | |||
| 237 | + | if globalFocusedWidget != 0 { | |||
| 238 | + | if prevStub, exists := widgetState[globalFocusedWidget]; exists { | |||
| 239 | + | prevStub.hasFocus = false | |||
| 240 | + | } | |||
| 241 | + | } | |||
| 242 | + | ||||
| 243 | + | // Set new focus | |||
| 244 | + | stub.hasFocus = true | |||
| 245 | + | globalFocusedWidget = widget | |||
| 246 | + | ||||
| 247 | + | // Simulate focus in parent hierarchy - each parent should point to its direct focused child | |||
| 248 | + | currentChild := widget | |||
| 249 | + | parent := stub.parent | |||
| 250 | + | for parent != 0 { | |||
| 251 | + | if parentStub, exists := widgetState[parent]; exists { | |||
| 252 | + | parentStub.focusedChild = currentChild // Direct child that contains focus | |||
| 253 | + | currentChild = parent // Move up the hierarchy | |||
| 254 | + | parent = parentStub.parent | |||
| 255 | + | } else { | |||
| 256 | + | break | |||
| 257 | + | } | |||
| 258 | + | } | |||
| 259 | + | } | |||
| 110 | 260 | ||||
| 111 | func WidgetRef(widget uintptr) bool { | 261 | func WidgetRef(widget uintptr) bool { | ||
| 112 | widgetMu.Lock() | 262 | widgetMu.Lock() | ||
| @@ -126,8 +276,13 @@ func WidgetHookDestroy(widget uintptr) {} | |||||
| 126 | func WidgetIsValid(widget uintptr) bool { | 276 | func WidgetIsValid(widget uintptr) bool { | ||
| 127 | widgetMu.Lock() | 277 | widgetMu.Lock() | ||
| 128 | defer widgetMu.Unlock() | 278 | defer widgetMu.Unlock() | ||
| 129 | - | _, ok := widgetState[widget] | 279 | + | if widget == 0 { |
| 130 | - | return ok && widget != 0 | 280 | + | return false |
| 281 | + | } | |||
| 282 | + | if stub, ok := widgetState[widget]; ok { | |||
| 283 | + | return !stub.isDestroyed | |||
| 284 | + | } | |||
| 285 | + | return false | |||
| 131 | } | 286 | } | ||
| 132 | 287 | ||||
| 133 | func WidgetRefCount(widget uintptr) uint { | 288 | func WidgetRefCount(widget uintptr) uint { | ||
| @@ -175,6 +330,25 @@ func WidgetGetBounds(widget uintptr) (WidgetBounds, bool) { | |||||
| 175 | return stub.bounds, true | 330 | return stub.bounds, true | ||
| 176 | } | 331 | } | ||
| 177 | 332 | ||||
| 333 | + | // WidgetWaitForDraw ensures widget operations are complete (GTK test pattern) | |||
| 334 | + | // This replaces the problematic IdleAdd pattern with proper widget synchronization | |||
| 335 | + | func WidgetWaitForDraw(widget uintptr) { | |||
| 336 | + | gtkTestInitOnce.Do(func() {}) | |||
| 337 | + | widgetMu.Lock() | |||
| 338 | + | defer widgetMu.Unlock() | |||
| 339 | + | ||||
| 340 | + | // Validate widget is still valid after any recent operations | |||
| 341 | + | if widget != 0 { | |||
| 342 | + | stub, ok := widgetState[widget] | |||
| 343 | + | if !ok || stub.isDestroyed { | |||
| 344 | + | panic("GTK-CRITICAL simulation: gtk_test_widget_wait_for_draw: widget is invalid") | |||
| 345 | + | } | |||
| 346 | + | } | |||
| 347 | + | // In real GTK, this would wait for all pending draw operations to complete | |||
| 348 | + | // In our stub, we just validate the widget state | |||
| 349 | + | } | |||
| 350 | + | ||||
| 351 | + | // IdleAdd simulates GTK's idle callback system - DEPRECATED, use WidgetWaitForDraw instead | |||
| 178 | func IdleAdd(fn func() bool) { | 352 | func IdleAdd(fn func() bool) { | ||
| 179 | if fn != nil { | 353 | if fn != nil { | ||
| 180 | fn() | 354 | fn() | ||
| @@ -187,19 +361,68 @@ func WidgetAddCSSClass(widget uintptr, class string) {} | |||||
| 187 | 361 | ||||
| 188 | func WidgetRemoveCSSClass(widget uintptr, class string) {} | 362 | func WidgetRemoveCSSClass(widget uintptr, class string) {} | ||
| 189 | 363 | ||||
| 364 | + | // SimulateFocusForTesting gives focus to a widget for test scenarios | |||
| 365 | + | func SimulateFocusForTesting(widget uintptr) { | |||
| 366 | + | WidgetGrabFocus(widget) | |||
| 367 | + | } | |||
| 368 | + | ||||
| 190 | // GtkBox functions for stacked panes (stub implementations) | 369 | // GtkBox functions for stacked panes (stub implementations) | ||
| 191 | 370 | ||||
| 192 | func NewBox(orientation Orientation, spacing int) uintptr { | 371 | func NewBox(orientation Orientation, spacing int) uintptr { | ||
| 193 | return newWidgetHandle() | 372 | return newWidgetHandle() | ||
| 194 | } | 373 | } | ||
| 195 | 374 | ||||
| 196 | - | func BoxAppend(box uintptr, child uintptr) {} | 375 | + | func BoxAppend(box uintptr, child uintptr) { |
| 376 | + | widgetMu.Lock() | |||
| 377 | + | defer widgetMu.Unlock() | |||
| 197 | 378 | ||||
| 198 | - | func BoxPrepend(box uintptr, child uintptr) {} | 379 | + | if box == 0 || child == 0 { |
| 380 | + | return | |||
| 381 | + | } | |||
| 199 | 382 | ||||
| 200 | - | func BoxRemove(box uintptr, child uintptr) {} | 383 | + | boxStub, ok := widgetState[box] |
| 384 | + | if !ok || boxStub.isDestroyed { | |||
| 385 | + | panic("GTK-CRITICAL simulation: gtk_box_append: box is invalid") | |||
| 386 | + | } | |||
| 201 | 387 | ||||
| 202 | - | func BoxInsertChildAfter(box uintptr, child uintptr, sibling uintptr) {} | 388 | + | childStub, ok := widgetState[child] |
| 389 | + | if !ok || childStub.isDestroyed { | |||
| 390 | + | panic("GTK-CRITICAL simulation: gtk_box_append: child is invalid") | |||
| 391 | + | } | |||
| 392 | + | ||||
| 393 | + | if childStub.parent != 0 && childStub.parent != box { | |||
| 394 | + | panic("GTK-CRITICAL simulation: gtk_box_append: child already has a different parent") | |||
| 395 | + | } | |||
| 396 | + | childStub.parent = box | |||
| 397 | + | } | |||
| 398 | + | ||||
| 399 | + | func BoxPrepend(box uintptr, child uintptr) { | |||
| 400 | + | BoxAppend(box, child) | |||
| 401 | + | } | |||
| 402 | + | ||||
| 403 | + | func BoxRemove(box uintptr, child uintptr) { | |||
| 404 | + | widgetMu.Lock() | |||
| 405 | + | defer widgetMu.Unlock() | |||
| 406 | + | ||||
| 407 | + | if box == 0 || child == 0 { | |||
| 408 | + | return | |||
| 409 | + | } | |||
| 410 | + | ||||
| 411 | + | childStub, ok := widgetState[child] | |||
| 412 | + | if !ok || childStub.isDestroyed { | |||
| 413 | + | panic("GTK-CRITICAL simulation: gtk_box_remove: child is invalid") | |||
| 414 | + | } | |||
| 415 | + | ||||
| 416 | + | if childStub.parent != box { | |||
| 417 | + | panic("GTK-CRITICAL simulation: gtk_box_remove: child is not parented to the specified box") | |||
| 418 | + | } | |||
| 419 | + | ||||
| 420 | + | childStub.parent = 0 | |||
| 421 | + | } | |||
| 422 | + | ||||
| 423 | + | func BoxInsertChildAfter(box uintptr, child uintptr, sibling uintptr) { | |||
| 424 | + | BoxAppend(box, child) | |||
| 425 | + | } | |||
| 203 | 426 | ||||
| 204 | // Widget visibility functions for stacked panes (stub implementations) | 427 | // Widget visibility functions for stacked panes (stub implementations) | ||
| 205 | 428 | ||||
| @@ -230,3 +453,10 @@ const ( | |||||
| 230 | ) | 453 | ) | ||
| 231 | 454 | ||||
| 232 | func LabelSetEllipsize(label uintptr, mode EllipsizeMode) {} | 455 | func LabelSetEllipsize(label uintptr, mode EllipsizeMode) {} | ||
| 456 | + | ||||
| 457 | + | func LabelSetMaxWidthChars(label uintptr, nChars int) {} | |||
| 458 | + | ||||
| 459 | + | // SetTestContainer is a testing helper to set the container field for WebView in stub builds | |||
| 460 | + | func (w *WebView) SetTestContainer(container uintptr) { | |||
| 461 | + | w.container = container | |||
| 462 | + | } | |||