@@ -9,10 +9,10 @@ import (
99 "strings"
1010 "sync"
1111
12+ "code.gitea.io/gitea/modules/git"
1213 "code.gitea.io/gitea/modules/json"
1314 "code.gitea.io/gitea/modules/log"
1415 "code.gitea.io/gitea/modules/options"
15- "code.gitea.io/gitea/modules/reqctx"
1616 "code.gitea.io/gitea/modules/svg"
1717)
1818
@@ -61,70 +61,49 @@ func (m *MaterialIconProvider) loadData() {
6161 log .Debug ("Loaded material icon rules and SVG images" )
6262}
6363
64- func (m * MaterialIconProvider ) renderFileIconSVG (ctx reqctx.RequestContext , name , svg , extraClass string ) template.HTML {
65- data := ctx .GetData ()
66- renderedSVGs , _ := data ["_RenderedSVGs" ].(map [string ]bool )
67- if renderedSVGs == nil {
68- renderedSVGs = make (map [string ]bool )
69- data ["_RenderedSVGs" ] = renderedSVGs
70- }
64+ func (m * MaterialIconProvider ) renderFileIconSVG (p * RenderedIconPool , name , svg , extraClass string ) template.HTML {
7165 // This part is a bit hacky, but it works really well. It should be safe to do so because all SVG icons are generated by us.
7266 // Will try to refactor this in the future.
7367 if ! strings .HasPrefix (svg , "<svg" ) {
7468 panic ("Invalid SVG icon" )
7569 }
7670 svgID := "svg-mfi-" + name
7771 svgCommonAttrs := `class="svg git-entry-icon ` + extraClass + `" width="16" height="16" aria-hidden="true"`
78- posOuterBefore := strings .IndexByte (svg , '>' )
79- if renderedSVGs [svgID ] && posOuterBefore != - 1 {
80- return template .HTML (`<svg ` + svgCommonAttrs + `><use xlink:href="#` + svgID + `"></use></svg>` )
72+ if p .IconSVGs [svgID ] == "" {
73+ p .IconSVGs [svgID ] = template .HTML (`<svg id="` + svgID + `" ` + svgCommonAttrs + svg [4 :])
8174 }
82- svg = `<svg id="` + svgID + `" ` + svgCommonAttrs + svg [4 :]
83- renderedSVGs [svgID ] = true
84- return template .HTML (svg )
75+ return template .HTML (`<svg ` + svgCommonAttrs + `><use xlink:href="#` + svgID + `"></use></svg>` )
8576}
8677
87- func (m * MaterialIconProvider ) FolderIcon (ctx reqctx.RequestContext , isOpen bool ) template.HTML {
88- iconName := "folder"
89- if isOpen {
90- iconName = "folder-open"
91- }
92- return m .renderIconByName (ctx , iconName , BasicThemeFolderIconName (isOpen ))
93- }
94-
95- func (m * MaterialIconProvider ) FileIcon (ctx reqctx.RequestContext , file * FileIcon ) template.HTML {
78+ func (m * MaterialIconProvider ) FileIcon (p * RenderedIconPool , entry * git.TreeEntry ) template.HTML {
9679 if m .rules == nil {
97- return BasicThemeIcon (file )
80+ return BasicThemeIcon (entry )
9881 }
9982
100- if file . EntryMode .IsLink () {
101- if te , err := file . Entry .FollowLink (); err == nil && te .IsDir () {
83+ if entry .IsLink () {
84+ if te , err := entry .FollowLink (); err == nil && te .IsDir () {
10285 // keep the old "octicon-xxx" class name to make some "theme plugin selector" could still work
10386 return svg .RenderHTML ("material-folder-symlink" , 16 , "octicon-file-directory-symlink" )
10487 }
10588 return svg .RenderHTML ("octicon-file-symlink-file" ) // TODO: find some better icons for them
10689 }
10790
108- name := m .findIconNameByGit (file )
109-
110- extraClass := "octicon-file"
111- switch {
112- case file .EntryMode .IsDir ():
113- extraClass = BasicThemeFolderIconName (false )
114- case file .EntryMode .IsSubModule ():
115- extraClass = "octicon-file-submodule"
116- }
117-
118- return m .renderIconByName (ctx , name , extraClass )
119- }
120-
121- func (m * MaterialIconProvider ) renderIconByName (ctx reqctx.RequestContext , name , extraClass string ) template.HTML {
122- if iconSVG , ok := m .svgs [name ]; ok && iconSVG != "" {
91+ name := m .findIconNameByGit (entry )
92+ // the material icon pack's "folder" icon doesn't look good, so use our built-in one
93+ // keep the old "octicon-xxx" class name to make some "theme plugin selector" could still work
94+ if iconSVG , ok := m .svgs [name ]; ok && name != "folder" && iconSVG != "" {
12395 // keep the old "octicon-xxx" class name to make some "theme plugin selector" could still work
124- return m .renderFileIconSVG (ctx , name , iconSVG , extraClass )
96+ extraClass := "octicon-file"
97+ switch {
98+ case entry .IsDir ():
99+ extraClass = "octicon-file-directory-fill"
100+ case entry .IsSubModule ():
101+ extraClass = "octicon-file-submodule"
102+ }
103+ return m .renderFileIconSVG (p , name , iconSVG , extraClass )
125104 }
126105 // TODO: use an interface or wrapper for git.Entry to make the code testable.
127- return svg . RenderHTML ( "octicon-file" )
106+ return BasicThemeIcon ( entry )
128107}
129108
130109func (m * MaterialIconProvider ) findIconNameWithLangID (s string ) string {
@@ -168,9 +147,9 @@ func (m *MaterialIconProvider) FindIconName(name string, isDir bool) string {
168147 return "file"
169148}
170149
171- func (m * MaterialIconProvider ) findIconNameByGit (file * FileIcon ) string {
172- if file . EntryMode .IsSubModule () {
150+ func (m * MaterialIconProvider ) findIconNameByGit (entry * git. TreeEntry ) string {
151+ if entry .IsSubModule () {
173152 return "folder-git"
174153 }
175- return m .FindIconName (file .Name , file . EntryMode .IsDir ())
154+ return m .FindIconName (entry .Name (), entry .IsDir ())
176155}
0 commit comments