Compare commits
5 Commits
v0.16.0
...
460733c8f3
| Author | SHA1 | Date | |
|---|---|---|---|
| 460733c8f3 | |||
| 5d2a366a62 | |||
| 2c7c77d8da | |||
| 8139d871cc | |||
| bb320dfcc9 |
@@ -6,7 +6,7 @@ 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 iconButtonLayout = layouts.NewGrid([]bool { true }, []bool { true })
|
||||
var bothButtonLayout = layouts.NewGrid([]bool { false, true }, []bool { true })
|
||||
|
||||
// Button is a clickable button.
|
||||
@@ -29,7 +29,6 @@ func NewButton (text string) *Button {
|
||||
label: NewLabel(text),
|
||||
}
|
||||
box.SetRole(tomo.R("objects", "Button", ""))
|
||||
tomo.Apply(box)
|
||||
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
|
||||
box.SetLayout(buttonLayout)
|
||||
box.SetText(text)
|
||||
|
||||
@@ -19,7 +19,6 @@ func NewCheckbox (value bool) *Checkbox {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
box.SetRole(tomo.R("objects", "Checkbox", ""))
|
||||
tomo.Apply(box)
|
||||
box.SetValue(false)
|
||||
|
||||
box.OnMouseUp(box.handleMouseUp)
|
||||
|
||||
2
go.mod
2
go.mod
@@ -2,6 +2,6 @@ module git.tebibyte.media/tomo/objects
|
||||
|
||||
go 1.20
|
||||
|
||||
require git.tebibyte.media/tomo/tomo v0.35.0
|
||||
require git.tebibyte.media/tomo/tomo v0.38.0
|
||||
|
||||
require golang.org/x/image v0.11.0 // indirect
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,5 +1,5 @@
|
||||
git.tebibyte.media/tomo/tomo v0.35.0 h1:1XvcUcWg1rBZXov3KfuX6VfiuBQ2mcJHIslHMLn07no=
|
||||
git.tebibyte.media/tomo/tomo v0.35.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
||||
git.tebibyte.media/tomo/tomo v0.38.0 h1:K5TP67RxnszudeNfmGZiU5cFTRjFueXiI3NCsgw+05U=
|
||||
git.tebibyte.media/tomo/tomo v0.38.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
|
||||
@@ -16,7 +16,6 @@ func NewHeading (level int, text string) *Heading {
|
||||
if level > 2 { level = 2 }
|
||||
this := &Heading { TextBox: tomo.NewTextBox() }
|
||||
this.SetRole(tomo.R("objects", "Heading", fmt.Sprint(level)))
|
||||
tomo.Apply(this)
|
||||
this.SetText(text)
|
||||
return this
|
||||
}
|
||||
|
||||
2
icon.go
2
icon.go
@@ -16,7 +16,6 @@ func NewIcon (id tomo.Icon, size tomo.IconSize) *Icon {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
this.SetRole(tomo.R("objects", "Icon", size.String()))
|
||||
tomo.Apply(this)
|
||||
this.SetTexture(id.Texture(size))
|
||||
return this
|
||||
}
|
||||
@@ -27,7 +26,6 @@ func NewMimeIcon (mime data.Mime, size tomo.IconSize) *Icon {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
this.SetRole(tomo.R("objects", "Icon", size.String()))
|
||||
tomo.Apply(this)
|
||||
this.SetTexture(tomo.MimeIcon(mime, size))
|
||||
return this
|
||||
}
|
||||
|
||||
1
label.go
1
label.go
@@ -11,7 +11,6 @@ type Label struct {
|
||||
func NewLabel (text string) *Label {
|
||||
this := &Label { TextBox: tomo.NewTextBox() }
|
||||
this.SetRole(tomo.R("objects", "Label", ""))
|
||||
tomo.Apply(this)
|
||||
this.SetText(text)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ func NewLabelCheckbox (value bool, text string) *LabelCheckbox {
|
||||
label: NewLabel(text),
|
||||
}
|
||||
box.SetRole(tomo.R("objects", "LabelCheckbox", ""))
|
||||
tomo.Apply(box)
|
||||
box.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
||||
box.Add(box.checkbox)
|
||||
box.Add(box.label)
|
||||
|
||||
99
menu.go
Normal file
99
menu.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package objects
|
||||
|
||||
import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
// import "git.tebibyte.media/tomo/tomo/event"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
|
||||
// Menu is a menu window.
|
||||
type Menu struct {
|
||||
tomo.Window
|
||||
|
||||
parent tomo.Window
|
||||
bounds image.Rectangle
|
||||
rootContainer tomo.ContainerBox
|
||||
tearLine tomo.Box
|
||||
torn bool
|
||||
}
|
||||
|
||||
// NewMenu creates a new menu with the specified items. The menu will appear
|
||||
// directly under the anchor Object. If the anchor is nil, it will appear
|
||||
// directly under the mouse pointer instead.
|
||||
func NewMenu (anchor tomo.Object, items ...tomo.Object) (*Menu, error) {
|
||||
menu := &Menu { }
|
||||
if anchor == nil {
|
||||
// TODO: *actually* put it under the mouse
|
||||
window, err := tomo.NewWindow(menu.bounds)
|
||||
if err != nil { return nil, err }
|
||||
menu.Window = window
|
||||
} else {
|
||||
menu.bounds = menuBoundsFromAnchor(anchor)
|
||||
menu.parent = anchor.GetBox().Window()
|
||||
window, err := menu.parent.NewMenu(menu.bounds)
|
||||
if err != nil { return nil, err }
|
||||
menu.Window = window
|
||||
}
|
||||
|
||||
menu.rootContainer = tomo.NewContainerBox()
|
||||
menu.rootContainer.SetLayout(layouts.ContractVertical)
|
||||
|
||||
if !menu.torn {
|
||||
menu.tearLine = tomo.NewBox()
|
||||
menu.tearLine.SetRole(tomo.R("objects", "TearLine", ""))
|
||||
menu.tearLine.SetFocusable(true)
|
||||
menu.tearLine.OnKeyUp(func (key input.Key, numberPad bool) {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return }
|
||||
menu.TearOff()
|
||||
})
|
||||
menu.tearLine.OnMouseUp(func (button input.Button) {
|
||||
if button != input.ButtonLeft { return }
|
||||
if menu.tearLine.MousePosition().In(menu.tearLine.Bounds()) {
|
||||
menu.TearOff()
|
||||
}
|
||||
})
|
||||
menu.rootContainer.Add(menu.tearLine)
|
||||
}
|
||||
|
||||
for _, item := range items {
|
||||
menu.rootContainer.Add(item)
|
||||
if item, ok := item.(*MenuItem); ok {
|
||||
item.OnClick(func () {
|
||||
if !menu.torn {
|
||||
menu.Close()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
menu.rootContainer.SetRole(tomo.R("objects", "Container", "menu"))
|
||||
|
||||
menu.Window.SetRoot(menu.rootContainer)
|
||||
return menu, nil
|
||||
}
|
||||
|
||||
// TearOff converts this menu into a tear-off menu.
|
||||
func (this *Menu) TearOff () {
|
||||
if this.torn { return }
|
||||
if this.parent == nil { return }
|
||||
this.torn = true
|
||||
|
||||
window, err := this.parent.NewChild(this.bounds)
|
||||
if err != nil { return }
|
||||
|
||||
visible := this.Window.Visible()
|
||||
this.Window.SetRoot(nil)
|
||||
this.Window.Close()
|
||||
|
||||
this.rootContainer.Remove(this.tearLine)
|
||||
|
||||
this.Window = window
|
||||
this.Window.SetRoot(this.rootContainer)
|
||||
this.Window.SetVisible(visible)
|
||||
}
|
||||
|
||||
func menuBoundsFromAnchor (anchor tomo.Object) image.Rectangle {
|
||||
bounds := anchor.GetBox().Bounds()
|
||||
return image.Rect (
|
||||
bounds.Min.X, bounds.Max.Y,
|
||||
bounds.Max.X, bounds.Max.Y)//.Add(windowBounds.Min)
|
||||
}
|
||||
73
menuitem.go
Normal file
73
menuitem.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
|
||||
// MenuItem is a clickable button.
|
||||
type MenuItem struct {
|
||||
tomo.ContainerBox
|
||||
|
||||
label *Label
|
||||
icon *Icon
|
||||
labelActive bool
|
||||
|
||||
on struct {
|
||||
click event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
// NewMenuItem creates a new menu item with the specified text.
|
||||
func NewMenuItem (text string) *MenuItem {
|
||||
box := &MenuItem {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
label: NewLabel(text),
|
||||
icon: NewIcon("", tomo.IconSizeSmall),
|
||||
}
|
||||
box.SetRole(tomo.R("objects", "MenuItem", ""))
|
||||
box.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
||||
box.SetLayout(layouts.NewGrid([]bool { false, true }, []bool { true }))
|
||||
|
||||
box.Add(box.icon)
|
||||
box.Add(box.label)
|
||||
|
||||
box.CaptureDND(true)
|
||||
box.CaptureMouse(true)
|
||||
box.CaptureScroll(true)
|
||||
box.CaptureKeyboard(true)
|
||||
|
||||
box.OnMouseUp(box.handleMouseUp)
|
||||
box.OnKeyUp(box.handleKeyUp)
|
||||
box.SetFocusable(true)
|
||||
return box
|
||||
}
|
||||
|
||||
// SetText sets the text of the items's label.
|
||||
func (this *MenuItem) SetText (text string) {
|
||||
this.label.SetText(text)
|
||||
}
|
||||
|
||||
// SetIcon sets an icon for this item. Setting the icon to IconUnknown will
|
||||
// remove it.
|
||||
func (this *MenuItem) SetIcon (id tomo.Icon) {
|
||||
if this.icon != nil { this.Remove(this.icon) }
|
||||
this.Insert(NewIcon(id, tomo.IconSizeSmall), this.label)
|
||||
}
|
||||
|
||||
// OnClick specifies a function to be called when the menu item is clicked.
|
||||
func (this *MenuItem) OnClick (callback func ()) event.Cookie {
|
||||
return this.on.click.Connect(callback)
|
||||
}
|
||||
|
||||
func (this *MenuItem) handleKeyUp (key input.Key, numberPad bool) {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return }
|
||||
this.on.click.Broadcast()
|
||||
}
|
||||
|
||||
func (this *MenuItem) handleMouseUp (button input.Button) {
|
||||
if button != input.ButtonLeft { return }
|
||||
if this.MousePosition().In(this.Bounds()) {
|
||||
this.on.click.Broadcast()
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,6 @@ func NewNumberInput (value float64) *NumberInput {
|
||||
decrement: NewButton(""),
|
||||
}
|
||||
box.SetRole(tomo.R("objects", "NumberInput", ""))
|
||||
tomo.Apply(box)
|
||||
box.Add(box.input)
|
||||
box.Add(box.decrement)
|
||||
box.Add(box.increment)
|
||||
|
||||
@@ -47,9 +47,7 @@ func newScrollbar (orient string) *Scrollbar {
|
||||
this.OnScroll(this.handleScroll)
|
||||
|
||||
this.handle.SetRole(tomo.R("objects", "SliderHandle", orient))
|
||||
tomo.Apply(this.handle)
|
||||
this.SetRole(tomo.R("objects", "Slider", orient))
|
||||
tomo.Apply(this)
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,6 @@ func NewScrollContainer (sides ScrollSide) *ScrollContainer {
|
||||
this.CaptureScroll(true)
|
||||
this.OnScroll(this.handleScroll)
|
||||
this.SetRole(tomo.R("objects", "ScrollContainer", sides.String()))
|
||||
tomo.Apply(this)
|
||||
this.SetLayout(this.layout)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -13,6 +13,5 @@ func NewSeparator () *Separator {
|
||||
Box: tomo.NewBox(),
|
||||
}
|
||||
this.SetRole(tomo.R("objects", "Separator", ""))
|
||||
tomo.Apply(this)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -53,9 +53,7 @@ func newSlider (orient string, value float64) *Slider {
|
||||
this.OnScroll(this.handleScroll)
|
||||
|
||||
this.handle.SetRole(tomo.R("objects", "SliderHandle", orient))
|
||||
tomo.Apply(this.handle)
|
||||
this.SetRole(tomo.R("objects", "Slider", orient))
|
||||
tomo.Apply(this)
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ type TextInput struct {
|
||||
func NewTextInput (text string) *TextInput {
|
||||
this := &TextInput { TextBox: tomo.NewTextBox() }
|
||||
this.SetRole(tomo.R("objects", "TextInput", ""))
|
||||
tomo.Apply(this)
|
||||
this.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
||||
this.SetText(text)
|
||||
this.SetFocusable(true)
|
||||
|
||||
@@ -12,7 +12,6 @@ type TextView struct {
|
||||
func NewTextView (text string) *TextView {
|
||||
this := &TextView { TextBox: tomo.NewTextBox() }
|
||||
this.SetRole(tomo.R("objects", "TextView", ""))
|
||||
tomo.Apply(this)
|
||||
this.SetFocusable(true)
|
||||
this.SetSelectable(true)
|
||||
this.SetText(text)
|
||||
|
||||
Reference in New Issue
Block a user