5 Commits

Author SHA1 Message Date
552df5a9cd Add doc comments to the cut layout 2023-08-13 21:52:09 -04:00
e09761dd98 Added "cut" layout 2023-08-13 21:47:51 -04:00
992c242ba2 Add slider 2023-08-12 01:02:48 -04:00
ac70dce00f Fix separator 2023-08-12 01:02:26 -04:00
c0bfa76ba6 I keep forgetting to commit stuff 2023-08-10 17:52:01 -04:00
11 changed files with 597 additions and 10 deletions

View File

@@ -16,7 +16,7 @@ type Button struct {
// NewButton creates a new button with the specified text.
func NewButton (text string) *Button {
box := &Button { TextBox: tomo.NewTextBox() }
theme.Apply(box, theme.R("objects", "Button"))
theme.Apply(box, theme.R("objects", "Button", ""))
box.SetText(text)
box.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
box.OnMouseUp(box.handleMouseUp)

39
container.go Normal file
View File

@@ -0,0 +1,39 @@
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
// container, and an inner container. The outer container has padding around
// its edges, whereas the inner container does not. The container will have a
// corresponding object role variation of either "outer" or "inner".
type Container struct {
tomo.ContainerBox
}
func newContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := &Container {
ContainerBox: tomo.NewContainerBox(),
}
this.SetLayout(layout)
for _, child := range children {
this.Add(child)
}
return this
}
// NewOuterContainer creates a new container that has padding around it.
func NewOuterContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := newContainer(layout, children...)
theme.Apply(this, theme.R("objects", "Container", "outer"))
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"))
return this
}

4
go.mod
View File

@@ -2,6 +2,6 @@ module git.tebibyte.media/tomo/objects
go 1.20
require git.tebibyte.media/tomo/tomo v0.20.0
require git.tebibyte.media/tomo/tomo v0.23.0
require golang.org/x/image v0.8.0 // indirect
require golang.org/x/image v0.11.0 // indirect

10
go.sum
View File

@@ -1,10 +1,10 @@
git.tebibyte.media/tomo/tomo v0.20.0 h1:dJJWmCAj/8XtbxjiCkgykP2vNCeP5zuvMEKGChbQ0AI=
git.tebibyte.media/tomo/tomo v0.20.0/go.mod h1:lTwjpiHbP4UN/kFw+6FwhG600B+PMKVtMOr7wpd5IUY=
git.tebibyte.media/tomo/tomo v0.23.0 h1:OZwI4oLKMP7JqFc98VxBoRxwjL+W9NyTQZLB/m1BvaA=
git.tebibyte.media/tomo/tomo v0.23.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=
golang.org/x/image v0.8.0 h1:agUcRXV/+w6L9ryntYYsF2x9fQTMd4T8fiiYXAVW6Jg=
golang.org/x/image v0.8.0/go.mod h1:PwLxp3opCYg4WR2WO9P0L6ESnsD6bLTWcw8zanLMVFM=
golang.org/x/image v0.11.0 h1:ds2RoQvBvYTiJkwpSFDwCcDFNX7DqjL2WsUgTNk0Ooo=
golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -27,7 +27,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=

23
heading.go Normal file
View File

@@ -0,0 +1,23 @@
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
// most subtle. The level is described in the role variation.
type Heading struct {
tomo.TextBox
}
// NewHeading creates a new section heading. The level can be from 0 to 2.
func NewHeading (level int, text string) *Label {
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.SetText(text)
return this
}

View File

@@ -18,7 +18,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"))
theme.Apply(this, theme.R("objects", "TextInput", ""))
this.SetText(text)
this.SetFocusable(true)
this.SetSelectable(true)

View File

@@ -11,7 +11,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"))
theme.Apply(this, theme.R("objects", "Label", ""))
this.SetText(text)
return this
}

212
layouts/cut.go Normal file
View File

