mouse

package
v1.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mouse provides mouse interaction infrastructure with hit region testing, click/double-click detection, drag tracking, and scroll event handling for TUI components.

Index

Constants

View Source
const WheelScrollLines = 3

WheelScrollLines is how many lines one physical wheel notch scrolls. Vertical wheel actions report Delta in lines, not notches, so a consumer that needs the notch count — anything translating back into wheel events, such as forwarding to a terminal application — has to divide by this.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionType

type ActionType int

ActionType represents the type of mouse action detected.

const (
	ActionNone ActionType = iota
	ActionClick
	ActionDoubleClick
	ActionTripleClick
	ActionScrollUp
	ActionScrollDown
	ActionScrollLeft  // Shift+scroll up = scroll left
	ActionScrollRight // Shift+scroll down = scroll right
	ActionDrag
	ActionDragEnd
	ActionHover
)

type ClickResult

type ClickResult struct {
	Region        *Region
	IsDoubleClick bool
	IsTripleClick bool
	ClickCount    int
}

ClickResult represents the result of processing a click event.

type Handler

type Handler struct {
	HitMap *HitMap
	// contains filtered or unexported fields
}

Handler combines a HitMap with mouse state tracking for drag and double-click detection.

func NewHandler

func NewHandler() *Handler

NewHandler creates a new mouse handler.

func (*Handler) Clear

func (h *Handler) Clear()

Clear resets the handler state and clears the hit map.

func (*Handler) DragDelta

func (h *Handler) DragDelta(x, y int) (dx, dy int)

DragDelta returns the X and Y movement since drag started.

func (*Handler) DragRegion

func (h *Handler) DragRegion() string

DragRegion returns the ID of the region the in-progress drag STARTED in (the drag source), not the region currently under the cursor. It is empty once EndDrag has run; consumers that need the source at drag-end time should read MouseAction.DragStartID, which survives the reset.

func (*Handler) DragStartValue

func (h *Handler) DragStartValue() int

DragStartValue returns the initial value when the drag started.

func (*Handler) EndDrag

func (h *Handler) EndDrag()

EndDrag stops tracking the drag operation.

func (*Handler) HandleClick

func (h *Handler) HandleClick(x, y int) ClickResult

HandleClick processes a mouse click and returns the hit region. Tracks click timing for double-click detection.

func (*Handler) HandleMouse

func (h *Handler) HandleMouse(msg tea.MouseMsg) MouseAction

HandleMouse is a convenience method for processing tea.MouseMsg events. Returns the action to take based on the mouse event.

func (*Handler) IsDragging

func (h *Handler) IsDragging() bool

IsDragging returns true if a drag operation is in progress.

func (*Handler) StartDrag

func (h *Handler) StartDrag(x, y int, regionID string, startValue int)

StartDrag begins tracking a drag operation.

type HitMap

type HitMap struct {
	// contains filtered or unexported fields
}

HitMap tracks hit regions for mouse click detection.

func NewHitMap

func NewHitMap() *HitMap

NewHitMap creates a new empty HitMap.

func (*HitMap) Add

func (h *HitMap) Add(id string, rect Rect, data any)

Add adds a new region to the hit map.

func (*HitMap) AddRect

func (h *HitMap) AddRect(id string, x, y, w, height int, data any)

AddRect adds a region using individual coordinates.

func (*HitMap) Clear

func (h *HitMap) Clear()

Clear removes all regions from the hit map.

func (*HitMap) Regions

func (h *HitMap) Regions() []Region

Regions returns a copy of all registered regions (for testing).

func (*HitMap) Test

func (h *HitMap) Test(x, y int) *Region

Test returns the first region containing the point, or nil if none.

type MouseAction

type MouseAction struct {
	Type   ActionType
	Region *Region
	X, Y   int
	// Delta is the scroll amount in lines for vertical wheel actions and in
	// columns for horizontal ones. See WheelScrollLines to recover notches.
	Delta  int
	DragDX int // Drag delta X
	DragDY int // Drag delta Y
	// DragStartID is the ID of the region the drag started in, for
	// ActionDrag/ActionDragEnd. Region, for those two actions, is the region
	// currently under the cursor (the drop target), which is usually a
	// different region entirely.
	DragStartID string
	Shift       bool
	Alt         bool
}

MouseAction represents a processed mouse event.

type Rect

type Rect struct {
	X, Y, W, H int
}

Rect represents a rectangular region.

func (Rect) Contains

func (r Rect) Contains(x, y int) bool

Contains returns true if the point (x, y) is within the rectangle.

type Region

type Region struct {
	ID   string
	Rect Rect
	Data any
}

Region is a named rectangular hit region with associated data.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL