Compare commits

...

186 Commits

Author SHA1 Message Date
a3bb4098fb Pegboard defaults to FlowVertical layout 2024-09-12 18:11:08 -04:00
c30ac90577 NewRoot now correctly returns a Root struct 2024-09-12 17:24:37 -04:00
7628903e59 Add PageWrapper sub-component to Notebook 2024-09-12 15:44:11 -04:00
ce08487eff Make a distinction between notebook tabs and pages 2024-09-12 15:13:38 -04:00
6130b84a27 Change naming of Notebook methods 2024-09-12 15:10:29 -04:00
8b80520f8c Rename TabbedContainer to Notebook 2024-09-12 15:08:03 -04:00
38434db75c TextView tests its own compliance to tomo.Object 2024-09-12 15:06:20 -04:00
ffb6e9fb95 Document torn tag 2024-09-12 15:04:38 -04:00
12b855ba24 Document special root container in Menu 2024-09-12 15:01:14 -04:00
5b8e401e60 Fix spelling mistake in HSVAColorPicker documentation 2024-09-12 14:59:57 -04:00
7144900d31 Remove TODO in segment.go
It would have conflicted with nasin.NewApplicationWindow
2024-09-12 14:55:44 -04:00
51ce2a84f2 Update object code to use new containers 2024-09-12 14:54:26 -04:00
f1f71208f2 Fix role of Root 2024-09-12 14:54:14 -04:00
cdf23c9b13 Overhaul collection of containers 2024-09-12 14:07:54 -04:00
b8b80f8862 Update goutil 2024-09-12 03:20:09 -04:00
2224d2e73e Fix history overflowing 2024-09-12 02:56:56 -04:00
c2245ec304 Fix object code 2024-09-12 02:34:28 -04:00
dca3880a87 Update Tomo API 2024-09-12 02:25:01 -04:00
f0573bf551 Fix preview image alignment 2024-09-10 18:54:42 -04:00
ba2eeeba74 Fix grammar in README 2024-09-10 18:51:55 -04:00
b37d5398d8 Forgot to embed the image 2024-09-10 18:51:04 -04:00
f761da8cdc Add preview image 2024-09-10 18:50:32 -04:00
039e0da646 Combine internal packages into one internal package 2024-09-10 18:29:04 -04:00
6f2a31cd60 Replace HSV color functionality with that of goutil 2024-09-10 18:24:50 -04:00
177167510b Shift+Ctrl+Z now works for redo 2024-09-06 00:16:27 -04:00
3077249a13 TextInput updates state better when typing 2024-09-06 00:14:10 -04:00
38d950f44a Remove debug line 2024-09-06 00:13:15 -04:00
8b1b2e4199 Text input history is looking good 2024-09-06 00:12:24 -04:00
63ad06e214 Improvements to internal/history 2024-09-06 00:12:05 -04:00
ac1a952b40 Move color functionality into subpackage of internal 2024-09-05 22:46:58 -04:00
45a6634e73 Add (buggy) history support to TextInput 2024-09-05 20:11:57 -04:00
ead7d493d7 Add history mechanism 2024-09-05 20:11:40 -04:00
c1cf6edd8e Add SetOverflow to Label 2024-08-29 17:06:38 -04:00
2727972c30 File. Why not. 2024-08-27 13:38:35 -04:00
e48933385e You get an OnDotChange! Everypony gets an OnDotChange! 2024-08-25 18:55:43 -04:00
b9c4e3c003 Forgot to wrap the multiline input text haha 2024-08-25 02:49:08 -04:00
92e4eb970d Add multi-line text inputs 2024-08-25 02:47:23 -04:00
c7887c5ea4 Fix tag on right TabSpacer 2024-08-25 02:37:39 -04:00
30d4e208b1 Document all tags and named sub-components
Closes #9
2024-08-25 02:36:05 -04:00
a688e2dc24 README.md tweaks 2024-08-25 01:59:30 -04:00
e1cef9bb37 Improve README.md 2024-08-25 01:58:03 -04:00
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
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
85fbe9c996 Update code for objects 2024-07-25 12:58:38 -04:00
25a59d888c Update Tomo API to v0.41.0 2024-07-25 12:55:03 -04:00
6ca6771fc6 Update code for layouts, objects 2024-07-21 11:48:28 -04:00
9077015db6 Update Tomo API 2024-07-21 11:48:06 -04:00
1125d98b3d Change OnEnter to OnConfirm
Full remediation of #6
2024-06-27 14:09:58 -04:00
638fc61d83 Make value getters/setters more consistent
See #6
2024-06-27 14:01:14 -04:00
d0ee6c432c Add OnEnter to Swatch and ColorPicker 2024-06-26 11:20:53 -04:00
b9f980e7fd Fix doc comment on MenuItem 2024-06-25 02:33:36 -04:00
b7d1a0abdd Add OnEnter for Slider 2024-06-25 02:33:13 -04:00
a38cee8437 Fix RGBAToHSVA 2024-06-25 02:32:00 -04:00
48bfa05452 Color editing popups use swatch label for title 2024-06-22 18:48:54 -04:00
e8a3a376ea Introduce new layouts.Grid construct 2024-06-22 18:44:26 -04:00
ae1e62c1f2 Add swatch 2024-06-22 15:44:37 -04:00
0b7e5392f4 Add color picker 2024-06-22 15:44:24 -04:00
1efb946953 Fix issues with grid layout 2024-06-22 15:38:52 -04:00
1e8df2392d Add tabs 2024-06-22 15:38:14 -04:00
83dca60257 Fix checkboxes not initializing to desired value 2024-06-22 15:37:52 -04:00
5b60717b8f Add an HSVA color implementation 2024-06-22 15:37:20 -04:00
d01d39569b Sliders work properly when initialized to 0.0 2024-06-20 17:48:41 -04:00
55637e36db Lables come vertically aligned to the middle 2024-06-20 17:47:51 -04:00
e62afcd667 Fix calendar prev/forward month buttons 2024-06-19 00:38:54 -04:00
f778ef5c95 Add calendar widget 2024-06-18 19:37:37 -04:00
c06f10c193 Fix flow layout getting stuck in a loop 2024-06-15 18:12:08 -04:00
23fb28ce5c Some internal layouts return recommended sizes 2024-06-15 09:27:35 -04:00
3533ce3726 Row/Column uses correct axis for determining free space 2024-06-15 07:58:51 -04:00
6d157eb9af Fix Column layout not allocating space to expanding boxes 2024-06-15 07:54:49 -04:00
da346f2f12 Row, Column have recommended size support 2024-06-12 03:35:38 -04:00
71a41d390f Row, Column handle expansion properly 2024-06-12 03:32:32 -04:00
9ce7f8b8f3 Contract layouts are now based on new Row and Column layouts 2024-06-12 03:15:38 -04:00
1596d54834 Fix up objects code 2024-06-11 17:17:11 -04:00
95d3dc3288 Add placeholder methods for recommended sizes 2024-06-11 17:12:18 -04:00
1069ae6455 Removed cut layout
Not a very good implementation
2024-06-11 16:58:38 -04:00
5c8358fc4a Add blank vars to ensure layouts satisfy tomo.Layout 2024-06-11 16:46:04 -04:00
6a8aaca18d Rename layouts.go -> contract.go 2024-06-11 16:44:15 -04:00
460733c8f3 Update code for objects 2024-06-11 16:40:35 -04:00
5d2a366a62 Update Tomo API 2024-06-11 16:40:06 -04:00
2c7c77d8da Add menus 2024-06-07 01:59:29 -04:00
8139d871cc Text-less buttons place their icon in the middle 2024-06-07 01:58:29 -04:00
bb320dfcc9 Update Tomo API 2024-06-07 01:58:09 -04:00
2ab920eb26 Store role in Boxes 2024-06-03 21:13:18 -04:00
d8ae20d739 Update Tomo API 2024-06-03 21:10:20 -04:00
06561bb671 Scrollbar, ScrollContainer use ContentObject now 2024-05-27 16:28:48 -04:00
a71d81af48 Checkbox uses CheckboxUnchecked icon when unchecked 2024-05-27 15:59:24 -04:00
bd9dbb762d Update Tomo API 2024-05-27 15:22:18 -04:00
6389556199 Updated all objects to new API 2024-05-26 17:21:58 -04:00
06d99b2790 Update Tomo API 2024-05-26 17:13:40 -04:00
6ab689b5c1 Renamed input.go to textinput.go 2024-05-20 02:23:07 -04:00
6497da69ed LabelCheckbox no longer expands vertically
It should in theory valign to middle this way, but grid layout
suffers from #1
2024-05-18 14:18:02 -04:00
34794f4c77 Make the dialog API more normal
It was very nice but inconsistent with literally everything else,
and never in my life have I seen someone else make a constructor
that way.
2024-05-18 14:16:57 -04:00
25625e9a99 Add dialog boxes 2024-05-18 13:25:06 -04:00
4cea0aa0bd Flow layout no longer leaves last box behind 2024-05-17 14:43:06 -04:00
1cb9e8091e Combine Row/Column layouts into Contract layout 2024-05-17 03:56:49 -04:00
68d49e1b02 Add flow layout 2024-05-17 03:51:24 -04:00
f025ec3d8a Slider can now be controlled by scrolling
If ya nasty
2024-05-17 01:22:47 -04:00
6fad52b335 Slider broadcasts slide event when manipulated with key presses 2024-05-17 01:09:59 -04:00
8134069e1f Add Value and SetValue to ScrollContainer 2024-05-17 01:08:39 -04:00
dca1eaefb5 Add SunkenContainer 2024-05-13 19:48:29 -04:00
87e81c00d3 Scrollbar and ScrollContainer use ScrollTo correctly 2024-05-13 19:45:18 -04:00
224ca25000 NewHeading returns a Heading as expected 2024-05-07 20:21:45 -04:00
43 changed files with 3623 additions and 881 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

@ -1,6 +1,29 @@
# objects
![Some of the objects in this package](assets/preview.png)
[![Go Reference](https://pkg.go.dev/badge/git.tebibyte.media/tomo/objects.svg)](https://pkg.go.dev/git.tebibyte.media/tomo/objects)
Objects contains a standard collection of re-usable objects. All objects in this
module visually conform to whatever the theme is set to.
Objects contains a standard collection of re-usable objects. It should also be
viewed as a reference for how to create custom objects in Tomo.
## Styling
All objects in this module have roles of the form:
```
objects.TypeName
```
Where `TypeName` is the exact Go type name of the object in question. Objects
may also have different tags to indicate variations, states, etc. If applicable,
they are listed and described in the doc comment for the object's type. More
complex objects may have sub-components that are not accessible from the API.
These are listed alongside the tags.
## Setting Attributes
It is generally not recommended to set attributes on these objects. However, if
you must, they can be set by obtaining the object's underlying box through the
`GetBox` method. Be aware that the exact type of box that is returned here is
not part of the API, and may change unexpectedly even after v1.0. This caveat
also applies to boxes/sub-components making up the internal composition of the
objects.

93
abstractcontainer.go Normal file
View File

@ -0,0 +1,93 @@
package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/event"
type abstractContainer struct {
box tomo.ContainerBox
}
func (this *abstractContainer) init (layout tomo.Layout, children ...tomo.Object) {
this.box = tomo.NewContainerBox()
this.SetLayout(layout)
for _, child := range children {
this.Add(child)
}
}
// GetBox returns the underlying box.
func (this *abstractContainer) 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 *abstractContainer) 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 *abstractContainer) 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 *abstractContainer) OnContentBoundsChange (callback func ()) event.Cookie {
return this.box.OnContentBoundsChange(callback)
}
// SetLayout sets the layout of the container.
func (this *abstractContainer) SetLayout (layout tomo.Layout) {
if layout == nil {
this.box.UnsetAttr(tomo.AttrKindLayout)
} else {
this.box.SetAttr(tomo.ALayout(layout))
}
}
// SetAlign sets the X and Y alignment of the container.
func (this *abstractContainer) SetAlign (x, y tomo.Align) {
this.box.SetAttr(tomo.AAlign(x, y))
}
// SetOverflow sets the X and Y overflow of the container.
func (this *abstractContainer) 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 *abstractContainer) Add (object tomo.Object) {
this.box.Add(object)
}
// Remove removes a child object, if it is a child of this container.
func (this *abstractContainer) 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 *abstractContainer) Insert (child tomo.Object, before tomo.Object) {
this.box.Insert(child, before)
}
// Clear removes all child objects.
func (this *abstractContainer) Clear () {
this.box.Clear()
}
// Len returns hte amount of child objects.
func (this *abstractContainer) Len () int {
return this.box.Len()
}
// At returns the child object at the specified index.
func (this *abstractContainer) At (index int) tomo.Object {
return this.box.At(index)
}

