backend/internal/system/attribute.go

71 lines
1.5 KiB
Go
Raw Normal View History

2024-07-25 11:01:15 -06:00
package system
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/sashakoshka/goutil/container"
2024-07-25 11:01:15 -06:00
type attrHierarchy [T tomo.Attr] struct {
2024-08-10 18:24:25 -06:00
fallback T
style ucontainer.Optional[T]
user ucontainer.Optional[T]
2024-08-10 18:24:25 -06:00
}
func (this *attrHierarchy[T]) SetFallback (fallback T) {
this.fallback = fallback
2024-07-25 11:01:15 -06:00
}
func (this *attrHierarchy[T]) SetStyle (style T) (different bool) {
2024-08-10 18:24:25 -06:00
styleEquals := false
if previous, ok := this.style.Value(); ok {
styleEquals = previous.Equals(style)
}
this.style.Set(style)
return !styleEquals && !this.user.Exists()
}
func (this *attrHierarchy[T]) UnsetStyle () (different bool) {
different = this.style.Exists()
this.style.Unset()
return different
2024-07-25 11:01:15 -06:00
}
func (this *attrHierarchy[T]) SetUser (user T) (different bool) {
2024-08-10 18:24:25 -06:00
userEquals := false
if previous, ok := this.user.Value(); ok {
userEquals = previous.Equals(user)
}
this.user.Set(user)
2024-07-25 11:01:15 -06:00
return !userEquals
}
2024-08-10 18:24:25 -06:00
func (this *attrHierarchy[T]) UnsetUser () (different bool) {
different = this.user.Exists()
this.user.Unset()
return different
}
2024-07-25 11:01:15 -06:00
func (this *attrHierarchy[T]) Set (attr T, user bool) (different bool) {
if user {
return this.SetUser(attr)
} else {
return this.SetStyle(attr)
}
}
2024-08-10 18:24:25 -06:00
func (this *attrHierarchy[T]) Unset (user bool) (different bool) {
if user {
return this.UnsetUser()
} else {
return this.UnsetStyle()
}
}
2024-07-25 11:01:15 -06:00
func (this *attrHierarchy[T]) Value () T {
2024-08-10 18:24:25 -06:00
if user, ok := this.user.Value(); ok {
return user
} else if style, ok := this.style.Value(); ok{
return style
2024-07-25 11:01:15 -06:00
} else {
2024-08-10 18:24:25 -06:00
return this.fallback
2024-07-25 11:01:15 -06:00
}
}