package app import ( _ "embed" "strings" "github.com/egoist/mygo/ui" ) // gopherPNG 是 Go 语言吉祥物的位图,用作 .go 文件的类型图标。 // // 不用应用自己的 logo:那是「MyGoland 的标识」,而文件树上的图标说的是 // 「这是什么语言写的」,Go 源文件就该用语言本身的形象。图由 // docs 里那张带 GOLANG 字样的图裁出并去掉白底,64×64 透明 PNG。 // //go:embed assets/gopher.png var gopherPNG []byte // gopherIcon 是解码后的位图。视图每帧重建,解码只能做一次。 var gopherIcon = func() ui.ImageSource { b, err := ui.DecodeBitmap(gopherPNG) if err != nil { // 图标缺失不该影响文件树,退化成回落到矢量代码图标。 logf("gopher 图标解码失败: %v", err) return nil } return b }() // 文件树的图标。mygo 的 TreeItem 只接受一段文字,挂不了图标,所以树行是 // 自绘的(见 view.go 的 treeRow),这里只负责「给一个名字,返回画什么」。 // // 三个单色矢量图标都用 currentColor 填充,由 ui.Icon 的 TextColor 上色, // 深浅主题自动跟随;只有 Go 文件用 gopher 位图——那个是彩色的吉祥物, // 换成单色矢量反而认不出来。 var ( iconFolder = mustSVG(``) iconFile = mustSVG(``) iconCode = mustSVG(``) ) func mustSVG(s string) *ui.SVG { v, err := ui.ParseSVG([]byte(s)) if err != nil { logf("解析图标失败: %v", err) return nil } return v } // fileIcon 在文件树的一行前面画上类型图标。 func (a *App) fileIcon(c *ui.Context, name string, isDir bool) { t := c.Theme() lower := strings.ToLower(name) // Go 源码用语言吉祥物本身做图标。 if !isDir && strings.HasSuffix(lower, ".go") { if gopherIcon != nil { ui.Image(c, gopherIcon).Size(14, 14) return } } var svg *ui.SVG var col ui.Color switch { case isDir: svg, col = iconFolder, t.Accent case lower == "go.mod", lower == "go.sum", strings.HasSuffix(lower, ".mod"), strings.HasSuffix(lower, ".sum"): svg, col = iconCode, ui.Hex(a.codeColor()) case isCodeFile(lower): svg, col = iconCode, ui.Hex(a.codeColor()) case isConfigFile(lower): svg, col = iconFile, t.Warning case isImageFile(lower): svg, col = iconFile, t.Success case isDocFile(lower): svg, col = iconFile, t.TextMuted default: svg, col = iconFile, t.TextMuted } if svg == nil { ui.Box(c).Size(14, 14) return } ui.Icon(c, svg).Size(14, 14).TextColor(col) } // codeColor 是源码类文件的图标色,深浅主题各一档,保证在两种底色上都跳得出来。 func (a *App) codeColor() string { if a.light { return "#0969DA" } return "#61AFEF" } func isCodeFile(l string) bool { for _, ext := range []string{".php", ".js", ".mjs", ".cjs", ".ts", ".jsx", ".tsx", ".vue", ".html", ".htm", ".css", ".scss", ".less", ".sql", ".sh", ".bat", ".ps1", ".c", ".h", ".cpp", ".hpp", ".cs", ".java", ".kt", ".py", ".rb", ".rs"} { if strings.HasSuffix(l, ext) { return true } } return false } func isConfigFile(l string) bool { for _, ext := range []string{".json", ".yaml", ".yml", ".toml", ".ini", ".conf", ".xml", ".properties", ".env"} { if strings.HasSuffix(l, ext) { return true } } return l == ".gitignore" || l == ".gitattributes" || l == "dockerfile" || l == "makefile" } func isImageFile(l string) bool { for _, ext := range []string{".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".ico", ".svg"} { if strings.HasSuffix(l, ext) { return true } } return false } func isDocFile(l string) bool { for _, ext := range []string{".md", ".markdown", ".txt", ".rst", ".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx"} { if strings.HasSuffix(l, ext) { return true } } return false }