20 Commits

Author SHA1 Message Date
6497da69ed LabelCheckbox no longer expands vertically
It should in theory valign to middle this way, but grid layout
suffers from #1
2024-05-18 14:18:02 -04:00
34794f4c77 Make the dialog API more normal
It was very nice but inconsistent with literally everything else,
and never in my life have I seen someone else make a constructor
that way.
2024-05-18 14:16:57 -04:00
25625e9a99 Add dialog boxes 2024-05-18 13:25:06 -04:00
4cea0aa0bd Flow layout no longer leaves last box behind 2024-05-17 14:43:06 -04:00
1cb9e8091e Combine Row/Column layouts into Contract layout 2024-05-17 03:56:49 -04:00
68d49e1b02 Add flow layout 2024-05-17 03:51:24 -04:00
f025ec3d8a Slider can now be controlled by scrolling
If ya nasty
2024-05-17 01:22:47 -04:00
6fad52b335 Slider broadcasts slide event when manipulated with key presses 2024-05-17 01:09:59 -04:00
8134069e1f Add Value and SetValue to ScrollContainer 2024-05-17 01:08:39 -04:00
dca1eaefb5 Add SunkenContainer 2024-05-13 19:48:29 -04:00
87e81c00d3 Scrollbar and ScrollContainer use ScrollTo correctly 2024-05-13 19:45:18 -04:00
224ca25000 NewHeading returns a Heading as expected 2024-05-07 20:21:45 -04:00
f99f60d642 Button now removes its label if it is empty 2024-05-07 20:15:49 -04:00
b4ab60df77 Reverse order of increment/decrement buttons in NumberInput 2024-05-07 20:15:27 -04:00
a0fe7bc00f Slider has an OnSlide method instead of OnValueChange 2024-05-07 18:24:44 -04:00
1ff9982e01 Add NumberInput 2024-05-07 18:24:37 -04:00
ca2f9654b3 Add OnEdit to TextInput 2024-05-07 18:24:25 -04:00
4d790f7264 Add checkboxes 2024-05-07 13:45:06 -04:00
49f010a8d6 Button.SetIcon now takes in theme.Icon 2024-05-06 23:23:59 -04:00
39541e1b78 Upgrade Tomo version 2024-05-03 13:30:32 -04:00
15 changed files with 657 additions and 111 deletions

View File

