2024-07-25 11:01:15 -06:00
|
|
|
package system
|
|
|
|
|
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
2024-08-10 18:24:25 -06:00
|
|
|
import "git.tebibyte.media/tomo/backend/style"
|
2024-07-25 11:01:15 -06:00
|
|
|
|
|
|
|
type styleApplicator struct {
|
2024-08-10 18:24:25 -06:00
|
|
|
style *style.Style
|
2024-07-25 11:01:15 -06:00
|
|
|
role tomo.Role
|
2024-08-10 18:24:25 -06:00
|
|
|
rules []style.Rule
|
2024-07-25 11:01:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *styleApplicator) apply (box anyBox) {
|
|
|
|
if box.Role() != this.role {
|
2024-07-25 19:04:21 -06:00
|
|
|
this.role = box.Role()
|
2024-07-25 11:01:15 -06:00
|
|
|
// the role has changed, so re-cache the list of rules
|
2024-07-25 19:04:21 -06:00
|
|
|
this.rules = nil
|
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 {
|
2024-07-25 19:04:21 -06:00
|
|
|
this.rules = append(this.rules, rule)
|
2024-07-25 11:01:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// compile list of attributes by searching through the cached ruleset
|
2024-08-10 18:24:25 -06:00
|
|
|
attrs := make(style.AttrSet)
|
2024-07-25 11:01:15 -06:00
|
|
|
for _, rule := range this.rules {
|
|
|
|
satisifed := true
|
2024-07-25 19:04:21 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|