Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node API: improved structure, make MapModel public #6164

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions api/node/__test__/api.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ test("loadFile constructor parameters", (t) => {
});

test("loadFile component instances and modules are sealed", (t) => {
"use strict";
const demo = loadFile(path.join(dirname, "resources/test.slint")) as any;

t.throws(
Expand Down Expand Up @@ -182,7 +181,6 @@ test("loadSource constructor parameters", (t) => {
});

test("loadSource component instances and modules are sealed", (t) => {
"use strict";
const source = `export component Test {
out property <string> check: "Test";
}`;
Expand Down
7 changes: 4 additions & 3 deletions api/node/__test__/js_value_conversion.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,12 @@ test("MapModel", (t) => {

instance!.setProperty("model", mapModel);

nameModel.setRowData(1, { first: "Simon", last: "Hausmann" });
nameModel.setRowData(0, { first: "Simon", last: "Hausmann" });
nameModel.setRowData(1, { first: "Olivier", last: "Goffart" });

const checkModel = instance!.getProperty("model") as Model<string>;
t.is(checkModel.rowData(0), "Emil, Hans");
t.is(checkModel.rowData(1), "Hausmann, Simon");
t.is(checkModel.rowData(0), "Hausmann, Simon");
t.is(checkModel.rowData(1), "Goffart, Olivier");
t.is(checkModel.rowData(2), "Tisch, Roman");
});

Expand Down
11 changes: 8 additions & 3 deletions api/node/typescript/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ export abstract class Model<T> implements Iterable<T> {
*/
modelNotify: napi.ExternalObject<napi.SharedModelNotify>;

constructor() {
this.modelNotify = napi.jsModelNotifyNew(this);
constructor(modelNotify?: napi.ExternalObject<napi.SharedModelNotify>) {
FloVanGH marked this conversation as resolved.
Show resolved Hide resolved
if (modelNotify === undefined) {
this.modelNotify = napi.jsModelNotifyNew(this);
return;
}

this.modelNotify = modelNotify;
FloVanGH marked this conversation as resolved.
Show resolved Hide resolved
}

// /**
Expand Down Expand Up @@ -404,7 +409,7 @@ export class MapModel<T, U> extends Model<U> {
* @param mapFunction maps the data from T to U.
*/
constructor(sourceModel: Model<T>, mapFunction: (data: T) => U) {
super();
super(sourceModel.modelNotify);
this.sourceModel = sourceModel;
this.#mapFunction = mapFunction;
}
Expand Down
44 changes: 44 additions & 0 deletions tests/cases/models/map_model.slint
FloVanGH marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

TestCase := Rectangle {
in-out property <[string]> model;
in-out property <string> changed-items;

for item in root.model : Text {
text: item;


changed text => {
root.changed-items += text;
}
}
}

/*

```js
var instance = new slint.TestCase();

let nameModel = new slintlib.ArrayModel([
{ first: "Hans", last: "Emil" },
{ first: "Max", last: "Mustermann" },
{ first: "Roman", last: "Tisch" },
]);

let mapModel = new slintlib.MapModel(nameModel, (data) => {
return data.last + ", " + data.first;
});
instance.model = mapModel;

slintlib.private_api.send_mouse_click(instance, 5., 5.);

nameModel.setRowData(0, { first: "Simon", last: "Hausmann" });
nameModel.setRowData(1, { first: "Olivier", last: "Goffart"});

slintlib.private_api.send_mouse_click(instance, 5., 5.);
const changedItems = instance.changed_items;
assert.equal(changedItems, "Goffart, OlivierHausmann, Simon");

```
*/
Loading