fix(editor): wire key events to cursor movement

- Replace gtx.Event(nil) with key.Filter{Focus: focusedID} and
  key.FocusFilter{Target: focusedID} so Gio correctly routes key
  and edit events to the focused editor text field.

- Convert EditorState.CursorPosition (byte offset) to line/column
  coordinates for accurate cursor rendering.

- Add debug logging in HandleKeyDown and HandleCursorMove.
This commit is contained in:
gmp
2026-06-03 07:41:18 -04:00
parent a17c3760f2
commit 0dac354f2a
13 changed files with 467 additions and 77 deletions
+26 -30
View File
@@ -4,7 +4,7 @@
This plan specifies the implementation of the directory browser for Pad, covering lazy loading, virtualized rendering, alphabetical indexing, and search functionality.
### Current State (as of 2026-05-30)
### Current State (as of 2026-06-02)
| Feature | Status | Location |
|---|---|---|
@@ -13,8 +13,8 @@ This plan specifies the implementation of the directory browser for Pad, coverin
| Search (case-insensitive) | ✅ Implemented | `internal/browser/search.go:HandleSearch()` |
| Sort (4 modes only) | ✅ Implemented | `internal/browser/sort.go` (4 modes, not 6) |
| Worker pool | ✅ Implemented | `internal/io/pool/` |
| Filesystem-backed entries | ❌ Not started | Uses static `browserEntries` slice |
| Lazy loading / pagination | ⏳ Partial | Page model + eviction implemented; disk I/O not wired |
| Mock filesystem-backed entries | ✅ Implemented | `internal/browser/manager.go` |
| Lazy loading / pagination | ✅ Implemented | `internal/browser/manager.go` |
| Directory index / caching | ✅ Implemented | `internal/browser/index.go:buildIndex()` |
| Alphabetical index sidebar | ⏸ Deferred | See §14 |
@@ -632,24 +632,23 @@ The browser scroll position is persisted in the existing `state.json`:
## 10. Implementation Phases
### Phase 1: Core Browser (MVP)
- [ ] `internal/browser/types.go` — Define Entry, Page, BrowserState
- [ ] `internal/browser/index.go` — Directory index build/cache
- [ ] `internal/browser/browser.go` — State management, page loading
- [ ] `internal/browser/layout.go` — BrowserLayout function
- [ ] Wire into main event loop (replace placeholder browser)
- [ ] Basic scroll and tap-to-open
- [x] `internal/browser/types.go` — Define Entry, Page, BrowserState
- [x] `internal/browser/index.go` — Directory index build/cache
- [x] `internal/browser/browser.go` — State management, page loading
- [x] `internal/browser/layout.go` — BrowserLayout function
- [x] Wire into main event loop (replace placeholder browser)
- [x] Basic scroll and tap-to-open
### Phase 2: Lazy Loading (Detailed Plan)
See §15 for the complete lazy loading implementation plan with step-by-step details.
- [ ] Create `BrowserManager` orchestrator (Step 1)
- [ ] Wire initial directory load via `BuildIndexTask` (Step 2)
- [ ] Implement scroll-based prefetching (Step 3)
- [ ] Handle worker results in logic goroutine (Step 4)
- [ ] Implement page eviction (Step 5)
- [ ] Implement dirty page refresh (Step 6)
- [ ] Write E2E tests with mock filesystem (Step 7)
Completed:
- [x] Create `BrowserManager` orchestrator
- [x] Wire initial directory load via `BuildIndexTask`
- [x] Implement scroll-based prefetching
- [x] Handle worker results in logic goroutine
- [x] Implement page eviction
- [ ] Implement dirty page refresh (Pending)
- [x] Write E2E tests with mock filesystem
### Phase 3: Deferred — Alphabetical Index Sidebar
@@ -661,14 +660,15 @@ Deferred items:
- Tap-to-jump functionality
### Phase 4: Search
- [ ] Search bar UI
- [ ] Incremental filtering
- [ ] Search result navigation
- [x] Search bar UI
- [x] Incremental filtering
- [x] Search result navigation
### Phase 5: Polish
- [ ] Scroll momentum (reuse from touch.md §6)
- [ ] External change detection (watcher integration)
- [ ] State persistence and restore
- [ ] Real filesystem backing (transition from mock)
- [ ] Performance testing with large directories
---
@@ -875,18 +875,14 @@ This section contains the detailed step-by-step plan for implementing lazy loadi
| Component | Status | Notes |
|-----------|--------|-------|
| Browser types | ✅ Complete | `BrowserState`, `Page`, `PageSize=100`, `PrefetchDist=2` |
| Page loading logic | ✅ Partial | `loadPageFromIndex()`, `LoadInitialPages()` exist but not wired |
| Worker pool | ✅ Complete | Priority dispatch, `LoadPagesTask` defined |
| Page loading logic | ✅ Complete | `loadPageFromIndex()`, `LoadInitialPages()` wired in `BrowserManager` |
| Worker pool | ✅ Complete | Priority dispatch, `LoadPagesTask` defined and used |
| Mock filesystem | ✅ Complete | Thread-safe, configurable delay |
| Test harness | ✅ Complete | Frame capture, input simulation |
**Gaps to Fill:**
1. **No worker dispatch for page loading**: `loadPageFromIndex()` reads from `SortIndex` but doesn't use the worker pool
2. **No result handling**: Logic goroutine doesn't process `TypeLoadPages` results
3. **No prefetch trigger**: `needsPrefetch()` exists but nothing calls it during scroll
4. **No initial load trigger**: `LoadInitialPages()` exists but isn't called anywhere
5. **No dirty page refresh**: `Page.Dirty` flag exists but no mechanism to refresh
1. **Implement dirty page refresh**: `Page.Dirty` flag exists but no mechanism to refresh.
---
@@ -1002,14 +998,14 @@ In `HandleScroll()` or periodic timer:
### 15.4 Dirty Page Handling
#### Step 6: Implement Dirty Page Refresh
#### Step 6: Implement Dirty Page Refresh (Pending)
When a page is marked dirty:
1. Reload from index
2. Merge into `Pages` map
**Dirty detection:**
- External file changes (via file watcher)
- External file changes (via file watcher - *To be implemented*)
- Manual refresh trigger
- Periodic mtime check
+4
View File
@@ -0,0 +1,4 @@
This file lists descriptions of outstanding bugs. Bugs are separated by empty lines.
On the BrowserPage, when I enter a search that limits the list of files, clicking on a file
delivers teh wrong file to the editor page.
+85
View File
@@ -0,0 +1,85 @@
# Editor Implementation Plan
## 1. Design Principles
- **Single-Owner Pattern**: The logic goroutine is the exclusive authority for modifying the editor buffer, cursor, and selection state.
- **Asynchronous I/O**: File saving and loading operations are delegated to the worker pool. The UI never blocks on disk operations.
- **Buffer-Based Editing**: For performance, the editor will eventually utilize a gap buffer or rope data structure. For MVP, string manipulation is acceptable if performance targets are met.
- **Event-Driven**: Keyboard input, mouse interactions, and menu actions are passed through the existing `inputChan` to the logic goroutine.
## 2. Core State Structure
The `State` struct in `internal/editor/state.go` will be extended to track active editing session data:
```go
type EditorState struct {
Buffer string // Document content (or gap buffer)
CursorPosition int // Byte offset
SelectionStart int // -1 if no selection
SelectionEnd int // -1 if no selection
UndoStack []EditCommand // For undo/redo
Dirty bool // Needs save
}
```
## 3. Implementation Phases
### Phase 1: Basic Buffer Management & Cursor
- [ ] Define `EditorState` (Cursor, Selection, Dirty flag).
- [ ] Implement cursor navigation (Arrow keys: Left, Right, Up, Down).
- [ ] Implement basic buffer updates (Insert character, Delete/Backspace).
- [ ] Implement cursor display and blink animation.
- [ ] Update `EditorLayout` to display cursor and selection highlight.
### Phase 2: File IO Integration
- [ ] Implement `SaveFile` handler: Dispatch `WriteFileTask` to worker pool.
- [ ] Implement `LoadFile` handler: Dispatch `ReadFileTask` to worker pool (existing in `logic.go`).
- [ ] Status bar integration: Show "Saving..." indicator, "Modified" status.
### Phase 3: Text Editing Operations
- [ ] Implement Cut/Copy/Paste interactions.
- [ ] Implement multi-line text navigation (Home/End, PageUp/PageDown).
- [ ] Implement text selection display and mouse interaction.
- [ ] Implement soft-wrap toggle (already exists, need to verify logic).
### Phase 4: Polish & Advanced Features
- [ ] Undo/Redo stack implementation.
- [ ] Performance testing with large files (>1MB).
---
## 4. Interaction Flow (Example: Keyboard/Mouse Input)
1. **User** performs an action (types a character, taps an icon).
2. **UI (Renderer)** registers the element's interaction (`event.Op` for keys, `gesture.Add` for mouse) within its clip context.
3. **Main Loop** captures the event (using `key.Filter` for keys, `gesture.Update` for mouse).
4. **Main Loop** generates an `InputEvent` and sends it to `inputChan`.
5. **Logic Goroutine** receives the event, triggers the corresponding `Handler` in `TheState`.
6. **Logic Goroutine** modifies state, and sends updated elements back through `frameChan`.
7. **Renderer** paints the new frame.
---
## 5. Performance Targets
| Operation | Target | Mechanism |
|---|---|---|
| Typing latency | < 16ms | Single-owner logic update, immediate frame redraw |
| File Load (1MB) | < 100ms | Async worker pool read, background loading |
| File Save | < 100ms | Async worker pool write |
| Undo/Redo | < 10ms | O(1) or O(N) memory-based buffer update |
---
## 7. Input Handling Mechanism
### 7.1 Mouse/Touch Input
- Uses `gesture.Click` and `gesture.Scroll`.
- Registered via `event.Op` and gesture `Add()` within the clip context of the element.
- Processed by `Renderer.CheckGestures` and dispatched via `InputEvent` to the logic goroutine.
### 7.2 Keyboard Input
- **Registration**: Elements register as input handlers using `event.Op(gtx.Ops, elementID)`. This tags the current clip context for input routing.
- **Focus**: Focus is managed by the logic goroutine. When an element is focused, `key.FocusCmd{Tag: elementID}` is submitted to the operation stack.
- **Filtering**: Keyboard events are processed in the main loop using `key.Filter{Focus: focusedElementID}` to ensure events are only routed to the active element.
- **Routing**: `key.Event` (for key presses) and `key.EditEvent` (for text input) are converted into `InputEvent` structs and sent to the logic goroutine via `inputChan` for state updates.