Compare commits

...

2 Commits

Author SHA1 Message Date
cf092b4447 Add UnsetAttr
Fully address #20
2024-07-31 00:19:39 -04:00
8403d621a8 AttrKind values are now part of the API 2024-07-30 18:23:37 -04:00
2 changed files with 59 additions and 28 deletions

View File

@ -6,11 +6,12 @@ import "golang.org/x/image/font"
import "git.tebibyte.media/tomo/tomo/canvas" import "git.tebibyte.media/tomo/tomo/canvas"
// AttrSet is a set of attributes wherein only one/zero of each attribute type // AttrSet is a set of attributes wherein only one/zero of each attribute type
// can exist. It is keyed by the AttrNumber of each attribute and must not be // can exist. It is keyed by the AttrKind of each attribute and must not be
// modified directly. // modified directly.
type AttrSet map[int] Attr type AttrSet map[AttrKind] Attr
// AS builds an AttrSet out of a vararg list of Attr values. // AS builds an AttrSet out of a vararg list of Attr values. If multiple Attrs
// of the same kind are specified, the last one will override the others.
func AS (attrs ...Attr) AttrSet { func AS (attrs ...Attr) AttrSet {
set := AttrSet { } set := AttrSet { }
set.Add(attrs...) set.Add(attrs...)
@ -20,7 +21,7 @@ func AS (attrs ...Attr) AttrSet {
// Add adds attributes to the set. // Add adds attributes to the set.
func (this AttrSet) Add (attrs ...Attr) { func (this AttrSet) Add (attrs ...Attr) {
for _, attr := range attrs { for _, attr := range attrs {
this[attr.attr()] = attr this[attr.Kind()] = attr
} }
} }
@ -29,7 +30,7 @@ func (this AttrSet) Add (attrs ...Attr) {
func (this AttrSet) MergeUnder (other AttrSet) { func (this AttrSet) MergeUnder (other AttrSet) {
if other == nil { return } if other == nil { return }
for _, attr := range other { for _, attr := range other {
if _, exists := this[attr.attr()]; !exists { if _, exists := this[attr.Kind()]; !exists {
this.Add(attr) this.Add(attr)
} }
} }
@ -49,9 +50,27 @@ type Attr interface {
// Equals returns true if both attributes can reasonably be declared // Equals returns true if both attributes can reasonably be declared
// equal. // equal.
Equals (Attr) bool Equals (Attr) bool
attr () int Kind () AttrKind
attr ()
} }
type AttrKind int; const (
AttrKindColor AttrKind = iota
AttrKindTexture
AttrKindTextureMode
AttrKindBorder
AttrKindMinimumSize
AttrKindPadding
AttrKindGap
AttrKindTextColor
AttrKindDotColor
AttrKindFace
AttrKindWrap
AttrKindAlign
AttrKindOverflow
AttrKindLayout
)
// AttrColor sets the background color of a box. // AttrColor sets the background color of a box.
type AttrColor struct { color.Color } type AttrColor struct { color.Color }
// AttrTexture sets the texture of a box to a named texture. // AttrTexture sets the texture of a box to a named texture.
@ -256,24 +275,32 @@ func (this AttrLayout) Equals (other Attr) bool {
return false return false
} }
// AttrNumber returns the number of an attribute. Each attribute type has a func (AttrColor) Kind () AttrKind { return AttrKindColor }
// unique number. The exact values of these numbers are not part of the API and func (AttrTexture) Kind () AttrKind { return AttrKindTexture }
// may change. func (AttrTextureMode) Kind () AttrKind { return AttrKindTextureMode }
func AttrNumber (attr Attr) int { func (AttrBorder) Kind () AttrKind { return AttrKindBorder }
return attr.attr() func (AttrMinimumSize) Kind () AttrKind { return AttrKindMinimumSize }
} func (AttrPadding) Kind () AttrKind { return AttrKindPadding }
func (AttrGap) Kind () AttrKind { return AttrKindGap }
func (AttrTextColor) Kind () AttrKind { return AttrKindTextColor }
func (AttrDotColor) Kind () AttrKind { return AttrKindDotColor }
func (AttrFace) Kind () AttrKind { return AttrKindFace }
func (AttrWrap) Kind () AttrKind { return AttrKindWrap }
func (AttrAlign) Kind () AttrKind { return AttrKindAlign }
func (AttrOverflow) Kind () AttrKind { return AttrKindOverflow }
func (AttrLayout) Kind () AttrKind { return AttrKindLayout }
func (AttrColor) attr () int { return 0 } func (AttrColor) attr () { }
func (AttrTexture) attr () int { return 1 } func (AttrTexture) attr () { }
func (AttrTextureMode) attr () int { return 2 } func (AttrTextureMode) attr () { }
func (AttrBorder) attr () int { return 3 } func (AttrBorder) attr () { }
func (AttrMinimumSize) attr () int { return 4 } func (AttrMinimumSize) attr () { }
func (AttrPadding) attr () int { return 5 } func (AttrPadding) attr () { }
func (AttrGap) attr () int { return 6 } func (AttrGap) attr () { }
func (AttrTextColor) attr () int { return 7 } func (AttrTextColor) attr () { }
func (AttrDotColor) attr () int { return 8 } func (AttrDotColor) attr () { }
func (AttrFace) attr () int { return 9 } func (AttrFace) attr () { }
func (AttrWrap) attr () int { return 10 } func (AttrWrap) attr () { }
func (AttrAlign) attr () int { return 11 } func (AttrAlign) attr () { }
func (AttrOverflow) attr () int { return 12 } func (AttrOverflow) attr () { }
func (AttrLayout) attr () int { return 13 } func (AttrLayout) attr () { }

View File

@ -57,8 +57,12 @@ type Box interface {
// SetTag adds or removes a named tag. // SetTag adds or removes a named tag.
SetTag (string, bool) SetTag (string, bool)
// SetAttr overrides a style attribute. // SetAttr sets a style attribute, overriding the currently applied
SetAttr(Attr) // style.
SetAttr (Attr)
// UnsetAttr reverts a style attribute to whatever is specified by the
// currently applied style.
UnsetAttr (AttrKind)
// SetDNDData sets the data that will be picked up if this Box is // SetDNDData sets the data that will be picked up if this Box is
// dragged. If this is nil (which is the default), this Box will not be // dragged. If this is nil (which is the default), this Box will not be