on

package
v0.0.0-alpha.13 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 3 Imported by: ? 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 on makes mouse handlers for bloom nodes, attached with their On method:

bloom.Text(name).On(
	on.Click(func() tea.Cmd { m.selected = i; return nil }),
	on.DoubleClick(func() tea.Cmd { return m.open(i) }),
	on.RightClick(on.Msg(menuMsg{i})),
)

Handlers run on the Bubble Tea event loop, as Update does, so they can change the model directly and return a command, which runs in the background. A handler only changes the real model if it reaches it, such as through a pointer receiver; one built in a component kept by value can send a message with Msg instead, for the component's Update to handle. Handlers must not block.

For each kind of event, the innermost node under the pointer with a handler for it takes it, and the nodes it is inside do not, so a row's Click and its pane's Scroll work side by side. Hover is the exception: every node under the pointer is hovered. A handler can also pass an event it has no use for on to the node around it by returning Pass, such as a pane at the end of what it scrolls handing the wheel to the page, as a browser does. A modal layer takes every event from what is beneath it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Msg

func Msg(msg tea.Msg) func() tea.Cmd

Msg returns a handler function that sends msg to Update, for changing state there rather than in the handler:

bloom.Text(name).On(on.Click(on.Msg(selectMsg(i))))

func Pass

func Pass() tea.Msg

Pass is the command a handler returns to pass its event on, as though its node had no handler for it: the nearest node around it with a handler for the event gets it, and so on outward, until one takes it or none is left. It is returned as it is, as tea.Quit is:

on.Scroll(func(_, dy int) tea.Cmd {
	next := min(max(m.cursor+dy, 0), len(m.items)-1)
	if next == m.cursor {
		return on.Pass // the form around the list scrolls instead
	}
	m.cursor = next
	return nil
})

Return it only when the handler changed nothing, since the nodes around it act on the event too. It can't be batched with another command: tea.Batch of it and nil is Pass, but of it and any other command takes the event.

It passes on the events of Click, ClickAt, RightClick, RightClickAt, MiddleClick, DoubleClick, Press, Scroll and Mouse, which go to one node. A Press run by a key leaves the key to the program, as bloom.Focus.Update tells. A left click goes to the next Click, ClickAt or Press handler, whichever it is. Hover, Down and ClickOutside already run for every node they are on, and Drag stays with the node it started on: they take Pass as nil.

Types

type Event

type Event struct {
	// Msg is the Bubble Tea event, in screen coordinates. It is nil when no
	// event caused the handler to run, such as when a hovered node is
	// covered by a dialog, or a key pressed a [Press] handler.
	Msg tea.MouseMsg
	// Screen is where the pointer is on screen.
	Screen image.Point
	// X and Y are where the pointer is, relative to the node's top-left
	// cell, which for a box is the outer corner of its border. During a
	// drag they can be outside the node.
	X, Y int
	// W and H are the node's size.
	W, H int
	// Inside reports whether the pointer is over the node, rather than over
	// something drawn on top of it, or outside it.
	Inside bool
	// Start is where a drag started, on screen.
	Start image.Point
	// Done is set on the last event of a drag: the button came up, or the
	// node left the frame or was covered by a modal layer.
	Done bool
}

Event is a mouse event at a node.

type Handler

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

Handler is a mouse handler for a node, made by one of the functions of this package.

func Click

func Click(fn func() tea.Cmd) Handler

Click runs fn when the left button is pressed and released over the node.

func ClickAt

func ClickAt(fn func(e Event) tea.Cmd) Handler

ClickAt runs fn when the left button is pressed and released over the node, with where it was released.

func ClickOutside

func ClickOutside(fn func() tea.Cmd) Handler

ClickOutside runs fn when a mouse button goes down anywhere but on the node, such as to close a menu. The click still reaches what it is on. On a modal layer, it runs for a click on the backdrop, which the layer still keeps from what is beneath it.

func DoubleClick

func DoubleClick(fn func() tea.Cmd) Handler

DoubleClick runs fn on the second of two left clicks on the node in quick succession. Both clicks still run the node's Click handlers.

func Down

func Down(fn func() tea.Cmd) Handler

Down runs fn when a mouse button goes down on the node, or on a node in it, such as to bring a window to the front. It doesn't wait for the button to come up, as a click does, and takes nothing from the nodes in it: their clicks and drags still run, and so do the Down handlers of every node the pointer is in, innermost first. It is the opposite of ClickOutside.

func Drag

func Drag(fn func(e Event) tea.Cmd) Handler

Drag runs fn when the left button goes down over the node, each time the pointer moves while it is held, wherever the pointer is, and once more with Done set when it comes up. A drag is followed from frame to frame by the node's ID, or without one, by where the node is in the nearest node with an ID around it, or in the tree, so a node that moves within the tree while dragged needs an ID, or to be in a node with one that moves with it.

func Hover

func Hover(fn func(over bool) tea.Cmd) Handler

Hover runs fn with true when the pointer moves onto the node, and false when it leaves, or something drawn on top of the node covers it, which is noticed with the next message after the frame is drawn. The pointer only moves while no button is down with tea.MouseModeAllMotion, which the view from bloomfx.dev/bloom.Root.View asks for while a node has a Hover handler. Terminals do not report the pointer leaving the window, so the hover ends when the window loses focus, which takes tea.View.ReportFocus.

func MiddleClick

func MiddleClick(fn func() tea.Cmd) Handler

MiddleClick runs fn when the middle button is pressed and released over the node.

func Mouse

func Mouse(fn func(e Event) tea.Cmd) Handler

Mouse runs fn for every mouse event over the node: presses, releases, the wheel, and motion. The event is in e.Msg.

func Press

func Press(fn func() tea.Cmd) Handler

Press runs fn when the node is clicked with the left button, as Click does, or when enter or space is pressed while it has the focus, which bloom.Focus.Update handles.

func RightClick

func RightClick(fn func() tea.Cmd) Handler

RightClick runs fn when the right button is pressed and released over the node.

func RightClickAt

func RightClickAt(fn func(e Event) tea.Cmd) Handler

RightClickAt runs fn when the right button is pressed and released over the node, with where it was released, such as to open a menu there.

func Scroll

func Scroll(fn func(dx, dy int) tea.Cmd) Handler

Scroll runs fn when the wheel turns over the node: dy is 1 for each step down and -1 for each step up, and dx is the same for left and right. Each event is one step, on one axis.

A step fn can't use, such as one down with a list's cursor already on its last item, can be passed on with Pass to the node around it, so the wheel over a field that can't scroll that way scrolls the form it is in. The first handler that takes a step takes all of it, even when it moves less than a whole step. Each step is judged on its own, even within a quick turn of the wheel.

func (Handler) Handle

func (h Handler) Handle(e Event) (tea.Cmd, bool)

Handle runs the handler for e, returning its command, and whether it took the event: false when it returned Pass, with no command. bloom calls it; it is exported for routing events some other way, and for tests.

func (Handler) Kind

func (h Handler) Kind() Kind

Kind returns the kind of event the handler takes.

type Kind

type Kind uint8

Kind is the kind of event a Handler takes.

const (
	KindClick Kind = iota + 1 // [Click] and [ClickAt]
	KindPress
	KindRightClick // [RightClick] and [RightClickAt]
	KindMiddleClick
	KindDoubleClick
	KindScroll
	KindHover
	KindDrag
	KindClickOutside
	KindDown
	KindMouse
)

The kinds of handler, one for each function of this package.

Jump to

Keyboard shortcuts

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