BIN
assets/preview.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -1,18 +1,22 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var buttonLayout = layouts.NewGrid([]bool { true }, []bool { true })
var iconButtonLayout = layouts.NewGrid([]bool { false }, []bool { true })
var bothButtonLayout = layouts.NewGrid([]bool { false, true }, []bool { true })
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.
//
// Tags:
// - [icon] The button has an icon.
type Button struct {
tomo.ContainerBox
box tomo.ContainerBox
label *Label
icon *Icon
@ -25,35 +29,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),
}
theme.Apply(box, theme.R("objects", "Button", ""))
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
box.SetLayout(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.CaptureDND(true)
box.CaptureMouse(true)
box.CaptureScroll(true)
box.CaptureKeyboard(true)
box.OnMouseUp(box.handleMouseUp)
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.Delete(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()
@ -61,17 +75,18 @@ func (this *Button) SetText (text string) {
// SetIcon sets an icon for this button. Setting the icon to IconUnknown will
// remove it.
func (this *Button) SetIcon (id theme.Icon) {
if this.icon != nil { this.Delete(this.icon) }
func (this *Button) SetIcon (id tomo.Icon) {
if this.icon != nil { this.box.Remove(this.icon) }
var icon *Icon; if id != theme.IconUnknown {
icon = NewIcon(id, theme.IconSizeSmall)
var icon *Icon; if id != tomo.IconUnknown {
icon = NewIcon(id, tomo.IconSizeSmall)
}
this.icon = icon
if this.icon != nil {
this.Insert(this.icon, this.label)
this.box.Insert(this.icon, this.label)
}
this.box.SetTag("icon", this.icon != nil)
this.applyLayout()
}
@ -82,22 +97,34 @@ func (this *Button) OnClick (callback func ()) event.Cookie {
func (this *Button) applyLayout () {
if this.labelActive && this.icon == nil {
this.SetLayout(buttonLayout)
this.box.SetAttr(tomo.ALayout(buttonLayout))
} else if !this.labelActive && this.icon != nil {
this.SetLayout(iconButtonLayout)
this.box.SetAttr(tomo.ALayout(iconButtonLayout))
} else {
this.SetLayout(bothButtonLayout)
this.box.SetAttr(tomo.ALayout(bothButtonLayout))
}
}
func (this *Button) handleKeyUp (key input.Key, numberPad bool) {
if key != input.KeyEnter && key != input.Key(' ') { return }
func (this *Button) handleKeyDown (key input.Key, numberPad bool) bool {
if !isClickingKey(key) { return false }
this.on.click.Broadcast()
return true
}
func (this *Button) handleMouseUp (button input.Button) {
if button != input.ButtonLeft { return }
if this.MousePosition().In(this.Bounds()) {
func (this *Button) handleKeyUp (key input.Key, numberPad bool) bool {
if !isClickingKey(key) { return false }
return true
}
func (this *Button) handleButtonDown (button input.Button) bool {
if !isClickingButton(button) { return false }
return true
}
func (this *Button) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.on.click.Broadcast()
}
return true
}

190
calendar.go Normal file
View File

@ -0,0 +1,190 @@
package objects
import "fmt"
import "time"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
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.
//
// Sub-components:
// - CalendarGrid organizes the days into a grid.
// - CalendarWeekdayHeader appears at the top of each grid column, and shows
// the day of the week that column represents.
// - CalendarDay appears within the grid for each day of the current month.
//
// CalendarDay tags:
// - [weekend] The day is a weekend.
// - [weekday] The day is a weekday.
type Calendar struct {
box tomo.ContainerBox
grid tomo.ContainerBox
time time.Time
monthLabel *Label
on struct {
valueChange event.FuncBroadcaster
}
}
// NewCalendar creates a new calendar with the specified date.
func NewCalendar (tm time.Time) *Calendar {
calendar := &Calendar {
box: tomo.NewContainerBox(),
time: tm,
}
calendar.box.SetRole(tomo.R("objects", "Calendar"))
calendar.box.SetAttr(tomo.ALayout(layouts.ContractVertical))
prevButton := NewButton("")
prevButton.SetIcon(tomo.IconGoPrevious)
prevButton.OnClick(func () {
calendar.prevMonth()
calendar.on.valueChange.Broadcast()
})
nextButton := NewButton("")
nextButton.SetIcon(tomo.IconGoNext)
nextButton.OnClick(func () {
calendar.nextMonth()
calendar.on.valueChange.Broadcast()
})
calendar.monthLabel = NewLabel("")
calendar.monthLabel.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.box.Add(NewContainer (
layouts.Row { false, true, false },
prevButton, calendar.monthLabel, nextButton))
calendar.box.Add(calendar.grid)
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
}
// SetValue sets the date the calendar will display.
func (this *Calendar) SetValue (tm time.Time) {
if this.time == tm { return }
this.time = tm
this.refresh()
}
// OnValueChange sets a function to be called when the user changes the date on
// the calendar.
func (this *Calendar) OnValueChange (callback func ()) {
this.on.valueChange.Connect(callback)
}
func (this *Calendar) prevMonth () {
this.time = firstOfMonth(this.time.Add(24 * time.Hour * -20))
this.refresh()
}
func (this *Calendar) nextMonth () {
this.time = firstOfMonth(this.time.Add(24 * time.Hour * 40))
this.refresh()
}
var weekdayAbbreviations = []string {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
}
func (this *Calendar) refresh () {
this.monthLabel.SetText(this.time.Format("2006 January"))
this.grid.Clear()
for _, day := range weekdayAbbreviations {
dayLabel := tomo.NewTextBox()
dayLabel.SetRole(tomo.R("objects", "CalendarWeekdayHeader"))
dayLabel.SetText(day)
dayLabel.SetAttr(tomo.AAlign(tomo.AlignMiddle, tomo.AlignMiddle))
this.grid.Add(dayLabel)
}
dayIter := 0 - int(firstOfMonth(this.time).Weekday())
if dayIter <= -6 {
dayIter = 1
}
weekday := 0
totalDays := daysInMonth(this.time)
for ; dayIter <= totalDays; dayIter ++ {
weekday = (weekday + 1) % 7
if dayIter > 0 {
day := tomo.NewTextBox()
day.SetText(fmt.Sprint(dayIter))
if weekday == 1 || weekday == 0 {
day.SetRole(tomo.R("objects", "CalendarDay"))
day.SetTag("weekend", true)
} else {
day.SetRole(tomo.R("objects", "CalendarDay"))
day.SetTag("weekday", true)
}
this.grid.Add(day)
} else {
this.grid.Add(tomo.NewBox())
}
}
}
func (this *Calendar) handleScroll (x, y float64) bool {
if y < 0 {
this.prevMonth()
} else {
this.nextMonth()
}
return true
}
func firstOfMonth (tm time.Time) time.Time {
return time.Date(tm.Year(), tm.Month(), 0, 0, 0, 0, 0, time.Local)
}
func daysInMonth (tm time.Time) (days int) {
year := tm.Year()
month := tm.Month()
switch month {
case 1: days = 31
case 2:
// betcha didn't know this about leap years
if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) {
days = 29
} else {
days = 28
}
case 3: days = 31
case 4: days = 30
case 5: days = 31
case 6: days = 30
case 7: days = 31
case 8: days = 31
case 9: days = 30
case 10: days = 31
case 11: days = 30
case 12: days = 31
}
return
}
func canonMonth (tm time.Time) int {
return int(tm.Month() - 1) + tm.Year() * 12
}

View File

@ -1,13 +1,18 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(Checkbox)
// Checkbox is a control that can be toggled.
//
// Tags:
// - [checked] The checkbox's value is true.
// - [unchecked] The checkbox's value is false.
type Checkbox struct {
tomo.Box
box tomo.Box
value bool
on struct {
valueChange event.FuncBroadcaster
@ -16,33 +21,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(),
}
theme.Apply(box, theme.R("objects", "Checkbox", ""))
box.SetValue(false)
checkbox.box.SetRole(tomo.R("objects", "Checkbox"))
checkbox.SetValue(value)
box.OnMouseUp(box.handleMouseUp)
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
}
// SetValue sets the value of the checkbox.
func (this *Checkbox) SetValue (value bool) {
this.value = value
if this.value {
// TODO perhaps have IconStatusOkay/Cancel in actions, and have
// a status icon for checked checkboxes.
this.SetTexture(theme.IconStatusOkay.Texture(theme.IconSizeSmall))
} else {
this.SetTexture(nil)
}
// GetBox returns the underlying box.
func (this *Checkbox) GetBox () tomo.Box {
return this.box
}
// Toggle toggles the value of the checkbox between true and false.
func (this *Checkbox) Toggle () {
this.SetValue(!this.Value())
// 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.
@ -50,20 +52,45 @@ func (this *Checkbox) Value () bool {
return this.value
}
// OnValueChange specifies a function to be called when the checkbox's value
// changes.
// SetValue sets the value of the checkbox.
func (this *Checkbox) SetValue (value bool) {
this.value = value
// the theme shall decide what checked and unchecked states look like
this.box.SetTag("checked", value)
this.box.SetTag("unchecked", !value)
}
// Toggle toggles the value of the checkbox between true and false.
func (this *Checkbox) Toggle () {
this.SetValue(!this.Value())
}
// OnValueChange specifies a function to be called when the user checks or
// unchecks the checkbox.
func (this *Checkbox) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
func (this *Checkbox) handleKeyUp (key input.Key, numberPad bool) {
if key != input.KeyEnter && key != input.Key(' ') { return }
func (this *Checkbox) handleKeyDown (key input.Key, numberPad bool) bool {
if !isClickingKey(key) { return false }
this.Toggle()
return true
}
func (this *Checkbox) handleMouseUp (button input.Button) {
if button != input.ButtonLeft { return }
if this.MousePosition().In(this.Bounds()) {
func (this *Checkbox) handleKeyUp (key input.Key, numberPad bool) bool {
if !isClickingKey(key) { return false}
return true
}
func (this *Checkbox) handleButtonDown (button input.Button) bool {
if !isClickingButton(button) { return false }
return true
}
func (this *Checkbox) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.Toggle()
}
return true
}

178
colorpicker.go Normal file
View File

@ -0,0 +1,178 @@
package objects
import "image/color"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/objects/layouts"
import "git.tebibyte.media/sashakoshka/goutil/image/color"
var _ tomo.Object = new(HSVAColorPicker)
// HSVAColorPicker allows the user to pick a color by controlling its HSVA
// parameters.
//
// Sub-components:
// - ColorPickerMap is a rectangular control where the X axis controls
// saturation and the Y axis controls value.
type HSVAColorPicker struct {
box tomo.ContainerBox
value ucolor.HSVA
pickerMap *hsvaColorPickerMap
hueSlider *Slider
alphaSlider *Slider
on struct {
valueChange event.FuncBroadcaster
}
}
// NewHSVAColorPicker creates a new color picker with the specified color.
func NewHSVAColorPicker (value color.Color) *HSVAColorPicker {
picker := &HSVAColorPicker {
box: tomo.NewContainerBox(),
}
picker.box.SetRole(tomo.R("objects", "ColorPicker"))
picker.box.SetAttr(tomo.ALayout(layouts.Row { true, false, false }))
picker.pickerMap = newHsvaColorPickerMap(picker)
picker.box.Add(picker.pickerMap)
picker.hueSlider = NewVerticalSlider(0.0)
picker.box.Add(picker.hueSlider)
picker.hueSlider.OnValueChange(func () {
picker.value.H = picker.hueSlider.Value()
picker.on.valueChange.Broadcast()
picker.pickerMap.Invalidate()
})
picker.alphaSlider = NewVerticalSlider(0.0)
picker.box.Add(picker.alphaSlider)
picker.alphaSlider.OnValueChange(func () {
picker.value.A = uint16(picker.alphaSlider.Value() * 0xFFFF)
picker.on.valueChange.Broadcast()
picker.pickerMap.Invalidate()
})
if value == nil { value = color.Transparent }
picker.SetValue(value)
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
}
// SetValue sets the color of the picker.
func (this *HSVAColorPicker) SetValue (value color.Color) {
if value == nil { value = color.Transparent }
this.value = ucolor.HSVAModel.Convert(value).(ucolor.HSVA)
this.hueSlider.SetValue(this.value.H)
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 *HSVAColorPicker) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
// RGBA satisfies the color.Color interface
func (this *HSVAColorPicker) RGBA () (r, g, b, a uint32) {
return this.value.RGBA()
}
type hsvaColorPickerMap struct {
tomo.CanvasBox
dragging bool
parent *HSVAColorPicker
}
func newHsvaColorPickerMap (parent *HSVAColorPicker) *hsvaColorPickerMap {
picker := &hsvaColorPickerMap {
CanvasBox: tomo.NewCanvasBox(),
parent: parent,
}
picker.SetDrawer(picker)
picker.SetRole(tomo.R("objects", "ColorPickerMap"))
picker.OnButtonUp(picker.handleButtonUp)
picker.OnButtonDown(picker.handleButtonDown)
picker.OnMouseMove(picker.handleMouseMove)
return picker
}
func (this *hsvaColorPickerMap) handleButtonDown (button input.Button) bool {
if button != input.ButtonLeft { return false }
this.dragging = true
this.drag()
return true
}
func (this *hsvaColorPickerMap) handleButtonUp (button input.Button) bool {
if button != input.ButtonLeft { return false }
this.dragging = false
return true
}
func (this *hsvaColorPickerMap) handleMouseMove () bool {
if !this.dragging { return false }
this.drag()
return true
}
func (this *hsvaColorPickerMap) drag () {
pointer := this.Window().MousePosition()
bounds := this.InnerBounds()
this.parent.value.S = float64(pointer.X - bounds.Min.X) / float64(bounds.Dx())
this.parent.value.V = 1 - float64(pointer.Y - bounds.Min.Y) / float64(bounds.Dy())
this.parent.value = this.parent.value.Canon()
this.parent.on.valueChange.Broadcast()
this.Invalidate()
}
func (this *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 ++ {
xx := x - bounds.Min.X
yy := y - bounds.Min.Y
pixel := ucolor.HSVA {
H: this.parent.value.H,
S: float64(xx) / float64(bounds.Dx()),
V: 1 - float64(yy) / float64(bounds.Dy()),
A: 0xFFFF,
}
sPos := int( this.parent.value.S * float64(bounds.Dx()))
vPos := int((1 - this.parent.value.V) * float64(bounds.Dy()))
sDist := sPos - xx
vDist := vPos - yy
crosshair :=
(sDist == 0 || vDist == 0) &&
-8 < sDist && sDist < 8 &&
-8 < vDist && vDist < 8
if crosshair {
pixel.S = 1 - pixel.S
pixel.V = 1 - pixel.V
}
can.Set(x, y, pixel)
}}
}

View File

@ -1,39 +1,21 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
// Container is an object that can contain other objects. It can be used as a
// primitive for building more complex layouts. It has two variants: an outer
// container, and an inner container. The outer container has padding around
// its edges, whereas the inner container does not. The container will have a
// corresponding object role variation of either "outer" or "inner".
var _ tomo.ContentObject = new(Container)
// Container is an object that can contain other objects. It is plain looking,
// and is intended to be used within other containers as a primitive for
// building more complex layouts.
type Container struct {
tomo.ContainerBox
abstractContainer
}
func newContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := &Container {
ContainerBox: tomo.NewContainerBox(),
}
this.SetLayout(layout)
for _, child := range children {
this.Add(child)
}
// NewContainer creates a new container.
func NewContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := &Container { }
this.init(layout, children...)
this.box.SetRole(tomo.R("objects", "Container"))
this.box.SetTag("outer", true)
return this
}
// NewOuterContainer creates a new container that has padding around it.
func NewOuterContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := newContainer(layout, children...)
theme.Apply(this, theme.R("objects", "Container", "outer"))
return this
}
// NewInnerContainer creates a new container that has no padding around it.
func NewInnerContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := newContainer(layout, children...)
theme.Apply(this, theme.R("objects", "Container", "inner"))
return this
}

100
dialog.go Normal file
View File

@ -0,0 +1,100 @@
package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
// DialogKind defines the semantic role of a dialog window.
type DialogKind int; const (
DialogInformation DialogKind = iota
DialogQuestion
DialogWarning
DialogError
)
// Dialog is a modal dialog window.
type Dialog struct {
tomo.Window
}
type clickable interface {
OnClick (func ()) event.Cookie
}
// NewDialog creates a new dialog window given a dialog kind.
func NewDialog (kind DialogKind, parent tomo.Window, title, message string, options ...tomo.Object) (*Dialog, error) {
if title == "" {
switch kind {
case DialogInformation: title = "Information"
case DialogQuestion: title = "Question"
case DialogWarning: title = "Warning"
case DialogError: title = "Error"
}
}
dialog := &Dialog { }
if parent == nil {
window, err := tomo.NewWindow(tomo.WindowKindNormal, image.Rectangle { })
if err != nil { return nil, err }
dialog.Window = window
} else {
window, err := parent.NewChild(tomo.WindowKindModal, image.Rectangle { })
if err != nil { return nil, err }
dialog.Window = window
}
var iconId tomo.Icon
switch kind {
case DialogInformation: iconId = tomo.IconDialogInformation
case DialogQuestion: iconId = tomo.IconDialogQuestion
case DialogWarning: iconId = tomo.IconDialogWarning
case DialogError: iconId = tomo.IconDialogError
}
dialog.SetTitle(title)
dialog.SetIcon(iconId)
icon := NewIcon(iconId, tomo.IconSizeLarge)
messageText := NewLabel(message)
messageText.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
for _, option := range options {
if option, ok := option.(clickable); ok {
option.OnClick(func () {
dialog.Close()
})
}
}
dialog.SetRoot(NewRoot (
layouts.Column { true, false },
NewContentSegment(layouts.ContractHorizontal, icon, messageText),
NewOptionSegment(nil, options...)))
return dialog, nil
}
// NewDialogOk creates a new dialog window with an OK option.
func NewDialogOk (kind DialogKind, parent tomo.Window, title, message string, onOk func ()) (*Dialog, error) {
okButton := NewButton("OK")
okButton.SetIcon(tomo.IconDialogOkay)
okButton.OnClick(func () {
if onOk != nil { onOk() }
})
okButton.SetFocused(true)
return NewDialog(kind, parent, title, message, okButton)
}
// NewDialogOkCancel creates a new dialog window with OK and Cancel options.
func NewDialogOkCancel (kind DialogKind, parent tomo.Window, title, message string, onOk func ()) (*Dialog, error) {
cancelButton := NewButton("Cancel")
cancelButton.SetIcon(tomo.IconDialogCancel)
okButton := NewButton("OK")
okButton.SetIcon(tomo.IconDialogOkay)
okButton.OnClick(func () {
if onOk != nil { onOk() }
})
okButton.SetFocused(true)
return NewDialog(kind, parent, title, message, cancelButton, okButton)
}

128
dropdown.go Normal file
View File

@ -0,0 +1,128 @@
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"
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 {
box 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 {
box: tomo.NewContainerBox(),
label: NewLabel(""),
}
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.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 {
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.box.Window().MousePosition().In(this.box.Bounds()) {
this.Choose()
}
return true
}

129
file.go Normal file
View File

@ -0,0 +1,129 @@
package objects
import "time"
import "unicode"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/data"
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(File)
// File is a representation of a file or directory.
type File struct {
box tomo.ContainerBox
label *Label
icon *MimeIcon
mime data.Mime
lastClick time.Time
on struct {
doubleClick event.FuncBroadcaster
}
}
// NewFile creates a new file icon with the given name and MIME type.
func NewFile (name string, mime data.Mime) *File {
file := &File {
box: tomo.NewContainerBox(),
label: NewLabel(""),
icon: NewMimeIcon(mime, tomo.IconSizeLarge),
}
file.box.SetRole(tomo.R("objects", "File"))
file.box.SetAttr(tomo.ALayout(layouts.ContractVertical))
file.box.Add(file.icon)
file.box.Add(file.label)
file.box.SetAttr(tomo.AAlign(tomo.AlignMiddle, tomo.AlignStart))
file.label.SetAlign(tomo.AlignMiddle, tomo.AlignStart)
// file.label.SetOverflow(false, true)
// file.label.SetWrap(true)
file.SetType(mime)
file.SetName(name)
file.box.SetInputMask(true)
file.box.SetFocusable(true)
file.box.OnButtonDown(file.handleButtonDown)
file.box.OnButtonUp(file.handleButtonUp)
file.box.OnKeyDown(file.handleKeyDown)
file.box.OnKeyUp(file.handleKeyUp)
return file
}
// GetBox returns the underlying box.
func (this *File) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this file has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *File) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// SetName sets the text of the file's label.
func (this *File) SetName (text string) {
this.label.SetText(truncateText(text, 16))
}
// SetType sets the MIME type of the file.
func (this *File) SetType (mime data.Mime) {
this.mime = mime
this.icon.SetIcon(mime, tomo.IconSizeLarge)
}
// OnDoubleClick specifies a function to be called when the file is
// double-clicked.
func (this *File) OnDoubleClick (callback func ()) event.Cookie {
return this.on.doubleClick.Connect(callback)
}
func (this *File) handleKeyDown (key input.Key, numberPad bool) bool {
if key != input.KeyEnter && key != input.Key(' ') { return false }
return true
}
func (this *File) handleKeyUp (key input.Key, numberPad bool) bool {
if key != input.KeyEnter && key != input.Key(' ') { return false }
this.on.doubleClick.Broadcast()
return true
}
func (this *File) handleButtonDown (button input.Button) bool {
if button != input.ButtonLeft { return false }
return true
}
func (this *File) handleButtonUp (button input.Button) bool {
if button != input.ButtonLeft { return false }
if this.box.Window().MousePosition().In(this.box.Bounds()) {
// TODO double click delay should be configurable
if time.Since(this.lastClick) < time.Second {
this.on.doubleClick.Broadcast()
}
this.lastClick = time.Now()
}
return true
}
func truncateText (text string, max int) string {
lastChar := -1
len := 0
for index, char := range text {
if !unicode.IsSpace(char) {
lastChar = index
}
len ++
if len >= max {
if lastChar != -1 {
return text[:lastChar] + "..."
}
break
}
}
return text
}

9
go.mod
View File

@ -1,7 +1,8 @@
module git.tebibyte.media/tomo/objects
go 1.20
go 1.21.0
require git.tebibyte.media/tomo/tomo v0.31.0
require golang.org/x/image v0.11.0 // indirect
require (
git.tebibyte.media/sashakoshka/goutil v0.3.1
git.tebibyte.media/tomo/tomo v0.48.0
)

39
go.sum
View File

@ -1,35 +1,4 @@
git.tebibyte.media/tomo/tomo v0.31.0 h1:LHPpj3AWycochnC8F441aaRNS6Tq6w6WnBrp/LGjyhM=
git.tebibyte.media/tomo/tomo v0.31.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.11.0 h1:ds2RoQvBvYTiJkwpSFDwCcDFNX7DqjL2WsUgTNk0Ooo=
golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
git.tebibyte.media/sashakoshka/goutil v0.3.1 h1:zvAMKS+aea96q6oTttCWfNLXqOHisI3IKAwX6BWKfY0=
git.tebibyte.media/sashakoshka/goutil v0.3.1/go.mod h1:Yo/M2sbi9IbzZCFsEj8/Fg7sNwHkDaJ6saTHOha+Dow=
git.tebibyte.media/tomo/tomo v0.48.0 h1:AE21ElHwUSPsX82ZWCnoNxJFi9Oswyd3dPDPMbxTueQ=
git.tebibyte.media/tomo/tomo v0.48.0/go.mod h1:WrtilgKB1y8O2Yu7X4mYcRiqOlPR8NuUnoA/ynkQWrs=

View File

@ -2,22 +2,72 @@ package objects
import "fmt"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
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.
// most subtle.
//
// Tags:
// - [0] The heading has a level of 0 (most prominent).
// - [1] The heading has a level of 1.
// - [2] The heading has a level of 2 (least prominent).
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) *Label {
func NewHeading (level int, text string) *Heading {
if level < 0 { level = 0 }
if level > 2 { level = 2 }
this := &Label { TextBox: tomo.NewTextBox() }
theme.Apply(this, theme.R("objects", "Heading", fmt.Sprint(level)))
this := &Heading { box: tomo.NewTextBox() }
this.box.SetRole(tomo.R("objects", "Heading"))
this.box.SetTag(fmt.Sprint(level), true)
this.SetText(text)
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.box.SetTag("menu", true)
return heading
}

63
icon.go
View File

@ -1,42 +1,65 @@
package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/data"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/canvas"
var _ tomo.Object = new(Icon)
// Icon displays a single icon.
//
// Tags:
// - [large] The icon is large sized.
// - [medium] The icon is medium sized.
// - [small] The icon is small sized.
type Icon struct {
tomo.Box
box tomo.Box
icon tomo.Icon
size tomo.IconSize
}
func iconSizeString (size tomo.IconSize) string {
switch size {
case tomo.IconSizeLarge: return "large"
case tomo.IconSizeMedium: return "medium"
default: return "small"
}
}
// NewIcon creates a new icon from an icon ID.
func NewIcon (id theme.Icon, size theme.IconSize) *Icon {
func NewIcon (icon tomo.Icon, size tomo.IconSize) *Icon {
this := &Icon {
Box: tomo.NewBox(),
box: tomo.NewBox(),
}
theme.Apply(this, theme.R("objects", "Icon", size.String()))
this.SetTexture(id.Texture(size))
this.box.SetRole(tomo.R("objects", "Icon"))
this.SetIcon(icon, size)
this.box.OnIconSetChange(this.handleIconSetChange)
return this
}
// NewMimeIcon creates a new icon from a MIME type.
func NewMimeIcon (mime data.Mime, size theme.IconSize) *Icon {
this := &Icon {
Box: tomo.NewBox(),
}
theme.Apply(this, theme.R("objects", "Icon", size.String()))
this.SetTexture(theme.MimeIcon(mime, size))
return this
// GetBox returns the underlying box.
func (this *Icon) GetBox () tomo.Box {
return this.box
}
func (this *Icon) SetTexture (texture canvas.Texture) {
this.Box.SetTexture(texture)
// 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.box.SetAttr(tomo.ATexture(texture))
this.box.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
if texture == nil {
this.SetMinimumSize(image.Pt(0, 0))
this.box.SetAttr(tomo.AMinimumSize(0, 0))
} else {
bounds := texture.Bounds()
this.SetMinimumSize(bounds.Max.Sub(bounds.Min))
this.box.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
}
}

107
input.go
View File

@ -1,110 +1,19 @@
package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/text"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
// TextInput is a single-line editable text box.
type TextInput struct {
tomo.TextBox
text []rune
on struct {
enter event.FuncBroadcaster
edit event.FuncBroadcaster
}
func isClickingKey (key input.Key) bool {
return key == input.KeyEnter || key == input.Key(' ')
}
// NewTextInput creates a new text input containing the specified text.
func NewTextInput (text string) *TextInput {
this := &TextInput { TextBox: tomo.NewTextBox() }
theme.Apply(this, theme.R("objects", "TextInput", ""))
this.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
this.SetText(text)
this.SetFocusable(true)
this.SetSelectable(true)
this.SetOverflow(true, false)
this.OnKeyDown(this.handleKeyDown)
this.OnScroll(this.handleScroll)
return this
func isConfirmationKey (key input.Key) bool {
return key == input.KeyEnter
}
// SetText sets the text content of the input.
func (this *TextInput) SetText (text string) {
this.text = []rune(text)
this.TextBox.SetText(text)
func isClickingButton (button input.Button) bool {
return button == input.ButtonLeft
}
// Text returns the text content of the input.
func (this *TextInput) Text () string {
return string(this.text)
}
// OnEnter specifies a function to be called when the user presses enter within
// the text input.
func (this *TextInput) OnEnter (callback func ()) event.Cookie {
return this.on.enter.Connect(callback)
}
// OnEdit specifies a function to be called when the user edits the input text.
func (this *TextInput) OnEdit (callback func ()) event.Cookie {
return this.on.edit.Connect(callback)
}
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
dot := this.Dot()
modifiers := this.Modifiers()
word := modifiers.Control
sel := modifiers.Shift
changed := false
// TODO all this (except editing stuff) really should be moved into the
// backend
switch {
case key == input.KeyEnter:
this.on.enter.Broadcast()
case key == input.KeyHome || (modifiers.Alt && key == input.KeyLeft):
dot.End = 0
if !sel { dot.Start = dot.End }
case key == input.KeyEnd || (modifiers.Alt && key == input.KeyRight):
dot.End = len(this.text)
if !sel { dot.Start = dot.End }
case key == input.KeyLeft:
if sel {
dot = text.SelectLeft(this.text, dot, word)
} else {
dot = text.MoveLeft(this.text, dot, word)
}
case key == input.KeyRight:
if sel {
dot = text.SelectRight(this.text, dot, word)
} else {
dot = text.MoveRight(this.text, dot, word)
}
case key == input.KeyBackspace:
this.text, dot = text.Backspace(this.text, dot, word)
changed = true
case key == input.KeyDelete:
this.text, dot = text.Delete(this.text, dot, word)
changed = true
case key == input.Key('a') && modifiers.Control:
dot.Start = 0
dot.End = len(this.text)
case key.Printable():
this.text, dot = text.Type(this.text, dot, rune(key))
changed = true
}
this.Select(dot)
if changed {
this.SetText(string(this.text))
this.on.edit.Broadcast()
}
}
func (this *TextInput) handleScroll (x, y float64) {
this.ScrollTo(this.ContentBounds().Min.Add(image.Pt(int(x), int(y))))
func isMenuButton (button input.Button) bool {
return button == input.ButtonLeft
}

59
internal/color.go Normal file
View File

@ -0,0 +1,59 @@
package internal
import "fmt"
import "image/color"
// 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)
}

92
internal/history.go Normal file
View File

@ -0,0 +1,92 @@
package internal
import "time"
// History stores a stack of items, always keeping the bottom-most one. It must
// be created using the NewHistory constructor, otherwise it will be invalid.
type History[T comparable] struct {
max int
stack []T
topIndex int
topTime time.Time
}
// NewHistory creates a new History. The initial item will be on the bottom, and
// it will remain there until the History overflows and chooses the item after
// it to be the initial item.
func NewHistory[T comparable] (initial T, max int) *History[T] {
return &History[T] {
max: max,
stack: []T { initial },
}
}
// Top returns the most recent item.
func (this *History[T]) Top () T {
return this.stack[this.topIndex]
}
// Swap replaces the most recent item with another.
func (this *History[T]) Swap (item T) {
this.topTime = time.Now()
this.SwapSilently(item)
}
// SwapSilently replaces the most recent item with another without updating the
// time.
func (this *History[T]) SwapSilently (item T) {
this.stack[this.topIndex] = item
}
// Push pushes a new item onto the stack. If the stack overflows (becomes bigger
// than the specified max value), the initial item is removed and the one on top
// of it takes its place.
func (this *History[T]) Push (item T) {
this.topTime = time.Now()
if this.Top() != item {
this.topIndex ++
this.stack = append(this.stack[:this.topIndex], item)
}
if len(this.stack) > this.max {
this.topIndex --
this.stack = this.stack[1:]
}
}
// PushWeak replaces the most recent item if it was added recently (sooner than
// specified by minAge), and will otherwise push the item normally. If the
// history was popped or cleared beforehand, the item will always be pushed
// normally. This is intended to be used for things such as keystrokes.
func (this *History[T]) PushWeak (item T, minAge time.Duration) {
if time.Since(this.topTime) > minAge {
this.Push(item)
} else {
this.Swap(item)
}
}
// Redo undoes an Undo operation and returns the resulting top of the stack.
func (this *History[T]) Redo () T {
if this.topIndex < len(this.stack) - 1 {
this.topIndex ++
}
return this.Top()
}
// Undo removes the most recent item and returns what was under it. If there is
// only one item (the initial item), it will kept and returned.
func (this *History[T]) Undo () T {
this.topTime = time.Time { }
if this.topIndex > 0 {
this.topIndex --
}
return this.Top()
}
// Clear removes all items except for the initial one.
func (this *History[T]) Clear () {
this.topTime = time.Time { }
this.stack = this.stack[:1]
this.topIndex = 0
}

View File

@ -1,17 +1,65 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
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() }
theme.Apply(this, theme.R("objects", "Label", ""))
this := &Label { box: tomo.NewTextBox() }
this.box.SetRole(tomo.R("objects", "Label"))
this.SetText(text)
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()
}
// 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)
}
// 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))
}
// SetOverflow sets the X and Y overflow of the label.
func (this *Label) SetOverflow (x, y bool) {
this.box.SetAttr(tomo.AOverflow(x, y))
}

View File

@ -1,14 +1,15 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(LabelCheckbox)
// LabelCheckbox is a checkbox with a label.
type LabelCheckbox struct {
tomo.ContainerBox
box tomo.ContainerBox
checkbox *Checkbox
label *Label
}
@ -16,20 +17,44 @@ 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),
}
theme.Apply(box, theme.R("objects", "LabelCheckbox", ""))
box.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
box.Add(box.checkbox)
box.Add(box.label)
box.SetLayout(layouts.NewGrid([]bool { false, true }, []bool { 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.OnMouseUp(box.handleMouseUp)
box.label.OnMouseUp(box.handleMouseUp)
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.
func (this *LabelCheckbox) Value () bool {
return this.checkbox.Value()
}
// SetValue sets the value of the checkbox.
@ -42,21 +67,22 @@ func (this *LabelCheckbox) Toggle () {
this.checkbox.Toggle()
}
// Value returns the value of the checkbox.
func (this *LabelCheckbox) Value () bool {
return this.checkbox.Value()
}
// OnValueChange specifies a function to be called when the checkbox's value
// changes.
// OnValueChange specifies a function to be called when the user checks or
// unchecks the checkbox.
func (this *LabelCheckbox) OnValueChange (callback func ()) event.Cookie {
return this.checkbox.OnValueChange(callback)
}
func (this *LabelCheckbox) handleMouseUp (button input.Button) {
if button != input.ButtonLeft { return }
if this.MousePosition().In(this.Bounds()) {
func (this *LabelCheckbox) handleButtonDown (button input.Button) bool {
if !isClickingButton(button) { return false }
return true
}
func (this *LabelCheckbox) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.checkbox.SetFocused(true)
this.checkbox.Toggle()
}
return true
}

96
labelswatch.go Normal file
View File

@ -0,0 +1,96 @@
package objects
import "image/color"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(LabelSwatch)
// LabelSwatch is a swatch with a label.
type LabelSwatch struct {
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 {
labelSwatch := &LabelSwatch {
box: tomo.NewContainerBox(),
swatch: NewSwatch(value),
label: NewLabel(text),
}
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 }))
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.
func (this *LabelSwatch) Value () color.Color {
return this.swatch.Value()
}
// SetValue sets the color of the swatch.
func (this *LabelSwatch) SetValue (value color.Color) {
this.swatch.SetValue(value)
}
// OnValueChange specifies a function to be called when the swatch's color
// is changed by the user.
func (this *LabelSwatch) OnValueChange (callback func ()) event.Cookie {
return this.swatch.OnValueChange(callback)
}
// RGBA satisfies the color.Color interface
func (this *LabelSwatch) RGBA () (r, g, b, a uint32) {
return this.swatch.RGBA()
}
// OnConfirm specifies a function to be called when the user selects "OK" in the
// color picker.
func (this *LabelSwatch) OnConfirm (callback func ()) event.Cookie {
return this.swatch.OnConfirm(callback)
}
func (this *LabelSwatch) handleButtonDown (button input.Button) bool {
if !isClickingButton(button) { return true }
return true
}
func (this *LabelSwatch) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return true }
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.swatch.SetFocused(true)
this.swatch.Choose()
}
return true
}

40
layouts/contract.go Normal file
View File

@ -0,0 +1,40 @@
package layouts
import "image"
import "git.tebibyte.media/tomo/tomo"
var _ tomo.Layout = ContractVertical
// Contract is a layout that arranges boxes in a simple row or column according
// to their minimum sizes.
type Contract bool
// ContractVertical is a vertical contracted layout.
const ContractVertical Contract = true
// ContractHorizontal is a horizontal contracted layout.
const ContractHorizontal Contract = false
func (contract Contract) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
return contract.fallback().MinimumSize(hints, boxes)
}
func (contract Contract) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
contract.fallback().Arrange(hints, boxes)
}
func (contract Contract) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
return contract.fallback().RecommendedHeight(hints, boxes, width)
}
func (contract Contract) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
return contract.fallback().RecommendedWidth(hints, boxes, height)
}
func (contract Contract) fallback () tomo.Layout {
if contract == ContractVertical {
return Column { }
} else {
return Row { }
}
}

