Fallback icon set now has general MIME icons support

Specifically, it supports all the XDG x-generic icons.
This commit is contained in:
Sasha Koshka 2024-08-19 03:18:37 -04:00
parent 894f34e3ef
commit 4fa29f2719
3 changed files with 58 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import _ "embed"
import _ "image/png" import _ "image/png"
import "git.tebibyte.media/tomo/tomo" import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/data" import "git.tebibyte.media/tomo/tomo/data"
import "git.tebibyte.media/tomo/nasin/internal/util"
import "git.tebibyte.media/tomo/tomo/canvas" import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/backend/style" import "git.tebibyte.media/tomo/backend/style"
@ -14,6 +15,26 @@ var atlasSmallBytes []byte
//go:embed assets/icons-large.png //go:embed assets/icons-large.png
var atlasLargeBytes []byte var atlasLargeBytes []byte
const (
iconApplicationXGeneric = tomo.Icon("application/x-generic")
iconApplicationXExecutable = tomo.Icon("application/x-executable")
iconAudioXGeneric = tomo.Icon("audio/x-generic")
iconFontXGeneric = tomo.Icon("font/x-generic")
iconImageXGeneric = tomo.Icon("image/x-generic")
iconModelXGeneric = tomo.Icon("model/x-generic")
iconPackageXGeneric = tomo.Icon("package/x-generic")
iconTextXGeneric = tomo.Icon("text/x-generic")
iconTextHtml = tomo.Icon("text/html")
iconTextXGenericTemplate = tomo.Icon("text/x-generic-template")
iconTextXScript = tomo.Icon("text/x-script")
iconVideoXGeneric = tomo.Icon("video/x-generic")
iconXOfficeAddressBook = tomo.Icon("x-office-address-book")
iconXOfficeCalendar = tomo.Icon("x-office-calendar")
iconXOfficeDocument = tomo.Icon("x-office-document")
iconXOfficePresentation = tomo.Icon("x-office-presentation")
iconXOfficeSpreadsheet = tomo.Icon("x-office-spreadsheet")
)
func generateSource (data []byte, width int) map[tomo.Icon] canvas.Texture { func generateSource (data []byte, width int) map[tomo.Icon] canvas.Texture {
atlasImage, _, err := image.Decode(bytes.NewReader(data)) atlasImage, _, err := image.Decode(bytes.NewReader(data))
if err != nil { panic(err) } if err != nil { panic(err) }
@ -37,7 +58,23 @@ func generateSource (data []byte, width int) map[tomo.Icon] canvas.Texture {
} }
col(tomo.IconUnknown) col(tomo.IconUnknown)
col(tomo.Icon("File")) col(iconApplicationXGeneric)
col(iconApplicationXExecutable)
col(iconAudioXGeneric)
col(iconFontXGeneric)
col(iconImageXGeneric)
col(iconModelXGeneric)
col(iconPackageXGeneric)
col(iconTextXGeneric)
col(iconTextHtml)
col(iconTextXGenericTemplate)
col(iconTextXScript)
col(iconVideoXGeneric)
col(iconXOfficeAddressBook)
col(iconXOfficeCalendar)
col(iconXOfficeDocument)
col(iconXOfficePresentation)
col(iconXOfficeSpreadsheet)
row() row()
// actions // actions
@ -450,9 +487,16 @@ func (this *iconSet) Icon (icon tomo.Icon, size tomo.IconSize) canvas.Texture {
func (this *iconSet) MimeIcon (mime data.Mime, size tomo.IconSize) canvas.Texture { func (this *iconSet) MimeIcon (mime data.Mime, size tomo.IconSize) canvas.Texture {
this.ensure() this.ensure()
source := this.selectSource(size) source := this.selectSource(size)
if icon, ok := source[tomo.Icon(mime.String())]; ok {
return icon
}
if mime == data.M("inode", "directory") { if mime == data.M("inode", "directory") {
return source[tomo.IconPlaceDirectory] return source[tomo.IconPlaceDirectory]
} else if icon, ok := source[tomo.Icon(util.GeneralizeXDGIconMimeType(mime).String())]; ok {
return icon
} else { } else {
return source[tomo.Icon("File")] return source[tomo.Icon(iconApplicationXGeneric)]
} }
} }

View File

@ -11,6 +11,7 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/data" import "git.tebibyte.media/tomo/tomo/data"
import "git.tebibyte.media/tomo/tomo/canvas" import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/backend/style" import "git.tebibyte.media/tomo/backend/style"
import "git.tebibyte.media/tomo/nasin/internal/util"
import xdgIconTheme "git.tebibyte.media/tomo/xdg/icon-theme" import xdgIconTheme "git.tebibyte.media/tomo/xdg/icon-theme"
type iconTheme struct { type iconTheme struct {
@ -113,7 +114,7 @@ func (this *iconTheme) mimeIcon (mime data.Mime, size tomo.IconSize) canvas.Text
if texture, ok := this.xdgIcon(xdgFormatMime(mime), size); ok { if texture, ok := this.xdgIcon(xdgFormatMime(mime), size); ok {
return texture return texture
} }
if texture, ok := this.xdgIcon(xdgFormatMime(generalizeMimeType(mime)), size); ok { if texture, ok := this.xdgIcon(xdgFormatMime(util.GeneralizeXDGIconMimeType(mime)), size); ok {
return texture return texture
} }
if texture, ok := this.xdgIcon(xdgFormatMime(data.M("text", "x-generic")), size); ok { if texture, ok := this.xdgIcon(xdgFormatMime(data.M("text", "x-generic")), size); ok {
@ -166,13 +167,6 @@ func xdgFormatMime (mime data.Mime) string {
return fmt.Sprintf("%s-%s", mime.Type, mime.Subtype) return fmt.Sprintf("%s-%s", mime.Type, mime.Subtype)
} }
func generalizeMimeType (mime data.Mime) data.Mime {
// FIXME make this more accurate
// https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
mime.Subtype = "x-generic"
return mime
}
func iconSizePixels (size tomo.IconSize) int { func iconSizePixels (size tomo.IconSize) int {
// TODO: once Tomo has scaling support, take that into account here // TODO: once Tomo has scaling support, take that into account here
switch size { switch size {

10
internal/util/util.go Normal file
View File

@ -0,0 +1,10 @@
package util
import "git.tebibyte.media/tomo/tomo/data"
func GeneralizeXDGIconMimeType (mime data.Mime) data.Mime {
// FIXME make this more accurate
// https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
mime.Subtype = "x-generic"
return mime
}