16 Commits

23 changed files with 194 additions and 107 deletions

View File

@@ -128,8 +128,9 @@ func (role ApplicationRole) Icon () tomo.Icon {
}
}
// RunApplication is like tomo.Run, but runs an application. If something fails
// to initialize, an error is written to the standard logger.
// RunApplication is like tomo.Run, but runs an application. It automatically
// sets up a backend. If something fails to initialize, an error is written to
// the standard logger.
func RunApplication (application Application) {
// TODO: see #4
@@ -138,20 +139,22 @@ func RunApplication (application Application) {
}
flag.Parse()
err := registrar.RegisterBackend()
reg := new(registrar.Registrar)
backend, err := reg.SetBackend()
if err != nil { log.Fatalln("nasin: could not register backend:", err) }
err = tomo.Run(func () {
err := registrar.SetTheme()
err = reg.SetTheme()
if err != nil { log.Fatalln("nasin: could not set theme:", err) }
err = registrar.SetIconSet()
err = reg.SetIconSet()
if err != nil { log.Fatalln("nasin: could not set icon set:", err) }
err = reg.SetFaceSet()
if err != nil { log.Fatalln("nasin: could not set face set:", err) }
err = application.Init()
if err != nil { log.Fatalln("nasin: could not run application:", err) }
// open URLs
args := flag.Args()
applicationOpenUrls(application, args...)
})
err = backend.Run()
if err != nil { log.Fatalln("nasin: could not run application:", err) }
}

10
go.mod
View File

