From e7c5b2d5b9234eda6191985e68f382d32c6bdb14 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 29 Aug 2023 21:28:39 -0400 Subject: [PATCH] WIP icon loading --- icons.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ theme.go | 26 +------------------------- 2 files changed, 56 insertions(+), 25 deletions(-) create mode 100644 icons.go diff --git a/icons.go b/icons.go new file mode 100644 index 0000000..b355844 --- /dev/null +++ b/icons.go @@ -0,0 +1,55 @@ +package aluminum + +import "os" +import "fs" +import "fmt" +import "image" +import "image/png" +import "git.tebibyte.media/tomo/tomo/data" +import "git.tebibyte.media/tomo/tomo/theme" +import "git.tebibyte.media/tomo/tomo/input" +import "git.tebibyte.media/tomo/tomo/canvas" + +type iconSet struct { + fs fs.FS + icons: map[theme.IconSize] map[string] canvas.Texture +} + +func (this *iconSet) get (size theme.IconSize, name string) canvas.Texture { + texture := this.icons[size][name] + if texture != nil { + return texture + } else { + texture = nil + defer func () { this.icons[size][name] = texture } () + + file, err := this.fs.Open(fmt.Sprint(size, "/", name, ".png")) + if err != nil { return nil } + defer file.Close() + image, _, err := image.Decode(file) + if err != nil { return nil } + texture = tomo.NewTexture(image) + } +} + +func (this *iconSet) Icon (id theme.Icon, size theme.IconSize) canvas.Texture { + return this.get(size, fmt.Sprint("actions/", id)) +} + +func (this *iconSet) MimeIcon (mime data.Mime, size theme.IconSize) canvas.Texture { + texture := this.get(size, fmt.Sprint("mime/", mime)) + if texture != nil { + return texture + } else { + return this.get(size, fmt.Sprint("mime-generic/", mime.Type)) + } +} + +func (this *iconSet) ApplicationIcon (id theme.ApplicationIcon, size theme.IconSize) canvas.Texture { + texture := this.get(size, fmt.Sprint("app/", id.Name)) + if texture != nil { + return texture + } else { + return this.get(size, fmt.Sprint("app-generic/", app.Role)) + } +} diff --git a/theme.go b/theme.go index f426663..870df5f 100644 --- a/theme.go +++ b/theme.go @@ -47,7 +47,7 @@ var gutterColor = hex(0xbfc6d1FF) var gutterColorHovered = hex(0xc5cbd6FF) type Theme struct { - initialized bool + iconSet } func (this *Theme) RGBA (id theme.Color) (r, g, b, a uint32) { @@ -221,27 +221,3 @@ func (this *Theme) Apply (object tomo.Object, role theme.Role) event.Cookie { return event.MultiCookie() } -func (this *Theme) Icon (theme.Icon, theme.IconSize) canvas.Texture { - this.ensure() - // TODO - return nil -} - -func (this *Theme) MimeIcon (data.Mime, theme.IconSize) canvas.Texture { - this.ensure() - // TODO - return nil -} - -func (this *Theme) ApplicationIcon (theme.ApplicationIcon, theme.IconSize) canvas.Texture { - this.ensure() - // TODO - return nil -} - -func (this *Theme) ensure () { - if this.initialized { return } - this.initialized = true - - // TODO -}