-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add material icons for file list #26236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c502126
file icon
wxiaoguang 4945914
normalizes SVG images when loading
wxiaoguang 10b2d48
add config option FILE_ICON_THEME, use builtin material-folder-generi…
wxiaoguang 4a7dbe9
Merge branch 'main' into custom-file-icon
wxiaoguang a5ef41e
Merge branch 'main' into custom-file-icon
GiteaBot 7c5c0bf
Merge branch 'main' into custom-file-icon
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//go:build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func main() { | ||
var destination string | ||
flag.StringVar(&destination, "dest", "options/fileicon/", "destination for the fileicon") | ||
flag.Parse() | ||
|
||
pkgName := "material-icon-theme" | ||
req, err := http.NewRequest("GET", fmt.Sprintf("https://registry.npmjs.org/%s/", pkgName), nil) | ||
if err != nil { | ||
log.Fatalf("http req: %s", err) | ||
} | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
log.Fatalf("http error: %s", err) | ||
} | ||
d := json.NewDecoder(resp.Body) | ||
defer resp.Body.Close() | ||
var m struct { | ||
DistTags map[string]string `json:"dist-tags"` | ||
} | ||
err = d.Decode(&m) | ||
if err != nil { | ||
log.Fatalf("json decode: %s", err) | ||
} | ||
|
||
latestTag := m.DistTags["latest"] | ||
if latestTag == "" { | ||
log.Fatal("no latest tag") | ||
} | ||
|
||
pkg := fmt.Sprintf("https://registry.npmjs.org/%s/-/%s-%s.tgz", pkgName, pkgName, latestTag) | ||
req, err = http.NewRequest("GET", pkg, nil) | ||
if err != nil { | ||
log.Fatalf("http req: %s", err) | ||
} | ||
resp, err = http.DefaultClient.Do(req) | ||
if err != nil { | ||
log.Fatalf("http error: %s", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
localFileName := filepath.Join(destination, "material.tgz") | ||
localFile, err := os.Create(localFileName) | ||
if err != nil { | ||
log.Fatalf("create file: %s", err) | ||
} | ||
defer localFile.Close() | ||
|
||
_, err = io.Copy(localFile, resp.Body) | ||
if err != nil { | ||
log.Fatalf("copy body to file: %s", err) | ||
} | ||
|
||
log.Printf("Downloaded %s to %s", pkg, localFileName) | ||
} |
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package fileicon | ||
|
||
import ( | ||
"context" | ||
"html/template" | ||
|
||
"code.gitea.io/gitea/modules/git" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/svg" | ||
) | ||
|
||
func fileIconBasic(ctx context.Context, entry *git.TreeEntry) template.HTML { | ||
svgName := "octicon-file" | ||
switch { | ||
case entry.IsLink(): | ||
svgName = "octicon-file-symlink-file" | ||
if te, err := entry.FollowLink(); err == nil && te.IsDir() { | ||
svgName = "octicon-file-directory-symlink" | ||
} | ||
case entry.IsDir(): | ||
svgName = "octicon-file-directory-fill" | ||
case entry.IsSubModule(): | ||
svgName = "octicon-file-submodule" | ||
} | ||
return svg.RenderHTML(svgName) | ||
} | ||
|
||
func FileIcon(ctx context.Context, entry *git.TreeEntry) template.HTML { | ||
if setting.UI.FileIconTheme == "material" { | ||
return DefaultMaterialIconProvider().FileIcon(ctx, entry) | ||
} | ||
return fileIconBasic(ctx, entry) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package fileicon | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/gzip" | ||
"context" | ||
"html/template" | ||
"io" | ||
"net/http" | ||
"path" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/git" | ||
"code.gitea.io/gitea/modules/json" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/options" | ||
"code.gitea.io/gitea/modules/svg" | ||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
type materialIconsData struct { | ||
IconDefinitions map[string]*struct { | ||
IconPath string `json:"iconPath"` | ||
IconContent string `json:"-"` | ||
} `json:"iconDefinitions"` | ||
FileNames map[string]string `json:"fileNames"` | ||
FolderNames map[string]string `json:"folderNames"` | ||
FileExtensions map[string]string `json:"fileExtensions"` | ||
LanguageIds map[string]string `json:"languageIds"` | ||
} | ||
|
||
type MaterialIconProvider struct { | ||
mu sync.RWMutex | ||
|
||
fs http.FileSystem | ||
packFile string | ||
packFileTime time.Time | ||
lastStatTime time.Time | ||
reloadInterval time.Duration | ||
|
||
materialIcons *materialIconsData | ||
} | ||
|
||
var ( | ||
materialIconProvider *MaterialIconProvider | ||
materialIconProviderOnce sync.Once | ||
) | ||
|
||
func DefaultMaterialIconProvider() *MaterialIconProvider { | ||
materialIconProviderOnce.Do(func() { | ||
materialIconProvider = NewMaterialIconProvider(options.AssetFS(), "fileicon/material.tgz") | ||
}) | ||
return materialIconProvider | ||
} | ||
|
||
func NewMaterialIconProvider(fs http.FileSystem, packFile string) *MaterialIconProvider { | ||
return &MaterialIconProvider{fs: fs, packFile: packFile, reloadInterval: time.Second} | ||
} | ||
|
||
func (m *MaterialIconProvider) loadDataFromPack(pack http.File) (*materialIconsData, error) { | ||
gzf, err := gzip.NewReader(pack) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
files := map[string][]byte{} | ||
tarReader := tar.NewReader(gzf) | ||
for { | ||
header, err := tarReader.Next() | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
files[util.PathJoinRelX(header.Name)], err = io.ReadAll(tarReader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
iconsData := materialIconsData{} | ||
err = json.Unmarshal(files["package/dist/material-icons.json"], &iconsData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for name, icon := range iconsData.IconDefinitions { | ||
iconContent := files[path.Join("package/dist", icon.IconPath)] | ||
iconsData.IconDefinitions[name].IconContent = string(svg.Normalize(iconContent, 16)) | ||
} | ||
|
||
return &iconsData, nil | ||
} | ||
|
||
func (m *MaterialIconProvider) loadData() { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
if time.Since(m.lastStatTime) > m.reloadInterval { | ||
m.lastStatTime = time.Now() | ||
|
||
f, err := m.fs.Open(m.packFile) | ||
if err != nil { | ||
log.Error("Failed to open material icon pack file: %v", err) | ||
return | ||
} | ||
defer f.Close() | ||
|
||
fileInfo, err := f.Stat() | ||
if err != nil { | ||
log.Error("Failed to stat material icon pack file: %v", err) | ||
return | ||
} | ||
if fileInfo.ModTime().Equal(m.packFileTime) { | ||
return | ||
} | ||
|
||
iconsData, err := m.loadDataFromPack(f) | ||
if err != nil { | ||
log.Error("Failed to load material icon pack file: %v", err) | ||
return | ||
} | ||
m.materialIcons = iconsData | ||
m.packFileTime = fileInfo.ModTime() | ||
} | ||
} | ||
|
||
func (m *MaterialIconProvider) FileIcon(ctx context.Context, entry *git.TreeEntry) template.HTML { | ||
m.mu.RLock() | ||
if time.Since(m.lastStatTime) > m.reloadInterval { | ||
m.mu.RUnlock() | ||
m.loadData() | ||
m.mu.RLock() | ||
} | ||
defer m.mu.RUnlock() | ||
|
||
if m.materialIcons == nil { | ||
return fileIconBasic(ctx, entry) | ||
} | ||
|
||
if entry.IsLink() { | ||
if te, err := entry.FollowLink(); err == nil && te.IsDir() { | ||
return svg.RenderHTML("material-folder-symlink") | ||
} | ||
return svg.RenderHTML("octicon-file-symlink-file") // TODO: find some better icons for them | ||
} | ||
|
||
name := m.findIconName(entry) | ||
if name == "folder" { | ||
// the material icon pack's "folder" icon doesn't look good, so use our built-in one | ||
return svg.RenderHTML("material-folder-generic") | ||
} | ||
if iconDef, ok := m.materialIcons.IconDefinitions[name]; ok && iconDef.IconContent != "" { | ||
return template.HTML(iconDef.IconContent) | ||
} | ||
return svg.RenderHTML("octicon-file") | ||
} | ||
|
||
func (m *MaterialIconProvider) findIconName(entry *git.TreeEntry) string { | ||
if entry.IsSubModule() { | ||
return "folder-git" | ||
} | ||
|
||
iconsData := m.materialIcons | ||
fileName := path.Base(entry.Name()) | ||
|
||
if entry.IsDir() { | ||
if s, ok := iconsData.FolderNames[fileName]; ok { | ||
return s | ||
} | ||
if s, ok := iconsData.FolderNames[strings.ToLower(fileName)]; ok { | ||
return s | ||
} | ||
return "folder" | ||
} | ||
|
||
if s, ok := iconsData.FileNames[fileName]; ok { | ||
return s | ||
} | ||
if s, ok := iconsData.FileNames[strings.ToLower(fileName)]; ok { | ||
return s | ||
} | ||
|
||
for i := len(fileName) - 1; i >= 0; i-- { | ||
if fileName[i] == '.' { | ||
ext := fileName[i+1:] | ||
if s, ok := iconsData.FileExtensions[ext]; ok { | ||
return s | ||
} | ||
} | ||
} | ||
|
||
return "file" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.