Skip to content
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

Add plugin setting to control default base image directory #8

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion builders/gen_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ const entry_base =
"default": "256",
"minValue": 8,
"maxValue": 1920, // arbitrary
"readOnly": false
"readOnly": false,
"description": "Image size produced when using standalone 'Draw' actions for producing icons, without any layering."
},
{
"name": "Default Image Files Path",
"type": "text",
"default": "",
"readOnly": false,
"description": "Base directory to use when loading image files specified using a relative path. When left empty, the default is Touch Portal's configuration directory for the current user."
}
],
"categories": [
Expand Down
19 changes: 17 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { SizeType, ParseState, TpActionDataArrayType } from './modules/types'
import { ILayerElement, IValuedElement } from './modules/interfaces';
import DynamicIcon from "./modules/DynamicIcon";
import * as m_el from "./modules/elements";
import { default as g_globalImageCache } from './modules/ImageCache'
import { default as g_globalImageCache, ImageCache } from './modules/ImageCache'
import { setCommonLogger } from './common'
import { dirname as pdirname, join as pjoin } from 'path';

// -------------------------------
// Constants
Expand All @@ -22,6 +23,13 @@ const IMAGE_COMPRESSOR_OPTIONS = {
palette: true // MP: Again the docs suggest enabling this would be slower but my tests show a significant speed improvement.
}

// Default image base directory to TP's config folder for current user.
// This is used to resolve relative paths when loading images, via the ImageCache.cacheOptions.baseImagePath setting.
// User can override this with the "Default Image Files Path" plugin setting in TP.
// NOTE: this only works when the plugin binary is run from its normal install location in TPs config folder.
// If there's a better x-platform way to find TPs config path, then fixme.
const DEFAULT_IMAGE_FILE_BASE_PATH = pjoin(pdirname(process.argv0), '..', '..');


// -------------------------------
// Globals
Expand All @@ -36,6 +44,9 @@ const TPClient = new TP.Client();
// hackish way to share the TP client "logger" with other modules
setCommonLogger((...args: any[]) => { TPClient.logIt(...args) });

// Set default image path here. It should be overwritten anyway when Settings are processed,
// but this preserves BC with previous 1.1 alpha versions w/out the setting. Could eventually be removed.
ImageCache.cacheOptions.baseImagePath = DEFAULT_IMAGE_FILE_BASE_PATH;

// -------------------------------
// Helper functions
Expand Down Expand Up @@ -393,10 +404,14 @@ TPClient.on("Action", (message:any /*, hold?:boolean */) => {
TPClient.on("Settings", (settings:{ [key:string]:string }[]) => {
settings.forEach((s) => {
if (s.hasOwnProperty('Default Icon Size')) {
const size:number = parseInt(s['Default Icon Size']) || 0;
const size:number = parseInt(s['Default Icon Size'].trim()) || 0;
if (size >= 8)
g_defaultIconSize = {width: size, height: size};
}
else if (s.hasOwnProperty('Default Image Files Path')) {
const path:string = s['Default Image Files Path'].trim();
ImageCache.cacheOptions.baseImagePath = path || DEFAULT_IMAGE_FILE_BASE_PATH;
}
});
})

Expand Down
4 changes: 4 additions & 0 deletions src/modules/ImageCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { loadImage } from 'skia-canvas';
import { Mutex } from 'async-mutex';
import { SizeType } from './types';
import { logIt } from '../common'
import { isAbsolute as isAbsPath, join as pjoin } from 'path';

/** Central storage for various image processing options; Set via ImageCache.cacheOptions
Some of these could in theory be controlled via plugin settings or action data. */
Expand All @@ -12,6 +13,7 @@ class ImageCacheOptions {
actual cache size may grow a bit above this level to optimize the buffer trimming operations.
Reducing this value at runtime will only take effect next time an image is added to the cache. */
maxCachedImages: number = 250;
baseImagePath: string = "";
// See https://sharp.pixelplumbing.com/api-constructor#sharp
sourceLoadOptions: any = { density: 72 }; // [dpi] only relevant for loading vector graphics. 72 is default;
// See also https://sharp.pixelplumbing.com/api-resize#resize
Expand Down Expand Up @@ -134,6 +136,8 @@ export class ImageCache
*/
public async getOrLoadImage(src: string, size: SizeType, resizeOptions:any = {}, meta?: any): Promise<ImageDataType>
{
if (ImageCache.cacheOptions.baseImagePath && !isAbsPath(src))
src = pjoin(ImageCache.cacheOptions.baseImagePath, src);
if (ImageCache.cacheOptions.maxCachedImages <= 0)
return this.loadImage(src, size, resizeOptions); // short-circuit for disabled cache

Expand Down