Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dca1eaefb5 | |||
| 87e81c00d3 | |||
| 224ca25000 | |||
| f99f60d642 | |||
| b4ab60df77 | |||
| a0fe7bc00f | |||
| 1ff9982e01 | |||
| ca2f9654b3 | |||
| 4d790f7264 | |||
| 49f010a8d6 | |||
| 39541e1b78 | |||
| c8bcb9e428 | |||
| 858129ec33 | |||
| 02551987a4 | |||
| e45e391f6d | |||
| 71bc235c0e | |||
| c9e556f47b | |||
| 15d7031e22 | |||
| 134f8016c1 | |||
| 2251d33ac7 | |||
| c210c07b74 | |||
| 312ee6270c | |||
| 59ca14cab6 | |||
| 67a0e09101 | |||
| 9cba0151ce | |||
| d8d24632bb | |||
| 05b6490095 |
@@ -1,4 +1,6 @@
|
|||||||
# objects
|
# objects
|
||||||
|
|
||||||
|
[](https://pkg.go.dev/git.tebibyte.media/tomo/objects)
|
||||||
|
|
||||||
Objects contains a standard collection of re-usable objects. All objects in this
|
Objects contains a standard collection of re-usable objects. All objects in this
|
||||||
module visually conform to whatever the theme is set to.
|
module visually conform to whatever the theme is set to.
|
||||||
|
|||||||
66
button.go
66
button.go
@@ -4,10 +4,20 @@ import "git.tebibyte.media/tomo/tomo"
|
|||||||
import "git.tebibyte.media/tomo/tomo/theme"
|
import "git.tebibyte.media/tomo/tomo/theme"
|
||||||
import "git.tebibyte.media/tomo/tomo/input"
|
import "git.tebibyte.media/tomo/tomo/input"
|
||||||
import "git.tebibyte.media/tomo/tomo/event"
|
import "git.tebibyte.media/tomo/tomo/event"
|
||||||
|
import "git.tebibyte.media/tomo/objects/layouts"
|
||||||
|
|
||||||
|
var buttonLayout = layouts.NewGrid([]bool { true }, []bool { true })
|
||||||
|
var iconButtonLayout = layouts.NewGrid([]bool { false }, []bool { true })
|
||||||
|
var bothButtonLayout = layouts.NewGrid([]bool { false, true }, []bool { true })
|
||||||
|
|
||||||
// Button is a clickable button.
|
// Button is a clickable button.
|
||||||
type Button struct {
|
type Button struct {
|
||||||
tomo.TextBox
|
tomo.ContainerBox
|
||||||
|
|
||||||
|
label *Label
|
||||||
|
icon *Icon
|
||||||
|
labelActive bool
|
||||||
|
|
||||||
on struct {
|
on struct {
|
||||||
click event.FuncBroadcaster
|
click event.FuncBroadcaster
|
||||||
}
|
}
|
||||||
@@ -15,21 +25,71 @@ type Button struct {
|
|||||||
|
|
||||||
// NewButton creates a new button with the specified text.
|
// NewButton creates a new button with the specified text.
|
||||||
func NewButton (text string) *Button {
|
func NewButton (text string) *Button {
|
||||||
box := &Button { TextBox: tomo.NewTextBox() }
|
box := &Button {
|
||||||
|
ContainerBox: tomo.NewContainerBox(),
|
||||||
|
label: NewLabel(text),
|
||||||
|
}
|
||||||
theme.Apply(box, theme.R("objects", "Button", ""))
|
theme.Apply(box, theme.R("objects", "Button", ""))
|
||||||
|
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
|
||||||
|
box.SetLayout(buttonLayout)
|
||||||
box.SetText(text)
|
box.SetText(text)
|
||||||
box.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
|
|
||||||
|
box.CaptureDND(true)
|
||||||
|
box.CaptureMouse(true)
|
||||||
|
box.CaptureScroll(true)
|
||||||
|
box.CaptureKeyboard(true)
|
||||||
|
|
||||||
box.OnMouseUp(box.handleMouseUp)
|
box.OnMouseUp(box.handleMouseUp)
|
||||||
box.OnKeyUp(box.handleKeyUp)
|
box.OnKeyUp(box.handleKeyUp)
|
||||||
box.SetFocusable(true)
|
box.SetFocusable(true)
|
||||||
return box
|
return box
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetText sets the text of the button's label.
|
||||||
|
func (this *Button) SetText (text string) {
|
||||||
|
this.label.SetText(text)
|
||||||
|
if this.labelActive && text == "" {
|
||||||
|
this.Delete(this.label)
|
||||||
|
this.labelActive = false
|
||||||
|
}
|
||||||
|
if !this.labelActive && text != "" {
|
||||||
|
this.Add(this.label)
|
||||||
|
this.labelActive = true
|
||||||
|
}
|
||||||
|
this.applyLayout()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetIcon sets an icon for this button. Setting the icon to IconUnknown will
|
||||||
|
// remove it.
|
||||||
|
func (this *Button) SetIcon (id theme.Icon) {
|
||||||
|
if this.icon != nil { this.Delete(this.icon) }
|
||||||
|
|
||||||
|
var icon *Icon; if id != theme.IconUnknown {
|
||||||
|
icon = NewIcon(id, theme.IconSizeSmall)
|
||||||
|
}
|
||||||
|
this.icon = icon
|
||||||
|
|
||||||
|
if this.icon != nil {
|
||||||
|
this.Insert(this.icon, this.label)
|
||||||
|
}
|
||||||
|
this.applyLayout()
|
||||||
|
}
|
||||||
|
|
||||||
// OnClick specifies a function to be called when the button is clicked.
|
// OnClick specifies a function to be called when the button is clicked.
|
||||||
func (this *Button) OnClick (callback func ()) event.Cookie {
|
func (this *Button) OnClick (callback func ()) event.Cookie {
|
||||||
return this.on.click.Connect(callback)
|
return this.on.click.Connect(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Button) applyLayout () {
|
||||||
|
if this.labelActive && this.icon == nil {
|
||||||
|
this.SetLayout(buttonLayout)
|
||||||
|
} else if !this.labelActive && this.icon != nil {
|
||||||
|
this.SetLayout(iconButtonLayout)
|
||||||
|
} else {
|
||||||
|
this.SetLayout(bothButtonLayout)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (this *Button) handleKeyUp (key input.Key, numberPad bool) {
|
func (this *Button) handleKeyUp (key input.Key, numberPad bool) {
|
||||||
if key != input.KeyEnter && key != input.Key(' ') { return }
|
if key != input.KeyEnter && key != input.Key(' ') { return }
|
||||||
this.on.click.Broadcast()
|
this.on.click.Broadcast()
|
||||||
|
|||||||
69
checkbox.go
Normal file
69
checkbox.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package objects
|
||||||
|
|
||||||
|
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"
|
||||||
|
|
||||||
|
// Checkbox is a control that can be toggled.
|
||||||
|
type Checkbox struct {
|
||||||
|
tomo.Box
|
||||||
|
value bool
|
||||||
|
on struct {
|
||||||
|
valueChange event.FuncBroadcaster
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCheckbox creates a new checkbox with the specified value.
|
||||||
|
func NewCheckbox (value bool) *Checkbox {
|
||||||
|
box := &Checkbox {
|
||||||
|
Box: tomo.NewBox(),
|
||||||
|
}
|
||||||
|
theme.Apply(box, theme.R("objects", "Checkbox", ""))
|
||||||
|
box.SetValue(false)
|
||||||
|
|
||||||
|
box.OnMouseUp(box.handleMouseUp)
|
||||||
|
box.OnKeyUp(box.handleKeyUp)
|
||||||
|
box.SetFocusable(true)
|
||||||
|
return box
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetValue sets the value of the checkbox.
|
||||||
|
func (this *Checkbox) SetValue (value bool) {
|
||||||
|
this.value = value
|
||||||
|
if this.value {
|
||||||
|
// TODO perhaps have IconStatusOkay/Cancel in actions, and have
|
||||||
|
// a status icon for checked checkboxes.
|
||||||
|
this.SetTexture(theme.IconStatusOkay.Texture(theme.IconSizeSmall))
|
||||||
|
} else {
|
||||||
|
this.SetTexture(nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Toggle toggles the value of the checkbox between true and false.
|
||||||
|
func (this *Checkbox) Toggle () {
|
||||||
|
this.SetValue(!this.Value())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value returns the value of the checkbox.
|
||||||
|
func (this *Checkbox) Value () bool {
|
||||||
|
return this.value
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnValueChange specifies a function to be called when the checkbox's value
|
||||||
|
// changes.
|
||||||
|
func (this *Checkbox) OnValueChange (callback func ()) event.Cookie {
|
||||||
|
return this.on.valueChange.Connect(callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Checkbox) handleKeyUp (key input.Key, numberPad bool) {
|
||||||
|
if key != input.KeyEnter && key != input.Key(' ') { return }
|
||||||
|
this.Toggle()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Checkbox) handleMouseUp (button input.Button) {
|
||||||
|
if button != input.ButtonLeft { return }
|
||||||
|
if this.MousePosition().In(this.Bounds()) {
|
||||||
|
this.Toggle()
|
||||||
|
}
|
||||||
|
}
|
||||||
12
container.go
12
container.go
@@ -23,13 +23,23 @@ func newContainer (layout tomo.Layout, children ...tomo.Object) *Container {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewOuterContainer creates a new container that has padding around it.
|
// NewOuterContainer creates a new container that has padding around it, as well
|
||||||
|
// as a solid background color. It is meant to be used as a root container for a
|
||||||
|
// window, tab pane, etc.
|
||||||
func NewOuterContainer (layout tomo.Layout, children ...tomo.Object) *Container {
|
func NewOuterContainer (layout tomo.Layout, children ...tomo.Object) *Container {
|
||||||
this := newContainer(layout, children...)
|
this := newContainer(layout, children...)
|
||||||
theme.Apply(this, theme.R("objects", "Container", "outer"))
|
theme.Apply(this, theme.R("objects", "Container", "outer"))
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewSunkenContainer creates a new container with a sunken style and padding
|
||||||
|
// around it. It is meant to be used as a root container for a ScrollContainer.
|
||||||
|
func NewSunkenContainer (layout tomo.Layout, children ...tomo.Object) *Container {
|
||||||
|
this := newContainer(layout, children...)
|
||||||
|
theme.Apply(this, theme.R("objects", "Container", "sunken"))
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
// NewInnerContainer creates a new container that has no padding around it.
|
// NewInnerContainer creates a new container that has no padding around it.
|
||||||
func NewInnerContainer (layout tomo.Layout, children ...tomo.Object) *Container {
|
func NewInnerContainer (layout tomo.Layout, children ...tomo.Object) *Container {
|
||||||
this := newContainer(layout, children...)
|
this := newContainer(layout, children...)
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -2,6 +2,6 @@ module git.tebibyte.media/tomo/objects
|
|||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require git.tebibyte.media/tomo/tomo v0.26.1
|
require git.tebibyte.media/tomo/tomo v0.31.0
|
||||||
|
|
||||||
require golang.org/x/image v0.11.0 // indirect
|
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.26.1 h1:V5ciRuixMYb79aAawgquFEfJ1icyEmMKBKFPWwi94NE=
|
git.tebibyte.media/tomo/tomo v0.31.0 h1:LHPpj3AWycochnC8F441aaRNS6Tq6w6WnBrp/LGjyhM=
|
||||||
git.tebibyte.media/tomo/tomo v0.26.1/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
git.tebibyte.media/tomo/tomo v0.31.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
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-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
|||||||
@@ -12,10 +12,10 @@ type Heading struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewHeading creates a new section heading. The level can be from 0 to 2.
|
// NewHeading creates a new section heading. The level can be from 0 to 2.
|
||||||
func NewHeading (level int, text string) *Label {
|
func NewHeading (level int, text string) *Heading {
|
||||||
if level < 0 { level = 0 }
|
if level < 0 { level = 0 }
|
||||||
if level > 2 { level = 2 }
|
if level > 2 { level = 2 }
|
||||||
this := &Label { TextBox: tomo.NewTextBox() }
|
this := &Heading { TextBox: tomo.NewTextBox() }
|
||||||
theme.Apply(this, theme.R("objects", "Heading", fmt.Sprint(level)))
|
theme.Apply(this, theme.R("objects", "Heading", fmt.Sprint(level)))
|
||||||
this.SetText(text)
|
this.SetText(text)
|
||||||
return this
|
return this
|
||||||
|
|||||||
42
icon.go
Normal file
42
icon.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package objects
|
||||||
|
|
||||||
|
import "image"
|
||||||
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/data"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/theme"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||||
|
|
||||||
|
// Icon displays a single icon.
|
||||||
|
type Icon struct {
|
||||||
|
tomo.Box
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewIcon creates a new icon from an icon ID.
|
||||||
|
func NewIcon (id theme.Icon, size theme.IconSize) *Icon {
|
||||||
|
this := &Icon {
|
||||||
|
Box: tomo.NewBox(),
|
||||||
|
}
|
||||||
|
theme.Apply(this, theme.R("objects", "Icon", size.String()))
|
||||||
|
this.SetTexture(id.Texture(size))
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMimeIcon creates a new icon from a MIME type.
|
||||||
|
func NewMimeIcon (mime data.Mime, size theme.IconSize) *Icon {
|
||||||
|
this := &Icon {
|
||||||
|
Box: tomo.NewBox(),
|
||||||
|
}
|
||||||
|
theme.Apply(this, theme.R("objects", "Icon", size.String()))
|
||||||
|
this.SetTexture(theme.MimeIcon(mime, size))
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Icon) SetTexture (texture canvas.Texture) {
|
||||||
|
this.Box.SetTexture(texture)
|
||||||
|
if texture == nil {
|
||||||
|
this.SetMinimumSize(image.Pt(0, 0))
|
||||||
|
} else {
|
||||||
|
bounds := texture.Bounds()
|
||||||
|
this.SetMinimumSize(bounds.Max.Sub(bounds.Min))
|
||||||
|
}
|
||||||
|
}
|
||||||
22
input.go
22
input.go
@@ -1,5 +1,6 @@
|
|||||||
package objects
|
package objects
|
||||||
|
|
||||||
|
import "image"
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
import "git.tebibyte.media/tomo/tomo/text"
|
import "git.tebibyte.media/tomo/tomo/text"
|
||||||
import "git.tebibyte.media/tomo/tomo/theme"
|
import "git.tebibyte.media/tomo/tomo/theme"
|
||||||
@@ -12,6 +13,7 @@ type TextInput struct {
|
|||||||
text []rune
|
text []rune
|
||||||
on struct {
|
on struct {
|
||||||
enter event.FuncBroadcaster
|
enter event.FuncBroadcaster
|
||||||
|
edit event.FuncBroadcaster
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,10 +21,13 @@ type TextInput struct {
|
|||||||
func NewTextInput (text string) *TextInput {
|
func NewTextInput (text string) *TextInput {
|
||||||
this := &TextInput { TextBox: tomo.NewTextBox() }
|
this := &TextInput { TextBox: tomo.NewTextBox() }
|
||||||
theme.Apply(this, theme.R("objects", "TextInput", ""))
|
theme.Apply(this, theme.R("objects", "TextInput", ""))
|
||||||
|
this.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
||||||
this.SetText(text)
|
this.SetText(text)
|
||||||
this.SetFocusable(true)
|
this.SetFocusable(true)
|
||||||
this.SetSelectable(true)
|
this.SetSelectable(true)
|
||||||
|
this.SetOverflow(true, false)
|
||||||
this.OnKeyDown(this.handleKeyDown)
|
this.OnKeyDown(this.handleKeyDown)
|
||||||
|
this.OnScroll(this.handleScroll)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,6 +48,11 @@ func (this *TextInput) OnEnter (callback func ()) event.Cookie {
|
|||||||
return this.on.enter.Connect(callback)
|
return this.on.enter.Connect(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnEdit specifies a function to be called when the user edits the input text.
|
||||||
|
func (this *TextInput) OnEdit (callback func ()) event.Cookie {
|
||||||
|
return this.on.edit.Connect(callback)
|
||||||
|
}
|
||||||
|
|
||||||
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
|
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
|
||||||
dot := this.Dot()
|
dot := this.Dot()
|
||||||
modifiers := this.Modifiers()
|
modifiers := this.Modifiers()
|
||||||
@@ -50,6 +60,9 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
|
|||||||
sel := modifiers.Shift
|
sel := modifiers.Shift
|
||||||
changed := false
|
changed := false
|
||||||
|
|
||||||
|
// TODO all this (except editing stuff) really should be moved into the
|
||||||
|
// backend
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case key == input.KeyEnter:
|
case key == input.KeyEnter:
|
||||||
this.on.enter.Broadcast()
|
this.on.enter.Broadcast()
|
||||||
@@ -86,5 +99,12 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.Select(dot)
|
this.Select(dot)
|
||||||
if changed { this.SetText(string(this.text)) }
|
if changed {
|
||||||
|
this.SetText(string(this.text))
|
||||||
|
this.on.edit.Broadcast()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *TextInput) handleScroll (x, y float64) {
|
||||||
|
this.ScrollTo(this.ContentBounds().Min.Add(image.Pt(int(x), int(y))))
|
||||||
}
|
}
|
||||||
|
|||||||
1
label.go
1
label.go
@@ -15,4 +15,3 @@ func NewLabel (text string) *Label {
|
|||||||
this.SetText(text)
|
this.SetText(text)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
62
labelcheckbox.go
Normal file
62
labelcheckbox.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package objects
|
||||||
|
|
||||||
|
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"
|
||||||
|
import "git.tebibyte.media/tomo/objects/layouts"
|
||||||
|
|
||||||
|
// LabelCheckbox is a checkbox with a label.
|
||||||
|
type LabelCheckbox struct {
|
||||||
|
tomo.ContainerBox
|
||||||
|
checkbox *Checkbox
|
||||||
|
label *Label
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLabelCheckbox creates a new labeled checkbox with the specified value and
|
||||||
|
// label text.
|
||||||
|
func NewLabelCheckbox (value bool, text string) *LabelCheckbox {
|
||||||
|
box := &LabelCheckbox {
|
||||||
|
ContainerBox: tomo.NewContainerBox(),
|
||||||
|
checkbox: NewCheckbox(value),
|
||||||
|
label: NewLabel(text),
|
||||||
|
}
|
||||||
|
theme.Apply(box, theme.R("objects", "LabelCheckbox", ""))
|
||||||
|
box.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
||||||
|
box.Add(box.checkbox)
|
||||||
|
box.Add(box.label)
|
||||||
|
box.SetLayout(layouts.NewGrid([]bool { false, true }, []bool { true }))
|
||||||
|
|
||||||
|
box.OnMouseUp(box.handleMouseUp)
|
||||||
|
box.label.OnMouseUp(box.handleMouseUp)
|
||||||
|
return box
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetValue sets the value of the checkbox.
|
||||||
|
func (this *LabelCheckbox) SetValue (value bool) {
|
||||||
|
this.checkbox.SetValue(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Toggle toggles the value of the checkbox between true and false.
|
||||||
|
func (this *LabelCheckbox) Toggle () {
|
||||||
|
this.checkbox.Toggle()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value returns the value of the checkbox.
|
||||||
|
func (this *LabelCheckbox) Value () bool {
|
||||||
|
return this.checkbox.Value()
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnValueChange specifies a function to be called when the checkbox's value
|
||||||
|
// changes.
|
||||||
|
func (this *LabelCheckbox) OnValueChange (callback func ()) event.Cookie {
|
||||||
|
return this.checkbox.OnValueChange(callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *LabelCheckbox) handleMouseUp (button input.Button) {
|
||||||
|
if button != input.ButtonLeft { return }
|
||||||
|
if this.MousePosition().In(this.Bounds()) {
|
||||||
|
this.checkbox.SetFocused(true)
|
||||||
|
this.checkbox.Toggle()
|
||||||
|
}
|
||||||
|
}
|
||||||
100
numberinput.go
Normal file
100
numberinput.go
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package objects
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
import "strconv"
|
||||||
|
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"
|
||||||
|
import "git.tebibyte.media/tomo/objects/layouts"
|
||||||
|
|
||||||
|
// NumberInput is an editable text box which accepts only numbers, and has
|
||||||
|
// controls to increment and decrement its value.
|
||||||
|
type NumberInput struct {
|
||||||
|
tomo.ContainerBox
|
||||||
|
input *TextInput
|
||||||
|
increment *Button
|
||||||
|
decrement *Button
|
||||||
|
on struct {
|
||||||
|
enter event.FuncBroadcaster
|
||||||
|
edit event.FuncBroadcaster
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNumberInput creates a new number input with the specified value.
|
||||||
|
func NewNumberInput (value float64) *NumberInput {
|
||||||
|
box := &NumberInput {
|
||||||
|
ContainerBox: tomo.NewContainerBox(),
|
||||||
|
input: NewTextInput(""),
|
||||||
|
increment: NewButton(""),
|
||||||
|
decrement: NewButton(""),
|
||||||
|
}
|
||||||
|
theme.Apply(box, theme.R("objects", "NumberInput", ""))
|
||||||
|
box.Add(box.input)
|
||||||
|
box.Add(box.decrement)
|
||||||
|
box.Add(box.increment)
|
||||||
|
box.SetLayout(layouts.NewGrid([]bool { true, false, false }, []bool { true }))
|
||||||
|
box.increment.SetIcon(theme.IconActionIncrement)
|
||||||
|
box.decrement.SetIcon(theme.IconActionDecrement)
|
||||||
|
|
||||||
|
box.SetValue(value)
|
||||||
|
|
||||||
|
box.CaptureScroll(true)
|
||||||
|
box.CaptureKeyboard(true)
|
||||||
|
|
||||||
|
box.OnScroll(box.handleScroll)
|
||||||
|
box.OnKeyDown(box.handleKeyDown)
|
||||||
|
box.input.OnEnter(box.handleEnter)
|
||||||
|
box.input.OnEdit(box.on.edit.Broadcast)
|
||||||
|
box.increment.OnClick(func () { box.shift(1) })
|
||||||
|
box.decrement.OnClick(func () { box.shift(-1) })
|
||||||
|
return box
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetValue sets the value of the input.
|
||||||
|
func (this *NumberInput) SetValue (value float64) {
|
||||||
|
this.input.SetText(strconv.FormatFloat(value, 'g', -1, 64))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value returns the value of the input.
|
||||||
|
func (this *NumberInput) Value () float64 {
|
||||||
|
value, _ := strconv.ParseFloat(this.input.Text(), 64)
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnEnter specifies a function to be called when the user presses enter within
|
||||||
|
// the text input.
|
||||||
|
func (this *NumberInput) OnEnter (callback func ()) event.Cookie {
|
||||||
|
return this.on.enter.Connect(callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnEdit specifies a function to be called when the user edits the input value.
|
||||||
|
func (this *NumberInput) OnEdit (callback func ()) event.Cookie {
|
||||||
|
return this.on.edit.Connect(callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *NumberInput) shift (by int) {
|
||||||
|
this.SetValue(this.Value() + float64(by))
|
||||||
|
this.on.edit.Broadcast()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *NumberInput) handleKeyDown (key input.Key, numpad bool) {
|
||||||
|
switch key {
|
||||||
|
case input.KeyUp: this.shift(1)
|
||||||
|
case input.KeyDown: this.shift(-1)
|
||||||
|
default: this.input.handleKeyDown(key, numpad)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *NumberInput) handleScroll (x, y float64) {
|
||||||
|
if x == 0 {
|
||||||
|
this.shift(-int(math.Round(y)))
|
||||||
|
} else {
|
||||||
|
this.input.handleScroll(x, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *NumberInput) handleEnter () {
|
||||||
|
this.SetValue(this.Value())
|
||||||
|
this.on.enter.Broadcast()
|
||||||
|
}
|
||||||
364
scrollbar.go
Normal file
364
scrollbar.go
Normal file
@@ -0,0 +1,364 @@
|
|||||||
|
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"
|
||||||
|
|
||||||
|
// Scrollbar is a special type of slider that can control the scroll of any
|
||||||
|
// overflowing ContainerBox.
|
||||||
|
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)
|
||||||
|
this.OnScroll(this.handleScroll)
|
||||||
|
theme.Apply(this.handle, theme.R("objects", "SliderHandle", orient))
|
||||||
|
theme.Apply(this, theme.R("objects", "Slider", orient))
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewVerticalScrollbar creates a new vertical scrollbar.
|
||||||
|
func NewVerticalScrollbar () *Scrollbar {
|
||||||
|
return newScrollbar("vertical")
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHorizontalScrollbar creates a new horizontal scrollbar.
|
||||||
|
func NewHorizontalScrollbar () *Scrollbar {
|
||||||
|
return newScrollbar("horizontal")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Link assigns this scrollbar to a ContentBox. Closing the returned cookie will
|
||||||
|
// unlink it.
|
||||||
|
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 () {
|
||||||
|
if this.layout.linked == nil { return }
|
||||||
|
previousValue := this.layout.value
|
||||||
|
trackLength := this.layout.contentLength() - this.layout.viewportLength()
|
||||||
|
if trackLength == 0 {
|
||||||
|
this.layout.value = 0
|
||||||
|
} else {
|
||||||
|
this.layout.value = this.layout.contentPos() / trackLength
|
||||||
|
}
|
||||||
|
this.SetLayout(this.layout)
|
||||||
|
if this.layout.value != previousValue {
|
||||||
|
this.on.valueChange.Broadcast()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
if this.layout.linked == nil { return }
|
||||||
|
if value > 1 { value = 1 }
|
||||||
|
if value < 0 { value = 0 }
|
||||||
|
|
||||||
|
trackLength := this.layout.contentLength() - this.layout.viewportLength()
|
||||||
|
position := trackLength * value
|
||||||
|
point := this.layout.linked.ContentBounds().Min
|
||||||
|
if this.layout.vertical {
|
||||||
|
point.Y = -int(position)
|
||||||
|
} else {
|
||||||
|
point.X = -int(position)
|
||||||
|
}
|
||||||
|
this.layout.linked.ScrollTo(point)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
if this.layout.linked == nil { return 0 }
|
||||||
|
return this.layout.value
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnValueChange specifies a function to be called when the position of the
|
||||||
|
// scrollbar changes.
|
||||||
|
func (this *Scrollbar) OnValueChange (callback func ()) event.Cookie {
|
||||||
|
return this.on.valueChange.Connect(callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
within := pointer.In(handle)
|
||||||
|
var above bool; if this.layout.vertical {
|
||||||
|
above = pointer.Y < handle.Min.Y + handle.Dy() / 2
|
||||||
|
} else {
|
||||||
|
above = pointer.X < handle.Min.X + handle.Dx() / 2
|
||||||
|
}
|
||||||
|
|
||||||
|
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.scrollBy(this.pageSize())
|
||||||
|
} else {
|
||||||
|
this.scrollBy(-this.pageSize())
|
||||||
|
}
|
||||||
|
case input.ButtonRight:
|
||||||
|
if above {
|
||||||
|
this.scrollBy(this.stepSize())
|
||||||
|
} else {
|
||||||
|
this.scrollBy(-this.stepSize())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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) handleScroll (x, y float64) {
|
||||||
|
if this.layout.linked == nil { return }
|
||||||
|
this.layout.linked.ScrollTo (
|
||||||
|
this.layout.linked.ContentBounds().Min.
|
||||||
|
Sub(image.Pt(int(x), int(y))))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Scrollbar) drag () {
|
||||||
|
pointer := this.MousePosition().Sub(this.dragOffset)
|
||||||
|
gutter := this.InnerBounds()
|
||||||
|
handle := this.handle.Bounds()
|
||||||
|
|
||||||
|
if this.layout.vertical {
|
||||||
|
this.SetValue (
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Scrollbar) pageSize () int {
|
||||||
|
if this.layout.linked == nil { return 0 }
|
||||||
|
viewport := this.layout.linked.InnerBounds()
|
||||||
|
if this.layout.vertical {
|
||||||
|
return viewport.Dy()
|
||||||
|
} else {
|
||||||
|
return viewport.Dx()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Scrollbar) stepSize () int {
|
||||||
|
// FIXME: this should not be hardcoded, need to get base font metrics
|
||||||
|
// from theme somehow. should be (emspace, lineheight)
|
||||||
|
return 16
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Scrollbar) scrollBy (distance int) {
|
||||||
|
if this.layout.linked == nil { return }
|
||||||
|
var vector image.Point; if this.layout.vertical {
|
||||||
|
vector.Y = distance
|
||||||
|
} else {
|
||||||
|
vector.X = distance
|
||||||
|
}
|
||||||
|
this.layout.linked.ScrollTo (
|
||||||
|
this.layout.linked.ContentBounds().Min.
|
||||||
|
Add(vector))
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
value float64
|
||||||
|
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
|
||||||
|
|
||||||
|
var gutterLength float64;
|
||||||
|
var handleMin float64;
|
||||||
|
if this.vertical {
|
||||||
|
gutterLength = float64(gutter.Dy())
|
||||||
|
handleMin = float64(handle.Dy())
|
||||||
|
} else {
|
||||||
|
gutterLength = float64(gutter.Dx())
|
||||||
|
handleMin = float64(handle.Dx())
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate handle length
|
||||||
|
handleLength := gutterLength * this.viewportContentRatio()
|
||||||
|
if handleLength < handleMin { handleLength = handleMin }
|
||||||
|
if handleLength >= gutterLength {
|
||||||
|
// just hide the handle if it isn't needed.
|
||||||
|
// TODO: we need a way to hide and show boxes, this is janky as
|
||||||
|
// fuck
|
||||||
|
boxes[0].SetBounds(image.Rect(-16, -16, 0, 0))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if this.vertical {
|
||||||
|
handle.Max.Y = int(handleLength)
|
||||||
|
} else {
|
||||||
|
handle.Max.X = int(handleLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate handle position
|
||||||
|
handlePosition := (gutterLength - handleLength) * this.value
|
||||||
|
var handleOffset image.Point
|
||||||
|
if this.vertical {
|
||||||
|
handleOffset = image.Pt(0, int(handlePosition))
|
||||||
|
} else {
|
||||||
|
handleOffset = image.Pt(int(handlePosition), 0)
|
||||||
|
}
|
||||||
|
handle = handle.Sub(handleOffset).Add(gutter.Min)
|
||||||
|
|
||||||
|
// place handle
|
||||||
|
boxes[0].SetBounds(handle)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this scrollbarLayout) viewportContentRatio () float64 {
|
||||||
|
if this.linked == nil { return 0 }
|
||||||
|
return this.viewportLength() / this.contentLength()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this scrollbarLayout) viewportLength () float64 {
|
||||||
|
if this.vertical {
|
||||||
|
return float64(this.linked.InnerBounds().Dy())
|
||||||
|
} else {
|
||||||
|
return float64(this.linked.InnerBounds().Dx())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this scrollbarLayout) contentLength () float64 {
|
||||||
|
if this.vertical {
|
||||||
|
return float64(this.linked.ContentBounds().Dy())
|
||||||
|
} else {
|
||||||
|
return float64(this.linked.ContentBounds().Dx())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this scrollbarLayout) contentPos () float64 {
|
||||||
|
if this.vertical {
|
||||||
|
return float64(this.linked.ContentBounds().Min.Y)
|
||||||
|
} else {
|
||||||
|
return float64(this.linked.ContentBounds().Min.X)
|
||||||
|
}
|
||||||
|
}
|
||||||
167
scrollcontainer.go
Normal file
167
scrollcontainer.go
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
package objects
|
||||||
|
|
||||||
|
import "image"
|
||||||
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/event"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/theme"
|
||||||
|
|
||||||
|
// ScrollSide determines which Scrollbars are active in a ScrollContainer.
|
||||||
|
type ScrollSide int; const (
|
||||||
|
ScrollVertical ScrollSide = 1 << iota
|
||||||
|
ScrollHorizontal
|
||||||
|
ScrollBoth = ScrollVertical | ScrollHorizontal
|
||||||
|
)
|
||||||
|
|
||||||
|
// Horizontal returns true if the side includes a horizontal bar.
|
||||||
|
func (sides ScrollSide) Horizontal () bool {
|
||||||
|
return sides & ScrollHorizontal > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vertical returns true if the side includes a vertical bar.
|
||||||
|
func (sides ScrollSide) Vertical () bool {
|
||||||
|
return sides & ScrollVertical > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns one of:
|
||||||
|
// - both
|
||||||
|
// - horizontal
|
||||||
|
// - vertical
|
||||||
|
// - none
|
||||||
|
func (sides ScrollSide) String () string {
|
||||||
|
switch {
|
||||||
|
case sides.Horizontal() && sides.Vertical(): return "both"
|
||||||
|
case sides.Horizontal(): return "horizontal"
|
||||||
|
case sides.Vertical(): return "vertical"
|
||||||
|
default: return "none"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ScrollContainer couples a ContentBox with one or two Scrollbars.
|
||||||
|
type ScrollContainer struct {
|
||||||
|
tomo.ContainerBox
|
||||||
|
layout *scrollContainerLayout
|
||||||
|
|
||||||
|
horizontalCookie event.Cookie
|
||||||
|
verticalCookie event.Cookie
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewScrollContainer creates a new scroll container.
|
||||||
|
func NewScrollContainer (sides ScrollSide) *ScrollContainer {
|
||||||
|
this := &ScrollContainer {
|
||||||
|
ContainerBox: tomo.NewContainerBox(),
|
||||||
|
layout: &scrollContainerLayout { },
|
||||||
|
}
|
||||||
|
if sides.Vertical() {
|
||||||
|
this.layout.vertical = NewVerticalScrollbar()
|
||||||
|
this.Add(this.layout.vertical)
|
||||||
|
}
|
||||||
|
if sides.Horizontal() {
|
||||||
|
this.layout.horizontal = NewHorizontalScrollbar()
|
||||||
|
this.Add(this.layout.horizontal)
|
||||||
|
}
|
||||||
|
this.CaptureScroll(true)
|
||||||
|
this.OnScroll(this.handleScroll)
|
||||||
|
theme.Apply(this, theme.R("objects", "ScrollContainer", sides.String()))
|
||||||
|
this.SetLayout(this.layout)
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRoot sets the root child of the ScrollContainer. There can only be one at
|
||||||
|
// a time, and setting it will remove and unlink the current child if there is
|
||||||
|
// one.
|
||||||
|
func (this *ScrollContainer) SetRoot (root tomo.ContentBox) {
|
||||||
|
if this.layout.root != nil {
|
||||||
|
// delete root and close cookies
|
||||||
|
this.Delete(this.layout.root)
|
||||||
|
if this.horizontalCookie != nil {
|
||||||
|
this.horizontalCookie.Close()
|
||||||
|
this.horizontalCookie = nil
|
||||||
|
}
|
||||||
|
if this.verticalCookie != nil {
|
||||||
|
this.verticalCookie.Close()
|
||||||
|
this.verticalCookie = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.layout.root = root
|
||||||
|
if root != nil {
|
||||||
|
// insert root at the beginning (for keynav)
|
||||||
|
switch {
|
||||||
|
case this.layout.vertical != nil:
|
||||||
|
this.Insert(root, this.layout.vertical)
|
||||||
|
case this.layout.horizontal != nil:
|
||||||
|
this.Insert(root, this.layout.horizontal)
|
||||||
|
default:
|
||||||
|
this.Add(root)
|
||||||
|
}
|
||||||
|
|
||||||
|
// link root and remember cookies
|
||||||
|
if this.layout.horizontal != nil {
|
||||||
|
this.horizontalCookie = this.layout.horizontal.Link(root)
|
||||||
|
}
|
||||||
|
if this.layout.vertical != nil {
|
||||||
|
this.verticalCookie = this.layout.vertical.Link(root)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *ScrollContainer) handleScroll (x, y float64) {
|
||||||
|
if this.layout.root == nil { return }
|
||||||
|
this.layout.root.ScrollTo (
|
||||||
|
this.layout.root.ContentBounds().Min.
|
||||||
|
Sub(image.Pt(int(x), int(y))))
|
||||||
|
}
|
||||||
|
|
||||||
|
type scrollContainerLayout struct {
|
||||||
|
root tomo.ContentBox
|
||||||
|
horizontal *Scrollbar
|
||||||
|
vertical *Scrollbar
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *scrollContainerLayout) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
|
||||||
|
var minimum image.Point; if this.root != nil {
|
||||||
|
minimum = this.root.MinimumSize()
|
||||||
|
}
|
||||||
|
if this.horizontal != nil {
|
||||||
|
minimum.Y += this.horizontal.MinimumSize().Y
|
||||||
|
}
|
||||||
|
if this.vertical != nil {
|
||||||
|
minimum.X += this.vertical.MinimumSize().X
|
||||||
|
minimum.Y = max(minimum.Y, this.vertical.MinimumSize().Y)
|
||||||
|
}
|
||||||
|
if this.horizontal != nil {
|
||||||
|
minimum.X = max(minimum.X, this.horizontal.MinimumSize().X)
|
||||||
|
}
|
||||||
|
return minimum
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *scrollContainerLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||||
|
rootBounds := hints.Bounds
|
||||||
|
if this.horizontal != nil {
|
||||||
|
rootBounds.Max.Y -= this.horizontal.MinimumSize().Y
|
||||||
|
}
|
||||||
|
if this.vertical != nil {
|
||||||
|
rootBounds.Max.X -= this.vertical.MinimumSize().X
|
||||||
|
}
|
||||||
|
if this.root != nil {
|
||||||
|
this.root.SetBounds(rootBounds)
|
||||||
|
}
|
||||||
|
if this.horizontal != nil {
|
||||||
|
this.horizontal.SetBounds(image.Rect (
|
||||||
|
hints.Bounds.Min.X,
|
||||||
|
rootBounds.Max.Y,
|
||||||
|
rootBounds.Max.X,
|
||||||
|
hints.Bounds.Max.Y))
|
||||||
|
}
|
||||||
|
if this.vertical != nil {
|
||||||
|
this.vertical.SetBounds(image.Rect (
|
||||||
|
rootBounds.Max.X,
|
||||||
|
hints.Bounds.Min.Y,
|
||||||
|
hints.Bounds.Max.X,
|
||||||
|
rootBounds.Max.Y))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func max (x, y int) int {
|
||||||
|
if x > y { return x }
|
||||||
|
return y
|
||||||
|
}
|
||||||
@@ -16,4 +16,3 @@ func NewSeparator () *Separator {
|
|||||||
theme.Apply(this, theme.R("objects", "Separator", ""))
|
theme.Apply(this, theme.R("objects", "Separator", ""))
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
53
slider.go
53
slider.go
@@ -6,8 +6,7 @@ import "git.tebibyte.media/tomo/tomo/theme"
|
|||||||
import "git.tebibyte.media/tomo/tomo/input"
|
import "git.tebibyte.media/tomo/tomo/input"
|
||||||
import "git.tebibyte.media/tomo/tomo/event"
|
import "git.tebibyte.media/tomo/tomo/event"
|
||||||
|
|
||||||
// UNDER CONSTRUCTION!
|
// Slider is a control that selects a numeric value between 0 and 1.
|
||||||
|
|
||||||
type Slider struct {
|
type Slider struct {
|
||||||
tomo.ContainerBox
|
tomo.ContainerBox
|
||||||
handle *SliderHandle
|
handle *SliderHandle
|
||||||
@@ -16,10 +15,12 @@ type Slider struct {
|
|||||||
dragOffset image.Point
|
dragOffset image.Point
|
||||||
|
|
||||||
on struct {
|
on struct {
|
||||||
valueChange event.FuncBroadcaster
|
slide event.FuncBroadcaster
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SliderHandle is a simple object that serves as a handle for sliders and
|
||||||
|
// scrollbars. It is completely inert.
|
||||||
type SliderHandle struct {
|
type SliderHandle struct {
|
||||||
tomo.Box
|
tomo.Box
|
||||||
}
|
}
|
||||||
@@ -37,7 +38,12 @@ func newSlider (orient string, value float64) *Slider {
|
|||||||
|
|
||||||
this.Add(this.handle)
|
this.Add(this.handle)
|
||||||
this.SetFocusable(true)
|
this.SetFocusable(true)
|
||||||
this.SetPropagateEvents(false)
|
|
||||||
|
this.CaptureDND(true)
|
||||||
|
this.CaptureMouse(true)
|
||||||
|
this.CaptureScroll(true)
|
||||||
|
this.CaptureKeyboard(true)
|
||||||
|
|
||||||
this.SetValue(value)
|
this.SetValue(value)
|
||||||
this.OnKeyDown(this.handleKeyDown)
|
this.OnKeyDown(this.handleKeyDown)
|
||||||
this.OnMouseDown(this.handleMouseDown)
|
this.OnMouseDown(this.handleMouseDown)
|
||||||
@@ -48,44 +54,55 @@ func newSlider (orient string, value float64) *Slider {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewVerticalSlider creates a new vertical slider with the specified value.
|
||||||
func NewVerticalSlider (value float64) *Slider {
|
func NewVerticalSlider (value float64) *Slider {
|
||||||
return newSlider("vertical", value)
|
return newSlider("vertical", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewHorizontalSlider creates a new horizontal slider with the specified value.
|
||||||
func NewHorizontalSlider (value float64) *Slider {
|
func NewHorizontalSlider (value float64) *Slider {
|
||||||
return newSlider("horizontal", value)
|
return newSlider("horizontal", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetValue sets the value of the slider between 0 and 1.
|
||||||
func (this *Slider) SetValue (value float64) {
|
func (this *Slider) SetValue (value float64) {
|
||||||
if value < 0 { value = 0 }
|
if value < 0 { value = 0 }
|
||||||
if value > 1 { value = 1 }
|
if value > 1 { value = 1 }
|
||||||
if value == this.layout.value { return }
|
if value == this.layout.value { return }
|
||||||
this.layout.value = value
|
this.layout.value = value
|
||||||
this.SetLayout(this.layout)
|
this.SetLayout(this.layout)
|
||||||
this.on.valueChange.Broadcast()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Value returns the value of the slider between 0 and 1.
|
||||||
func (this *Slider) Value () float64 {
|
func (this *Slider) Value () float64 {
|
||||||
return this.layout.value
|
return this.layout.value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Slider) OnValueChange (callback func ()) event.Cookie {
|
// OnValueChange specifies a function to be called when the user moves the
|
||||||
return this.on.valueChange.Connect(callback)
|
// slider.
|
||||||
|
func (this *Slider) OnSlide (callback func ()) event.Cookie {
|
||||||
|
return this.on.slide.Connect(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
|
func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
|
||||||
|
var increment float64; if this.layout.vertical {
|
||||||
|
increment = -0.05
|
||||||
|
} else {
|
||||||
|
increment = 0.05
|
||||||
|
}
|
||||||
|
|
||||||
switch key {
|
switch key {
|
||||||
case input.KeyUp, input.KeyLeft:
|
case input.KeyUp, input.KeyLeft:
|
||||||
if this.Modifiers().Alt {
|
if this.Modifiers().Alt {
|
||||||
this.SetValue(0)
|
this.SetValue(0)
|
||||||
} else {
|
} else {
|
||||||
this.SetValue(this.Value() - 0.05)
|
this.SetValue(this.Value() - increment)
|
||||||
}
|
}
|
||||||
case input.KeyDown, input.KeyRight:
|
case input.KeyDown, input.KeyRight:
|
||||||
if this.Modifiers().Alt {
|
if this.Modifiers().Alt {
|
||||||
this.SetValue(1)
|
this.SetValue(1)
|
||||||
} else {
|
} else {
|
||||||
this.SetValue(this.Value() + 0.05)
|
this.SetValue(this.Value() + increment)
|
||||||
}
|
}
|
||||||
case input.KeyHome:
|
case input.KeyHome:
|
||||||
this.SetValue(0)
|
this.SetValue(0)
|
||||||
@@ -97,14 +114,12 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
|
|||||||
func (this *Slider) handleMouseDown (button input.Button) {
|
func (this *Slider) handleMouseDown (button input.Button) {
|
||||||
pointer := this.MousePosition()
|
pointer := this.MousePosition()
|
||||||
handle := this.handle.Bounds()
|
handle := this.handle.Bounds()
|
||||||
var above, within bool
|
|
||||||
|
|
||||||
if pointer.In(handle) {
|
within := pointer.In(handle)
|
||||||
within = true
|
var above bool; if this.layout.vertical {
|
||||||
} else if this.layout.vertical {
|
above = pointer.Y < handle.Min.Y + handle.Dy() / 2
|
||||||
above = pointer.Y < handle.Min.Y
|
|
||||||
} else {
|
} else {
|
||||||
above = pointer.X < handle.Min.X
|
above = pointer.X < handle.Min.X + handle.Dx() / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
switch button {
|
switch button {
|
||||||
@@ -123,14 +138,18 @@ func (this *Slider) handleMouseDown (button input.Button) {
|
|||||||
case input.ButtonMiddle:
|
case input.ButtonMiddle:
|
||||||
if above {
|
if above {
|
||||||
this.SetValue(0)
|
this.SetValue(0)
|
||||||
|
this.on.slide.Broadcast()
|
||||||
} else {
|
} else {
|
||||||
this.SetValue(1)
|
this.SetValue(1)
|
||||||
|
this.on.slide.Broadcast()
|
||||||
}
|
}
|
||||||
case input.ButtonRight:
|
case input.ButtonRight:
|
||||||
if above {
|
if above {
|
||||||
this.SetValue(this.Value() - 0.05)
|
this.SetValue(this.Value() - 0.05)
|
||||||
|
this.on.slide.Broadcast()
|
||||||
} else {
|
} else {
|
||||||
this.SetValue(this.Value() + 0.05)
|
this.SetValue(this.Value() + 0.05)
|
||||||
|
this.on.slide.Broadcast()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -152,6 +171,7 @@ func (this *Slider) drag () {
|
|||||||
|
|
||||||
if this.layout.vertical {
|
if this.layout.vertical {
|
||||||
this.SetValue (
|
this.SetValue (
|
||||||
|
1 -
|
||||||
float64(pointer.Y) /
|
float64(pointer.Y) /
|
||||||
float64(gutter.Dy() - handle.Dy()))
|
float64(gutter.Dy() - handle.Dy()))
|
||||||
} else {
|
} else {
|
||||||
@@ -159,6 +179,7 @@ func (this *Slider) drag () {
|
|||||||
float64(pointer.X) /
|
float64(pointer.X) /
|
||||||
float64(gutter.Dx() - handle.Dx()))
|
float64(gutter.Dx() - handle.Dx()))
|
||||||
}
|
}
|
||||||
|
this.on.slide.Broadcast()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Slider) fallbackDragOffset () image.Point {
|
func (this *Slider) fallbackDragOffset () image.Point {
|
||||||
@@ -188,7 +209,7 @@ func (this sliderLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
|||||||
|
|
||||||
if this.vertical {
|
if this.vertical {
|
||||||
height := gutter.Dy() - handle.Dy()
|
height := gutter.Dy() - handle.Dy()
|
||||||
offset := int(float64(height) * this.value)
|
offset := int(float64(height) * (1 - this.value))
|
||||||
handle.Max.X = gutter.Dx()
|
handle.Max.X = gutter.Dx()
|
||||||
boxes[0].SetBounds (
|
boxes[0].SetBounds (
|
||||||
handle.
|
handle.
|
||||||
|
|||||||
27
textview.go
Normal file
27
textview.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package objects
|
||||||
|
|
||||||
|
import "image"
|
||||||
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/theme"
|
||||||
|
|
||||||
|
// TextView is an area for displaying a large amount of multi-line text.
|
||||||
|
type TextView struct {
|
||||||
|
tomo.TextBox
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTextView creates a new text view.
|
||||||
|
func NewTextView (text string) *TextView {
|
||||||
|
this := &TextView { TextBox: tomo.NewTextBox() }
|
||||||
|
theme.Apply(this, theme.R("objects", "TextView", ""))
|
||||||
|
this.SetFocusable(true)
|
||||||
|
this.SetSelectable(true)
|
||||||
|
this.SetText(text)
|
||||||
|
this.SetOverflow(false, true)
|
||||||
|
this.SetWrap(true)
|
||||||
|
this.OnScroll(this.handleScroll)
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *TextView) handleScroll (x, y float64) {
|
||||||
|
this.ScrollTo(this.ContentBounds().Min.Add(image.Pt(int(x), int(y))))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user