Added OpenRawResource function

This commit is contained in:
Alexei Anoshenko 2024-07-17 16:32:05 +03:00
parent 95043b0b9a
commit c9954afa7f
2 changed files with 41 additions and 1 deletions

View File

@ -2,6 +2,7 @@
* Added "mod", "rem", "round", "round-up", "round-down", and "round-to-zero" SizeFunc functions
* Added ModSize, RemSize, RoundSize, RoundUpSize, RoundDownSize, and RoundToZeroSize functions
* Added Start, Stop, Pause, and Resume methods to Animation interface
* Added OpenRawResource function
# v0.16.0
* Can use ListAdapter as "content" property value of ListLayout

View File

@ -3,6 +3,7 @@ package rui
import (
"embed"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
@ -329,7 +330,7 @@ func ReadRawResource(filename string) []byte {
rootDirs := resources.embedRootDirs(fs)
for _, dir := range rootDirs {
switch dir {
case imageDir, themeDir, viewDir:
case imageDir, themeDir, viewDir, stringsDir:
// do nothing
case rawDir:
@ -361,6 +362,44 @@ func ReadRawResource(filename string) []byte {
return nil
}
// OpenRawResource returns the contents of the raw resource with the specified name
func OpenRawResource(filename string) fs.File {
for _, fs := range resources.embedFS {
rootDirs := resources.embedRootDirs(fs)
for _, dir := range rootDirs {
switch dir {
case imageDir, themeDir, viewDir, stringsDir:
// do nothing
case rawDir:
if file, err := fs.Open(dir + "/" + filename); err == nil {
return file
}
default:
if file, err := fs.Open(dir + "/" + rawDir + "/" + filename); err == nil {
return file
}
}
}
}
if resources.path != "" {
if file, err := os.Open(resources.path + rawDir + "/" + filename); err == nil {
return file
}
}
if exe, err := os.Executable(); err == nil {
if file, err := os.Open(filepath.Dir(exe) + "/resources/" + rawDir + "/" + filename); err == nil {
return file
}
}
ErrorLogF(`The "%s" raw file don't found`, filename)
return nil
}
// AllRawResources returns the list of all raw resouces
func AllRawResources() []string {
result := []string{}