@@ -4,16 +4,13 @@ go 1.22.2
require (
git.tebibyte.media/sashakoshka/goparse v0.2.0
git.tebibyte.media/tomo/backend v0.5.1
git.tebibyte.media/tomo/objects v0.20.1
git.tebibyte.media/tomo/tomo v0.41.1
git.tebibyte.media/tomo/backend v0.6.1
git.tebibyte.media/tomo/objects v0.21.0
git.tebibyte.media/tomo/tomo v0.45.0
git.tebibyte.media/tomo/xdg v0.1.0
github.com/flopp/go-findfont v0.1.0
golang.org/x/image v0.11.0
)
replace git.tebibyte.media/tomo/tomo => /home/sashakoshka/Repos/tebibyte/tomo/tomo
require (
git.tebibyte.media/tomo/typeset v0.7.1 // indirect
git.tebibyte.media/tomo/xgbkb v1.0.1 // indirect
@@ -21,5 +18,4 @@ require (
github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966 // indirect
github.com/jezek/xgb v1.1.1 // indirect
github.com/jezek/xgbutil v0.0.0-20231116234834-47f30c120111 // indirect
golang.org/x/text v0.12.0 // indirect
)

13
go.sum
View File

@@ -1,10 +1,12 @@
git.tebibyte.media/sashakoshka/goparse v0.2.0 h1:uQmKvOCV2AOlCHEDjg9uclZCXQZzq2PxaXfZ1aIMiQI=
git.tebibyte.media/sashakoshka/goparse v0.2.0/go.mod h1:tSQwfuD+EujRoKr6Y1oaRy74ZynatzkRLxjE3sbpCmk=
git.tebibyte.media/sashakoshka/xgbkb v1.0.0/go.mod h1:pNcE6TRO93vHd6q42SdwLSTTj25L0Yzggz7yLe0JV6Q=
git.tebibyte.media/tomo/backend v0.5.1 h1:u3DLVcNWNdQsIxAEGcZ+kGG7zuew8tpPbyEFBx8ehjM=
git.tebibyte.media/tomo/backend v0.5.1/go.mod h1:urnfu+D56Q9AOCZ/qp5YqkmlRRTIB5p9RbzBN7yIibQ=
git.tebibyte.media/tomo/objects v0.20.1 h1:dCMMzaHmj7w016XtTb8QWvKMHVK2Fru/2a70NqApzHQ=
git.tebibyte.media/tomo/objects v0.20.1/go.mod h1:hbBlQem9cNOt/G2rBvH/C94FT1oWgbtEftwg6ield9U=
git.tebibyte.media/tomo/backend v0.6.1 h1:TVbvfbcMrF8YAVGsXPQNQLCam3xuOWJmZA0B+op0ig0=
git.tebibyte.media/tomo/backend v0.6.1/go.mod h1:7gl0Z1V8Vcns41pXIpQt1FYlANrQf5bCboxMjTCCrgc=
git.tebibyte.media/tomo/objects v0.21.0 h1:exFbzQPQhGIVQK5BCDg69ZV96zMamV50G4GRsnK+yfA=
git.tebibyte.media/tomo/objects v0.21.0/go.mod h1:ljnNcCuNfvcYLHmQrEU7LuG0OvQiAVDCXU+ajspq+TI=
git.tebibyte.media/tomo/tomo v0.45.0 h1:fQH0WIPidW275hOq9dE6R7p064xG1RGx2QU68Avlr84=
git.tebibyte.media/tomo/tomo v0.45.0/go.mod h1:WrtilgKB1y8O2Yu7X4mYcRiqOlPR8NuUnoA/ynkQWrs=
git.tebibyte.media/tomo/typeset v0.7.1 h1:aZrsHwCG5ZB4f5CruRFsxLv5ezJUCFUFsQJJso2sXQ8=
git.tebibyte.media/tomo/typeset v0.7.1/go.mod h1:PwDpSdBF3l/EzoIsa2ME7QffVVajnTHZN6l3MHEGe1g=
git.tebibyte.media/tomo/xdg v0.1.0 h1:6G2WYPPiM2IXleCpKKHuJA34BxumwNWuLsUoX3yu5zA=
@@ -15,8 +17,6 @@ github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 h1:1qlsVAQJ
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298/go.mod h1:D+QujdIlUNfa0igpNMk6UIvlb6C252URs4yupRUV4lQ=
github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966 h1:lTG4HQym5oPKjL7nGs+csTgiDna685ZXjxijkne828g=
github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966/go.mod h1:Mid70uvE93zn9wgF92A/r5ixgnvX8Lh68fxp9KQBaI0=
github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU=
github.com/flopp/go-findfont v0.1.0/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw=
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
@@ -50,7 +50,6 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=

View File

@@ -0,0 +1,35 @@
package fallbackFaces
import "golang.org/x/image/font"
import "git.tebibyte.media/tomo/tomo"
import "golang.org/x/image/font/basicfont"
import "git.tebibyte.media/tomo/backend/style"
type faceSet struct {
regular font.Face
bold font.Face
italic font.Face
boldItalic font.Face
}
// New creates a new fallback face set.
func New () style.FaceSet {
// TODO maybe pre-generate different variations of this face
return &faceSet {
regular: basicfont.Face7x13,
bold: basicfont.Face7x13,
italic: basicfont.Face7x13,
boldItalic: basicfont.Face7x13,
}
}
func (this *faceSet) Face (face tomo.Face) font.Face {
bold := face.Weight >= 500
italic := face.Italic >= 0.1 || face.Slant >= 0.1
switch {
case bold && italic: return this.boldItalic
case bold: return this.bold
case italic: return this.italic
default: return this.regular
}
}

View File

@@ -412,23 +412,23 @@ func generateSource (data []byte, width int) map[tomo.Icon] canvas.Texture {
return source
}
type iconTheme struct {
type iconSet struct {
texturesSmall map[tomo.Icon] canvas.Texture
texturesLarge map[tomo.Icon] canvas.Texture
}
// New creates a new fallback icon theme.
// New creates a new fallback icon set.
func New () style.IconSet {
return new(iconTheme)
return new(iconSet)
}
func (this *iconTheme) ensure () {
func (this *iconSet) ensure () {
if this.texturesSmall != nil { return }
this.texturesSmall = generateSource(atlasSmallBytes, 16)
this.texturesLarge = generateSource(atlasLargeBytes, 32)
}
func (this *iconTheme) selectSource (size tomo.IconSize) map[tomo.Icon] canvas.Texture {
func (this *iconSet) selectSource (size tomo.IconSize) map[tomo.Icon] canvas.Texture {
if size == tomo.IconSizeSmall {
return this.texturesSmall
} else {
@@ -436,7 +436,7 @@ func (this *iconTheme) selectSource (size tomo.IconSize) map[tomo.Icon] canvas.T
}
}
func (this *iconTheme) Icon (icon tomo.Icon, size tomo.IconSize) canvas.Texture {
func (this *iconSet) Icon (icon tomo.Icon, size tomo.IconSize) canvas.Texture {
this.ensure()
source := this.selectSource(size)
if texture, ok := source[icon]; ok {
@@ -445,7 +445,7 @@ func (this *iconTheme) Icon (icon tomo.Icon, size tomo.IconSize) canvas.Texture
return nil
}
func (this *iconTheme) MimeIcon (mime data.Mime, size tomo.IconSize) canvas.Texture {
func (this *iconSet) MimeIcon (mime data.Mime, size tomo.IconSize) canvas.Texture {
this.ensure()
source := this.selectSource(size)
if mime == data.M("inode", "directory") {

View File

@@ -7,23 +7,29 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/backend/x"
import "git.tebibyte.media/sashakoshka/goparse"
import "git.tebibyte.media/tomo/nasin/internal/icons/xdg"
import "git.tebibyte.media/tomo/nasin/internal/style/tss"
import "git.tebibyte.media/tomo/nasin/internal/styles/tss"
import "git.tebibyte.media/tomo/nasin/internal/icons/fallback"
import "git.tebibyte.media/tomo/nasin/internal/style/fallback"
import "git.tebibyte.media/tomo/nasin/internal/styles/fallback"
import "git.tebibyte.media/tomo/nasin/internal/faces/fallback"
func RegisterBackend () error {
tomo.Register(1, x.New)
return nil
type Registrar struct {
backend *x.Backend
}
func SetTheme () error {
// TODO eventually get rid of this when we make a file format for
// storing visual styles
func (this *Registrar) SetBackend () (tomo.Backend, error) {
backend, err := x.New()
if err != nil { return nil, err }
this.backend = backend.(*x.Backend)
tomo.SetBackend(backend)
return backend, nil
}
func (this *Registrar) SetTheme () error {
styleSheetName := os.Getenv("TOMO_STYLE_SHEET")
if styleSheetName != "" {
styl, _, err := tss.LoadFile(styleSheetName)
if err == nil {
tomo.SetStyle(styl)
this.backend.SetStyle(styl)
return nil
} else {
log.Printf (
@@ -33,11 +39,11 @@ func SetTheme () error {
}
styl, _ := fallbackStyle.New()
tomo.SetStyle(styl)
this.backend.SetStyle(styl)
return nil
}
func SetIconSet () error {
func (this *Registrar) SetIconSet () error {
iconSet := fallbackIcons.New()
iconSetName := os.Getenv("TOMO_XDG_ICON_THEME")
if iconSetName != "" {
@@ -49,6 +55,14 @@ func SetIconSet () error {
}
}
tomo.SetIconSet(iconSet)
this.backend.SetIconSet(iconSet)
return nil
}
func (this *Registrar) SetFaceSet () error {
// TODO replace this with something that uses findfont, and caches and
// refcounts the faces
faceSet := fallbackFaces.New()
this.backend.SetFaceSet(faceSet)
return nil
}

View File

@@ -1,5 +1,5 @@
// Colors
$ColorDot = #7391c0;
$ColorDot = #7391c080;
$ColorAccent = #5f8bc4;
$ColorHighlight = #5f8bc4;
$ColorBackground = #d4d4d4;
@@ -36,7 +36,6 @@ $BorderOuterShadow = #a4afc0 / 0 1 1 0;
TextColor: $ColorForeground;
DotColor: $ColorDot;
Gap: 8;
Face: "DMMono-Regular.ttf" 11;
}
*.Button {
@@ -150,6 +149,11 @@ $BorderOuterShadow = #a4afc0 / 0 1 1 0;
Padding: 0;
}
*.Checkbox[checked] {
Texture: "assets/checkbox-checked.png";
TextureMode: center;
}
*.MenuItem {
Padding: 4;
Gap: 4;

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 B

View File

Before

Width:  |  Height:  |  Size: 409 B

After

Width:  |  Height:  |  Size: 409 B

View File

@@ -1,39 +1,36 @@
package tss
import "os"
import "io"
import "fmt"
import "image"
import "errors"
import "image/color"
import "golang.org/x/image/font"
import "github.com/flopp/go-findfont"
import _ "image/png"
import "path/filepath"
import "git.tebibyte.media/tomo/tomo"
import "golang.org/x/image/font/opentype"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/backend/style"
type styleBuilder struct {
sheet Sheet
fonts map[string] *opentype.Font
faces map[faceKey] font.Face
}
type faceKey struct {
name string
size int
workingDir string
textures map[string] canvas.TextureCloser
}
// BuildStyle builds a Tomo style from the specified sheet. Resources associated
// with it (such as textures) can be freed by closing the returned cookie.
func BuildStyle (sheet Sheet) (*tomo.Style, event.Cookie, error) {
func BuildStyle (sheet Sheet) (*style.Style, event.Cookie, error) {
builder := &styleBuilder {
sheet: sheet,
fonts: make(map[string] *opentype.Font),
faces: make(map[faceKey] font.Face),
workingDir: filepath.Dir(sheet.Path),
}
return builder.build()
}
func (this *styleBuilder) build () (*tomo.Style, event.Cookie, error) {
func (this *styleBuilder) build () (*style.Style, event.Cookie, error) {
// ensure the sheet is flattened (all vars are resolved)
err := this.sheet.Flatten()
if err != nil { return nil, nil, err }
@@ -48,9 +45,10 @@ func (this *styleBuilder) build () (*tomo.Style, event.Cookie, error) {
return color.RGBA { R: 255, B: 255, A: 255 }
}
// construct style and get colors
cookies := []event.Cookie { }
style := &tomo.Style {
Rules: make([]tomo.Rule, len(this.sheet.Rules)),
sty := &style.Style {
Rules: make([]style.Rule, len(this.sheet.Rules)),
Colors: map[tomo.Color] color.Color {
tomo.ColorBackground: getColor("ColorBackground"),
tomo.ColorForeground: getColor("ColorForeground"),
@@ -60,14 +58,15 @@ func (this *styleBuilder) build () (*tomo.Style, event.Cookie, error) {
},
}
// build style rules from the sheet's rule slice
for index, rule := range this.sheet.Rules {
styleRule := tomo.Rule {
styleRule := style.Rule {
Role: tomo.Role {
Package: rule.Selector.Package,
Object: rule.Selector.Object,
},
Tags: rule.Selector.Tags,
Set: make(tomo.AttrSet),
Set: make(style.AttrSet),
}
for name, attr := range rule.Attrs {
styleAttr, cookie, err := this.buildAttr(name, attr)
@@ -77,13 +76,15 @@ func (this *styleBuilder) build () (*tomo.Style, event.Cookie, error) {
cookies = append(cookies, cookie)
}
}
style.Rules[index] = styleRule
sty.Rules[index] = styleRule
}
// TODO include all the faces in this.faces (ONCE EACH) in the
// multicookie
// add each texture to the cookies list
for _, texture := range this.textures {
cookies = append(cookies, closerCookie { Closer: texture })
}
return style, event.MultiCookie(cookies...), nil
return sty, event.MultiCookie(cookies...), nil
}
func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error) {
@@ -173,7 +174,14 @@ func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr,
return nil, nil, errWrongType()
case "Texture":
// TODO load image from file
err := expectSingleSingle()
if err != nil { return nil, nil, err }
if name, ok := attr[0][0].(ValueString); ok {
texture, err := this.texture(string(name))
if err != nil { return nil, nil, err }
return tomo.ATexture(texture), nil, nil
}
return nil, nil, errWrongType()
case "TextureMode":
err := expectSingleSingle()
@@ -238,6 +246,7 @@ func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr,
return nil, nil, errWrongType()
case "Face":
// TODO support weight, italic, slant
err := expectSingle()
if err != nil { return nil, nil, err }
list := attr[0]
@@ -250,9 +259,10 @@ func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr,
if !ok { return nil, nil, errWrongType() }
size, ok := list[1].(ValueNumber)
if !ok { return nil, nil, errWrongType() }
face, err := this.face(string(name), int(size))
if err != nil { return nil, nil, err }
return tomo.AFace(face), nil, nil
return tomo.AFace(tomo.Face {
Font: string(name),
Size: float64(size),
}), nil, nil
case "Wrap":
err := expectSingleSingle()
@@ -311,34 +321,17 @@ func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr,
return nil, nil, errors.New(fmt.Sprintf("unimplemented attribute name %s", name))
}
func (this *styleBuilder) face (name string, size int) (font.Face, error) {
key := faceKey { name: name, size: size }
face, ok := this.faces[key]
if ok { return face, nil }
font, ok := this.fonts[name]
if !ok {
path, err := findfont.Find(name)
if err != nil { return nil, err }
func (this *styleBuilder) texture (path string) (canvas.TextureCloser, error) {
path = filepath.Join(this.workingDir, path)
if texture, ok := this.textures[path]; ok {
return texture, nil
}
file, err := os.Open(path)
if err != nil { return nil, err }
defer file.Close()
fnt, err := opentype.ParseReaderAt(file)
if err != nil {
return nil, errors.New(fmt.Sprintf (
"could not load font %s: %v", path, err))
}
font = fnt
this.fonts[name] = font
}
face, err := opentype.NewFace(font, &opentype.FaceOptions {
Size: float64(size),
DPI: 96, // TODO: replace this with the actual DPI
})
rawImage, _, err := image.Decode(file)
if err != nil { return nil, err }
this.faces[key] = face
return face, nil
return tomo.NewTexture(rawImage), nil
}
func buildAttrBorder (attr []ValueList) (tomo.Attr, error) {
@@ -406,3 +399,9 @@ func copyBorderValue[T any, U ~[]T] (destination, source U) bool {
}
}
type closerCookie struct {
io.Closer
}
func (cookie closerCookie) Close () {
cookie.Closer.Close()
}

View File

@@ -1,10 +1,11 @@
package tss
import "os"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/backend/style"
type Sheet struct {
Path string
Variables map[string] ValueList
Rules []Rule
flat bool
@@ -65,7 +66,7 @@ func (ValueCut) value () { }
// LoadFile loads the stylesheet from the specified file. This may return a
// parse.Error, so use parse.Format to print it.
func LoadFile (name string) (*tomo.Style, event.Cookie, error) {
func LoadFile (name string) (*style.Style, event.Cookie, error) {
// TODO check cache for gobbed sheet. if the cache is nonexistent or
// invalid, then open/load/cache.
@@ -75,5 +76,6 @@ func LoadFile (name string) (*tomo.Style, event.Cookie, error) {
sheet, err := Parse(Lex(name, file))
if err != nil { return nil, nil, err }
sheet.Path = name
return BuildStyle(sheet)
}

View File

@@ -0,0 +1,35 @@
# Happen to use Micro to edit text?
# Drop this in ~/.config/micro/syntax and get syntax highlighting for TSS files!
filetype: tss
detect:
filename: "\\.tss$"
rules:
- type: "\\b([A-Z][a-zA-Z0-9]*).*:"
- identifier.var: "\\$[a-zA-Z0-9]*\\b"
- identifier.class: "(\\*|[a-z][a-zA-Z0-9]*)\\.(\\*|[A-Z][a-zA-Z0-9]*)"
- constant: "\\b(tile|center)\\b"
- constant: "\\b(start|middle|end|even)\\b"
- constant.bool: "\\b(true|false)\\b"
- special: "(\\/|,|\\;|:|\\.)"
- symbol.operator: "(=|\\*)"
- symbol.brackets: "(\\{\\[|\\}\\])"
- comment:
start: "//"
end: "$"
rules:
- todo: "(TODO|XXX|FIXME|BUG):?"
- constant.string:
start: "\""
end: "\""
skip: "\\\\."
rules:
- constant.specialChar: "\\\\[abfnrtv'\\\"\\\\]"
- constant.specialChar: "\\\\([0-7]{3})"
- constant.number: "\\b[0-9][0-9.]*\\b"
- constant.string: "\\B#[0-9a-fA-F]*"