@@ -7,13 +7,17 @@ import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts" import "git.tebibyte.media/tomo/objects/layouts"
var buttonLayout = layouts.NewGrid([]bool { true }, []bool { true }) var buttonLayout = layouts.NewGrid([]bool { true }, []bool { true })
var iconButtonLayout = layouts.NewGrid([]bool { false, 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.ContainerBox tomo.ContainerBox
label *Label label *Label
icon *Icon icon *Icon
labelActive bool
on struct { on struct {
click event.FuncBroadcaster click event.FuncBroadcaster
} }
@@ -27,8 +31,8 @@ func NewButton (text string) *Button {
} }
theme.Apply(box, theme.R("objects", "Button", "")) theme.Apply(box, theme.R("objects", "Button", ""))
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle) box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
box.Add(box.label)
box.SetLayout(buttonLayout) box.SetLayout(buttonLayout)
box.SetText(text)
box.CaptureDND(true) box.CaptureDND(true)
box.CaptureMouse(true) box.CaptureMouse(true)
@@ -44,20 +48,31 @@ func NewButton (text string) *Button {
// SetText sets the text of the button's label. // SetText sets the text of the button's label.
func (this *Button) SetText (text string) { func (this *Button) SetText (text string) {
this.label.SetText(text) 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. // SetIcon sets an icon for this button. Setting the icon to IconUnknown will
// TODO: use tomo.Icon instead, use small size icons // remove it.
func (this *Button) SetIcon (icon *Icon) { func (this *Button) SetIcon (id theme.Icon) {
if this.icon != nil { this.Delete(this.icon) } if this.icon != nil { this.Delete(this.icon) }
var icon *Icon; if id != theme.IconUnknown {
icon = NewIcon(id, theme.IconSizeSmall)
}
this.icon = icon this.icon = icon
if this.icon == nil { if this.icon != nil {
this.SetLayout(buttonLayout)
} else {
this.SetLayout(iconButtonLayout)
this.Insert(this.icon, this.label) 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.
@@ -65,6 +80,16 @@ 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
View 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()
}
}

View File

@@ -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...)

101
dialog.go Normal file
View File

@@ -0,0 +1,101 @@
package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/objects/layouts"
// DialogKind defines the semantic role of a dialog window.
type DialogKind int; const (
DialogInformation DialogKind = iota
DialogQuestion
DialogWarning
DialogError
)
// Dialog is a modal dialog window.
type Dialog struct {
tomo.Window
controlRow tomo.ContainerBox
}
type clickable interface {
OnClick (func ()) event.Cookie
}
// NewDialog creates a new dialog window given a dialog kind.
func NewDialog (kind DialogKind, parent tomo.Window, title, message string, options ...tomo.Object) (*Dialog, error) {
if title == "" {
switch kind {
case DialogInformation: title = "Information"
case DialogQuestion: title = "Question"
case DialogWarning: title = "Warning"
case DialogError: title = "Error"
}
}
dialog := &Dialog { }
if parent == nil {
window, err := tomo.NewWindow(image.Rectangle { })
if err != nil { return nil, err }
dialog.Window = window
} else {
window, err := parent.NewModal(image.Rectangle { })
if err != nil { return nil, err }
dialog.Window = window
}
var iconId theme.Icon
switch kind {
case DialogInformation: iconId = theme.IconStatusInformation
case DialogQuestion: iconId = theme.IconStatusQuestion
case DialogWarning: iconId = theme.IconStatusWarning
case DialogError: iconId = theme.IconStatusError
}
dialog.SetTitle(title)
icon := NewIcon(iconId, theme.IconSizeLarge)
messageText := NewLabel(message)
messageText.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
for _, option := range options {
if option, ok := option.(clickable); ok {
option.OnClick(dialog.Close)
}
}
dialog.controlRow = NewInnerContainer(layouts.ContractHorizontal, options...)
dialog.controlRow.SetAlign(tomo.AlignEnd, tomo.AlignEnd)
dialog.SetRoot(NewOuterContainer (
layouts.NewGrid([]bool { true }, []bool { true, false }),
NewInnerContainer(layouts.ContractHorizontal, icon, messageText),
dialog.controlRow))
return dialog, nil
}
// NewDialogOk creates a new dialog window with an OK option.
func NewDialogOk (kind DialogKind, parent tomo.Window, title, message string, onOk func ()) (*Dialog, error) {
okButton := NewButton("OK")
okButton.SetIcon(theme.IconStatusOkay)
okButton.OnClick(func () {
if onOk != nil { onOk() }
})
okButton.SetFocused(true)
return NewDialog(kind, parent, title, message, okButton)
}
// NewDialogOkCancel creates a new dialog window with OK and Cancel options.
func NewDialogOkCancel (kind DialogKind, parent tomo.Window, title, message string, onOk func ()) (*Dialog, error) {
cancelButton := NewButton("Cancel")
cancelButton.SetIcon(theme.IconStatusCancel)
okButton := NewButton("OK")
okButton.SetIcon(theme.IconStatusOkay)
okButton.OnClick(func () {
if onOk != nil { onOk() }
})
okButton.SetFocused(true)
return NewDialog(kind, parent, title, message, cancelButton, okButton)
}

2
go.mod
View File

@@ -2,6 +2,6 @@ module git.tebibyte.media/tomo/objects
go 1.20 go 1.20
require git.tebibyte.media/tomo/tomo v0.30.0 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
View File

@@ -1,5 +1,5 @@
git.tebibyte.media/tomo/tomo v0.30.0 h1:JoTklJ7yFVrzre4AwuKBMwzho9GomC9ySw354wDB4f4= git.tebibyte.media/tomo/tomo v0.31.0 h1:LHPpj3AWycochnC8F441aaRNS6Tq6w6WnBrp/LGjyhM=
git.tebibyte.media/tomo/tomo v0.30.0/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=

View File

@@ -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

View File

@@ -13,6 +13,7 @@ type TextInput struct {
text []rune text []rune
on struct { on struct {
enter event.FuncBroadcaster enter event.FuncBroadcaster
edit event.FuncBroadcaster
} }
} }
@@ -47,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()
@@ -93,7 +99,10 @@ 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) { func (this *TextInput) handleScroll (x, y float64) {

62
labelcheckbox.go Normal file
View 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 { false }))
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()
}
}

