diff --git a/docs/changelog.rst b/docs/changelog.rst index 1ab3673..6fb8c0c 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -263,6 +263,11 @@ Detailed list of changes :kbd:`kitty_mod+H` is pressed. The panel can also be started with :code:`kitty +kitten help`. +- The :doc:`file manager ` can now be browsed with the + mouse: the wheel scrolls the list, a double click opens the entry under the + pointer like :kbd:`Enter` and a right click opens the action palette on the + clicked row. + - The in-place scrollback search got a usable default shortcut: it was bound to :kbd:`kitty_mod+shift+f`, the same physical key as :ac:`move_window_forward` (which allows the shifted fallback), so the last definition won and the search diff --git a/docs/kittens/file-manager.rst b/docs/kittens/file-manager.rst index 03e8184..60e0ad1 100644 --- a/docs/kittens/file-manager.rst +++ b/docs/kittens/file-manager.rst @@ -161,6 +161,16 @@ pas soumise. synchronisation reste inerte. +Souris +------ + +Le panneau se pilote aussi à la souris. La molette parcourt la liste par pas de +trois lignes, sans transmettre l'événement au terminal qui héberge le dock. Un +double-clic ouvre l'entrée sous le pointeur, comme :kbd:`Entrée` : un répertoire +est parcouru, un fichier est confié à l'éditeur. Un clic droit ouvre la palette +d'actions sur la ligne cliquée, identique au raccourci :kbd:`:`. + + .. _file-manager-shortcuts: Raccourcis et configuration diff --git a/kittens/file_manager/main.go b/kittens/file_manager/main.go index e437cfe..76a47b8 100644 --- a/kittens/file_manager/main.go +++ b/kittens/file_manager/main.go @@ -228,6 +228,28 @@ func truncate_to_width(s string, max_width int) string { return string(runes) + "\u2026" } +// wheel_scroll_lines is the number of rows one wheel notch scrolls. It sits +// between a single line, which makes a long directory tedious, and the default +// kitty wheel_scroll_multiplier of five, which skips too much in a panel. +const wheel_scroll_lines = 3 + +// double_click_interval is the largest gap between two clicks on the same row +// that still counts as a double click, matching kitty's click_interval default. +const double_click_interval = 500 * time.Millisecond + +// click_tracker remembers the last click so that a second click on the same row +// opens it, while clicks on different rows stay single selections. +type click_tracker struct { + at time.Time + idx int +} + +func (c *click_tracker) is_double(now time.Time, idx int) bool { + prev_at, prev_idx := c.at, c.idx + c.at, c.idx = now, idx + return idx == prev_idx && !prev_at.IsZero() && now.Sub(prev_at) <= double_click_interval +} + type Handler struct { lp *loop.Loop screen_size loop.ScreenSize @@ -255,6 +277,7 @@ type Handler struct { sync *shell_sync sync_handled bool sync_published bool + clicks click_tracker } func (h *Handler) initialize() (string, error) { @@ -1088,13 +1111,42 @@ func (h *Handler) row_to_entry(cell_y int) int { } func (h *Handler) on_mouse_event(ev *loop.MouseEvent) error { + // The palette owns the pointer while it is open, so clicks and wheel events + // do not change the selection behind it. + if h.palette_open && ev.Event_type != loop.MOUSE_MOVE { + return nil + } switch ev.Event_type { + case loop.MOUSE_PRESS: + // Wheel notches arrive as presses carrying a wheel button. Moving the + // selection keeps the list scrolled to the row under it, without sending + // the event to the terminal that hosts the dock. + switch { + case ev.Buttons&loop.MOUSE_WHEEL_UP != 0: + h.move_selection(-wheel_scroll_lines) + case ev.Buttons&loop.MOUSE_WHEEL_DOWN != 0: + h.move_selection(wheel_scroll_lines) + } case loop.MOUSE_CLICK: - if ev.Buttons&loop.LEFT_MOUSE_BUTTON != 0 { + switch { + case ev.Buttons&loop.LEFT_MOUSE_BUTTON != 0: + idx := h.row_to_entry(ev.Cell.Y) + if idx < 0 { + return nil + } + h.selected = idx + if h.clicks.is_double(time.Now(), idx) { + h.activate() + return nil + } + h.draw_screen() + case ev.Buttons&loop.RIGHT_MOUSE_BUTTON != 0: + // The context menu is the action palette that :: already opens, so + // every panel action is reachable from the pointer too. if idx := h.row_to_entry(ev.Cell.Y); idx >= 0 { h.selected = idx - h.draw_screen() } + h.open_palette() } case loop.MOUSE_MOVE: h.lp.ClearPointerShapes() diff --git a/kittens/file_manager/main_test.go b/kittens/file_manager/main_test.go index 19a4133..8af062b 100644 --- a/kittens/file_manager/main_test.go +++ b/kittens/file_manager/main_test.go @@ -8,6 +8,8 @@ import ( "path/filepath" "testing" "time" + + "github.com/kovidgoyal/kitty/tools/tui/loop" ) func TestSortEntriesDirectoriesFirstThenByName(t *testing.T) { @@ -430,6 +432,47 @@ func TestEditArgsInWindow(t *testing.T) { } } +func TestRowToEntryAccountsForScroll(t *testing.T) { + h := &Handler{ + screen_size: loop.ScreenSize{WidthCells: 40, HeightCells: 10}, + entries: make([]Entry, 12), + scroll: 3, + } + // The list starts on row 2 and the status line takes the last row, so rows + // 2..8 display entries 3..9 + cases := map[int]int{0: -1, 1: 3, 4: 6, 7: 9, 8: -1, 9: -1} + for cell_y, want := range cases { + if got := h.row_to_entry(cell_y); got != want { + t.Fatalf("cell Y %d: wanted entry %d, got %d", cell_y, want, got) + } + } + // A scroll past the end of the list maps to no entry rather than panicking + h.scroll = 20 + if got := h.row_to_entry(1); got != -1 { + t.Fatalf("wanted no entry past the end of the list, got %d", got) + } +} + +func TestClickTrackerDetectsDoubleClick(t *testing.T) { + start := time.Unix(1000, 0) + var c click_tracker + if c.is_double(start, 2) { + t.Fatal("a first click must not be a double click") + } + if !c.is_double(start.Add(200*time.Millisecond), 2) { + t.Fatal("a second click on the same row within the interval is a double click") + } + if c.is_double(start.Add(5*time.Second), 2) { + t.Fatal("a click after the interval must not be a double click") + } + if c.is_double(start.Add(5*time.Second+100*time.Millisecond), 7) { + t.Fatal("a click on another row must not be a double click") + } + if !c.is_double(start.Add(5*time.Second+200*time.Millisecond), 7) { + t.Fatal("the tracker must accept the row clicked last") + } +} + func TestTruncateToWidth(t *testing.T) { if got := truncate_to_width("abcdef", 3); got != "ab\u2026" { t.Fatalf("unexpected truncation %q", got)