Made icons a bit better

There is a new IconNone and an IconUpward, and buttons don't
expand awkwardly when they have an icon.
This commit is contained in:
Sasha Koshka 2023-03-20 01:12:19 -04:00
parent bf667aded9
commit 221647a265
3 changed files with 24 additions and 25 deletions

View File

@ -100,19 +100,18 @@ func (element *Button) SetText (text string) {
element.drawAndPush() element.drawAndPush()
} }
// SetIcon sets the icon of the button. // SetIcon sets the icon of the button. Passing theme.IconNone removes the
// current icon if it exists.
func (element *Button) SetIcon (id theme.Icon) { func (element *Button) SetIcon (id theme.Icon) {
if element.hasIcon && element.iconId == id { return } if id == theme.IconNone {
element.hasIcon = true element.hasIcon = false
element.iconId = id element.updateMinimumSize()
element.updateMinimumSize() element.drawAndPush()
element.drawAndPush() } else {
} if element.hasIcon && element.iconId == id { return }
element.hasIcon = true
// ClearIcon removes the button's icon, if it exists. element.iconId = id
func (element *Button) ClearIcon () { }
if !element.hasIcon { return }
element.hasIcon = false
element.updateMinimumSize() element.updateMinimumSize()
element.drawAndPush() element.drawAndPush()
} }
@ -145,26 +144,21 @@ func (element *Button) SetConfig (new config.Config) {
} }
func (element *Button) updateMinimumSize () { func (element *Button) updateMinimumSize () {
padding := element.theme.Padding(theme.PatternButton) padding := element.theme.Padding(theme.PatternButton)
margin := element.theme.Margin(theme.PatternButton) margin := element.theme.Margin(theme.PatternButton)
var minimumSize image.Rectangle
if element.showText { textBounds := element.drawer.LayoutBounds()
minimumSize = element.drawer.LayoutBounds() minimumSize := textBounds.Sub(textBounds.Min)
}
minimumSize = minimumSize.Sub(minimumSize.Min)
if element.hasIcon { if element.hasIcon {
icon := element.theme.Icon(element.iconId, theme.IconSizeSmall) icon := element.theme.Icon(element.iconId, theme.IconSizeSmall)
if icon != nil { if icon != nil {
bounds := icon.Bounds() bounds := icon.Bounds()
minimumSize.Max.X += bounds.Dx()
if element.showText { if element.showText {
minimumSize.Max.X += bounds.Dx()
minimumSize.Max.X += margin.X minimumSize.Max.X += margin.X
} } else {
if minimumSize.Max.Y < bounds.Dy() { minimumSize.Max.X = bounds.Dx()
minimumSize.Max.Y = bounds.Dy()
} }
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@ -64,6 +64,9 @@ type Color int; const (
// Icon lists a number of cannonical icons, each with its own ID. // Icon lists a number of cannonical icons, each with its own ID.
type Icon int; const ( type Icon int; const (
// IconNone specifies no icon.
IconNone = -1
// Place icons // Place icons
IconHome Icon = iota IconHome Icon = iota
Icon3DObjects Icon3DObjects
@ -168,7 +171,9 @@ const (
IconHistory IconHistory
IconYes IconYes
IconNo) IconNo
IconUpward)
const ( const (
// Status icons // Status icons