Scrollbars respond to mouse dragging more naturally

This commit is contained in:
Sasha Koshka 2023-01-21 01:24:24 -05:00
parent d435f5a6a9
commit 92a9c9370d
2 changed files with 11 additions and 12 deletions

View File

@ -272,10 +272,7 @@ func (window *Window) compressMotionNotify (
typedEvent, ok := untypedEvent.Event.(xproto.MotionNotifyEvent)
if !ok { continue }
if firstEvent.Event == typedEvent.Event &&
typedEvent.Detail >= 4 &&
typedEvent.Detail <= 7 {
if firstEvent.Event == typedEvent.Event {
lastEvent = typedEvent
defer func (index int) {
xevent.DequeueAt(window.backend.connection, index)

View File

@ -20,7 +20,7 @@ type ScrollContainer struct {
exists bool
enabled bool
dragging bool
dragOffset image.Point
dragOffset int
gutter image.Rectangle
bar image.Rectangle
}
@ -29,7 +29,7 @@ type ScrollContainer struct {
exists bool
enabled bool
dragging bool
dragOffset image.Point
dragOffset int
gutter image.Rectangle
bar image.Rectangle
}
@ -111,12 +111,16 @@ func (element *ScrollContainer) HandleKeyUp (key tomo.Key, modifiers tomo.Modifi
func (element *ScrollContainer) HandleMouseDown (x, y int, button tomo.Button) {
point := image.Pt(x, y)
if point.In(element.horizontal.gutter) {
if point.In(element.horizontal.bar) {
element.horizontal.dragging = true
element.horizontal.dragOffset =
point.Sub(element.horizontal.bar.Min).X
element.dragHorizontalBar(point)
} else if point.In(element.vertical.gutter) {
} else if point.In(element.vertical.bar) {
element.vertical.dragging = true
element.vertical.dragOffset =
point.Sub(element.vertical.bar.Min).Y
element.dragVerticalBar(point)
} else if child, ok := element.child.(tomo.MouseTarget); ok {
@ -353,8 +357,7 @@ func (element *ScrollContainer) dragHorizontalBar (mousePosition image.Point) {
scrollX :=
float64(element.child.ScrollContentBounds().Dx()) /
float64(element.horizontal.gutter.Dx()) *
float64 (
mousePosition.X - element.horizontal.bar.Dx() / 2)
float64(mousePosition.X - element.horizontal.dragOffset)
scrollY := element.child.ScrollViewportBounds().Min.Y
element.child.ScrollTo(image.Pt(int(scrollX), scrollY))
}
@ -363,8 +366,7 @@ func (element *ScrollContainer) dragVerticalBar (mousePosition image.Point) {
scrollY :=
float64(element.child.ScrollContentBounds().Dy()) /
float64(element.vertical.gutter.Dy()) *
float64 (
mousePosition.Y - element.vertical.bar.Dy() / 2)
float64(mousePosition.Y - element.vertical.dragOffset)
scrollX := element.child.ScrollViewportBounds().Min.X
element.child.ScrollTo(image.Pt(scrollX, int(scrollY)))
}