WIP scrolling
This commit is contained in:
parent
312ee6270c
commit
c210c07b74
@ -29,8 +29,12 @@ func NewButton (text string) *Button {
|
||||
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
|
||||
box.Add(box.label)
|
||||
box.SetLayout(buttonLayout)
|
||||
|
||||
box.SetPropagateEvents(false)
|
||||
|
||||
box.CaptureDND(true)
|
||||
box.CaptureMouse(true)
|
||||
box.CaptureScroll(true)
|
||||
box.CaptureKeyboard(true)
|
||||
|
||||
box.OnMouseUp(box.handleMouseUp)
|
||||
box.OnKeyUp(box.handleKeyUp)
|
||||
box.SetFocusable(true)
|
||||
|
2
go.mod
2
go.mod
@ -2,6 +2,6 @@ module git.tebibyte.media/tomo/objects
|
||||
|
||||
go 1.20
|
||||
|
||||
require git.tebibyte.media/tomo/tomo v0.27.0
|
||||
require git.tebibyte.media/tomo/tomo v0.29.0
|
||||
|
||||
require golang.org/x/image v0.11.0 // indirect
|
||||
|
4
go.sum
4
go.sum
@ -1,5 +1,5 @@
|
||||
git.tebibyte.media/tomo/tomo v0.27.0 h1:gCwxQe0qm1hZLfHkMI3OccNMC/lB1cfs4BbaMz/bXug=
|
||||
git.tebibyte.media/tomo/tomo v0.27.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
||||
git.tebibyte.media/tomo/tomo v0.29.0 h1:uvdPaEQYcWH965y85SjIKwhLklnTbs+x6MRwLfdRvfo=
|
||||
git.tebibyte.media/tomo/tomo v0.29.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
|
7
input.go
7
input.go
@ -1,5 +1,6 @@
|
||||
package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/text"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
@ -22,7 +23,9 @@ func NewTextInput (text string) *TextInput {
|
||||
this.SetText(text)
|
||||
this.SetFocusable(true)
|
||||
this.SetSelectable(true)
|
||||
this.SetOverflow(true, false)
|
||||
this.OnKeyDown(this.handleKeyDown)
|
||||
this.OnScroll(this.handleScroll)
|
||||
return this
|
||||
}
|
||||
|
||||
@ -88,3 +91,7 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
|
||||
this.Select(dot)
|
||||
if changed { this.SetText(string(this.text)) }
|
||||
}
|
||||
|
||||
func (this *TextInput) handleScroll (x, y float64) {
|
||||
this.ScrollTo(this.ContentBounds().Min.Add(image.Pt(int(x), int(y))))
|
||||
}
|
||||
|
234
scrollbar.go
Normal file
234
scrollbar.go
Normal file
@ -0,0 +1,234 @@
|
||||
package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
|
||||
type Scrollbar struct {
|
||||
tomo.ContainerBox
|
||||
handle *SliderHandle
|
||||
layout scrollbarLayout
|
||||
dragging bool
|
||||
dragOffset image.Point
|
||||
|
||||
linkCookie event.Cookie
|
||||
|
||||
on struct {
|
||||
valueChange event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
func newScrollbar (orient string) *Scrollbar {
|
||||
this := &Scrollbar {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
handle: &SliderHandle {
|
||||
Box: tomo.NewBox(),
|
||||
},
|
||||
layout: scrollbarLayout {
|
||||
vertical: orient == "vertical",
|
||||
},
|
||||
}
|
||||
|
||||
this.Add(this.handle)
|
||||
this.SetFocusable(true)
|
||||
|
||||
this.CaptureDND(true)
|
||||
this.CaptureMouse(true)
|
||||
this.CaptureScroll(true)
|
||||
this.CaptureKeyboard(true)
|
||||
|
||||
this.OnKeyDown(this.handleKeyDown)
|
||||
this.OnMouseDown(this.handleMouseDown)
|
||||
this.OnMouseUp(this.handleMouseUp)
|
||||
this.OnMouseMove(this.handleMouseMove)
|
||||
theme.Apply(this.handle, theme.R("objects", "SliderHandle", orient))
|
||||
theme.Apply(this, theme.R("objects", "Slider", orient))
|
||||
return this
|
||||
}
|
||||
|
||||
func NewVerticalScrollbar () *Scrollbar {
|
||||
return newScrollbar("vertical")
|
||||
}
|
||||
|
||||
func NewHorizontalScrollbar () *Scrollbar {
|
||||
return newScrollbar("horizontal")
|
||||
}
|
||||
|
||||
func (this *Scrollbar) Link (box tomo.ContentBox) event.Cookie {
|
||||
this.layout.linked = box
|
||||
this.linkCookie = this.newLinkCookie (
|
||||
box.OnContentBoundsChange(this.handleLinkedContentBoundsChange))
|
||||
this.SetLayout(this.layout)
|
||||
return this.linkCookie
|
||||
}
|
||||
|
||||
func (this *Scrollbar) handleLinkedContentBoundsChange () {
|
||||
// TODO
|
||||
}
|
||||
|
||||
// SetValue sets the value of the scrollbar between 0 and 1, where 0 is scrolled
|
||||
// all the way to the left/top, and 1 is scrolled all the way to the
|
||||
// right/bottom.
|
||||
func (this *Scrollbar) SetValue (value float64) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
// Value returns the value of the scrollbar between 0 and 1 where 0 is scrolled
|
||||
// all the way to the left/top, and 1 is scrolled all the way to the
|
||||
// right/bottom.
|
||||
func (this *Scrollbar) Value () float64 {
|
||||
// TODO
|
||||
}
|
||||
|
||||
func (this *Scrollbar) handleKeyDown (key input.Key, numpad bool) {
|
||||
var increment float64; if this.layout.vertical {
|
||||
increment = -0.05
|
||||
} else {
|
||||
increment = 0.05
|
||||
}
|
||||
|
||||
switch key {
|
||||
case input.KeyUp, input.KeyLeft:
|
||||
if this.Modifiers().Alt {
|
||||
this.SetValue(0)
|
||||
} else {
|
||||
this.SetValue(this.Value() - increment)
|
||||
}
|
||||
case input.KeyDown, input.KeyRight:
|
||||
if this.Modifiers().Alt {
|
||||
this.SetValue(1)
|
||||
} else {
|
||||
this.SetValue(this.Value() + increment)
|
||||
}
|
||||
case input.KeyHome:
|
||||
this.SetValue(0)
|
||||
case input.KeyEnd:
|
||||
this.SetValue(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Scrollbar) handleMouseDown (button input.Button) {
|
||||
pointer := this.MousePosition()
|
||||
handle := this.handle.Bounds()
|
||||
var above, within bool
|
||||
|
||||
if pointer.In(handle) {
|
||||
within = true
|
||||
} else if this.layout.vertical {
|
||||
above = pointer.Y < handle.Min.Y
|
||||
} else {
|
||||
above = pointer.X < handle.Min.X
|
||||
}
|
||||
|
||||
switch button {
|
||||
case input.ButtonLeft:
|
||||
if within {
|
||||
this.dragging = true
|
||||
this.dragOffset =
|
||||
pointer.Sub(this.handle.Bounds().Min).
|
||||
Add(this.InnerBounds().Min)
|
||||
this.drag()
|
||||
} else {
|
||||
this.dragOffset = this.fallbackDragOffset()
|
||||
this.dragging = true
|
||||
this.drag()
|
||||
}
|
||||
case input.ButtonMiddle:
|
||||
if above {
|
||||
this.SetValue(0)
|
||||
} else {
|
||||
this.SetValue(1)
|
||||
}
|
||||
case input.ButtonRight:
|
||||
if above {
|
||||
this.SetValue(this.Value() - 0.05)
|
||||
} else {
|
||||
this.SetValue(this.Value() + 0.05)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Scrollbar) handleMouseUp (button input.Button) {
|
||||
if button != input.ButtonLeft || !this.dragging { return }
|
||||
this.dragging = false
|
||||
}
|
||||
|
||||
func (this *Scrollbar) handleMouseMove () {
|
||||
if !this.dragging { return }
|
||||
this.drag()
|
||||
}
|
||||
|
||||
func (this *Scrollbar) drag () {
|
||||
pointer := this.MousePosition().Sub(this.dragOffset)
|
||||
gutter := this.InnerBounds()
|
||||
handle := this.handle.Bounds()
|
||||
|
||||
if this.layout.vertical {
|
||||
this.SetValue (
|
||||
1 -
|
||||
float64(pointer.Y) /
|
||||
float64(gutter.Dy() - handle.Dy()))
|
||||
} else {
|
||||
this.SetValue (
|
||||
float64(pointer.X) /
|
||||
float64(gutter.Dx() - handle.Dx()))
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Scrollbar) fallbackDragOffset () image.Point {
|
||||
if this.layout.vertical {
|
||||
return this.InnerBounds().Min.
|
||||
Add(image.Pt(0, this.handle.Bounds().Dy() / 2))
|
||||
} else {
|
||||
return this.InnerBounds().Min.
|
||||
Add(image.Pt(this.handle.Bounds().Dx() / 2, 0))
|
||||
}
|
||||
}
|
||||
|
||||
type scrollbarCookie struct {
|
||||
owner *Scrollbar
|
||||
subCookies []event.Cookie
|
||||
}
|
||||
|
||||
func (this *Scrollbar) newLinkCookie (subCookies ...event.Cookie) *scrollbarCookie {
|
||||
return &scrollbarCookie {
|
||||
owner: this,
|
||||
subCookies: subCookies,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *scrollbarCookie) Close () {
|
||||
for _, cookie := range this.subCookies {
|
||||
cookie.Close()
|
||||
}
|
||||
this.owner.layout.linked = nil
|
||||
this.owner.SetLayout(this.owner.layout)
|
||||
}
|
||||
|
||||
type scrollbarLayout struct {
|
||||
vertical bool
|
||||
linked tomo.ContentBox
|
||||
}
|
||||
|
||||
func (scrollbarLayout) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
|
||||
if len(boxes) != 1 { return image.Pt(0, 0) }
|
||||
return boxes[0].MinimumSize()
|
||||
}
|
||||
|
||||
func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
if len(boxes) != 1 { return }
|
||||
handle := image.Rectangle { Max: boxes[0].MinimumSize() }
|
||||
gutter := hints.Bounds
|
||||
|
||||
// TODO
|
||||
// - get length of viewport as a percent of length of content
|
||||
// - apply percent to length of gutter
|
||||
// - constrain to minimum length of handle
|
||||
// - set handle to length
|
||||
// - get scroll of content as a percent
|
||||
// - content pos relative to (content length minus viewport length)
|
||||
// - apply percent to length of gutter minus length of handler
|
||||
// - that is the handle position
|
||||
}
|
@ -6,8 +6,6 @@ import "git.tebibyte.media/tomo/tomo/theme"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
|
||||
// UNDER CONSTRUCTION!
|
||||
|
||||
type Slider struct {
|
||||
tomo.ContainerBox
|
||||
handle *SliderHandle
|
||||
@ -37,7 +35,12 @@ func newSlider (orient string, value float64) *Slider {
|
||||
|
||||
this.Add(this.handle)
|
||||
this.SetFocusable(true)
|
||||
this.SetPropagateEvents(false)
|
||||
|
||||
this.CaptureDND(true)
|
||||
this.CaptureMouse(true)
|
||||
this.CaptureScroll(true)
|
||||
this.CaptureKeyboard(true)
|
||||
|
||||
this.SetValue(value)
|
||||
this.OnKeyDown(this.handleKeyDown)
|
||||
this.OnMouseDown(this.handleMouseDown)
|
||||
|
Loading…
Reference in New Issue
Block a user