diff --git a/button.go b/button.go index 4c57a3b..9945b3c 100644 --- a/button.go +++ b/button.go @@ -4,10 +4,16 @@ 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, true }, []bool { true }) // Button is a clickable button. type Button struct { - tomo.TextBox + tomo.ContainerBox + label *Label + icon *Icon on struct { click event.FuncBroadcaster } @@ -15,16 +21,41 @@ type Button struct { // NewButton creates a new button with the specified text. 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", "")) - box.SetText(text) - box.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle) + box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle) + box.Add(box.label) + box.SetLayout(buttonLayout) + + box.SetPropagateEvents(false) box.OnMouseUp(box.handleMouseUp) box.OnKeyUp(box.handleKeyUp) box.SetFocusable(true) 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. func (this *Button) OnClick (callback func ()) event.Cookie { return this.on.click.Connect(callback)