Browse Source

Add support for webapp defined file icon types

Toby Chui 6 days ago
parent
commit
36ed5bd1a9

+ 21 - 6
src/mod/modules/module.go

@@ -30,9 +30,10 @@ type ModuleInfo struct {
 }
 
 type ModuleHandler struct {
-	LoadedModule []*ModuleInfo
-	userHandler  *user.UserHandler
-	tmpDirectory string
+	LoadedModule    []*ModuleInfo
+	userHandler     *user.UserHandler
+	tmpDirectory    string
+	extIconRegistry map[string]string // lowercased ext (no dot) --> web-root-relative icon path
 
 	// OnModuleUninstall is an optional hook called when a module is uninstalled.
 	// The hook receives the module name and can be used to clean up associated resources
@@ -42,12 +43,26 @@ type ModuleHandler struct {
 
 func NewModuleHandler(userHandler *user.UserHandler, tmpFolderPath string) *ModuleHandler {
 	return &ModuleHandler{
-		LoadedModule: []*ModuleInfo{},
-		userHandler:  userHandler,
-		tmpDirectory: tmpFolderPath,
+		LoadedModule:    []*ModuleInfo{},
+		userHandler:     userHandler,
+		tmpDirectory:    tmpFolderPath,
+		extIconRegistry: map[string]string{},
 	}
 }
 
+// RegisterExtIcon stores a web-root-relative icon path for the given file extension.
+// ext may include or omit a leading dot; it is normalised to lowercase without dot.
+func (m *ModuleHandler) RegisterExtIcon(ext, iconPath string) {
+	ext = strings.ToLower(strings.TrimPrefix(ext, "."))
+	m.extIconRegistry[ext] = iconPath
+}
+
+// HandleGetExtIcons serves the extension-to-icon registry as a JSON object.
+func (m *ModuleHandler) HandleGetExtIcons(w http.ResponseWriter, r *http.Request) {
+	js, _ := json.Marshal(m.extIconRegistry)
+	utils.SendJSONResponse(w, string(js))
+}
+
 // Register endpoint. Provide moduleInfo datastructure or unparsed json
 func (m *ModuleHandler) RegisterModule(module ModuleInfo) {
 	m.LoadedModule = append(m.LoadedModule, &module)

+ 3 - 0
src/module.go

@@ -37,6 +37,9 @@ func ModuleServiceInit() {
 	http.HandleFunc("/system/modules/getLaunchPara", func(w http.ResponseWriter, r *http.Request) {
 		authAgent.HandleCheckAuth(w, r, moduleHandler.GetLaunchParameter)
 	})
+	http.HandleFunc("/system/modules/exticons", func(w http.ResponseWriter, r *http.Request) {
+		authAgent.HandleCheckAuth(w, r, moduleHandler.HandleGetExtIcons)
+	})
 
 	adminRouter.HandleFunc("/system/modules/reload", func(w http.ResponseWriter, r *http.Request) {
 		moduleHandler.ReloadAllModules(AGIGateway)

BIN
src/web/Pixel Studio/img/file_icon.png


BIN
src/web/Pixel Studio/img/file_icon.psd


+ 3 - 0
src/web/Pixel Studio/init.agi

@@ -25,3 +25,6 @@ var moduleLaunchInfo = {
 
 //Register the module
 registerModule(JSON.stringify(moduleLaunchInfo));
+
+//Register a custom icon for .pxs files so the file manager shows it instead of a generic icon
+registerExtensionIcon(".pxs", "img/file_icon.png");

+ 13 - 0
src/web/SystemAO/file_system/file_explorer.html

@@ -601,6 +601,9 @@
                 return ((crc32UpdateState(0xFFFFFFFF, bytes) ^ 0xFFFFFFFF) >>> 0).toString(16).padStart(8, '0');
             }
 
+            //Module-registered extension icons (ext without dot → web-root-relative path)
+            var extIconRegistry = {};
+
             //File Sharing related
             let shareEditingObject = "";
             let shareEditingUUID = "";
@@ -622,6 +625,12 @@
                 
                 //Function to complete initialization after applocale is ready
                 function completeInitialization(){
+                    //Fetch module-registered extension icons once so the grid view can use them
+                    $.get("/system/modules/exticons", function(data) {
+                        if (typeof data === "string") { try { data = JSON.parse(data); } catch(e) {} }
+                        if (data && typeof data === "object") { extIconRegistry = data; }
+                    });
+
                     initRootDirs();
                     initSystemInfo();
                     initUploadMode();
@@ -1350,6 +1359,10 @@
                     }else if (viewMode == "grid"){
                         //Update the icon path depends on the type of file
                         var imagePath = "../../img/desktop/files_icon/" + filesIconTheme + "/" + icon + ".png";
+                        //Use module-registered icon for proprietary/unrecognised extensions
+                        if (icon === "file outline" && extIconRegistry[ext]) {
+                            imagePath = "../../" + extIconRegistry[ext];
+                        }
                         var displayName = JSON.parse(JSON.stringify(filename));
                         if (ext == "shortcut"){
                             //This is a shortcut file. Treat it specially

+ 13 - 0
src/web/desktop.html

@@ -1468,6 +1468,9 @@
         var backgroundIntervalCounter; //The interval object for background changer
         var desktopThemeColor = ""; //The current desktop theme color, which renders to the status bar and suchs
 
+        //Module-registered extension icons (ext without dot → web-root-relative path)
+        var extIconRegistry = {};
+
         //Desktop icon realted
         var desktopIconSize = "medium"; //Size of desktop icons. Allow {small / medium / big}
         var desktopIconPack = "default"; //Desktop icon pack to be used. Located under desktop/file_icon/{pack-name}
@@ -1571,6 +1574,12 @@
 
 
         function initDesktop(){
+            //Fetch module-registered extension icons once so the desktop can use them
+            $.get("system/modules/exticons", function(data) {
+                if (typeof data === "string") { try { data = JSON.parse(data); } catch(e) {} }
+                if (data && typeof data === "object") { extIconRegistry = data; }
+            });
+
             //Initiation functions
             initDesktopTheme();
             initDesktopHostInfo();
@@ -3990,6 +3999,10 @@
             //Get the icon from extension
             var icon = ao_module_utils.getIconFromExt(filedata.Ext.substring(1, filedata.Ext.length));
             var imagePath = "img/desktop/files_icon/" + desktopIconPack + "/" + icon + ".png";
+            //Use module-registered icon for proprietary/unrecognised extensions
+            if (icon === "file outline" && extIconRegistry[filedata.Ext.substring(1).toLowerCase()]) {
+                imagePath = extIconRegistry[filedata.Ext.substring(1).toLowerCase()];
+            }
             //Handle special icon types here
             var size = desktopIconSize;
             var filename = JSON.parse(JSON.stringify(filedata.Filename));