View File

@ -1,173 +0,0 @@
package layouts
import "image"
import "git.tebibyte.media/tomo/tomo"
// Cut is a layout that can be divided into smaller and smaller sections.
type Cut struct {
branches []*Cut
expand []bool
vertical bool
}
// NewCut creates and returns a new Cut layout.
func NewCut () *Cut {
return new(Cut)
}
// Vertical divides the layout vertically. Sections are specified using
// booleans. If a section is true, it will expand. If false, it will contract.
func (this *Cut) Vertical (expand ...bool) {
this.expand = expand
this.vertical = true
this.fill()
}
// Horizontal divides the layout horizontally. Sections are specified using
// booleans. If a section is true, it will expand. If false, it will contract.
func (this *Cut) Horizontal (expand ...bool) {
this.expand = expand
this.vertical = false
this.fill()
}
// At returns the section of this layout at the specified index.
func (this *Cut) At (index int) *Cut {
return this.branches[index]
}
func (this *Cut) real () bool {
return this != nil && this.branches != nil
}
func (this *Cut) fill () {
this.branches = make([]*Cut, len(this.expand))
for index := range this.branches {
this.branches[index] = new(Cut)
}
}
func (this *Cut) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
size, _ := this.minimumSize(hints, boxes)
return size
}
func (this *Cut) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
this.arrange(hints, boxes)
}
func (this *Cut) minimumSize (hints tomo.LayoutHints, boxes []tomo.Box) (image.Point, []tomo.Box) {
size := image.Point { }
for index, branch := range this.branches {
if len(boxes) == 0 { break }
var point image.Point
if branch.real() {
point, boxes = branch.minimumSize(hints, boxes)
} else {
point = boxes[0].MinimumSize()
boxes = boxes[1:]
}
if this.vertical {
if point.X > size.X { size.X = point.X }
if index > 0 { size.Y += hints.Gap.Y }
size.Y += point.Y
} else {
if point.Y > size.Y { size.Y = point.Y }
if index > 0 { size.X += hints.Gap.X }
size.X += point.X
}
}
return size, boxes
}
func (this *Cut) arrange (hints tomo.LayoutHints, boxes []tomo.Box) []tomo.Box {
nChildren := len(this.branches)
// collect minimum sizes and physical endpoints
var minimums = make([]image.Point, nChildren)
var leaves = make([]tomo.Box, nChildren)
var nBranches int
remaining := boxes
for index, branch := range this.branches {
if branch.real() {
minimums[index], remaining = branch.minimumSize(hints, remaining)
} else {
if len(remaining) == 0 { break }
leaves[index] = remaining[0]
minimums[index] = remaining[0].MinimumSize()
remaining = remaining[1:]
}
nBranches ++
}
// determine the amount of space to divide among expanding branches
gaps := nBranches - 1
var freeSpace float64; if this.vertical {
freeSpace = float64(hints.Bounds.Dy() - hints.Gap.Y * gaps)
} else {
freeSpace = float64(hints.Bounds.Dx() - hints.Gap.X * gaps)
}
var nExpanding float64
for index, minimum := range minimums {
if this.expand[index] {
nExpanding ++
} else if this.vertical {
freeSpace -= float64(minimum.Y)
} else {
freeSpace -= float64(minimum.X)
}
}
expandingSize := freeSpace / nExpanding
// calculate the size and position of branches
var bounds = make([]image.Rectangle, nChildren)
x := float64(hints.Bounds.Min.X)
y := float64(hints.Bounds.Min.Y)
for index, minimum := range minimums {
// get size along significant axis
var size float64; if this.expand[index] {
size = expandingSize
} else if this.vertical {
size = float64(minimum.Y)
} else {
size = float64(minimum.X)
}
// figure out bounds from size
if this.vertical {
bounds[index].Max = image.Pt (
int(hints.Bounds.Dx()),
int(size))
} else {
bounds[index].Max = image.Pt (
int(size),
int(hints.Bounds.Dy()))
}
bounds[index] = bounds[index].Add(image.Pt(int(x), int(y)))
// move along
if this.vertical {
y += float64(hints.Gap.Y) + size
} else {
x += float64(hints.Gap.X) + size
}
}
// apply the size and position
for index, bound := range bounds {
if leaves[index] != nil {
leaves[index].SetBounds(bound)
boxes = boxes[1:]
} else if this.branches[index] != nil {
newHints := hints
newHints.Bounds = bound
boxes = this.branches[index].arrange(newHints, boxes)
}
}
return boxes
}

