Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6389556199 | |||
| 06d99b2790 | |||
| 6ab689b5c1 | |||
| 6497da69ed | |||
| 34794f4c77 | |||
| 25625e9a99 | |||
| 4cea0aa0bd | |||
| 1cb9e8091e | |||
| 68d49e1b02 | |||
| f025ec3d8a | |||
| 6fad52b335 | |||
| 8134069e1f | |||
| dca1eaefb5 | |||
| 87e81c00d3 | |||
| 224ca25000 |
13
button.go
13
button.go
@@ -1,7 +1,6 @@
|
||||
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"
|
||||
@@ -29,7 +28,7 @@ func NewButton (text string) *Button {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
label: NewLabel(text),
|
||||
}
|
||||
theme.Apply(box, theme.R("objects", "Button", ""))
|
||||
tomo.Apply(box, tomo.R("objects", "Button", ""))
|
||||
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
|
||||
box.SetLayout(buttonLayout)
|
||||
box.SetText(text)
|
||||
@@ -49,7 +48,7 @@ func NewButton (text string) *Button {
|
||||
func (this *Button) SetText (text string) {
|
||||
this.label.SetText(text)
|
||||
if this.labelActive && text == "" {
|
||||
this.Delete(this.label)
|
||||
this.Remove(this.label)
|
||||
this.labelActive = false
|
||||
}
|
||||
if !this.labelActive && text != "" {
|
||||
@@ -61,11 +60,11 @@ func (this *Button) SetText (text string) {
|
||||
|
||||
// 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) }
|
||||
func (this *Button) SetIcon (id tomo.Icon) {
|
||||
if this.icon != nil { this.Remove(this.icon) }
|
||||
|
||||
var icon *Icon; if id != theme.IconUnknown {
|
||||
icon = NewIcon(id, theme.IconSizeSmall)
|
||||
var icon *Icon; if id != tomo.IconUnknown {
|
||||
icon = NewIcon(id, tomo.IconSizeSmall)
|
||||
}
|
||||
this.icon = icon
|
||||
|
||||
|
||||
10
checkbox.go
10
checkbox.go
@@ -1,7 +1,6 @@
|
||||
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"
|
||||
|
||||
@@ -19,7 +18,7 @@ func NewCheckbox (value bool) *Checkbox {
|
||||
box := &Checkbox {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
theme.Apply(box, theme.R("objects", "Checkbox", ""))
|
||||
tomo.Apply(box, tomo.R("objects", "Checkbox", ""))
|
||||
box.SetValue(false)
|
||||
|
||||
box.OnMouseUp(box.handleMouseUp)
|
||||
@@ -32,11 +31,10 @@ func NewCheckbox (value bool) *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))
|
||||
// FIXME: we need checkbox icons
|
||||
this.SetTextureCenter(tomo.Icon("").Texture(tomo.IconSizeSmall))
|
||||
} else {
|
||||
this.SetTexture(nil)
|
||||
this.SetTextureCenter(nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
17
container.go
17
container.go
@@ -1,7 +1,6 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
|
||||
// Container is an object that can contain other objects. It can be used as a
|
||||
// primitive for building more complex layouts. It has two variants: an outer
|
||||
@@ -23,17 +22,27 @@ func newContainer (layout tomo.Layout, children ...tomo.Object) *Container {
|
||||
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 {
|
||||
this := newContainer(layout, children...)
|
||||
theme.Apply(this, theme.R("objects", "Container", "outer"))
|
||||
tomo.Apply(this, tomo.R("objects", "Container", "outer"))
|
||||
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...)
|
||||
tomo.Apply(this, tomo.R("objects", "Container", "sunken"))
|
||||
return this
|
||||
}
|
||||
|
||||
// NewInnerContainer creates a new container that has no padding around it.
|
||||
func NewInnerContainer (layout tomo.Layout, children ...tomo.Object) *Container {
|
||||
this := newContainer(layout, children...)
|
||||
theme.Apply(this, theme.R("objects", "Container", "inner"))
|
||||
tomo.Apply(this, tomo.R("objects", "Container", "inner"))
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
103
dialog.go
Normal file
103
dialog.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
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 tomo.Icon
|
||||
switch kind {
|
||||
case DialogInformation: iconId = tomo.IconDialogInformation
|
||||
case DialogQuestion: iconId = tomo.IconDialogQuestion
|
||||
case DialogWarning: iconId = tomo.IconDialogWarning
|
||||
case DialogError: iconId = tomo.IconDialogError
|
||||
}
|
||||
dialog.SetTitle(title)
|
||||
icon := NewIcon(iconId, tomo.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")
|
||||
// FIXME: need dialog accept/reject action icons
|
||||
// okButton.SetIcon(tomo.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")
|
||||
// FIXME: need dialog accept/reject action icons
|
||||
// cancelButton.SetIcon(tomo.IconStatusCancel)
|
||||
|
||||
okButton := NewButton("OK")
|
||||
// FIXME: need dialog accept/reject action icons
|
||||
// okButton.SetIcon(tomo.IconStatusOkay)
|
||||
okButton.OnClick(func () {
|
||||
if onOk != nil { onOk() }
|
||||
})
|
||||
okButton.SetFocused(true)
|
||||
|
||||
return NewDialog(kind, parent, title, message, cancelButton, okButton)
|
||||
}
|
||||
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.31.0
|
||||
require git.tebibyte.media/tomo/tomo v0.33.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.31.0 h1:LHPpj3AWycochnC8F441aaRNS6Tq6w6WnBrp/LGjyhM=
|
||||
git.tebibyte.media/tomo/tomo v0.31.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
||||
git.tebibyte.media/tomo/tomo v0.33.0 h1:BBm1oRsogBLeqVKeevNqG9RPCOdmbGeiQM/9hd2GHE8=
|
||||
git.tebibyte.media/tomo/tomo v0.33.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=
|
||||
|
||||
@@ -2,7 +2,6 @@ package objects
|
||||
|
||||
import "fmt"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
|
||||
// Heading is a label that denotes the start of some section of content. It can
|
||||
// have a level from 0 to 2, with 0 being the most prominent and 2 being the
|
||||
@@ -12,11 +11,11 @@ type Heading struct {
|
||||
}
|
||||
|
||||
// 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 > 2 { level = 2 }
|
||||
this := &Label { TextBox: tomo.NewTextBox() }
|
||||
theme.Apply(this, theme.R("objects", "Heading", fmt.Sprint(level)))
|
||||
this := &Heading { TextBox: tomo.NewTextBox() }
|
||||
tomo.Apply(this, tomo.R("objects", "Heading", fmt.Sprint(level)))
|
||||
this.SetText(text)
|
||||
return this
|
||||
}
|
||||
|
||||
13
icon.go
13
icon.go
@@ -3,7 +3,6 @@ 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.
|
||||
@@ -12,27 +11,27 @@ type Icon struct {
|
||||
}
|
||||
|
||||
// NewIcon creates a new icon from an icon ID.
|
||||
func NewIcon (id theme.Icon, size theme.IconSize) *Icon {
|
||||
func NewIcon (id tomo.Icon, size tomo.IconSize) *Icon {
|
||||
this := &Icon {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
theme.Apply(this, theme.R("objects", "Icon", size.String()))
|
||||
tomo.Apply(this, tomo.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 {
|
||||
func NewMimeIcon (mime data.Mime, size tomo.IconSize) *Icon {
|
||||
this := &Icon {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
theme.Apply(this, theme.R("objects", "Icon", size.String()))
|
||||
this.SetTexture(theme.MimeIcon(mime, size))
|
||||
tomo.Apply(this, tomo.R("objects", "Icon", size.String()))
|
||||
this.SetTexture(tomo.MimeIcon(mime, size))
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Icon) SetTexture (texture canvas.Texture) {
|
||||
this.Box.SetTexture(texture)
|
||||
this.Box.SetTextureCenter(texture)
|
||||
if texture == nil {
|
||||
this.SetMinimumSize(image.Pt(0, 0))
|
||||
} else {
|
||||
|
||||
3
label.go
3
label.go
@@ -1,7 +1,6 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
|
||||
// Label is a simple text label.
|
||||
type Label struct {
|
||||
@@ -11,7 +10,7 @@ type Label struct {
|
||||
// NewLabel creates a new text label.
|
||||
func NewLabel (text string) *Label {
|
||||
this := &Label { TextBox: tomo.NewTextBox() }
|
||||
theme.Apply(this, theme.R("objects", "Label", ""))
|
||||
tomo.Apply(this, tomo.R("objects", "Label", ""))
|
||||
this.SetText(text)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
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"
|
||||
@@ -21,11 +20,11 @@ func NewLabelCheckbox (value bool, text string) *LabelCheckbox {
|
||||
checkbox: NewCheckbox(value),
|
||||
label: NewLabel(text),
|
||||
}
|
||||
theme.Apply(box, theme.R("objects", "LabelCheckbox", ""))
|
||||
tomo.Apply(box, tomo.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.SetLayout(layouts.NewGrid([]bool { false, true }, []bool { false }))
|
||||
|
||||
box.OnMouseUp(box.handleMouseUp)
|
||||
box.label.OnMouseUp(box.handleMouseUp)
|
||||
|
||||
123
layouts/flow.go
Normal file
123
layouts/flow.go
Normal 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)
|
||||
}
|
||||
@@ -3,86 +3,93 @@ package layouts
|
||||
import "image"
|
||||
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 {
|
||||
dot := image.Point { }
|
||||
for _, box := range boxes {
|
||||
minimum := box.MinimumSize()
|
||||
dot.X += minimum.X
|
||||
if dot.Y < minimum.Y {
|
||||
dot.Y = minimum.Y
|
||||
// ContractVertical is a vertical contracted layout.
|
||||
const ContractVertical Contract = true
|
||||
|
||||
// ContractHorizontal is a horizontal contracted layout.
|
||||
const ContractHorizontal Contract = false
|
||||
|
||||
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) {
|
||||
// TODO respect alignment value
|
||||
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)))
|
||||
}
|
||||
}
|
||||
func (contract Contract) v () bool { return contract == ContractVertical }
|
||||
func (contract Contract) h () bool { return contract == ContractHorizontal }
|
||||
|
||||
@@ -3,7 +3,6 @@ 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"
|
||||
@@ -29,13 +28,13 @@ func NewNumberInput (value float64) *NumberInput {
|
||||
increment: NewButton(""),
|
||||
decrement: NewButton(""),
|
||||
}
|
||||
theme.Apply(box, theme.R("objects", "NumberInput", ""))
|
||||
tomo.Apply(box, tomo.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.increment.SetIcon(tomo.IconListAdd) // FIXME: need incr/decrment icons
|
||||
box.decrement.SetIcon(tomo.IconListRemove)
|
||||
|
||||
box.SetValue(value)
|
||||
|
||||
|
||||
23
scrollbar.go
23
scrollbar.go
@@ -2,7 +2,6 @@ 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"
|
||||
|
||||
@@ -46,8 +45,8 @@ func newScrollbar (orient string) *Scrollbar {
|
||||
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))
|
||||
tomo.Apply(this.handle, tomo.R("objects", "SliderHandle", orient))
|
||||
tomo.Apply(this, tomo.R("objects", "Slider", orient))
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -98,9 +97,9 @@ func (this *Scrollbar) SetValue (value float64) {
|
||||
position := trackLength * value
|
||||
point := this.layout.linked.ContentBounds().Min
|
||||
if this.layout.vertical {
|
||||
point.Y = int(position)
|
||||
point.Y = -int(position)
|
||||
} else {
|
||||
point.X = int(position)
|
||||
point.X = -int(position)
|
||||
}
|
||||
this.layout.linked.ScrollTo(point)
|
||||
|
||||
@@ -173,15 +172,15 @@ func (this *Scrollbar) handleMouseDown (button input.Button) {
|
||||
}
|
||||
case input.ButtonMiddle:
|
||||
if above {
|
||||
this.scrollBy(-this.pageSize())
|
||||
} else {
|
||||
this.scrollBy(this.pageSize())
|
||||
} else {
|
||||
this.scrollBy(-this.pageSize())
|
||||
}
|
||||
case input.ButtonRight:
|
||||
if above {
|
||||
this.scrollBy(-this.stepSize())
|
||||
} else {
|
||||
this.scrollBy(this.stepSize())
|
||||
} else {
|
||||
this.scrollBy(-this.stepSize())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -200,7 +199,7 @@ func (this *Scrollbar) handleScroll (x, y float64) {
|
||||
if this.layout.linked == nil { return }
|
||||
this.layout.linked.ScrollTo (
|
||||
this.layout.linked.ContentBounds().Min.
|
||||
Add(image.Pt(int(x), int(y))))
|
||||
Sub(image.Pt(int(x), int(y))))
|
||||
}
|
||||
|
||||
func (this *Scrollbar) drag () {
|
||||
@@ -241,7 +240,7 @@ func (this *Scrollbar) pageSize () int {
|
||||
|
||||
func (this *Scrollbar) stepSize () int {
|
||||
// FIXME: this should not be hardcoded, need to get base font metrics
|
||||
// from theme somehow. should be (emspace, lineheight)
|
||||
// from tomo.somehow. should be (emspace, lineheight)
|
||||
return 16
|
||||
}
|
||||
|
||||
@@ -327,7 +326,7 @@ func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
} else {
|
||||
handleOffset = image.Pt(int(handlePosition), 0)
|
||||
}
|
||||
handle = handle.Add(handleOffset).Add(gutter.Min)
|
||||
handle = handle.Sub(handleOffset).Add(gutter.Min)
|
||||
|
||||
// place handle
|
||||
boxes[0].SetBounds(handle)
|
||||
|
||||
@@ -3,7 +3,6 @@ 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 (
|
||||
@@ -61,7 +60,7 @@ func NewScrollContainer (sides ScrollSide) *ScrollContainer {
|
||||
}
|
||||
this.CaptureScroll(true)
|
||||
this.OnScroll(this.handleScroll)
|
||||
theme.Apply(this, theme.R("objects", "ScrollContainer", sides.String()))
|
||||
tomo.Apply(this, tomo.R("objects", "ScrollContainer", sides.String()))
|
||||
this.SetLayout(this.layout)
|
||||
return this
|
||||
}
|
||||
@@ -71,8 +70,8 @@ func NewScrollContainer (sides ScrollSide) *ScrollContainer {
|
||||
// one.
|
||||
func (this *ScrollContainer) SetRoot (root tomo.ContentBox) {
|
||||
if this.layout.root != nil {
|
||||
// delete root and close cookies
|
||||
this.Delete(this.layout.root)
|
||||
// remove root and close cookies
|
||||
this.Remove(this.layout.root)
|
||||
if this.horizontalCookie != nil {
|
||||
this.horizontalCookie.Close()
|
||||
this.horizontalCookie = nil
|
||||
@@ -104,11 +103,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) {
|
||||
if this.layout.root == nil { return }
|
||||
this.layout.root.ScrollTo (
|
||||
this.layout.root.ContentBounds().Min.
|
||||
Add(image.Pt(int(x), int(y))))
|
||||
Sub(image.Pt(int(x), int(y))))
|
||||
}
|
||||
|
||||
type scrollContainerLayout struct {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
|
||||
// Separator is a line for visually separating elements.
|
||||
type Separator struct {
|
||||
@@ -13,6 +12,6 @@ func NewSeparator () *Separator {
|
||||
this := &Separator {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
theme.Apply(this, theme.R("objects", "Separator", ""))
|
||||
tomo.Apply(this, tomo.R("objects", "Separator", ""))
|
||||
return this
|
||||
}
|
||||
|
||||
22
slider.go
22
slider.go
@@ -2,7 +2,6 @@ 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"
|
||||
|
||||
@@ -13,6 +12,7 @@ type Slider struct {
|
||||
layout sliderLayout
|
||||
dragging bool
|
||||
dragOffset image.Point
|
||||
step float64
|
||||
|
||||
on struct {
|
||||
slide event.FuncBroadcaster
|
||||
@@ -34,6 +34,7 @@ func newSlider (orient string, value float64) *Slider {
|
||||
layout: sliderLayout {
|
||||
vertical: orient == "vertical",
|
||||
},
|
||||
step: 0.05,
|
||||
}
|
||||
|
||||
this.Add(this.handle)
|
||||
@@ -49,8 +50,9 @@ func newSlider (orient string, value float64) *Slider {
|
||||
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))
|
||||
this.OnScroll(this.handleScroll)
|
||||
tomo.Apply(this.handle, tomo.R("objects", "SliderHandle", orient))
|
||||
tomo.Apply(this, tomo.R("objects", "Slider", orient))
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -98,16 +100,20 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
|
||||
} else {
|
||||
this.SetValue(this.Value() - increment)
|
||||
}
|
||||
this.on.slide.Broadcast()
|
||||
case input.KeyDown, input.KeyRight:
|
||||
if this.Modifiers().Alt {
|
||||
this.SetValue(1)
|
||||
} else {
|
||||
this.SetValue(this.Value() + increment)
|
||||
}
|
||||
this.on.slide.Broadcast()
|
||||
case input.KeyHome:
|
||||
this.SetValue(0)
|
||||
this.on.slide.Broadcast()
|
||||
case input.KeyEnd:
|
||||
this.SetValue(1)
|
||||
this.on.slide.Broadcast()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,10 +151,10 @@ func (this *Slider) handleMouseDown (button input.Button) {
|
||||
}
|
||||
case input.ButtonRight:
|
||||
if above {
|
||||
this.SetValue(this.Value() - 0.05)
|
||||
this.SetValue(this.Value() - this.step)
|
||||
this.on.slide.Broadcast()
|
||||
} else {
|
||||
this.SetValue(this.Value() + 0.05)
|
||||
this.SetValue(this.Value() + this.step)
|
||||
this.on.slide.Broadcast()
|
||||
}
|
||||
}
|
||||
@@ -164,6 +170,12 @@ func (this *Slider) handleMouseMove () {
|
||||
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 () {
|
||||
pointer := this.MousePosition().Sub(this.dragOffset)
|
||||
gutter := this.InnerBounds()
|
||||
|
||||
@@ -3,7 +3,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"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
|
||||
@@ -20,7 +19,7 @@ type TextInput struct {
|
||||
// NewTextInput creates a new text input containing the specified text.
|
||||
func NewTextInput (text string) *TextInput {
|
||||
this := &TextInput { TextBox: tomo.NewTextBox() }
|
||||
theme.Apply(this, theme.R("objects", "TextInput", ""))
|
||||
tomo.Apply(this, tomo.R("objects", "TextInput", ""))
|
||||
this.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
||||
this.SetText(text)
|
||||
this.SetFocusable(true)
|
||||
@@ -60,8 +59,8 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
|
||||
sel := modifiers.Shift
|
||||
changed := false
|
||||
|
||||
// TODO all this (except editing stuff) really should be moved into the
|
||||
// backend
|
||||
// TODO all dot control (movement, selection, etc) should be done in the
|
||||
// backend. (editing should be done here, though)
|
||||
|
||||
switch {
|
||||
case key == input.KeyEnter:
|
||||
@@ -105,6 +104,14 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// Type types a character at the current dot position.
|
||||
func (this *TextInput) Type (char rune) {
|
||||
dot := this.Dot()
|
||||
this.text, dot = text.Type(this.text, dot, rune(char))
|
||||
this.Select(dot)
|
||||
this.SetText(string(this.text))
|
||||
}
|
||||
|
||||
func (this *TextInput) handleScroll (x, y float64) {
|
||||
this.ScrollTo(this.ContentBounds().Min.Add(image.Pt(int(x), int(y))))
|
||||
}
|
||||
@@ -2,7 +2,6 @@ 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 {
|
||||
@@ -12,7 +11,7 @@ type TextView struct {
|
||||
// NewTextView creates a new text view.
|
||||
func NewTextView (text string) *TextView {
|
||||
this := &TextView { TextBox: tomo.NewTextBox() }
|
||||
theme.Apply(this, theme.R("objects", "TextView", ""))
|
||||
tomo.Apply(this, tomo.R("objects", "TextView", ""))
|
||||
this.SetFocusable(true)
|
||||
this.SetSelectable(true)
|
||||
this.SetText(text)
|
||||
|
||||
Reference in New Issue
Block a user