Compare commits
62 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f778ef5c95 | |||
| c06f10c193 | |||
| 23fb28ce5c | |||
| 3533ce3726 | |||
| 6d157eb9af | |||
| da346f2f12 | |||
| 71a41d390f | |||
| 9ce7f8b8f3 | |||
| 1596d54834 | |||
| 95d3dc3288 | |||
| 1069ae6455 | |||
| 5c8358fc4a | |||
| 6a8aaca18d | |||
| 460733c8f3 | |||
| 5d2a366a62 | |||
| 2c7c77d8da | |||
| 8139d871cc | |||
| bb320dfcc9 | |||
| 2ab920eb26 | |||
| d8ae20d739 | |||
| 06561bb671 | |||
| a71d81af48 | |||
| bd9dbb762d | |||
| 6389556199 | |||
| 06d99b2790 | |||
| 6ab689b5c1 | |||
| 6497da69ed | |||
| 34794f4c77 | |||
| 25625e9a99 | |||
| 4cea0aa0bd | |||
| 1cb9e8091e | |||
| 68d49e1b02 | |||
| f025ec3d8a | |||
| 6fad52b335 | |||
| 8134069e1f | |||
| 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
|
||||
|
||||
[](https://pkg.go.dev/git.tebibyte.media/tomo/objects)
|
||||
|
||||
Objects contains a standard collection of re-usable objects. All objects in this
|
||||
module visually conform to whatever the theme is set to.
|
||||
|
||||
69
button.go
69
button.go
@@ -1,13 +1,22 @@
|
||||
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"
|
||||
|
||||
var buttonLayout = layouts.Row { true }
|
||||
var iconButtonLayout = layouts.Row { true }
|
||||
var bothButtonLayout = layouts.Row { false, true }
|
||||
|
||||
// Button is a clickable button.
|
||||
type Button struct {
|
||||
tomo.TextBox
|
||||
tomo.ContainerBox
|
||||
|
||||
label *Label
|
||||
icon *Icon
|
||||
labelActive bool
|
||||
|
||||
on struct {
|
||||
click event.FuncBroadcaster
|
||||
}
|
||||
@@ -15,21 +24,71 @@ 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", ""))
|
||||
box := &Button {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
label: NewLabel(text),
|
||||
}
|
||||
box.SetRole(tomo.R("objects", "Button", ""))
|
||||
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
|
||||
box.SetLayout(buttonLayout)
|
||||
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.OnKeyUp(box.handleKeyUp)
|
||||
box.SetFocusable(true)
|
||||
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.Remove(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 tomo.Icon) {
|
||||
if this.icon != nil { this.Remove(this.icon) }
|
||||
|
||||
var icon *Icon; if id != tomo.IconUnknown {
|
||||
icon = NewIcon(id, tomo.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.
|
||||
func (this *Button) OnClick (callback func ()) event.Cookie {
|
||||
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) {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return }
|
||||
this.on.click.Broadcast()
|
||||
|
||||
167
calendar.go
Normal file
167
calendar.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package objects
|
||||
|
||||
import "fmt"
|
||||
import "time"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
|
||||
// Calendar is an object that can display a date and allow the user to change
|
||||
// it. It can display one month at a time.
|
||||
type Calendar struct {
|
||||
tomo.ContainerBox
|
||||
|
||||
grid tomo.ContainerBox
|
||||
time time.Time
|
||||
monthLabel *Label
|
||||
|
||||
on struct {
|
||||
edit event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
// NewCalendar creates a new calendar with the specified date.
|
||||
func NewCalendar (tm time.Time) *Calendar {
|
||||
calendar := &Calendar {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
time: tm,
|
||||
}
|
||||
calendar.SetRole(tomo.R("objects", "Calendar", ""))
|
||||
calendar.SetLayout(layouts.ContractVertical)
|
||||
|
||||
prevButton := NewButton("")
|
||||
prevButton.SetIcon(tomo.IconGoPrevious)
|
||||
prevButton.OnClick(func () {
|
||||
calendar.prevMonth()
|
||||
calendar.on.edit.Broadcast()
|
||||
})
|
||||
nextButton := NewButton("")
|
||||
nextButton.SetIcon(tomo.IconGoNext)
|
||||
nextButton.OnClick(func () {
|
||||
calendar.nextMonth()
|
||||
calendar.on.edit.Broadcast()
|
||||
})
|
||||
calendar.monthLabel = NewLabel("")
|
||||
calendar.monthLabel.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
|
||||
|
||||
calendar.grid = tomo.NewContainerBox()
|
||||
calendar.grid.SetRole(tomo.R("objects", "CalendarGrid", ""))
|
||||
calendar.grid.SetLayout(layouts.NewGrid (
|
||||
[]bool { true, true, true, true, true, true, true },
|
||||
[]bool { }))
|
||||
calendar.Add(NewInnerContainer (
|
||||
layouts.Row { false, true, false },
|
||||
prevButton, calendar.monthLabel, nextButton))
|
||||
calendar.Add(calendar.grid)
|
||||
|
||||
calendar.OnScroll(calendar.handleScroll)
|
||||
calendar.CaptureScroll(true)
|
||||
|
||||
calendar.refresh()
|
||||
return calendar
|
||||
}
|
||||
|
||||
// SetTime sets the date the calendar will display.
|
||||
func (this *Calendar) SetTime (tm time.Time) {
|
||||
if this.time == tm { return }
|
||||
this.time = tm
|
||||
this.refresh()
|
||||
}
|
||||
|
||||
// OnEdit sets a function to be called when the user changes the date on the
|
||||
// calendar.
|
||||
func (this *Calendar) OnEdit (callback func ()) {
|
||||
this.on.edit.Connect(callback)
|
||||
}
|
||||
|
||||
func (this *Calendar) prevMonth () {
|
||||
this.time = firstOfMonth(this.time.Add(24 * time.Hour * 40))
|
||||
this.refresh()
|
||||
}
|
||||
|
||||
func (this *Calendar) nextMonth () {
|
||||
this.time = firstOfMonth(this.time.Add(24 * time.Hour * -20))
|
||||
this.refresh()
|
||||
}
|
||||
|
||||
var weekdayAbbreviations = []string {
|
||||
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
|
||||
}
|
||||
|
||||
func (this *Calendar) refresh () {
|
||||
this.monthLabel.SetText(this.time.Format("2006 January"))
|
||||
|
||||
this.grid.Clear()
|
||||
for _, day := range weekdayAbbreviations {
|
||||
dayLabel := tomo.NewTextBox()
|
||||
dayLabel.SetRole(tomo.R("objects", "CalendarWeekdayHeader", ""))
|
||||
dayLabel.SetText(day)
|
||||
dayLabel.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
|
||||
this.grid.Add(dayLabel)
|
||||
}
|
||||
|
||||
dayIter := 0 - int(firstOfMonth(this.time).Weekday())
|
||||
if dayIter <= -6 {
|
||||
dayIter = 1
|
||||
}
|
||||
weekday := 0
|
||||
totalDays := daysInMonth(this.time)
|
||||
for ; dayIter <= totalDays; dayIter ++ {
|
||||
weekday = (weekday + 1) % 7
|
||||
if dayIter > 0 {
|
||||
day := tomo.NewTextBox()
|
||||
day.SetText(fmt.Sprint(dayIter))
|
||||
if weekday == 1 || weekday == 0 {
|
||||
day.SetRole(tomo.R("objects", "CalendarDay", "weekend"))
|
||||
} else {
|
||||
day.SetRole(tomo.R("objects", "CalendarDay", "weekday"))
|
||||
}
|
||||
this.grid.Add(day)
|
||||
} else {
|
||||
this.grid.Add(tomo.NewBox())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Calendar) handleScroll (x, y float64) {
|
||||
if y < 0 {
|
||||
this.nextMonth()
|
||||
} else {
|
||||
this.prevMonth()
|
||||
}
|
||||
}
|
||||
|
||||
func firstOfMonth (tm time.Time) time.Time {
|
||||
return time.Date(tm.Year(), tm.Month(), 0, 0, 0, 0, 0, time.Local)
|
||||
}
|
||||
|
||||
func daysInMonth (tm time.Time) (days int) {
|
||||
year := tm.Year()
|
||||
month := tm.Month()
|
||||
switch month {
|
||||
case 1: days = 31
|
||||
case 2:
|
||||
// betcha didn't know this about leap years
|
||||
if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) {
|
||||
days = 29
|
||||
} else {
|
||||
days = 28
|
||||
}
|
||||
case 3: days = 31
|
||||
case 4: days = 30
|
||||
case 5: days = 31
|
||||
case 6: days = 30
|
||||
case 7: days = 31
|
||||
case 8: days = 31
|
||||
case 9: days = 30
|
||||
case 10: days = 31
|
||||
case 11: days = 30
|
||||
case 12: days = 31
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func canonMonth (tm time.Time) int {
|
||||
return int(tm.Month() - 1) + tm.Year() * 12
|
||||
}
|
||||
66
checkbox.go
Normal file
66
checkbox.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
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(),
|
||||
}
|
||||
box.SetRole(tomo.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 {
|
||||
this.SetTextureCenter(tomo.IconCheckboxChecked.Texture(tomo.IconSizeSmall))
|
||||
} else {
|
||||
this.SetTextureCenter(tomo.IconCheckboxUnchecked.Texture(tomo.IconSizeSmall))
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
}
|
||||
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"))
|
||||
this.SetRole(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...)
|
||||
this.SetRole(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"))
|
||||
this.SetRole(tomo.R("objects", "Container", "inner"))
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
100
dialog.go
Normal file
100
dialog.go
Normal file
@@ -0,0 +1,100 @@
|
||||
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")
|
||||
okButton.SetIcon(tomo.IconDialogOkay)
|
||||
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(tomo.IconDialogCancel)
|
||||
|
||||
okButton := NewButton("OK")
|
||||
okButton.SetIcon(tomo.IconDialogOkay)
|
||||
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.26.1
|
||||
require git.tebibyte.media/tomo/tomo v0.38.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.26.1 h1:V5ciRuixMYb79aAawgquFEfJ1icyEmMKBKFPWwi94NE=
|
||||
git.tebibyte.media/tomo/tomo v0.26.1/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
||||
git.tebibyte.media/tomo/tomo v0.38.0 h1:K5TP67RxnszudeNfmGZiU5cFTRjFueXiI3NCsgw+05U=
|
||||
git.tebibyte.media/tomo/tomo v0.38.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() }
|
||||
this.SetRole(tomo.R("objects", "Heading", fmt.Sprint(level)))
|
||||
this.SetText(text)
|
||||
return this
|
||||
}
|
||||
|
||||
41
icon.go
Normal file
41
icon.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/data"
|
||||
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 tomo.Icon, size tomo.IconSize) *Icon {
|
||||
this := &Icon {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
this.SetRole(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 tomo.IconSize) *Icon {
|
||||
this := &Icon {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
this.SetRole(tomo.R("objects", "Icon", size.String()))
|
||||
this.SetTexture(tomo.MimeIcon(mime, size))
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Icon) SetTexture (texture canvas.Texture) {
|
||||
this.Box.SetTextureCenter(texture)
|
||||
if texture == nil {
|
||||
this.SetMinimumSize(image.Pt(0, 0))
|
||||
} else {
|
||||
bounds := texture.Bounds()
|
||||
this.SetMinimumSize(bounds.Max.Sub(bounds.Min))
|
||||
}
|
||||
}
|
||||
4
label.go
4
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,8 +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", ""))
|
||||
this.SetRole(tomo.R("objects", "Label", ""))
|
||||
this.SetText(text)
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
61
labelcheckbox.go
Normal file
61
labelcheckbox.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
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),
|
||||
}
|
||||
box.SetRole(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 { 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()
|
||||
}
|
||||
}
|
||||
40
layouts/contract.go
Normal file
40
layouts/contract.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package layouts
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
|
||||
var _ tomo.Layout = ContractVertical
|
||||
|
||||
// Contract is a layout that arranges boxes in a simple row or column according
|
||||
// to their minimum sizes.
|
||||
type Contract bool
|
||||
|
||||
// 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 {
|
||||
return contract.fallback().MinimumSize(hints, boxes)
|
||||
}
|
||||
|
||||
func (contract Contract) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
contract.fallback().Arrange(hints, boxes)
|
||||
}
|
||||
|
||||
func (contract Contract) RecommendedHeight (hints tomo.LayoutHints, boxes []tomo.Box, width int) int {
|
||||
return contract.fallback().RecommendedHeight(hints, boxes, width)
|
||||
}
|
||||
|
||||
func (contract Contract) RecommendedWidth (hints tomo.LayoutHints, boxes []tomo.Box, height int) int {
|
||||
return contract.fallback().RecommendedWidth(hints, boxes, height)
|
||||
}
|
||||
|
||||
func (contract Contract) fallback () tomo.Layout {
|
||||
if contract == ContractVertical {
|
||||
return Column { }
|
||||
} else {
|
||||
return Row { }
|
||||
}
|
||||
}
|
||||
173
layouts/cut.go
173
layouts/cut.go
@@ -1,173 +0,0 @@
|
||||
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 []*Cut
|
||||
expand []bool
|
||||
vertical bool
|
||||
}
|
||||
|
||||
// NewCut creates and returns a new Cut layout.
|
||||
func NewCut () *Cut {
|
||||
return new(Cut)
|
||||
}
|
||||
|
||||
// Vertical divides the layout vertically. Sections are specified using
|
||||
// booleans. If a section is true, it will expand. If false, it will contract.
|
||||
func (this *Cut) Vertical (expand ...bool) {
|
||||
this.expand = expand
|
||||
this.vertical = true
|
||||
this.fill()
|
||||
}
|
||||
|
||||
// Horizontal divides the layout horizontally. Sections are specified using
|
||||
// booleans. If a section is true, it will expand. If false, it will contract.
|
||||
func (this *Cut) Horizontal (expand ...bool) {
|
||||
this.expand = expand
|
||||
this.vertical = false
|
||||
this.fill()
|
||||
}
|
||||
|
||||
// At returns the section of this layout at the specified index.
|
||||
func (this *Cut) At (index int) *Cut {
|
||||
return this.branches[index]
|
||||
}
|
||||
|
||||
func (this *Cut) real () bool {
|
||||
return this != nil && this.branches != nil
|
||||
}
|
||||
|
||||
func (this *Cut) fill () {
|
||||
this.branches = make([]*Cut, len(this.expand))
|
||||
for index := range this.branches {
|
||||
this.branches[index] = new(Cut)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
nChildren := len(this.branches)
|
||||
|
||||
// collect minimum sizes and physical endpoints
|
||||
var minimums = make([]image.Point, nChildren)
|
||||
var leaves = make([]tomo.Box, nChildren)
|
||||
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 = make([]image.Rectangle, nChildren)
|
||||
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
|
||||
}
|
||||
136
layouts/flow.go
Normal file
136
layouts/flow.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package layouts
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
|
||||
var _ tomo.Layout = FlowVertical
|
||||
|
||||
// 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))
|
||||
if minorSteps < 1 { minorSteps = 1 }
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
func (flow Flow) RecommendedHeight (hints tomo.LayoutHints, boxes []tomo.Box, width int) int {
|
||||
// TODO
|
||||
return 0
|
||||
}
|
||||
|
||||
func (flow Flow) RecommendedWidth (hints tomo.LayoutHints, boxes []tomo.Box, height int) int {
|
||||
// TODO
|
||||
return 0
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import "math"
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
|
||||
var _ tomo.Layout = new(Grid)
|
||||
|
||||
// Grid is a layout that arranges boxes in a grid formation with distinct rows
|
||||
// and columns. It is great for creating forms.
|
||||
type Grid struct {
|
||||
@@ -106,3 +108,13 @@ func expand (hints tomo.LayoutHints, sizes []int, space int, expands func (int)
|
||||
func ceilDiv (x, y int) int {
|
||||
return int(math.Ceil(float64(x) / float64(y)))
|
||||
}
|
||||
|
||||
func (this *Grid) RecommendedHeight (hints tomo.LayoutHints, boxes []tomo.Box, width int) int {
|
||||
// TODO
|
||||
return 0
|
||||
}
|
||||
|
||||
func (this *Grid) RecommendedWidth (hints tomo.LayoutHints, boxes []tomo.Box, height int) int {
|
||||
// TODO
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
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)))
|
||||
}
|
||||
}
|
||||
238
layouts/rowcol.go
Normal file
238
layouts/rowcol.go
Normal file
@@ -0,0 +1,238 @@
|
||||
package layouts
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
|
||||
var _ tomo.Layout = ContractVertical
|
||||
|
||||
// Row arranges boxes in a row. Boxes that share an index with a true value will
|
||||
// expand, and others will contract.
|
||||
type Row []bool
|
||||
|
||||
// Column arranges boxes in a column. Boxes that share an index with a true
|
||||
// value will expand, and others will contract.
|
||||
type Column []bool
|
||||
|
||||
func (column 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 (row 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 (column Column) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
expands := func (index int) bool {
|
||||
if index >= len(column) { return false }
|
||||
return column[index]
|
||||
}
|
||||
|
||||
// determine expanding box size
|
||||
expandingSize := 0.0
|
||||
if !hints.OverflowY {
|
||||
gaps := len(boxes) - 1
|
||||
freeSpace := float64(hints.Bounds.Dy() - hints.Gap.Y * gaps)
|
||||
nExpanding := 0; for index, box := range boxes {
|
||||
if expands(index) {
|
||||
nExpanding ++
|
||||
} else {
|
||||
freeSpace -= float64(box.MinimumSize().Y)
|
||||
}
|
||||
}
|
||||
expandingSize = freeSpace / float64(nExpanding)
|
||||
}
|
||||
|
||||
// determine width
|
||||
width := 0
|
||||
if hints.OverflowX {
|
||||
for _, box := range boxes {
|
||||
minimum := box.MinimumSize()
|
||||
if width < minimum.X { width = minimum.X }
|
||||
}
|
||||
} else {
|
||||
width = hints.Bounds.Dx()
|
||||
}
|
||||
|
||||
// arrange
|
||||
dot := hints.Bounds.Min
|
||||
for index, box := range boxes {
|
||||
if index > 0 { dot.Y += hints.Gap.Y }
|
||||
|
||||
// determine height
|
||||
height := box.MinimumSize().Y
|
||||
if hints.OverflowY {
|
||||
if box, ok := box.(tomo.ContentBox); ok {
|
||||
height = box.RecommendedHeight(width)
|
||||
}
|
||||
} else {
|
||||
if expands(index) {
|
||||
height = int(expandingSize)
|
||||
}
|
||||
}
|
||||
|
||||
// set bounds
|
||||
box.SetBounds(image.Rectangle {
|
||||
Min: dot,
|
||||
Max: dot.Add(image.Pt(width, height)),
|
||||
})
|
||||
dot.Y += height
|
||||
}
|
||||
|
||||
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 (row Row) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
expands := func (index int) bool {
|
||||
if index >= len(row) { return false }
|
||||
return row[index]
|
||||
}
|
||||
|
||||
// determine expanding box size
|
||||
expandingSize := 0.0
|
||||
if !hints.OverflowY {
|
||||
gaps := len(boxes) - 1
|
||||
freeSpace := float64(hints.Bounds.Dx() - hints.Gap.X * gaps)
|
||||
nExpanding := 0; for index, box := range boxes {
|
||||
if expands(index) {
|
||||
nExpanding ++
|
||||
} else {
|
||||
freeSpace -= float64(box.MinimumSize().X)
|
||||
}
|
||||
}
|
||||
expandingSize = freeSpace / float64(nExpanding)
|
||||
}
|
||||
|
||||
// determine height
|
||||
height := 0
|
||||
if hints.OverflowY {
|
||||
for _, box := range boxes {
|
||||
minimum := box.MinimumSize()
|
||||
if height < minimum.Y { height = minimum.Y }
|
||||
}
|
||||
} else {
|
||||
height = hints.Bounds.Dy()
|
||||
}
|
||||
|
||||
// arrange
|
||||
dot := hints.Bounds.Min
|
||||
for index, box := range boxes {
|
||||
if index > 0 { dot.X += hints.Gap.X }
|
||||
|
||||
// determine width
|
||||
width := box.MinimumSize().X
|
||||
if hints.OverflowY {
|
||||
if box, ok := box.(tomo.ContentBox); ok {
|
||||
width = box.RecommendedHeight(height)
|
||||
}
|
||||
} else {
|
||||
if expands(index) {
|
||||
width = int(expandingSize)
|
||||
}
|
||||
}
|
||||
|
||||
// set bounds
|
||||
box.SetBounds(image.Rectangle {
|
||||
Min: dot,
|
||||
Max: dot.Add(image.Pt(width, height)),
|
||||
})
|
||||
dot.X += width
|
||||
}
|
||||
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
||||
func (column Column) RecommendedHeight (hints tomo.LayoutHints, boxes []tomo.Box, width int) int {
|
||||
height := 0
|
||||
for _, box := range boxes {
|
||||
if box, ok := box.(tomo.ContentBox); ok {
|
||||
height += box.RecommendedHeight(width)
|
||||
} else {
|
||||
height += box.MinimumSize().Y
|
||||
}
|
||||
}
|
||||
height += hints.Gap.Y * (len(boxes) - 1)
|
||||
return height
|
||||
}
|
||||
|
||||
func (row Row) RecommendedHeight (hints tomo.LayoutHints, boxes []tomo.Box, width int) int {
|
||||
height := 0
|
||||
for _, box := range boxes {
|
||||
minimum := box.MinimumSize()
|
||||
boxHeight := 0
|
||||
if box, ok := box.(tomo.ContentBox); ok {
|
||||
boxHeight = box.RecommendedHeight(minimum.X)
|
||||
} else {
|
||||
boxHeight = minimum.Y
|
||||
}
|
||||
if boxHeight > height { height = boxHeight }
|
||||
}
|
||||
return height
|
||||
}
|
||||
|
||||
func (column Column) RecommendedWidth (hints tomo.LayoutHints, boxes []tomo.Box, height int) int {
|
||||
width := 0
|
||||
for _, box := range boxes {
|
||||
minimum := box.MinimumSize()
|
||||
boxWidth := 0
|
||||
if box, ok := box.(tomo.ContentBox); ok {
|
||||
boxWidth = box.RecommendedHeight(minimum.Y)
|
||||
} else {
|
||||
boxWidth = minimum.X
|
||||
}
|
||||
if boxWidth > width { width = boxWidth }
|
||||
}
|
||||
return width
|
||||
}
|
||||
|
||||
func (row Row) RecommendedWidth (hints tomo.LayoutHints, boxes []tomo.Box, height int) int {
|
||||
width := 0
|
||||
for _, box := range boxes {
|
||||
if box, ok := box.(tomo.ContentBox); ok {
|
||||
width += box.RecommendedWidth(height)
|
||||
} else {
|
||||
width += box.MinimumSize().X
|
||||
}
|
||||
}
|
||||
width += hints.Gap.X * (len(boxes) - 1)
|
||||
return width
|
||||
}
|
||||
99
menu.go
Normal file
99
menu.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
// import "git.tebibyte.media/tomo/tomo/event"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
|
||||
// Menu is a menu window.
|
||||
type Menu struct {
|
||||
tomo.Window
|
||||
|
||||
parent tomo.Window
|
||||
bounds image.Rectangle
|
||||
rootContainer tomo.ContainerBox
|
||||
tearLine tomo.Box
|
||||
torn bool
|
||||
}
|
||||
|
||||
// NewMenu creates a new menu with the specified items. The menu will appear
|
||||
// directly under the anchor Object. If the anchor is nil, it will appear
|
||||
// directly under the mouse pointer instead.
|
||||
func NewMenu (anchor tomo.Object, items ...tomo.Object) (*Menu, error) {
|
||||
menu := &Menu { }
|
||||
if anchor == nil {
|
||||
// TODO: *actually* put it under the mouse
|
||||
window, err := tomo.NewWindow(menu.bounds)
|
||||
if err != nil { return nil, err }
|
||||
menu.Window = window
|
||||
} else {
|
||||
menu.bounds = menuBoundsFromAnchor(anchor)
|
||||
menu.parent = anchor.GetBox().Window()
|
||||
window, err := menu.parent.NewMenu(menu.bounds)
|
||||
if err != nil { return nil, err }
|
||||
menu.Window = window
|
||||
}
|
||||
|
||||
menu.rootContainer = tomo.NewContainerBox()
|
||||
menu.rootContainer.SetLayout(layouts.ContractVertical)
|
||||
|
||||
if !menu.torn {
|
||||
menu.tearLine = tomo.NewBox()
|
||||
menu.tearLine.SetRole(tomo.R("objects", "TearLine", ""))
|
||||
menu.tearLine.SetFocusable(true)
|
||||
menu.tearLine.OnKeyUp(func (key input.Key, numberPad bool) {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return }
|
||||
menu.TearOff()
|
||||
})
|
||||
menu.tearLine.OnMouseUp(func (button input.Button) {
|
||||
if button != input.ButtonLeft { return }
|
||||
if menu.tearLine.MousePosition().In(menu.tearLine.Bounds()) {
|
||||
menu.TearOff()
|
||||
}
|
||||
})
|
||||
menu.rootContainer.Add(menu.tearLine)
|
||||
}
|
||||
|
||||
for _, item := range items {
|
||||
menu.rootContainer.Add(item)
|
||||
if item, ok := item.(*MenuItem); ok {
|
||||
item.OnClick(func () {
|
||||
if !menu.torn {
|
||||
menu.Close()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
menu.rootContainer.SetRole(tomo.R("objects", "Container", "menu"))
|
||||
|
||||
menu.Window.SetRoot(menu.rootContainer)
|
||||
return menu, nil
|
||||
}
|
||||
|
||||
// TearOff converts this menu into a tear-off menu.
|
||||
func (this *Menu) TearOff () {
|
||||
if this.torn { return }
|
||||
if this.parent == nil { return }
|
||||
this.torn = true
|
||||
|
||||
window, err := this.parent.NewChild(this.bounds)
|
||||
if err != nil { return }
|
||||
|
||||
visible := this.Window.Visible()
|
||||
this.Window.SetRoot(nil)
|
||||
this.Window.Close()
|
||||
|
||||
this.rootContainer.Remove(this.tearLine)
|
||||
|
||||
this.Window = window
|
||||
this.Window.SetRoot(this.rootContainer)
|
||||
this.Window.SetVisible(visible)
|
||||
}
|
||||
|
||||
func menuBoundsFromAnchor (anchor tomo.Object) image.Rectangle {
|
||||
bounds := anchor.GetBox().Bounds()
|
||||
return image.Rect (
|
||||
bounds.Min.X, bounds.Max.Y,
|
||||
bounds.Max.X, bounds.Max.Y)//.Add(windowBounds.Min)
|
||||
}
|
||||
73
menuitem.go
Normal file
73
menuitem.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
|
||||
// MenuItem is a clickable button.
|
||||
type MenuItem struct {
|
||||
tomo.ContainerBox
|
||||
|
||||
label *Label
|
||||
icon *Icon
|
||||
labelActive bool
|
||||
|
||||
on struct {
|
||||
click event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
// NewMenuItem creates a new menu item with the specified text.
|
||||
func NewMenuItem (text string) *MenuItem {
|
||||
box := &MenuItem {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
label: NewLabel(text),
|
||||
icon: NewIcon("", tomo.IconSizeSmall),
|
||||
}
|
||||
box.SetRole(tomo.R("objects", "MenuItem", ""))
|
||||
box.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
||||
box.SetLayout(layouts.NewGrid([]bool { false, true }, []bool { true }))
|
||||
|
||||
box.Add(box.icon)
|
||||
box.Add(box.label)
|
||||
|
||||
box.CaptureDND(true)
|
||||
box.CaptureMouse(true)
|
||||
box.CaptureScroll(true)
|
||||
box.CaptureKeyboard(true)
|
||||
|
||||
box.OnMouseUp(box.handleMouseUp)
|
||||
box.OnKeyUp(box.handleKeyUp)
|
||||
box.SetFocusable(true)
|
||||
return box
|
||||
}
|
||||
|
||||
// SetText sets the text of the items's label.
|
||||
func (this *MenuItem) SetText (text string) {
|
||||
this.label.SetText(text)
|
||||
}
|
||||
|
||||
// SetIcon sets an icon for this item. Setting the icon to IconUnknown will
|
||||
// remove it.
|
||||
func (this *MenuItem) SetIcon (id tomo.Icon) {
|
||||
if this.icon != nil { this.Remove(this.icon) }
|
||||
this.Insert(NewIcon(id, tomo.IconSizeSmall), this.label)
|
||||
}
|
||||
|
||||
// OnClick specifies a function to be called when the menu item is clicked.
|
||||
func (this *MenuItem) OnClick (callback func ()) event.Cookie {
|
||||
return this.on.click.Connect(callback)
|
||||
}
|
||||
|
||||
func (this *MenuItem) handleKeyUp (key input.Key, numberPad bool) {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return }
|
||||
this.on.click.Broadcast()
|
||||
}
|
||||
|
||||
func (this *MenuItem) handleMouseUp (button input.Button) {
|
||||
if button != input.ButtonLeft { return }
|
||||
if this.MousePosition().In(this.Bounds()) {
|
||||
this.on.click.Broadcast()
|
||||
}
|
||||
}
|
||||
99
numberinput.go
Normal file
99
numberinput.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package objects
|
||||
|
||||
import "math"
|
||||
import "strconv"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
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(""),
|
||||
}
|
||||
box.SetRole(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(tomo.IconValueIncrement)
|
||||
box.decrement.SetIcon(tomo.IconValueDecrement)
|
||||
|
||||
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()
|
||||
}
|
||||
373
scrollbar.go
Normal file
373
scrollbar.go
Normal file
@@ -0,0 +1,373 @@
|
||||
package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
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)
|
||||
|
||||
this.handle.SetRole(tomo.R("objects", "SliderHandle", orient))
|
||||
this.SetRole(tomo.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 ContentObject. Closing the returned cookie
|
||||
// will unlink it.
|
||||
func (this *Scrollbar) Link (box tomo.ContentObject) 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.GetBox().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 tomo.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.ContentObject
|
||||
}
|
||||
|
||||
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. we are the layout
|
||||
// and we shouldn't be adding and removing boxes, so this is
|
||||
// really the only good way to hide things.
|
||||
// TODO perhaps have a "Hidden" rectangle in the Tomo API?
|
||||
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) RecommendedHeight (hints tomo.LayoutHints, boxes []tomo.Box, width int) int {
|
||||
return this.MinimumSize(hints, boxes).X
|
||||
}
|
||||
|
||||
func (this scrollbarLayout) RecommendedWidth (hints tomo.LayoutHints, boxes []tomo.Box, height int) int {
|
||||
return this.MinimumSize(hints, boxes).Y
|
||||
}
|
||||
|
||||
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.GetBox().InnerBounds().Dy())
|
||||
} else {
|
||||
return float64(this.linked.GetBox().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)
|
||||
}
|
||||
}
|
||||
198
scrollcontainer.go
Normal file
198
scrollcontainer.go
Normal file
@@ -0,0 +1,198 @@
|
||||
package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
|
||||
// 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)
|
||||
this.SetRole(tomo.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.ContentObject) {
|
||||
if this.layout.root != nil {
|
||||
// remove root and close cookies
|
||||
this.Remove(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
Sub(image.Pt(int(x), int(y))))
|
||||
}
|
||||
|
||||
// TODO: remove this and replace it with something from the layouts package
|
||||
type scrollContainerLayout struct {
|
||||
root tomo.ContentObject
|
||||
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.GetBox().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.GetBox().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 (this *scrollContainerLayout) RecommendedHeight (hints tomo.LayoutHints, boxes []tomo.Box, width int) int {
|
||||
return this.MinimumSize(hints, boxes).X
|
||||
}
|
||||
|
||||
func (this *scrollContainerLayout) RecommendedWidth (hints tomo.LayoutHints, boxes []tomo.Box, height int) int {
|
||||
return this.MinimumSize(hints, boxes).Y
|
||||
}
|
||||
|
||||
func max (x, y int) int {
|
||||
if x > y { return x }
|
||||
return y
|
||||
}
|
||||
@@ -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,7 +12,6 @@ func NewSeparator () *Separator {
|
||||
this := &Separator {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
theme.Apply(this, theme.R("objects", "Separator", ""))
|
||||
this.SetRole(tomo.R("objects", "Separator", ""))
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
92
slider.go
92
slider.go
@@ -2,24 +2,25 @@ 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!
|
||||
|
||||
// Slider is a control that selects a numeric value between 0 and 1.
|
||||
type Slider struct {
|
||||
tomo.ContainerBox
|
||||
handle *SliderHandle
|
||||
layout sliderLayout
|
||||
dragging bool
|
||||
dragOffset image.Point
|
||||
|
||||
step float64
|
||||
|
||||
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 {
|
||||
tomo.Box
|
||||
}
|
||||
@@ -33,80 +34,101 @@ func newSlider (orient string, value float64) *Slider {
|
||||
layout: sliderLayout {
|
||||
vertical: orient == "vertical",
|
||||
},
|
||||
step: 0.05,
|
||||
}
|
||||
|
||||
|
||||
this.Add(this.handle)
|
||||
this.SetFocusable(true)
|
||||
this.SetPropagateEvents(false)
|
||||
|
||||
this.CaptureDND(true)
|
||||
this.CaptureMouse(true)
|
||||
this.CaptureScroll(true)
|
||||
this.CaptureKeyboard(true)
|
||||
|
||||
this.SetValue(value)
|
||||
this.OnKeyDown(this.handleKeyDown)
|
||||
this.OnMouseDown(this.handleMouseDown)
|
||||
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)
|
||||
|
||||
this.handle.SetRole(tomo.R("objects", "SliderHandle", orient))
|
||||
this.SetRole(tomo.R("objects", "Slider", orient))
|
||||
return this
|
||||
}
|
||||
|
||||
// NewVerticalSlider creates a new vertical slider with the specified value.
|
||||
func NewVerticalSlider (value float64) *Slider {
|
||||
return newSlider("vertical", value)
|
||||
}
|
||||
|
||||
// NewHorizontalSlider creates a new horizontal slider with the specified value.
|
||||
func NewHorizontalSlider (value float64) *Slider {
|
||||
return newSlider("horizontal", value)
|
||||
}
|
||||
|
||||
// SetValue sets the value of the slider between 0 and 1.
|
||||
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()
|
||||
}
|
||||
|
||||
// Value returns the value of the slider between 0 and 1.
|
||||
func (this *Slider) Value () float64 {
|
||||
return this.layout.value
|
||||
}
|
||||
|
||||
func (this *Slider) OnValueChange (callback func ()) event.Cookie {
|
||||
return this.on.valueChange.Connect(callback)
|
||||
// OnValueChange specifies a function to be called when the user moves the
|
||||
// slider.
|
||||
func (this *Slider) OnSlide (callback func ()) event.Cookie {
|
||||
return this.on.slide.Connect(callback)
|
||||
}
|
||||
|
||||
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 {
|
||||
case input.KeyUp, input.KeyLeft:
|
||||
if this.Modifiers().Alt {
|
||||
this.SetValue(0)
|
||||
} else {
|
||||
this.SetValue(this.Value() - 0.05)
|
||||
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() + 0.05)
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
above = pointer.X < handle.Min.X + handle.Dx() / 2
|
||||
}
|
||||
|
||||
|
||||
switch button {
|
||||
case input.ButtonLeft:
|
||||
if within {
|
||||
@@ -123,14 +145,18 @@ func (this *Slider) handleMouseDown (button input.Button) {
|
||||
case input.ButtonMiddle:
|
||||
if above {
|
||||
this.SetValue(0)
|
||||
this.on.slide.Broadcast()
|
||||
} else {
|
||||
this.SetValue(1)
|
||||
this.on.slide.Broadcast()
|
||||
}
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,6 +171,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()
|
||||
@@ -152,6 +184,7 @@ func (this *Slider) drag () {
|
||||
|
||||
if this.layout.vertical {
|
||||
this.SetValue (
|
||||
1 -
|
||||
float64(pointer.Y) /
|
||||
float64(gutter.Dy() - handle.Dy()))
|
||||
} else {
|
||||
@@ -159,6 +192,7 @@ func (this *Slider) drag () {
|
||||
float64(pointer.X) /
|
||||
float64(gutter.Dx() - handle.Dx()))
|
||||
}
|
||||
this.on.slide.Broadcast()
|
||||
}
|
||||
|
||||
func (this *Slider) fallbackDragOffset () image.Point {
|
||||
@@ -185,10 +219,10 @@ 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)
|
||||
offset := int(float64(height) * (1 - this.value))
|
||||
handle.Max.X = gutter.Dx()
|
||||
boxes[0].SetBounds (
|
||||
handle.
|
||||
@@ -204,3 +238,11 @@ func (this sliderLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
Add(gutter.Min))
|
||||
}
|
||||
}
|
||||
|
||||
func (this sliderLayout) RecommendedHeight (tomo.LayoutHints, []tomo.Box, int) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (this sliderLayout) RecommendedWidth (tomo.LayoutHints, []tomo.Box, int) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
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"
|
||||
|
||||
@@ -12,17 +12,21 @@ type TextInput struct {
|
||||
text []rune
|
||||
on struct {
|
||||
enter event.FuncBroadcaster
|
||||
edit event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
// 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", ""))
|
||||
this.SetRole(tomo.R("objects", "TextInput", ""))
|
||||
this.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
||||
this.SetText(text)
|
||||
this.SetFocusable(true)
|
||||
this.SetSelectable(true)
|
||||
this.SetOverflow(true, false)
|
||||
this.OnKeyDown(this.handleKeyDown)
|
||||
this.OnScroll(this.handleScroll)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -43,13 +47,21 @@ func (this *TextInput) OnEnter (callback func ()) event.Cookie {
|
||||
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) {
|
||||
dot := this.Dot()
|
||||
modifiers := this.Modifiers()
|
||||
word := modifiers.Control
|
||||
sel := modifiers.Shift
|
||||
changed := false
|
||||
|
||||
|
||||
// TODO all dot control (movement, selection, etc) should be done in the
|
||||
// backend. (editing should be done here, though)
|
||||
|
||||
switch {
|
||||
case key == input.KeyEnter:
|
||||
this.on.enter.Broadcast()
|
||||
@@ -84,7 +96,22 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
|
||||
this.text, dot = text.Type(this.text, dot, rune(key))
|
||||
changed = true
|
||||
}
|
||||
|
||||
|
||||
this.Select(dot)
|
||||
if changed { this.SetText(string(this.text)) }
|
||||
if changed {
|
||||
this.SetText(string(this.text))
|
||||
this.on.edit.Broadcast()
|
||||
}
|
||||
}
|
||||
|
||||
// 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))))
|
||||
}
|
||||
26
textview.go
Normal file
26
textview.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
|
||||
// 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() }
|
||||
this.SetRole(tomo.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