Compare commits
71 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5a32b06cef | |||
| fe50f5783b | |||
| 73a5fab0bc | |||
| 61addc051b | |||
| 7e275cc70e | |||
| 9856cd327f | |||
| 572e0c49af | |||
| e0f4ecb509 | |||
| fc51ffe33c | |||
| 987f4bfc4a | |||
| b883542f3b | |||
| c8d33a0ef4 | |||
| 9fa764c7b9 | |||
| 84ab0895f8 | |||
| b9c77fd5f7 | |||
| 2722d19ecd | |||
| 4fc44c11e8 | |||
| 0cdb116ec1 | |||
| 6ea1679112 | |||
| b87f32eac9 | |||
| 793526238a | |||
| 884148f006 | |||
| 3e382da688 | |||
| 18b8898644 | |||
| 85fbe9c996 | |||
| 25a59d888c | |||
| 6ca6771fc6 | |||
| 9077015db6 | |||
| 1125d98b3d | |||
| 638fc61d83 | |||
| d0ee6c432c | |||
| b9f980e7fd | |||
| b7d1a0abdd | |||
| a38cee8437 | |||
| 48bfa05452 | |||
| e8a3a376ea | |||
| ae1e62c1f2 | |||
| 0b7e5392f4 | |||
| 1efb946953 | |||
| 1e8df2392d | |||
| 83dca60257 | |||
| 5b60717b8f | |||
| d01d39569b | |||
| 55637e36db | |||
| e62afcd667 | |||
| 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 |
12
.editorconfig
Normal file
12
.editorconfig
Normal file
@@ -0,0 +1,12 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
indent_style = tab
|
||||
indent_size = 8
|
||||
charset = utf-8
|
||||
|
||||
[*.md]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
64
button.go
64
button.go
@@ -1,14 +1,13 @@
|
||||
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.NewGrid([]bool { true }, []bool { true })
|
||||
var iconButtonLayout = layouts.NewGrid([]bool { false }, []bool { true })
|
||||
var bothButtonLayout = layouts.NewGrid([]bool { false, true }, []bool { true })
|
||||
var buttonLayout = layouts.Row { true }
|
||||
var iconButtonLayout = layouts.Row { true }
|
||||
var bothButtonLayout = layouts.Row { false, true }
|
||||
|
||||
// Button is a clickable button.
|
||||
type Button struct {
|
||||
@@ -29,17 +28,15 @@ func NewButton (text string) *Button {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
label: NewLabel(text),
|
||||
}
|
||||
theme.Apply(box, theme.R("objects", "Button", ""))
|
||||
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
|
||||
box.SetLayout(buttonLayout)
|
||||
box.SetRole(tomo.R("objects", "Button"))
|
||||
box.label.SetAttr(tomo.AAlign(tomo.AlignMiddle, tomo.AlignMiddle))
|
||||
box.SetAttr(tomo.ALayout(buttonLayout))
|
||||
box.SetText(text)
|
||||
|
||||
box.CaptureDND(true)
|
||||
box.CaptureMouse(true)
|
||||
box.CaptureScroll(true)
|
||||
box.CaptureKeyboard(true)
|
||||
|
||||
box.OnMouseUp(box.handleMouseUp)
|
||||
box.SetInputMask(true)
|
||||
box.OnButtonDown(box.handleButtonDown)
|
||||
box.OnButtonUp(box.handleButtonUp)
|
||||
box.OnKeyDown(box.handleKeyDown)
|
||||
box.OnKeyUp(box.handleKeyUp)
|
||||
box.SetFocusable(true)
|
||||
return box
|
||||
@@ -49,7 +46,7 @@ func NewButton (text string) *Button {
|
||||
func (this *Button) SetText (text string) {
|
||||
this.label.SetText(text)
|
||||
if this.labelActive && text == "" {
|
||||
this.Delete(this.label)
|
||||
this.Remove(this.label)
|
||||
this.labelActive = false
|
||||
}
|
||||
if !this.labelActive && text != "" {
|
||||
@@ -61,17 +58,18 @@ func (this *Button) SetText (text string) {
|
||||
|
||||
// SetIcon sets an icon for this button. Setting the icon to IconUnknown will
|
||||
// remove it.
|
||||
func (this *Button) SetIcon (id theme.Icon) {
|
||||
if this.icon != nil { this.Delete(this.icon) }
|
||||
func (this *Button) SetIcon (id tomo.Icon) {
|
||||
if this.icon != nil { this.Remove(this.icon) }
|
||||
|
||||
var icon *Icon; if id != theme.IconUnknown {
|
||||
icon = NewIcon(id, theme.IconSizeSmall)
|
||||
var icon *Icon; if id != tomo.IconUnknown {
|
||||
icon = NewIcon(id, tomo.IconSizeSmall)
|
||||
}
|
||||
this.icon = icon
|
||||
|
||||
if this.icon != nil {
|
||||
this.Insert(this.icon, this.label)
|
||||
}
|
||||
this.SetTag("icon", this.icon != nil)
|
||||
this.applyLayout()
|
||||
}
|
||||
|
||||
@@ -82,22 +80,34 @@ func (this *Button) OnClick (callback func ()) event.Cookie {
|
||||
|
||||
func (this *Button) applyLayout () {
|
||||
if this.labelActive && this.icon == nil {
|
||||
this.SetLayout(buttonLayout)
|
||||
this.SetAttr(tomo.ALayout(buttonLayout))
|
||||
} else if !this.labelActive && this.icon != nil {
|
||||
this.SetLayout(iconButtonLayout)
|
||||
this.SetAttr(tomo.ALayout(iconButtonLayout))
|
||||
} else {
|
||||
this.SetLayout(bothButtonLayout)
|
||||
this.SetAttr(tomo.ALayout(bothButtonLayout))
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Button) handleKeyUp (key input.Key, numberPad bool) {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return }
|
||||
this.on.click.Broadcast()
|
||||
func (this *Button) handleKeyDown (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Button) handleMouseUp (button input.Button) {
|
||||
if button != input.ButtonLeft { return }
|
||||
if this.MousePosition().In(this.Bounds()) {
|
||||
func (this *Button) handleKeyUp (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
this.on.click.Broadcast()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Button) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Button) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if this.Window().MousePosition().In(this.Bounds()) {
|
||||
this.on.click.Broadcast()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
173
calendar.go
Normal file
173
calendar.go
Normal file
@@ -0,0 +1,173 @@
|
||||
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 {
|
||||
valueChange 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.SetAttr(tomo.ALayout(layouts.ContractVertical))
|
||||
|
||||
prevButton := NewButton("")
|
||||
prevButton.SetIcon(tomo.IconGoPrevious)
|
||||
prevButton.OnClick(func () {
|
||||
calendar.prevMonth()
|
||||
calendar.on.valueChange.Broadcast()
|
||||
})
|
||||
nextButton := NewButton("")
|
||||
nextButton.SetIcon(tomo.IconGoNext)
|
||||
nextButton.OnClick(func () {
|
||||
calendar.nextMonth()
|
||||
calendar.on.valueChange.Broadcast()
|
||||
})
|
||||
calendar.monthLabel = NewLabel("")
|
||||
calendar.monthLabel.SetAttr(tomo.AAlign(tomo.AlignMiddle, tomo.AlignMiddle))
|
||||
|
||||
calendar.grid = tomo.NewContainerBox()
|
||||
calendar.grid.SetRole(tomo.R("objects", "CalendarGrid"))
|
||||
calendar.grid.SetAttr(tomo.ALayout(layouts.NewGrid (
|
||||
true, true, true, true, true, true, true)()))
|
||||
calendar.Add(NewInnerContainer (
|
||||
layouts.Row { false, true, false },
|
||||
prevButton, calendar.monthLabel, nextButton))
|
||||
calendar.Add(calendar.grid)
|
||||
|
||||
calendar.OnScroll(calendar.handleScroll)
|
||||
|
||||
calendar.refresh()
|
||||
return calendar
|
||||
}
|
||||
|
||||
// Value returns the time this calendar is displaying.
|
||||
func (this *Calendar) Value () time.Time {
|
||||
return this.time
|
||||
}
|
||||
|
||||
// SetValue sets the date the calendar will display.
|
||||
func (this *Calendar) SetValue (tm time.Time) {
|
||||
if this.time == tm { return }
|
||||
this.time = tm
|
||||
this.refresh()
|
||||
}
|
||||
|
||||
// OnValueChange sets a function to be called when the user changes the date on
|
||||
// the calendar.
|
||||
func (this *Calendar) OnValueChange (callback func ()) {
|
||||
this.on.valueChange.Connect(callback)
|
||||
}
|
||||
|
||||
func (this *Calendar) prevMonth () {
|
||||
this.time = firstOfMonth(this.time.Add(24 * time.Hour * -20))
|
||||
this.refresh()
|
||||
}
|
||||
|
||||
func (this *Calendar) nextMonth () {
|
||||
this.time = firstOfMonth(this.time.Add(24 * time.Hour * 40))
|
||||
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.SetAttr(tomo.AAlign(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"))
|
||||
day.SetTag("weekend", true)
|
||||
} else {
|
||||
day.SetRole(tomo.R("objects", "CalendarDay"))
|
||||
day.SetTag("weekday", true)
|
||||
}
|
||||
this.grid.Add(day)
|
||||
} else {
|
||||
this.grid.Add(tomo.NewBox())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Calendar) handleScroll (x, y float64) bool {
|
||||
if y < 0 {
|
||||
this.prevMonth()
|
||||
} else {
|
||||
this.nextMonth()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
65
checkbox.go
65
checkbox.go
@@ -1,7 +1,6 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
|
||||
@@ -19,51 +18,61 @@ func NewCheckbox (value bool) *Checkbox {
|
||||
box := &Checkbox {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
theme.Apply(box, theme.R("objects", "Checkbox", ""))
|
||||
box.SetValue(false)
|
||||
box.SetRole(tomo.R("objects", "Checkbox"))
|
||||
box.SetValue(value)
|
||||
|
||||
box.OnMouseUp(box.handleMouseUp)
|
||||
box.OnButtonDown(box.handleButtonDown)
|
||||
box.OnButtonUp(box.handleButtonUp)
|
||||
box.OnKeyDown(box.handleKeyDown)
|
||||
box.OnKeyUp(box.handleKeyUp)
|
||||
box.SetFocusable(true)
|
||||
return box
|
||||
}
|
||||
|
||||
// SetValue sets the value of the checkbox.
|
||||
func (this *Checkbox) SetValue (value bool) {
|
||||
this.value = value
|
||||
if this.value {
|
||||
// TODO perhaps have IconStatusOkay/Cancel in actions, and have
|
||||
// a status icon for checked checkboxes.
|
||||
this.SetTexture(theme.IconStatusOkay.Texture(theme.IconSizeSmall))
|
||||
} else {
|
||||
this.SetTexture(nil)
|
||||
}
|
||||
}
|
||||
|
||||
// Toggle toggles the value of the checkbox between true and false.
|
||||
func (this *Checkbox) Toggle () {
|
||||
this.SetValue(!this.Value())
|
||||
}
|
||||
|
||||
// Value returns the value of the checkbox.
|
||||
func (this *Checkbox) Value () bool {
|
||||
return this.value
|
||||
}
|
||||
|
||||
// OnValueChange specifies a function to be called when the checkbox's value
|
||||
// changes.
|
||||
// SetValue sets the value of the checkbox.
|
||||
func (this *Checkbox) SetValue (value bool) {
|
||||
this.value = value
|
||||
// the theme shall decide what checked and unchecked states look like
|
||||
this.SetTag("checked", value)
|
||||
this.SetTag("unchecked", !value)
|
||||
}
|
||||
|
||||
// Toggle toggles the value of the checkbox between true and false.
|
||||
func (this *Checkbox) Toggle () {
|
||||
this.SetValue(!this.Value())
|
||||
}
|
||||
|
||||
// OnValueChange specifies a function to be called when the user checks or
|
||||
// unchecks the checkbox.
|
||||
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 }
|
||||
func (this *Checkbox) handleKeyDown (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
this.Toggle()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Checkbox) handleMouseUp (button input.Button) {
|
||||
if button != input.ButtonLeft { return }
|
||||
if this.MousePosition().In(this.Bounds()) {
|
||||
func (this *Checkbox) handleKeyUp (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false}
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Checkbox) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Checkbox) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if this.Window().MousePosition().In(this.Bounds()) {
|
||||
this.Toggle()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
159
colorpicker.go
Normal file
159
colorpicker.go
Normal file
@@ -0,0 +1,159 @@
|
||||
package objects
|
||||
|
||||
import "image/color"
|
||||
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/tomo/canvas"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
import "git.tebibyte.media/tomo/objects/internal"
|
||||
|
||||
// ColorPicker allows the user to pick a color by controlling its HSBA
|
||||
// parameters.
|
||||
type ColorPicker struct {
|
||||
tomo.ContainerBox
|
||||
value internal.HSVA
|
||||
|
||||
pickerMap *colorPickerMap
|
||||
hueSlider *Slider
|
||||
alphaSlider *Slider
|
||||
|
||||
on struct {
|
||||
valueChange event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
// NewColorPicker creates a new color picker with the specified color.
|
||||
func NewColorPicker (value color.Color) *ColorPicker {
|
||||
picker := &ColorPicker {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
}
|
||||
picker.SetRole(tomo.R("objects", "ColorPicker"))
|
||||
picker.SetAttr(tomo.ALayout(layouts.Row { true, false, false }))
|
||||
picker.pickerMap = newColorPickerMap(picker)
|
||||
picker.Add(picker.pickerMap)
|
||||
|
||||
picker.hueSlider = NewVerticalSlider(0.0)
|
||||
picker.Add(picker.hueSlider)
|
||||
picker.hueSlider.OnValueChange(func () {
|
||||
picker.value.H = picker.hueSlider.Value()
|
||||
picker.on.valueChange.Broadcast()
|
||||
picker.pickerMap.Invalidate()
|
||||
})
|
||||
|
||||
picker.alphaSlider = NewVerticalSlider(0.0)
|
||||
picker.Add(picker.alphaSlider)
|
||||
picker.alphaSlider.OnValueChange(func () {
|
||||
picker.value.A = uint8(picker.alphaSlider.Value() * 255)
|
||||
picker.on.valueChange.Broadcast()
|
||||
picker.pickerMap.Invalidate()
|
||||
})
|
||||
|
||||
if value == nil { value = color.Transparent }
|
||||
picker.SetValue(value)
|
||||
return picker
|
||||
}
|
||||
|
||||
// Value returns the color of the picker.
|
||||
func (this *ColorPicker) Value () color.Color {
|
||||
return this.value
|
||||
}
|
||||
|
||||
// SetValue sets the color of the picker.
|
||||
func (this *ColorPicker) SetValue (value color.Color) {
|
||||
if value == nil { value = color.Transparent }
|
||||
this.value = internal.RGBAToHSVA(value.RGBA())
|
||||
this.hueSlider.SetValue(this.value.H)
|
||||
this.alphaSlider.SetValue(float64(this.value.A) / 255)
|
||||
}
|
||||
|
||||
// OnValueChange specifies a function to be called when the user changes the
|
||||
// swatch's color.
|
||||
func (this *ColorPicker) OnValueChange (callback func ()) event.Cookie {
|
||||
return this.on.valueChange.Connect(callback)
|
||||
}
|
||||
|
||||
// RGBA satisfies the color.Color interface
|
||||
func (this *ColorPicker) RGBA () (r, g, b, a uint32) {
|
||||
return this.value.RGBA()
|
||||
}
|
||||
|
||||
type colorPickerMap struct {
|
||||
tomo.CanvasBox
|
||||
dragging bool
|
||||
parent *ColorPicker
|
||||
}
|
||||
|
||||
func newColorPickerMap (parent *ColorPicker) *colorPickerMap {
|
||||
picker := &colorPickerMap {
|
||||
CanvasBox: tomo.NewCanvasBox(),
|
||||
parent: parent,
|
||||
}
|
||||
picker.SetDrawer(picker)
|
||||
picker.SetRole(tomo.R("objects", "ColorPickerMap"))
|
||||
picker.OnButtonUp(picker.handleButtonUp)
|
||||
picker.OnButtonDown(picker.handleButtonDown)
|
||||
picker.OnMouseMove(picker.handleMouseMove)
|
||||
return picker
|
||||
}
|
||||
|
||||
func (this *colorPickerMap) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
this.dragging = true
|
||||
this.drag()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *colorPickerMap) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
this.dragging = false
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *colorPickerMap) handleMouseMove () bool {
|
||||
if !this.dragging { return false }
|
||||
this.drag()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *colorPickerMap) drag () {
|
||||
pointer := this.Window().MousePosition()
|
||||
bounds := this.InnerBounds()
|
||||
this.parent.value.S = float64(pointer.X - bounds.Min.X) / float64(bounds.Dx())
|
||||
this.parent.value.V = 1 - float64(pointer.Y - bounds.Min.Y) / float64(bounds.Dy())
|
||||
this.parent.value = this.parent.value.Canon()
|
||||
this.parent.on.valueChange.Broadcast()
|
||||
this.Invalidate()
|
||||
}
|
||||
|
||||
func (this *colorPickerMap) Draw (can canvas.Canvas) {
|
||||
bounds := can.Bounds()
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y ++ {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x ++ {
|
||||
xx := x - bounds.Min.X
|
||||
yy := y - bounds.Min.Y
|
||||
|
||||
pixel := internal.HSVA {
|
||||
H: this.parent.value.H,
|
||||
S: float64(xx) / float64(bounds.Dx()),
|
||||
V: 1 - float64(yy) / float64(bounds.Dy()),
|
||||
A: 255,
|
||||
}
|
||||
|
||||
sPos := int( this.parent.value.S * float64(bounds.Dx()))
|
||||
vPos := int((1 - this.parent.value.V) * float64(bounds.Dy()))
|
||||
sDist := sPos - xx
|
||||
vDist := vPos - yy
|
||||
crosshair :=
|
||||
(sDist == 0 || vDist == 0) &&
|
||||
-8 < sDist && sDist < 8 &&
|
||||
-8 < vDist && vDist < 8
|
||||
if crosshair {
|
||||
pixel.S = 1 - pixel.S
|
||||
pixel.V = 1 - pixel.V
|
||||
}
|
||||
|
||||
can.Set(x, y, pixel)
|
||||
}}
|
||||
}
|
||||
|
||||
12
container.go
12
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
|
||||
@@ -16,7 +15,7 @@ func newContainer (layout tomo.Layout, children ...tomo.Object) *Container {
|
||||
this := &Container {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
}
|
||||
this.SetLayout(layout)
|
||||
this.SetAttr(tomo.ALayout(layout))
|
||||
for _, child := range children {
|
||||
this.Add(child)
|
||||
}
|
||||
@@ -28,7 +27,8 @@ func newContainer (layout tomo.Layout, children ...tomo.Object) *Container {
|
||||
// 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"))
|
||||
this.SetTag("outer", true)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -36,14 +36,16 @@ func NewOuterContainer (layout tomo.Layout, children ...tomo.Object) *Container
|
||||
// around it. It is meant to be used as a root container for a ScrollContainer.
|
||||
func NewSunkenContainer (layout tomo.Layout, children ...tomo.Object) *Container {
|
||||
this := newContainer(layout, children...)
|
||||
theme.Apply(this, theme.R("objects", "Container", "sunken"))
|
||||
this.SetRole(tomo.R("objects", "Container"))
|
||||
this.SetTag("sunken", true)
|
||||
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"))
|
||||
this.SetTag("inner", true)
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
25
dialog.go
25
dialog.go
@@ -3,7 +3,6 @@ package objects
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
|
||||
// DialogKind defines the semantic role of a dialog window.
|
||||
@@ -46,17 +45,17 @@ func NewDialog (kind DialogKind, parent tomo.Window, title, message string, opti
|
||||
dialog.Window = window
|
||||
}
|
||||
|
||||
var iconId theme.Icon
|
||||
var iconId tomo.Icon
|
||||
switch kind {
|
||||
case DialogInformation: iconId = theme.IconStatusInformation
|
||||
case DialogQuestion: iconId = theme.IconStatusQuestion
|
||||
case DialogWarning: iconId = theme.IconStatusWarning
|
||||
case DialogError: iconId = theme.IconStatusError
|
||||
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, theme.IconSizeLarge)
|
||||
icon := NewIcon(iconId, tomo.IconSizeLarge)
|
||||
messageText := NewLabel(message)
|
||||
messageText.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
||||
messageText.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
|
||||
|
||||
for _, option := range options {
|
||||
if option, ok := option.(clickable); ok {
|
||||
@@ -64,10 +63,10 @@ func NewDialog (kind DialogKind, parent tomo.Window, title, message string, opti
|
||||
}
|
||||
}
|
||||
dialog.controlRow = NewInnerContainer(layouts.ContractHorizontal, options...)
|
||||
dialog.controlRow.SetAlign(tomo.AlignEnd, tomo.AlignEnd)
|
||||
dialog.controlRow.SetAttr(tomo.AAlign(tomo.AlignEnd, tomo.AlignEnd))
|
||||
|
||||
dialog.SetRoot(NewOuterContainer (
|
||||
layouts.NewGrid([]bool { true }, []bool { true, false }),
|
||||
layouts.Column { true, false },
|
||||
NewInnerContainer(layouts.ContractHorizontal, icon, messageText),
|
||||
dialog.controlRow))
|
||||
return dialog, nil
|
||||
@@ -76,7 +75,7 @@ func NewDialog (kind DialogKind, parent tomo.Window, title, message string, opti
|
||||
// NewDialogOk creates a new dialog window with an OK option.
|
||||
func NewDialogOk (kind DialogKind, parent tomo.Window, title, message string, onOk func ()) (*Dialog, error) {
|
||||
okButton := NewButton("OK")
|
||||
okButton.SetIcon(theme.IconStatusOkay)
|
||||
okButton.SetIcon(tomo.IconDialogOkay)
|
||||
okButton.OnClick(func () {
|
||||
if onOk != nil { onOk() }
|
||||
})
|
||||
@@ -88,10 +87,10 @@ func NewDialogOk (kind DialogKind, parent tomo.Window, title, message string, on
|
||||
// NewDialogOkCancel creates a new dialog window with OK and Cancel options.
|
||||
func NewDialogOkCancel (kind DialogKind, parent tomo.Window, title, message string, onOk func ()) (*Dialog, error) {
|
||||
cancelButton := NewButton("Cancel")
|
||||
cancelButton.SetIcon(theme.IconStatusCancel)
|
||||
cancelButton.SetIcon(tomo.IconDialogCancel)
|
||||
|
||||
okButton := NewButton("OK")
|
||||
okButton.SetIcon(theme.IconStatusOkay)
|
||||
okButton.SetIcon(tomo.IconDialogOkay)
|
||||
okButton.OnClick(func () {
|
||||
if onOk != nil { onOk() }
|
||||
})
|
||||
|
||||
4
go.mod
4
go.mod
@@ -2,6 +2,4 @@ module git.tebibyte.media/tomo/objects
|
||||
|
||||
go 1.20
|
||||
|
||||
require git.tebibyte.media/tomo/tomo v0.31.0
|
||||
|
||||
require golang.org/x/image v0.11.0 // indirect
|
||||
require git.tebibyte.media/tomo/tomo v0.45.0
|
||||
|
||||
37
go.sum
37
go.sum
@@ -1,35 +1,2 @@
|
||||
git.tebibyte.media/tomo/tomo v0.31.0 h1:LHPpj3AWycochnC8F441aaRNS6Tq6w6WnBrp/LGjyhM=
|
||||
git.tebibyte.media/tomo/tomo v0.31.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
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.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=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
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.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=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
git.tebibyte.media/tomo/tomo v0.45.0 h1:fQH0WIPidW275hOq9dE6R7p064xG1RGx2QU68Avlr84=
|
||||
git.tebibyte.media/tomo/tomo v0.45.0/go.mod h1:WrtilgKB1y8O2Yu7X4mYcRiqOlPR8NuUnoA/ynkQWrs=
|
||||
|
||||
@@ -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
|
||||
@@ -16,8 +15,11 @@ func NewHeading (level int, text string) *Heading {
|
||||
if level < 0 { level = 0 }
|
||||
if level > 2 { level = 2 }
|
||||
this := &Heading { TextBox: tomo.NewTextBox() }
|
||||
theme.Apply(this, theme.R("objects", "Heading", fmt.Sprint(level)))
|
||||
this.SetRole(tomo.R("objects", "Heading"))
|
||||
this.SetTag(fmt.Sprint(level), true)
|
||||
this.SetText(text)
|
||||
this.SetSelectable(true)
|
||||
this.SetFocusable(true)
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
46
icon.go
46
icon.go
@@ -1,42 +1,52 @@
|
||||
package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/data"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||
|
||||
// Icon displays a single icon.
|
||||
type Icon struct {
|
||||
tomo.Box
|
||||
icon tomo.Icon
|
||||
size tomo.IconSize
|
||||
}
|
||||
|
||||
func iconSizeString (size tomo.IconSize) string {
|
||||
switch size {
|
||||
case tomo.IconSizeLarge: return "large"
|
||||
case tomo.IconSizeMedium: return "medium"
|
||||
default: return "small"
|
||||
}
|
||||
}
|
||||
|
||||
// NewIcon creates a new icon from an icon ID.
|
||||
func NewIcon (id theme.Icon, size theme.IconSize) *Icon {
|
||||
func NewIcon (icon tomo.Icon, size tomo.IconSize) *Icon {
|
||||
this := &Icon {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
theme.Apply(this, theme.R("objects", "Icon", size.String()))
|
||||
this.SetTexture(id.Texture(size))
|
||||
this.SetRole(tomo.R("objects", "Icon"))
|
||||
this.SetIcon(icon, size)
|
||||
this.OnIconSetChange(this.handleIconSetChange)
|
||||
return this
|
||||
}
|
||||
|
||||
// NewMimeIcon creates a new icon from a MIME type.
|
||||
func NewMimeIcon (mime data.Mime, size theme.IconSize) *Icon {
|
||||
this := &Icon {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
theme.Apply(this, theme.R("objects", "Icon", size.String()))
|
||||
this.SetTexture(theme.MimeIcon(mime, size))
|
||||
return this
|
||||
// SetIcon sets the icon.
|
||||
func (this *Icon) SetIcon (icon tomo.Icon, size tomo.IconSize) {
|
||||
if this.icon == icon { return }
|
||||
this.icon = icon
|
||||
this.setTexture(icon.Texture(size))
|
||||
}
|
||||
|
||||
func (this *Icon) SetTexture (texture canvas.Texture) {
|
||||
this.Box.SetTexture(texture)
|
||||
func (this *Icon) handleIconSetChange () {
|
||||
this.setTexture(this.icon.Texture(this.size))
|
||||
}
|
||||
|
||||
func (this *Icon) setTexture (texture canvas.Texture) {
|
||||
this.SetAttr(tomo.ATexture(texture))
|
||||
this.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
|
||||
if texture == nil {
|
||||
this.SetMinimumSize(image.Pt(0, 0))
|
||||
this.SetAttr(tomo.AMinimumSize(0, 0))
|
||||
} else {
|
||||
bounds := texture.Bounds()
|
||||
this.SetMinimumSize(bounds.Max.Sub(bounds.Min))
|
||||
this.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
|
||||
}
|
||||
}
|
||||
|
||||
110
input.go
110
input.go
@@ -1,110 +0,0 @@
|
||||
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"
|
||||
|
||||
// TextInput is a single-line editable text box.
|
||||
type TextInput struct {
|
||||
tomo.TextBox
|
||||
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.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
|
||||
}
|
||||
|
||||
// SetText sets the text content of the input.
|
||||
func (this *TextInput) SetText (text string) {
|
||||
this.text = []rune(text)
|
||||
this.TextBox.SetText(text)
|
||||
}
|
||||
|
||||
// Text returns the text content of the input.
|
||||
func (this *TextInput) Text () string {
|
||||
return string(this.text)
|
||||
}
|
||||
|
||||
// OnEnter specifies a function to be called when the user presses enter within
|
||||
// the text input.
|
||||
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 this (except editing stuff) really should be moved into the
|
||||
// backend
|
||||
|
||||
switch {
|
||||
case key == input.KeyEnter:
|
||||
this.on.enter.Broadcast()
|
||||
case key == input.KeyHome || (modifiers.Alt && key == input.KeyLeft):
|
||||
dot.End = 0
|
||||
if !sel { dot.Start = dot.End }
|
||||
case key == input.KeyEnd || (modifiers.Alt && key == input.KeyRight):
|
||||
dot.End = len(this.text)
|
||||
if !sel { dot.Start = dot.End }
|
||||
case key == input.KeyLeft:
|
||||
if sel {
|
||||
dot = text.SelectLeft(this.text, dot, word)
|
||||
} else {
|
||||
dot = text.MoveLeft(this.text, dot, word)
|
||||
}
|
||||
case key == input.KeyRight:
|
||||
if sel {
|
||||
dot = text.SelectRight(this.text, dot, word)
|
||||
} else {
|
||||
dot = text.MoveRight(this.text, dot, word)
|
||||
}
|
||||
case key == input.KeyBackspace:
|
||||
this.text, dot = text.Backspace(this.text, dot, word)
|
||||
changed = true
|
||||
case key == input.KeyDelete:
|
||||
this.text, dot = text.Delete(this.text, dot, word)
|
||||
changed = true
|
||||
case key == input.Key('a') && modifiers.Control:
|
||||
dot.Start = 0
|
||||
dot.End = len(this.text)
|
||||
case key.Printable():
|
||||
this.text, dot = text.Type(this.text, dot, rune(key))
|
||||
changed = true
|
||||
}
|
||||
|
||||
this.Select(dot)
|
||||
if changed {
|
||||
this.SetText(string(this.text))
|
||||
this.on.edit.Broadcast()
|
||||
}
|
||||
}
|
||||
|
||||
func (this *TextInput) handleScroll (x, y float64) {
|
||||
this.ScrollTo(this.ContentBounds().Min.Add(image.Pt(int(x), int(y))))
|
||||
}
|
||||
104
internal/color.go
Normal file
104
internal/color.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package internal
|
||||
|
||||
type HSVA struct {
|
||||
H float64
|
||||
S float64
|
||||
V float64
|
||||
A uint8
|
||||
}
|
||||
|
||||
func (hsva HSVA) RGBA () (r, g, b, a uint32) {
|
||||
// Adapted from:
|
||||
// https://www.cs.rit.edu/~ncs/color/t_convert.html
|
||||
|
||||
component := func (x float64) uint32 {
|
||||
return uint32(float64(0xFFFF) * x)
|
||||
}
|
||||
|
||||
ca := uint32(hsva.A) << 8
|
||||
s := clamp01(hsva.S)
|
||||
v := clamp01(hsva.V)
|
||||
if s == 0 {
|
||||
light := component(v)
|
||||
return light, light, light, ca
|
||||
}
|
||||
|
||||
h := clamp01(hsva.H) * 360
|
||||
sector := int(h / 60)
|
||||
offset := (h / 60) - float64(sector)
|
||||
|
||||
fac := float64(hsva.A) / 255
|
||||
p := component(fac * v * (1 - s))
|
||||
q := component(fac * v * (1 - s * offset))
|
||||
t := component(fac * v * (1 - s * (1 - offset)))
|
||||
va := component(v)
|
||||
|
||||
switch sector {
|
||||
case 0: return va, t, p, ca
|
||||
case 1: return q, va, p, ca
|
||||
case 2: return p, va, t, ca
|
||||
case 3: return p, q, va, ca
|
||||
case 4: return t, p, va, ca
|
||||
default: return va, p, q, ca
|
||||
}
|
||||
}
|
||||
|
||||
// Canon returns the color but with the H, S, and V fields are constrained to
|
||||
// the range 0.0-1.0
|
||||
func (hsva HSVA) Canon () HSVA {
|
||||
hsva.H = clamp01(hsva.H)
|
||||
hsva.S = clamp01(hsva.S)
|
||||
hsva.V = clamp01(hsva.V)
|
||||
return hsva
|
||||
}
|
||||
|
||||
func clamp01 (x float64) float64 {
|
||||
if x > 1.0 { return 1.0 }
|
||||
if x < 0.0 { return 0.0 }
|
||||
return x
|
||||
}
|
||||
|
||||
func RGBAToHSVA (r, g, b, a uint32) HSVA {
|
||||
// Adapted from:
|
||||
// https://www.cs.rit.edu/~ncs/color/t_convert.html
|
||||
|
||||
component := func (x uint32) float64 {
|
||||
return clamp01(float64(x) / 0xFFFF)
|
||||
}
|
||||
cr := component(r)
|
||||
cg := component(g)
|
||||
cb := component(b)
|
||||
|
||||
var maxComponent float64
|
||||
if cr > maxComponent { maxComponent = cr }
|
||||
if cg > maxComponent { maxComponent = cg }
|
||||
if cb > maxComponent { maxComponent = cb }
|
||||
var minComponent = 1.0
|
||||
if cr < minComponent { minComponent = cr }
|
||||
if cg < minComponent { minComponent = cg }
|
||||
if cb < minComponent { minComponent = cb }
|
||||
|
||||
hsva := HSVA {
|
||||
V: maxComponent,
|
||||
A: uint8(a >> 8),
|
||||
}
|
||||
|
||||
delta := maxComponent - minComponent
|
||||
if delta == 0 {
|
||||
// hsva.S is undefined, so hue doesn't matter
|
||||
return hsva
|
||||
}
|
||||
hsva.S = delta / maxComponent
|
||||
|
||||
switch {
|
||||
case cr == maxComponent: hsva.H = (cg - cb) / delta
|
||||
case cg == maxComponent: hsva.H = 2 + (cb - cr) / delta
|
||||
case cb == maxComponent: hsva.H = 4 + (cr - cg) / delta
|
||||
}
|
||||
|
||||
hsva.H *= 60
|
||||
if hsva.H < 0 { hsva.H += 360 }
|
||||
hsva.H /= 360
|
||||
|
||||
return hsva
|
||||
}
|
||||
6
label.go
6
label.go
@@ -1,7 +1,6 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
|
||||
// Label is a simple text label.
|
||||
type Label struct {
|
||||
@@ -11,7 +10,10 @@ 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)
|
||||
this.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
|
||||
this.SetSelectable(true)
|
||||
this.SetFocusable(true)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
@@ -21,17 +20,24 @@ func NewLabelCheckbox (value bool, text string) *LabelCheckbox {
|
||||
checkbox: NewCheckbox(value),
|
||||
label: NewLabel(text),
|
||||
}
|
||||
theme.Apply(box, theme.R("objects", "LabelCheckbox", ""))
|
||||
box.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
||||
box.SetRole(tomo.R("objects", "LabelCheckbox"))
|
||||
box.label.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
|
||||
box.label.SetSelectable(false)
|
||||
box.label.SetFocusable(false)
|
||||
box.Add(box.checkbox)
|
||||
box.Add(box.label)
|
||||
box.SetLayout(layouts.NewGrid([]bool { false, true }, []bool { false }))
|
||||
box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
|
||||
|
||||
box.OnMouseUp(box.handleMouseUp)
|
||||
box.label.OnMouseUp(box.handleMouseUp)
|
||||
box.OnButtonDown(box.handleButtonDown)
|
||||
box.OnButtonUp(box.handleButtonUp)
|
||||
return box
|
||||
}
|
||||
|
||||
// Value returns the value of the checkbox.
|
||||
func (this *LabelCheckbox) Value () bool {
|
||||
return this.checkbox.Value()
|
||||
}
|
||||
|
||||
// SetValue sets the value of the checkbox.
|
||||
func (this *LabelCheckbox) SetValue (value bool) {
|
||||
this.checkbox.SetValue(value)
|
||||
@@ -42,21 +48,22 @@ 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.
|
||||
// OnValueChange specifies a function to be called when the user checks or
|
||||
// unchecks the checkbox.
|
||||
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()) {
|
||||
func (this *LabelCheckbox) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *LabelCheckbox) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if this.Window().MousePosition().In(this.Bounds()) {
|
||||
this.checkbox.SetFocused(true)
|
||||
this.checkbox.Toggle()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
75
labelswatch.go
Normal file
75
labelswatch.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package objects
|
||||
|
||||
import "image/color"
|
||||
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"
|
||||
|
||||
// LabelSwatch is a swatch with a label.
|
||||
type LabelSwatch struct {
|
||||
tomo.ContainerBox
|
||||
swatch *Swatch
|
||||
label *Label
|
||||
}
|
||||
|
||||
// NewLabelSwatch creates a new labeled swatch with the specified color and
|
||||
// label text.
|
||||
func NewLabelSwatch (value color.Color, text string) *LabelSwatch {
|
||||
box := &LabelSwatch {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
swatch: NewSwatch(value),
|
||||
label: NewLabel(text),
|
||||
}
|
||||
box.SetRole(tomo.R("objects", "LabelSwatch"))
|
||||
box.swatch.label = text
|
||||
box.label.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
|
||||
box.Add(box.swatch)
|
||||
box.Add(box.label)
|
||||
box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
|
||||
|
||||
box.OnButtonDown(box.handleButtonDown)
|
||||
box.OnButtonUp(box.handleButtonUp)
|
||||
return box
|
||||
}
|
||||
|
||||
// Value returns the color of the swatch.
|
||||
func (this *LabelSwatch) Value () color.Color {
|
||||
return this.swatch.Value()
|
||||
}
|
||||
|
||||
// SetValue sets the color of the swatch.
|
||||
func (this *LabelSwatch) SetValue (value color.Color) {
|
||||
this.swatch.SetValue(value)
|
||||
}
|
||||
|
||||
// OnValueChange specifies a function to be called when the swatch's color
|
||||
// is changed by the user.
|
||||
func (this *LabelSwatch) OnValueChange (callback func ()) event.Cookie {
|
||||
return this.swatch.OnValueChange(callback)
|
||||
}
|
||||
|
||||
// RGBA satisfies the color.Color interface
|
||||
func (this *LabelSwatch) RGBA () (r, g, b, a uint32) {
|
||||
return this.swatch.RGBA()
|
||||
}
|
||||
|
||||
// OnConfirm specifies a function to be called when the user selects "OK" in the
|
||||
// color picker.
|
||||
func (this *LabelSwatch) OnConfirm (callback func ()) event.Cookie {
|
||||
return this.swatch.OnConfirm(callback)
|
||||
}
|
||||
|
||||
func (this *LabelSwatch) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return true }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *LabelSwatch) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return true }
|
||||
if this.Window().MousePosition().In(this.Bounds()) {
|
||||
this.swatch.SetFocused(true)
|
||||
this.swatch.Choose()
|
||||
}
|
||||
return true
|
||||
}
|
||||
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.BoxQuerier) image.Point {
|
||||
return contract.fallback().MinimumSize(hints, boxes)
|
||||
}
|
||||
|
||||
func (contract Contract) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
|
||||
contract.fallback().Arrange(hints, boxes)
|
||||
}
|
||||
|
||||
func (contract Contract) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
|
||||
return contract.fallback().RecommendedHeight(hints, boxes, width)
|
||||
}
|
||||
|
||||
func (contract Contract) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, 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
|
||||
}
|
||||
@@ -3,6 +3,8 @@ 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
|
||||
@@ -15,42 +17,43 @@ 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 {
|
||||
func (flow Flow) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) 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) {
|
||||
func (flow Flow) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
|
||||
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())
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
boxSize := flow.minor(boxes.MinimumSize(index))
|
||||
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) {
|
||||
for index < boxes.Len() {
|
||||
// get a slice of boxes for this major step
|
||||
stepIndexEnd := index + minorSteps
|
||||
if stepIndexEnd > len(boxes) { stepIndexEnd = len(boxes) }
|
||||
step := boxes[index:stepIndexEnd]
|
||||
stepIndexStart := index
|
||||
stepIndexEnd := index + minorSteps
|
||||
if stepIndexEnd > boxes.Len() { stepIndexEnd = boxes.Len() }
|
||||
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())
|
||||
for index := stepIndexStart; index < stepIndexEnd; index ++ {
|
||||
boxSize := flow.major(boxes.MinimumSize(index))
|
||||
if boxSize > majorSize { majorSize = boxSize }
|
||||
}
|
||||
if majorSize == 0 { continue }
|
||||
@@ -59,9 +62,9 @@ func (flow Flow) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
var size image.Point
|
||||
size = flow.incrMajor(size, majorSize)
|
||||
size = flow.incrMinor(size, minorSize)
|
||||
for _, box := range step {
|
||||
for index := stepIndexStart; index < stepIndexEnd; index ++ {
|
||||
bounds := image.Rectangle { Min: point, Max: point.Add(size) }
|
||||
box.SetBounds(bounds)
|
||||
boxes.SetBounds(index, bounds)
|
||||
|
||||
point = flow.incrMinor(point, minorSize + flow.minor(hints.Gap))
|
||||
}
|
||||
@@ -121,3 +124,13 @@ func (flow Flow) deltaMinor (rectangle image.Rectangle) int {
|
||||
func (flow Flow) fallback () tomo.Layout {
|
||||
return Contract(flow)
|
||||
}
|
||||
|
||||
func (flow Flow) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
|
||||
// TODO
|
||||
return 0
|
||||
}
|
||||
|
||||
func (flow Flow) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, 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 {
|
||||
@@ -16,15 +18,21 @@ type Grid struct {
|
||||
// will contract. Boxes are laid out left to right, then top to bottom. Boxes
|
||||
// that go beyond the lengh of rows will be laid out according to columns, but
|
||||
// they will not expand vertically.
|
||||
func NewGrid (columns, rows []bool) *Grid {
|
||||
this := &Grid {
|
||||
xExpand: columns,
|
||||
yExpand: rows,
|
||||
//
|
||||
// If you aren't sure how to use this constructor, here is an example:
|
||||
//
|
||||
// X0 X1 X2 Y0 Y1 Y2
|
||||
// NewGrid(true, false, true)(false, true, true)
|
||||
func NewGrid (columns ...bool) func (rows ...bool) *Grid {
|
||||
return func (rows ...bool) *Grid {
|
||||
return &Grid {
|
||||
xExpand: columns,
|
||||
yExpand: rows,
|
||||
}
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Grid) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
|
||||
func (this *Grid) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
|
||||
cols, rows := this.minimums(boxes)
|
||||
size := image.Pt (
|
||||
(len(cols) - 1) * hints.Gap.X,
|
||||
@@ -34,7 +42,7 @@ func (this *Grid) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.P
|
||||
return size
|
||||
}
|
||||
|
||||
func (this *Grid) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
func (this *Grid) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
|
||||
xExpand := func (index int) bool {
|
||||
return this.xExpand[index]
|
||||
}
|
||||
@@ -48,9 +56,9 @@ func (this *Grid) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
expand(hints, rows, hints.Bounds.Dy(), yExpand)
|
||||
|
||||
position := hints.Bounds.Min
|
||||
for index, box := range boxes {
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
col, row := index % len(cols), index / len(cols)
|
||||
box.SetBounds(image.Rectangle {
|
||||
boxes.SetBounds(index, image.Rectangle {
|
||||
Min: position,
|
||||
Max: position.Add(image.Pt(cols[col], rows[row])),
|
||||
})
|
||||
@@ -63,13 +71,13 @@ func (this *Grid) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Grid) minimums (boxes []tomo.Box) ([]int, []int) {
|
||||
func (this *Grid) minimums (boxes tomo.BoxQuerier) ([]int, []int) {
|
||||
nCols, nRows := this.dimensions(boxes)
|
||||
cols, rows := make([]int, nCols), make([]int, nRows)
|
||||
|
||||
for index, box := range boxes {
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
col, row := index % len(cols), index / len(cols)
|
||||
minimum := box.MinimumSize()
|
||||
minimum := boxes.MinimumSize(index)
|
||||
if cols[col] < minimum.X {
|
||||
cols[col] = minimum.X
|
||||
}
|
||||
@@ -81,8 +89,8 @@ func (this *Grid) minimums (boxes []tomo.Box) ([]int, []int) {
|
||||
return cols, rows
|
||||
}
|
||||
|
||||
func (this *Grid) dimensions (boxes []tomo.Box) (int, int) {
|
||||
return len(this.xExpand), ceilDiv(len(boxes), len(this.xExpand))
|
||||
func (this *Grid) dimensions (boxes tomo.BoxQuerier) (int, int) {
|
||||
return len(this.xExpand), ceilDiv(boxes.Len(), len(this.xExpand))
|
||||
}
|
||||
|
||||
func expand (hints tomo.LayoutHints, sizes []int, space int, expands func (int) bool) {
|
||||
@@ -104,5 +112,14 @@ func expand (hints tomo.LayoutHints, sizes []int, space int, expands func (int)
|
||||
}
|
||||
|
||||
func ceilDiv (x, y int) int {
|
||||
if y == 0 { return 0 }
|
||||
return int(math.Ceil(float64(x) / float64(y)))
|
||||
}
|
||||
|
||||
func (this *Grid) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
|
||||
return this.MinimumSize(hints, boxes).Y
|
||||
}
|
||||
|
||||
func (this *Grid) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
|
||||
return this.MinimumSize(hints, boxes).X
|
||||
}
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
package layouts
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
|
||||
// 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 {
|
||||
if contract.v() {
|
||||
dot := image.Point { }
|
||||
for _, box := range boxes {
|
||||
minimum := box.MinimumSize()
|
||||
dot.Y += minimum.Y
|
||||
if dot.X < minimum.X {
|
||||
dot.X = minimum.X
|
||||
}
|
||||
}
|
||||
dot.Y += hints.Gap.Y * (len(boxes) - 1)
|
||||
return dot
|
||||
} else {
|
||||
dot := image.Point { }
|
||||
for _, box := range boxes {
|
||||
minimum := box.MinimumSize()
|
||||
dot.X += minimum.X
|
||||
if dot.Y < minimum.Y {
|
||||
dot.Y = minimum.Y
|
||||
}
|
||||
}
|
||||
dot.X += hints.Gap.X * (len(boxes) - 1)
|
||||
return dot
|
||||
}
|
||||
}
|
||||
|
||||
func (contract Contract) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
if contract.v() {
|
||||
dot := hints.Bounds.Min
|
||||
for index, box := range boxes {
|
||||
if index > 0 { dot.Y += hints.Gap.Y }
|
||||
minimum := box.MinimumSize()
|
||||
box.SetBounds(image.Rectangle {
|
||||
Min: dot,
|
||||
Max: dot.Add(image.Pt(hints.Bounds.Dx(), minimum.Y)),
|
||||
})
|
||||
dot.Y += minimum.Y
|
||||
}
|
||||
|
||||
height := dot.Y - hints.Bounds.Min.Y
|
||||
offset := 0
|
||||
|
||||
switch hints.AlignY {
|
||||
case tomo.AlignMiddle:
|
||||
offset = (hints.Bounds.Dy() - height) / 2
|
||||
case tomo.AlignEnd:
|
||||
offset = hints.Bounds.Dy() - height
|
||||
}
|
||||
for _, box := range boxes {
|
||||
box.SetBounds(box.Bounds().Add(image.Pt(0, offset)))
|
||||
}
|
||||
} else {
|
||||
dot := hints.Bounds.Min
|
||||
for index, box := range boxes {
|
||||
if index > 0 { dot.X += hints.Gap.X }
|
||||
minimum := box.MinimumSize()
|
||||
box.SetBounds(image.Rectangle {
|
||||
Min: dot,
|
||||
Max: dot.Add(image.Pt(minimum.X, hints.Bounds.Dy())),
|
||||
})
|
||||
dot.X += minimum.X
|
||||
}
|
||||
|
||||
width := dot.X - hints.Bounds.Min.X
|
||||
offset := 0
|
||||
|
||||
switch hints.AlignX {
|
||||
case tomo.AlignMiddle:
|
||||
offset = (hints.Bounds.Dx() - width) / 2
|
||||
case tomo.AlignEnd:
|
||||
offset = hints.Bounds.Dx() - width
|
||||
}
|
||||
for _, box := range boxes {
|
||||
box.SetBounds(box.Bounds().Add(image.Pt(offset, 0)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (contract Contract) v () bool { return contract == ContractVertical }
|
||||
func (contract Contract) h () bool { return contract == ContractHorizontal }
|
||||
220
layouts/rowcol.go
Normal file
220
layouts/rowcol.go
Normal file
@@ -0,0 +1,220 @@
|
||||
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.BoxQuerier) image.Point {
|
||||
dot := image.Point { }
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
minimum := boxes.MinimumSize(index)
|
||||
dot.Y += minimum.Y
|
||||
if dot.X < minimum.X {
|
||||
dot.X = minimum.X
|
||||
}
|
||||
}
|
||||
dot.Y += hints.Gap.Y * (boxes.Len() - 1)
|
||||
return dot
|
||||
}
|
||||
|
||||
func (row Row) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
|
||||
dot := image.Point { }
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
minimum := boxes.MinimumSize(index)
|
||||
dot.X += minimum.X
|
||||
if dot.Y < minimum.Y {
|
||||
dot.Y = minimum.Y
|
||||
}
|
||||
}
|
||||
dot.X += hints.Gap.X * (boxes.Len() - 1)
|
||||
return dot
|
||||
}
|
||||
|
||||
func (column Column) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
|
||||
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 := boxes.Len() - 1
|
||||
freeSpace := float64(hints.Bounds.Dy() - hints.Gap.Y * gaps)
|
||||
nExpanding := 0
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
if expands(index) {
|
||||
nExpanding ++
|
||||
} else {
|
||||
freeSpace -= float64(boxes.MinimumSize(index).Y)
|
||||
}
|
||||
}
|
||||
expandingSize = freeSpace / float64(nExpanding)
|
||||
}
|
||||
|
||||
// determine width
|
||||
width := 0
|
||||
if hints.OverflowX {
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
minimum := boxes.MinimumSize(index)
|
||||
if width < minimum.X { width = minimum.X }
|
||||
}
|
||||
} else {
|
||||
width = hints.Bounds.Dx()
|
||||
}
|
||||
|
||||
// arrange
|
||||
dot := hints.Bounds.Min
|
||||
bounds := make([]image.Rectangle, boxes.Len())
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
if index > 0 { dot.Y += hints.Gap.Y }
|
||||
|
||||
// determine height
|
||||
height := boxes.MinimumSize(index).Y
|
||||
if hints.OverflowY {
|
||||
height = boxes.RecommendedHeight(index, width)
|
||||
} else {
|
||||
if expands(index) {
|
||||
height = int(expandingSize)
|
||||
}
|
||||
}
|
||||
|
||||
// store bounds of this box
|
||||
bounds[index] = image.Rectangle {
|
||||
Min: dot,
|
||||
Max: dot.Add(image.Pt(width, height)),
|
||||
}
|
||||
dot.Y += height
|
||||
}
|
||||
|
||||
// align
|
||||
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 index := 0; index < boxes.Len(); index ++ {
|
||||
boxes.SetBounds(index, bounds[index].Add(image.Pt(0, offset)))
|
||||
}
|
||||
}
|
||||
|
||||
func (row Row) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
|
||||
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 := boxes.Len() - 1
|
||||
freeSpace := float64(hints.Bounds.Dx() - hints.Gap.X * gaps)
|
||||
nExpanding := 0
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
if expands(index) {
|
||||
nExpanding ++
|
||||
} else {
|
||||
freeSpace -= float64(boxes.MinimumSize(index).X)
|
||||
}
|
||||
}
|
||||
expandingSize = freeSpace / float64(nExpanding)
|
||||
}
|
||||
|
||||
// determine height
|
||||
height := 0
|
||||
if hints.OverflowY {
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
minimum := boxes.MinimumSize(index)
|
||||
if height < minimum.Y { height = minimum.Y }
|
||||
}
|
||||
} else {
|
||||
height = hints.Bounds.Dy()
|
||||
}
|
||||
|
||||
// arrange
|
||||
dot := hints.Bounds.Min
|
||||
bounds := make([]image.Rectangle, boxes.Len())
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
if index > 0 { dot.X += hints.Gap.X }
|
||||
|
||||
// determine width
|
||||
width := boxes.MinimumSize(index).X
|
||||
if hints.OverflowY {
|
||||
width = boxes.RecommendedHeight(index, height)
|
||||
} else {
|
||||
if expands(index) {
|
||||
width = int(expandingSize)
|
||||
}
|
||||
}
|
||||
|
||||
// store bounds
|
||||
bounds[index] = image.Rectangle {
|
||||
Min: dot,
|
||||
Max: dot.Add(image.Pt(width, height)),
|
||||
}
|
||||
dot.X += width
|
||||
}
|
||||
|
||||
// align
|
||||
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 index := 0; index < boxes.Len(); index ++ {
|
||||
boxes.SetBounds(index, bounds[index].Add(image.Pt(offset, 0)))
|
||||
}
|
||||
}
|
||||
|
||||
func (column Column) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
|
||||
height := 0
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
height += boxes.RecommendedHeight(index, width)
|
||||
}
|
||||
height += hints.Gap.Y * (boxes.Len() - 1)
|
||||
return height
|
||||
}
|
||||
|
||||
func (row Row) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
|
||||
height := 0
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
minimum := boxes.MinimumSize(index)
|
||||
boxHeight := boxes.RecommendedHeight(index, minimum.X)
|
||||
if boxHeight > height { height = boxHeight }
|
||||
}
|
||||
return height
|
||||
}
|
||||
|
||||
func (column Column) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
|
||||
width := 0
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
minimum := boxes.MinimumSize(index)
|
||||
boxWidth := boxes.RecommendedWidth(index, minimum.Y)
|
||||
if boxWidth > width { width = boxWidth }
|
||||
}
|
||||
return width
|
||||
}
|
||||
|
||||
func (row Row) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
|
||||
width := 0
|
||||
for index := 0; index < boxes.Len(); index ++ {
|
||||
width += boxes.RecommendedWidth(index, height)
|
||||
}
|
||||
width += hints.Gap.X * (boxes.Len() - 1)
|
||||
return width
|
||||
}
|
||||
115
menu.go
Normal file
115
menu.go
Normal file
@@ -0,0 +1,115 @@
|
||||
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.Object
|
||||
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.SetAttr(tomo.ALayout(layouts.ContractVertical))
|
||||
|
||||
if !menu.torn {
|
||||
menu.tearLine = menu.newTearLine()
|
||||
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.rootContainer.SetTag("menu", true)
|
||||
|
||||
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 (this *Menu) newTearLine () tomo.Object {
|
||||
tearLine := tomo.NewBox()
|
||||
tearLine.SetRole(tomo.R("objects", "TearLine"))
|
||||
tearLine.SetFocusable(true)
|
||||
tearLine.OnKeyDown(func (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
return true
|
||||
})
|
||||
tearLine.OnKeyUp(func (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
this.TearOff()
|
||||
return true
|
||||
})
|
||||
tearLine.OnButtonDown(func (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
return true
|
||||
})
|
||||
tearLine.OnButtonUp(func (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if tearLine.Window().MousePosition().In(tearLine.Bounds()) {
|
||||
this.TearOff()
|
||||
}
|
||||
return true
|
||||
})
|
||||
return tearLine
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
83
menuitem.go
Normal file
83
menuitem.go
Normal file
@@ -0,0 +1,83 @@
|
||||
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 selectable memu item.
|
||||
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.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
|
||||
box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
|
||||
|
||||
box.Add(box.icon)
|
||||
box.Add(box.label)
|
||||
|
||||
box.SetInputMask(true)
|
||||
box.OnButtonDown(box.handleButtonDown)
|
||||
box.OnButtonUp(box.handleButtonUp)
|
||||
box.OnKeyDown(box.handleKeyDown)
|
||||
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) handleKeyDown (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *MenuItem) handleKeyUp (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
this.on.click.Broadcast()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *MenuItem) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *MenuItem) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if this.Window().MousePosition().In(this.Bounds()) {
|
||||
this.on.click.Broadcast()
|
||||
}
|
||||
return true
|
||||
}
|
||||
46
mimeicon.go
Normal file
46
mimeicon.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/data"
|
||||
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||
|
||||
// MimeIcon displays an icon of a MIME type.
|
||||
type MimeIcon struct {
|
||||
tomo.Box
|
||||
mime data.Mime
|
||||
size tomo.IconSize
|
||||
}
|
||||
|
||||
// NewMimeIcon creates a new icon from a MIME type.
|
||||
func NewMimeIcon (mime data.Mime, size tomo.IconSize) *MimeIcon {
|
||||
this := &MimeIcon {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
this.SetRole(tomo.R("objects", "MimeIcon"))
|
||||
this.SetIcon(mime, size)
|
||||
this.OnIconSetChange(this.handleIconSetChange)
|
||||
return this
|
||||
}
|
||||
|
||||
// SetIcon sets the MIME type and size of the icon.
|
||||
func (this *MimeIcon) SetIcon (mime data.Mime, size tomo.IconSize) {
|
||||
if this.mime == mime && this.size == size { return }
|
||||
this.mime = mime
|
||||
this.size = size
|
||||
this.setTexture(tomo.MimeIconTexture(mime, size))
|
||||
}
|
||||
|
||||
func (this *MimeIcon) handleIconSetChange () {
|
||||
this.setTexture(tomo.MimeIconTexture(this.mime, this.size))
|
||||
}
|
||||
|
||||
func (this *MimeIcon) setTexture (texture canvas.Texture) {
|
||||
this.SetAttr(tomo.ATexture(texture))
|
||||
this.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
|
||||
if texture == nil {
|
||||
this.SetAttr(tomo.AMinimumSize(0, 0))
|
||||
} else {
|
||||
bounds := texture.Bounds()
|
||||
this.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package objects
|
||||
import "math"
|
||||
import "strconv"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
@@ -16,8 +15,8 @@ type NumberInput struct {
|
||||
increment *Button
|
||||
decrement *Button
|
||||
on struct {
|
||||
enter event.FuncBroadcaster
|
||||
edit event.FuncBroadcaster
|
||||
valueChange event.FuncBroadcaster
|
||||
confirm event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,72 +28,83 @@ func NewNumberInput (value float64) *NumberInput {
|
||||
increment: NewButton(""),
|
||||
decrement: NewButton(""),
|
||||
}
|
||||
theme.Apply(box, theme.R("objects", "NumberInput", ""))
|
||||
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(theme.IconActionIncrement)
|
||||
box.decrement.SetIcon(theme.IconActionDecrement)
|
||||
box.SetAttr(tomo.ALayout(layouts.Row { true, false, false }))
|
||||
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.OnKeyUp(box.handleKeyUp)
|
||||
box.input.OnConfirm(box.handleConfirm)
|
||||
box.input.OnValueChange(box.on.valueChange.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)
|
||||
// SetValue sets the value of the input.
|
||||
func (this *NumberInput) SetValue (value float64) {
|
||||
this.input.SetText(strconv.FormatFloat(value, 'g', -1, 64))
|
||||
}
|
||||
|
||||
// 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)
|
||||
// OnValueChange specifies a function to be called when the user edits the input
|
||||
// value.
|
||||
func (this *NumberInput) OnValueChange (callback func ()) event.Cookie {
|
||||
return this.on.valueChange.Connect(callback)
|
||||
}
|
||||
|
||||
// OnConfirm specifies a function to be called when the user presses enter within
|
||||
// the number input.
|
||||
func (this *NumberInput) OnConfirm (callback func ()) event.Cookie {
|
||||
return this.on.confirm.Connect(callback)
|
||||
}
|
||||
|
||||
func (this *NumberInput) shift (by int) {
|
||||
this.SetValue(this.Value() + float64(by))
|
||||
this.on.edit.Broadcast()
|
||||
this.on.valueChange.Broadcast()
|
||||
}
|
||||
|
||||
func (this *NumberInput) handleKeyDown (key input.Key, numpad bool) {
|
||||
func (this *NumberInput) handleKeyDown (key input.Key, numpad bool) bool {
|
||||
switch key {
|
||||
case input.KeyUp: this.shift(1)
|
||||
case input.KeyDown: this.shift(-1)
|
||||
default: this.input.handleKeyDown(key, numpad)
|
||||
case input.KeyUp:
|
||||
this.shift(1)
|
||||
return true
|
||||
case input.KeyDown:
|
||||
this.shift(-1)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *NumberInput) handleScroll (x, y float64) {
|
||||
func (this *NumberInput) handleKeyUp (key input.Key, numpad bool) bool {
|
||||
switch key {
|
||||
case input.KeyUp: return true
|
||||
case input.KeyDown: return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *NumberInput) handleScroll (x, y float64) bool {
|
||||
if x == 0 {
|
||||
this.shift(-int(math.Round(y)))
|
||||
} else {
|
||||
this.input.handleScroll(x, y)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *NumberInput) handleEnter () {
|
||||
func (this *NumberInput) handleConfirm () {
|
||||
this.SetValue(this.Value())
|
||||
this.on.enter.Broadcast()
|
||||
this.on.confirm.Broadcast()
|
||||
}
|
||||
|
||||
228
scrollbar.go
228
scrollbar.go
@@ -2,7 +2,6 @@ package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
|
||||
@@ -34,20 +33,20 @@ func newScrollbar (orient string) *Scrollbar {
|
||||
}
|
||||
|
||||
this.Add(this.handle)
|
||||
|
||||
this.SetFocusable(true)
|
||||
|
||||
this.CaptureDND(true)
|
||||
this.CaptureMouse(true)
|
||||
this.CaptureScroll(true)
|
||||
this.CaptureKeyboard(true)
|
||||
|
||||
this.SetInputMask(true)
|
||||
this.OnKeyUp(this.handleKeyUp)
|
||||
this.OnKeyDown(this.handleKeyDown)
|
||||
this.OnMouseDown(this.handleMouseDown)
|
||||
this.OnMouseUp(this.handleMouseUp)
|
||||
this.OnButtonDown(this.handleButtonDown)
|
||||
this.OnButtonUp(this.handleButtonUp)
|
||||
this.OnMouseMove(this.handleMouseMove)
|
||||
this.OnScroll(this.handleScroll)
|
||||
theme.Apply(this.handle, theme.R("objects", "SliderHandle", orient))
|
||||
theme.Apply(this, theme.R("objects", "Slider", orient))
|
||||
|
||||
this.handle.SetRole(tomo.R("objects", "SliderHandle"))
|
||||
this.handle.SetTag(orient, true)
|
||||
this.SetRole(tomo.R("objects", "Slider"))
|
||||
this.SetTag(orient, true)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -61,13 +60,13 @@ func NewHorizontalScrollbar () *Scrollbar {
|
||||
return newScrollbar("horizontal")
|
||||
}
|
||||
|
||||
// Link assigns this scrollbar to a ContentBox. Closing the returned cookie will
|
||||
// unlink it.
|
||||
func (this *Scrollbar) Link (box tomo.ContentBox) event.Cookie {
|
||||
// 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)
|
||||
this.SetAttr(tomo.ALayout(this.layout))
|
||||
return this.linkCookie
|
||||
}
|
||||
|
||||
@@ -80,12 +79,20 @@ func (this *Scrollbar) handleLinkedContentBoundsChange () {
|
||||
} else {
|
||||
this.layout.value = this.layout.contentPos() / trackLength
|
||||
}
|
||||
this.SetLayout(this.layout)
|
||||
this.SetAttr(tomo.ALayout(this.layout))
|
||||
if this.layout.value != previousValue {
|
||||
this.on.valueChange.Broadcast()
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -106,49 +113,77 @@ func (this *Scrollbar) SetValue (value float64) {
|
||||
|
||||
}
|
||||
|
||||
// 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.
|
||||
// OnValueChange specifies a function to be called when the user changes the
|
||||
// position of the scrollbar.
|
||||
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
|
||||
// PageSize returns the scroll distance of a page.
|
||||
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 {
|
||||
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)
|
||||
return viewport.Dx()
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Scrollbar) handleMouseDown (button input.Button) {
|
||||
pointer := this.MousePosition()
|
||||
// StepSize returns the scroll distance of a step.
|
||||
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) handleKeyUp (key input.Key, numpad bool) bool {
|
||||
switch key {
|
||||
case input.KeyUp, input.KeyLeft: return true
|
||||
case input.KeyDown, input.KeyRight: return true
|
||||
case input.KeyPageUp: return true
|
||||
case input.KeyPageDown: return true
|
||||
case input.KeyHome: return true
|
||||
case input.KeyEnd: return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *Scrollbar) handleKeyDown (key input.Key, numpad bool) bool {
|
||||
modifiers := this.Window().Modifiers()
|
||||
|
||||
switch key {
|
||||
case input.KeyUp, input.KeyLeft:
|
||||
if modifiers.Alt {
|
||||
this.SetValue(0)
|
||||
} else {
|
||||
this.scrollBy(this.StepSize())
|
||||
}
|
||||
return true
|
||||
case input.KeyDown, input.KeyRight:
|
||||
if modifiers.Alt {
|
||||
this.SetValue(1)
|
||||
} else {
|
||||
this.scrollBy(-this.StepSize())
|
||||
}
|
||||
case input.KeyPageUp:
|
||||
this.scrollBy(this.PageSize())
|
||||
return true
|
||||
case input.KeyPageDown:
|
||||
this.scrollBy(-this.PageSize())
|
||||
return true
|
||||
case input.KeyHome:
|
||||
this.SetValue(0)
|
||||
return true
|
||||
case input.KeyEnd:
|
||||
this.SetValue(1)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *Scrollbar) handleButtonDown (button input.Button) bool {
|
||||
pointer := this.Window().MousePosition()
|
||||
handle := this.handle.Bounds()
|
||||
|
||||
within := pointer.In(handle)
|
||||
@@ -173,38 +208,52 @@ func (this *Scrollbar) handleMouseDown (button input.Button) {
|
||||
}
|
||||
case input.ButtonMiddle:
|
||||
if above {
|
||||
this.scrollBy(this.pageSize())
|
||||
this.scrollBy(this.PageSize())
|
||||
} else {
|
||||
this.scrollBy(-this.pageSize())
|
||||
this.scrollBy(-this.PageSize())
|
||||
}
|
||||
case input.ButtonRight:
|
||||
if above {
|
||||
this.scrollBy(this.stepSize())
|
||||
this.scrollBy(this.StepSize())
|
||||
} else {
|
||||
this.scrollBy(-this.stepSize())
|
||||
this.scrollBy(-this.StepSize())
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Scrollbar) handleMouseUp (button input.Button) {
|
||||
if button != input.ButtonLeft || !this.dragging { return }
|
||||
func (this *Scrollbar) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft || !this.dragging { return true }
|
||||
this.dragging = false
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Scrollbar) handleMouseMove () {
|
||||
if !this.dragging { return }
|
||||
func (this *Scrollbar) handleMouseMove () bool {
|
||||
if !this.dragging { return false }
|
||||
this.drag()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Scrollbar) handleScroll (x, y float64) {
|
||||
if this.layout.linked == nil { return }
|
||||
func (this *Scrollbar) handleScroll (x, y float64) bool {
|
||||
if this.layout.linked == nil { return false }
|
||||
|
||||
delta := (x + y)
|
||||
if this.layout.vertical {
|
||||
x = 0
|
||||
y = delta
|
||||
} else {
|
||||
x = delta
|
||||
y = 0
|
||||
}
|
||||
|
||||
this.layout.linked.ScrollTo (
|
||||
this.layout.linked.ContentBounds().Min.
|
||||
Sub(image.Pt(int(x), int(y))))
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Scrollbar) drag () {
|
||||
pointer := this.MousePosition().Sub(this.dragOffset)
|
||||
pointer := this.Window().MousePosition().Sub(this.dragOffset)
|
||||
gutter := this.InnerBounds()
|
||||
handle := this.handle.Bounds()
|
||||
|
||||
@@ -229,22 +278,6 @@ func (this *Scrollbar) fallbackDragOffset () image.Point {
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Scrollbar) pageSize () int {
|
||||
if this.layout.linked == nil { return 0 }
|
||||
viewport := this.layout.linked.InnerBounds()
|
||||
if this.layout.vertical {
|
||||
return viewport.Dy()
|
||||
} else {
|
||||
return viewport.Dx()
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Scrollbar) stepSize () int {
|
||||
// FIXME: this should not be hardcoded, need to get base font metrics
|
||||
// from theme somehow. should be (emspace, lineheight)
|
||||
return 16
|
||||
}
|
||||
|
||||
func (this *Scrollbar) scrollBy (distance int) {
|
||||
if this.layout.linked == nil { return }
|
||||
var vector image.Point; if this.layout.vertical {
|
||||
@@ -274,23 +307,23 @@ func (this *scrollbarCookie) Close () {
|
||||
cookie.Close()
|
||||
}
|
||||
this.owner.layout.linked = nil
|
||||
this.owner.SetLayout(this.owner.layout)
|
||||
this.owner.SetAttr(tomo.ALayout(this.owner.layout))
|
||||
}
|
||||
|
||||
type scrollbarLayout struct {
|
||||
vertical bool
|
||||
value float64
|
||||
linked tomo.ContentBox
|
||||
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 (scrollbarLayout) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
|
||||
if boxes.Len() != 1 { return image.Pt(0, 0) }
|
||||
return boxes.MinimumSize(0)
|
||||
}
|
||||
|
||||
func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
if len(boxes) != 1 { return }
|
||||
handle := image.Rectangle { Max: boxes[0].MinimumSize() }
|
||||
func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
|
||||
if boxes.Len() != 1 { return }
|
||||
handle := image.Rectangle { Max: boxes.MinimumSize(0) }
|
||||
gutter := hints.Bounds
|
||||
|
||||
var gutterLength float64;
|
||||
@@ -307,10 +340,11 @@ func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
handleLength := gutterLength * this.viewportContentRatio()
|
||||
if handleLength < handleMin { handleLength = handleMin }
|
||||
if handleLength >= gutterLength {
|
||||
// just hide the handle if it isn't needed.
|
||||
// TODO: we need a way to hide and show boxes, this is janky as
|
||||
// fuck
|
||||
boxes[0].SetBounds(image.Rect(-16, -16, 0, 0))
|
||||
// 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.SetBounds(0, image.Rect(-32, -32, -16, -16))
|
||||
return
|
||||
}
|
||||
if this.vertical {
|
||||
@@ -330,10 +364,18 @@ func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
handle = handle.Sub(handleOffset).Add(gutter.Min)
|
||||
|
||||
// place handle
|
||||
boxes[0].SetBounds(handle)
|
||||
boxes.SetBounds(0, handle)
|
||||
|
||||
}
|
||||
|
||||
func (this scrollbarLayout) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
|
||||
return this.MinimumSize(hints, boxes).X
|
||||
}
|
||||
|
||||
func (this scrollbarLayout) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, 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()
|
||||
@@ -341,9 +383,9 @@ func (this scrollbarLayout) viewportContentRatio () float64 {
|
||||
|
||||
func (this scrollbarLayout) viewportLength () float64 {
|
||||
if this.vertical {
|
||||
return float64(this.linked.InnerBounds().Dy())
|
||||
return float64(this.linked.GetBox().InnerBounds().Dy())
|
||||
} else {
|
||||
return float64(this.linked.InnerBounds().Dx())
|
||||
return float64(this.linked.GetBox().InnerBounds().Dx())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,9 @@ 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/tomo/theme"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
|
||||
// ScrollSide determines which Scrollbars are active in a ScrollContainer.
|
||||
type ScrollSide int; const (
|
||||
@@ -39,40 +40,54 @@ func (sides ScrollSide) String () string {
|
||||
// ScrollContainer couples a ContentBox with one or two Scrollbars.
|
||||
type ScrollContainer struct {
|
||||
tomo.ContainerBox
|
||||
layout *scrollContainerLayout
|
||||
|
||||
root tomo.ContentObject
|
||||
horizontal *Scrollbar
|
||||
vertical *Scrollbar
|
||||
|
||||
horizontalCookie event.Cookie
|
||||
verticalCookie event.Cookie
|
||||
|
||||
on struct {
|
||||
valueChange event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
this.vertical = NewVerticalScrollbar()
|
||||
this.vertical.OnValueChange(this.handleValueChange)
|
||||
this.Add(this.vertical)
|
||||
}
|
||||
if sides.Horizontal() {
|
||||
this.layout.horizontal = NewHorizontalScrollbar()
|
||||
this.Add(this.layout.horizontal)
|
||||
this.horizontal = NewHorizontalScrollbar()
|
||||
this.horizontal.OnValueChange(this.handleValueChange)
|
||||
this.Add(this.horizontal)
|
||||
}
|
||||
this.CaptureScroll(true)
|
||||
this.OnScroll(this.handleScroll)
|
||||
theme.Apply(this, theme.R("objects", "ScrollContainer", sides.String()))
|
||||
this.SetLayout(this.layout)
|
||||
this.OnKeyDown(this.handleKeyDown)
|
||||
this.OnKeyUp(this.handleKeyUp)
|
||||
this.SetRole(tomo.R("objects", "ScrollContainer"))
|
||||
this.SetTag(sides.String(), true)
|
||||
if sides == ScrollHorizontal {
|
||||
this.SetAttr(tomo.ALayout(layouts.NewGrid(true)(true, false)))
|
||||
} else {
|
||||
this.SetAttr(tomo.ALayout(layouts.NewGrid(true, false)(true, false)))
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
// SetRoot sets the root child of the ScrollContainer. There can only be one at
|
||||
// a time, and setting it will remove and unlink the current child if there is
|
||||
// one.
|
||||
func (this *ScrollContainer) SetRoot (root tomo.ContentBox) {
|
||||
if this.layout.root != nil {
|
||||
// delete root and close cookies
|
||||
this.Delete(this.layout.root)
|
||||
func (this *ScrollContainer) SetRoot (root tomo.ContentObject) {
|
||||
if this.root != nil {
|
||||
// remove root and close cookies
|
||||
this.Remove(this.root)
|
||||
if this.horizontalCookie != nil {
|
||||
this.horizontalCookie.Close()
|
||||
this.horizontalCookie = nil
|
||||
@@ -82,109 +97,143 @@ func (this *ScrollContainer) SetRoot (root tomo.ContentBox) {
|
||||
this.verticalCookie = nil
|
||||
}
|
||||
}
|
||||
this.layout.root = root
|
||||
this.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)
|
||||
case this.vertical != nil:
|
||||
this.Insert(root, this.vertical)
|
||||
case this.horizontal != nil:
|
||||
this.Insert(root, this.horizontal)
|
||||
default:
|
||||
this.Add(root)
|
||||
}
|
||||
|
||||
// link root and remember cookies
|
||||
if this.layout.horizontal != nil {
|
||||
this.horizontalCookie = this.layout.horizontal.Link(root)
|
||||
if this.horizontal != nil {
|
||||
this.horizontalCookie = this.horizontal.Link(root)
|
||||
}
|
||||
if this.layout.vertical != nil {
|
||||
this.verticalCookie = this.layout.vertical.Link(root)
|
||||
if this.vertical != nil {
|
||||
this.verticalCookie = this.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.horizontal != nil {
|
||||
x = this.horizontal.Value()
|
||||
}
|
||||
if this.layout.vertical != nil {
|
||||
y = this.layout.vertical.Value()
|
||||
if this.vertical != nil {
|
||||
y = this.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))))
|
||||
}
|
||||
|
||||
type scrollContainerLayout struct {
|
||||
root tomo.ContentBox
|
||||
horizontal *Scrollbar
|
||||
vertical *Scrollbar
|
||||
}
|
||||
|
||||
func (this *scrollContainerLayout) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
|
||||
var minimum image.Point; if this.root != nil {
|
||||
minimum = this.root.MinimumSize()
|
||||
}
|
||||
// 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.horizontal != nil {
|
||||
minimum.Y += this.horizontal.MinimumSize().Y
|
||||
this.horizontal.SetValue(x)
|
||||
}
|
||||
if this.vertical != nil {
|
||||
minimum.X += this.vertical.MinimumSize().X
|
||||
minimum.Y = max(minimum.Y, this.vertical.MinimumSize().Y)
|
||||
}
|
||||
if this.horizontal != nil {
|
||||
minimum.X = max(minimum.X, this.horizontal.MinimumSize().X)
|
||||
}
|
||||
return minimum
|
||||
}
|
||||
|
||||
func (this *scrollContainerLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
rootBounds := hints.Bounds
|
||||
if this.horizontal != nil {
|
||||
rootBounds.Max.Y -= this.horizontal.MinimumSize().Y
|
||||
}
|
||||
if this.vertical != nil {
|
||||
rootBounds.Max.X -= this.vertical.MinimumSize().X
|
||||
}
|
||||
if this.root != nil {
|
||||
this.root.SetBounds(rootBounds)
|
||||
}
|
||||
if this.horizontal != nil {
|
||||
this.horizontal.SetBounds(image.Rect (
|
||||
hints.Bounds.Min.X,
|
||||
rootBounds.Max.Y,
|
||||
rootBounds.Max.X,
|
||||
hints.Bounds.Max.Y))
|
||||
}
|
||||
if this.vertical != nil {
|
||||
this.vertical.SetBounds(image.Rect (
|
||||
rootBounds.Max.X,
|
||||
hints.Bounds.Min.Y,
|
||||
hints.Bounds.Max.X,
|
||||
rootBounds.Max.Y))
|
||||
this.vertical.SetValue(y)
|
||||
}
|
||||
}
|
||||
|
||||
func max (x, y int) int {
|
||||
if x > y { return x }
|
||||
return y
|
||||
// OnValueChange specifies a function to be called when the user changes the
|
||||
// position of the horizontal or vertical scrollbars.
|
||||
func (this *ScrollContainer) OnValueChange (callback func ()) event.Cookie {
|
||||
return this.on.valueChange.Connect(callback)
|
||||
}
|
||||
|
||||
// PageSize returns the scroll distance of a page.
|
||||
func (this *ScrollContainer) PageSize () image.Point {
|
||||
page := image.Point { }
|
||||
if this.horizontal != nil {
|
||||
page.X = this.horizontal.PageSize()
|
||||
}
|
||||
if this.vertical != nil {
|
||||
page.Y = this.vertical.PageSize()
|
||||
}
|
||||
return page
|
||||
}
|
||||
|
||||
// StepSize returns the scroll distance of a step.
|
||||
func (this *ScrollContainer) StepSize () image.Point {
|
||||
page := image.Point { }
|
||||
if this.horizontal != nil {
|
||||
page.X = this.horizontal.StepSize()
|
||||
}
|
||||
if this.vertical != nil {
|
||||
page.Y = this.vertical.StepSize()
|
||||
}
|
||||
return page
|
||||
}
|
||||
|
||||
func (this *ScrollContainer) handleValueChange () {
|
||||
this.on.valueChange.Broadcast()
|
||||
}
|
||||
|
||||
func (this *ScrollContainer) scrollBy (vector image.Point) {
|
||||
if this.root == nil { return }
|
||||
if vector == (image.Point { }) { return }
|
||||
this.root.ScrollTo (
|
||||
this.root.ContentBounds().Min.
|
||||
Sub(vector))
|
||||
}
|
||||
|
||||
func (this *ScrollContainer) handleScroll (x, y float64) bool {
|
||||
if this.root == nil { return false }
|
||||
this.scrollBy(image.Pt(int(x), int(y)))
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *ScrollContainer) handleKeyDown (key input.Key, numpad bool) bool {
|
||||
modifiers := this.Window().Modifiers()
|
||||
vector := image.Point { }
|
||||
switch key {
|
||||
case input.KeyPageUp:
|
||||
if modifiers.Shift {
|
||||
vector.X -= this.PageSize().X
|
||||
} else {
|
||||
vector.Y -= this.PageSize().Y
|
||||
}
|
||||
this.scrollBy(vector)
|
||||
return true
|
||||
case input.KeyPageDown:
|
||||
if modifiers.Shift {
|
||||
vector.X += this.PageSize().X
|
||||
} else {
|
||||
vector.Y += this.PageSize().Y
|
||||
}
|
||||
this.scrollBy(vector)
|
||||
return true
|
||||
case input.KeyUp:
|
||||
if modifiers.Shift {
|
||||
vector.X -= this.StepSize().X
|
||||
} else {
|
||||
vector.Y -= this.StepSize().Y
|
||||
}
|
||||
this.scrollBy(vector)
|
||||
return true
|
||||
case input.KeyDown:
|
||||
if modifiers.Shift {
|
||||
vector.X += this.StepSize().X
|
||||
} else {
|
||||
vector.Y += this.StepSize().Y
|
||||
}
|
||||
this.scrollBy(vector)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *ScrollContainer) handleKeyUp (key input.Key, numpad bool) bool {
|
||||
switch key {
|
||||
case input.KeyPageUp: return true
|
||||
case input.KeyPageDown: return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
|
||||
// Separator is a line for visually separating elements.
|
||||
type Separator struct {
|
||||
@@ -13,6 +12,6 @@ func NewSeparator () *Separator {
|
||||
this := &Separator {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
theme.Apply(this, theme.R("objects", "Separator", ""))
|
||||
this.SetRole(tomo.R("objects", "Separator"))
|
||||
return this
|
||||
}
|
||||
|
||||
139
slider.go
139
slider.go
@@ -1,8 +1,8 @@
|
||||
package objects
|
||||
|
||||
import "math"
|
||||
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"
|
||||
|
||||
@@ -16,7 +16,8 @@ type Slider struct {
|
||||
step float64
|
||||
|
||||
on struct {
|
||||
slide event.FuncBroadcaster
|
||||
valueChange event.FuncBroadcaster
|
||||
confirm event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,26 +35,27 @@ func newSlider (orient string, value float64) *Slider {
|
||||
},
|
||||
layout: sliderLayout {
|
||||
vertical: orient == "vertical",
|
||||
value: math.NaN(),
|
||||
},
|
||||
step: 0.05,
|
||||
}
|
||||
|
||||
this.Add(this.handle)
|
||||
this.SetFocusable(true)
|
||||
|
||||
this.CaptureDND(true)
|
||||
this.CaptureMouse(true)
|
||||
this.CaptureScroll(true)
|
||||
this.CaptureKeyboard(true)
|
||||
|
||||
this.SetValue(value)
|
||||
|
||||
this.SetInputMask(true)
|
||||
this.OnKeyUp(this.handleKeyUp)
|
||||
this.OnKeyDown(this.handleKeyDown)
|
||||
this.OnMouseDown(this.handleMouseDown)
|
||||
this.OnMouseUp(this.handleMouseUp)
|
||||
this.OnButtonDown(this.handleButtonDown)
|
||||
this.OnButtonUp(this.handleButtonUp)
|
||||
this.OnMouseMove(this.handleMouseMove)
|
||||
this.OnScroll(this.handleScroll)
|
||||
theme.Apply(this.handle, theme.R("objects", "SliderHandle", orient))
|
||||
theme.Apply(this, theme.R("objects", "Slider", orient))
|
||||
|
||||
this.handle.SetRole(tomo.R("objects", "SliderHandle"))
|
||||
this.handle.SetTag(orient, true)
|
||||
this.SetRole(tomo.R("objects", "Slider"))
|
||||
this.SetTag(orient, true)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -73,7 +75,7 @@ func (this *Slider) SetValue (value float64) {
|
||||
if value > 1 { value = 1 }
|
||||
if value == this.layout.value { return }
|
||||
this.layout.value = value
|
||||
this.SetLayout(this.layout)
|
||||
this.SetAttr(tomo.ALayout(this.layout))
|
||||
}
|
||||
|
||||
// Value returns the value of the slider between 0 and 1.
|
||||
@@ -83,11 +85,17 @@ func (this *Slider) Value () float64 {
|
||||
|
||||
// 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) OnValueChange (callback func ()) event.Cookie {
|
||||
return this.on.valueChange.Connect(callback)
|
||||
}
|
||||
|
||||
func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
|
||||
// OnConfirm specifies a function to be called when the user stops moving the
|
||||
// slider.
|
||||
func (this *Slider) OnConfirm (callback func ()) event.Cookie {
|
||||
return this.on.confirm.Connect(callback)
|
||||
}
|
||||
|
||||
func (this *Slider) handleKeyDown (key input.Key, numpad bool) bool {
|
||||
var increment float64; if this.layout.vertical {
|
||||
increment = -0.05
|
||||
} else {
|
||||
@@ -96,30 +104,45 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
|
||||
|
||||
switch key {
|
||||
case input.KeyUp, input.KeyLeft:
|
||||
if this.Modifiers().Alt {
|
||||
if this.Window().Modifiers().Alt {
|
||||
this.SetValue(0)
|
||||
} else {
|
||||
this.SetValue(this.Value() - increment)
|
||||
}
|
||||
this.on.slide.Broadcast()
|
||||
this.on.valueChange.Broadcast()
|
||||
return true
|
||||
case input.KeyDown, input.KeyRight:
|
||||
if this.Modifiers().Alt {
|
||||
if this.Window().Modifiers().Alt {
|
||||
this.SetValue(1)
|
||||
} else {
|
||||
this.SetValue(this.Value() + increment)
|
||||
}
|
||||
this.on.slide.Broadcast()
|
||||
this.on.valueChange.Broadcast()
|
||||
return true
|
||||
case input.KeyHome:
|
||||
this.SetValue(0)
|
||||
this.on.slide.Broadcast()
|
||||
this.on.valueChange.Broadcast()
|
||||
return true
|
||||
case input.KeyEnd:
|
||||
this.SetValue(1)
|
||||
this.on.slide.Broadcast()
|
||||
this.on.valueChange.Broadcast()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *Slider) handleMouseDown (button input.Button) {
|
||||
pointer := this.MousePosition()
|
||||
func (this *Slider) handleKeyUp (key input.Key, numpad bool) bool {
|
||||
switch key {
|
||||
case input.KeyUp, input.KeyLeft: return true
|
||||
case input.KeyDown, input.KeyRight: return true
|
||||
case input.KeyHome: return true
|
||||
case input.KeyEnd: return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *Slider) handleButtonDown (button input.Button) bool {
|
||||
pointer := this.Window().MousePosition()
|
||||
handle := this.handle.Bounds()
|
||||
|
||||
within := pointer.In(handle)
|
||||
@@ -145,40 +168,50 @@ func (this *Slider) handleMouseDown (button input.Button) {
|
||||
case input.ButtonMiddle:
|
||||
if above {
|
||||
this.SetValue(0)
|
||||
this.on.slide.Broadcast()
|
||||
this.on.valueChange.Broadcast()
|
||||
this.on.confirm.Broadcast()
|
||||
} else {
|
||||
this.SetValue(1)
|
||||
this.on.slide.Broadcast()
|
||||
this.on.valueChange.Broadcast()
|
||||
this.on.confirm.Broadcast()
|
||||
}
|
||||
case input.ButtonRight:
|
||||
if above {
|
||||
this.SetValue(this.Value() - this.step)
|
||||
this.on.slide.Broadcast()
|
||||
this.on.valueChange.Broadcast()
|
||||
this.on.confirm.Broadcast()
|
||||
} else {
|
||||
this.SetValue(this.Value() + this.step)
|
||||
this.on.slide.Broadcast()
|
||||
this.on.valueChange.Broadcast()
|
||||
this.on.confirm.Broadcast()
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Slider) handleMouseUp (button input.Button) {
|
||||
if button != input.ButtonLeft || !this.dragging { return }
|
||||
func (this *Slider) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft || !this.dragging { return true }
|
||||
this.dragging = false
|
||||
this.on.confirm.Broadcast()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Slider) handleMouseMove () {
|
||||
if !this.dragging { return }
|
||||
func (this *Slider) handleMouseMove () bool {
|
||||
if !this.dragging { return false }
|
||||
this.drag()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Slider) handleScroll (x, y float64) {
|
||||
func (this *Slider) handleScroll (x, y float64) bool {
|
||||
delta := (x + y) * 0.005
|
||||
this.SetValue(this.Value() + delta)
|
||||
this.on.slide.Broadcast()
|
||||
this.on.valueChange.Broadcast()
|
||||
this.on.confirm.Broadcast()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Slider) drag () {
|
||||
pointer := this.MousePosition().Sub(this.dragOffset)
|
||||
pointer := this.Window().MousePosition().Sub(this.dragOffset)
|
||||
gutter := this.InnerBounds()
|
||||
handle := this.handle.Bounds()
|
||||
|
||||
@@ -192,7 +225,7 @@ func (this *Slider) drag () {
|
||||
float64(pointer.X) /
|
||||
float64(gutter.Dx() - handle.Dx()))
|
||||
}
|
||||
this.on.slide.Broadcast()
|
||||
this.on.valueChange.Broadcast()
|
||||
}
|
||||
|
||||
func (this *Slider) fallbackDragOffset () image.Point {
|
||||
@@ -210,31 +243,37 @@ type sliderLayout struct {
|
||||
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 (sliderLayout) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
|
||||
if boxes.Len() != 1 { return image.Pt(0, 0) }
|
||||
return boxes.MinimumSize(0)
|
||||
}
|
||||
|
||||
func (this sliderLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
|
||||
if len(boxes) != 1 { return }
|
||||
handle := image.Rectangle { Max: boxes[0].MinimumSize() }
|
||||
func (this sliderLayout) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
|
||||
if boxes.Len() != 1 { return }
|
||||
handle := image.Rectangle { Max: boxes.MinimumSize(0) }
|
||||
gutter := hints.Bounds
|
||||
|
||||
if this.vertical {
|
||||
height := gutter.Dy() - handle.Dy()
|
||||
offset := int(float64(height) * (1 - this.value))
|
||||
handle.Max.X = gutter.Dx()
|
||||
boxes[0].SetBounds (
|
||||
handle.
|
||||
Add(image.Pt(0, offset)).
|
||||
Add(gutter.Min))
|
||||
boxes.SetBounds (
|
||||
0,
|
||||
handle.Add(image.Pt(0, offset)).Add(gutter.Min))
|
||||
} else {
|
||||
width := gutter.Dx() - handle.Dx()
|
||||
offset := int(float64(width) * this.value)
|
||||
handle.Max.Y = gutter.Dy()
|
||||
boxes[0].SetBounds (
|
||||
handle.
|
||||
Add(image.Pt(offset, 0)).
|
||||
Add(gutter.Min))
|
||||
boxes.SetBounds (
|
||||
0,
|
||||
handle.Add(image.Pt(offset, 0)).Add(gutter.Min))
|
||||
}
|
||||
}
|
||||
|
||||
func (this sliderLayout) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
|
||||
return this.MinimumSize(hints, boxes).X
|
||||
}
|
||||
|
||||
func (this sliderLayout) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
|
||||
return this.MinimumSize(hints, boxes).Y
|
||||
}
|
||||
|
||||
182
swatch.go
Normal file
182
swatch.go
Normal file
@@ -0,0 +1,182 @@
|
||||
package objects
|
||||
|
||||
import "log"
|
||||
import "image"
|
||||
import "image/color"
|
||||
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/tomo/canvas"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
|
||||
// Swatch displays a color, allowing the user to edit it by clicking on it.
|
||||
type Swatch struct {
|
||||
tomo.CanvasBox
|
||||
value color.Color
|
||||
editing bool
|
||||
label string
|
||||
on struct {
|
||||
valueChange event.FuncBroadcaster
|
||||
confirm event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
// NewSwatch creates a new swatch with the given color.
|
||||
func NewSwatch (value color.Color) *Swatch {
|
||||
swatch := &Swatch {
|
||||
CanvasBox: tomo.NewCanvasBox(),
|
||||
}
|
||||
swatch.SetRole(tomo.R("objects", "Swatch"))
|
||||
swatch.SetDrawer(swatch)
|
||||
swatch.SetValue(value)
|
||||
|
||||
swatch.OnButtonDown(swatch.handleButtonDown)
|
||||
swatch.OnButtonUp(swatch.handleButtonUp)
|
||||
swatch.OnKeyDown(swatch.handleKeyDown)
|
||||
swatch.OnKeyUp(swatch.handleKeyUp)
|
||||
swatch.SetFocusable(true)
|
||||
return swatch
|
||||
}
|
||||
|
||||
// Value returns the color of the swatch.
|
||||
func (this *Swatch) Value () color.Color {
|
||||
return this.value
|
||||
}
|
||||
|
||||
// SetValue sets the color of the swatch.
|
||||
func (this *Swatch) SetValue (value color.Color) {
|
||||
this.value = value
|
||||
if value == nil { value = color.Transparent }
|
||||
this.Invalidate()
|
||||
}
|
||||
|
||||
// OnValueChange specifies a function to be called when the swatch's color
|
||||
// is changed by the user.
|
||||
func (this *Swatch) OnValueChange (callback func ()) event.Cookie {
|
||||
return this.on.valueChange.Connect(callback)
|
||||
}
|
||||
|
||||
// RGBA satisfies the color.Color interface
|
||||
func (this *Swatch) RGBA () (r, g, b, a uint32) {
|
||||
if this.value == nil { return }
|
||||
return this.value.RGBA()
|
||||
}
|
||||
|
||||
// OnConfirm specifies a function to be called when the user selects "OK" in the
|
||||
// color picker.
|
||||
func (this *Swatch) OnConfirm (callback func ()) event.Cookie {
|
||||
return this.on.confirm.Connect(callback)
|
||||
}
|
||||
|
||||
// Choose creates a modal that allows the user to edit the color of the swatch.
|
||||
func (this *Swatch) Choose () {
|
||||
if this.editing { return }
|
||||
|
||||
var err error
|
||||
var window tomo.Window
|
||||
if parent := this.Window(); parent != nil {
|
||||
window, err = parent.NewChild(image.Rectangle { })
|
||||
} else {
|
||||
window, err = tomo.NewWindow(image.Rectangle { })
|
||||
}
|
||||
if err != nil {
|
||||
log.Println("objects: could not create swatch modal:", err)
|
||||
return
|
||||
}
|
||||
if this.label == "" {
|
||||
window.SetTitle("Select Color")
|
||||
} else {
|
||||
window.SetTitle(this.label)
|
||||
}
|
||||
|
||||
committed := false
|
||||
|
||||
colorPicker := NewColorPicker(this.Value())
|
||||
colorPicker.OnValueChange(func () {
|
||||
this.userSetValue(colorPicker.Value())
|
||||
})
|
||||
|
||||
hexInput := NewTextInput("TODO")
|
||||
|
||||
colorMemory := this.value
|
||||
cancelButton := NewButton("Cancel")
|
||||
cancelButton.SetIcon(tomo.IconDialogCancel)
|
||||
cancelButton.OnClick(func () {
|
||||
window.Close()
|
||||
})
|
||||
okButton := NewButton("OK")
|
||||
okButton.SetFocused(true)
|
||||
okButton.SetIcon(tomo.IconDialogOkay)
|
||||
okButton.OnClick(func () {
|
||||
committed = true
|
||||
window.Close()
|
||||
})
|
||||
|
||||
controlRow := NewInnerContainer (
|
||||
layouts.ContractHorizontal,
|
||||
cancelButton,
|
||||
okButton)
|
||||
controlRow.SetAttr(tomo.AAlign(tomo.AlignEnd, tomo.AlignMiddle))
|
||||
window.SetRoot(NewOuterContainer (
|
||||
layouts.Column { true, false },
|
||||
colorPicker,
|
||||
NewInnerContainer(layouts.Row { false, true },
|
||||
NewLabel("Hex"),
|
||||
hexInput),
|
||||
controlRow))
|
||||
window.OnClose(func () {
|
||||
if committed {
|
||||
this.on.confirm.Broadcast()
|
||||
} else {
|
||||
this.userSetValue(colorMemory)
|
||||
}
|
||||
this.editing = false
|
||||
})
|
||||
this.editing = true
|
||||
window.SetVisible(true)
|
||||
}
|
||||
|
||||
func (this *Swatch) Draw (can canvas.Canvas) {
|
||||
pen := can.Pen()
|
||||
|
||||
// transparency slash
|
||||
pen.Stroke(color.RGBA { R: 255, A: 255 })
|
||||
pen.StrokeWeight(1)
|
||||
pen.Path(this.Bounds().Min, this.Bounds().Max)
|
||||
|
||||
// color
|
||||
if this.value != nil {
|
||||
pen.StrokeWeight(0)
|
||||
pen.Fill(this.value)
|
||||
pen.Rectangle(this.Bounds())
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Swatch) userSetValue (value color.Color) {
|
||||
this.SetValue(value)
|
||||
this.on.valueChange.Broadcast()
|
||||
}
|
||||
|
||||
func (this *Swatch) handleKeyDown (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Swatch) handleKeyUp (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
this.Choose()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Swatch) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Swatch) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if this.Window().MousePosition().In(this.Bounds()) {
|
||||
this.Choose()
|
||||
}
|
||||
return true
|
||||
}
|
||||
135
tabbedcontainer.go
Normal file
135
tabbedcontainer.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
|
||||
type TabbedContainer struct {
|
||||
tomo.ContainerBox
|
||||
|
||||
leftSpacer tomo.Box
|
||||
rightSpacer tomo.Box
|
||||
tabsRow tomo.ContainerBox
|
||||
active string
|
||||
tabs []*tab
|
||||
}
|
||||
|
||||
func NewTabbedContainer () *TabbedContainer {
|
||||
container := &TabbedContainer {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
}
|
||||
container.SetRole(tomo.R("objects", "TabbedContainer"))
|
||||
container.SetAttr(tomo.ALayout(layouts.Column { false, true }))
|
||||
|
||||
container.tabsRow = tomo.NewContainerBox()
|
||||
container.tabsRow.SetRole(tomo.R("objects", "TabRow"))
|
||||
container.Add(container.tabsRow)
|
||||
|
||||
container.leftSpacer = tomo.NewBox()
|
||||
container.leftSpacer.SetRole(tomo.R("objects", "TabSpacer"))
|
||||
container.leftSpacer.SetTag("left", true)
|
||||
container.rightSpacer = tomo.NewBox()
|
||||
container.rightSpacer.SetRole(tomo.R("objects", "TabSpacer"))
|
||||
container.rightSpacer.SetTag("left", true)
|
||||
|
||||
container.ClearTabs()
|
||||
container.setTabRowLayout()
|
||||
return container
|
||||
}
|
||||
|
||||
func (this *TabbedContainer) Activate (name string) {
|
||||
if _, tab := this.findTab(this.active); tab != nil {
|
||||
tab.setActive(false)
|
||||
this.Remove(tab.root)
|
||||
}
|
||||
if _, tab := this.findTab(name); tab != nil {
|
||||
tab.setActive(true)
|
||||
this.Add(tab.root)
|
||||
} else {
|
||||
name = ""
|
||||
}
|
||||
this.active = name
|
||||
}
|
||||
|
||||
func (this *TabbedContainer) AddTab (name string, root tomo.Object) {
|
||||
tab := &tab {
|
||||
TextBox: tomo.NewTextBox(),
|
||||
name: name,
|
||||
root: root,
|
||||
}
|
||||
tab.SetRole(tomo.R("objects", "Tab"))
|
||||
tab.SetText(name)
|
||||
tab.OnButtonDown(func (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
this.Activate(name)
|
||||
return true
|
||||
})
|
||||
tab.OnButtonUp(func (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
return true
|
||||
})
|
||||
|
||||
this.tabs = append(this.tabs, tab)
|
||||
this.tabsRow.Insert(tab, this.rightSpacer)
|
||||
this.setTabRowLayout()
|
||||
|
||||
// if the row was empty before, activate this tab
|
||||
if len(this.tabs) == 1 {
|
||||
this.Activate(name)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *TabbedContainer) RemoveTab (name string) {
|
||||
index, tab := this.findTab(name)
|
||||
if index < 0 { return }
|
||||
nextIndex := index - 1
|
||||
|
||||
this.tabsRow.Remove(tab)
|
||||
this.tabs = append(this.tabs[:index], this.tabs[index - 1:]...)
|
||||
this.setTabRowLayout()
|
||||
|
||||
if nextIndex < 0 { nextIndex = 0 }
|
||||
if nextIndex >= len(this.tabs) { nextIndex = len(this.tabs) - 1 }
|
||||
if nextIndex < 0 {
|
||||
this.Activate("")
|
||||
} else {
|
||||
this.Activate(this.tabs[nextIndex].name)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *TabbedContainer) ClearTabs () {
|
||||
this.tabs = nil
|
||||
this.tabsRow.Clear()
|
||||
this.tabsRow.Add(this.leftSpacer)
|
||||
this.tabsRow.Add(this.rightSpacer)
|
||||
}
|
||||
|
||||
func (this *TabbedContainer) setTabRowLayout () {
|
||||
row := make(layouts.Row, 1 + len(this.tabs) + 1)
|
||||
row[len(row) - 1] = true
|
||||
this.tabsRow.SetAttr(tomo.ALayout(row))
|
||||
}
|
||||
|
||||
func (this *TabbedContainer) findTab (name string) (int, *tab) {
|
||||
for index, tab := range this.tabs {
|
||||
if tab.name == name { return index, tab }
|
||||
}
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
type tab struct {
|
||||
tomo.TextBox
|
||||
name string
|
||||
root tomo.Object
|
||||
}
|
||||
|
||||
func (this *tab) setActive (active bool) {
|
||||
if active {
|
||||
this.SetRole(tomo.R("objects", "Tab"))
|
||||
this.SetTag("active", false)
|
||||
} else {
|
||||
this.SetRole(tomo.R("objects", "Tab"))
|
||||
this.SetTag("active", true)
|
||||
}
|
||||
}
|
||||
|
||||
126
textinput.go
Normal file
126
textinput.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/text"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
|
||||
// TextInput is a single-line editable text box.
|
||||
type TextInput struct {
|
||||
tomo.TextBox
|
||||
text []rune
|
||||
on struct {
|
||||
valueChange event.FuncBroadcaster
|
||||
confirm event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
// NewTextInput creates a new text input containing the specified text.
|
||||
func NewTextInput (text string) *TextInput {
|
||||
this := &TextInput { TextBox: tomo.NewTextBox() }
|
||||
this.SetRole(tomo.R("objects", "TextInput"))
|
||||
this.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
|
||||
this.SetAttr(tomo.AOverflow(true, false))
|
||||
this.SetText(text)
|
||||
this.SetFocusable(true)
|
||||
this.SetSelectable(true)
|
||||
this.OnKeyDown(this.handleKeyDown)
|
||||
this.OnKeyUp(this.handleKeyUp)
|
||||
this.OnScroll(this.handleScroll)
|
||||
return this
|
||||
}
|
||||
|
||||
// SetText sets the text content of the input.
|
||||
func (this *TextInput) SetText (text string) {
|
||||
this.text = []rune(text)
|
||||
this.TextBox.SetText(text)
|
||||
}
|
||||
|
||||
// Text returns the text content of the input.
|
||||
func (this *TextInput) Text () string {
|
||||
return string(this.text)
|
||||
}
|
||||
|
||||
// OnConfirm specifies a function to be called when the user presses enter
|
||||
// within the text input.
|
||||
func (this *TextInput) OnConfirm (callback func ()) event.Cookie {
|
||||
return this.on.confirm.Connect(callback)
|
||||
}
|
||||
|
||||
// OnValueChange specifies a function to be called when the user edits the input
|
||||
// text.
|
||||
func (this *TextInput) OnValueChange (callback func ()) event.Cookie {
|
||||
return this.on.valueChange.Connect(callback)
|
||||
}
|
||||
|
||||
// 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) handleKeyDown (key input.Key, numpad bool) bool {
|
||||
dot := this.Dot()
|
||||
modifiers := this.Window().Modifiers()
|
||||
word := modifiers.Control
|
||||
changed := false
|
||||
|
||||
defer func () {
|
||||
this.Select(dot)
|
||||
if changed {
|
||||
this.SetText(string(this.text))
|
||||
this.on.valueChange.Broadcast()
|
||||
}
|
||||
} ()
|
||||
|
||||
switch {
|
||||
case key == input.KeyEnter:
|
||||
this.on.confirm.Broadcast()
|
||||
return true
|
||||
case key == input.KeyBackspace:
|
||||
this.text, dot = text.Backspace(this.text, dot, word)
|
||||
changed = true
|
||||
return true
|
||||
case key == input.KeyDelete:
|
||||
this.text, dot = text.Delete(this.text, dot, word)
|
||||
changed = true
|
||||
return true
|
||||
case key == input.Key('a') && modifiers.Control:
|
||||
dot.Start = 0
|
||||
dot.End = len(this.text)
|
||||
return true
|
||||
case key.Printable():
|
||||
this.text, dot = text.Type(this.text, dot, rune(key))
|
||||
changed = true
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (this *TextInput) handleKeyUp (key input.Key, numpad bool) bool {
|
||||
modifiers := this.Window().Modifiers()
|
||||
switch {
|
||||
case key == input.KeyEnter:
|
||||
return true
|
||||
case key == input.KeyBackspace:
|
||||
return true
|
||||
case key == input.KeyDelete:
|
||||
return true
|
||||
case key == input.Key('a') && modifiers.Control:
|
||||
return true
|
||||
case key.Printable():
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (this *TextInput) handleScroll (x, y float64) bool {
|
||||
if x == 0 { return false }
|
||||
this.ScrollTo(this.ContentBounds().Min.Sub(image.Pt(int(x), int(y))))
|
||||
return true
|
||||
}
|
||||
12
textview.go
12
textview.go
@@ -2,7 +2,6 @@ package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/theme"
|
||||
|
||||
// TextView is an area for displaying a large amount of multi-line text.
|
||||
type TextView struct {
|
||||
@@ -12,16 +11,17 @@ type TextView struct {
|
||||
// NewTextView creates a new text view.
|
||||
func NewTextView (text string) *TextView {
|
||||
this := &TextView { TextBox: tomo.NewTextBox() }
|
||||
theme.Apply(this, theme.R("objects", "TextView", ""))
|
||||
this.SetRole(tomo.R("objects", "TextView"))
|
||||
this.SetFocusable(true)
|
||||
this.SetSelectable(true)
|
||||
this.SetText(text)
|
||||
this.SetOverflow(false, true)
|
||||
this.SetWrap(true)
|
||||
this.SetAttr(tomo.AOverflow(false, true))
|
||||
this.SetAttr(tomo.AWrap(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))))
|
||||
func (this *TextView) handleScroll (x, y float64) bool {
|
||||
this.ScrollTo(this.ContentBounds().Min.Sub(image.Pt(int(x), int(y))))
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user