Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f59949c3bb | |||
| a5dfdda651 | |||
| 82657bf251 | |||
| 992a5b23f9 | |||
| f45476a5c9 | |||
| 4a400b68c2 | |||
| 2d7ac914a4 | |||
| f39e5245fc | |||
| 7e879defbd | |||
| aeea57a09c | |||
| 407049097d | |||
| 7c7d93d6d1 | |||
| 2eb82d9035 | |||
| ab0d84140f |
@@ -1,4 +1,6 @@
|
|||||||
# nasin
|
# nasin
|
||||||
|
|
||||||
|
[](https://pkg.go.dev/git.tebibyte.media/tomo/nasin)
|
||||||
|
|
||||||
Nasin provides an easy way to write applications with Tomo. Parts of Tomo that
|
Nasin provides an easy way to write applications with Tomo. Parts of Tomo that
|
||||||
aren't the GUI toolkit may be found here.
|
aren't the GUI toolkit may be found here.
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package nasin
|
package nasin
|
||||||
|
|
||||||
|
import "log"
|
||||||
import "image"
|
import "image"
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
import "git.tebibyte.media/tomo/nasin/internal/registry"
|
||||||
|
|
||||||
// Application represents an application object.
|
// Application represents an application object.
|
||||||
type Application interface {
|
type Application interface {
|
||||||
@@ -9,7 +11,7 @@ type Application interface {
|
|||||||
Describe () ApplicationDescription
|
Describe () ApplicationDescription
|
||||||
|
|
||||||
// Init performs the initial setup of the application.
|
// Init performs the initial setup of the application.
|
||||||
Init ()
|
Init () error
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplicationDescription describes the name and type of an application.
|
// ApplicationDescription describes the name and type of an application.
|
||||||
@@ -74,9 +76,16 @@ type ApplicationRole string; const (
|
|||||||
RoleChecklist ApplicationRole = "Checklist"
|
RoleChecklist ApplicationRole = "Checklist"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RunApplication is like Run, but runs an application.
|
// RunApplication is like tomo.Run, but runs an application. If something fails
|
||||||
func RunApplication (application Application) error {
|
// to initialize, an error is written to the standard logger.
|
||||||
return tomo.Run(application.Init)
|
func RunApplication (application Application) {
|
||||||
|
err := registry.Init()
|
||||||
|
if err != nil { log.Fatal("nasin: could not init registry:", err) }
|
||||||
|
err = tomo.Run(func () {
|
||||||
|
err := application.Init()
|
||||||
|
if err != nil { log.Fatal("nasin: could not run application:", err) }
|
||||||
|
})
|
||||||
|
if err != nil { log.Fatal("nasin: could not run application:", err) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewApplicationWindow creates a window for an application. It will
|
// NewApplicationWindow creates a window for an application. It will
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
package nasin
|
|
||||||
|
|
||||||
import "git.tebibyte.media/tomo/x"
|
|
||||||
import "git.tebibyte.media/tomo/tomo"
|
|
||||||
|
|
||||||
func init () {
|
|
||||||
tomo.Register(1, x.NewBackend)
|
|
||||||
}
|
|
||||||
4
go.mod
4
go.mod
@@ -3,8 +3,8 @@ module git.tebibyte.media/tomo/nasin
|
|||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.tebibyte.media/tomo/tomo v0.30.0
|
git.tebibyte.media/tomo/tomo v0.31.0
|
||||||
git.tebibyte.media/tomo/x v0.6.0
|
git.tebibyte.media/tomo/x v0.7.0
|
||||||
git.tebibyte.media/tomo/xdg v0.1.0
|
git.tebibyte.media/tomo/xdg v0.1.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
8
go.sum
8
go.sum
@@ -1,10 +1,10 @@
|
|||||||
git.tebibyte.media/sashakoshka/xgbkb v1.0.0/go.mod h1:pNcE6TRO93vHd6q42SdwLSTTj25L0Yzggz7yLe0JV6Q=
|
git.tebibyte.media/sashakoshka/xgbkb v1.0.0/go.mod h1:pNcE6TRO93vHd6q42SdwLSTTj25L0Yzggz7yLe0JV6Q=
|
||||||
git.tebibyte.media/tomo/tomo v0.30.0 h1:JoTklJ7yFVrzre4AwuKBMwzho9GomC9ySw354wDB4f4=
|
git.tebibyte.media/tomo/tomo v0.31.0 h1:LHPpj3AWycochnC8F441aaRNS6Tq6w6WnBrp/LGjyhM=
|
||||||
git.tebibyte.media/tomo/tomo v0.30.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
git.tebibyte.media/tomo/tomo v0.31.0/go.mod h1:C9EzepS9wjkTJjnZaPBh22YvVPyA4hbBAJVU20Rdmps=
|
||||||
git.tebibyte.media/tomo/typeset v0.7.0 h1:JFpEuGmN6R2XSCvkINYxpH0AyYUqqs+dZYr6OSd91y0=
|
git.tebibyte.media/tomo/typeset v0.7.0 h1:JFpEuGmN6R2XSCvkINYxpH0AyYUqqs+dZYr6OSd91y0=
|
||||||
git.tebibyte.media/tomo/typeset v0.7.0/go.mod h1:PwDpSdBF3l/EzoIsa2ME7QffVVajnTHZN6l3MHEGe1g=
|
git.tebibyte.media/tomo/typeset v0.7.0/go.mod h1:PwDpSdBF3l/EzoIsa2ME7QffVVajnTHZN6l3MHEGe1g=
|
||||||
git.tebibyte.media/tomo/x v0.6.0 h1:80BRiSwhZCqu6IPKZoQj7t1puKXXJpMB9eWVHQliTHM=
|
git.tebibyte.media/tomo/x v0.7.0 h1:HiLbKRWwwR+D1lYruhK3Z63JPiMlcISBe9TtCkZTeBI=
|
||||||
git.tebibyte.media/tomo/x v0.6.0/go.mod h1:6INfDGlcPyoYVMem64ScD5AZb43PkXDGkfgaNa5GCqQ=
|
git.tebibyte.media/tomo/x v0.7.0/go.mod h1:h4vXFU+ZQETr7hxr/ydHqM1xFzHKvV2uKnmGzagWgnY=
|
||||||
git.tebibyte.media/tomo/xdg v0.1.0 h1:6G2WYPPiM2IXleCpKKHuJA34BxumwNWuLsUoX3yu5zA=
|
git.tebibyte.media/tomo/xdg v0.1.0 h1:6G2WYPPiM2IXleCpKKHuJA34BxumwNWuLsUoX3yu5zA=
|
||||||
git.tebibyte.media/tomo/xdg v0.1.0/go.mod h1:tuaRwRkyYW7mqlxA7P2+V+e10KzcamNoUzcOgaIYKAY=
|
git.tebibyte.media/tomo/xdg v0.1.0/go.mod h1:tuaRwRkyYW7mqlxA7P2+V+e10KzcamNoUzcOgaIYKAY=
|
||||||
git.tebibyte.media/tomo/xgbkb v1.0.1 h1:b3HDUopjdQp1MZrb5Vpil4bOtk3NnNXtfQW27Blw2kE=
|
git.tebibyte.media/tomo/xgbkb v1.0.1 h1:b3HDUopjdQp1MZrb5Vpil4bOtk3NnNXtfQW27Blw2kE=
|
||||||
|
|||||||
2
internal/registry/doc.go
Normal file
2
internal/registry/doc.go
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
// Package registry provides platform-dependent components at compile time.
|
||||||
|
package registry
|
||||||
13
internal/registry/registry_unix.go
Normal file
13
internal/registry/registry_unix.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
//go:build unix && (!darwin)
|
||||||
|
package registry
|
||||||
|
|
||||||
|
import "git.tebibyte.media/tomo/x"
|
||||||
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/theme"
|
||||||
|
import "git.tebibyte.media/tomo/nasin/internal/theme/default"
|
||||||
|
|
||||||
|
func Init () error {
|
||||||
|
theme.SetTheme(defaultTheme.Theme())
|
||||||
|
tomo.Register(1, x.NewBackend)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
41
internal/theme/attribute.go
Normal file
41
internal/theme/attribute.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package theme
|
||||||
|
|
||||||
|
import "image"
|
||||||
|
import "image/color"
|
||||||
|
import "golang.org/x/image/font"
|
||||||
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
|
||||||
|
// Attr modifies one thing about an Objects's style.
|
||||||
|
type Attr interface { attr () int }
|
||||||
|
|
||||||
|
// AttrColor sets the background color of an Objects.
|
||||||
|
type AttrColor struct { color.Color }
|
||||||
|
// AttrTexture sets the texture of an Objects to a named texture.
|
||||||
|
type AttrTexture string
|
||||||
|
// AttrBorder sets the border of an Objects.
|
||||||
|
type AttrBorder []tomo.Border
|
||||||
|
// AttrMinimumSize sets the minimum size of an Objects.
|
||||||
|
type AttrMinimumSize image.Point
|
||||||
|
// AttrPadding sets the inner padding of an Objects.
|
||||||
|
type AttrPadding tomo.Inset
|
||||||
|
// AttrGap sets the gap between child Objects, if the Object is a ContainerBox.
|
||||||
|
type AttrGap image.Point
|
||||||
|
// AttrTextColor sets the text color, if the Object is a TextBox.
|
||||||
|
type AttrTextColor struct { color.Color }
|
||||||
|
// AttrDotColor sets the text selection color, if the Object is a TextBox.
|
||||||
|
type AttrDotColor struct { color.Color }
|
||||||
|
// AttrFace sets the font face, if the Object is a TextBox.
|
||||||
|
type AttrFace struct { font.Face }
|
||||||
|
// AttrAlign sets the alignment, if the Object is a ContentBox.
|
||||||
|
type AttrAlign struct { X, Y tomo.Align }
|
||||||
|
|
||||||
|
func (AttrColor) attr () int { return 0 }
|
||||||
|
func (AttrTexture) attr () int { return 1 }
|
||||||
|
func (AttrBorder) attr () int { return 2 }
|
||||||
|
func (AttrMinimumSize) attr () int { return 3 }
|
||||||
|
func (AttrPadding) attr () int { return 4 }
|
||||||
|
func (AttrGap) attr () int { return 5 }
|
||||||
|
func (AttrTextColor) attr () int { return 6 }
|
||||||
|
func (AttrDotColor) attr () int { return 7 }
|
||||||
|
func (AttrFace) attr () int { return 8 }
|
||||||
|
func (AttrAlign) attr () int { return 9 }
|
||||||
231
internal/theme/default/default.go
Normal file
231
internal/theme/default/default.go
Normal file
@@ -0,0 +1,231 @@
|
|||||||
|
package defaultTheme
|
||||||
|
|
||||||
|
import "image/color"
|
||||||
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
import "golang.org/x/image/font/basicfont"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/theme"
|
||||||
|
import dataTheme "git.tebibyte.media/tomo/nasin/internal/theme"
|
||||||
|
|
||||||
|
var colorFocus = color.RGBA { R: 61, G: 128, B: 143, A: 255 }
|
||||||
|
var colorInput = color.RGBA { R: 208, G: 203, B: 150, A: 255 }
|
||||||
|
var colorCarved = color.RGBA { R: 151, G: 160, B: 150, A: 255 }
|
||||||
|
var colorShadow = color.RGBA { R: 57, G: 59, B: 57, A: 255 }
|
||||||
|
var colorInputShadow = color.RGBA { R: 143, G: 146, B: 91, A: 255 }
|
||||||
|
var colorHighlight = color.RGBA { R: 207, G: 215, B: 210, A: 255 }
|
||||||
|
var colorBackground = color.RGBA { R: 169, G: 171, B: 168, A: 255 }
|
||||||
|
var colorCarvedPressed = color.RGBA { R: 129, G: 142, B: 137, A: 255 }
|
||||||
|
var colorForeground = color.Black
|
||||||
|
var colorOutline = color.Black
|
||||||
|
|
||||||
|
var outline = tomo.Border {
|
||||||
|
Width: tomo.I(1),
|
||||||
|
Color: [4]color.Color {
|
||||||
|
colorOutline,
|
||||||
|
colorOutline,
|
||||||
|
colorOutline,
|
||||||
|
colorOutline,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var borderColorEngraved = [4]color.Color { colorShadow, colorHighlight, colorHighlight, colorShadow }
|
||||||
|
var borderColorLifted = [4]color.Color { colorHighlight, colorShadow, colorShadow, colorHighlight }
|
||||||
|
var borderColorInput = [4]color.Color { colorInputShadow, colorInput, colorInput, colorInputShadow }
|
||||||
|
var borderColorFocused = [4]color.Color { colorFocus, colorFocus, colorFocus, colorFocus }
|
||||||
|
|
||||||
|
var rules = []dataTheme.Rule {
|
||||||
|
// *.*[*]
|
||||||
|
dataTheme.Rule {
|
||||||
|
Default: dataTheme.AS (
|
||||||
|
dataTheme.AttrFace { Face: basicfont.Face7x13 },
|
||||||
|
dataTheme.AttrTextColor { Color: theme.ColorForeground },
|
||||||
|
dataTheme.AttrDotColor { Color: theme.ColorAccent },
|
||||||
|
dataTheme.AttrGap { X: 8, Y: 8 },
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
|
// *.Button[*]
|
||||||
|
dataTheme.Rule {
|
||||||
|
Role: theme.R("", "Button", ""),
|
||||||
|
Default: dataTheme.AS (
|
||||||
|
dataTheme.AttrBorder {
|
||||||
|
outline,
|
||||||
|
tomo.Border {
|
||||||
|
Width: tomo.I(1),
|
||||||
|
Color: borderColorLifted,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
dataTheme.AttrPadding(tomo.I(4, 8)),
|
||||||
|
dataTheme.AttrColor { Color: theme.ColorRaised },
|
||||||
|
),
|
||||||
|
Pressed: dataTheme.AS (
|
||||||
|
dataTheme.AttrBorder {
|
||||||
|
outline,
|
||||||
|
tomo.Border {
|
||||||
|
Width: tomo.I(1, 0, 0, 1),
|
||||||
|
Color: borderColorEngraved,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
dataTheme.AttrPadding(tomo.I(5, 8, 4, 9)),
|
||||||
|
dataTheme.AttrColor { Color: colorCarvedPressed },
|
||||||
|
),
|
||||||
|
Focused: dataTheme.AS (
|
||||||
|
dataTheme.AttrBorder {
|
||||||
|
outline,
|
||||||
|
tomo.Border {
|
||||||
|
Width: tomo.I(1),
|
||||||
|
Color: borderColorFocused,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
dataTheme.AttrPadding(tomo.I(4, 8)),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
|
// *.TextInput[*]
|
||||||
|
dataTheme.Rule {
|
||||||
|
Role: theme.R("", "TextInput", ""),
|
||||||
|
Default: dataTheme.AS (
|
||||||
|
dataTheme.AttrBorder {
|
||||||
|
outline,
|
||||||
|
tomo.Border {
|
||||||
|
Width: tomo.I(1),
|
||||||
|
Color: borderColorInput,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
dataTheme.AttrColor { Color: colorInput },
|
||||||
|
dataTheme.AttrPadding(tomo.I(5, 4, 4, 5)),
|
||||||
|
),
|
||||||
|
Focused: dataTheme.AS (
|
||||||
|
dataTheme.AttrBorder {
|
||||||
|
outline,
|
||||||
|
tomo.Border {
|
||||||
|
Width: tomo.I(1),
|
||||||
|
Color: borderColorFocused,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
|
// *.NumberInput[*]
|
||||||
|
dataTheme.Rule {
|
||||||
|
Role: theme.R("", "NumberInput", ""),
|
||||||
|
Default: dataTheme.AS (
|
||||||
|
dataTheme.AttrBorder {
|
||||||
|
outline,
|
||||||
|
tomo.Border {
|
||||||
|
Width: tomo.I(1),
|
||||||
|
Color: borderColorInput,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
dataTheme.AttrColor { Color: colorInput },
|
||||||
|
dataTheme.AttrPadding(tomo.I(5, 4, 4, 5)),
|
||||||
|
),
|
||||||
|
Focused: dataTheme.AS (
|
||||||
|
dataTheme.AttrBorder {
|
||||||
|
outline,
|
||||||
|
tomo.Border {
|
||||||
|
Width: tomo.I(1),
|
||||||
|
Color: borderColorFocused,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
|
// *.Container[outer]
|
||||||
|
dataTheme.Rule {
|
||||||
|
Role: theme.R("", "Container", "outer"),
|
||||||
|
Default: dataTheme.AS (
|
||||||
|
dataTheme.AttrColor { Color: theme.ColorBackground },
|
||||||
|
dataTheme.AttrPadding(tomo.I(8)),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
|
// *.Heading[*]
|
||||||
|
dataTheme.Rule {
|
||||||
|
Role: theme.R("", "Heading", ""),
|
||||||
|
Default: dataTheme.AS (
|
||||||
|
dataTheme.AttrAlign { X: tomo.AlignMiddle, Y: tomo.AlignMiddle },
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
|
// *.Separator[*]
|
||||||
|
dataTheme.Rule {
|
||||||
|
Role: theme.R("", "Separator", ""),
|
||||||
|
Default: dataTheme.AS (
|
||||||
|
dataTheme.AttrBorder {
|
||||||
|
tomo.Border {
|
||||||
|
Width: tomo.I(1),
|
||||||
|
Color: borderColorEngraved,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
|
// *.Slider[*]
|
||||||
|
dataTheme.Rule {
|
||||||
|
Role: theme.R("", "Slider", ""),
|
||||||
|
Default: dataTheme.AS (
|
||||||
|
dataTheme.AttrBorder {
|
||||||
|
outline,
|
||||||
|
tomo.Border {
|
||||||
|
Width: tomo.I(1, 0, 0, 1),
|
||||||
|
Color: borderColorEngraved,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
dataTheme.AttrColor { Color: theme.ColorSunken },
|
||||||
|
dataTheme.AttrPadding(tomo.I(0, 1, 1, 0)),
|
||||||
|
),
|
||||||
|
Focused: dataTheme.AS (
|
||||||
|
dataTheme.AttrBorder {
|
||||||
|
outline,
|
||||||
|
tomo.Border {
|
||||||
|
Width: tomo.I(1),
|
||||||
|
Color: borderColorFocused,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
dataTheme.AttrPadding(tomo.I(0)),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
|
// *.Slider[horizontal]
|
||||||
|
dataTheme.Rule {
|
||||||
|
Role: theme.R("", "Slider", "horizontal"),
|
||||||
|
Default: dataTheme.AS(dataTheme.AttrMinimumSize { X: 48 }),
|
||||||
|
},
|
||||||
|
|
||||||
|
// *.Slider[vertical]
|
||||||
|
dataTheme.Rule {
|
||||||
|
Role: theme.R("", "Slider", "vertical"),
|
||||||
|
Default: dataTheme.AS(dataTheme.AttrMinimumSize { Y: 48 }),
|
||||||
|
},
|
||||||
|
|
||||||
|
// *.SliderHandle[*]
|
||||||
|
dataTheme.Rule {
|
||||||
|
Role: theme.R("", "SliderHandle", ""),
|
||||||
|
Default: dataTheme.AS (
|
||||||
|
dataTheme.AttrBorder {
|
||||||
|
outline,
|
||||||
|
tomo.Border {
|
||||||
|
Width: tomo.I(1),
|
||||||
|
Color: borderColorLifted,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
dataTheme.AttrColor { Color: theme.ColorRaised },
|
||||||
|
dataTheme.AttrMinimumSize { X: 12, Y: 12, },
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Theme returns Wintergreen, the default Tomo theme. It is neutral-gray with
|
||||||
|
// green and turquoise accents.
|
||||||
|
func Theme () theme.Theme {
|
||||||
|
return &dataTheme.Theme {
|
||||||
|
Colors: map[theme.Color] color.Color {
|
||||||
|
theme.ColorBackground: colorBackground,
|
||||||
|
theme.ColorForeground: colorForeground,
|
||||||
|
theme.ColorRaised: colorCarved,
|
||||||
|
theme.ColorSunken: colorCarved,
|
||||||
|
theme.ColorAccent: colorFocus,
|
||||||
|
},
|
||||||
|
Rules: rules,
|
||||||
|
}
|
||||||
|
}
|
||||||
24
internal/theme/missing.go
Normal file
24
internal/theme/missing.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package theme
|
||||||
|
|
||||||
|
import "image"
|
||||||
|
import "image/color"
|
||||||
|
|
||||||
|
type missingTexture int
|
||||||
|
|
||||||
|
func (texture missingTexture) ColorModel () color.Model {
|
||||||
|
return color.RGBAModel
|
||||||
|
}
|
||||||
|
|
||||||
|
func (texture missingTexture) Bounds () image.Rectangle {
|
||||||
|
return image.Rect(0, 0, int(texture), int(texture))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (texture missingTexture) At (x, y int) color.Color {
|
||||||
|
x /= 8
|
||||||
|
y /= 8
|
||||||
|
if (x + y) % 2 == 0 {
|
||||||
|
return color.RGBA { R: 0xFF, B: 0xFF, A: 0xFF }
|
||||||
|
} else {
|
||||||
|
return color.RGBA { A: 0xFF }
|
||||||
|
}
|
||||||
|
}
|
||||||
268
internal/theme/theme.go
Normal file
268
internal/theme/theme.go
Normal file
@@ -0,0 +1,268 @@
|
|||||||
|
// Package theme provides a data-driven theme implementation.
|
||||||
|
package theme
|
||||||
|
|
||||||
|
import "image"
|
||||||
|
import "image/color"
|
||||||
|
import "git.tebibyte.media/tomo/tomo"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/data"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/event"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/input"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/theme"
|
||||||
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||||
|
|
||||||
|
// this is CSS's bastard child
|
||||||
|
|
||||||
|
// Theme allows the use of data to define a visual style.
|
||||||
|
type Theme struct {
|
||||||
|
// Textures maps texture names to image textures.
|
||||||
|
Textures map[string] image.Image
|
||||||
|
textures map[string] canvas.TextureCloser // private texture cache
|
||||||
|
missing canvas.TextureCloser // cache for "missing" texture
|
||||||
|
|
||||||
|
// Rules determines which styles get applied to which Objects.
|
||||||
|
Rules []Rule
|
||||||
|
|
||||||
|
// Colors maps theme.Color values to color.RGBA values.
|
||||||
|
Colors map[theme.Color] color.Color
|
||||||
|
|
||||||
|
// This type does not handle icons, and as such, a special icon theme
|
||||||
|
// must be separately specified.
|
||||||
|
IconTheme
|
||||||
|
}
|
||||||
|
|
||||||
|
// IconTheme implements the part of theme.Theme that handles icons.
|
||||||
|
type IconTheme interface {
|
||||||
|
// Icon returns a texture of the corresponding icon ID.
|
||||||
|
Icon (theme.Icon, theme.IconSize) canvas.Texture
|
||||||
|
// MimeIcon returns an icon corresponding to a MIME type.
|
||||||
|
MimeIcon (data.Mime, theme.IconSize) canvas.Texture
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rule describes under what circumstances should certain style attributes be
|
||||||
|
// active.
|
||||||
|
type Rule struct {
|
||||||
|
Role theme.Role
|
||||||
|
Default AttrSet
|
||||||
|
Hovered AttrSet
|
||||||
|
Pressed AttrSet
|
||||||
|
Focused AttrSet
|
||||||
|
}
|
||||||
|
|
||||||
|
// AttrSet is a set of attributes wherein only one/zero of each attribute type
|
||||||
|
// can exist. I deserve to be imprisoned for the way I made this work (look in
|
||||||
|
// attribute.go). Its zero value can be used safely, and you can copy it if you
|
||||||
|
// want, but it will point to the same set of attributes.
|
||||||
|
type AttrSet struct {
|
||||||
|
set map[int] Attr
|
||||||
|
}
|
||||||
|
|
||||||
|
// AS builds an AttrSet out of a vararg list of Attr values.
|
||||||
|
func AS (attrs ...Attr) AttrSet {
|
||||||
|
set := AttrSet { }
|
||||||
|
set.Add(attrs...)
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add adds attributes to the set.
|
||||||
|
func (this *AttrSet) Add (attrs ...Attr) {
|
||||||
|
this.ensure()
|
||||||
|
for _, attr := range attrs {
|
||||||
|
this.set[attr.attr()] = attr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MergeUnder takes attributes from another set and adds them if they don't
|
||||||
|
// already exist in this one.
|
||||||
|
func (this *AttrSet) MergeUnder (other AttrSet) {
|
||||||
|
this.ensure()
|
||||||
|
if other.set == nil { return }
|
||||||
|
for _, attr := range other.set {
|
||||||
|
if _, exists := this.set[attr.attr()]; !exists {
|
||||||
|
this.Add(attr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MergeOver takes attributes from another set and adds them, overriding this
|
||||||
|
// one.
|
||||||
|
func (this *AttrSet) MergeOver (other AttrSet) {
|
||||||
|
this.ensure()
|
||||||
|
if other.set == nil { return }
|
||||||
|
for _, attr := range other.set {
|
||||||
|
this.Add(attr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *AttrSet) ensure () {
|
||||||
|
if this.set == nil { this.set = make(map[int] Attr) }
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Theme) execute (object tomo.Object, set AttrSet) {
|
||||||
|
box := object.GetBox()
|
||||||
|
|
||||||
|
for _, attr := range set.set {
|
||||||
|
switch attr := attr.(type) {
|
||||||
|
case AttrColor:
|
||||||
|
box.SetColor(attr.Color)
|
||||||
|
case AttrTexture:
|
||||||
|
box.SetTexture(this.texture(string(attr)))
|
||||||
|
case AttrBorder:
|
||||||
|
box.SetBorder([]tomo.Border(attr)...)
|
||||||
|
case AttrMinimumSize:
|
||||||
|
box.SetMinimumSize(image.Point(attr))
|
||||||
|
case AttrPadding:
|
||||||
|
box.SetPadding(tomo.Inset(attr))
|
||||||
|
case AttrGap:
|
||||||
|
if box, ok := box.(tomo.ContainerBox); ok {
|
||||||
|
box.SetGap(image.Point(attr))
|
||||||
|
}
|
||||||
|
case AttrTextColor:
|
||||||
|
if box, ok := box.(tomo.TextBox); ok {
|
||||||
|
box.SetTextColor(attr.Color)
|
||||||
|
}
|
||||||
|
case AttrDotColor:
|
||||||
|
if box, ok := box.(tomo.TextBox); ok {
|
||||||
|
box.SetDotColor(attr.Color)
|
||||||
|
}
|
||||||
|
case AttrFace:
|
||||||
|
if box, ok := box.(tomo.TextBox); ok {
|
||||||
|
box.SetFace(attr)
|
||||||
|
}
|
||||||
|
case AttrAlign:
|
||||||
|
if box, ok := box.(tomo.ContentBox); ok {
|
||||||
|
box.SetAlign(attr.X, attr.Y)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("bug: nasin/internal/theme.Theme: unexpected attribute")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Theme) texture (name string) canvas.Texture {
|
||||||
|
this.ensureTextureCache()
|
||||||
|
if texture, ok := this.textures[name]; ok {
|
||||||
|
return texture
|
||||||
|
}
|
||||||
|
if this.Textures == nil {
|
||||||
|
if source, ok := this.Textures[name]; ok {
|
||||||
|
texture := tomo.NewTexture(source)
|
||||||
|
this.textures[name] = texture
|
||||||
|
return texture
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.missingTexture()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Theme) missingTexture () canvas.Texture {
|
||||||
|
if this.missing == nil {
|
||||||
|
this.missing = tomo.NewTexture(missingTexture(16))
|
||||||
|
}
|
||||||
|
return this.missing
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Theme) ensureTextureCache () {
|
||||||
|
if this.textures == nil { this.textures = make(map[string] canvas.TextureCloser) }
|
||||||
|
}
|
||||||
|
|
||||||
|
// setsFor builds flattened attr sets for a specific role based on the rules list
|
||||||
|
func (this *Theme) setsFor (role theme.Role) (defaul, hovered, pressed, focused AttrSet) {
|
||||||
|
for _, current := range this.Rules {
|
||||||
|
// check for a match
|
||||||
|
packageMatch := current.Role.Package == role.Package || current.Role.Package == ""
|
||||||
|
objectMatch := current.Role.Object == role.Object || current.Role.Object == ""
|
||||||
|
variantMatch := current.Role.Variant == role.Variant || current.Role.Variant == ""
|
||||||
|
if packageMatch && objectMatch && variantMatch {
|
||||||
|
// if found, merge and override
|
||||||
|
defaul.MergeOver(current.Default)
|
||||||
|
hovered.MergeOver(current.Hovered)
|
||||||
|
pressed.MergeOver(current.Pressed)
|
||||||
|
focused.MergeOver(current.Focused)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// hovered and pressed are mutually exclusive states, so we compress
|
||||||
|
// them with the default state.
|
||||||
|
hovered.MergeUnder(defaul)
|
||||||
|
pressed.MergeUnder(defaul)
|
||||||
|
return defaul, hovered, pressed, focused
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Theme) Apply (object tomo.Object, role theme.Role) event.Cookie {
|
||||||
|
pressed := false
|
||||||
|
hovered := false
|
||||||
|
box := object.GetBox()
|
||||||
|
|
||||||
|
defaultSet, hoveredSet, pressedSet, focusedSet := this.setsFor(role)
|
||||||
|
|
||||||
|
updateStyle := func () {
|
||||||
|
if pressed {
|
||||||
|
this.execute(object, pressedSet)
|
||||||
|
} else if hovered {
|
||||||
|
this.execute(object, hoveredSet)
|
||||||
|
} else {
|
||||||
|
this.execute(object, defaultSet)
|
||||||
|
}
|
||||||
|
if box.Focused() && !pressed {
|
||||||
|
this.execute(object, focusedSet)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updateStyle()
|
||||||
|
|
||||||
|
return event.MultiCookie (
|
||||||
|
box.OnFocusEnter(updateStyle),
|
||||||
|
box.OnFocusLeave(updateStyle),
|
||||||
|
box.OnMouseDown(func (button input.Button) {
|
||||||
|
if button != input.ButtonLeft { return }
|
||||||
|
pressed = true
|
||||||
|
updateStyle()
|
||||||
|
}),
|
||||||
|
box.OnMouseUp(func (button input.Button) {
|
||||||
|
if button != input.ButtonLeft { return }
|
||||||
|
pressed = false
|
||||||
|
updateStyle()
|
||||||
|
}),
|
||||||
|
box.OnMouseEnter(func () {
|
||||||
|
hovered = true
|
||||||
|
updateStyle()
|
||||||
|
}),
|
||||||
|
box.OnMouseLeave(func () {
|
||||||
|
hovered = false
|
||||||
|
updateStyle()
|
||||||
|
}))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Theme) RGBA (c theme.Color) (r, g, b, a uint32) {
|
||||||
|
if this.Colors == nil { return 0xFFFF, 0, 0xFFFF, 0xFFFF }
|
||||||
|
color, ok := this.Colors[c]
|
||||||
|
if !ok { return 0xFFFF, 0, 0xFFFF, 0xFFFF }
|
||||||
|
return color.RGBA()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Theme) Icon (icon theme.Icon, size theme.IconSize) canvas.Texture {
|
||||||
|
if this.IconTheme == nil {
|
||||||
|
return this.missingTexture()
|
||||||
|
} else {
|
||||||
|
return this.IconTheme.Icon(icon, size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Theme) MimeIcon (mime data.Mime, size theme.IconSize) canvas.Texture {
|
||||||
|
if this.IconTheme == nil {
|
||||||
|
return this.missingTexture()
|
||||||
|
} else {
|
||||||
|
return this.IconTheme.MimeIcon(mime, size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes all cached textures this theme has open. Do not call this while
|
||||||
|
// the theme is in use.
|
||||||
|
func (this *Theme) Close () error {
|
||||||
|
this.missing.Close()
|
||||||
|
this.missing = nil
|
||||||
|
for _, texture := range this.textures {
|
||||||
|
texture.Close()
|
||||||
|
}
|
||||||
|
this.textures = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user