backend/internal/system/style.go

49 lines
1.0 KiB
Go
Raw Normal View History

2024-07-25 11:01:15 -06:00
package system
import "git.tebibyte.media/tomo/tomo"
type styleApplicator struct {
style *tomo.Style
role tomo.Role
rules []*tomo.Rule
}
func (this *styleApplicator) apply (box anyBox) {
if box.Role() != this.role {
// the role has changed, so re-cache the list of rules
2024-07-25 16:17:43 -06:00
this.rules = make([]*tomo.Rule, 0, 4)
2024-07-25 11:01:15 -06:00
for _, rule := range this.style.Rules {
role := box.Role()
// blank fields match anything
if rule.Role.Package == "" { role.Package = "" }
if rule.Role.Object == "" { role.Object = "" }
if rule.Role == role {
this.rules = append(this.rules, &rule)
}
}
}
// compile list of attributes by searching through the cached ruleset
attrs := make(tomo.AttrSet)
for _, rule := range this.rules {
satisifed := true
2024-07-25 16:17:43 -06:00
if rule.Tags != nil {
for _, tag := range rule.Tags {
if !box.Tag(tag) {
satisifed = false
break
}
2024-07-25 11:01:15 -06:00
}
}
if satisifed {
attrs.MergeOver(rule.Set)
}
}
// apply that list of attributes
for _, attr := range attrs {
box.setAttr(attr, false)
}
}