43 Commits

Author SHA1 Message Date
6089dd3ff1 TextView no longer embeds tomo.TextBox 2024-08-25 01:40:40 -04:00
d4e8847908 Fix doc comments on Label, TextInput 2024-08-25 01:38:42 -04:00
82cf822602 Update NumberInput to use new TextInput methods 2024-08-25 01:32:44 -04:00
2b354979aa TextInput no longer embeds tomo.TextBox 2024-08-25 01:31:55 -04:00
1a2449d2b7 TabbedContainer no longer embeds tomo.ContainerBox 2024-08-24 22:26:00 -04:00
889c691c40 TabbedContainer no longer embeds tomo.ContainerBox 2024-08-24 22:15:21 -04:00
32eae0bcca Swatch no longer embeds tomo.CanvasBox 2024-08-24 22:12:43 -04:00
d9d758b5fc Slider no longer embeds tomo.ContainerBox 2024-08-24 22:09:31 -04:00
a11bab452b Ensure Separator fulfils tomo.Object 2024-08-24 22:03:35 -04:00
033e9debf6 Separator no longer embeds tomo.Box 2024-08-24 21:52:37 -04:00
8b79fec1bd ScrollContainer no longer embeds ContainerBox 2024-08-24 21:41:16 -04:00
bc175bb5ae Scrollbar no longer embeds tomo.ContainerBox 2024-08-24 21:35:43 -04:00
02fed8ce48 NumberInput no longer embeds tomo.ContainerBox 2024-08-24 20:19:01 -04:00
b784596b4d Update doc comment for Container 2024-08-24 20:13:38 -04:00
694f9127c0 MimeIcon no longer embeds tomo.Box 2024-08-24 20:10:49 -04:00
ae74c3dbf4 MenuItem no longer embeds tomo.ContainerBox 2024-08-24 20:10:37 -04:00
6f8d5cc426 LabelSwatch, LabelCheckbox changing their labels 2024-08-24 20:03:48 -04:00
f0c334c278 LabelSwatch no longer embeds tomo.ContainerBox 2024-08-24 20:02:14 -04:00
6ee2c5669e Ensure Label satisfies tomo.Object 2024-08-24 19:57:48 -04:00
3aa4b12ffe Update other objects to use new methods of Label 2024-08-24 19:56:58 -04:00
c7caa5bcb6 Label no longer embeds tomo.TextBox 2024-08-24 19:52:47 -04:00
0960fe013d LabelCheckbox no longer embeds tomo.ContainerBox 2024-08-24 19:50:29 -04:00
697229d183 Icon no longer embeds tomo.Box 2024-08-24 19:50:20 -04:00
c043f9bf8d Remove Heading.SetSelectable 2024-08-24 19:49:20 -04:00
14f6e175f0 Heading no longer embeds tomo.TextBox 2024-08-24 19:42:25 -04:00
df2e8f1b07 Dropdown no longer embeds tomo.ContainerBox 2024-08-24 19:33:16 -04:00
0c4e098680 HSVAColorPicker no longer embeds tomo.ContainerBox 2024-08-24 19:28:48 -04:00
fc51e7ab9f Checkbox no longer embeds tomo.ContainerBox 2024-08-24 15:41:47 -04:00
4e8823ef9f Calendar no longer embeds tomo.ContainerBox 2024-08-24 15:20:09 -04:00
8de08a9bdc Button no longer embeds tomo.ContainerBox 2024-08-24 15:11:57 -04:00
04f44cea86 Ensure Container satisfies tomo.ContentObject 2024-08-24 15:04:44 -04:00
c889838c9c Container no longer embeds tomo.ContainerBox
Progress on #7
2024-08-24 15:02:17 -04:00
7bcb4cf823 Add SetLayout to Container 2024-08-24 14:45:31 -04:00
02516bdcce Same as last commit but for TearLine 2024-08-24 14:42:08 -04:00
8432cc70da MenuItem focuses on hover
Styles should remove MenuItem[hover] styling
2024-08-24 14:37:44 -04:00
8469962c90 Use key/button functions for menu 2024-08-24 14:32:19 -04:00
0ccdb609ef Tear off menu windows now have an icon 2024-08-24 01:00:34 -04:00
d1f0786043 Dialog boxes have icons now 2024-08-23 21:39:39 -04:00
73731c6201 Scrollbar has Scrollbar role 2024-08-16 18:36:49 -04:00
7c42b7ad37 Scrollbar has ScrollbarHandle instead of SliderHandle 2024-08-16 18:36:20 -04:00
0fe4979483 Un-export SliderHandle
Closes #8
2024-08-16 18:35:19 -04:00
155752ba78 LabelSwatch uses the new button functions 2024-08-16 18:32:07 -04:00
f4a3cb3c00 LabelSwatch's label is not selectable, to match LabelCheckbox 2024-08-16 18:26:13 -04:00
24 changed files with 837 additions and 356 deletions

View File