123
layouts/flow.go Normal file
View File

@@ -0,0 +1,123 @@
package layouts
import "image"
import "git.tebibyte.media/tomo/tomo"
// Flow is a grid layout where the number of rows and columns changes depending
// on the size of the container. It is designed to be used with an overflowing
// container. If the container does not overflow in the correct direction, the
// layout will behave like Contract.
type Flow bool
// FlowVertical is a vertical flow layout.
const FlowVertical Flow = true
// FlowHorizontal is a horizontal flow layout.
const FlowHorizontal Flow = false
func (flow Flow) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
// TODO: write down somewhere that layout minimums aren't taken into
// account when the respective direction is overflowed
return flow.fallback().MinimumSize(hints, boxes)
}
func (flow Flow) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
if flow.v() && !hints.OverflowY || flow.h() && !hints.OverflowX {
flow.fallback().Arrange(hints, boxes)
}
// find a minor size value that will fit all boxes
minorSize := 0
for _, box := range boxes {
boxSize := flow.minor(box.MinimumSize())
if boxSize > minorSize { minorSize = boxSize }
}
if minorSize == 0 { return }
minorSteps :=
(flow.deltaMinor(hints.Bounds) + flow.minor(hints.Gap)) /
(minorSize + flow.minor(hints.Gap))
// arrange
point := hints.Bounds.Min
index := 0
for index < len(boxes) {
// get a slice of boxes for this major step
stepIndexEnd := index + minorSteps
if stepIndexEnd > len(boxes) { stepIndexEnd = len(boxes) }
step := boxes[index:stepIndexEnd]
index += minorSteps
// find a major size that will fit all boxes on this major step
majorSize := 0
for _, box := range step {
boxSize := flow.major(box.MinimumSize())
if boxSize > majorSize { majorSize = boxSize }
}
if majorSize == 0 { continue }
// arrange all boxes on this major step
var size image.Point
size = flow.incrMajor(size, majorSize)
size = flow.incrMinor(size, minorSize)
for _, box := range step {
bounds := image.Rectangle { Min: point, Max: point.Add(size) }
box.SetBounds(bounds)
point = flow.incrMinor(point, minorSize + flow.minor(hints.Gap))
}
if flow.v() {
point.Y += majorSize + hints.Gap.Y
point.X = hints.Bounds.Min.X
} else {
point.X += majorSize + hints.Gap.X
point.Y = hints.Bounds.Min.Y
}
}
}
func (flow Flow) v () bool { return flow == FlowVertical }
func (flow Flow) h () bool { return flow == FlowHorizontal }
func (flow Flow) minor (point image.Point) int {
if flow.v() {
return point.X
} else {
return point.Y
}
}
func (flow Flow) major (point image.Point) int {
if flow.v() {
return point.Y
} else {
return point.X
}
}
func (flow Flow) incrMinor (point image.Point, delta int) image.Point {
if flow.v() {
point.X += delta
return point
} else {
point.Y += delta
return point
}
}
func (flow Flow) incrMajor (point image.Point, delta int) image.Point {
if flow.v() {
point.Y += delta
return point
} else {
point.X += delta
return point
}
}
func (flow Flow) deltaMinor (rectangle image.Rectangle) int {
if flow.v() {
return rectangle.Dx()
} else {
return rectangle.Dy()
}
}
func (flow Flow) fallback () tomo.Layout {
return Contract(flow)
}

