[Refactor] Remove repeat interface definitions (#976)

Prefers param destructuring over manual impl.
This commit is contained in:
filtered
2025-04-28 02:30:59 +10:00
committed by GitHub
parent 47732404cd
commit 81a00b5e6c
8 changed files with 28 additions and 109 deletions

View File

@@ -12,6 +12,12 @@ export interface DrawWidgetOptions {
margin?: number
}
export interface WidgetEventOptions {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}
export abstract class BaseWidget implements IBaseWidget {
/** From node edge to widget edge */
static margin = 15
@@ -117,33 +123,20 @@ export abstract class BaseWidget implements IBaseWidget {
* Handles the click event for the widget
* @param options The options for handling the click event
*/
abstract onClick(options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}): void
abstract onClick(options: WidgetEventOptions): void
/**
* Handles the drag event for the widget
* @param options The options for handling the drag event
*/
onDrag?(options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}): void
onDrag?(options: WidgetEventOptions): void
/**
* Sets the value of the widget
* @param value The value to set
* @param options The options for setting the value
*/
setValue(value: TWidgetValue, options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}) {
const { node, canvas, e } = options
setValue(value: TWidgetValue, { e, node, canvas }: WidgetEventOptions) {
const oldValue = this.value
if (value === this.value) return

View File

@@ -1,9 +1,6 @@
import type { LGraphCanvas } from "@/LGraphCanvas"
import type { LGraphNode } from "@/LGraphNode"
import type { CanvasMouseEvent } from "@/types/events"
import type { IBooleanWidget } from "@/types/widgets"
import { BaseWidget, type DrawWidgetOptions } from "./BaseWidget"
import { BaseWidget, type DrawWidgetOptions, type WidgetEventOptions } from "./BaseWidget"
export class BooleanWidget extends BaseWidget implements IBooleanWidget {
// IBooleanWidget properties
@@ -60,11 +57,7 @@ export class BooleanWidget extends BaseWidget implements IBooleanWidget {
}
}
override onClick(options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}) {
override onClick(options: WidgetEventOptions) {
this.setValue(!this.value, options)
}
}

View File

@@ -1,9 +1,6 @@
import type { LGraphCanvas } from "@/LGraphCanvas"
import type { LGraphNode } from "@/LGraphNode"
import type { CanvasMouseEvent } from "@/types/events"
import type { IButtonWidget, IWidgetOptions } from "@/types/widgets"
import { BaseWidget, type DrawWidgetOptions } from "./BaseWidget"
import { BaseWidget, type DrawWidgetOptions, type WidgetEventOptions } from "./BaseWidget"
export class ButtonWidget extends BaseWidget implements IButtonWidget {
// IButtonWidget properties
@@ -67,12 +64,7 @@ export class ButtonWidget extends BaseWidget implements IButtonWidget {
ctx.fillStyle = originalFillStyle
}
override onClick(options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}) {
const { e, node, canvas } = options
override onClick({ e, node, canvas }: WidgetEventOptions) {
const pos = canvas.graph_mouse
// Set clicked state and mark canvas as dirty

View File

@@ -1,11 +1,8 @@
import type { LGraphCanvas } from "@/LGraphCanvas"
import type { LGraphNode } from "@/LGraphNode"
import type { CanvasMouseEvent } from "@/types/events"
import type { IComboWidget, IWidgetOptions } from "@/types/widgets"
import { LiteGraph } from "@/litegraph"
import { BaseWidget, type DrawWidgetOptions } from "./BaseWidget"
import { BaseWidget, type DrawWidgetOptions, type WidgetEventOptions } from "./BaseWidget"
export class ComboWidget extends BaseWidget implements IComboWidget {
// IComboWidget properties
@@ -121,12 +118,7 @@ export class ComboWidget extends BaseWidget implements IComboWidget {
ctx.fillStyle = originalFillStyle
}
override onClick(options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}) {
const { e, node, canvas } = options
override onClick({ e, node, canvas }: WidgetEventOptions) {
const x = e.canvasX - node.pos[0]
const width = this.width || node.size[0]
@@ -163,11 +155,7 @@ export class ComboWidget extends BaseWidget implements IComboWidget {
Array.isArray(values)
? values[index]
: index,
{
e,
node,
canvas,
},
{ e, node, canvas },
)
return
}
@@ -184,11 +172,7 @@ export class ComboWidget extends BaseWidget implements IComboWidget {
values != values_list
? text_values.indexOf(value)
: value,
{
e,
node,
canvas,
},
{ e, node, canvas },
)
},
})

View File

@@ -1,12 +1,9 @@
import type { LGraphNode } from "@/LGraphNode"
import type { IKnobWidget, IWidgetKnobOptions } from "@/types/widgets"
import { LGraphCanvas } from "@/LGraphCanvas"
import { clamp } from "@/litegraph"
import { CanvasMouseEvent } from "@/types/events"
import { getWidgetStep } from "@/utils/widget"
import { BaseWidget, type DrawWidgetOptions } from "./BaseWidget"
import { BaseWidget, type DrawWidgetOptions, type WidgetEventOptions } from "./BaseWidget"
export class KnobWidget extends BaseWidget implements IKnobWidget {
declare type: "knob"
@@ -202,11 +199,7 @@ export class KnobWidget extends BaseWidget implements IKnobWidget {
}
current_drag_offset = 0
override onDrag(options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}): void {
override onDrag(options: WidgetEventOptions): void {
if (this.options.read_only) return
const { e } = options
const step = getWidgetStep(this.options)

View File

@@ -1,11 +1,8 @@
import type { LGraphCanvas } from "@/LGraphCanvas"
import type { LGraphNode } from "@/LGraphNode"
import type { CanvasMouseEvent } from "@/types/events"
import type { INumericWidget, IWidgetOptions } from "@/types/widgets"
import { getWidgetStep } from "@/utils/widget"
import { BaseWidget, type DrawWidgetOptions } from "./BaseWidget"
import { BaseWidget, type DrawWidgetOptions, type WidgetEventOptions } from "./BaseWidget"
export class NumberWidget extends BaseWidget implements INumericWidget {
// INumberWidget properties
@@ -19,11 +16,7 @@ export class NumberWidget extends BaseWidget implements INumericWidget {
this.value = widget.value
}
override setValue(value: number, options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}) {
override setValue(value: number, options: WidgetEventOptions) {
let newValue = value
if (this.options.min != null && newValue < this.options.min) {
newValue = this.options.min
@@ -96,12 +89,7 @@ export class NumberWidget extends BaseWidget implements INumericWidget {
ctx.fillStyle = originalFillStyle
}
override onClick(options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}) {
const { e, node, canvas } = options
override onClick({ e, node, canvas }: WidgetEventOptions) {
const x = e.canvasX - node.pos[0]
const width = this.width || node.size[0]
@@ -138,11 +126,7 @@ export class NumberWidget extends BaseWidget implements INumericWidget {
* Handles drag events for the number widget
* @param options The options for handling the drag event
*/
override onDrag(options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}) {
override onDrag(options: WidgetEventOptions) {
const { e, node, canvas } = options
const width = this.width || node.width
const x = e.canvasX - node.pos[0]

View File

@@ -1,11 +1,8 @@
import type { LGraphCanvas } from "@/LGraphCanvas"
import type { LGraphNode } from "@/LGraphNode"
import type { CanvasMouseEvent } from "@/types/events"
import type { ISliderWidget, IWidgetSliderOptions } from "@/types/widgets"
import { clamp } from "@/litegraph"
import { BaseWidget, type DrawWidgetOptions } from "./BaseWidget"
import { BaseWidget, type DrawWidgetOptions, type WidgetEventOptions } from "./BaseWidget"
export class SliderWidget extends BaseWidget implements ISliderWidget {
// ISliderWidget properties
@@ -93,11 +90,7 @@ export class SliderWidget extends BaseWidget implements ISliderWidget {
/**
* Handles click events for the slider widget
*/
override onClick(options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}) {
override onClick(options: WidgetEventOptions) {
if (this.options.read_only) return
const { e, node } = options
@@ -116,11 +109,7 @@ export class SliderWidget extends BaseWidget implements ISliderWidget {
/**
* Handles drag events for the slider widget
*/
override onDrag(options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}) {
override onDrag(options: WidgetEventOptions) {
if (this.options.read_only) return false
const { e, node } = options

View File

@@ -1,9 +1,6 @@
import type { LGraphCanvas } from "@/LGraphCanvas"
import type { LGraphNode } from "@/LGraphNode"
import type { CanvasMouseEvent } from "@/types/events"
import type { IStringWidget, IWidgetOptions } from "@/types/widgets"
import { BaseWidget, type DrawWidgetOptions } from "./BaseWidget"
import { BaseWidget, type DrawWidgetOptions, type WidgetEventOptions } from "./BaseWidget"
export class TextWidget extends BaseWidget implements IStringWidget {
// IStringWidget properties
@@ -78,13 +75,7 @@ export class TextWidget extends BaseWidget implements IStringWidget {
ctx.fillStyle = originalFillStyle
}
override onClick(options: {
e: CanvasMouseEvent
node: LGraphNode
canvas: LGraphCanvas
}) {
const { e, node, canvas } = options
override onClick({ e, node, canvas }: WidgetEventOptions) {
// Show prompt dialog for text input
canvas.prompt(
"Value",