@@ -5,13 +5,15 @@ import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(Button)
var buttonLayout = layouts.Row { true }
var iconButtonLayout = layouts.Row { true }
var bothButtonLayout = layouts.Row { false, true }
// Button is a clickable button.
type Button struct {
tomo.ContainerBox
box tomo.ContainerBox
label *Label
icon *Icon
@@ -24,33 +26,45 @@ type Button struct {
// NewButton creates a new button with the specified text.
func NewButton (text string) *Button {
box := &Button {
ContainerBox: tomo.NewContainerBox(),
label: NewLabel(text),
button := &Button {
box: tomo.NewContainerBox(),
label: NewLabel(text),
}
box.SetRole(tomo.R("objects", "Button"))
box.label.SetAttr(tomo.AAlign(tomo.AlignMiddle, tomo.AlignMiddle))
box.SetAttr(tomo.ALayout(buttonLayout))
box.SetText(text)
button.box.SetRole(tomo.R("objects", "Button"))
button.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
button.box.SetAttr(tomo.ALayout(buttonLayout))
button.SetText(text)
box.SetInputMask(true)
box.OnButtonDown(box.handleButtonDown)
box.OnButtonUp(box.handleButtonUp)
box.OnKeyDown(box.handleKeyDown)
box.OnKeyUp(box.handleKeyUp)
box.SetFocusable(true)
return box
button.box.SetInputMask(true)
button.box.OnButtonDown(button.handleButtonDown)
button.box.OnButtonUp(button.handleButtonUp)
button.box.OnKeyDown(button.handleKeyDown)
button.box.OnKeyUp(button.handleKeyUp)
button.box.SetFocusable(true)
return button
}
// GetBox returns the underlying box.
func (this *Button) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this button has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *Button) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// SetText sets the text of the button's label.
func (this *Button) SetText (text string) {
this.label.SetText(text)
if this.labelActive && text == "" {
this.Remove(this.label)
this.box.Remove(this.label)
this.labelActive = false
}
if !this.labelActive && text != "" {
this.Add(this.label)
this.box.Add(this.label)
this.labelActive = true
}
this.applyLayout()
@@ -59,7 +73,7 @@ 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 tomo.Icon) {
if this.icon != nil { this.Remove(this.icon) }
if this.icon != nil { this.box.Remove(this.icon) }
var icon *Icon; if id != tomo.IconUnknown {
icon = NewIcon(id, tomo.IconSizeSmall)
@@ -67,9 +81,9 @@ func (this *Button) SetIcon (id tomo.Icon) {
this.icon = icon
if this.icon != nil {
this.Insert(this.icon, this.label)
this.box.Insert(this.icon, this.label)
}
this.SetTag("icon", this.icon != nil)
this.box.SetTag("icon", this.icon != nil)
this.applyLayout()
}
@@ -80,11 +94,11 @@ func (this *Button) OnClick (callback func ()) event.Cookie {
func (this *Button) applyLayout () {
if this.labelActive && this.icon == nil {
this.SetAttr(tomo.ALayout(buttonLayout))
this.box.SetAttr(tomo.ALayout(buttonLayout))
} else if !this.labelActive && this.icon != nil {
this.SetAttr(tomo.ALayout(iconButtonLayout))
this.box.SetAttr(tomo.ALayout(iconButtonLayout))
} else {
this.SetAttr(tomo.ALayout(bothButtonLayout))
this.box.SetAttr(tomo.ALayout(bothButtonLayout))
}
}
@@ -106,7 +120,7 @@ func (this *Button) handleButtonDown (button input.Button) bool {
func (this *Button) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) {
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.on.click.Broadcast()
}
return true

View File

@@ -6,10 +6,12 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(Calendar)
// 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
box tomo.ContainerBox
grid tomo.ContainerBox
time time.Time
@@ -23,11 +25,11 @@ type Calendar struct {
// NewCalendar creates a new calendar with the specified date.
func NewCalendar (tm time.Time) *Calendar {
calendar := &Calendar {
ContainerBox: tomo.NewContainerBox(),
time: tm,
box: tomo.NewContainerBox(),
time: tm,
}
calendar.SetRole(tomo.R("objects", "Calendar"))
calendar.SetAttr(tomo.ALayout(layouts.ContractVertical))
calendar.box.SetRole(tomo.R("objects", "Calendar"))
calendar.box.SetAttr(tomo.ALayout(layouts.ContractVertical))
prevButton := NewButton("")
prevButton.SetIcon(tomo.IconGoPrevious)
@@ -42,23 +44,28 @@ func NewCalendar (tm time.Time) *Calendar {
calendar.on.valueChange.Broadcast()
})
calendar.monthLabel = NewLabel("")
calendar.monthLabel.SetAttr(tomo.AAlign(tomo.AlignMiddle, tomo.AlignMiddle))
calendar.monthLabel.SetAlign(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 (
calendar.box.Add(NewInnerContainer (
layouts.Row { false, true, false },
prevButton, calendar.monthLabel, nextButton))
calendar.Add(calendar.grid)
calendar.box.Add(calendar.grid)
calendar.OnScroll(calendar.handleScroll)
calendar.box.OnScroll(calendar.handleScroll)
calendar.refresh()
return calendar
}
// GetBox returns the underlying box.
func (this *Calendar) GetBox () tomo.Box {
return this.box
}
// Value returns the time this calendar is displaying.
func (this *Calendar) Value () time.Time {
return this.time

View File

@@ -4,9 +4,11 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(Checkbox)
// Checkbox is a control that can be toggled.
type Checkbox struct {
tomo.Box
box tomo.Box
value bool
on struct {
valueChange event.FuncBroadcaster
@@ -15,18 +17,30 @@ type Checkbox struct {
// NewCheckbox creates a new checkbox with the specified value.
func NewCheckbox (value bool) *Checkbox {
box := &Checkbox {
Box: tomo.NewBox(),
checkbox := &Checkbox {
box: tomo.NewBox(),
}
box.SetRole(tomo.R("objects", "Checkbox"))
box.SetValue(value)
checkbox.box.SetRole(tomo.R("objects", "Checkbox"))
checkbox.SetValue(value)
box.OnButtonDown(box.handleButtonDown)
box.OnButtonUp(box.handleButtonUp)
box.OnKeyDown(box.handleKeyDown)
box.OnKeyUp(box.handleKeyUp)
box.SetFocusable(true)
return box
checkbox.box.OnButtonDown(checkbox.handleButtonDown)
checkbox.box.OnButtonUp(checkbox.handleButtonUp)
checkbox.box.OnKeyDown(checkbox.handleKeyDown)
checkbox.box.OnKeyUp(checkbox.handleKeyUp)
checkbox.box.SetFocusable(true)
return checkbox
}
// GetBox returns the underlying box.
func (this *Checkbox) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this checkbox has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *Checkbox) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// Value returns the value of the checkbox.
@@ -38,8 +52,8 @@ func (this *Checkbox) Value () bool {
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)
this.box.SetTag("checked", value)
this.box.SetTag("unchecked", !value)
}
// Toggle toggles the value of the checkbox between true and false.
@@ -71,7 +85,7 @@ func (this *Checkbox) handleButtonDown (button input.Button) bool {
func (this *Checkbox) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) {
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.Toggle()
}
return true

View File

@@ -8,10 +8,12 @@ import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/objects/layouts"
import "git.tebibyte.media/tomo/objects/internal"
var _ tomo.Object = new(HSVAColorPicker)
// HSVAColorPicker allows the user to pick a color by controlling its HSVA
// parameters.
type HSVAColorPicker struct {
tomo.ContainerBox
box tomo.ContainerBox
value internal.HSVA
pickerMap *hsvaColorPickerMap
@@ -26,15 +28,15 @@ type HSVAColorPicker struct {
// NewHSVAColorPicker creates a new color picker with the specified color.
func NewHSVAColorPicker (value color.Color) *HSVAColorPicker {
picker := &HSVAColorPicker {
ContainerBox: tomo.NewContainerBox(),
box: tomo.NewContainerBox(),
}
picker.SetRole(tomo.R("objects", "ColorPicker"))
picker.SetAttr(tomo.ALayout(layouts.Row { true, false, false }))
picker.box.SetRole(tomo.R("objects", "ColorPicker"))
picker.box.SetAttr(tomo.ALayout(layouts.Row { true, false, false }))
picker.pickerMap = newHsvaColorPickerMap(picker)
picker.Add(picker.pickerMap)
picker.box.Add(picker.pickerMap)
picker.hueSlider = NewVerticalSlider(0.0)
picker.Add(picker.hueSlider)
picker.box.Add(picker.hueSlider)
picker.hueSlider.OnValueChange(func () {
picker.value.H = picker.hueSlider.Value()
picker.on.valueChange.Broadcast()
@@ -42,7 +44,7 @@ func NewHSVAColorPicker (value color.Color) *HSVAColorPicker {
})
picker.alphaSlider = NewVerticalSlider(0.0)
picker.Add(picker.alphaSlider)
picker.box.Add(picker.alphaSlider)
picker.alphaSlider.OnValueChange(func () {
picker.value.A = uint16(picker.alphaSlider.Value() * 0xFFFF)
picker.on.valueChange.Broadcast()
@@ -54,6 +56,18 @@ func NewHSVAColorPicker (value color.Color) *HSVAColorPicker {
return picker
}
// GetBox returns the underlying box.
func (this *HSVAColorPicker) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this color picker has keyboard focus. If set
// to true, this method will steal focus away from whichever object currently
// has focus.
func (this *HSVAColorPicker) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// Value returns the color of the picker.
func (this *HSVAColorPicker) Value () color.Color {
return this.value

View File

@@ -1,21 +1,27 @@
package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.ContentObject = new(Container)
// Container is an object that can contain other objects. It can be used as a
// primitive for building more complex layouts. It has two variants: an outer
// container, and an inner container. The outer container has padding around
// its edges, whereas the inner container does not. The container will have a
// corresponding object role variation of either "outer" or "inner".
// primitive for building more complex layouts. It has two main variants: an
// outer container, and an inner container. The outer container has padding
// around its edges, whereas the inner container does not. It also has a
// "sunken" variation designed to hold a scrolled list of items. The container
// will have a corresponding object role variation of either "outer", "inner",
// or "sunken".
type Container struct {
tomo.ContainerBox
box tomo.ContainerBox
}
func newContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := &Container {
ContainerBox: tomo.NewContainerBox(),
box: tomo.NewContainerBox(),
}
this.SetAttr(tomo.ALayout(layout))
this.box.SetAttr(tomo.ALayout(layout))
for _, child := range children {
this.Add(child)
}
@@ -27,8 +33,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...)
this.SetRole(tomo.R("objects", "Container"))
this.SetTag("outer", true)
this.box.SetRole(tomo.R("objects", "Container"))
this.box.SetTag("outer", true)
return this
}
@@ -36,16 +42,87 @@ 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...)
this.SetRole(tomo.R("objects", "Container"))
this.SetTag("sunken", true)
this.box.SetRole(tomo.R("objects", "Container"))
this.box.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...)
this.SetRole(tomo.R("objects", "Container"))
this.SetTag("inner", true)
this.box.SetRole(tomo.R("objects", "Container"))
this.box.SetTag("inner", true)
return this
}
// GetBox returns the underlying box.
func (this *Container) GetBox () tomo.Box {
return this.box
}
// ContentBounds returns the bounds of the inner content of the container
// relative to the container's InnerBounds.
func (this *Container) ContentBounds () image.Rectangle {
return this.box.ContentBounds()
}
// ScrollTo shifts the origin of the container's content to the origin of the
// container's InnerBounds, offset by the given point.
func (this *Container) ScrollTo (position image.Point) {
this.box.ScrollTo(position)
}
// OnContentBoundsChange specifies a function to be called when the container's
// ContentBounds or InnerBounds changes.
func (this *Container) OnContentBoundsChange (callback func ()) event.Cookie {
return this.box.OnContentBoundsChange(callback)
}
// SetLayout sets the layout of the container.
func (this *Container) SetLayout (layout tomo.Layout) {
this.box.SetAttr(tomo.ALayout(layout))
}
// SetAlign sets the X and Y alignment of the container.
func (this *Container) SetAlign (x, y tomo.Align) {
this.box.SetAttr(tomo.AAlign(x, y))
}
// SetOverflow sets the X and Y overflow of the container.
func (this *Container) SetOverflow (x, y bool) {
this.box.SetAttr(tomo.AOverflow(x, y))
}
// Add appends a child object. If the object is already a child of another
// object, it will be removed from that object first.
func (this *Container) Add (object tomo.Object) {
this.box.Add(object)
}
// Remove removes a child object, if it is a child of this container.
func (this *Container) Remove (object tomo.Object) {
this.box.Remove(object)
}
// Insert inserts a child object before a specified object. If the before object
// is nil or is not contained within this container, the inserted object is
// appended. If the inserted object is already a child of another object, it
// will be removed from that object first.
func (this *Container) Insert (child tomo.Object, before tomo.Object) {
this.box.Insert(child, before)
}
// Clear removes all child objects.
func (this *Container) Clear () {
this.box.Clear()
}
// Len returns hte amount of child objects.
func (this *Container) Len () int {
return this.box.Len()
}
// At returns the child object at the specified index.
func (this *Container) At (index int) tomo.Object {
return this.box.At(index)
}

View File

@@ -16,7 +16,7 @@ type DialogKind int; const (
// Dialog is a modal dialog window.
type Dialog struct {
tomo.Window
controlRow tomo.ContainerBox
controlRow *Container
}
type clickable interface {
@@ -53,9 +53,10 @@ func NewDialog (kind DialogKind, parent tomo.Window, title, message string, opti
case DialogError: iconId = tomo.IconDialogError
}
dialog.SetTitle(title)
dialog.SetIcon(iconId)
icon := NewIcon(iconId, tomo.IconSizeLarge)
messageText := NewLabel(message)
messageText.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
messageText.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
for _, option := range options {
if option, ok := option.(clickable); ok {
@@ -63,7 +64,7 @@ func NewDialog (kind DialogKind, parent tomo.Window, title, message string, opti
}
}
dialog.controlRow = NewInnerContainer(layouts.ContractHorizontal, options...)
dialog.controlRow.SetAttr(tomo.AAlign(tomo.AlignEnd, tomo.AlignEnd))
dialog.controlRow.SetAlign(tomo.AlignEnd, tomo.AlignEnd)
dialog.SetRoot(NewOuterContainer (
layouts.Column { true, false },

View File

@@ -6,10 +6,12 @@ import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(Dropdown)
// Dropdown is a non-editable text input that allows the user to pick a value
// from a list.
type Dropdown struct {
tomo.ContainerBox
box tomo.ContainerBox
label *Label
value string
@@ -24,28 +26,40 @@ type Dropdown struct {
// NewDropdown creates a new dropdown input with the specified items
func NewDropdown (items ...string) *Dropdown {
dropdown := &Dropdown {
ContainerBox: tomo.NewContainerBox(),
box: tomo.NewContainerBox(),
label: NewLabel(""),
}
dropdown.SetRole(tomo.R("objects", "Dropdown"))
dropdown.SetAttr(tomo.ALayout(layouts.Row { true, false }))
dropdown.Add(dropdown.label)
dropdown.Add(NewIcon(tomo.IconListChoose, tomo.IconSizeSmall))
dropdown.box.SetRole(tomo.R("objects", "Dropdown"))
dropdown.box.SetAttr(tomo.ALayout(layouts.Row { true, false }))
dropdown.box.Add(dropdown.label)
dropdown.box.Add(NewIcon(tomo.IconListChoose, tomo.IconSizeSmall))
dropdown.SetItems(items...)
if len(items) > 0 {
dropdown.SetValue(items[0])
}
dropdown.SetInputMask(true)
dropdown.OnButtonDown(dropdown.handleButtonDown)
dropdown.OnButtonUp(dropdown.handleButtonUp)
dropdown.OnKeyDown(dropdown.handleKeyDown)
dropdown.OnKeyUp(dropdown.handleKeyUp)
dropdown.SetFocusable(true)
dropdown.box.SetInputMask(true)
dropdown.box.OnButtonDown(dropdown.handleButtonDown)
dropdown.box.OnButtonUp(dropdown.handleButtonUp)
dropdown.box.OnKeyDown(dropdown.handleKeyDown)
dropdown.box.OnKeyUp(dropdown.handleKeyUp)
dropdown.box.SetFocusable(true)
return dropdown
}
// GetBox returns the underlying box.
func (this *Dropdown) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this dropdown has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *Dropdown) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// Value returns the value of the dropdown. This does not necissarily have to be
// in the list of items.
func (this *Dropdown) Value () string {
@@ -107,7 +121,7 @@ func (this *Dropdown) handleButtonDown (button input.Button) bool {
func (this *Dropdown) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) {
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.Choose()
}
return true

View File

@@ -2,30 +2,67 @@ package objects
import "fmt"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/text"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(Heading)
// Heading is a label that denotes the start of some section of content. It can
// have a level from 0 to 2, with 0 being the most prominent and 2 being the
// most subtle. The level is described in the role variation.
type Heading struct {
tomo.TextBox
box tomo.TextBox
}
// NewHeading creates a new section heading. The level can be from 0 to 2.
func NewHeading (level int, text string) *Heading {
if level < 0 { level = 0 }
if level > 2 { level = 2 }
this := &Heading { TextBox: tomo.NewTextBox() }
this.SetRole(tomo.R("objects", "Heading"))
this.SetTag(fmt.Sprint(level), true)
this := &Heading { box: tomo.NewTextBox() }
this.box.SetRole(tomo.R("objects", "Heading"))
this.box.SetTag(fmt.Sprint(level), true)
this.SetText(text)
this.SetSelectable(true)
this.SetFocusable(true)
this.box.SetSelectable(true)
this.box.SetFocusable(true)
return this
}
// GetBox returns the underlying box.
func (this *Heading) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this heading has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *Heading) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// SetText sets the text content of the heading.
func (this *Heading) SetText (text string) {
this.box.SetText(text)
}
// Select sets the text cursor or selection.
func (this *Heading) Select (dot text.Dot) {
this.box.Select(dot)
}
// Dot returns the text cursor or selection.
func (this *Heading) Dot () text.Dot {
return this.box.Dot()
}
// OnDotChange specifies a function to be called when the text cursor or
// selection changes.
func (this *Heading) OnDotChange (callback func ()) event.Cookie {
return this.box.OnDotChange(callback)
}
// NewMenuHeading creatss a new heading for use in menus.
func NewMenuHeading (text string) *Heading {
heading := NewHeading(0, text)
heading.SetTag("menu", true)
heading.box.SetTag("menu", true)
return heading
}

23
icon.go
View File

@@ -3,9 +3,11 @@ package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/canvas"
var _ tomo.Object = new(Icon)
// Icon displays a single icon.
type Icon struct {
tomo.Box
box tomo.Box
icon tomo.Icon
size tomo.IconSize
}
@@ -21,14 +23,19 @@ func iconSizeString (size tomo.IconSize) string {
// NewIcon creates a new icon from an icon ID.
func NewIcon (icon tomo.Icon, size tomo.IconSize) *Icon {
this := &Icon {
Box: tomo.NewBox(),
box: tomo.NewBox(),
}
this.SetRole(tomo.R("objects", "Icon"))
this.box.SetRole(tomo.R("objects", "Icon"))
this.SetIcon(icon, size)
this.OnIconSetChange(this.handleIconSetChange)
this.box.OnIconSetChange(this.handleIconSetChange)
return this
}
// GetBox returns the underlying box.
func (this *Icon) GetBox () tomo.Box {
return this.box
}
// SetIcon sets the icon.
func (this *Icon) SetIcon (icon tomo.Icon, size tomo.IconSize) {
if this.icon == icon { return }
@@ -42,12 +49,12 @@ func (this *Icon) handleIconSetChange () {
}
func (this *Icon) setTexture (texture canvas.Texture) {
this.SetAttr(tomo.ATexture(texture))
this.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
this.box.SetAttr(tomo.ATexture(texture))
this.box.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
if texture == nil {
this.SetAttr(tomo.AMinimumSize(0, 0))
this.box.SetAttr(tomo.AMinimumSize(0, 0))
} else {
bounds := texture.Bounds()
this.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
this.box.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
}
}

View File

@@ -1,19 +1,60 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/text"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(Label)
// Label is a simple text label.
type Label struct {
tomo.TextBox
box tomo.TextBox
}
// NewLabel creates a new text label.
func NewLabel (text string) *Label {
this := &Label { TextBox: tomo.NewTextBox() }
this.SetRole(tomo.R("objects", "Label"))
this := &Label { box: tomo.NewTextBox() }
this.box.SetRole(tomo.R("objects", "Label"))
this.SetText(text)
this.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
this.SetSelectable(true)
this.SetFocusable(true)
this.box.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
this.box.SetSelectable(true)
this.box.SetFocusable(true)
return this
}
// GetBox returns the underlying box.
func (this *Label) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this label has keyboard focus. If set to true,
// this method will steal focus away from whichever object currently has focus.
func (this *Label) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// SetText sets the text content of the label.
func (this *Label) SetText (text string) {
this.box.SetText(text)
}
// Select sets the text cursor or selection.
func (this *Label) Select (dot text.Dot) {
this.box.Select(dot)
}
// Dot returns the text cursor or selection.
func (this *Label) Dot () text.Dot {
return this.box.Dot()
}
// SetAlign sets the X and Y alignment of the label.
func (this *Label) SetAlign (x, y tomo.Align) {
this.box.SetAttr(tomo.AAlign(x, y))
}
// OnDotChange specifies a function to be called when the text cursor or
// selection changes.
func (this *Label) OnDotChange (callback func ()) event.Cookie {
return this.box.OnDotChange(callback)
}

View File

@@ -5,9 +5,11 @@ import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(LabelCheckbox)
// LabelCheckbox is a checkbox with a label.
type LabelCheckbox struct {
tomo.ContainerBox
box tomo.ContainerBox
checkbox *Checkbox
label *Label
}
@@ -15,22 +17,39 @@ type LabelCheckbox struct {
// NewLabelCheckbox creates a new labeled checkbox with the specified value and
// label text.
func NewLabelCheckbox (value bool, text string) *LabelCheckbox {
box := &LabelCheckbox {
ContainerBox: tomo.NewContainerBox(),
checkbox: NewCheckbox(value),
label: NewLabel(text),
labelCheckbox := &LabelCheckbox {
box: tomo.NewContainerBox(),
checkbox: NewCheckbox(value),
label: NewLabel(text),
}
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.SetAttr(tomo.ALayout(layouts.Row { false, true }))
labelCheckbox.box.SetRole(tomo.R("objects", "LabelCheckbox"))
labelCheckbox.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
labelCheckbox.label.GetBox().(tomo.TextBox).SetSelectable(false)
labelCheckbox.label.GetBox().(tomo.TextBox).SetFocusable(false)
labelCheckbox.box.Add(labelCheckbox.checkbox)
labelCheckbox.box.Add(labelCheckbox.label)
labelCheckbox.box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
box.OnButtonDown(box.handleButtonDown)
box.OnButtonUp(box.handleButtonUp)
return box
labelCheckbox.box.OnButtonDown(labelCheckbox.handleButtonDown)
labelCheckbox.box.OnButtonUp(labelCheckbox.handleButtonUp)
return labelCheckbox
}
// GetBox returns the underlying box.
func (this *LabelCheckbox) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this checkbox has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *LabelCheckbox) SetFocused (focused bool) {
this.checkbox.SetFocused(focused)
}
// SetText sets the text label of the checkbox.
func (this *LabelCheckbox) SetText (text string) {
this.label.SetText(text)
}
// Value returns the value of the checkbox.
@@ -61,7 +80,7 @@ func (this *LabelCheckbox) handleButtonDown (button input.Button) bool {
func (this *LabelCheckbox) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) {
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.checkbox.SetFocused(true)
this.checkbox.Toggle()
}

View File

@@ -6,31 +6,52 @@ import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(LabelSwatch)
// LabelSwatch is a swatch with a label.
type LabelSwatch struct {
tomo.ContainerBox
swatch *Swatch
label *Label
box 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),
labelSwatch := &LabelSwatch {
box: 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 }))
labelSwatch.box.SetRole(tomo.R("objects", "LabelSwatch"))
labelSwatch.swatch.label = text
labelSwatch.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
labelSwatch.label.GetBox().(tomo.TextBox).SetSelectable(false)
labelSwatch.label.GetBox().(tomo.TextBox).SetFocusable(false)
labelSwatch.box.Add(labelSwatch.swatch)
labelSwatch.box.Add(labelSwatch.label)
labelSwatch.box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
box.OnButtonDown(box.handleButtonDown)
box.OnButtonUp(box.handleButtonUp)
return box
labelSwatch.box.OnButtonDown(labelSwatch.handleButtonDown)
labelSwatch.box.OnButtonUp(labelSwatch.handleButtonUp)
return labelSwatch
}
// GetBox returns the underlying box.
func (this *LabelSwatch) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this swatch has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *LabelSwatch) SetFocused (focused bool) {
this.swatch.SetFocused(focused)
}
// SetText sets the text label of the swatch.
func (this *LabelSwatch) SetText (text string) {
this.label.SetText(text)
}
// Value returns the color of the swatch.
@@ -61,13 +82,13 @@ func (this *LabelSwatch) OnConfirm (callback func ()) event.Cookie {
}
func (this *LabelSwatch) handleButtonDown (button input.Button) bool {
if button != input.ButtonLeft { return true }
if !isClickingButton(button) { 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()) {
if !isClickingButton(button) { return true }
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.swatch.SetFocused(true)
this.swatch.Choose()
}

15
menu.go
View File

@@ -72,6 +72,7 @@ func (this *Menu) TearOff () {
this.torn = true
window, err := this.parent.NewChild(this.bounds)
window.SetIcon(tomo.IconListChoose)
if err != nil { return }
visible := this.Window.Visible()
@@ -90,21 +91,27 @@ func (this *Menu) newTearLine () tomo.Object {
tearLine := tomo.NewBox()
tearLine.SetRole(tomo.R("objects", "TearLine"))
tearLine.SetFocusable(true)
tearLine.OnMouseEnter(func () {
tearLine.SetFocused(true)
})
tearLine.OnMouseLeave(func () {
tearLine.SetFocused(false)
})
tearLine.OnKeyDown(func (key input.Key, numberPad bool) bool {
if key != input.KeyEnter && key != input.Key(' ') { return false }
if !isClickingKey(key) { return false }
return true
})
tearLine.OnKeyUp(func (key input.Key, numberPad bool) bool {
if key != input.KeyEnter && key != input.Key(' ') { return false }
if !isClickingKey(key) { return false }
this.TearOff()
return true
})
tearLine.OnButtonDown(func (button input.Button) bool {
if button != input.ButtonLeft { return false }
if !isClickingButton(button) { return false }
return true
})
tearLine.OnButtonUp(func (button input.Button) bool {
if button != input.ButtonLeft { return false }
if !isClickingButton(button) { return false }
if tearLine.Window().MousePosition().In(tearLine.Bounds()) {
this.TearOff()
}

View File

@@ -5,12 +5,14 @@ import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(MenuItem)
// MenuItem is a selectable memu item.
type MenuItem struct {
tomo.ContainerBox
box tomo.ContainerBox
label *Label
icon *Icon
label *Label
icon *Icon
labelActive bool
on struct {
@@ -25,25 +27,39 @@ func NewMenuItem (text string) *MenuItem {
// NewIconMenuItem creates a new menu item with the specified icon and text.
func NewIconMenuItem (icon tomo.Icon, text string) *MenuItem {
box := &MenuItem {
ContainerBox: tomo.NewContainerBox(),
label: NewLabel(text),
icon: NewIcon(icon, tomo.IconSizeSmall),
menuItem := &MenuItem {
box: tomo.NewContainerBox(),
label: NewLabel(text),
icon: NewIcon(icon, 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 }))
menuItem.box.SetRole(tomo.R("objects", "MenuItem"))
menuItem.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
menuItem.box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
box.Add(box.icon)
box.Add(box.label)
menuItem.box.Add(menuItem.icon)
menuItem.box.Add(menuItem.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
menuItem.box.SetInputMask(true)
menuItem.box.OnMouseEnter(menuItem.handleMouseEnter)
menuItem.box.OnMouseLeave(menuItem.handleMouseLeave)
menuItem.box.OnButtonDown(menuItem.handleButtonDown)
menuItem.box.OnButtonUp(menuItem.handleButtonUp)
menuItem.box.OnKeyDown(menuItem.handleKeyDown)
menuItem.box.OnKeyUp(menuItem.handleKeyUp)
menuItem.box.SetFocusable(true)
return menuItem
}
// GetBox returns the underlying box.
func (this *MenuItem) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this menu item has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *MenuItem) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// SetText sets the text of the items's label.
@@ -62,6 +78,14 @@ func (this *MenuItem) OnClick (callback func ()) event.Cookie {
return this.on.click.Connect(callback)
}
func (this *MenuItem) handleMouseEnter () {
this.SetFocused(true)
}
func (this *MenuItem) handleMouseLeave () {
this.SetFocused(false)
}
func (this *MenuItem) handleKeyDown (key input.Key, numberPad bool) bool {
if key != input.KeyEnter && key != input.Key(' ') { return false }
return true
@@ -80,7 +104,7 @@ func (this *MenuItem) handleButtonDown (button input.Button) bool {
func (this *MenuItem) handleButtonUp (button input.Button) bool {
if button != input.ButtonLeft { return false }
if this.Window().MousePosition().In(this.Bounds()) {
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.on.click.Broadcast()
}
return true

View File

@@ -4,22 +4,29 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/data"
import "git.tebibyte.media/tomo/tomo/canvas"
var _ tomo.Object = new(MimeIcon)
// MimeIcon displays an icon of a MIME type.
type MimeIcon struct {
tomo.Box
box 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(),
mimeIcon := &MimeIcon {
box: tomo.NewBox(),
}
this.SetRole(tomo.R("objects", "MimeIcon"))
this.SetIcon(mime, size)
this.OnIconSetChange(this.handleIconSetChange)
return this
mimeIcon.box.SetRole(tomo.R("objects", "MimeIcon"))
mimeIcon.SetIcon(mime, size)
mimeIcon.box.OnIconSetChange(mimeIcon.handleIconSetChange)
return mimeIcon
}
// GetBox returns the underlying box.
func (this *MimeIcon) GetBox () tomo.Box {
return this.box
}
// SetIcon sets the MIME type and size of the icon.
@@ -35,12 +42,12 @@ func (this *MimeIcon) handleIconSetChange () {
}
func (this *MimeIcon) setTexture (texture canvas.Texture) {
this.SetAttr(tomo.ATexture(texture))
this.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
this.box.SetAttr(tomo.ATexture(texture))
this.box.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
if texture == nil {
this.SetAttr(tomo.AMinimumSize(0, 0))
this.box.SetAttr(tomo.AMinimumSize(0, 0))
} else {
bounds := texture.Bounds()
this.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
this.box.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
}
}

View File

@@ -3,14 +3,17 @@ package objects
import "math"
import "strconv"
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"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(NumberInput)
// NumberInput is an editable text box which accepts only numbers, and has
// controls to increment and decrement its value.
type NumberInput struct {
tomo.ContainerBox
box tomo.ContainerBox
input *TextInput
increment *Button
decrement *Button
@@ -22,41 +25,63 @@ type NumberInput struct {
// NewNumberInput creates a new number input with the specified value.
func NewNumberInput (value float64) *NumberInput {
box := &NumberInput {
ContainerBox: tomo.NewContainerBox(),
input: NewTextInput(""),
increment: NewButton(""),
decrement: NewButton(""),
numberInput := &NumberInput {
box: tomo.NewContainerBox(),
input: NewTextInput(""),
increment: NewButton(""),
decrement: NewButton(""),
}
box.SetRole(tomo.R("objects", "NumberInput"))
box.Add(box.input)
box.Add(box.decrement)
box.Add(box.increment)
box.SetAttr(tomo.ALayout(layouts.Row { true, false, false }))
box.increment.SetIcon(tomo.IconValueIncrement)
box.decrement.SetIcon(tomo.IconValueDecrement)
numberInput.box.SetRole(tomo.R("objects", "NumberInput"))
numberInput.box.Add(numberInput.input)
numberInput.box.Add(numberInput.decrement)
numberInput.box.Add(numberInput.increment)
numberInput.box.SetAttr(tomo.ALayout(layouts.Row { true, false, false }))
numberInput.increment.SetIcon(tomo.IconValueIncrement)
numberInput.decrement.SetIcon(tomo.IconValueDecrement)
box.SetValue(value)
numberInput.SetValue(value)
box.OnScroll(box.handleScroll)
box.OnKeyDown(box.handleKeyDown)
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
numberInput.box.OnScroll(numberInput.handleScroll)
numberInput.box.OnKeyDown(numberInput.handleKeyDown)
numberInput.box.OnKeyUp(numberInput.handleKeyUp)
numberInput.input.OnConfirm(numberInput.handleConfirm)
numberInput.input.OnValueChange(numberInput.on.valueChange.Broadcast)
numberInput.increment.OnClick(func () { numberInput.shift( 1) })
numberInput.decrement.OnClick(func () { numberInput.shift(-1) })
return numberInput
}
// GetBox returns the underlying box.
func (this *NumberInput) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this number input has keyboard focus. If set
// to true, this method will steal focus away from whichever object currently
// has focus.
func (this *NumberInput) SetFocused (focused bool) {
this.input.SetFocused(focused)
}
// Select sets the text cursor or selection.
func (this *NumberInput) Select (dot text.Dot) {
this.input.Select(dot)
}
// Dot returns the text cursor or selection.
func (this *NumberInput) Dot () text.Dot {
return this.input.Dot()
}
// Value returns the value of the input.
func (this *NumberInput) Value () float64 {
value, _ := strconv.ParseFloat(this.input.Text(), 64)
value, _ := strconv.ParseFloat(this.input.Value(), 64)
return value
}
// SetValue sets the value of the input.
func (this *NumberInput) SetValue (value float64) {
this.input.SetText(strconv.FormatFloat(value, 'g', -1, 64))
this.input.SetValue(strconv.FormatFloat(value, 'g', -1, 64))
}
// OnValueChange specifies a function to be called when the user edits the input

View File

@@ -5,11 +5,13 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(Scrollbar)
// Scrollbar is a special type of slider that can control the scroll of any
// overflowing ContainerBox.
// overflowing ContainerObject.
type Scrollbar struct {
tomo.ContainerBox
handle *SliderHandle
box tomo.ContainerBox
handle *sliderHandle
layout scrollbarLayout
dragging bool
dragOffset image.Point
@@ -23,8 +25,8 @@ type Scrollbar struct {
func newScrollbar (orient string) *Scrollbar {
this := &Scrollbar {
ContainerBox: tomo.NewContainerBox(),
handle: &SliderHandle {
box: tomo.NewContainerBox(),
handle: &sliderHandle {
Box: tomo.NewBox(),
},
layout: scrollbarLayout {
@@ -32,21 +34,21 @@ func newScrollbar (orient string) *Scrollbar {
},
}
this.Add(this.handle)
this.box.Add(this.handle)
this.SetFocusable(true)
this.SetInputMask(true)
this.OnKeyUp(this.handleKeyUp)
this.OnKeyDown(this.handleKeyDown)
this.OnButtonDown(this.handleButtonDown)
this.OnButtonUp(this.handleButtonUp)
this.OnMouseMove(this.handleMouseMove)
this.OnScroll(this.handleScroll)
this.box.SetFocusable(true)
this.box.SetInputMask(true)
this.box.OnKeyUp(this.handleKeyUp)
this.box.OnKeyDown(this.handleKeyDown)
this.box.OnButtonDown(this.handleButtonDown)
this.box.OnButtonUp(this.handleButtonUp)
this.box.OnMouseMove(this.handleMouseMove)
this.box.OnScroll(this.handleScroll)
this.handle.SetRole(tomo.R("objects", "SliderHandle"))
this.handle.SetRole(tomo.R("objects", "ScrollbarHandle"))
this.handle.SetTag(orient, true)
this.SetRole(tomo.R("objects", "Slider"))
this.SetTag(orient, true)
this.box.SetRole(tomo.R("objects", "Scrollbar"))
this.box.SetTag(orient, true)
return this
}
@@ -60,13 +62,18 @@ func NewHorizontalScrollbar () *Scrollbar {
return newScrollbar("horizontal")
}
// GetBox returns the underlying box.
func (this *Scrollbar) GetBox () tomo.Box {
return this.box
}
// 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.SetAttr(tomo.ALayout(this.layout))
this.box.SetAttr(tomo.ALayout(this.layout))
return this.linkCookie
}
@@ -79,7 +86,7 @@ func (this *Scrollbar) handleLinkedContentBoundsChange () {
} else {
this.layout.value = this.layout.contentPos() / trackLength
}
this.SetAttr(tomo.ALayout(this.layout))
this.box.SetAttr(tomo.ALayout(this.layout))
if this.layout.value != previousValue {
this.on.valueChange.Broadcast()
}
@@ -150,7 +157,7 @@ func (this *Scrollbar) handleKeyUp (key input.Key, numpad bool) bool {
}
func (this *Scrollbar) handleKeyDown (key input.Key, numpad bool) bool {
modifiers := this.Window().Modifiers()
modifiers := this.box.Window().Modifiers()
switch key {
case input.KeyUp, input.KeyLeft:
@@ -183,7 +190,7 @@ func (this *Scrollbar) handleKeyDown (key input.Key, numpad bool) bool {
}
func (this *Scrollbar) handleButtonDown (button input.Button) bool {
pointer := this.Window().MousePosition()
pointer := this.box.Window().MousePosition()
handle := this.handle.Bounds()
within := pointer.In(handle)
@@ -199,7 +206,7 @@ func (this *Scrollbar) handleButtonDown (button input.Button) bool {
this.dragging = true
this.dragOffset =
pointer.Sub(this.handle.Bounds().Min).
Add(this.InnerBounds().Min)
Add(this.box.InnerBounds().Min)
this.drag()
} else {
this.dragOffset = this.fallbackDragOffset()
@@ -253,8 +260,8 @@ func (this *Scrollbar) handleScroll (x, y float64) bool {
}
func (this *Scrollbar) drag () {
pointer := this.Window().MousePosition().Sub(this.dragOffset)
gutter := this.InnerBounds()
pointer := this.box.Window().MousePosition().Sub(this.dragOffset)
gutter := this.box.InnerBounds()
handle := this.handle.Bounds()
if this.layout.vertical {
@@ -270,10 +277,10 @@ func (this *Scrollbar) drag () {
func (this *Scrollbar) fallbackDragOffset () image.Point {
if this.layout.vertical {
return this.InnerBounds().Min.
return this.box.InnerBounds().Min.
Add(image.Pt(0, this.handle.Bounds().Dy() / 2))
} else {
return this.InnerBounds().Min.
return this.box.InnerBounds().Min.
Add(image.Pt(this.handle.Bounds().Dx() / 2, 0))
}
}
@@ -307,7 +314,7 @@ func (this *scrollbarCookie) Close () {
cookie.Close()
}
this.owner.layout.linked = nil
this.owner.SetAttr(tomo.ALayout(this.owner.layout))
this.owner.box.SetAttr(tomo.ALayout(this.owner.layout))
}
type scrollbarLayout struct {

View File

@@ -37,9 +37,11 @@ func (sides ScrollSide) String () string {
}
}
var _ tomo.Object = new(ScrollContainer)
// ScrollContainer couples a ContentBox with one or two Scrollbars.
type ScrollContainer struct {
tomo.ContainerBox
box tomo.ContainerBox
root tomo.ContentObject
horizontal *Scrollbar
@@ -55,30 +57,35 @@ type ScrollContainer struct {
// NewScrollContainer creates a new scroll container.
func NewScrollContainer (sides ScrollSide) *ScrollContainer {
this := &ScrollContainer {
ContainerBox: tomo.NewContainerBox(),
scrollContainer := &ScrollContainer {
box: tomo.NewContainerBox(),
}
if sides.Vertical() {
this.vertical = NewVerticalScrollbar()
this.vertical.OnValueChange(this.handleValueChange)
this.Add(this.vertical)
scrollContainer.vertical = NewVerticalScrollbar()
scrollContainer.vertical.OnValueChange(scrollContainer.handleValueChange)
scrollContainer.box.Add(scrollContainer.vertical)
}
if sides.Horizontal() {
this.horizontal = NewHorizontalScrollbar()
this.horizontal.OnValueChange(this.handleValueChange)
this.Add(this.horizontal)
scrollContainer.horizontal = NewHorizontalScrollbar()
scrollContainer.horizontal.OnValueChange(scrollContainer.handleValueChange)
scrollContainer.box.Add(scrollContainer.horizontal)
}
this.OnScroll(this.handleScroll)
this.OnKeyDown(this.handleKeyDown)
this.OnKeyUp(this.handleKeyUp)
this.SetRole(tomo.R("objects", "ScrollContainer"))
this.SetTag(sides.String(), true)
scrollContainer.box.OnScroll(scrollContainer.handleScroll)
scrollContainer.box.OnKeyDown(scrollContainer.handleKeyDown)
scrollContainer.box.OnKeyUp(scrollContainer.handleKeyUp)
scrollContainer.box.SetRole(tomo.R("objects", "ScrollContainer"))
scrollContainer.box.SetTag(sides.String(), true)
if sides == ScrollHorizontal {
this.SetAttr(tomo.ALayout(layouts.NewGrid(true)(true, false)))
scrollContainer.box.SetAttr(tomo.ALayout(layouts.NewGrid(true)(true, false)))
} else {
this.SetAttr(tomo.ALayout(layouts.NewGrid(true, false)(true, false)))
scrollContainer.box.SetAttr(tomo.ALayout(layouts.NewGrid(true, false)(true, false)))
}
return this
return scrollContainer
}
// GetBox returns the underlying box.
func (this *ScrollContainer) GetBox () tomo.Box {
return this.box
}
// SetRoot sets the root child of the ScrollContainer. There can only be one at
@@ -87,7 +94,7 @@ func NewScrollContainer (sides ScrollSide) *ScrollContainer {
func (this *ScrollContainer) SetRoot (root tomo.ContentObject) {
if this.root != nil {
// remove root and close cookies
this.Remove(this.root)
this.box.Remove(this.root)
if this.horizontalCookie != nil {
this.horizontalCookie.Close()
this.horizontalCookie = nil
@@ -102,11 +109,11 @@ func (this *ScrollContainer) SetRoot (root tomo.ContentObject) {
// insert root at the beginning (for keynav)
switch {
case this.vertical != nil:
this.Insert(root, this.vertical)
this.box.Insert(root, this.vertical)
case this.horizontal != nil:
this.Insert(root, this.horizontal)
this.box.Insert(root, this.horizontal)
default:
this.Add(root)
this.box.Add(root)
}
// link root and remember cookies
@@ -191,7 +198,7 @@ func (this *ScrollContainer) handleScroll (x, y float64) bool {
}
func (this *ScrollContainer) handleKeyDown (key input.Key, numpad bool) bool {
modifiers := this.Window().Modifiers()
modifiers := this.box.Window().Modifiers()
vector := image.Point { }
switch key {
case input.KeyPageUp:

View File

@@ -2,16 +2,23 @@ package objects
import "git.tebibyte.media/tomo/tomo"
var _ tomo.Object = new(Separator)
// Separator is a line for visually separating elements.
type Separator struct {
tomo.Box
box tomo.Box
}
// NewSeparator creates a new separator line.
func NewSeparator () *Separator {
this := &Separator {
Box: tomo.NewBox(),
box: tomo.NewBox(),
}
this.SetRole(tomo.R("objects", "Separator"))
this.box.SetRole(tomo.R("objects", "Separator"))
return this
}
// GetBox returns the underlying box.
func (this *Separator) GetBox () tomo.Box {
return this.box
}

View File

@@ -6,10 +6,12 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(Slider)
// Slider is a control that selects a numeric value between 0 and 1.
type Slider struct {
tomo.ContainerBox
handle *SliderHandle
box tomo.ContainerBox
handle *sliderHandle
layout sliderLayout
dragging bool
dragOffset image.Point
@@ -21,16 +23,14 @@ type Slider struct {
}
}
// SliderHandle is a simple object that serves as a handle for sliders and
// scrollbars. It is completely inert.
type SliderHandle struct {
type sliderHandle struct {
tomo.Box
}
func newSlider (orient string, value float64) *Slider {
this := &Slider {
ContainerBox: tomo.NewContainerBox(),
handle: &SliderHandle {
slider := &Slider {
box: tomo.NewContainerBox(),
handle: &sliderHandle {
Box: tomo.NewBox(),
},
layout: sliderLayout {
@@ -40,23 +40,23 @@ func newSlider (orient string, value float64) *Slider {
step: 0.05,
}
this.Add(this.handle)
this.SetFocusable(true)
this.SetValue(value)
this.SetInputMask(true)
this.OnKeyUp(this.handleKeyUp)
this.OnKeyDown(this.handleKeyDown)
this.OnButtonDown(this.handleButtonDown)
this.OnButtonUp(this.handleButtonUp)
this.OnMouseMove(this.handleMouseMove)
this.OnScroll(this.handleScroll)
slider.handle.SetRole(tomo.R("objects", "SliderHandle"))
slider.handle.SetTag(orient, true)
slider.box.SetRole(tomo.R("objects", "Slider"))
slider.box.SetTag(orient, true)
this.handle.SetRole(tomo.R("objects", "SliderHandle"))
this.handle.SetTag(orient, true)
this.SetRole(tomo.R("objects", "Slider"))
this.SetTag(orient, true)
return this
slider.box.Add(slider.handle)
slider.box.SetFocusable(true)
slider.SetValue(value)
slider.box.SetInputMask(true)
slider.box.OnKeyUp(slider.handleKeyUp)
slider.box.OnKeyDown(slider.handleKeyDown)
slider.box.OnButtonDown(slider.handleButtonDown)
slider.box.OnButtonUp(slider.handleButtonUp)
slider.box.OnMouseMove(slider.handleMouseMove)
slider.box.OnScroll(slider.handleScroll)
return slider
}
// NewVerticalSlider creates a new vertical slider with the specified value.
@@ -69,13 +69,25 @@ func NewHorizontalSlider (value float64) *Slider {
return newSlider("horizontal", value)
}
// GetBox returns the underlying box.
func (this *Slider) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this slider has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *Slider) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// SetValue sets the value of the slider between 0 and 1.
func (this *Slider) SetValue (value float64) {
if value < 0 { value = 0 }
if value > 1 { value = 1 }
if value == this.layout.value { return }
this.layout.value = value
this.SetAttr(tomo.ALayout(this.layout))
this.box.SetAttr(tomo.ALayout(this.layout))
}
// Value returns the value of the slider between 0 and 1.
@@ -104,7 +116,7 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) bool {
switch key {
case input.KeyUp, input.KeyLeft:
if this.Window().Modifiers().Alt {
if this.box.Window().Modifiers().Alt {
this.SetValue(0)
} else {
this.SetValue(this.Value() - increment)
@@ -112,7 +124,7 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) bool {
this.on.valueChange.Broadcast()
return true
case input.KeyDown, input.KeyRight:
if this.Window().Modifiers().Alt {
if this.box.Window().Modifiers().Alt {
this.SetValue(1)
} else {
this.SetValue(this.Value() + increment)
@@ -142,7 +154,7 @@ func (this *Slider) handleKeyUp (key input.Key, numpad bool) bool {
}
func (this *Slider) handleButtonDown (button input.Button) bool {
pointer := this.Window().MousePosition()
pointer := this.box.Window().MousePosition()
handle := this.handle.Bounds()
within := pointer.In(handle)
@@ -158,7 +170,7 @@ func (this *Slider) handleButtonDown (button input.Button) bool {
this.dragging = true
this.dragOffset =
pointer.Sub(this.handle.Bounds().Min).
Add(this.InnerBounds().Min)
Add(this.box.InnerBounds().Min)
this.drag()
} else {
this.dragOffset = this.fallbackDragOffset()
@@ -211,8 +223,8 @@ func (this *Slider) handleScroll (x, y float64) bool {
}
func (this *Slider) drag () {
pointer := this.Window().MousePosition().Sub(this.dragOffset)
gutter := this.InnerBounds()
pointer := this.box.Window().MousePosition().Sub(this.dragOffset)
gutter := this.box.InnerBounds()
handle := this.handle.Bounds()
if this.layout.vertical {
@@ -230,10 +242,10 @@ func (this *Slider) drag () {
func (this *Slider) fallbackDragOffset () image.Point {
if this.layout.vertical {
return this.InnerBounds().Min.
return this.box.InnerBounds().Min.
Add(image.Pt(0, this.handle.Bounds().Dy() / 2))
} else {
return this.InnerBounds().Min.
return this.box.InnerBounds().Min.
Add(image.Pt(this.handle.Bounds().Dx() / 2, 0))
}
}

View File

@@ -10,12 +10,14 @@ import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/objects/layouts"
import "git.tebibyte.media/tomo/objects/internal"
var _ tomo.Object = new(Swatch)
// Swatch displays a color, allowing the user to edit it by clicking on it.
type Swatch struct {
tomo.CanvasBox
value color.Color
box tomo.CanvasBox
value color.Color
editing bool
label string
label string
on struct {
valueChange event.FuncBroadcaster
confirm event.FuncBroadcaster
@@ -25,20 +27,32 @@ type Swatch struct {
// NewSwatch creates a new swatch with the given color.
func NewSwatch (value color.Color) *Swatch {
swatch := &Swatch {
CanvasBox: tomo.NewCanvasBox(),
box: tomo.NewCanvasBox(),
}
swatch.SetRole(tomo.R("objects", "Swatch"))
swatch.SetDrawer(swatch)
swatch.box.SetRole(tomo.R("objects", "Swatch"))
swatch.box.SetDrawer(swatch)
swatch.SetValue(value)
swatch.OnButtonDown(swatch.handleButtonDown)
swatch.OnButtonUp(swatch.handleButtonUp)
swatch.OnKeyDown(swatch.handleKeyDown)
swatch.OnKeyUp(swatch.handleKeyUp)
swatch.SetFocusable(true)
swatch.box.OnButtonDown(swatch.handleButtonDown)
swatch.box.OnButtonUp(swatch.handleButtonUp)
swatch.box.OnKeyDown(swatch.handleKeyDown)
swatch.box.OnKeyUp(swatch.handleKeyUp)
swatch.box.SetFocusable(true)
return swatch
}
// GetBox returns the underlying box.
func (this *Swatch) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this swatch has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *Swatch) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// Value returns the color of the swatch.
func (this *Swatch) Value () color.Color {
return this.value
@@ -48,7 +62,7 @@ func (this *Swatch) Value () color.Color {
func (this *Swatch) SetValue (value color.Color) {
this.value = value
if value == nil { value = color.Transparent }
this.Invalidate()
this.box.Invalidate()
}
// OnValueChange specifies a function to be called when the swatch's color
@@ -75,7 +89,7 @@ func (this *Swatch) Choose () {
var err error
var window tomo.Window
if parent := this.Window(); parent != nil {
if parent := this.box.Window(); parent != nil {
window, err = parent.NewChild(image.Rectangle { })
} else {
window, err = tomo.NewWindow(image.Rectangle { })
@@ -129,7 +143,7 @@ func (this *Swatch) Choose () {
layouts.ContractHorizontal,
cancelButton,
okButton)
controlRow.SetAttr(tomo.AAlign(tomo.AlignEnd, tomo.AlignMiddle))
controlRow.SetAlign(tomo.AlignEnd, tomo.AlignMiddle)
window.SetRoot(NewOuterContainer (
layouts.Column { true, false },
colorPicker,
@@ -155,13 +169,13 @@ func (this *Swatch) Draw (can canvas.Canvas) {
// transparency slash
pen.Stroke(color.RGBA { R: 255, A: 255 })
pen.StrokeWeight(1)
pen.Path(this.Bounds().Min, this.Bounds().Max)
pen.Path(this.box.Bounds().Min, this.box.Bounds().Max)
// color
if this.value != nil {
pen.StrokeWeight(0)
pen.Fill(this.value)
pen.Rectangle(this.Bounds())
pen.Rectangle(this.box.Bounds())
}
}
@@ -188,7 +202,7 @@ func (this *Swatch) handleButtonDown (button input.Button) bool {
func (this *Swatch) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) {
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.Choose()
}
return true

View File

@@ -4,8 +4,12 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(TabbedContainer)
// TabbedContainer holds multiple objects, each in their own tab. The user can
// click the tab bar at the top to choose which one is activated.
type TabbedContainer struct {
tomo.ContainerBox
box tomo.ContainerBox
leftSpacer tomo.Box
rightSpacer tomo.Box
@@ -14,43 +18,51 @@ type TabbedContainer struct {
tabs []*tab
}
// NewTabbedContainer creates a new tabbed container.
func NewTabbedContainer () *TabbedContainer {
container := &TabbedContainer {
ContainerBox: tomo.NewContainerBox(),
tabbedContainer := &TabbedContainer {
box: tomo.NewContainerBox(),
}
container.SetRole(tomo.R("objects", "TabbedContainer"))
container.SetAttr(tomo.ALayout(layouts.Column { false, true }))
tabbedContainer.box.SetRole(tomo.R("objects", "TabbedContainer"))
tabbedContainer.box.SetAttr(tomo.ALayout(layouts.Column { false, true }))
container.tabsRow = tomo.NewContainerBox()
container.tabsRow.SetRole(tomo.R("objects", "TabRow"))
container.Add(container.tabsRow)
tabbedContainer.tabsRow = tomo.NewContainerBox()
tabbedContainer.tabsRow.SetRole(tomo.R("objects", "TabRow"))
tabbedContainer.box.Add(tabbedContainer.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)
tabbedContainer.leftSpacer = tomo.NewBox()
tabbedContainer.leftSpacer.SetRole(tomo.R("objects", "TabSpacer"))
tabbedContainer.leftSpacer.SetTag("left", true)
tabbedContainer.rightSpacer = tomo.NewBox()
tabbedContainer.rightSpacer.SetRole(tomo.R("objects", "TabSpacer"))
tabbedContainer.rightSpacer.SetTag("left", true)
container.ClearTabs()
container.setTabRowLayout()
return container
tabbedContainer.ClearTabs()
tabbedContainer.setTabRowLayout()
return tabbedContainer
}
// GetBox returns the underlying box.
func (this *TabbedContainer) GetBox () tomo.Box {
return this.box
}
// Activate switches to a named tab.
func (this *TabbedContainer) Activate (name string) {
if _, tab := this.findTab(this.active); tab != nil {
tab.setActive(false)
this.Remove(tab.root)
this.box.Remove(tab.root)
}
if _, tab := this.findTab(name); tab != nil {
tab.setActive(true)
this.Add(tab.root)
this.box.Add(tab.root)
} else {
name = ""
}
this.active = name
}
// AddTab adds an object as a tab with the specified name.
func (this *TabbedContainer) AddTab (name string, root tomo.Object) {
tab := &tab {
TextBox: tomo.NewTextBox(),
@@ -79,6 +91,7 @@ func (this *TabbedContainer) AddTab (name string, root tomo.Object) {
}
}
// RemoveTab removes the named tab.
func (this *TabbedContainer) RemoveTab (name string) {
index, tab := this.findTab(name)
if index < 0 { return }
@@ -97,6 +110,7 @@ func (this *TabbedContainer) RemoveTab (name string) {
}
}
// ClearTabs removes all tabs.
func (this *TabbedContainer) ClearTabs () {
this.tabs = nil
this.tabsRow.Clear()

View File

@@ -6,9 +6,11 @@ import "git.tebibyte.media/tomo/tomo/text"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.ContentObject = new(TextInput)
// TextInput is a single-line editable text box.
type TextInput struct {
tomo.TextBox
box tomo.TextBox
text []rune
on struct {
valueChange event.FuncBroadcaster
@@ -18,23 +20,68 @@ type TextInput struct {
// 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
textInput := &TextInput { box: tomo.NewTextBox() }
textInput.box.SetRole(tomo.R("objects", "TextInput"))
textInput.box.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
textInput.box.SetAttr(tomo.AOverflow(true, false))
textInput.SetValue(text)
textInput.box.SetFocusable(true)
textInput.box.SetSelectable(true)
textInput.box.OnKeyDown(textInput.handleKeyDown)
textInput.box.OnKeyUp(textInput.handleKeyUp)
textInput.box.OnScroll(textInput.handleScroll)
return textInput
}
// GetBox returns the underlying box.
func (this *TextInput) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this text input has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *TextInput) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// Select sets the text cursor or selection.
func (this *TextInput) Select (dot text.Dot) {
this.box.Select(dot)
}
// Dot returns the text cursor or selection.
func (this *TextInput) Dot () text.Dot {
return this.box.Dot()
}
// SetAlign sets the X and Y alignment of the text input.
func (this *TextInput) SetAlign (x, y tomo.Align) {
this.box.SetAttr(tomo.AAlign(x, y))
}
// ContentBounds returns the bounds of the inner content of the text input
// relative to the input's InnerBounds.
func (this *TextInput) ContentBounds () image.Rectangle {
return this.box.ContentBounds()
}
// ScrollTo shifts the origin of the text input's content to the origin of the
// inputs's InnerBounds, offset by the given point.
func (this *TextInput) ScrollTo (position image.Point) {
this.box.ScrollTo(position)
}
// OnContentBoundsChange specifies a function to be called when the text input's
// ContentBounds or InnerBounds changes.
func (this *TextInput) OnContentBoundsChange (callback func ()) event.Cookie {
return this.box.OnContentBoundsChange(callback)
}
// SetValue sets the text content of the input.
func (this *TextInput) SetValue (text string) {
this.text = []rune(text)
this.TextBox.SetText(text)
this.box.SetText(text)
}
// Value returns the text content of the input.
@@ -42,16 +89,6 @@ func (this *TextInput) Value () string {
return string(this.text)
}
// SetText sets the text content of the input.
func (this *TextInput) SetText (text string) {
this.SetValue(text)
}
// Text returns the text content of the input.
func (this *TextInput) Text () string {
return this.Value()
}
// OnConfirm specifies a function to be called when the user presses enter
// within the text input.
func (this *TextInput) OnConfirm (callback func ()) event.Cookie {
@@ -69,19 +106,19 @@ 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))
this.box.SetText(string(this.text))
}
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
dot := this.Dot()
modifiers := this.Window().Modifiers()
modifiers := this.box.Window().Modifiers()
word := modifiers.Control
changed := false
defer func () {
this.Select(dot)
if changed {
this.SetText(string(this.text))
this.box.SetText(string(this.text))
this.on.valueChange.Broadcast()
}
} ()
@@ -108,7 +145,7 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
}
func (this *TextInput) handleKeyUp (key input.Key, numpad bool) bool {
modifiers := this.Window().Modifiers()
modifiers := this.box.Window().Modifiers()
switch {
case isConfirmationKey(key):
return true

View File

@@ -2,23 +2,77 @@ package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/text"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(TabbedContainer)
// TextView is an area for displaying a large amount of multi-line text.
type TextView struct {
tomo.TextBox
box tomo.TextBox
}
// NewTextView creates a new text view.
func NewTextView (text string) *TextView {
this := &TextView { TextBox: tomo.NewTextBox() }
this.SetRole(tomo.R("objects", "TextView"))
this.SetFocusable(true)
this.SetSelectable(true)
this.SetText(text)
this.SetAttr(tomo.AOverflow(false, true))
this.SetAttr(tomo.AWrap(true))
this.OnScroll(this.handleScroll)
return this
textView := &TextView { box: tomo.NewTextBox() }
textView.box.SetRole(tomo.R("objects", "TextView"))
textView.box.SetFocusable(true)
textView.box.SetSelectable(true)
textView.SetText(text)
textView.box.SetAttr(tomo.AOverflow(false, true))
textView.box.SetAttr(tomo.AWrap(true))
textView.box.OnScroll(textView.handleScroll)
return textView
}
// GetBox returns the underlying box.
func (this *TextView) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this text view has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *TextView) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// Select sets the text cursor or selection.
func (this *TextView) Select (dot text.Dot) {
this.box.Select(dot)
}
// Dot returns the text cursor or selection.
func (this *TextView) Dot () text.Dot {
return this.box.Dot()
}
// SetAlign sets the X and Y alignment of the text view.
func (this *TextView) SetAlign (x, y tomo.Align) {
this.box.SetAttr(tomo.AAlign(x, y))
}
// ContentBounds returns the bounds of the inner content of the text view
// relative to the text view's InnerBounds.
func (this *TextView) ContentBounds () image.Rectangle {
return this.box.ContentBounds()
}
// ScrollTo shifts the origin of the text view's content to the origin of the
// text view's InnerBounds, offset by the given point.
func (this *TextView) ScrollTo (position image.Point) {
this.box.ScrollTo(position)
}
// OnContentBoundsChange specifies a function to be called when the text view's
// ContentBounds or InnerBounds changes.
func (this *TextView) OnContentBoundsChange (callback func ()) event.Cookie {
return this.box.OnContentBoundsChange(callback)
}
// SetText sets the text content of the view.
func (this *TextView) SetText (text string) {
this.box.SetText(text)
}
func (this *TextView) handleScroll (x, y float64) bool {