View File

@@ -3,86 +3,93 @@ package layouts
import "image" import "image"
import "git.tebibyte.media/tomo/tomo" import "git.tebibyte.media/tomo/tomo"
type Row struct { } // Contract is a layout that arranges boxes in a simple row or column according
// to their minimum sizes.
type Contract bool
func (Row) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point { // ContractVertical is a vertical contracted layout.
dot := image.Point { } const ContractVertical Contract = true
for _, box := range boxes {
minimum := box.MinimumSize() // ContractHorizontal is a horizontal contracted layout.
dot.X += minimum.X const ContractHorizontal Contract = false
if dot.Y < minimum.Y {
dot.Y = minimum.Y func (contract Contract) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
if contract.v() {
dot := image.Point { }
for _, box := range boxes {
minimum := box.MinimumSize()
dot.Y += minimum.Y
if dot.X < minimum.X {
dot.X = minimum.X
}
}
dot.Y += hints.Gap.Y * (len(boxes) - 1)
return dot
} else {
dot := image.Point { }
for _, box := range boxes {
minimum := box.MinimumSize()
dot.X += minimum.X
if dot.Y < minimum.Y {
dot.Y = minimum.Y
}
}
dot.X += hints.Gap.X * (len(boxes) - 1)
return dot
}
}
func (contract Contract) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
if contract.v() {
dot := hints.Bounds.Min
for index, box := range boxes {
if index > 0 { dot.Y += hints.Gap.Y }
minimum := box.MinimumSize()
box.SetBounds(image.Rectangle {
Min: dot,
Max: dot.Add(image.Pt(hints.Bounds.Dx(), minimum.Y)),
})
dot.Y += minimum.Y
}
height := dot.Y - hints.Bounds.Min.Y
offset := 0
switch hints.AlignY {
case tomo.AlignMiddle:
offset = (hints.Bounds.Dy() - height) / 2
case tomo.AlignEnd:
offset = hints.Bounds.Dy() - height
}
for _, box := range boxes {
box.SetBounds(box.Bounds().Add(image.Pt(0, offset)))
}
} else {
dot := hints.Bounds.Min
for index, box := range boxes {
if index > 0 { dot.X += hints.Gap.X }
minimum := box.MinimumSize()
box.SetBounds(image.Rectangle {
Min: dot,
Max: dot.Add(image.Pt(minimum.X, hints.Bounds.Dy())),
})
dot.X += minimum.X
}
width := dot.X - hints.Bounds.Min.X
offset := 0
switch hints.AlignX {
case tomo.AlignMiddle:
offset = (hints.Bounds.Dx() - width) / 2
case tomo.AlignEnd:
offset = hints.Bounds.Dx() - width
}
for _, box := range boxes {
box.SetBounds(box.Bounds().Add(image.Pt(offset, 0)))
} }
} }
dot.X += hints.Gap.X * (len(boxes) - 1)
return dot
} }
func (Row) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) { func (contract Contract) v () bool { return contract == ContractVertical }
// TODO respect alignment value func (contract Contract) h () bool { return contract == ContractHorizontal }
dot := hints.Bounds.Min
for index, box := range boxes {
if index > 0 { dot.X += hints.Gap.X }
minimum := box.MinimumSize()
box.SetBounds(image.Rectangle {
Min: dot,
Max: dot.Add(image.Pt(minimum.X, hints.Bounds.Dy())),
})
dot.X += minimum.X
}
width := dot.X - hints.Bounds.Min.X
offset := 0
switch hints.AlignX {
case tomo.AlignMiddle:
offset = (hints.Bounds.Dx() - width) / 2
case tomo.AlignEnd:
offset = hints.Bounds.Dx() - width
}
for _, box := range boxes {
box.SetBounds(box.Bounds().Add(image.Pt(offset, 0)))
}
}
type Column struct { }
func (Column) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
dot := image.Point { }
for _, box := range boxes {
minimum := box.MinimumSize()
dot.Y += minimum.Y
if dot.X < minimum.X {
dot.X = minimum.X
}
}
dot.Y += hints.Gap.Y * (len(boxes) - 1)
return dot
}
func (Column) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
// TODO respect alignment value
dot := hints.Bounds.Min
for index, box := range boxes {
if index > 0 { dot.Y += hints.Gap.Y }
minimum := box.MinimumSize()
box.SetBounds(image.Rectangle {
Min: dot,
Max: dot.Add(image.Pt(hints.Bounds.Dx(), minimum.Y)),
})
dot.Y += minimum.Y
}
height := dot.Y - hints.Bounds.Min.Y
offset := 0
switch hints.AlignY {
case tomo.AlignMiddle:
offset = (hints.Bounds.Dy() - height) / 2
case tomo.AlignEnd:
offset = hints.Bounds.Dy() - height
}
for _, box := range boxes {
box.SetBounds(box.Bounds().Add(image.Pt(0, offset)))
}
}

