tomo/style.go

95 lines
2.2 KiB
Go

package tomo
import "fmt"
import "image/color"
// Role describes the role of an object.
type Role struct {
// Package is an optional namespace field. If specified, it should be
// the package name or module name the object is from.
Package string
// Object specifies what type of object it is. For example:
// - TextInput
// - Table
// - Label
// - Dial
// This should correspond directly to the type name of the object.
Object string
}
// String satisfies the fmt.Stringer interface. It follows the format of:
// Package.Object
func (r Role) String () string {
return fmt.Sprintf("%s.%s", r.Package, r.Object)
}
// R is shorthand for creating a role structure.
func R (pack, object string) Role {
return Role { Package: pack, Object: object }
}
// Color represents a color ID.
type Color int; const (
ColorBackground Color = iota
ColorForeground
ColorRaised
ColorSunken
ColorAccent
)
// RGBA satisfies the color.Color interface.
func (id Color) RGBA () (r, g, b, a uint32) {
if style == nil { return }
return style.Colors[id].RGBA()
}
var style *Style
// SetStyle sets the style.
func SetStyle (sty *Style) {
assertBackend()
style = sty
backend.SetStyle(sty)
}
// Style can apply a visual style to different objects.
type Style struct {
// Rules determines which styles get applied to which Objects.
Rules []Rule
// Colors maps tomo.Color values to color.RGBA values.
Colors map[Color] color.Color
}
// Rule describes under what circumstances should certain style attributes be
// active.
type Rule struct {
Role Role
Tags []string
Default AttrSet
Hovered AttrSet
Pressed AttrSet
Focused AttrSet
}
// Ru is shorthand for creating a rule structure. It is a partially applied
// function and is called like this:
// Ru(R("package", "Object"), "tag0", "tag1", "tag2") (
// AS( ... )
// AS( ... )
// AS( ... )
// AS( ... ))
func Ru (role Role, tags ...string) func (defaul, hovered, pressed, focused AttrSet) Rule {
return func (defaul, hovered, pressed, focused AttrSet) Rule {
return Rule {
Role: role,
Tags: tags,
Default: defaul,
Hovered: hovered,
Pressed: pressed,
Focused: focused,
}
}
}