Separate icon functionality out of Style and into Icons

This commit is contained in:
Sasha Koshka 2024-06-07 18:27:26 -04:00
parent f0c092be8a
commit d587e39506
2 changed files with 34 additions and 26 deletions

38
icon.go
View File

@ -382,12 +382,42 @@ const (
// Texture returns a texture of the corresponding icon ID.
func (id Icon) Texture (size IconSize) canvas.Texture {
if style == nil { return nil }
return style.Icon(id, size)
if icons == nil { return nil }
return icons.Icon(id, size)
}
// MimeIcon returns an icon corresponding to a MIME type.
func MimeIcon (mime data.Mime, size IconSize) canvas.Texture {
if style == nil { return nil }
return style.MimeIcon(mime, size)
if icons == nil { return nil }
return icons.MimeIcon(mime, size)
}
// Icons holds a set of icon textures.
type Icons interface {
// A word on textures:
//
// Because textures can be linked to some resource that is outside of
// the control of Go's garbage collector, methods of Icons must not
// allocate new copies of a texture each time they are called. It is
// fine to lazily load textures and save them for later use, but the
// same texture must never be allocated multiple times as this could
// cause a memory leak.
//
// As such, textures returned by these methods must be protected.
// Icon returns a texture of the corresponding icon ID. If there is no
// suitable option, it should return nil.
Icon (Icon, IconSize) canvas.Texture
// MimeIcon returns a texture of an icon corresponding to a MIME type.
// If there is no suitable specific option, it should return a more
// generic icon or a plain file icon.
MimeIcon (data.Mime, IconSize) canvas.Texture
}
var icons Icons
// SetIcons sets the icon set.
func SetIcons (icns Icons) {
icons = icns
}

View File

@ -1,9 +1,7 @@
package tomo
import "fmt"
import "git.tebibyte.media/tomo/tomo/data"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/canvas"
// Role describes the role of an object.
type Role struct {
@ -66,17 +64,6 @@ func (id Color) RGBA () (r, g, b, a uint32) {
// Style can apply a visual style to different objects.
type Style interface {
// A word on textures:
//
// Because textures can be linked to some resource that is outside of
// the control of Go's garbage collector, methods of Theme must not
// allocate new copies of a texture each time they are called. It is
// fine to lazily load textures and save them for later use, but the
// same texture must never be allocated multiple times as this could
// cause a memory leak.
//
// As such, textures returned by these methods must be protected.
// Apply applies the theme to the given object, according to its role.
// This may register event listeners with the given object; closing the
// returned cookie must remove them.
@ -84,15 +71,6 @@ type Style interface {
// RGBA returns the RGBA values of the corresponding color ID.
RGBA (Color) (r, g, b, a uint32)
// Icon returns a texture of the corresponding icon ID. If there is no
// suitable option, it should return nil.
Icon (Icon, IconSize) canvas.Texture
// MimeIcon returns a texture of an icon corresponding to a MIME type.
// If there is no suitable specific option, it should return a more
// generic icon or a plain file icon.
MimeIcon (data.Mime, IconSize) canvas.Texture
}
var style Style