100
numberinput.go Normal file
View 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()
}

View File

@@ -98,9 +98,9 @@ func (this *Scrollbar) SetValue (value float64) {
position := trackLength * value position := trackLength * value
point := this.layout.linked.ContentBounds().Min point := this.layout.linked.ContentBounds().Min
if this.layout.vertical { if this.layout.vertical {
point.Y = int(position) point.Y = -int(position)
} else { } else {
point.X = int(position) point.X = -int(position)
} }
this.layout.linked.ScrollTo(point) this.layout.linked.ScrollTo(point)
@@ -173,15 +173,15 @@ func (this *Scrollbar) handleMouseDown (button input.Button) {
} }
case input.ButtonMiddle: case input.ButtonMiddle:
if above { if above {
this.scrollBy(-this.pageSize())
} else {
this.scrollBy(this.pageSize()) this.scrollBy(this.pageSize())
} else {
this.scrollBy(-this.pageSize())
} }
case input.ButtonRight: case input.ButtonRight:
if above { if above {
this.scrollBy(-this.stepSize())
} else {
this.scrollBy(this.stepSize()) this.scrollBy(this.stepSize())
} else {
this.scrollBy(-this.stepSize())
} }
} }
} }
@@ -200,7 +200,7 @@ func (this *Scrollbar) handleScroll (x, y float64) {
if this.layout.linked == nil { return } if this.layout.linked == nil { return }
this.layout.linked.ScrollTo ( this.layout.linked.ScrollTo (
this.layout.linked.ContentBounds().Min. this.layout.linked.ContentBounds().Min.
Add(image.Pt(int(x), int(y)))) Sub(image.Pt(int(x), int(y))))
} }
func (this *Scrollbar) drag () { func (this *Scrollbar) drag () {
@@ -327,7 +327,7 @@ func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
} else { } else {
handleOffset = image.Pt(int(handlePosition), 0) handleOffset = image.Pt(int(handlePosition), 0)
} }
handle = handle.Add(handleOffset).Add(gutter.Min) handle = handle.Sub(handleOffset).Add(gutter.Min)
// place handle // place handle
boxes[0].SetBounds(handle) boxes[0].SetBounds(handle)

View File

