Overhauled mouse events

Everything gets an image.Point instead of an x y pair, and most
things now get modifiers.
This commit is contained in:
Sasha Koshka
2023-04-20 14:44:54 -04:00
parent eaee284aaf
commit 53f78cb0e7
12 changed files with 183 additions and 69 deletions

View File

@@ -177,7 +177,11 @@ func (element *Button) HandleFocusChange () {
element.entity.Invalidate()
}
func (element *Button) HandleMouseDown (x, y int, button input.Button) {
func (element *Button) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if !element.Enabled() { return }
element.Focus()
if button != input.ButtonLeft { return }
@@ -185,10 +189,14 @@ func (element *Button) HandleMouseDown (x, y int, button input.Button) {
element.entity.Invalidate()
}
func (element *Button) HandleMouseUp (x, y int, button input.Button) {
func (element *Button) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if button != input.ButtonLeft { return }
element.pressed = false
within := image.Point { x, y }.In(element.entity.Bounds())
within := position.In(element.entity.Bounds())
if element.Enabled() && within && element.onClick != nil {
element.onClick()
}

View File

@@ -130,18 +130,26 @@ func (element *Checkbox) HandleFocusChange () {
element.entity.Invalidate()
}
func (element *Checkbox) HandleMouseDown (x, y int, button input.Button) {
func (element *Checkbox) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if !element.Enabled() { return }
element.Focus()
element.pressed = true
element.entity.Invalidate()
}
func (element *Checkbox) HandleMouseUp (x, y int, button input.Button) {
func (element *Checkbox) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if button != input.ButtonLeft || !element.pressed { return }
element.pressed = false
within := image.Point { x, y }.In(element.entity.Bounds())
within := position.In(element.entity.Bounds())
if within {
element.checked = !element.checked
}

View File

@@ -124,13 +124,26 @@ func (element *Directory) Layout () {
}
}
func (element *Directory) HandleMouseDown (x, y int, button input.Button) {
func (element *Directory) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
element.selectNone()
}
func (element *Directory) HandleMouseUp (x, y int, button input.Button) { }
func (element *Directory) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) { }
func (element *Directory) HandleChildMouseDown (x, y int, button input.Button, child tomo.Element) {
func (element *Directory) HandleChildMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
child tomo.Element,
) {
element.selectNone()
if child, ok := child.(tomo.Selectable); ok {
index := element.entity.IndexOf(child)
@@ -138,7 +151,12 @@ func (element *Directory) HandleChildMouseDown (x, y int, button input.Button, c
}
}
func (element *Directory) HandleChildMouseUp (int, int, input.Button, tomo.Element) { }
func (element *Directory) HandleChildMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
child tomo.Element,
) { }
func (element *Directory) HandleChildFlexibleHeightChange (child tomo.Flexible) {
element.updateMinimumSize()

View File

@@ -125,7 +125,7 @@ func (element *File) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
}
}
func (element *File) HandleKeyUp(key input.Key, modifiers input.Modifiers) {
func (element *File) HandleKeyUp (key input.Key, modifiers input.Modifiers) {
if key == input.KeyEnter && element.pressed {
element.pressed = false
if !element.Enabled() { return }
@@ -165,7 +165,11 @@ func (element *File) SetEnabled (enabled bool) {
element.entity.Invalidate()
}
func (element *File) HandleMouseDown (x, y int, button input.Button) {
func (element *File) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if !element.Enabled() { return }
if !element.entity.Focused() { element.Focus() }
if button != input.ButtonLeft { return }
@@ -173,10 +177,14 @@ func (element *File) HandleMouseDown (x, y int, button input.Button) {
element.entity.Invalidate()
}
func (element *File) HandleMouseUp (x, y int, button input.Button) {
func (element *File) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if button != input.ButtonLeft { return }
element.pressed = false
within := image.Point { x, y }.In(element.entity.Bounds())
within := position.In(element.entity.Bounds())
if time.Since(element.lastClick) < element.config.DoubleClickDelay() {
if element.Enabled() && within && element.onChoose != nil {
element.onChoose()

View File

@@ -87,7 +87,12 @@ func (element *List) Layout () {
}
}
func (element *List) HandleChildMouseDown (x, y int, button input.Button, child tomo.Element) {
func (element *List) HandleChildMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
child tomo.Element,
) {
if child, ok := child.(tomo.Selectable); ok {
index := element.entity.IndexOf(child)
if element.selected == index { return }
@@ -99,7 +104,12 @@ func (element *List) HandleChildMouseDown (x, y int, button input.Button, child
}
}
func (element *List) HandleChildMouseUp (int, int, input.Button, tomo.Element) { }
func (element *List) HandleChildMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
child tomo.Element,
) { }
func (element *List) HandleChildFlexibleHeightChange (child tomo.Flexible) {
element.updateMinimumSize()

View File

@@ -2,7 +2,7 @@ package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo"
// import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
import "git.tebibyte.media/sashakoshka/tomo/default/config"
@@ -179,8 +179,9 @@ func (element *Scroll) HandleChildScrollBoundsChange (tomo.Scrollable) {
}
func (element *Scroll) HandleScroll (
x, y int,
position image.Point,
deltaX, deltaY float64,
modifiers input.Modifiers,
) {
horizontal, vertical := element.child.ScrollAxes()
if !horizontal { deltaX = 0 }

View File

@@ -82,18 +82,21 @@ func (element *ScrollBar) Draw (destination canvas.Canvas) {
element.bar)
}
func (element *ScrollBar) HandleMouseDown (x, y int, button input.Button) {
func (element *ScrollBar) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
velocity := element.config.ScrollVelocity()
point := image.Pt(x, y)
if point.In(element.bar) {
if position.In(element.bar) {
// the mouse is pressed down within the bar's handle
element.dragging = true
element.entity.Invalidate()
element.dragOffset =
point.Sub(element.bar.Min).
position.Sub(element.bar.Min).
Add(element.entity.Bounds().Min)
element.dragTo(point)
element.dragTo(position)
} else {
// the mouse is pressed down within the bar's gutter
switch button {
@@ -102,7 +105,7 @@ func (element *ScrollBar) HandleMouseDown (x, y int, button input.Button) {
// the middle of the handle
element.dragging = true
element.dragOffset = element.fallbackDragOffset()
element.dragTo(point)
element.dragTo(position)
case input.ButtonMiddle:
// page up/down on middle click
@@ -112,7 +115,7 @@ func (element *ScrollBar) HandleMouseDown (x, y int, button input.Button) {
} else {
viewport = element.viewportBounds.Dx()
}
if element.isAfterHandle(point) {
if element.isAfterHandle(position) {
element.scrollBy(viewport)
} else {
element.scrollBy(-viewport)
@@ -120,7 +123,7 @@ func (element *ScrollBar) HandleMouseDown (x, y int, button input.Button) {
case input.ButtonRight:
// inch up/down on right click
if element.isAfterHandle(point) {
if element.isAfterHandle(position) {
element.scrollBy(velocity)
} else {
element.scrollBy(-velocity)
@@ -129,20 +132,28 @@ func (element *ScrollBar) HandleMouseDown (x, y int, button input.Button) {
}
}
func (element *ScrollBar) HandleMouseUp (x, y int, button input.Button) {
func (element *ScrollBar) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if element.dragging {
element.dragging = false
element.entity.Invalidate()
}
}
func (element *ScrollBar) HandleMotion (x, y int) {
func (element *ScrollBar) HandleMotion (position image.Point) {
if element.dragging {
element.dragTo(image.Pt(x, y))
element.dragTo(position)
}
}
func (element *ScrollBar) HandleScroll (x, y int, deltaX, deltaY float64) {
func (element *ScrollBar) HandleScroll (
position image.Point,
deltaX, deltaY float64,
modifiers input.Modifiers,
) {
if element.vertical {
element.scrollBy(int(deltaY))
} else {

View File

@@ -111,12 +111,16 @@ func (element *slider) HandleFocusChange () {
element.entity.Invalidate()
}
func (element *slider) HandleMouseDown (x, y int, button input.Button) {
func (element *slider) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if !element.Enabled() { return }
element.Focus()
if button == input.ButtonLeft {
element.dragging = true
element.value = element.valueFor(x, y)
element.value = element.valueFor(position.X, position.Y)
if element.onSlide != nil {
element.onSlide()
}
@@ -124,7 +128,11 @@ func (element *slider) HandleMouseDown (x, y int, button input.Button) {
}
}
func (element *slider) HandleMouseUp (x, y int, button input.Button) {
func (element *slider) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if button != input.ButtonLeft || !element.dragging { return }
element.dragging = false
if element.onRelease != nil {
@@ -133,10 +141,10 @@ func (element *slider) HandleMouseUp (x, y int, button input.Button) {
element.entity.Invalidate()
}
func (element *slider) HandleMotion (x, y int) {
func (element *slider) HandleMotion (position image.Point) {
if element.dragging {
element.dragging = true
element.value = element.valueFor(x, y)
element.value = element.valueFor(position.X, position.Y)
if element.onSlide != nil {
element.onSlide()
}
@@ -144,7 +152,11 @@ func (element *slider) HandleMotion (x, y int) {
}
}
func (element *slider) HandleScroll (x, y int, deltaX, deltaY float64) { }
func (element *slider) HandleScroll (
position image.Point,
deltaX, deltaY float64,
modifiers input.Modifiers,
) { }
func (element *slider) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
switch key {

View File

@@ -101,19 +101,26 @@ func (element *Switch) HandleFocusChange () {
element.entity.Invalidate()
}
func (element *Switch) HandleMouseDown (x, y int, button input.Button) {
func (element *Switch) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if !element.Enabled() { return }
element.Focus()
element.pressed = true
element.entity.Invalidate()
}
func (element *Switch) HandleMouseUp (x, y int, button input.Button) {
func (element *Switch) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if button != input.ButtonLeft || !element.pressed { return }
element.pressed = false
within := image.Point { x, y }.
In(element.entity.Bounds())
within := position.In(element.entity.Bounds())
if within {
element.checked = !element.checked
}

View File

@@ -143,12 +143,16 @@ func (element *TextBox) HandleFocusChange () {
element.entity.Invalidate()
}
func (element *TextBox) HandleMouseDown (x, y int, button input.Button) {
func (element *TextBox) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if !element.Enabled() { return }
element.Focus()
if button == input.ButtonLeft {
runeIndex := element.atPosition(image.Pt(x, y))
runeIndex := element.atPosition(position)
if runeIndex == -1 { return }
if time.Since(element.lastClick) < element.config.DoubleClickDelay() {
@@ -164,19 +168,19 @@ func (element *TextBox) HandleMouseDown (x, y int, button input.Button) {
}
}
func (element *TextBox) HandleMotion (x, y int) {
func (element *TextBox) HandleMotion (position image.Point) {
if !element.Enabled() { return }
switch element.dragging {
case 1:
runeIndex := element.atPosition(image.Pt(x, y))
runeIndex := element.atPosition(position)
if runeIndex > -1 {
element.dot.End = runeIndex
element.entity.Invalidate()
}
case 2:
runeIndex := element.atPosition(image.Pt(x, y))
runeIndex := element.atPosition(position)
if runeIndex > -1 {
if runeIndex < element.dot.Start {
element.dot.End =
@@ -213,7 +217,11 @@ func (element *TextBox) atPosition (position image.Point) int {
fixedutil.Pt(position.Sub(offset).Add(textBoundsMin)))
}
func (element *TextBox) HandleMouseUp (x, y int, button input.Button) {
func (element *TextBox) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if button == input.ButtonLeft {
element.dragging = 0
}