Skip to content

Commit

Permalink
Fix widget() control not accepting any specific widget properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Basssiiie committed Apr 18, 2024
1 parent 6fa269e commit 89673b7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/elements/controls/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export abstract class Control<W extends WidgetBaseDesc, Positioning, ParsedPosit
{
name: string = identifier();
type: W["type"];
x: number = 0;
y: number = 0;
width: number = 0;
height: number = 0;
x: number;
y: number;
width: number;
height: number;

tooltip?: string;
isDisabled?: boolean;
Expand All @@ -31,6 +31,7 @@ export abstract class Control<W extends WidgetBaseDesc, Positioning, ParsedPosit
{
super(parent, params);
this.type = type;
this.x = this.y = this.width = this.height = 0;

const { binder, context } = output, visibility = params.visibility;
binder.add(this, "tooltip", params.tooltip);
Expand Down
30 changes: 27 additions & 3 deletions src/elements/controls/widget.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { BuildOutput } from "@src/windows/buildOutput";
import { ParentControl } from "@src/windows/parentControl";
import { WidgetCreator } from "@src/windows/widgets/widgetCreator";
import { ElementParams } from "../elementParams";
import { AbsolutePosition } from "../layouts/absolute/absolutePosition";
import { FlexiblePosition } from "../layouts/flexible/flexiblePosition";
import { Control } from "./control";
Expand All @@ -7,7 +10,8 @@ import { Control } from "./control";
/**
* The parameters for configuring a custom widget.
*/
export type WidgetParams = Omit<WidgetDesc, "x" | "y" | "width" | "height">;
export type WidgetParams<W extends WidgetDesc = WidgetDesc> = ElementParams
& (W extends WidgetBaseDesc ? Omit<W, "x" | "y" | "width" | "height" | "name" | "tooltip" | "isVisible" | "isDisabled"> : never);


/**
Expand All @@ -21,9 +25,29 @@ export function widget<I, P>(params: WidgetParams & I): WidgetCreator<I, P>
}


// All keys that should be ignored on the params object.
const omittedKeys =
[
"x", "y", "width", "height", "name", "tooltip", "isVisible", "isDisabled"
];


/**
* A controller class for a custom widget.
*/
class WidgetControl<I, P> extends Control<WidgetDesc, I, P> implements WidgetParams
class WidgetControl<I, P> extends Control<WidgetDesc, I, P>
{
}
constructor(type: WidgetType, parent: ParentControl<I, P>, output: BuildOutput, params: WidgetParams & I)
{
super(type, parent, output, params);

for (const key in params)
{
if (omittedKeys.indexOf(key) == -1)
{
// @ts-expect-error This mapping defies TS logic.
this[key] = params[key];
}
}
}
}
46 changes: 46 additions & 0 deletions tests/elements/controls/widget.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// <reference path="../../../lib/openrct2.d.ts" />

import { widget } from "@src/elements/controls/widget";
import { window } from "@src/windows/window";
import test from "ava";
import Mock from "openrct2-mocks";


test("Standard properties are set", t =>
{
const mock = Mock.ui();
globalThis.ui = mock;

const callback = (): void => {};
const template = window({
width: 100, height: 100, padding: 10,
content: [
widget({
type: "button",
width: "50%",
text: "Click me!",
tooltip: "Best button in the world",
isPressed: true,
onClick: callback,
// @ts-expect-error These properties should be skipped
isVisible: true, x: 25, y: 3421, name: "bob", isDisabled: true
})
]
});
template.open();

const result = mock.createdWindows[0].widgets[0] as ButtonDesc;
t.is(result.type, "button");
t.is(result.text, "Click me!");
t.is(result.tooltip, "Best button in the world");
t.is(result.x, 10);
t.is(result.y, 25);
t.is(result.width, 40);
t.is(result.height, 65);
t.is(result.onClick, callback);
t.true(result.isPressed);

t.not(result.name, "bob");
t.falsy(result.isVisible);
t.falsy(result.isDisabled);
});

0 comments on commit 89673b7

Please sign in to comment.