@@ -104,11 +104,34 @@ func (this *ScrollContainer) SetRoot (root tomo.ContentBox) {
} }
} }
// SetValue sets the horizontal and vertical scrollbar values where 0 is all the
// way to the left/top, and 1 is all the way to the right/bottom.
func (this *ScrollContainer) SetValue (x, y float64) {
if this.layout.horizontal != nil {
this.layout.horizontal.SetValue(x)
}
if this.layout.vertical != nil {
this.layout.vertical.SetValue(y)
}
}
// Value returns the horizontal and vertical scrollbar values where 0 is all the
// way to the left/top, and 1 is all the way to the right/bottom.
func (this *ScrollContainer) Value () (x, y float64) {
if this.layout.horizontal != nil {
x = this.layout.horizontal.Value()
}
if this.layout.vertical != nil {
y = this.layout.vertical.Value()
}
return x, y
}
func (this *ScrollContainer) handleScroll (x, y float64) { func (this *ScrollContainer) handleScroll (x, y float64) {
if this.layout.root == nil { return } if this.layout.root == nil { return }
this.layout.root.ScrollTo ( this.layout.root.ScrollTo (
this.layout.root.ContentBounds().Min. this.layout.root.ContentBounds().Min.
Add(image.Pt(int(x), int(y)))) Sub(image.Pt(int(x), int(y))))
} }
type scrollContainerLayout struct { type scrollContainerLayout struct {

View File

@@ -13,9 +13,10 @@ type Slider struct {
layout sliderLayout layout sliderLayout
dragging bool dragging bool
dragOffset image.Point dragOffset image.Point
step float64
on struct { on struct {
valueChange event.FuncBroadcaster slide event.FuncBroadcaster
} }
} }
@@ -34,6 +35,7 @@ func newSlider (orient string, value float64) *Slider {
layout: sliderLayout { layout: sliderLayout {
vertical: orient == "vertical", vertical: orient == "vertical",
}, },
step: 0.05,
} }
this.Add(this.handle) this.Add(this.handle)
@@ -49,6 +51,7 @@ func newSlider (orient string, value float64) *Slider {
this.OnMouseDown(this.handleMouseDown) this.OnMouseDown(this.handleMouseDown)
this.OnMouseUp(this.handleMouseUp) this.OnMouseUp(this.handleMouseUp)
this.OnMouseMove(this.handleMouseMove) this.OnMouseMove(this.handleMouseMove)
this.OnScroll(this.handleScroll)
theme.Apply(this.handle, theme.R("objects", "SliderHandle", orient)) theme.Apply(this.handle, theme.R("objects", "SliderHandle", orient))
theme.Apply(this, theme.R("objects", "Slider", orient)) theme.Apply(this, theme.R("objects", "Slider", orient))
return this return this
@@ -71,7 +74,6 @@ func (this *Slider) SetValue (value float64) {
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. // Value returns the value of the slider between 0 and 1.
@@ -79,10 +81,10 @@ func (this *Slider) Value () float64 {
return this.layout.value return this.layout.value
} }
// OnValueChange specifies a function to be called when the slider's value // OnValueChange specifies a function to be called when the user moves the
// changes. // slider.
func (this *Slider) OnValueChange (callback func ()) event.Cookie { func (this *Slider) OnSlide (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback) return this.on.slide.Connect(callback)
} }
func (this *Slider) handleKeyDown (key input.Key, numpad bool) { func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
@@ -99,16 +101,20 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
} else { } else {
this.SetValue(this.Value() - increment) this.SetValue(this.Value() - increment)
} }
this.on.slide.Broadcast()
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() + increment) this.SetValue(this.Value() + increment)
} }
this.on.slide.Broadcast()
case input.KeyHome: case input.KeyHome:
this.SetValue(0) this.SetValue(0)
this.on.slide.Broadcast()
case input.KeyEnd: case input.KeyEnd:
this.SetValue(1) this.SetValue(1)
this.on.slide.Broadcast()
} }
} }
@@ -139,14 +145,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() - this.step)
this.on.slide.Broadcast()
} else { } else {
this.SetValue(this.Value() + 0.05) this.SetValue(this.Value() + this.step)
this.on.slide.Broadcast()
} }
} }
} }
@@ -161,6 +171,12 @@ func (this *Slider) handleMouseMove () {
this.drag() this.drag()
} }
func (this *Slider) handleScroll (x, y float64) {
delta := (x + y) * 0.005
this.SetValue(this.Value() + delta)
this.on.slide.Broadcast()
}
func (this *Slider) drag () { func (this *Slider) drag () {
pointer := this.MousePosition().Sub(this.dragOffset) pointer := this.MousePosition().Sub(this.dragOffset)
gutter := this.InnerBounds() gutter := this.InnerBounds()
@@ -176,6 +192,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 {