58 Commits

Author SHA1 Message Date
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
611705fa0d Change icon on dropdown 2024-08-16 18:01:08 -04:00
16645eeeda Update Tomo API 2024-08-16 18:01:01 -04:00
3219cb712c Remove TODO input value in swatch.go 2024-08-16 16:28:47 -04:00
7d14a25482 Fixed ctrl+key combos on TextInput 2024-08-16 16:17:11 -04:00
e4857da22d Functions to check for common buttons/keys 2024-08-16 16:15:52 -04:00
114cbb346d Keyboard controls activate on key down instead of key up 2024-08-16 15:31:48 -04:00
43ec7a0311 Swatch accepts hex input 2024-08-16 15:17:44 -04:00
3d28c8fea1 Add functions for parsing/formatting NRGBA values 2024-08-16 15:17:15 -04:00
669c638fad Fix transparency in color pickers again 2024-08-16 13:50:22 -04:00
2fe433991d Rename ColorPicker to HSVAColorPicker 2024-08-15 17:05:19 -04:00
acec0f6222 Fix HSV.RGBA sector overflow 2024-08-15 16:51:36 -04:00
0865c28965 Update color picker code in response to HSV color changes 2024-08-15 16:42:31 -04:00
2546c338ad Separate HSVA color into HSV, HSVA, fix alpha premultiplication 2024-08-15 16:41:22 -04:00
b3e7178176 Bring TextInput in line with all the other inputs 2024-08-15 13:17:43 -04:00
080e4511f2 Add Dropdown 2024-08-14 19:06:41 -04:00
f1ac74dcbc Fix TabbedContainer not setting tags correctly 2024-08-14 11:45:30 -04:00
ce0bc5be3b Add MenuHeading 2024-08-14 11:45:10 -04:00
eb0bf58961 Improvements to menus
Major progress on #4
2024-08-14 11:44:47 -04:00
8068036219 Fix icon sizes 2024-08-12 22:06:06 -04:00
5a32b06cef Various improvements to Icon, MimeIcon 2024-08-11 22:36:10 -04:00
fe50f5783b Upgrade Tomo API 2024-08-11 22:35:50 -04:00
73a5fab0bc Icon sets its minimum size properly 2024-08-11 12:20:23 -04:00
61addc051b Button sets an icon tag 2024-08-11 12:20:12 -04:00
7e275cc70e Update Tomo API 2024-08-10 21:44:33 -04:00
9856cd327f Split MimeIcon out from Icon 2024-08-10 21:44:03 -04:00
572e0c49af LabelCheckbox sets its label as not selectable 2024-07-29 01:50:00 -04:00
e0f4ecb509 Update Tomo API to v0.41.1 2024-07-27 15:04:41 -04:00
fc51ffe33c Fix flow layout 2024-07-27 14:41:46 -04:00
987f4bfc4a Remove random semicolons 2024-07-27 02:17:41 -04:00
b883542f3b Add .editorconfig 2024-07-27 02:10:00 -04:00
c8d33a0ef4 Fix keys for Scrollbar 2024-07-27 00:54:40 -04:00
9fa764c7b9 ScrollContainer can step scroll with normal up/down 2024-07-27 00:35:17 -04:00
84ab0895f8 Fix TextView scrolling 2024-07-27 00:19:53 -04:00
b9c77fd5f7 ScrollContainer properly responds to pgup/down 2024-07-27 00:19:31 -04:00
2722d19ecd Scrollbars accept both directions of scroll 2024-07-26 21:01:18 -04:00
4fc44c11e8 Label and Heading are now selectable 2024-07-26 21:00:53 -04:00
0cdb116ec1 TextInput no longer captures scroll with a zero X 2024-07-26 18:58:16 -04:00
6ea1679112 Offload selection manipulation of TextInput to backend 2024-07-26 18:49:54 -04:00
b87f32eac9 TextInput scrolls in the proper direction 2024-07-26 18:11:02 -04:00
793526238a Scrollbar drags on mouse motion 2024-07-26 18:10:48 -04:00
884148f006 Fix ScrollContainer layout again 2024-07-26 17:56:29 -04:00
3e382da688 Fix ScrollContainer layout 2024-07-26 17:54:32 -04:00
18b8898644 Fix dialog alignment 2024-07-26 00:34:05 -04:00
29 changed files with 750 additions and 275 deletions