136
layouts/flow.go Normal file
View File

@ -0,0 +1,136 @@
package layouts
import "image"
import "git.tebibyte.media/tomo/tomo"
var _ tomo.Layout = FlowVertical
// Flow is a grid layout where the number of rows and columns changes depending
// on the size of the container. It is designed to be used with an overflowing
// container. If the container does not overflow in the correct direction, the
// layout will behave like Contract.
type Flow bool
// FlowVertical is a vertical flow layout.
const FlowVertical Flow = true
// FlowHorizontal is a horizontal flow layout.
const FlowHorizontal Flow = false
func (flow Flow) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
// TODO: write down somewhere that layout minimums aren't taken into
// account when the respective direction is overflowed
return flow.fallback().MinimumSize(hints, boxes)
}
func (flow Flow) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
if flow.v() && !hints.OverflowY || flow.h() && !hints.OverflowX {
flow.fallback().Arrange(hints, boxes)
}
// find a minor size value that will fit all boxes
minorSize := 0
for index := 0; index < boxes.Len(); index ++ {
boxSize := flow.minor(boxes.MinimumSize(index))
if boxSize > minorSize { minorSize = boxSize }
}
if minorSize == 0 { return }
minorSteps :=
(flow.deltaMinor(hints.Bounds) + flow.minor(hints.Gap)) /
(minorSize + flow.minor(hints.Gap))
if minorSteps < 1 { minorSteps = 1 }
// arrange
point := hints.Bounds.Min
index := 0
for index < boxes.Len() {
// get a slice of boxes for this major step
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 := stepIndexStart; index < stepIndexEnd; index ++ {
boxSize := flow.major(boxes.MinimumSize(index))
if boxSize > majorSize { majorSize = boxSize }
}
if majorSize == 0 { continue }
// arrange all boxes on this major step
var size image.Point
size = flow.incrMajor(size, majorSize)
size = flow.incrMinor(size, minorSize)
for index := stepIndexStart; index < stepIndexEnd; index ++ {
bounds := image.Rectangle { Min: point, Max: point.Add(size) }
boxes.SetBounds(index, bounds)
point = flow.incrMinor(point, minorSize + flow.minor(hints.Gap))
}
if flow.v() {
point.Y += majorSize + hints.Gap.Y
point.X = hints.Bounds.Min.X
} else {
point.X += majorSize + hints.Gap.X
point.Y = hints.Bounds.Min.Y
}
}
}
func (flow Flow) v () bool { return flow == FlowVertical }
func (flow Flow) h () bool { return flow == FlowHorizontal }
func (flow Flow) minor (point image.Point) int {
if flow.v() {
return point.X
} else {
return point.Y
}
}
func (flow Flow) major (point image.Point) int {
if flow.v() {
return point.Y
} else {
return point.X
}
}
func (flow Flow) incrMinor (point image.Point, delta int) image.Point {
if flow.v() {
point.X += delta
return point
} else {
point.Y += delta
return point
}
}
func (flow Flow) incrMajor (point image.Point, delta int) image.Point {
if flow.v() {
point.Y += delta
return point
} else {
point.X += delta
return point
}
}
func (flow Flow) deltaMinor (rectangle image.Rectangle) int {
if flow.v() {
return rectangle.Dx()
} else {
return rectangle.Dy()
}
}
func (flow Flow) fallback () tomo.Layout {
return Contract(flow)
}
func (flow Flow) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
// TODO
return 0
}
func (flow Flow) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
// TODO
return 0
}

View File

