Compare commits

...

2 Commits

2 changed files with 54 additions and 33 deletions

View File

@ -5,12 +5,14 @@ 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 {
tomo.ContainerBox
box tomo.ContainerBox
label *Label
icon *Icon
label *Label
icon *Icon
labelActive bool
on struct {
@ -25,27 +27,39 @@ func NewMenuItem (text string) *MenuItem {
// NewIconMenuItem creates a new menu item with the specified icon and text.
func NewIconMenuItem (icon tomo.Icon, text string) *MenuItem {
box := &MenuItem {
ContainerBox: tomo.NewContainerBox(),
label: NewLabel(text),
icon: NewIcon(icon, tomo.IconSizeSmall),
menuItem := &MenuItem {
box: tomo.NewContainerBox(),
label: NewLabel(text),
icon: NewIcon(icon, tomo.IconSizeSmall),
}
box.SetRole(tomo.R("objects", "MenuItem"))
box.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
menuItem.box.SetRole(tomo.R("objects", "MenuItem"))
menuItem.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
menuItem.box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
box.Add(box.icon)
box.Add(box.label)
menuItem.box.Add(menuItem.icon)
menuItem.box.Add(menuItem.label)
box.SetInputMask(true)
box.OnMouseEnter(box.handleMouseEnter)
box.OnMouseLeave(box.handleMouseLeave)
box.OnButtonDown(box.handleButtonDown)
box.OnButtonUp(box.handleButtonUp)
box.OnKeyDown(box.handleKeyDown)
box.OnKeyUp(box.handleKeyUp)
box.SetFocusable(true)
return box
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.
@ -90,7 +104,7 @@ func (this *MenuItem) handleButtonDown (button input.Button) bool {
func (this *MenuItem) handleButtonUp (button input.Button) bool {
if button != input.ButtonLeft { return false }
if this.Window().MousePosition().In(this.Bounds()) {
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.on.click.Broadcast()
}
return true

View File

@ -4,22 +4,29 @@ 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 {
tomo.Box
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 {
this := &MimeIcon {
Box: tomo.NewBox(),
mimeIcon := &MimeIcon {
box: tomo.NewBox(),
}
this.SetRole(tomo.R("objects", "MimeIcon"))
this.SetIcon(mime, size)
this.OnIconSetChange(this.handleIconSetChange)
return this
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.
@ -35,12 +42,12 @@ func (this *MimeIcon) handleIconSetChange () {
}
func (this *MimeIcon) setTexture (texture canvas.Texture) {
this.SetAttr(tomo.ATexture(texture))
this.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
this.box.SetAttr(tomo.ATexture(texture))
this.box.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
if texture == nil {
this.SetAttr(tomo.AMinimumSize(0, 0))
this.box.SetAttr(tomo.AMinimumSize(0, 0))
} else {
bounds := texture.Bounds()
this.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
this.box.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
}
}