12
.editorconfig Normal file
View 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

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.SetAttr(tomo.AAlign(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,8 +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.box.SetTag("icon", this.icon != nil)
this.applyLayout()
}
@@ -79,33 +94,33 @@ 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))
}
}
func (this *Button) handleKeyDown (key input.Key, numberPad bool) bool {
if key != input.KeyEnter && key != input.Key(' ') { return false }
return true
}
func (this *Button) handleKeyUp (key input.Key, numberPad bool) bool {
if key != input.KeyEnter && key != input.Key(' ') { return false }
if !isClickingKey(key) { return false }
this.on.click.Broadcast()
return true
}
func (this *Button) handleKeyUp (key input.Key, numberPad bool) bool {
if !isClickingKey(key) { return false }
return true
}
func (this *Button) handleButtonDown (button input.Button) bool {
if button != input.ButtonLeft { return false }
if !isClickingButton(button) { 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()) {
if !isClickingButton(button) { return false }
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)
@@ -48,17 +50,22 @@ func NewCalendar (tm time.Time) *Calendar {
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

@@ -54,23 +54,23 @@ func (this *Checkbox) OnValueChange (callback func ()) event.Cookie {
}
func (this *Checkbox) handleKeyDown (key input.Key, numberPad bool) bool {
if key != input.KeyEnter && key != input.Key(' ') { return false }
if !isClickingKey(key) { return false }
this.Toggle()
return true
}
func (this *Checkbox) handleKeyUp (key input.Key, numberPad bool) bool {
if key != input.KeyEnter && key != input.Key(' ') { return false}
if !isClickingKey(key) { return false}
return true
}
func (this *Checkbox) handleButtonDown (button input.Button) bool {
if button != input.ButtonLeft { return false }
if !isClickingButton(button) { return false }
return true
}
func (this *Checkbox) handleButtonUp (button input.Button) bool {
if button != input.ButtonLeft { return false }
if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) {
this.Toggle()
}

View File

@@ -8,13 +8,13 @@ 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
// HSVAColorPicker allows the user to pick a color by controlling its HSVA
// parameters.
type ColorPicker struct {
type HSVAColorPicker struct {
tomo.ContainerBox
value internal.HSVA
pickerMap *colorPickerMap
pickerMap *hsvaColorPickerMap
hueSlider *Slider
alphaSlider *Slider
@@ -23,14 +23,14 @@ type ColorPicker struct {
}
}
// NewColorPicker creates a new color picker with the specified color.
func NewColorPicker (value color.Color) *ColorPicker {
picker := &ColorPicker {
// NewHSVAColorPicker creates a new color picker with the specified color.
func NewHSVAColorPicker (value color.Color) *HSVAColorPicker {
picker := &HSVAColorPicker {
ContainerBox: tomo.NewContainerBox(),
}
picker.SetRole(tomo.R("objects", "ColorPicker"))
picker.SetAttr(tomo.ALayout(layouts.Row { true, false, false }))
picker.pickerMap = newColorPickerMap(picker)
picker.pickerMap = newHsvaColorPickerMap(picker)
picker.Add(picker.pickerMap)
picker.hueSlider = NewVerticalSlider(0.0)
@@ -44,7 +44,7 @@ func NewColorPicker (value color.Color) *ColorPicker {
picker.alphaSlider = NewVerticalSlider(0.0)
picker.Add(picker.alphaSlider)
picker.alphaSlider.OnValueChange(func () {
picker.value.A = uint8(picker.alphaSlider.Value() * 255)
picker.value.A = uint16(picker.alphaSlider.Value() * 0xFFFF)
picker.on.valueChange.Broadcast()
picker.pickerMap.Invalidate()
})
@@ -55,37 +55,38 @@ func NewColorPicker (value color.Color) *ColorPicker {
}
// Value returns the color of the picker.
func (this *ColorPicker) Value () color.Color {
func (this *HSVAColorPicker) Value () color.Color {
return this.value
}
// SetValue sets the color of the picker.
func (this *ColorPicker) SetValue (value color.Color) {
func (this *HSVAColorPicker) SetValue (value color.Color) {
if value == nil { value = color.Transparent }
this.value = internal.RGBAToHSVA(value.RGBA())
this.value = internal.HSVAModel.Convert(value).(internal.HSVA)
this.hueSlider.SetValue(this.value.H)
this.alphaSlider.SetValue(float64(this.value.A) / 255)
this.alphaSlider.SetValue(float64(this.value.A) / 0xFFFF)
this.pickerMap.Invalidate()
}
// OnValueChange specifies a function to be called when the user changes the
// swatch's color.
func (this *ColorPicker) OnValueChange (callback func ()) event.Cookie {
func (this *HSVAColorPicker) 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) {
func (this *HSVAColorPicker) RGBA () (r, g, b, a uint32) {
return this.value.RGBA()
}
type colorPickerMap struct {
type hsvaColorPickerMap struct {
tomo.CanvasBox
dragging bool
parent *ColorPicker
parent *HSVAColorPicker
}
func newColorPickerMap (parent *ColorPicker) *colorPickerMap {
picker := &colorPickerMap {
func newHsvaColorPickerMap (parent *HSVAColorPicker) *hsvaColorPickerMap {
picker := &hsvaColorPickerMap {
CanvasBox: tomo.NewCanvasBox(),
parent: parent,
}
@@ -97,26 +98,26 @@ func newColorPickerMap (parent *ColorPicker) *colorPickerMap {
return picker
}
func (this *colorPickerMap) handleButtonDown (button input.Button) bool {
func (this *hsvaColorPickerMap) 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 {
func (this *hsvaColorPickerMap) handleButtonUp (button input.Button) bool {
if button != input.ButtonLeft { return false }
this.dragging = false
return true
}
func (this *colorPickerMap) handleMouseMove () bool {
func (this *hsvaColorPickerMap) handleMouseMove () bool {
if !this.dragging { return false }
this.drag()
return true
}
func (this *colorPickerMap) drag () {
func (this *hsvaColorPickerMap) drag () {
pointer := this.Window().MousePosition()
bounds := this.InnerBounds()
this.parent.value.S = float64(pointer.X - bounds.Min.X) / float64(bounds.Dx())
@@ -126,7 +127,7 @@ func (this *colorPickerMap) drag () {
this.Invalidate()
}
func (this *colorPickerMap) Draw (can canvas.Canvas) {
func (this *hsvaColorPickerMap) 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 ++ {
@@ -137,7 +138,7 @@ func (this *colorPickerMap) Draw (can canvas.Canvas) {
H: this.parent.value.H,
S: float64(xx) / float64(bounds.Dx()),
V: 1 - float64(yy) / float64(bounds.Dy()),
A: 255,
A: 0xFFFF,
}
sPos := int( this.parent.value.S * float64(bounds.Dx()))

View File

@@ -1,6 +1,10 @@
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
@@ -8,14 +12,14 @@ import "git.tebibyte.media/tomo/tomo"
// its edges, whereas the inner container does not. The container will have a
// corresponding object role variation of either "outer" or "inner".
type Container struct {
tomo.ContainerBox
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 +31,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 +40,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,6 +53,7 @@ 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))
@@ -63,7 +64,7 @@ func NewDialog (kind DialogKind, parent tomo.Window, title, message string, opti
}
}
dialog.controlRow = NewInnerContainer(layouts.ContractHorizontal, options...)
messageText.SetAttr(tomo.AAlign(tomo.AlignEnd, tomo.AlignEnd))
dialog.controlRow.SetAlign(tomo.AlignEnd, tomo.AlignEnd)
dialog.SetRoot(NewOuterContainer (
layouts.Column { true, false },

114
dropdown.go Normal file
View File

@@ -0,0 +1,114 @@
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"
// Dropdown is a non-editable text input that allows the user to pick a value
// from a list.
type Dropdown struct {
tomo.ContainerBox
label *Label
value string
items []string
menu *Menu
on struct {
valueChange event.FuncBroadcaster
}
}
// NewDropdown creates a new dropdown input with the specified items
func NewDropdown (items ...string) *Dropdown {
dropdown := &Dropdown {
ContainerBox: 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.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)
return dropdown
}
// Value returns the value of the dropdown. This does not necissarily have to be
// in the list of items.
func (this *Dropdown) Value () string {
return this.value
}
// SetValue sets the value of the dropdown. This does not necissarily have to be
// in the list of items.
func (this *Dropdown) SetValue (value string) {
this.value = value
this.label.SetText(value)
}
// SetItems sets the items from which the user is able to pick.
func (this *Dropdown) SetItems (items ...string) {
this.items = items
}
// Choose creates a menu that allows the user to pick a value.
func (this *Dropdown) Choose () {
if this.menu != nil {
this.menu.Close()
}
menu, err := NewAnchoredMenu(this, this.itemList()...)
if err != nil { return }
this.menu = menu
menu.SetVisible(true)
}
func (this *Dropdown) itemList () []tomo.Object {
items := make([]tomo.Object, len(this.items))
for index, value := range this.items {
value := value
item := NewMenuItem(value)
item.OnClick(func () {
this.SetValue(value)
this.on.valueChange.Broadcast()
})
items[index] = item
}
return items
}
func (this *Dropdown) handleKeyDown (key input.Key, numberPad bool) bool {
if !isClickingKey(key) { return false }
this.Choose()
return true
}
func (this *Dropdown) handleKeyUp (key input.Key, numberPad bool) bool {
if !isClickingKey(key) { return false }
return true
}
func (this *Dropdown) handleButtonDown (button input.Button) bool {
if !isClickingButton(button) { return false }
return true
}
func (this *Dropdown) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) {
this.Choose()
}
return true
}

4
go.mod
View File

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

37
go.sum
View File

@@ -1,35 +1,2 @@
git.tebibyte.media/tomo/tomo v0.41.0 h1:Z+7FHhbGiKjs+kQNvuJOfz47xIct5qxvSJqyDuoNIOs=
git.tebibyte.media/tomo/tomo v0.41.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.46.1 h1:/8fT6I9l4TK529zokrThbNDHGRvUsNgif1Zs++0PBSQ=
git.tebibyte.media/tomo/tomo v0.46.1/go.mod h1:WrtilgKB1y8O2Yu7X4mYcRiqOlPR8NuUnoA/ynkQWrs=

View File

@@ -18,6 +18,14 @@ func NewHeading (level int, text string) *Heading {
this.SetRole(tomo.R("objects", "Heading"))
this.SetTag(fmt.Sprint(level), true)
this.SetText(text)
this.SetSelectable(true)
this.SetFocusable(true)
return this
}
// NewMenuHeading creatss a new heading for use in menus.
func NewMenuHeading (text string) *Heading {
heading := NewHeading(0, text)
heading.SetTag("menu", true)
return heading
}

29
icon.go
View File

@@ -1,12 +1,13 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/data"
import "git.tebibyte.media/tomo/tomo/canvas"
// Icon displays a single icon.
type Icon struct {
tomo.Box
icon tomo.Icon
size tomo.IconSize
}
func iconSizeString (size tomo.IconSize) string {
@@ -18,29 +19,29 @@ func iconSizeString (size tomo.IconSize) string {
}
// NewIcon creates a new icon from an icon ID.
func NewIcon (id tomo.Icon, size tomo.IconSize) *Icon {
func NewIcon (icon tomo.Icon, size tomo.IconSize) *Icon {
this := &Icon {
Box: tomo.NewBox(),
}
this.SetRole(tomo.R("objects", "Icon"))
this.SetTag(iconSizeString(size), true)
this.setTexture(id.Texture(size))
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 tomo.IconSize) *Icon {
this := &Icon {
Box: tomo.NewBox(),
}
this.SetRole(tomo.R("objects", "Icon"))
this.SetTag(iconSizeString(size), true)
this.setTexture(tomo.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.size = size
this.setTexture(icon.Texture(size))
}
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 {

19
input.go Normal file
View File

@@ -0,0 +1,19 @@
package objects
import "git.tebibyte.media/tomo/tomo/input"
func isClickingKey (key input.Key) bool {
return key == input.KeyEnter || key == input.Key(' ')
}
func isConfirmationKey (key input.Key) bool {
return key == input.KeyEnter
}
func isClickingButton (button input.Button) bool {
return button == input.ButtonLeft
}
func isMenuButton (button input.Button) bool {
return button == input.ButtonLeft
}

View File

@@ -1,13 +1,32 @@
package internal
import "fmt"
import "image/color"
// HSV represents a color with hue, saturation, and value components. Each
// component C is in range 0 <= C <= 1.
type HSV struct {
H float64
S float64
V float64
}
// HSVA is an HSV color with an added 8-bit alpha component. The alpha component
// ranges from 0x0000 (fully transparent) to 0xFFFF (opaque), and has no bearing
// on the other components.
type HSVA struct {
H float64
S float64
V float64
A uint8
A uint16
}
func (hsva HSVA) RGBA () (r, g, b, a uint32) {
var (
HSVModel color.Model = color.ModelFunc(hsvModel)
HSVAModel color.Model = color.ModelFunc(hsvaModel)
)
func (hsv HSV) RGBA () (r, g, b, a uint32) {
// Adapted from:
// https://www.cs.rit.edu/~ncs/color/t_convert.html
@@ -15,34 +34,57 @@ func (hsva HSVA) RGBA () (r, g, b, a uint32) {
return uint32(float64(0xFFFF) * x)
}
ca := uint32(hsva.A) << 8
s := clamp01(hsva.S)
v := clamp01(hsva.V)
s := clamp01(hsv.S)
v := clamp01(hsv.V)
if s == 0 {
light := component(v)
return light, light, light, ca
return light, light, light, 0xFFFF
}
h := clamp01(hsva.H) * 360
h := clamp01(hsv.H) * 360
sector := int(h / 60)
// otherwise when given 1.0 for H, sector would overflow to 6
if sector > 5 { sector = 5 }
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)))
p := component(v * (1 - s))
q := component(v * (1 - s * offset))
t := component(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
case 0: return va, t, p, 0xFFFF
case 1: return q, va, p, 0xFFFF
case 2: return p, va, t, 0xFFFF
case 3: return p, q, va, 0xFFFF
case 4: return t, p, va, 0xFFFF
default: return va, p, q, 0xFFFF
}
}
func (hsva HSVA) RGBA () (r, g, b, a uint32) {
r, g, b, a = HSV {
H: hsva.H,
S: hsva.S,
V: hsva.V,
}.RGBA()
a = uint32(hsva.A)
// alpha premultiplication
r = (r * a) / 0xFFFF
g = (g * a) / 0xFFFF
b = (b * a) / 0xFFFF
return
}
// Canon returns the color but with the H, S, and V fields are constrained to
// the range 0.0-1.0
func (hsv HSV) Canon () HSV {
hsv.H = clamp01(hsv.H)
hsv.S = clamp01(hsv.S)
hsv.V = clamp01(hsv.V)
return hsv
}
// 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 {
@@ -58,7 +100,38 @@ func clamp01 (x float64) float64 {
return x
}
func RGBAToHSVA (r, g, b, a uint32) HSVA {
func hsvModel (c color.Color) color.Color {
switch c := c.(type) {
case HSV: return c
case HSVA: return HSV { H: c.H, S: c.S, V: c.V }
default:
r, g, b, a := c.RGBA()
// alpha unpremultiplication
r = (r / a) * 0xFFFF
g = (g / a) * 0xFFFF
b = (b / a) * 0xFFFF
return rgbToHSV(r, g, b)
}
}
func hsvaModel (c color.Color) color.Color {
switch c := c.(type) {
case HSV: return HSVA { H: c.H, S: c.S, V: c.V, A: 0xFFFF }
case HSVA: return c
default:
r, g, b, a := c.RGBA()
hsv := rgbToHSV(r, g, b)
return HSVA {
H: hsv.H,
S: hsv.S,
V: hsv.V,
A: uint16(a),
}
}
}
func rgbToHSV (r, g, b uint32) HSV {
// Adapted from:
// https://www.cs.rit.edu/~ncs/color/t_convert.html
@@ -78,27 +151,81 @@ func RGBAToHSVA (r, g, b, a uint32) HSVA {
if cg < minComponent { minComponent = cg }
if cb < minComponent { minComponent = cb }
hsva := HSVA {
hsv := HSV {
V: maxComponent,
A: uint8(a >> 8),
}
delta := maxComponent - minComponent
if delta == 0 {
// hsva.S is undefined, so hue doesn't matter
return hsva
return hsv
}
hsva.S = delta / maxComponent
hsv.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
case cr == maxComponent: hsv.H = (cg - cb) / delta
case cg == maxComponent: hsv.H = 2 + (cb - cr) / delta
case cb == maxComponent: hsv.H = 4 + (cr - cg) / delta
}
hsva.H *= 60
if hsva.H < 0 { hsva.H += 360 }
hsva.H /= 360
hsv.H *= 60
if hsv.H < 0 { hsv.H += 360 }
hsv.H /= 360
return hsva
return hsv
}
// FormatNRGBA formats an NRGBA value into a hex string.
func FormatNRGBA (nrgba color.NRGBA) string {
return fmt.Sprintf("%02X%02X%02X%02X", nrgba.R, nrgba.G, nrgba.B, nrgba.A)
}
// ParseNRGBA parses an NRGBA value from a hex string. It can be of the format:
// - RGB
// - RGBA
// - RRGGBB
// - RRGGBBAA
// If none of these are specified, this function will return an opaque black
// color. Hex digits may either be upper case or lower case.
func ParseNRGBA (str string) color.NRGBA {
runes := []rune(str)
c := color.NRGBA { A: 255 }
switch len(runes) {
case 3:
c.R = fillOctet(hexDigit(runes[0]))
c.G = fillOctet(hexDigit(runes[1]))
c.B = fillOctet(hexDigit(runes[2]))
case 4:
c.R = fillOctet(hexDigit(runes[0]))
c.G = fillOctet(hexDigit(runes[1]))
c.B = fillOctet(hexDigit(runes[2]))
c.A = fillOctet(hexDigit(runes[3]))
case 6:
c.R = hexOctet(runes[0], runes[1])
c.G = hexOctet(runes[2], runes[3])
c.B = hexOctet(runes[4], runes[5])
case 8:
c.R = hexOctet(runes[0], runes[1])
c.G = hexOctet(runes[2], runes[3])
c.B = hexOctet(runes[4], runes[5])
c.A = hexOctet(runes[6], runes[7])
}
return c
}
func hexDigit (r rune) uint8 {
switch {
case r >= '0' && r <= '9': return uint8(r - '0')
case r >= 'A' && r <= 'F': return uint8(r - 'A') + 10
case r >= 'a' && r <= 'f': return uint8(r - 'a') + 10
default: return 0
}
}
func fillOctet (low uint8) uint8 {
return low << 4 | low
}
func hexOctet (high, low rune) uint8 {
return hexDigit(high) << 4 | hexDigit(low)
}

View File

@@ -13,5 +13,7 @@ func NewLabel (text string) *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
}

View File

@@ -22,6 +22,8 @@ func NewLabelCheckbox (value bool, text string) *LabelCheckbox {
}
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 }))
@@ -53,12 +55,12 @@ func (this *LabelCheckbox) OnValueChange (callback func ()) event.Cookie {
}
func (this *LabelCheckbox) handleButtonDown (button input.Button) bool {
if button != input.ButtonLeft { return false }
if !isClickingButton(button) { return false }
return true
}
func (this *LabelCheckbox) handleButtonUp (button input.Button) bool {
if button != input.ButtonLeft { return false }
if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) {
this.checkbox.SetFocused(true)
this.checkbox.Toggle()

View File

@@ -24,6 +24,8 @@ func NewLabelSwatch (value color.Color, text string) *LabelSwatch {
box.SetRole(tomo.R("objects", "LabelSwatch"))
box.swatch.label = text
box.label.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
box.label.SetSelectable(false)
box.label.SetFocusable(false)
box.Add(box.swatch)
box.Add(box.label)
box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
@@ -61,12 +63,12 @@ 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 !isClickingButton(button) { return true }
if this.Window().MousePosition().In(this.Bounds()) {
this.swatch.SetFocused(true)
this.swatch.Choose()

View File

@@ -45,13 +45,14 @@ func (flow Flow) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
index := 0
for index < boxes.Len() {
// get a slice of boxes for this major step
stepIndexEnd := index + minorSteps
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 index := index; index < stepIndexEnd; index ++ {
for index := stepIndexStart; index < stepIndexEnd; index ++ {
boxSize := flow.major(boxes.MinimumSize(index))
if boxSize > majorSize { majorSize = boxSize }
}
@@ -61,7 +62,7 @@ func (flow Flow) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
var size image.Point
size = flow.incrMajor(size, majorSize)
size = flow.incrMinor(size, minorSize)
for index := index; index < stepIndexEnd; index ++ {
for index := stepIndexStart; index < stepIndexEnd; index ++ {
bounds := image.Rectangle { Min: point, Max: point.Add(size) }
boxes.SetBounds(index, bounds)

View File

@@ -50,7 +50,7 @@ func (column Column) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
if !hints.OverflowY {
gaps := boxes.Len() - 1
freeSpace := float64(hints.Bounds.Dy() - hints.Gap.Y * gaps)
nExpanding := 0;
nExpanding := 0
for index := 0; index < boxes.Len(); index ++ {
if expands(index) {
nExpanding ++
@@ -121,7 +121,7 @@ func (row Row) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
if !hints.OverflowY {
gaps := boxes.Len() - 1
freeSpace := float64(hints.Bounds.Dx() - hints.Gap.X * gaps)
nExpanding := 0;
nExpanding := 0
for index := 0; index < boxes.Len(); index ++ {
if expands(index) {
nExpanding ++

51
menu.go
View File

@@ -18,22 +18,27 @@ type Menu struct {
}
// 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) {
// directly under the mouse pointer.
func NewMenu (parent tomo.Window, items ...tomo.Object) (*Menu, error) {
bounds := (image.Rectangle { }).Add(parent.MousePosition())
return newMenu(parent, bounds, items...)
}
// NewAnchoredMenu creates a new menu with the specified items. The menu will
// appear directly under the anchor.
func NewAnchoredMenu (anchor tomo.Object, items ...tomo.Object) (*Menu, error) {
parent := anchor.GetBox().Window()
bounds := menuBoundsFromAnchor(anchor)
return newMenu(parent, bounds, items...)
}
func newMenu (parent tomo.Window, bounds image.Rectangle, 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.bounds = bounds
menu.parent = parent
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))
@@ -67,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()
@@ -74,6 +80,7 @@ func (this *Menu) TearOff () {
this.Window.Close()
this.rootContainer.Remove(this.tearLine)
this.rootContainer.SetTag("torn", true)
this.Window = window
this.Window.SetRoot(this.rootContainer)
@@ -84,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

@@ -20,10 +20,15 @@ type MenuItem struct {
// NewMenuItem creates a new menu item with the specified text.
func NewMenuItem (text string) *MenuItem {
return NewIconMenuItem(tomo.IconUnknown, text)
}
// 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("", tomo.IconSizeSmall),
icon: NewIcon(icon, tomo.IconSizeSmall),
}
box.SetRole(tomo.R("objects", "MenuItem"))
box.label.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
@@ -33,6 +38,8 @@ func NewMenuItem (text string) *MenuItem {
box.Add(box.label)
box.SetInputMask(true)
box.OnMouseEnter(box.handleMouseEnter)
box.OnMouseLeave(box.handleMouseLeave)
box.OnButtonDown(box.handleButtonDown)
box.OnButtonUp(box.handleButtonUp)
box.OnKeyDown(box.handleKeyDown)
@@ -49,8 +56,7 @@ func (this *MenuItem) SetText (text string) {
// 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)
this.icon.SetIcon(id, tomo.IconSizeSmall)
}
// OnClick specifies a function to be called when the menu item is clicked.
@@ -58,6 +64,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

46
mimeicon.go Normal file
View 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)))
}
}

View File

@@ -9,7 +9,7 @@ import "git.tebibyte.media/tomo/tomo/event"
// overflowing ContainerBox.
type Scrollbar struct {
tomo.ContainerBox
handle *SliderHandle
handle *sliderHandle
layout scrollbarLayout
dragging bool
dragOffset image.Point
@@ -24,7 +24,7 @@ type Scrollbar struct {
func newScrollbar (orient string) *Scrollbar {
this := &Scrollbar {
ContainerBox: tomo.NewContainerBox(),
handle: &SliderHandle {
handle: &sliderHandle {
Box: tomo.NewBox(),
},
layout: scrollbarLayout {
@@ -43,9 +43,9 @@ func newScrollbar (orient string) *Scrollbar {
this.OnMouseMove(this.handleMouseMove)
this.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.SetRole(tomo.R("objects", "Scrollbar"))
this.SetTag(orient, true)
return this
}
@@ -141,19 +141,15 @@ 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.KeyHome: return true
case input.KeyEnd: 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 {
var increment float64; if this.layout.vertical {
increment = -0.05
} else {
increment = 0.05
}
modifiers := this.Window().Modifiers()
switch key {
@@ -161,15 +157,20 @@ func (this *Scrollbar) handleKeyDown (key input.Key, numpad bool) bool {
if modifiers.Alt {
this.SetValue(0)
} else {
this.SetValue(this.Value() - increment)
this.scrollBy(this.StepSize())
}
return true
case input.KeyDown, input.KeyRight:
if modifiers.Alt {
this.SetValue(1)
} else {
this.SetValue(this.Value() + increment)
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)
@@ -229,11 +230,22 @@ func (this *Scrollbar) handleButtonUp (button input.Button) bool {
func (this *Scrollbar) handleMouseMove () bool {
if !this.dragging { return false }
this.drag()
return true
}
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))))

View File

@@ -73,7 +73,11 @@ func NewScrollContainer (sides ScrollSide) *ScrollContainer {
this.OnKeyUp(this.handleKeyUp)
this.SetRole(tomo.R("objects", "ScrollContainer"))
this.SetTag(sides.String(), true)
this.SetAttr(tomo.ALayout(layouts.NewGrid(true, false)(true, false)))
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
}
@@ -196,6 +200,7 @@ func (this *ScrollContainer) handleKeyDown (key input.Key, numpad bool) bool {
} else {
vector.Y -= this.PageSize().Y
}
this.scrollBy(vector)
return true
case input.KeyPageDown:
if modifiers.Shift {
@@ -203,6 +208,23 @@ func (this *ScrollContainer) handleKeyDown (key input.Key, numpad bool) bool {
} 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

View File

@@ -9,7 +9,7 @@ import "git.tebibyte.media/tomo/tomo/event"
// Slider is a control that selects a numeric value between 0 and 1.
type Slider struct {
tomo.ContainerBox
handle *SliderHandle
handle *sliderHandle
layout sliderLayout
dragging bool
dragOffset image.Point
@@ -21,16 +21,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 {
handle: &sliderHandle {
Box: tomo.NewBox(),
},
layout: sliderLayout {

View File

@@ -8,6 +8,7 @@ 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"
// Swatch displays a color, allowing the user to edit it by clicking on it.
type Swatch struct {
@@ -91,32 +92,44 @@ func (this *Swatch) Choose () {
committed := false
colorPicker := NewColorPicker(this.Value())
colorPicker.OnValueChange(func () {
this.userSetValue(colorPicker.Value())
})
hexInput := NewTextInput("TODO")
colorPicker := NewHSVAColorPicker(this.Value())
colorMemory := this.value
hexInput := NewTextInput("")
hexInput.SetFocused(true)
cancelButton := NewButton("Cancel")
cancelButton.SetIcon(tomo.IconDialogCancel)
okButton := NewButton("OK")
okButton.SetIcon(tomo.IconDialogOkay)
updateHexInput := func () {
nrgba := color.NRGBAModel.Convert(colorPicker.Value()).(color.NRGBA)
hexInput.SetValue(internal.FormatNRGBA(nrgba))
}
updateHexInput()
commit := func () {
committed = true
window.Close()
}
colorPicker.OnValueChange(func () {
this.userSetValue(colorPicker.Value())
updateHexInput()
})
hexInput.OnConfirm(commit)
hexInput.OnValueChange(func () {
nrgba := internal.ParseNRGBA(hexInput.Value())
this.userSetValue(nrgba)
colorPicker.SetValue(nrgba)
})
cancelButton.OnClick(func () {
window.Close()
})
okButton := NewButton("OK")
okButton.SetFocused(true)
okButton.SetIcon(tomo.IconDialogOkay)
okButton.OnClick(func () {
committed = true
window.Close()
})
okButton.OnClick(commit)
controlRow := NewInnerContainer (
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,
@@ -158,23 +171,23 @@ func (this *Swatch) userSetValue (value color.Color) {
}
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 }
if !isClickingKey(key) { return false }
this.Choose()
return true
}
func (this *Swatch) handleKeyUp (key input.Key, numberPad bool) bool {
if !isClickingKey(key) { return false }
return true
}
func (this *Swatch) handleButtonDown (button input.Button) bool {
if button != input.ButtonLeft { return false }
if !isClickingButton(button) { return false }
return true
}
func (this *Swatch) handleButtonUp (button input.Button) bool {
if button != input.ButtonLeft { return false }
if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) {
this.Choose()
}

View File

@@ -126,10 +126,10 @@ type tab struct {
func (this *tab) setActive (active bool) {
if active {
this.SetRole(tomo.R("objects", "Tab"))
this.SetTag("active", false)
this.SetTag("active", true)
} else {
this.SetRole(tomo.R("objects", "Tab"))
this.SetTag("active", true)
this.SetTag("active", false)
}
}

View File

@@ -31,15 +31,25 @@ func NewTextInput (text string) *TextInput {
return this
}
// SetText sets the text content of the input.
func (this *TextInput) SetText (text string) {
// SetValue sets the text content of the input.
func (this *TextInput) SetValue (text string) {
this.text = []rune(text)
this.TextBox.SetText(text)
}
// Value returns the text content of the input.
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 string(this.text)
return this.Value()
}
// OnConfirm specifies a function to be called when the user presses enter
@@ -66,60 +76,55 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
dot := this.Dot()
modifiers := this.Window().Modifiers()
word := modifiers.Control
sel := modifiers.Shift
changed := false
// TODO all dot control (movement, selection, etc) should be done in the
// backend. (editing should be done here, though)
defer func () {
this.Select(dot)
if changed {
this.SetText(string(this.text))
this.on.valueChange.Broadcast()
}
} ()
switch {
case key == input.KeyEnter:
case isConfirmationKey(key):
this.on.confirm.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)
}
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
case key == input.Key('a') && modifiers.Control:
dot.Start = 0
dot.End = len(this.text)
case key.Printable():
return true
case key.Printable() && !modifiers.Control:
this.text, dot = text.Type(this.text, dot, rune(key))
changed = true
return true
default:
return false
}
this.Select(dot)
if changed {
this.SetText(string(this.text))
this.on.valueChange.Broadcast()
}
return true
}
func (this *TextInput) handleKeyUp (key input.Key, numpad bool) bool {
return true
modifiers := this.Window().Modifiers()
switch {
case isConfirmationKey(key):
return true
case key == input.KeyBackspace:
return true
case key == input.KeyDelete:
return true
case key.Printable() && !modifiers.Control:
return true
default:
return false
}
}
func (this *TextInput) handleScroll (x, y float64) bool {
this.ScrollTo(this.ContentBounds().Min.Add(image.Pt(int(x), int(y))))
if x == 0 { return false }
this.ScrollTo(this.ContentBounds().Min.Sub(image.Pt(int(x), int(y))))
return true
}

View File

@@ -22,6 +22,6 @@ func NewTextView (text string) *TextView {
}
func (this *TextView) handleScroll (x, y float64) bool {
this.ScrollTo(this.ContentBounds().Min.Add(image.Pt(int(x), int(y))))
this.ScrollTo(this.ContentBounds().Min.Sub(image.Pt(int(x), int(y))))
return true
}