@ -4,6 +4,8 @@ import "math"
import "image"
import "git.tebibyte.media/tomo/tomo"
var _ tomo.Layout = new(Grid)
// Grid is a layout that arranges boxes in a grid formation with distinct rows
// and columns. It is great for creating forms.
type Grid struct {
@ -16,15 +18,21 @@ type Grid struct {
// will contract. Boxes are laid out left to right, then top to bottom. Boxes
// that go beyond the lengh of rows will be laid out according to columns, but
// they will not expand vertically.
func NewGrid (columns, rows []bool) *Grid {
this := &Grid {
xExpand: columns,
yExpand: rows,
//
// If you aren't sure how to use this constructor, here is an example:
//
// X0 X1 X2 Y0 Y1 Y2
// NewGrid(true, false, true)(false, true, true)
func NewGrid (columns ...bool) func (rows ...bool) *Grid {
return func (rows ...bool) *Grid {
return &Grid {
xExpand: columns,
yExpand: rows,
}
}
return this
}
func (this *Grid) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
func (this *Grid) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
cols, rows := this.minimums(boxes)
size := image.Pt (
(len(cols) - 1) * hints.Gap.X,
@ -34,7 +42,7 @@ func (this *Grid) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.P
return size
}
func (this *Grid) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
func (this *Grid) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
xExpand := func (index int) bool {
return this.xExpand[index]
}
@ -48,9 +56,9 @@ func (this *Grid) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
expand(hints, rows, hints.Bounds.Dy(), yExpand)
position := hints.Bounds.Min
for index, box := range boxes {
for index := 0; index < boxes.Len(); index ++ {
col, row := index % len(cols), index / len(cols)
box.SetBounds(image.Rectangle {
boxes.SetBounds(index, image.Rectangle {
Min: position,
Max: position.Add(image.Pt(cols[col], rows[row])),
})
@ -63,13 +71,13 @@ func (this *Grid) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
}
}
func (this *Grid) minimums (boxes []tomo.Box) ([]int, []int) {
func (this *Grid) minimums (boxes tomo.BoxQuerier) ([]int, []int) {
nCols, nRows := this.dimensions(boxes)
cols, rows := make([]int, nCols), make([]int, nRows)
for index, box := range boxes {
for index := 0; index < boxes.Len(); index ++ {
col, row := index % len(cols), index / len(cols)
minimum := box.MinimumSize()
minimum := boxes.MinimumSize(index)
if cols[col] < minimum.X {
cols[col] = minimum.X
}
@ -81,8 +89,8 @@ func (this *Grid) minimums (boxes []tomo.Box) ([]int, []int) {
return cols, rows
}
func (this *Grid) dimensions (boxes []tomo.Box) (int, int) {
return len(this.xExpand), ceilDiv(len(boxes), len(this.xExpand))
func (this *Grid) dimensions (boxes tomo.BoxQuerier) (int, int) {
return len(this.xExpand), ceilDiv(boxes.Len(), len(this.xExpand))
}
func expand (hints tomo.LayoutHints, sizes []int, space int, expands func (int) bool) {
@ -104,5 +112,14 @@ func expand (hints tomo.LayoutHints, sizes []int, space int, expands func (int)
}
func ceilDiv (x, y int) int {
if y == 0 { return 0 }
return int(math.Ceil(float64(x) / float64(y)))
}
func (this *Grid) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
return this.MinimumSize(hints, boxes).Y
}
func (this *Grid) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
return this.MinimumSize(hints, boxes).X
}

View File

@ -1,88 +0,0 @@
package layouts
import "image"
import "git.tebibyte.media/tomo/tomo"
type Row struct { }
func (Row) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
dot := image.Point { }
for _, box := range boxes {
minimum := box.MinimumSize()
dot.X += minimum.X
if dot.Y < minimum.Y {
dot.Y = minimum.Y
}
}
dot.X += hints.Gap.X * (len(boxes) - 1)
return dot
}
func (Row) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
// TODO respect alignment value
dot := hints.Bounds.Min
for index, box := range boxes {
if index > 0 { dot.X += hints.Gap.X }
minimum := box.MinimumSize()
box.SetBounds(image.Rectangle {
Min: dot,
Max: dot.Add(image.Pt(minimum.X, hints.Bounds.Dy())),
})
dot.X += minimum.X
}
width := dot.X - hints.Bounds.Min.X
offset := 0
switch hints.AlignX {
case tomo.AlignMiddle:
offset = (hints.Bounds.Dx() - width) / 2
case tomo.AlignEnd:
offset = hints.Bounds.Dx() - width
}
for _, box := range boxes {
box.SetBounds(box.Bounds().Add(image.Pt(offset, 0)))
}
}
type Column struct { }
func (Column) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
dot := image.Point { }
for _, box := range boxes {
minimum := box.MinimumSize()
dot.Y += minimum.Y
if dot.X < minimum.X {
dot.X = minimum.X
}
}
dot.Y += hints.Gap.Y * (len(boxes) - 1)
return dot
}
func (Column) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
// TODO respect alignment value
dot := hints.Bounds.Min
for index, box := range boxes {
if index > 0 { dot.Y += hints.Gap.Y }
minimum := box.MinimumSize()
box.SetBounds(image.Rectangle {
Min: dot,
Max: dot.Add(image.Pt(hints.Bounds.Dx(), minimum.Y)),
})
dot.Y += minimum.Y
}
height := dot.Y - hints.Bounds.Min.Y
offset := 0
switch hints.AlignY {
case tomo.AlignMiddle:
offset = (hints.Bounds.Dy() - height) / 2
case tomo.AlignEnd:
offset = hints.Bounds.Dy() - height
}
for _, box := range boxes {
box.SetBounds(box.Bounds().Add(image.Pt(0, offset)))
}
}

220
layouts/rowcol.go Normal file
View File

@ -0,0 +1,220 @@
package layouts
import "image"
import "git.tebibyte.media/tomo/tomo"
var _ tomo.Layout = ContractVertical
// Row arranges boxes in a row. Boxes that share an index with a true value will
// expand, and others will contract.
type Row []bool
// Column arranges boxes in a column. Boxes that share an index with a true
// value will expand, and others will contract.
type Column []bool
func (column Column) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
dot := image.Point { }
for index := 0; index < boxes.Len(); index ++ {
minimum := boxes.MinimumSize(index)
dot.Y += minimum.Y
if dot.X < minimum.X {
dot.X = minimum.X
}
}
dot.Y += hints.Gap.Y * (boxes.Len() - 1)
return dot
}
func (row Row) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
dot := image.Point { }
for index := 0; index < boxes.Len(); index ++ {
minimum := boxes.MinimumSize(index)
dot.X += minimum.X
if dot.Y < minimum.Y {
dot.Y = minimum.Y
}
}
dot.X += hints.Gap.X * (boxes.Len() - 1)
return dot
}
func (column Column) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
expands := func (index int) bool {
if index >= len(column) { return false }
return column[index]
}
// determine expanding box size
expandingSize := 0.0
if !hints.OverflowY {
gaps := boxes.Len() - 1
freeSpace := float64(hints.Bounds.Dy() - hints.Gap.Y * gaps)
nExpanding := 0
for index := 0; index < boxes.Len(); index ++ {
if expands(index) {
nExpanding ++
} else {
freeSpace -= float64(boxes.MinimumSize(index).Y)
}
}
expandingSize = freeSpace / float64(nExpanding)
}
// determine width
width := 0
if hints.OverflowX {
for index := 0; index < boxes.Len(); index ++ {
minimum := boxes.MinimumSize(index)
if width < minimum.X { width = minimum.X }
}
} else {
width = hints.Bounds.Dx()
}
// arrange
dot := hints.Bounds.Min
bounds := make([]image.Rectangle, boxes.Len())
for index := 0; index < boxes.Len(); index ++ {
if index > 0 { dot.Y += hints.Gap.Y }
// determine height
height := boxes.MinimumSize(index).Y
if hints.OverflowY {
height = boxes.RecommendedHeight(index, width)
} else {
if expands(index) {
height = int(expandingSize)
}
}
// store bounds of this box
bounds[index] = image.Rectangle {
Min: dot,
Max: dot.Add(image.Pt(width, height)),
}
dot.Y += height
}
// align
height := dot.Y - hints.Bounds.Min.Y
offset := 0
switch hints.AlignY {
case tomo.AlignMiddle:
offset = (hints.Bounds.Dy() - height) / 2
case tomo.AlignEnd:
offset = hints.Bounds.Dy() - height
}
for index := 0; index < boxes.Len(); index ++ {
boxes.SetBounds(index, bounds[index].Add(image.Pt(0, offset)))
}
}
func (row Row) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
expands := func (index int) bool {
if index >= len(row) { return false }
return row[index]
}
// determine expanding box size
expandingSize := 0.0
if !hints.OverflowY {
gaps := boxes.Len() - 1
freeSpace := float64(hints.Bounds.Dx() - hints.Gap.X * gaps)
nExpanding := 0
for index := 0; index < boxes.Len(); index ++ {
if expands(index) {
nExpanding ++
} else {
freeSpace -= float64(boxes.MinimumSize(index).X)
}
}
expandingSize = freeSpace / float64(nExpanding)
}
// determine height
height := 0
if hints.OverflowY {
for index := 0; index < boxes.Len(); index ++ {
minimum := boxes.MinimumSize(index)
if height < minimum.Y { height = minimum.Y }
}
} else {
height = hints.Bounds.Dy()
}
// arrange
dot := hints.Bounds.Min
bounds := make([]image.Rectangle, boxes.Len())
for index := 0; index < boxes.Len(); index ++ {
if index > 0 { dot.X += hints.Gap.X }
// determine width
width := boxes.MinimumSize(index).X
if hints.OverflowY {
width = boxes.RecommendedHeight(index, height)
} else {
if expands(index) {
width = int(expandingSize)
}
}
// store bounds
bounds[index] = image.Rectangle {
Min: dot,
Max: dot.Add(image.Pt(width, height)),
}
dot.X += width
}
// align
width := dot.X - hints.Bounds.Min.X
offset := 0
switch hints.AlignX {
case tomo.AlignMiddle:
offset = (hints.Bounds.Dx() - width) / 2
case tomo.AlignEnd:
offset = hints.Bounds.Dx() - width
}
for index := 0; index < boxes.Len(); index ++ {
boxes.SetBounds(index, bounds[index].Add(image.Pt(offset, 0)))
}
}
func (column Column) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
height := 0
for index := 0; index < boxes.Len(); index ++ {
height += boxes.RecommendedHeight(index, width)
}
height += hints.Gap.Y * (boxes.Len() - 1)
return height
}
func (row Row) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
height := 0
for index := 0; index < boxes.Len(); index ++ {
minimum := boxes.MinimumSize(index)
boxHeight := boxes.RecommendedHeight(index, minimum.X)
if boxHeight > height { height = boxHeight }
}
return height
}
func (column Column) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
width := 0
for index := 0; index < boxes.Len(); index ++ {
minimum := boxes.MinimumSize(index)
boxWidth := boxes.RecommendedWidth(index, minimum.Y)
if boxWidth > width { width = boxWidth }
}
return width
}
func (row Row) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
width := 0
for index := 0; index < boxes.Len(); index ++ {
width += boxes.RecommendedWidth(index, height)
}
width += hints.Gap.X * (boxes.Len() - 1)
return width
}

135
menu.go Normal file
View File

@ -0,0 +1,135 @@
package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
// import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
// Menu is a menu window.
//
// Sub-components:
// - Root is the root of the window. It is differentiated from a normal Root
// object in that it has the [menu] tag. If the menu has been torn off, it
// will have the [torn] tag.
// - TearLine is a horizontal line at the top of the menu that, when clicked,
// causes the menu to be "torn off" into a movable window.
type Menu struct {
tomo.Window
parent tomo.Window
bounds image.Rectangle
rootContainer tomo.ContainerBox
tearLine tomo.Object
torn bool
}
// NewMenu creates a new menu with the specified items. The menu will appear
// directly under the 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 { }
menu.bounds = bounds
menu.parent = parent
window, err := menu.parent.NewChild(tomo.WindowKindMenu, menu.bounds)
if err != nil { return nil, err }
menu.Window = window
menu.rootContainer = tomo.NewContainerBox()
menu.rootContainer.SetAttr(tomo.ALayout(layouts.ContractVertical))
if !menu.torn {
menu.tearLine = menu.newTearLine()
menu.rootContainer.Add(menu.tearLine)
}
for _, item := range items {
menu.rootContainer.Add(item)
if item, ok := item.(*MenuItem); ok {
item.OnClick(func () {
if !menu.torn {
menu.Close()
}
})
}
}
menu.rootContainer.SetRole(tomo.R("objects", "Root"))
menu.rootContainer.SetTag("menu", true)
menu.Window.SetRoot(menu.rootContainer)
return menu, nil
}
// TearOff converts this menu into a tear-off menu.
func (this *Menu) TearOff () {
if this.torn { return }
if this.parent == nil { return }
this.torn = true
window, err := this.parent.NewChild(tomo.WindowKindToolbar, this.bounds)
window.SetIcon(tomo.IconListChoose)
if err != nil { return }
visible := this.Window.Visible()
this.Window.SetRoot(nil)
this.Window.Close()
this.rootContainer.Remove(this.tearLine)
this.rootContainer.SetTag("torn", true)
this.Window = window
this.Window.SetRoot(this.rootContainer)
this.Window.SetVisible(visible)
}
func (this *Menu) newTearLine () tomo.Object {
tearLine := tomo.NewBox()
tearLine.SetRole(tomo.R("objects", "TearLine"))
tearLine.SetFocusable(true)
tearLine.OnMouseEnter(func () {
tearLine.SetFocused(true)
})
tearLine.OnMouseLeave(func () {
tearLine.SetFocused(false)
})
tearLine.OnKeyDown(func (key input.Key, numberPad bool) bool {
if !isClickingKey(key) { return false }
return true
})
tearLine.OnKeyUp(func (key input.Key, numberPad bool) bool {
if !isClickingKey(key) { return false }
this.TearOff()
return true
})
tearLine.OnButtonDown(func (button input.Button) bool {
if !isClickingButton(button) { return false }
return true
})
tearLine.OnButtonUp(func (button input.Button) bool {
if !isClickingButton(button) { return false }
if tearLine.Window().MousePosition().In(tearLine.Bounds()) {
this.TearOff()
}
return true
})
return tearLine
}
func menuBoundsFromAnchor (anchor tomo.Object) image.Rectangle {
bounds := anchor.GetBox().Bounds()
return image.Rect (
bounds.Min.X, bounds.Max.Y,
bounds.Max.X, bounds.Max.Y)//.Add(windowBounds.Min)
}

111
menuitem.go Normal file
View File

@ -0,0 +1,111 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(MenuItem)
// MenuItem is a selectable memu item.
type MenuItem struct {
box tomo.ContainerBox
label *Label
icon *Icon
labelActive bool
on struct {
click event.FuncBroadcaster
}
}
// NewMenuItem creates a new menu item with the specified text.
func NewMenuItem (text string) *MenuItem {
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 {
menuItem := &MenuItem {
box: tomo.NewContainerBox(),
label: NewLabel(text),
icon: NewIcon(icon, tomo.IconSizeSmall),
}
menuItem.box.SetRole(tomo.R("objects", "MenuItem"))
menuItem.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
menuItem.box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
menuItem.box.Add(menuItem.icon)
menuItem.box.Add(menuItem.label)
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.
func (this *MenuItem) SetText (text string) {
this.label.SetText(text)
}
// SetIcon sets an icon for this item. Setting the icon to IconUnknown will
// remove it.
func (this *MenuItem) SetIcon (id tomo.Icon) {
this.icon.SetIcon(id, tomo.IconSizeSmall)
}
// OnClick specifies a function to be called when the menu item is clicked.
func (this *MenuItem) OnClick (callback func ()) event.Cookie {
return this.on.click.Connect(callback)
}
func (this *MenuItem) 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
}
func (this *MenuItem) handleKeyUp (key input.Key, numberPad bool) bool {
if key != input.KeyEnter && key != input.Key(' ') { return false }
this.on.click.Broadcast()
return true
}
func (this *MenuItem) handleButtonDown (button input.Button) bool {
if button != input.ButtonLeft { return false }
return true
}
func (this *MenuItem) handleButtonUp (button input.Button) bool {
if button != input.ButtonLeft { return false }
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.on.click.Broadcast()
}
return true
}

53
mimeicon.go Normal file
View File

