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"
// 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.
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 {
set := AttrSet { }
set.Add(attrs...)
@ -20,7 +21,7 @@ func AS (attrs ...Attr) AttrSet {
// Add adds attributes to the set.
func (this AttrSet) Add (attrs ...Attr) {
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) {
if other == nil { return }
for _, attr := range other {
if _, exists := this[attr.attr()]; !exists {
if _, exists := this[attr.Kind()]; !exists {
this.Add(attr)
}
}
@ -49,9 +50,27 @@ type Attr interface {
// Equals returns true if both attributes can reasonably be declared
// equal.
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.
type AttrColor struct { color.Color }
// AttrTexture sets the texture of a box to a named texture.
@ -256,24 +275,32 @@ func (this AttrLayout) Equals (other Attr) bool {
return false
}
// AttrNumber returns the number of an attribute. Each attribute type has a
// unique number. The exact values of these numbers are not part of the API and
// may change.
func AttrNumber (attr Attr) int {
return attr.attr()
}
func (AttrColor) Kind () AttrKind { return AttrKindColor }
func (AttrTexture) Kind () AttrKind { return AttrKindTexture }
func (AttrTextureMode) Kind () AttrKind { return AttrKindTextureMode }
func (AttrBorder) Kind () AttrKind { return AttrKindBorder }
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 (AttrTexture) attr () int { return 1 }
func (AttrTextureMode) attr () int { return 2 }
func (AttrBorder) attr () int { return 3 }
func (AttrMinimumSize) attr () int { return 4 }
func (AttrPadding) attr () int { return 5 }
func (AttrGap) attr () int { return 6 }
func (AttrTextColor) attr () int { return 7 }
func (AttrDotColor) attr () int { return 8 }
func (AttrFace) attr () int { return 9 }
func (AttrWrap) attr () int { return 10 }
func (AttrAlign) attr () int { return 11 }
func (AttrOverflow) attr () int { return 12 }
func (AttrLayout) attr () int { return 13 }
func (AttrColor) attr () { }
func (AttrTexture) attr () { }
func (AttrTextureMode) attr () { }
func (AttrBorder) attr () { }
func (AttrMinimumSize) attr () { }
func (AttrPadding) attr () { }
func (AttrGap) attr () { }
func (AttrTextColor) attr () { }
func (AttrDotColor) attr () { }
func (AttrFace) attr () { }
func (AttrWrap) attr () { }
func (AttrAlign) attr () { }
func (AttrOverflow) attr () { }
func (AttrLayout) attr () { }

View File

@ -57,8 +57,12 @@ type Box interface {
// SetTag adds or removes a named tag.
SetTag (string, bool)
// SetAttr overrides a style attribute.
SetAttr(Attr)
// SetAttr sets a style attribute, overriding the currently applied
// 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
// dragged. If this is nil (which is the default), this Box will not be