@@ -0,0 +1,212 @@
package layouts
import "image"
import "git.tebibyte.media/tomo/tomo"
// Cut is a layout that can be divided into smaller and smaller sections.
type Cut struct {
branches [2]*Cut
expand [2]bool
vertical bool
}
// Vertical divides the layout in equal halves vertically.
func (this *Cut) Vertical () (top, bottom *Cut) {
this.fill()
this.even()
this.vertical = true
return this.Branches()
}
// Top divides the layout vertically, expanding the top half and contracting the
// bottom half.
func (this *Cut) Top () (top, bottom *Cut) {
this.fill()
this.first()
this.vertical = true
return this.Branches()
}
// Bottom divides the layout vertically, expanding the bottom half and
// contracting the top half.
func (this *Cut) Bottom () (top, bottom *Cut) {
this.fill()
this.second()
this.vertical = true
return this.Branches()
}
// Horizontal divides the layout in equal halves horizontally.
func (this *Cut) Horizontal () (top, bottom *Cut) {
this.fill()
this.even()
return this.Branches()
}
// Left divides the layout horizontally, expanding the left half and contracting
// the right half.
func (this *Cut) Left () (top, bottom *Cut) {
this.fill()
this.first()
return this.Branches()
}
// Right divides the layout horizontally, expanding the right half and
// contracting the left half.
func (this *Cut) Right () (top, bottom *Cut) {
this.fill()
this.second()
return this.Branches()
}
// Branches returns the two halves of this layout.
func (this *Cut) Branches () (first, second *Cut) {
return this.branches[0], this.branches[1]
}
func (this *Cut) real () bool {
return this != nil && this.branches[0] != nil && this.branches[1] != nil
}
func (this *Cut) fill () {
this.branches[0] = &Cut { }
this.branches[1] = &Cut { }
}
func (this *Cut) first () {
this.expand[0] = true
this.expand[1] = false
}
func (this *Cut) second () {
this.expand[0] = false
this.expand[1] = true
}
func (this *Cut) even () {
this.expand[0] = true
this.expand[1] = true
}
func (this *Cut) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
size, _ := this.minimumSize(hints, boxes)
return size
}
func (this *Cut) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
this.arrange(hints, boxes)
}
func (this *Cut) minimumSize (hints tomo.LayoutHints, boxes []tomo.Box) (image.Point, []tomo.Box) {
size := image.Point { }
for index, branch := range this.branches {
if len(boxes) == 0 { break }
var point image.Point
if branch.real() {
point, boxes = branch.minimumSize(hints, boxes)
} else {
point = boxes[0].MinimumSize()
boxes = boxes[1:]
}
if this.vertical {
if point.X > size.X { size.X = point.X }
if index > 0 { size.Y += hints.Gap.Y }
size.Y += point.Y
} else {
if point.Y > size.Y { size.Y = point.Y }
if index > 0 { size.X += hints.Gap.X }
size.X += point.X
}
}
return size, boxes
}
func (this *Cut) arrange (hints tomo.LayoutHints, boxes []tomo.Box) []tomo.Box {
// collect minimum sizes and physical endpoints
var minimums [2]image.Point
var leaves [2]tomo.Box
var nBranches int
remaining := boxes
for index, branch := range this.branches {
if branch.real() {
minimums[index], remaining = branch.minimumSize(hints, remaining)
} else {
if len(remaining) == 0 { break }
leaves[index] = remaining[0]
minimums[index] = remaining[0].MinimumSize()
remaining = remaining[1:]
}
nBranches ++
}
// determine the amount of space to divide among expanding branches
gaps := nBranches - 1
var freeSpace float64; if this.vertical {
freeSpace = float64(hints.Bounds.Dy() - hints.Gap.Y * gaps)
} else {
freeSpace = float64(hints.Bounds.Dx() - hints.Gap.X * gaps)
}
var nExpanding float64
for index, minimum := range minimums {
if this.expand[index] {
nExpanding ++
} else if this.vertical {
freeSpace -= float64(minimum.Y)
} else {
freeSpace -= float64(minimum.X)
}
}
expandingSize := freeSpace / nExpanding
// calculate the size and position of branches
var bounds [2]image.Rectangle
x := float64(hints.Bounds.Min.X)
y := float64(hints.Bounds.Min.Y)
for index, minimum := range minimums {
// get size along significant axis
var size float64; if this.expand[index] {
size = expandingSize
} else if this.vertical {
size = float64(minimum.Y)
} else {
size = float64(minimum.X)
}
// figure out bounds from size
if this.vertical {
bounds[index].Max = image.Pt (
int(hints.Bounds.Dx()),
int(size))
} else {
bounds[index].Max = image.Pt (
int(size),
int(hints.Bounds.Dy()))
}
bounds[index] = bounds[index].Add(image.Pt(int(x), int(y)))
// move along
if this.vertical {
y += float64(hints.Gap.Y) + size
} else {
x += float64(hints.Gap.X) + size
}
}
// apply the size and position
for index, bound := range bounds {
if leaves[index] != nil {
leaves[index].SetBounds(bound)
boxes = boxes[1:]
} else if this.branches[index] != nil {
newHints := hints
newHints.Bounds = bound
boxes = this.branches[index].arrange(newHints, boxes)
}
}
return boxes
}

88
layouts/layouts.go Normal file
View File

@@ -0,0 +1,88 @@
package layouts
import "image"
import "git.tebibyte.media/tomo/tomo"
type Row struct { }
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
}
}
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)))
}
}

19
separator.go Normal file
View File

