User can now select text with the mouse

This commit is contained in:
Sasha Koshka 2023-08-06 03:38:12 -04:00
parent ea95473090
commit b74ec4336f
3 changed files with 42 additions and 6 deletions

View File

@ -306,6 +306,7 @@ func (window *window) handleMotionNotify (
if window.hasModal { return }
motionEvent := window.compressMotionNotify(*event.MotionNotifyEvent)
window.updateMousePosition(motionEvent.EventX, motionEvent.EventY)
x := int(motionEvent.EventX)
y := int(motionEvent.EventY)

View File

@ -3,10 +3,11 @@ package x
import "image"
import "image/color"
import "golang.org/x/image/font"
import "golang.org/x/image/math/fixed"
import "git.tebibyte.media/tomo/tomo"
import "golang.org/x/image/math/fixed"
import "git.tebibyte.media/tomo/typeset"
import "git.tebibyte.media/tomo/tomo/text"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/canvas"
@ -24,6 +25,8 @@ type textBox struct {
hAlign tomo.Align
selectable bool
selecting bool
selectStart int
dot text.Dot
dotColor color.Color
@ -228,6 +231,38 @@ func (this *textBox) handleFocusLeave () {
this.box.handleFocusLeave()
}
func (this *textBox) handleMouseDown (button input.Button) {
if button == input.ButtonLeft {
index := this.runeUnderMouse()
this.selectStart = index
this.selecting = true
this.Select(text.Dot { Start: this.selectStart, End: index })
}
this.box.handleMouseDown(button)
}
func (this *textBox) handleMouseUp (button input.Button) {
if button == input.ButtonLeft && this.selecting {
index := this.runeUnderMouse()
this.selecting = false
this.Select(text.Dot { Start: this.selectStart, End: index })
}
this.box.handleMouseUp(button)
}
func (this *textBox) handleMouseMove () {
if this.selecting {
index := this.runeUnderMouse()
this.Select(text.Dot { Start: this.selectStart, End: index })
}
this.box.handleMouseMove()
}
func (this *textBox) runeUnderMouse () int {
position := this.MousePosition().Sub(this.textOffset())
return this.drawer.AtPosition(fixPt(position))
}
func (this *textBox) normalizedLayoutBoundsSpace () image.Rectangle {
bounds := this.drawer.LayoutBoundsSpace()
return bounds.Sub(bounds.Min)

View File

@ -117,8 +117,8 @@ func (backend *Backend) newWindow (
Connect(backend.x, window.xWindow.Id)
xevent.ButtonReleaseFun(window.handleButtonRelease).
Connect(backend.x, window.xWindow.Id)
// xevent.MotionNotifyFun(window.handleMotionNotify).
// Connect(backend.x, window.xWindow.Id)
xevent.MotionNotifyFun(window.handleMotionNotify).
Connect(backend.x, window.xWindow.Id)
// xevent.SelectionNotifyFun(window.handleSelectionNotify).
// Connect(backend.x, window.xWindow.Id)
// xevent.PropertyNotifyFun(window.handlePropertyNotify).