Remove private variables, simplify logic

Turns out, ComboWidget was the only one using private variables.
Everything else is simple
This commit is contained in:
Austin Mroz
2025-08-30 07:44:36 -07:00
parent ca7698dae5
commit 703dc8fd4c
3 changed files with 20 additions and 187 deletions

View File

@@ -591,32 +591,26 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph {
return n.widgets.find((w) => w.name === widgetName)
}
const overlay: object = {nodeId, widgetName, graph: this.subgraph}
const handler = Object.fromEntries(['get', 'set', 'getPrototypeOf', 'getOwnPropertyDescriptor', 'ownKeys', 'has'].map((s) => {
const handler = Object.fromEntries(['get', 'set', 'getPrototypeOf', 'ownKeys', 'has'].map((s) => {
const func = function(t,p,...rest) {
if (s == 'get' && p == '_overlay')
return overlay
const lw = linkedWidget(overlay.graph, overlay.nodeId, overlay.widgetName)
if (s == 'ownKeys') {
return Reflect.ownKeys(lw)
}
if (s == 'get' && p == 'node')
return {pos: this.pos, __proto__:lw.node}
const r = rest.at(-1)
if (['y', 'last_y', 'width', 'computedHeight', 'computedDisabled'].includes(p))
r = overlay
else
r = linkedWidget(overlay.graph, overlay.nodeId, overlay.widgetName)
//NOTE: p may be undefined
let r = rest.at(-1)
if (['y', 'last_y', 'width', 'computedHeight', 'computedDisabled', 'afterQueued', 'beforeQueued', 'onRemove'].includes(p))
t = overlay
else if (p == "value") {
t = lw
r = lw
} else {
t = lw
if (!t)
return//TODO: pass to overlay subitem to display a disconnected state
const ret = Reflect[s](t,p,...rest.slice(0,-1), r)
if (ret instanceof Function) {
return function (...args) {
//console.log(r,t,r===t)
return ret.apply(this === r ? t : this, args)
}
}
return ret
return Reflect[s](t,p,...rest.slice(0,-1),r)
}
return [s, func]
}))

View File

@@ -45,7 +45,7 @@ export class ComboWidget
return typeof this.value === 'number' ? String(this.value) : this.value
}
#getValues(node: LGraphNode): Values {
getValues(node: LGraphNode): Values {
const { values } = this.options
if (values == null) throw new Error('[ComboWidget]: values is required')
@@ -57,7 +57,7 @@ export class ComboWidget
* @param increment `true` if checking the use of the increment button, `false` for decrement
* @returns `true` if the value is at the given index, otherwise `false`.
*/
#canUseButton(increment: boolean): boolean {
canUseButton(increment: boolean): boolean {
const { values } = this.options
// If using legacy duck-typed method, false is the most permissive return value
if (typeof values === 'function') return false
@@ -78,23 +78,23 @@ export class ComboWidget
* Handles edge case where the value is both the first and last item in the list.
*/
override canIncrement(): boolean {
return this.#canUseButton(true)
return this.canUseButton(true)
}
override canDecrement(): boolean {
return this.#canUseButton(false)
return this.canUseButton(false)
}
override incrementValue(options: WidgetEventOptions): void {
this.#tryChangeValue(1, options)
this.tryChangeValue(1, options)
}
override decrementValue(options: WidgetEventOptions): void {
this.#tryChangeValue(-1, options)
this.tryChangeValue(-1, options)
}
#tryChangeValue(delta: number, options: WidgetEventOptions): void {
const values = this.#getValues(options.node)
tryChangeValue(delta: number, options: WidgetEventOptions): void {
const values = this.getValues(options.node)
const indexedValues = toArray(values)
// avoids double click event
@@ -128,7 +128,7 @@ export class ComboWidget
if (x > width - 40) return this.incrementValue({ e, node, canvas })
// Otherwise, show dropdown menu
const values = this.#getValues(node)
const values = this.getValues(node)
const values_list = toArray(values)
// Handle center click - show dropdown menu

View File

@@ -1,161 +0,0 @@
import type { BaseWidget } from './BaseWidget'
function getProxy2() {
return {
__proto__:
}
}
function getProxy() {
const overlay: object = {}
const handler = Object.fromEntries(['get', 'set'].map((s) => {
const func = function(t,p,r) => {
if (s == 'get' && p == '_overlay')
return overlay
if (!['y', 'last_y', 'width'].includes(p))
t = overlay
else
t = linkedWidget(overlay.graph, overlay.nodeId, overlay.widgetName)
if (!t)
return//TODO: pass to overlay subitem to display a disconnected state
return Reflect[s](t,p,r)
}
return [s, func]
}))
return new Proxy(overlay, handler)
}
function linkedWidget(graph, nodeId, widgetName) {
let g = g
let n = undefined
for (let id of nodeId.split(':')) {
n = g?.nodes_by_id[id]
graph = n?.subgraph
}
if (!node) return
return node.widgets.find((w) => w.name == widgetName)
}
function linkedWidget(graph: Subgraph, nodeId: string, widgetName): IBaseWidget|undefined {
let g: Subgraph|undefined = g
let n: LGraphNode|SubgraphNode|undefined = undefined
for (let id of nodeId.split(':')) {
n = g?.nodes_by_id[id]
graph = n?.subgraph
}
if (!node) return
return node.widgets.find((w) => w.name == widgetName)
}
function createMirrorWidget(nodeId: string, widgetName: string) {
const base = {pos: [0,0], size: [0,0]}
}
export class MirrorWidget extends BaseWidget{
linkedNodeId: string
//TODO: Add node ref to detect id reuse
linkedWidgetName: string
get _linkedWidget(): IBaseWidget|undefined {
let graph: Subgraph|undefined = this.node.subgraph
let node: LGraphNode|SubgraphNode|undefined = undefined
for (let id of this.linkedNodeId.split(':')) {
node = graph.nodes_by_id[id]
graph = node?.subgraph
}
if (!node) return
return node.widgets.find((w) => w.name == this.linkedWidgetName)
}
mockedWidget(): IBaseWidget {
return new Proxy(this._linkedWidget, handler)
}
override get _displayValue() {
}
override canIncrement(): boolean {
const { max } = this.options
return max == null || this.value < max
}
override canDecrement(): boolean {
const { min } = this.options
return min == null || this.value > min
}
override incrementValue(options: WidgetEventOptions): void {
this.setValue(this.value + getWidgetStep(this.options), options)
}
override decrementValue(options: WidgetEventOptions): void {
this.setValue(this.value - getWidgetStep(this.options), options)
}
override setValue(value: number, options: WidgetEventOptions) {
let newValue = value
if (this.options.min != null && newValue < this.options.min) {
newValue = this.options.min
}
if (this.options.max != null && newValue > this.options.max) {
newValue = this.options.max
}
super.setValue(newValue, options)
}
override onClick({ e, node, canvas }: WidgetEventOptions) {
const x = e.canvasX - node.pos[0]
const width = this.width || node.size[0]
// Determine if clicked on left/right arrows
const delta = x < 40 ? -1 : x > width - 40 ? 1 : 0
if (delta) {
// Handle left/right arrow clicks
this.setValue(this.value + delta * getWidgetStep(this.options), {
e,
node,
canvas
})
return
}
// Handle center click - show prompt
canvas.prompt(
'Value',
this.value,
(v: string) => {
// Check if v is a valid equation or a number
if (/^[\d\s()*+/-]+|\d+\.\d+$/.test(v)) {
// Solve the equation if possible
try {
v = eval(v)
} catch {
// Ignore eval errors
}
}
const newValue = Number(v)
if (!isNaN(newValue)) {
this.setValue(newValue, { e, node, canvas })
}
},
e
)
}
/**
* Handles drag events for the number widget
* @param options The options for handling the drag event
*/
override onDrag({ e, node, canvas }: WidgetEventOptions) {
const width = this.width || node.width
const x = e.canvasX - node.pos[0]
const delta = x < 40 ? -1 : x > width - 40 ? 1 : 0
if (delta && x > -3 && x < width + 3) return
this.setValue(this.value + (e.deltaX ?? 0) * getWidgetStep(this.options), {
e,
node,
canvas
})
}
}