From bda3d460c6f0578ab60159f9d309d3eb1502c9a7 Mon Sep 17 00:00:00 2001 From: OctoNezd Date: Tue, 26 Mar 2024 21:20:53 +0300 Subject: [PATCH] Implement resizable icons. --- .../css/olPreferences.css | 13 ++- src/features/base.ts | 68 +++++++++++++- src/features/iconRegulator.ts | 94 +++++++++++++++++++ src/features/index.ts | 2 + 4 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 src/features/iconRegulator.ts diff --git a/src/extensionPreferences/css/olPreferences.css b/src/extensionPreferences/css/olPreferences.css index 93290df..46a2510 100644 --- a/src/extensionPreferences/css/olPreferences.css +++ b/src/extensionPreferences/css/olPreferences.css @@ -22,7 +22,8 @@ body.olPreferencesOpen { font-size: var(--md-sys-typescale-body-large-font-size); line-height: var(--md-sys-typescale-body-large-font-line-height); } -.ol-set-description { +.ol-set-description, +.ol-set-val { color: var(--md-sys-color-on-surface-variant); font-family: var(--md-sys-typescale-body-medium-font-family-name); font-weight: var(--md-sys-typescale-body-medium-font-weight); @@ -54,3 +55,13 @@ body.olPreferencesOpen { padding-right: 16px; padding-top: 8px; } +.ol-setting-slider { + justify-content: center; + flex-direction: column; + align-items: stretch; +} +.ol-setting-slider .ol-set-inner { + display: flex; + flex-direction: row; + justify-content: space-between; +} diff --git a/src/features/base.ts b/src/features/base.ts index ae95e9a..5538db5 100644 --- a/src/features/base.ts +++ b/src/features/base.ts @@ -13,7 +13,7 @@ export class OLFeature { return; } } -class SettingOption { +export class SettingOption { element: HTMLDivElement; constructor(title: string, description: string) { this.element = document.createElement("div"); @@ -43,7 +43,9 @@ export class SettingToggle extends SettingOption { this.settingId = settingId; this.callback = callback; this.element.classList.add("ol-setting-toggle"); - this.element.innerHTML = `

${title}

${description}

`; + this.element.innerHTML = + `

${title}

` + + `

${description}

`; this.element.addEventListener( "click", async () => await this.toggleSetting() @@ -69,3 +71,65 @@ export class SettingToggle extends SettingOption { this.callback(new_value); } } +export class SettingSlider extends SettingOption { + settingId: string; + callback: (newState: number) => void; + default: number = 0; + minVal: number = 1; + maxVal: number = Infinity; + constructor( + title: string, + settingId: string, + valSuffix: string, + defaultValue: number, + minValue: number, + maxValue: number, + callback: (newState: number) => void + ) { + super(title, ""); + this.settingId = settingId; + this.callback = callback; + this.minVal = minValue; + this.maxVal = maxValue; + this.default = defaultValue; + this.element.classList.add("ol-setting-slider"); + this.element.innerHTML = + `

${title}

` + + `${defaultValue}${valSuffix}
` + + ``; + this.element + .querySelector("input") + ?.addEventListener("change", (e) => this.update(e)); + this.loadPD(); + } + async getValue(): Promise { + const prefData = await store.get(this.settingId); + console.log("pd:", prefData); + // @ts-ignore + return prefData ?? this.default; + } + async loadPD() { + const value = await this.getValue(); + this.element.querySelector("input")!.value = value.toString(); + ( + this.element.querySelector(".ol-set-val") as HTMLParagraphElement + ).innerText = value.toString(); + this.callback(value); + } + async update(e: Event) { + const new_value = Number((e.currentTarget as HTMLInputElement).value); + if (new_value < this.minVal || new_value > this.maxVal) { + console.log("invalid value, not updating storage"); + (e.currentTarget as HTMLInputElement).value = ( + await this.getValue() + ).toString(); + return; + } + await store.set(this.settingId, new_value); + console.log("updating", this.settingId, new_value); + this.callback(new_value); + ( + this.element.querySelector(".ol-set-val") as HTMLParagraphElement + ).innerText = new_value.toString(); + } +} diff --git a/src/features/iconRegulator.ts b/src/features/iconRegulator.ts new file mode 100644 index 0000000..a3f2b42 --- /dev/null +++ b/src/features/iconRegulator.ts @@ -0,0 +1,94 @@ +import { OLFeature, SettingOption, SettingSlider } from "./base"; + +class IconSizePreview extends SettingOption { + settingId = "iconSizePreview"; + constructor() { + super("", ""); + // @ts-ignore + this.element = document.createElement("div"); + this.element.innerHTML = ` +

Preview:

+