@ -0,0 +1,53 @@
package objects
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 {
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 {
mimeIcon := &MimeIcon {
box: tomo.NewBox(),
}
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.
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.box.SetAttr(tomo.ATexture(texture))
this.box.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
if texture == nil {
this.box.SetAttr(tomo.AMinimumSize(0, 0))
} else {
bounds := texture.Bounds()
this.box.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
}
}

170
notebook.go Normal file
View File

@ -0,0 +1,170 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.Object = new(Notebook)
// Notebook holds multiple objects, each in their own page. The user can click
// the tab bar at the top to choose which one is activated.
//
// Sub-components:
// - TabRow sits at the top of the container and contains a row of tabs.
// - TabSpacer sits at either end of the tab row, bookending the list of tabs.
// - Tab appears in the tab row for each tab in the notebook. The user can
// click on the tab to switch to it.
// - PageWrapper sits underneath the TabRow and contains the active page.
//
// TabSpacer tags:
// - [left] The spacer is on the left.
// - [right] The spacer is on the right.
//
// Tab tags:
// - [active] The tab is currently active and its page is visible.
type Notebook struct {
box tomo.ContainerBox
leftSpacer tomo.Box
rightSpacer tomo.Box
tabsRow tomo.ContainerBox
pageWrapper tomo.ContainerBox
active string
pages []*page
}
// NewNotebook creates a new tabbed notebook.
func NewNotebook () *Notebook {
notebook := &Notebook {
box: tomo.NewContainerBox(),
}
notebook.box.SetRole(tomo.R("objects", "Notebook"))
notebook.box.SetAttr(tomo.ALayout(layouts.Column { false, true }))
notebook.leftSpacer = tomo.NewBox()
notebook.leftSpacer.SetRole(tomo.R("objects", "TabSpacer"))
notebook.leftSpacer.SetTag("left", true)
notebook.rightSpacer = tomo.NewBox()
notebook.rightSpacer.SetRole(tomo.R("objects", "TabSpacer"))
notebook.rightSpacer.SetTag("right", true)
notebook.tabsRow = tomo.NewContainerBox()
notebook.tabsRow.SetRole(tomo.R("objects", "TabRow"))
notebook.box.Add(notebook.tabsRow)
notebook.pageWrapper = tomo.NewContainerBox()
notebook.pageWrapper.SetRole(tomo.R("objects", "PageWrapper"))
notebook.pageWrapper.SetAttr(tomo.ALayout(layouts.Column { true }))
notebook.box.Add(notebook.pageWrapper)
notebook.Clear()
notebook.setTabRowLayout()
return notebook
}
// GetBox returns the underlying box.
func (this *Notebook) GetBox () tomo.Box {
return this.box
}
// Activate switches to a named page.
func (this *Notebook) Activate (name string) {
if _, tab := this.findTab(this.active); tab != nil {
tab.setActive(false)
this.pageWrapper.Remove(tab.root)
}
if _, tab := this.findTab(name); tab != nil {
tab.setActive(true)
this.pageWrapper.Add(tab.root)
} else {
name = ""
}
this.active = name
}
// Add adds an object as a page with the specified name.
func (this *Notebook) Add (name string, root tomo.Object) {
pag := &page {
TextBox: tomo.NewTextBox(),
name: name,
root: root,
}
pag.SetRole(tomo.R("objects", "Tab"))
pag.SetText(name)
pag.OnButtonDown(func (button input.Button) bool {
if button != input.ButtonLeft { return false }
this.Activate(name)
return true
})
pag.OnButtonUp(func (button input.Button) bool {
if button != input.ButtonLeft { return false }
return true
})
this.pages = append(this.pages, pag)
this.tabsRow.Insert(pag, this.rightSpacer)
this.setTabRowLayout()
// if the row was empty before, activate this tab
if len(this.pages) == 1 {
this.Activate(name)
}
}
// Remove removes the named page.
func (this *Notebook) Remove (name string) {
index, tab := this.findTab(name)
if index < 0 { return }
nextIndex := index - 1
this.tabsRow.Remove(tab)
this.pages = append(this.pages[:index], this.pages[index - 1:]...)
this.setTabRowLayout()
if nextIndex < 0 { nextIndex = 0 }
if nextIndex >= len(this.pages) { nextIndex = len(this.pages) - 1 }
if nextIndex < 0 {
this.Activate("")
} else {
this.Activate(this.pages[nextIndex].name)
}
}
// Clear removes all tabs.
func (this *Notebook) Clear () {
this.pages = nil
this.tabsRow.Clear()
this.tabsRow.Add(this.leftSpacer)
this.tabsRow.Add(this.rightSpacer)
this.pageWrapper.Clear()
}
func (this *Notebook) setTabRowLayout () {
row := make(layouts.Row, 1 + len(this.pages) + 1)
row[len(row) - 1] = true
this.tabsRow.SetAttr(tomo.ALayout(row))
}
func (this *Notebook) findTab (name string) (int, *page) {
for index, pag := range this.pages {
if pag.name == name { return index, pag }
}
return -1, nil
}
type page struct {
tomo.TextBox
name string
root tomo.Object
}
func (this *page) setActive (active bool) {
if active {
this.SetRole(tomo.R("objects", "Tab"))
this.SetTag("active", true)
} else {
this.SetRole(tomo.R("objects", "Tab"))
this.SetTag("active", false)
}
}

View File

@ -3,98 +3,133 @@ package objects
import "math"
import "strconv"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/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
on struct {
enter event.FuncBroadcaster
edit event.FuncBroadcaster
valueChange event.FuncBroadcaster
confirm event.FuncBroadcaster
}
}
// NewNumberInput creates a new number input with the specified value.
func NewNumberInput (value float64) *NumberInput {
box := &NumberInput {
ContainerBox: tomo.NewContainerBox(),
input: NewTextInput(""),
increment: NewButton(""),
decrement: NewButton(""),
numberInput := &NumberInput {
box: tomo.NewContainerBox(),
input: NewTextInput(""),
increment: NewButton(""),
decrement: NewButton(""),
}
theme.Apply(box, theme.R("objects", "NumberInput", ""))
box.Add(box.input)
box.Add(box.decrement)
box.Add(box.increment)
box.SetLayout(layouts.NewGrid([]bool { true, false, false }, []bool { true }))
box.increment.SetIcon(theme.IconActionIncrement)
box.decrement.SetIcon(theme.IconActionDecrement)
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.CaptureScroll(true)
box.CaptureKeyboard(true)
box.OnScroll(box.handleScroll)
box.OnKeyDown(box.handleKeyDown)
box.input.OnEnter(box.handleEnter)
box.input.OnEdit(box.on.edit.Broadcast)
box.increment.OnClick(func () { box.shift(1) })
box.decrement.OnClick(func () { box.shift(-1) })
return box
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
}
// SetValue sets the value of the input.
func (this *NumberInput) SetValue (value float64) {
this.input.SetText(strconv.FormatFloat(value, 'g', -1, 64))
// 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
}
// OnEnter specifies a function to be called when the user presses enter within
// the text input.
func (this *NumberInput) OnEnter (callback func ()) event.Cookie {
return this.on.enter.Connect(callback)
// SetValue sets the value of the input.
func (this *NumberInput) SetValue (value float64) {
this.input.SetValue(strconv.FormatFloat(value, 'g', -1, 64))
}
// OnEdit specifies a function to be called when the user edits the input value.
func (this *NumberInput) OnEdit (callback func ()) event.Cookie {
return this.on.edit.Connect(callback)
// OnValueChange specifies a function to be called when the user edits the input
// value.
func (this *NumberInput) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
// OnConfirm specifies a function to be called when the user presses enter within
// the number input.
func (this *NumberInput) OnConfirm (callback func ()) event.Cookie {
return this.on.confirm.Connect(callback)
}
func (this *NumberInput) shift (by int) {
this.SetValue(this.Value() + float64(by))
this.on.edit.Broadcast()
this.on.valueChange.Broadcast()
}
func (this *NumberInput) handleKeyDown (key input.Key, numpad bool) {
func (this *NumberInput) handleKeyDown (key input.Key, numpad bool) bool {
switch key {
case input.KeyUp: this.shift(1)
case input.KeyDown: this.shift(-1)
default: this.input.handleKeyDown(key, numpad)
case input.KeyUp:
this.shift(1)
return true
case input.KeyDown:
this.shift(-1)
return true
}
return false
}
func (this *NumberInput) handleScroll (x, y float64) {
func (this *NumberInput) handleKeyUp (key input.Key, numpad bool) bool {
switch key {
case input.KeyUp: return true
case input.KeyDown: return true
}
return false
}
func (this *NumberInput) handleScroll (x, y float64) bool {
if x == 0 {
this.shift(-int(math.Round(y)))
} else {
this.input.handleScroll(x, y)
return true
}
return false
}
func (this *NumberInput) handleEnter () {
func (this *NumberInput) handleConfirm () {
this.SetValue(this.Value())
this.on.enter.Broadcast()
this.on.confirm.Broadcast()
}

22
pegboard.go Normal file
View File

@ -0,0 +1,22 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.ContentObject = new(Pegboard)
// Pegboard is an object that can contain other objects. It is intended to
// contain a flowed list of objects which represent some data, such as files.
type Pegboard struct {
abstractContainer
}
// NewPegboard creates a new pegboard. If the provided layout is nil, it will
// use a FlowVertical layout.
func NewPegboard (layout tomo.Layout, children ...tomo.Object) *Pegboard {
if layout == nil { layout = layouts.FlowVertical }
pegboard := &Pegboard { }
pegboard.init(layout, children...)
pegboard.box.SetRole(tomo.R("objects", "Pegboard"))
return pegboard
}

20
root.go Normal file
View File

@ -0,0 +1,20 @@
package objects
import "git.tebibyte.media/tomo/tomo"
var _ tomo.ContentObject = new(Root)
// Root is an object that can contain other objects. It is intended to be used
// as the root of a window in order to contain its segments.
type Root struct {
abstractContainer
}
// NewRoot creates a new container.
func NewRoot (layout tomo.Layout, children ...tomo.Object) *Root {
this := &Root { }
this.init(layout, children...)
this.box.SetRole(tomo.R("objects", "Root"))
this.box.SetTag("outer", true)
return this
}

View File

@ -2,15 +2,27 @@ package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(Scrollbar)
// Scrollbar is a special type of slider that can control the scroll of any
// overflowing ContainerBox.
// overflowing ContainerObject.
//
// Sub-components:
// - ScrollbarHandle is the grabbable handle of the scrollbar.
//
// Tags:
// - [vertical] The scrollbar is oriented vertically.
// - [horizontall] The scrollbar is oriented horizontally.
//
// ScrollbarHandle tags:
// - [vertical] The handle is oriented vertically.
// - [horizontall] The handle is oriented horizontally.
type Scrollbar struct {
tomo.ContainerBox
handle *SliderHandle
box tomo.ContainerBox
handle *sliderHandle
layout scrollbarLayout
dragging bool
dragOffset image.Point
@ -24,8 +36,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 {
@ -33,21 +45,21 @@ func newScrollbar (orient string) *Scrollbar {
},
}
this.Add(this.handle)
this.SetFocusable(true)
this.box.Add(this.handle)
this.CaptureDND(true)
this.CaptureMouse(true)
this.CaptureScroll(true)
this.CaptureKeyboard(true)
this.OnKeyDown(this.handleKeyDown)
this.OnMouseDown(this.handleMouseDown)
this.OnMouseUp(this.handleMouseUp)
this.OnMouseMove(this.handleMouseMove)
this.OnScroll(this.handleScroll)
theme.Apply(this.handle, theme.R("objects", "SliderHandle", orient))
theme.Apply(this, theme.R("objects", "Slider", orient))
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", "ScrollbarHandle"))
this.handle.SetTag(orient, true)
this.box.SetRole(tomo.R("objects", "Scrollbar"))
this.box.SetTag(orient, true)
return this
}
@ -61,13 +73,18 @@ func NewHorizontalScrollbar () *Scrollbar {
return newScrollbar("horizontal")
}
// Link assigns this scrollbar to a ContentBox. Closing the returned cookie will
// unlink it.
func (this *Scrollbar) Link (box tomo.ContentBox) event.Cookie {
// 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.SetLayout(this.layout)
this.box.SetAttr(tomo.ALayout(this.layout))
return this.linkCookie
}
@ -80,12 +97,20 @@ func (this *Scrollbar) handleLinkedContentBoundsChange () {
} else {
this.layout.value = this.layout.contentPos() / trackLength
}
this.SetLayout(this.layout)
this.box.SetAttr(tomo.ALayout(this.layout))
if this.layout.value != previousValue {
this.on.valueChange.Broadcast()
}
}
// Value returns the value of the scrollbar between 0 and 1 where 0 is scrolled
// all the way to the left/top, and 1 is scrolled all the way to the
// right/bottom.
func (this *Scrollbar) Value () float64 {
if this.layout.linked == nil { return 0 }
return this.layout.value
}
// SetValue sets the value of the scrollbar between 0 and 1, where 0 is scrolled
// all the way to the left/top, and 1 is scrolled all the way to the
// right/bottom.
@ -98,57 +123,85 @@ func (this *Scrollbar) SetValue (value float64) {
position := trackLength * value
point := this.layout.linked.ContentBounds().Min
if this.layout.vertical {
point.Y = int(position)
point.Y = -int(position)
} else {
point.X = int(position)
point.X = -int(position)
}
this.layout.linked.ScrollTo(point)
}
// Value returns the value of the scrollbar between 0 and 1 where 0 is scrolled
// all the way to the left/top, and 1 is scrolled all the way to the
// right/bottom.
func (this *Scrollbar) Value () float64 {
if this.layout.linked == nil { return 0 }
return this.layout.value
}
// OnValueChange specifies a function to be called when the position of the
// scrollbar changes.
// OnValueChange specifies a function to be called when the user changes the
// position of the scrollbar.
func (this *Scrollbar) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
func (this *Scrollbar) handleKeyDown (key input.Key, numpad bool) {
var increment float64; if this.layout.vertical {
increment = -0.05
// PageSize returns the scroll distance of a page.
func (this *Scrollbar) PageSize () int {
if this.layout.linked == nil { return 0 }
viewport := this.layout.linked.GetBox().InnerBounds()
if this.layout.vertical {
return viewport.Dy()
} else {
increment = 0.05
}
switch key {
case input.KeyUp, input.KeyLeft:
if this.Modifiers().Alt {
this.SetValue(0)
} else {
this.SetValue(this.Value() - increment)
}
case input.KeyDown, input.KeyRight:
if this.Modifiers().Alt {
this.SetValue(1)
} else {
this.SetValue(this.Value() + increment)
}
case input.KeyHome:
this.SetValue(0)
case input.KeyEnd:
this.SetValue(1)
return viewport.Dx()
}
}
func (this *Scrollbar) handleMouseDown (button input.Button) {
pointer := this.MousePosition()
// StepSize returns the scroll distance of a step.
func (this *Scrollbar) StepSize () int {
// FIXME: this should not be hardcoded, need to get base font metrics
// from tomo somehow. should be (emspace, lineheight)
return 16
}
func (this *Scrollbar) handleKeyUp (key input.Key, numpad bool) bool {
switch key {
case input.KeyUp, input.KeyLeft: return true
case input.KeyDown, input.KeyRight: return true
case input.KeyPageUp: return true
case input.KeyPageDown: return true
case input.KeyHome: return true
case input.KeyEnd: return true
}
return false
}
func (this *Scrollbar) handleKeyDown (key input.Key, numpad bool) bool {
modifiers := this.box.Window().Modifiers()
switch key {
case input.KeyUp, input.KeyLeft:
if modifiers.Alt() {
this.SetValue(0)
} else {
this.scrollBy(this.StepSize())
}
return true
case input.KeyDown, input.KeyRight:
if modifiers.Alt() {
this.SetValue(1)
} else {
this.scrollBy(-this.StepSize())
}
case input.KeyPageUp:
this.scrollBy(this.PageSize())
return true
case input.KeyPageDown:
this.scrollBy(-this.PageSize())
return true
case input.KeyHome:
this.SetValue(0)
return true
case input.KeyEnd:
this.SetValue(1)
return true
}
return false
}
func (this *Scrollbar) handleButtonDown (button input.Button) bool {
pointer := this.box.Window().MousePosition()
handle := this.handle.Bounds()
within := pointer.In(handle)
@ -164,7 +217,7 @@ func (this *Scrollbar) handleMouseDown (button input.Button) {
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()
@ -173,39 +226,53 @@ func (this *Scrollbar) handleMouseDown (button input.Button) {
}
case input.ButtonMiddle:
if above {
this.scrollBy(-this.pageSize())
this.scrollBy(this.PageSize())
} else {
this.scrollBy(this.pageSize())
this.scrollBy(-this.PageSize())
}
case input.ButtonRight:
if above {
this.scrollBy(-this.stepSize())
this.scrollBy(this.StepSize())
} else {
this.scrollBy(this.stepSize())
this.scrollBy(-this.StepSize())
}
}
return true
}
func (this *Scrollbar) handleMouseUp (button input.Button) {
if button != input.ButtonLeft || !this.dragging { return }
func (this *Scrollbar) handleButtonUp (button input.Button) bool {
if button != input.ButtonLeft || !this.dragging { return true }
this.dragging = false
return true
}
func (this *Scrollbar) handleMouseMove () {
if !this.dragging { return }
func (this *Scrollbar) handleMouseMove () bool {
if !this.dragging { return false }
this.drag()
return true
}
func (this *Scrollbar) handleScroll (x, y float64) {
if this.layout.linked == nil { return }
func (this *Scrollbar) handleScroll (x, y float64) bool {
if this.layout.linked == nil { return false }
delta := (x + y)
if this.layout.vertical {
x = 0
y = delta
} else {
x = delta
y = 0
}
this.layout.linked.ScrollTo (
this.layout.linked.ContentBounds().Min.
Add(image.Pt(int(x), int(y))))
Sub(image.Pt(int(x), int(y))))
return true
}
func (this *Scrollbar) drag () {
pointer := this.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 {
@ -221,30 +288,14 @@ 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))
}
}
func (this *Scrollbar) pageSize () int {
if this.layout.linked == nil { return 0 }
viewport := this.layout.linked.InnerBounds()
if this.layout.vertical {
return viewport.Dy()
} else {
return viewport.Dx()
}
}
func (this *Scrollbar) stepSize () int {
// FIXME: this should not be hardcoded, need to get base font metrics
// from theme somehow. should be (emspace, lineheight)
return 16
}
func (this *Scrollbar) scrollBy (distance int) {
if this.layout.linked == nil { return }
var vector image.Point; if this.layout.vertical {
@ -269,28 +320,29 @@ func (this *Scrollbar) newLinkCookie (subCookies ...event.Cookie) *scrollbarCook
}
}
func (this *scrollbarCookie) Close () {
func (this *scrollbarCookie) Close () error {
for _, cookie := range this.subCookies {
cookie.Close()
}
this.owner.layout.linked = nil
this.owner.SetLayout(this.owner.layout)
this.owner.box.SetAttr(tomo.ALayout(this.owner.layout))
return nil
}
type scrollbarLayout struct {
vertical bool
value float64
linked tomo.ContentBox
linked tomo.ContentObject
}
func (scrollbarLayout) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
if len(boxes) != 1 { return image.Pt(0, 0) }
return boxes[0].MinimumSize()
func (scrollbarLayout) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
if boxes.Len() != 1 { return image.Pt(0, 0) }
return boxes.MinimumSize(0)
}
func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
if len(boxes) != 1 { return }
handle := image.Rectangle { Max: boxes[0].MinimumSize() }
func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
if boxes.Len() != 1 { return }
handle := image.Rectangle { Max: boxes.MinimumSize(0) }
gutter := hints.Bounds
var gutterLength float64;
@ -307,10 +359,11 @@ func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
handleLength := gutterLength * this.viewportContentRatio()
if handleLength < handleMin { handleLength = handleMin }
if handleLength >= gutterLength {
// just hide the handle if it isn't needed.
// TODO: we need a way to hide and show boxes, this is janky as
// fuck
boxes[0].SetBounds(image.Rect(-16, -16, 0, 0))
// just hide the handle if it isn't needed. we are the layout
// and we shouldn't be adding and removing boxes, so this is
// really the only good way to hide things.
// TODO perhaps have a "Hidden" rectangle in the Tomo API?
boxes.SetBounds(0, image.Rect(-32, -32, -16, -16))
return
}
if this.vertical {
@ -327,13 +380,21 @@ func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
} else {
handleOffset = image.Pt(int(handlePosition), 0)
}
handle = handle.Add(handleOffset).Add(gutter.Min)
handle = handle.Sub(handleOffset).Add(gutter.Min)
// place handle
boxes[0].SetBounds(handle)
boxes.SetBounds(0, handle)
}
func (this scrollbarLayout) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
return this.MinimumSize(hints, boxes).X
}
func (this scrollbarLayout) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
return this.MinimumSize(hints, boxes).Y
}
func (this scrollbarLayout) viewportContentRatio () float64 {
if this.linked == nil { return 0 }
return this.viewportLength() / this.contentLength()
@ -341,9 +402,9 @@ func (this scrollbarLayout) viewportContentRatio () float64 {
func (this scrollbarLayout) viewportLength () float64 {
if this.vertical {
return float64(this.linked.InnerBounds().Dy())
return float64(this.linked.GetBox().InnerBounds().Dy())
} else {
return float64(this.linked.InnerBounds().Dx())
return float64(this.linked.GetBox().InnerBounds().Dx())
}
}

View File

@ -2,8 +2,9 @@ package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/objects/layouts"
// ScrollSide determines which Scrollbars are active in a ScrollContainer.
type ScrollSide int; const (
@ -36,43 +37,64 @@ func (sides ScrollSide) String () string {
}
}
var _ tomo.Object = new(ScrollContainer)
// ScrollContainer couples a ContentBox with one or two Scrollbars.
type ScrollContainer struct {
tomo.ContainerBox
layout *scrollContainerLayout
box tomo.ContainerBox
root tomo.ContentObject
horizontal *Scrollbar
vertical *Scrollbar
horizontalCookie event.Cookie
verticalCookie event.Cookie
on struct {
valueChange event.FuncBroadcaster
}
}
// NewScrollContainer creates a new scroll container.
func NewScrollContainer (sides ScrollSide) *ScrollContainer {
this := &ScrollContainer {
ContainerBox: tomo.NewContainerBox(),
layout: &scrollContainerLayout { },
scrollContainer := &ScrollContainer {
box: tomo.NewContainerBox(),
}
if sides.Vertical() {
this.layout.vertical = NewVerticalScrollbar()
this.Add(this.layout.vertical)
scrollContainer.vertical = NewVerticalScrollbar()
scrollContainer.vertical.OnValueChange(scrollContainer.handleValueChange)
scrollContainer.box.Add(scrollContainer.vertical)
}
if sides.Horizontal() {
this.layout.horizontal = NewHorizontalScrollbar()
this.Add(this.layout.horizontal)
scrollContainer.horizontal = NewHorizontalScrollbar()
scrollContainer.horizontal.OnValueChange(scrollContainer.handleValueChange)
scrollContainer.box.Add(scrollContainer.horizontal)
}
this.CaptureScroll(true)
this.OnScroll(this.handleScroll)
theme.Apply(this, theme.R("objects", "ScrollContainer", sides.String()))
this.SetLayout(this.layout)
return this
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 {
scrollContainer.box.SetAttr(tomo.ALayout(layouts.NewGrid(true)(true, false)))
} else {
scrollContainer.box.SetAttr(tomo.ALayout(layouts.NewGrid(true, false)(true, false)))
}
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
// a time, and setting it will remove and unlink the current child if there is
// one.
func (this *ScrollContainer) SetRoot (root tomo.ContentBox) {
if this.layout.root != nil {
// delete root and close cookies
this.Delete(this.layout.root)
func (this *ScrollContainer) SetRoot (root tomo.ContentObject) {
if this.root != nil {
// remove root and close cookies
this.box.Remove(this.root)
if this.horizontalCookie != nil {
this.horizontalCookie.Close()
this.horizontalCookie = nil
@ -82,86 +104,145 @@ func (this *ScrollContainer) SetRoot (root tomo.ContentBox) {
this.verticalCookie = nil
}
}
this.layout.root = root
this.root = root
if root != nil {
// insert root at the beginning (for keynav)
switch {
case this.layout.vertical != nil:
this.Insert(root, this.layout.vertical)
case this.layout.horizontal != nil:
this.Insert(root, this.layout.horizontal)
case this.vertical != nil:
this.box.Insert(root, this.vertical)
case this.horizontal != nil:
this.box.Insert(root, this.horizontal)
default:
this.Add(root)
this.box.Add(root)
}
// link root and remember cookies
if this.layout.horizontal != nil {
this.horizontalCookie = this.layout.horizontal.Link(root)
if this.horizontal != nil {
this.horizontalCookie = this.horizontal.Link(root)
}
if this.layout.vertical != nil {
this.verticalCookie = this.layout.vertical.Link(root)
if this.vertical != nil {
this.verticalCookie = this.vertical.Link(root)
}
}
}
func (this *ScrollContainer) handleScroll (x, y float64) {
if this.layout.root == nil { return }
this.layout.root.ScrollTo (
this.layout.root.ContentBounds().Min.
Add(image.Pt(int(x), int(y))))
}
type scrollContainerLayout struct {
root tomo.ContentBox
horizontal *Scrollbar
vertical *Scrollbar
}
func (this *scrollContainerLayout) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
var minimum image.Point; if this.root != nil {
minimum = this.root.MinimumSize()
}
// Value returns the horizontal and vertical scrollbar values where 0 is all the
// way to the left/top, and 1 is all the way to the right/bottom.
func (this *ScrollContainer) Value () (x, y float64) {
if this.horizontal != nil {
minimum.Y += this.horizontal.MinimumSize().Y
x = this.horizontal.Value()
}
if this.vertical != nil {
minimum.X += this.vertical.MinimumSize().X
minimum.Y = max(minimum.Y, this.vertical.MinimumSize().Y)
y = this.vertical.Value()
}
if this.horizontal != nil {
minimum.X = max(minimum.X, this.horizontal.MinimumSize().X)
}
return minimum
return x, y
}
func (this *scrollContainerLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
rootBounds := hints.Bounds
// SetValue sets the horizontal and vertical scrollbar values where 0 is all the
// way to the left/top, and 1 is all the way to the right/bottom.
func (this *ScrollContainer) SetValue (x, y float64) {
if this.horizontal != nil {
rootBounds.Max.Y -= this.horizontal.MinimumSize().Y
this.horizontal.SetValue(x)
}
if this.vertical != nil {
rootBounds.Max.X -= this.vertical.MinimumSize().X
}
if this.root != nil {
this.root.SetBounds(rootBounds)
}
if this.horizontal != nil {
this.horizontal.SetBounds(image.Rect (
hints.Bounds.Min.X,
rootBounds.Max.Y,
rootBounds.Max.X,
hints.Bounds.Max.Y))
}
if this.vertical != nil {
this.vertical.SetBounds(image.Rect (
rootBounds.Max.X,
hints.Bounds.Min.Y,
hints.Bounds.Max.X,
rootBounds.Max.Y))
this.vertical.SetValue(y)
}
}
func max (x, y int) int {
if x > y { return x }
return y
// OnValueChange specifies a function to be called when the user changes the
// position of the horizontal or vertical scrollbars.
func (this *ScrollContainer) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
// PageSize returns the scroll distance of a page.
func (this *ScrollContainer) PageSize () image.Point {
page := image.Point { }
if this.horizontal != nil {
page.X = this.horizontal.PageSize()
}
if this.vertical != nil {
page.Y = this.vertical.PageSize()
}
return page
}
// StepSize returns the scroll distance of a step.
func (this *ScrollContainer) StepSize () image.Point {
page := image.Point { }
if this.horizontal != nil {
page.X = this.horizontal.StepSize()
}
if this.vertical != nil {
page.Y = this.vertical.StepSize()
}
return page
}
func (this *ScrollContainer) handleValueChange () {
this.on.valueChange.Broadcast()
}
func (this *ScrollContainer) scrollBy (vector image.Point) {
if this.root == nil { return }
if vector == (image.Point { }) { return }
this.root.ScrollTo (
this.root.ContentBounds().Min.
Sub(vector))
}
func (this *ScrollContainer) handleScroll (x, y float64) bool {
if this.root == nil { return false }
this.scrollBy(image.Pt(int(x), int(y)))
return true
}
func (this *ScrollContainer) handleKeyDown (key input.Key, numpad bool) bool {
modifiers := this.box.Window().Modifiers()
vector := image.Point { }
switch key {
case input.KeyPageUp:
if modifiers.Shift() {
vector.X -= this.PageSize().X
} else {
vector.Y -= this.PageSize().Y
}
this.scrollBy(vector)
return true
case input.KeyPageDown:
if modifiers.Shift() {
vector.X += this.PageSize().X
} else {
vector.Y += this.PageSize().Y
}
this.scrollBy(vector)
return true
case input.KeyUp:
if modifiers.Shift() {
vector.X -= this.StepSize().X
} else {
vector.Y -= this.StepSize().Y
}
this.scrollBy(vector)
return true
case input.KeyDown:
if modifiers.Shift() {
vector.X += this.StepSize().X
} else {
vector.Y += this.StepSize().Y
}
this.scrollBy(vector)
return true
}
return false
}
func (this *ScrollContainer) handleKeyUp (key input.Key, numpad bool) bool {
switch key {
case input.KeyPageUp: return true
case input.KeyPageDown: return true
case input.KeyUp: return true
case input.KeyDown: return true
}
return false
}

89
segment.go Normal file
View File

@ -0,0 +1,89 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/objects/layouts"
var _ tomo.ContentObject = new(Segment)
// Segment is an object that can contain other objects, and provides a way to
// categorize the functionality of a window. Segments are typically laid out
// vertically in a window. There are several variants, the main one being
// the content segment. They can all be created using specialized constructors.
//
// The following is a brief visual discription of how they are typically used,
// and in what order:
//
// ┌──────────────────────────┐
// │ ┌──┐ ┌──┐ ┌────────────┐ ├─ command
// │ │◄─│ │─►│ │/foo/bar/baz│ │
// │ └──┘ └──┘ └────────────┘ │
// ├──────────────────────────┤
// │ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ├─ content
// │ │ │ │ │ │ │ │ │ │
// │ └───┘ └───┘ └───┘ └───┘ │
// │ ┌───┐ │
// │ │ │ │
// │ └───┘ │
// ├──────────────────────────┤
// │ 5 items, 4KiB ├─ status
// └──────────────────────────┘
// ┌─────────────────┐
// │ ┌───┐ │
// │ │ ! │ Continue? │
// │ └───┘ │
// ├─────────────────┤
// │ ┌──┐ ┌───┐ ├────────── option
// │ │No│ │Yes│ │
// │ └──┘ └───┘ │
// └─────────────────┘
//
// Tags:
// - [command] The segment is a command segment.
// - [content] The segment is a content segment.
// - [status] The segment is a status segment.
// - [option] The segment is an option segment.
type Segment struct {
abstractContainer
}
func newSegment (kind string, layout tomo.Layout, children ...tomo.Object) *Segment {
segment := &Segment { }
segment.init(layout, children...)
segment.box.SetRole(tomo.R("objects", "Segment"))
segment.box.SetTag(kind, true)
return segment
}
// NewCommandSegment creates a new segment intended to hold the window's
// navigational controls and command functionality. If the provided layout is
// nil, it will use a ContractHorizontal layout.
func NewCommandSegment (layout tomo.Layout, children ...tomo.Object) *Segment {
if layout == nil { layout = layouts.ContractHorizontal }
return newSegment("command", layout, children...)
}
// NewContentSegment creates a new segment intended to hold the window's main
// content. If the provided layout is nil, it will use a ContractVertical
// layout.
func NewContentSegment (layout tomo.Layout, children ...tomo.Object) *Segment {
if layout == nil { layout = layouts.ContractVertical }
return newSegment("content", layout, children...)
}
// NewStatusSegment creates a new segment intended to display the window's
// status. If the provided layout is nil, it will use a ContractHorizontal
// layout.
func NewStatusSegment (layout tomo.Layout, children ...tomo.Object) *Segment {
if layout == nil { layout = layouts.ContractHorizontal }
return newSegment("status", layout, children...)
}
// NewOptionSegment creates a new segment intended to hold the window's options.
// This is typically used for dialog boxes. If the provided layout is nil, it
// will use a ContractHorizontal layout. By default, it is end-aligned.
func NewOptionSegment (layout tomo.Layout, children ...tomo.Object) *Segment {
if layout == nil { layout = layouts.ContractHorizontal }
segment := newSegment("option", layout, children...)
segment.GetBox().SetAttr(tomo.AAlign(tomo.AlignEnd, tomo.AlignMiddle))
return segment
}

View File

@ -1,18 +1,24 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
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(),
}
theme.Apply(this, theme.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
}

201
slider.go
View File

@ -1,57 +1,73 @@
package objects
import "math"
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(Slider)
// Slider is a control that selects a numeric value between 0 and 1.
//
// Sub-components:
// - SliderHandle is the grabbable handle of the slider.
//
// Tags:
// - [vertical] The slider is oriented vertically.
// - [horizontall] The slider is oriented horizontally.
//
// SliderHandle tags:
// - [vertical] The handle is oriented vertically.
// - [horizontall] The handle is oriented horizontally.
type Slider struct {
tomo.ContainerBox
handle *SliderHandle
box tomo.ContainerBox
handle *sliderHandle
layout sliderLayout
dragging bool
dragOffset image.Point
step float64
on struct {
slide event.FuncBroadcaster
valueChange event.FuncBroadcaster
confirm event.FuncBroadcaster
}
}
// 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 {
vertical: orient == "vertical",
value: math.NaN(),
},
step: 0.05,
}
this.Add(this.handle)
this.SetFocusable(true)
slider.handle.SetRole(tomo.R("objects", "SliderHandle"))
slider.handle.SetTag(orient, true)
slider.box.SetRole(tomo.R("objects", "Slider"))
slider.box.SetTag(orient, true)
slider.box.Add(slider.handle)
slider.box.SetFocusable(true)
slider.SetValue(value)
this.CaptureDND(true)
this.CaptureMouse(true)
this.CaptureScroll(true)
this.CaptureKeyboard(true)
this.SetValue(value)
this.OnKeyDown(this.handleKeyDown)
this.OnMouseDown(this.handleMouseDown)
this.OnMouseUp(this.handleMouseUp)
this.OnMouseMove(this.handleMouseMove)
theme.Apply(this.handle, theme.R("objects", "SliderHandle", orient))
theme.Apply(this, theme.R("objects", "Slider", orient))
return this
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.
@ -64,13 +80,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.SetLayout(this.layout)
this.box.SetAttr(tomo.ALayout(this.layout))
}
// Value returns the value of the slider between 0 and 1.
@ -80,11 +108,17 @@ func (this *Slider) Value () float64 {
// OnValueChange specifies a function to be called when the user moves the
// slider.
func (this *Slider) OnSlide (callback func ()) event.Cookie {
return this.on.slide.Connect(callback)
func (this *Slider) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
// OnConfirm specifies a function to be called when the user stops moving the
// slider.
func (this *Slider) OnConfirm (callback func ()) event.Cookie {
return this.on.confirm.Connect(callback)
}
func (this *Slider) handleKeyDown (key input.Key, numpad bool) bool {
var increment float64; if this.layout.vertical {
increment = -0.05
} else {
@ -93,26 +127,45 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
switch key {
case input.KeyUp, input.KeyLeft:
if this.Modifiers().Alt {
if this.box.Window().Modifiers().Alt() {
this.SetValue(0)
} else {
this.SetValue(this.Value() - increment)
}
this.on.valueChange.Broadcast()
return true
case input.KeyDown, input.KeyRight:
if this.Modifiers().Alt {
if this.box.Window().Modifiers().Alt() {
this.SetValue(1)
} else {
this.SetValue(this.Value() + increment)
}
this.on.valueChange.Broadcast()
return true
case input.KeyHome:
this.SetValue(0)
this.on.valueChange.Broadcast()
return true
case input.KeyEnd:
this.SetValue(1)
this.on.valueChange.Broadcast()
return true
}
return false
}
func (this *Slider) handleMouseDown (button input.Button) {
pointer := this.MousePosition()
func (this *Slider) handleKeyUp (key input.Key, numpad bool) bool {
switch key {
case input.KeyUp, input.KeyLeft: return true
case input.KeyDown, input.KeyRight: return true
case input.KeyHome: return true
case input.KeyEnd: return true
}
return false
}
func (this *Slider) handleButtonDown (button input.Button) bool {
pointer := this.box.Window().MousePosition()
handle := this.handle.Bounds()
within := pointer.In(handle)
@ -128,7 +181,7 @@ func (this *Slider) handleMouseDown (button input.Button) {
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()
@ -138,35 +191,51 @@ func (this *Slider) handleMouseDown (button input.Button) {
case input.ButtonMiddle:
if above {
this.SetValue(0)
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
this.on.confirm.Broadcast()
} else {
this.SetValue(1)
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
this.on.confirm.Broadcast()
}
case input.ButtonRight:
if above {
this.SetValue(this.Value() - 0.05)
this.on.slide.Broadcast()
this.SetValue(this.Value() - this.step)
this.on.valueChange.Broadcast()
this.on.confirm.Broadcast()
} else {
this.SetValue(this.Value() + 0.05)
this.on.slide.Broadcast()
this.SetValue(this.Value() + this.step)
this.on.valueChange.Broadcast()
this.on.confirm.Broadcast()
}
}
return true
}
func (this *Slider) handleMouseUp (button input.Button) {
if button != input.ButtonLeft || !this.dragging { return }
func (this *Slider) handleButtonUp (button input.Button) bool {
if button != input.ButtonLeft || !this.dragging { return true }
this.dragging = false
this.on.confirm.Broadcast()
return true
}
func (this *Slider) handleMouseMove () {
if !this.dragging { return }
func (this *Slider) handleMouseMove () bool {
if !this.dragging { return false }
this.drag()
return true
}
func (this *Slider) handleScroll (x, y float64) bool {
delta := (x + y) * 0.005
this.SetValue(this.Value() + delta)
this.on.valueChange.Broadcast()
this.on.confirm.Broadcast()
return true
}
func (this *Slider) drag () {
pointer := this.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 {
@ -179,15 +248,15 @@ func (this *Slider) drag () {
float64(pointer.X) /
float64(gutter.Dx() - handle.Dx()))
}
this.on.slide.Broadcast()
this.on.valueChange.Broadcast()
}
func (this *Slider) fallbackDragOffset () image.Point {
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))
}
}
@ -197,31 +266,37 @@ type sliderLayout struct {
value float64
}
func (sliderLayout) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
if len(boxes) != 1 { return image.Pt(0, 0) }
return boxes[0].MinimumSize()
func (sliderLayout) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
if boxes.Len() != 1 { return image.Pt(0, 0) }
return boxes.MinimumSize(0)
}
func (this sliderLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
if len(boxes) != 1 { return }
handle := image.Rectangle { Max: boxes[0].MinimumSize() }
func (this sliderLayout) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
if boxes.Len() != 1 { return }
handle := image.Rectangle { Max: boxes.MinimumSize(0) }
gutter := hints.Bounds
if this.vertical {
height := gutter.Dy() - handle.Dy()
offset := int(float64(height) * (1 - this.value))
handle.Max.X = gutter.Dx()
boxes[0].SetBounds (
handle.
Add(image.Pt(0, offset)).
Add(gutter.Min))
boxes.SetBounds (
0,
handle.Add(image.Pt(0, offset)).Add(gutter.Min))
} else {
width := gutter.Dx() - handle.Dx()
offset := int(float64(width) * this.value)
handle.Max.Y = gutter.Dy()
boxes[0].SetBounds (
handle.
Add(image.Pt(offset, 0)).
Add(gutter.Min))
boxes.SetBounds (
0,
handle.Add(image.Pt(offset, 0)).Add(gutter.Min))
}
}
func (this sliderLayout) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
return this.MinimumSize(hints, boxes).X
}
func (this sliderLayout) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
return this.MinimumSize(hints, boxes).Y
}

207
swatch.go Normal file
View File

@ -0,0 +1,207 @@
package objects
import "log"
import "image"
import "image/color"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/objects/layouts"
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 {
box tomo.CanvasBox
value color.Color
editing bool
label string
on struct {
valueChange event.FuncBroadcaster
confirm event.FuncBroadcaster
}
}
// NewSwatch creates a new swatch with the given color.
func NewSwatch (value color.Color) *Swatch {
swatch := &Swatch {
box: tomo.NewCanvasBox(),
}
swatch.box.SetRole(tomo.R("objects", "Swatch"))
swatch.box.SetDrawer(swatch)
swatch.SetValue(value)
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
}
// SetValue sets the color of the swatch.
func (this *Swatch) SetValue (value color.Color) {
this.value = value
if value == nil { value = color.Transparent }
this.box.Invalidate()
}
// OnValueChange specifies a function to be called when the swatch's color
// is changed by the user.
func (this *Swatch) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
// RGBA satisfies the color.Color interface
func (this *Swatch) RGBA () (r, g, b, a uint32) {
if this.value == nil { return }
return this.value.RGBA()
}
// OnConfirm specifies a function to be called when the user selects "OK" in the
// color picker.
func (this *Swatch) OnConfirm (callback func ()) event.Cookie {
return this.on.confirm.Connect(callback)
}
// Choose creates a modal that allows the user to edit the color of the swatch.
func (this *Swatch) Choose () {
if this.editing { return }
var err error
var window tomo.Window
if parent := this.box.Window(); parent != nil {
window, err = parent.NewChild(tomo.WindowKindNormal, image.Rectangle { })
} else {
window, err = tomo.NewWindow(tomo.WindowKindNormal, image.Rectangle { })
}
if err != nil {
log.Println("objects: could not create swatch modal:", err)
return
}
if this.label == "" {
window.SetTitle("Select Color")
} else {
window.SetTitle(this.label)
}
committed := false
colorPicker := 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.OnClick(commit)
window.SetRoot(NewRoot (
layouts.Column { true, false },
NewContentSegment (
layouts.Column { true, false },
colorPicker,
NewContainer (
layouts.Row { false, true },
NewLabel("Hex"),
hexInput)),
NewOptionSegment(nil, cancelButton, okButton)))
window.OnClose(func () {
if committed {
this.on.confirm.Broadcast()
} else {
this.userSetValue(colorMemory)
}
this.editing = false
})
this.editing = true
window.SetVisible(true)
}
func (this *Swatch) Draw (can canvas.Canvas) {
pen := can.Pen()
// transparency slash
pen.Stroke(color.RGBA { R: 255, A: 255 })
pen.StrokeWeight(1)
pen.Path(this.box.Bounds().Min, this.box.Bounds().Max)
// color
if this.value != nil {
pen.StrokeWeight(0)
pen.Fill(this.value)
pen.Rectangle(this.box.Bounds())
}
}
func (this *Swatch) userSetValue (value color.Color) {
this.SetValue(value)
this.on.valueChange.Broadcast()
}
func (this *Swatch) handleKeyDown (key input.Key, numberPad bool) bool {
if !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 !isClickingButton(button) { return false }
return true
}
func (this *Swatch) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.Choose()
}
return true
}

303
textinput.go Normal file
View File

@ -0,0 +1,303 @@
package objects
import "time"
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/text"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/internal"
const textInputHistoryMaximum = 64
const textInputHistoryMaxAge = time.Second / 4
var _ tomo.ContentObject = new(TextInput)
type textHistoryItem struct {
text string
dot text.Dot
}
// TextInput is a single-line editable text box.
type TextInput struct {
box tomo.TextBox
text []rune
multiline bool
history *internal.History[textHistoryItem]
on struct {
dotChange event.FuncBroadcaster
valueChange event.FuncBroadcaster
confirm event.FuncBroadcaster
}
}
func newTextInput (text string, multiline bool) *TextInput {
textInput := &TextInput {
box: tomo.NewTextBox(),
text: []rune(text),
multiline: multiline,
history: internal.NewHistory[textHistoryItem] (
textHistoryItem { text: text },
textInputHistoryMaximum),
}
textInput.box.SetRole(tomo.R("objects", "TextInput"))
textInput.box.SetTag("multiline", multiline)
if multiline {
textInput.box.SetAttr(tomo.AOverflow(false, true))
textInput.box.SetAttr(tomo.AWrap(true))
textInput.box.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignStart))
} else {
textInput.box.SetAttr(tomo.AOverflow(true, false))
textInput.box.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
}
textInput.box.SetText(text)
textInput.box.SetFocusable(true)
textInput.box.SetSelectable(true)
textInput.box.OnKeyDown(textInput.handleKeyDown)
textInput.box.OnKeyUp(textInput.handleKeyUp)
textInput.box.OnScroll(textInput.handleScroll)
textInput.box.OnDotChange(textInput.handleDotChange)
return textInput
}
// NewTextInput creates a new text input containing the specified text.
func NewTextInput (text string) *TextInput {
return newTextInput(text, false)
}
// NewMultilineTextInput creates a new multiline text input containing the
// specified text.
func NewMultilineTextInput (text string) *TextInput {
return newTextInput(text, true)
}
// 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)
this.historySwapDot()
}
// Dot returns the text cursor or selection.
func (this *TextInput) Dot () text.Dot {
return this.box.Dot()
}
// OnDotChange specifies a function to be called when the text cursor or
// selection changes.
func (this *TextInput) OnDotChange (callback func ()) event.Cookie {
return this.on.dotChange.Connect(callback)
}
// 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.box.SetText(text)
this.logLargeAction()
}
// Value returns the text content of the input.
func (this *TextInput) Value () string {
return string(this.text)
}
// OnConfirm specifies a function to be called when the user presses enter
// within the text input.
func (this *TextInput) OnConfirm (callback func ()) event.Cookie {
return this.on.confirm.Connect(callback)
}
// OnValueChange specifies a function to be called when the user edits the input
// text.
func (this *TextInput) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
// Undo undoes the last action.
func (this *TextInput) Undo () {
this.recoverHistoryItem(this.history.Undo())
}
// Redo redoes the last previously undone action.
func (this *TextInput) Redo () {
this.recoverHistoryItem(this.history.Redo())
}
// Type types a character at the current dot position.
func (this *TextInput) Type (char rune) {
dot := this.Dot()
this.historySwapDot()
this.text, dot = text.Type(this.text, dot, rune(char))
this.Select(dot)
this.box.SetText(string(this.text))
this.logKeystroke()
}
func (this *TextInput) logKeystroke () {
if this.Dot().Empty() {
this.history.PushWeak (
this.currentHistoryState(),
textInputHistoryMaxAge)
} else {
this.logLargeAction()
}
}
func (this *TextInput) logLargeAction () {
this.history.Push(this.currentHistoryState())
}
func (this *TextInput) historySwapDot () {
top := this.history.Top()
top.dot = this.Dot()
this.history.SwapSilently(top)
}
func (this *TextInput) currentHistoryState () textHistoryItem {
return textHistoryItem {
text: string(this.text),
dot: this.Dot(),
}
}
func (this *TextInput) recoverHistoryItem (item textHistoryItem) {
this.box.SetText(item.text)
this.text = []rune(item.text)
this.box.Select(item.dot)
}
// TODO: add things like alt+up/down to move lines
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
dot := this.Dot()
txt := this.text
modifiers := this.box.Window().Modifiers()
word := modifiers.Control()
changed := false
defer func () {
if changed {
this.historySwapDot()
this.text = txt
this.box.SetText(string(txt))
this.box.Select(dot)
this.on.valueChange.Broadcast()
this.on.dotChange.Broadcast()
this.logKeystroke()
}
} ()
typeRune := func () {
txt, dot = text.Type(txt, dot, rune(key))
changed = true
}
if this.multiline && !modifiers.Control() {
switch {
case key == '\n', key == '\t':
typeRune()
return true
}
}
switch {
case isConfirmationKey(key):
this.on.confirm.Broadcast()
return true
case key == input.KeyBackspace:
txt, dot = text.Backspace(txt, dot, word)
changed = true
return true
case key == input.KeyDelete:
txt, dot = text.Delete(txt, dot, word)
changed = true
return true
case key.Printable() && !modifiers.Control():
typeRune()
return true
case key == 'z' || key == 'Z' && modifiers.Control():
if modifiers.Shift() {
this.Redo()
} else {
this.Undo()
}
return true
case key == 'y' && modifiers.Control():
this.Redo()
return true
default:
return false
}
}
func (this *TextInput) handleKeyUp (key input.Key, numpad bool) bool {
modifiers := this.box.Window().Modifiers()
if this.multiline && !modifiers.Control() {
switch {
case key == '\n', key == '\t':
return true
}
}
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
case key == 'z' && modifiers.Control():
return true
case key == 'y' && modifiers.Control():
return true
default:
return false
}
}
func (this *TextInput) handleScroll (x, y float64) bool {
if x == 0 { return false }
this.ScrollTo(this.ContentBounds().Min.Sub(image.Pt(int(x), int(y))))
return true
}
func (this *TextInput) handleDotChange () {
this.historySwapDot()
this.on.dotChange.Broadcast()
}

View File

@ -2,26 +2,86 @@ package objects
import "image"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/text"
import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(TextView)
// 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() }
theme.Apply(this, theme.R("objects", "TextView", ""))
this.SetFocusable(true)
this.SetSelectable(true)
this.SetText(text)
this.SetOverflow(false, true)
this.SetWrap(true)
this.OnScroll(this.handleScroll)
return this
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
}
func (this *TextView) handleScroll (x, y float64) {
this.ScrollTo(this.ContentBounds().Min.Add(image.Pt(int(x), int(y))))
// 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()
}
// OnDotChange specifies a function to be called when the text cursor or
// selection changes.
func (this *TextView) OnDotChange (callback func ()) event.Cookie {
return this.box.OnDotChange(callback)
}
// 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 {
this.ScrollTo(this.ContentBounds().Min.Sub(image.Pt(int(x), int(y))))
return true
}