Skip to content

Commit 13cda7d

Browse files
authored
Annotate settings.ts (#134)
* Annotate settings.ts * nit
1 parent e216fa8 commit 13cda7d

File tree

4 files changed

+53
-41
lines changed

4 files changed

+53
-41
lines changed

src/scripts/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
LGraphNode,
3131
LiteGraph,
3232
} from "@comfyorg/litegraph";
33+
import { StorageLocation } from "@/types/settingTypes";
3334

3435
// CSS imports. style.css must be imported later as it overwrites some litegraph styles.
3536
import "@comfyorg/litegraph/css/litegraph.css";
@@ -104,8 +105,7 @@ export class ComfyApp {
104105
progress: { value: number; max: number } | null;
105106
configuringGraph: boolean;
106107
isNewUserSession: boolean;
107-
// Are there any other options than "server"?
108-
storageLocation: string;
108+
storageLocation: StorageLocation;
109109
multiUserServer: boolean;
110110
ctx: CanvasRenderingContext2D;
111111
widgets: Record<string, ComfyWidgetConstructor>;

src/scripts/ui/menu/workflows.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ export class ComfyWorkflowsMenu {
233233
) {
234234
const r = getExtraMenuOptions?.apply?.(this, arguments);
235235
if (
236-
app.ui.settings.getSettingValue("Comfy.UseNewMenu", false) === true
236+
app.ui.settings.getSettingValue<boolean>(
237+
"Comfy.UseNewMenu",
238+
false
239+
) === true
237240
) {
238241
const t = this;
239242
let img;

src/scripts/ui/settings.ts

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,14 @@ import { $el } from "../ui";
22
import { api } from "../api";
33
import { ComfyDialog } from "./dialog";
44
import type { ComfyApp } from "../app";
5-
6-
/* The Setting entry stored in `ComfySettingsDialog` */
7-
interface Setting {
8-
id: string;
9-
onChange?: (value: any, oldValue?: any) => void;
10-
name: string;
11-
render: () => HTMLElement;
12-
}
13-
14-
interface SettingOption {
15-
text: string;
16-
value?: string;
17-
}
18-
19-
interface SettingParams {
20-
id: string;
21-
name: string;
22-
type:
23-
| string
24-
| ((
25-
name: string,
26-
setter: (v: any) => void,
27-
value: any,
28-
attrs: any
29-
) => HTMLElement);
30-
defaultValue: any;
31-
onChange?: (newValue: any, oldValue?: any) => void;
32-
attrs?: any;
33-
tooltip?: string;
34-
options?: Array<string | SettingOption> | ((value: any) => SettingOption[]);
35-
}
5+
import type { Setting, SettingParams } from "@/types/settingTypes";
366

377
export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
388
app: ComfyApp;
399
settingsValues: any;
4010
settingsLookup: Record<string, Setting>;
4111

42-
constructor(app) {
12+
constructor(app: ComfyApp) {
4313
super();
4414
this.app = app;
4515
this.settingsValues = {};
@@ -83,7 +53,7 @@ export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
8353
return Object.values(this.settingsLookup);
8454
}
8555

86-
#dispatchChange(id, value, oldValue?) {
56+
#dispatchChange<T>(id: string, value: T, oldValue?: T) {
8757
this.dispatchEvent(
8858
new CustomEvent(id + ".change", {
8959
detail: {
@@ -109,26 +79,26 @@ export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
10979
}
11080
}
11181

112-
getId(id) {
82+
getId(id: string) {
11383
if (this.app.storageLocation === "browser") {
11484
id = "Comfy.Settings." + id;
11585
}
11686
return id;
11787
}
11888

119-
getSettingValue(id, defaultValue?) {
89+
getSettingValue<T>(id: string, defaultValue?: T): T {
12090
let value = this.settingsValues[this.getId(id)];
12191
if (value != null) {
12292
if (this.app.storageLocation === "browser") {
12393
try {
124-
value = JSON.parse(value);
94+
value = JSON.parse(value) as T;
12595
} catch (error) {}
12696
}
12797
}
12898
return value ?? defaultValue;
12999
}
130100

131-
async setSettingValueAsync(id, value) {
101+
async setSettingValueAsync(id: string, value: any) {
132102
const json = JSON.stringify(value);
133103
localStorage["Comfy.Settings." + id] = json; // backwards compatibility for extensions keep setting in storage
134104

@@ -143,7 +113,7 @@ export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
143113
await api.storeSetting(id, value);
144114
}
145115

146-
setSettingValue(id, value) {
116+
setSettingValue(id: string, value: any) {
147117
this.setSettingValueAsync(id, value).catch((err) => {
148118
alert(`Error saving setting '${id}'`);
149119
console.error(err);

src/types/settingTypes.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export type StorageLocation = "browser" | "server";
2+
3+
export type SettingInputType =
4+
| "boolean"
5+
| "number"
6+
| "slider"
7+
| "combo"
8+
| "text"
9+
| "hidden";
10+
11+
export type SettingCustomRenderer = (
12+
name: string,
13+
setter: (v: any) => void,
14+
value: any,
15+
attrs: any
16+
) => HTMLElement;
17+
18+
export interface SettingOption {
19+
text: string;
20+
value?: string;
21+
}
22+
23+
export interface Setting {
24+
id: string;
25+
onChange?: (value: any, oldValue?: any) => void;
26+
name: string;
27+
render: () => HTMLElement;
28+
}
29+
30+
export interface SettingParams {
31+
id: string;
32+
name: string;
33+
type: SettingInputType | SettingCustomRenderer;
34+
defaultValue: any;
35+
onChange?: (newValue: any, oldValue?: any) => void;
36+
attrs?: any;
37+
tooltip?: string;
38+
options?: Array<string | SettingOption> | ((value: any) => SettingOption[]);
39+
}

0 commit comments

Comments
 (0)