Buttons can have icons (wip)

This commit is contained in:
Sasha Koshka 2023-09-07 18:13:10 -04:00
parent 59ca14cab6
commit 312ee6270c

View File

@ -4,10 +4,16 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme" import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input" import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event" 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, true }, []bool { true })
// Button is a clickable button. // Button is a clickable button.
type Button struct { type Button struct {
tomo.TextBox tomo.ContainerBox
label *Label
icon *Icon
on struct { on struct {
click event.FuncBroadcaster click event.FuncBroadcaster
} }
@ -15,16 +21,41 @@ type Button struct {
// NewButton creates a new button with the specified text. // NewButton creates a new button with the specified text.
func NewButton (text string) *Button { func NewButton (text string) *Button {
box := &Button { TextBox: tomo.NewTextBox() } box := &Button {
ContainerBox: tomo.NewContainerBox(),
label: NewLabel(text),
}
theme.Apply(box, theme.R("objects", "Button", "")) theme.Apply(box, theme.R("objects", "Button", ""))
box.SetText(text) box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
box.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle) box.Add(box.label)
box.SetLayout(buttonLayout)
box.SetPropagateEvents(false)
box.OnMouseUp(box.handleMouseUp) box.OnMouseUp(box.handleMouseUp)
box.OnKeyUp(box.handleKeyUp) box.OnKeyUp(box.handleKeyUp)
box.SetFocusable(true) box.SetFocusable(true)
return box return box
} }
// SetText sets the text of the button's label.
func (this *Button) SetText (text string) {
this.label.SetText(text)
}
// SetIcon sets an icon for this button.
// TODO: use tomo.Icon instead, use small size icons
func (this *Button) SetIcon (icon *Icon) {
if this.icon != nil { this.Delete(this.icon) }
this.icon = icon
if this.icon == nil {
this.SetLayout(buttonLayout)
} else {
this.SetLayout(iconButtonLayout)
this.Insert(this.icon, this.label)
}
}
// OnClick specifies a function to be called when the button is clicked. // OnClick specifies a function to be called when the button is clicked.
func (this *Button) OnClick (callback func ()) event.Cookie { func (this *Button) OnClick (callback func ()) event.Cookie {
return this.on.click.Connect(callback) return this.on.click.Connect(callback)