@@ -0,0 +1,19 @@
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 {
tomo.Box
}
// NewSeparator creates a new separator line.
func NewSeparator () *Separator {
this := &Separator {
Box: tomo.NewBox(),
}
theme.Apply(this, theme.R("objects", "Separator", ""))
return this
}

206
slider.go Normal file
View File

@@ -0,0 +1,206 @@
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"
// UNDER CONSTRUCTION!
type Slider struct {
tomo.ContainerBox
handle *SliderHandle
layout sliderLayout
dragging bool
dragOffset image.Point
on struct {
valueChange event.FuncBroadcaster
}
}
type SliderHandle struct {
tomo.Box
}
func newSlider (orient string, value float64) *Slider {
this := &Slider {
ContainerBox: tomo.NewContainerBox(),
handle: &SliderHandle {
Box: tomo.NewBox(),
},
layout: sliderLayout {
vertical: orient == "vertical",
},
}
this.Add(this.handle)
this.SetFocusable(true)
this.SetPropagateEvents(false)
this.SetValue(value)
this.OnKeyDown(this.handleKeyDown)
this.OnMouseDown(this.handleMouseDown)
this.OnMouseUp(this.handleMouseUp)
this.OnMouseMove(this.handleMouseMove)
theme.Apply(this.handle, theme.R("objects", "SliderHandle", orient))
theme.Apply(this, theme.R("objects", "Slider", orient))
return this
}
func NewVerticalSlider (value float64) *Slider {
return newSlider("vertical", value)
}
func NewHorizontalSlider (value float64) *Slider {
return newSlider("horizontal", value)
}
func (this *Slider) SetValue (value float64) {
if value < 0 { value = 0 }
if value > 1 { value = 1 }
if value == this.layout.value { return }
this.layout.value = value
this.SetLayout(this.layout)
this.on.valueChange.Broadcast()
}
func (this *Slider) Value () float64 {
return this.layout.value
}
func (this *Slider) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
switch key {
case input.KeyUp, input.KeyLeft:
if this.Modifiers().Alt {
this.SetValue(0)
} else {
this.SetValue(this.Value() - 0.05)
}
case input.KeyDown, input.KeyRight:
if this.Modifiers().Alt {
this.SetValue(1)
} else {
this.SetValue(this.Value() + 0.05)
}
case input.KeyHome:
this.SetValue(0)
case input.KeyEnd:
this.SetValue(1)
}
}
func (this *Slider) handleMouseDown (button input.Button) {
pointer := this.MousePosition()
handle := this.handle.Bounds()
var above, within bool
if pointer.In(handle) {
within = true
} else if this.layout.vertical {
above = pointer.Y < handle.Min.Y
} else {
above = pointer.X < handle.Min.X
}
switch button {
case input.ButtonLeft:
if within {
this.dragging = true
this.dragOffset =
pointer.Sub(this.handle.Bounds().Min).
Add(this.InnerBounds().Min)
this.drag()
} else {
this.dragOffset = this.fallbackDragOffset()
this.dragging = true
this.drag()
}
case input.ButtonMiddle:
if above {
this.SetValue(0)
} else {
this.SetValue(1)
}
case input.ButtonRight:
if above {
this.SetValue(this.Value() - 0.05)
} else {
this.SetValue(this.Value() + 0.05)
}
}
}
func (this *Slider) handleMouseUp (button input.Button) {
if button != input.ButtonLeft || !this.dragging { return }
this.dragging = false
}
func (this *Slider) handleMouseMove () {
if !this.dragging { return }
this.drag()
}
func (this *Slider) 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 *Slider) fallbackDragOffset () image.Point {
if this.layout.vertical {
return this.InnerBounds().Min.
Add(image.Pt(0, this.handle.Bounds().Dy() / 2))
} else {
return this.InnerBounds().Min.
Add(image.Pt(this.handle.Bounds().Dx() / 2, 0))
}
}
type sliderLayout struct {
vertical bool
value float64
}
func (sliderLayout) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
if len(boxes) != 1 { return image.Pt(0, 0) }
return boxes[0].MinimumSize()
}
func (this sliderLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
if len(boxes) != 1 { return }
handle := image.Rectangle { Max: boxes[0].MinimumSize() }
gutter := hints.Bounds
if this.vertical {
height := gutter.Dy() - handle.Dy()
offset := int(float64(height) * this.value)
gutter.Max.X = handle.Max.X
boxes[0].SetBounds (
handle.
Add(image.Pt(0, offset)).
Add(gutter.Min))
} else {
width := gutter.Dx() - handle.Dx()
offset := int(float64(width) * this.value)
gutter.Max.Y = handle.Max.Y
boxes[0].SetBounds (
handle.
Add(image.Pt(offset, 0)).
Add(gutter.Min))
}
}