-
-
Notifications
You must be signed in to change notification settings - Fork 325
Add mouse support: resize preview, click tabs & rows #722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hjanuschka
wants to merge
9
commits into
dlvhdr:main
Choose a base branch
from
hjanuschka:feature/mouse-resize-and-tab-click
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+996
−74
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5af1b12
feat: add mouse support for preview pane resize and tab clicking
hjanuschka 656851b
feat: add scroll wheel support for preview pane and list sections
hjanuschka 699dc3f
feat: add clickable rows and persist preview width
hjanuschka dc8651f
up
hjanuschka dcabc21
Fix mouse row selection lag
hjanuschka b1baabd
Fix mouse interaction artifacts and scroll speed
hjanuschka 647abc7
Refine mouse resize and scrolling
hjanuschka 749dd38
trigger CI
hjanuschka 54c9f5b
Fix scroll behavior: remove unused lastScrollTime and double NextRow …
hjanuschka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,3 +44,4 @@ jobs: | |
| env: | ||
| PR_URL: ${{github.event.pull_request.html_url}} | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| const StateFileName = "state.yml" | ||
| const DEFAULT_XDG_STATE_DIRNAME = ".local/state" | ||
|
|
||
| // State holds runtime state that should persist between sessions | ||
| type State struct { | ||
| PreviewWidth int `yaml:"previewWidth,omitempty"` | ||
| } | ||
|
|
||
| // GetStatePath returns the path to the state file | ||
| // State files are stored in $XDG_STATE_HOME (defaults to ~/.local/state) | ||
| func GetStatePath() (string, error) { | ||
| stateDir := os.Getenv("XDG_STATE_HOME") | ||
| if stateDir == "" { | ||
| homeDir, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| stateDir = filepath.Join(homeDir, DEFAULT_XDG_STATE_DIRNAME) | ||
| } | ||
| return filepath.Join(stateDir, DashDir, StateFileName), nil | ||
| } | ||
|
|
||
| // LoadState loads the state from the state file | ||
| func LoadState() (State, error) { | ||
| state := State{} | ||
|
|
||
| statePath, err := GetStatePath() | ||
| if err != nil { | ||
| return state, err | ||
| } | ||
|
|
||
| data, err := os.ReadFile(statePath) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| return state, nil // No state file yet, return empty state | ||
| } | ||
| return state, err | ||
| } | ||
|
|
||
| err = yaml.Unmarshal(data, &state) | ||
| return state, err | ||
| } | ||
|
|
||
| // SaveState saves the state to the state file | ||
| func SaveState(state State) error { | ||
| statePath, err := GetStatePath() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Ensure directory exists | ||
| dir := filepath.Dir(statePath) | ||
| if err := os.MkdirAll(dir, 0755); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| data, err := yaml.Marshal(state) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return os.WriteFile(statePath, data, 0644) | ||
| } | ||
|
|
||
| // SavePreviewWidth saves just the preview width to state | ||
| func SavePreviewWidth(width int) error { | ||
| state, _ := LoadState() // Ignore error, start fresh if needed | ||
| state.PreviewWidth = width | ||
| return SaveState(state) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| package carousel | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| tea "github.com/charmbracelet/bubbletea" | ||
| zone "github.com/lrstanley/bubblezone" | ||
| ) | ||
|
|
||
| func init() { | ||
| zone.NewGlobal() | ||
| } | ||
|
|
||
| func TestCarousel(t *testing.T) { | ||
| t.Run("Should create carousel with items", func(t *testing.T) { | ||
| items := []string{"Tab 1", "Tab 2", "Tab 3"} | ||
| c := New(WithItems(items), WithWidth(100)) | ||
|
|
||
| if len(c.Items()) != len(items) { | ||
| t.Errorf("Expected %d items, got %d", len(items), len(c.Items())) | ||
| } | ||
|
|
||
| if c.Cursor() != 0 { | ||
| t.Errorf("Expected cursor at 0, got %d", c.Cursor()) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("Should set cursor position", func(t *testing.T) { | ||
| items := []string{"Tab 1", "Tab 2", "Tab 3"} | ||
| c := New(WithItems(items), WithWidth(100)) | ||
|
|
||
| c.SetCursor(2) | ||
| if c.Cursor() != 2 { | ||
| t.Errorf("Expected cursor at 2, got %d", c.Cursor()) | ||
| } | ||
|
|
||
| // Should clamp to valid range | ||
| c.SetCursor(10) | ||
| if c.Cursor() != 2 { | ||
| t.Errorf("Expected cursor clamped to 2, got %d", c.Cursor()) | ||
| } | ||
|
|
||
| c.SetCursor(-1) | ||
| if c.Cursor() != 0 { | ||
| t.Errorf("Expected cursor clamped to 0, got %d", c.Cursor()) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("Should move left and right", func(t *testing.T) { | ||
| items := []string{"Tab 1", "Tab 2", "Tab 3"} | ||
| c := New(WithItems(items), WithWidth(100)) | ||
|
|
||
| c.SetCursor(1) | ||
| c.MoveLeft() | ||
| if c.Cursor() != 0 { | ||
| t.Errorf("Expected cursor at 0 after MoveLeft, got %d", c.Cursor()) | ||
| } | ||
|
|
||
| c.MoveRight() | ||
| if c.Cursor() != 1 { | ||
| t.Errorf("Expected cursor at 1 after MoveRight, got %d", c.Cursor()) | ||
| } | ||
|
|
||
| // Should not go below 0 | ||
| c.SetCursor(0) | ||
| c.MoveLeft() | ||
| if c.Cursor() != 0 { | ||
| t.Errorf("Expected cursor to stay at 0, got %d", c.Cursor()) | ||
| } | ||
|
|
||
| // Should not go above max | ||
| c.SetCursor(2) | ||
| c.MoveRight() | ||
| if c.Cursor() != 2 { | ||
| t.Errorf("Expected cursor to stay at 2, got %d", c.Cursor()) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("Should render items with zone markers", func(t *testing.T) { | ||
| items := []string{"Tab 1", "Tab 2", "Tab 3"} | ||
| c := New(WithItems(items), WithWidth(100)) | ||
| c.UpdateSize() | ||
|
|
||
| view := c.View() | ||
| if view == "" { | ||
| t.Error("Expected non-empty view") | ||
| } | ||
|
|
||
| // The view should contain the items (after zone.Scan) | ||
| scanned := zone.Scan(view) | ||
| for _, item := range items { | ||
| if !strings.Contains(scanned, item) { | ||
| t.Errorf("Expected view to contain %q", item) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| t.Run("HandleClick should return index for clicked tab", func(t *testing.T) { | ||
| items := []string{"Tab 1", "Tab 2", "Tab 3"} | ||
| c := New(WithItems(items), WithWidth(200)) | ||
| c.UpdateSize() | ||
|
|
||
| // Render the view to set up zones | ||
| view := c.View() | ||
| _ = zone.Scan(view) | ||
|
|
||
| // Click inside the first tab's zone | ||
| // Zone IDs now include the zonePrefix, so construct it the same way as tabZoneID() | ||
| zoneID := fmt.Sprintf("%s%s%d", TabZonePrefix, c.zonePrefix, 0) | ||
| z := zone.Get(zoneID) | ||
| if z.IsZero() { | ||
| t.Fatal("Expected zone to be registered") | ||
| } | ||
| msg := tea.MouseMsg{ | ||
| X: z.StartX, | ||
| Y: z.StartY, | ||
| Button: tea.MouseButtonLeft, | ||
| Action: tea.MouseActionRelease, | ||
| } | ||
|
|
||
| result := c.HandleClick(msg) | ||
| if result != 0 { | ||
| t.Errorf("Expected tab index 0, got %d", result) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("HandleClick should return -1 for empty items", func(t *testing.T) { | ||
| c := New(WithItems([]string{}), WithWidth(100)) | ||
| c.UpdateSize() | ||
|
|
||
| msg := tea.MouseMsg{ | ||
| X: 1, | ||
| Y: 1, | ||
| Button: tea.MouseButtonLeft, | ||
| Action: tea.MouseActionRelease, | ||
| } | ||
|
|
||
| result := c.HandleClick(msg) | ||
| if result != -1 { | ||
| t.Errorf("Expected -1 for empty items, got %d", result) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("HandleClick should return -1 for click outside tabs", func(t *testing.T) { | ||
| items := []string{"Tab 1", "Tab 2", "Tab 3"} | ||
| c := New(WithItems(items), WithWidth(100)) | ||
| c.UpdateSize() | ||
|
|
||
| // Click way outside any tab area | ||
| msg := tea.MouseMsg{ | ||
| X: 1000, | ||
| Y: 1000, | ||
| Button: tea.MouseButtonLeft, | ||
| Action: tea.MouseActionRelease, | ||
| } | ||
|
|
||
| result := c.HandleClick(msg) | ||
| if result != -1 { | ||
| t.Errorf("Expected -1 for click outside tabs, got %d", result) | ||
| } | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.