Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161package termview
import ( "image"
"gioui.org/io/key" "gioui.org/io/pointer"
"go.mitchellh.com/libghostty"
"100gex/internal/rex")
// reporter passes clicks, drags and motion to a program that reads the// mouse, instead of selecting. A gesture goes to the program if it starts// there without Shift: Shift selects, as in other terminals. Once it does,// its drags and release follow it there whatever is held.type reporter struct { held pointer.Buttons // buttons whose presses went to the program cell image.Point // the cell of the last report sent bool // cell is set}
// mouseTracking reports whether the program reads mouse buttons, and// whether it wants motion with no button held too.func mouseTracking(term *libghostty.Terminal) (buttons, motion bool, err error) { if term == nil { return false, false, nil } for _, mode := range []libghostty.Mode{ libghostty.ModeMouseEventX10, libghostty.ModeMouseEventNormal, libghostty.ModeMouseEventButton, libghostty.ModeMouseEventAny, } { on, err := term.Mode(mode) if err != nil { return false, false, err } if on { buttons = true motion = mode == libghostty.ModeMouseEventAny } } return buttons, motion, nil}
// reportMouse sends e to the program if it belongs to a gesture that goes// there, and reports whether it did, so that it doesn't select too.func (v *View) reportMouse(term *libghostty.Terminal, e pointer.Event, tracking, motion bool, m metrics, pad int) (bool, error) { r := &v.report switch e.Kind { case pointer.Press: if r.held == 0 && (!tracking || e.Modifiers&key.ModShift != 0 || v.OnMouse == nil || term == nil) { return false, nil } if r.held == 0 { // A click on the program ends any selection, as one on // the text does. if err := term.SetSelection(nil); err != nil { return true, err } } v.sendButtons(term, e, e.Buttons&^r.held, "press", m, pad) case pointer.Release, pointer.Cancel: if r.held == 0 { return false, nil } // e.Buttons are those still held. A release that seems to free // none, as some sources send, frees them all, so that no button // stays down in the program; so does a gesture going elsewhere. up := r.held &^ e.Buttons if up == 0 || e.Kind == pointer.Cancel { up = r.held } v.sendButtons(term, e, up, "release", m, pad) case pointer.Drag, pointer.Move: // Gio reports a drag as a move once any one button is released, // though others are held. if r.held == 0 && (e.Kind == pointer.Drag || !motion || e.Modifiers&key.ModShift != 0 || v.OnMouse == nil) { return e.Kind == pointer.Move, nil // a move never selects } if term == nil { return true, nil } // Programs track cells, so motion within one says nothing. if c := gridCell(term, e, m, pad); !r.sent || c != r.cell { v.send(term, e, "motion", "unknown", m, pad) } default: return false, nil } return true, nil}
// sendButtons sends one event for each of buttons, the buttons that e// pressed or released, and tracks them as held or not.func (v *View) sendButtons(term *libghostty.Terminal, e pointer.Event, buttons pointer.Buttons, action string, m metrics, pad int) { for _, b := range []pointer.Buttons{ pointer.ButtonPrimary, pointer.ButtonSecondary, pointer.ButtonTertiary, pointer.ButtonQuaternary, pointer.ButtonQuinary, } { if buttons&b == 0 { continue } if action == "press" { v.report.held |= b } else { v.report.held &^= b } v.send(term, e, action, buttonNames[b], m, pad) }}
// send reports e to the program at its cell, clamped to the grid, with// the buttons the program has been told are held.func (v *View) send(term *libghostty.Terminal, e pointer.Event, action, button string, m metrics, pad int) { if v.OnMouse == nil { return } c := gridCell(term, e, m, pad) if e.Kind == pointer.Cancel && v.report.sent { c = v.report.cell // a cancel has no position } v.report.cell, v.report.sent = c, true var held map[string]bool for b, name := range buttonNames { if v.report.held&b != 0 { if held == nil { held = make(map[string]bool) } held[name] = true } } v.OnMouse(rex.MouseEvent{ Action: action, Button: button, Buttons: held, X: uint16(c.X), Y: uint16(c.Y), Mods: rex.Mods(e.Modifiers&key.ModShift != 0, e.Modifiers&key.ModCtrl != 0, e.Modifiers&key.ModAlt != 0, e.Modifiers&(key.ModSuper|key.ModCommand) != 0), })}
// gridCell is the cell under e, clamped to the grid.func gridCell(term *libghostty.Terminal, e pointer.Event, m metrics, pad int) image.Point { cols, _ := term.Cols() rows, _ := term.Rows() x := min(max(floorDiv(int(e.Position.X)-pad, max(m.width, 1)), 0), max(int(cols)-1, 0)) y := min(max(floorDiv(int(e.Position.Y)-pad, max(m.height, 1)), 0), max(int(rows)-1, 0)) return image.Pt(x, y)}
// buttonNames are Rex's names for Gio's buttons.var buttonNames = map[pointer.Buttons]string{ pointer.ButtonPrimary: "left", pointer.ButtonSecondary: "right", pointer.ButtonTertiary: "middle", pointer.ButtonQuaternary: "four", pointer.ButtonQuinary: "five",}