slint-ui 1.7.0-nightly.2024070208 → 1.7.0-nightly.2024070318

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Cargo.toml CHANGED
@@ -40,10 +40,10 @@ accessibility = ["slint-interpreter/accessibility"]
40
40
  [dependencies]
41
41
  napi = { version = "2.14.0", default-features = false, features = ["napi8"] }
42
42
  napi-derive = "2.14.0"
43
- i-slint-compiler = { features = ["default"] , git = "https://github.com/slint-ui/slint", rev = "a24c04fd84d53879d5cc389c78af8d4d334e6a6a", version = "=1.7.0", default-features = false }
44
- i-slint-core = { features = ["default"] , git = "https://github.com/slint-ui/slint", rev = "a24c04fd84d53879d5cc389c78af8d4d334e6a6a", version = "=1.7.0", default-features = false }
45
- i-slint-backend-selector = { git = "https://github.com/slint-ui/slint", rev = "a24c04fd84d53879d5cc389c78af8d4d334e6a6a", version = "=1.7.0", default-features = false }
46
- slint-interpreter = { default-features = false , features = ["display-diagnostics", "internal", "compat-1-2"] , git = "https://github.com/slint-ui/slint", rev = "a24c04fd84d53879d5cc389c78af8d4d334e6a6a", version = "=1.7.0"}
43
+ i-slint-compiler = { features = ["default"] , git = "https://github.com/slint-ui/slint", rev = "8117e9b29cb7fd744b7c3bc13eae09b6a2b18af3", version = "=1.7.0", default-features = false }
44
+ i-slint-core = { features = ["default"] , git = "https://github.com/slint-ui/slint", rev = "8117e9b29cb7fd744b7c3bc13eae09b6a2b18af3", version = "=1.7.0", default-features = false }
45
+ i-slint-backend-selector = { git = "https://github.com/slint-ui/slint", rev = "8117e9b29cb7fd744b7c3bc13eae09b6a2b18af3", version = "=1.7.0", default-features = false }
46
+ slint-interpreter = { default-features = false , features = ["display-diagnostics", "internal", "compat-1-2"] , git = "https://github.com/slint-ui/slint", rev = "8117e9b29cb7fd744b7c3bc13eae09b6a2b18af3", version = "=1.7.0"}
47
47
  spin_on = { version = "0.1" }
48
48
  css-color-parser2 = { version = "1.0.1" }
49
49
  itertools = { version = "0.13" }
package/README.md CHANGED
@@ -85,7 +85,7 @@ export component MainWindow inherits Window {
85
85
  }
86
86
  ```
87
87
 
88
- The exported component is exposed as a type constructor. The type constructor takes as parameter
88
+ Each exported Window component is exposed as a type constructor. The type constructor takes as parameter
89
89
  an object which allow to initialize the value of public properties or callbacks.
90
90
 
91
91
  **`main.js`**
@@ -144,6 +144,37 @@ component.clicked = function() { console.log("hello"); };
144
144
  component.clicked();
145
145
  ```
146
146
 
147
+ ### Functions
148
+
149
+ Functions in Slint can be defined using the `function` keyword.
150
+
151
+ **`ui/my-component.slint`**
152
+
153
+ ```slint
154
+ export component MyComponent inherits Window {
155
+ width: 400px;
156
+ height: 200px;
157
+
158
+ public function my-function() -> int {
159
+ return 42;
160
+ }
161
+ }
162
+ ```
163
+
164
+ If the function is marked `public`, it can also be called from JavaScript.
165
+
166
+ **`main.js`**
167
+
168
+ ```js
169
+ import * as slint from "slint-ui";
170
+
171
+ let ui = slint.loadFile("ui/my-component.slint");
172
+ let component = new ui.MyComponent();
173
+
174
+ // call a public function
175
+ let result = component.my_function();
176
+ ```
177
+
147
178
  ### Type Mappings
148
179
 
149
180
  The types used for properties in .slint design markup each translate to specific types in JavaScript. The follow table summarizes the entire mapping:
package/index.d.ts CHANGED
@@ -476,13 +476,13 @@ export interface LoadFileOptions {
476
476
  libraryPaths?: Record<string, string>;
477
477
  }
478
478
  /**
479
- * Loads the given Slint file and returns an objects that contains a functions to construct the exported
480
- * component of the slint file.
479
+ * Loads the specified Slint file and returns an object containing functions to construct the exported
480
+ * components defined within the Slint file.
481
481
  *
482
482
  * The following example loads a "Hello World" style Slint file and changes the Text label to a new greeting:
483
- * `main.slint`:
483
+ * **`main.slint`**:
484
484
  * ```
485
- * export component Main {
485
+ * export component Main inherits Window {
486
486
  * in-out property <string> greeting <=> label.text;
487
487
  * label := Text {
488
488
  * text: "Hello World";
@@ -490,28 +490,27 @@ export interface LoadFileOptions {
490
490
  * }
491
491
  * ```
492
492
  *
493
- * ```js
493
+ * **`index.js`**:
494
+ * ```javascript
494
495
  * import * as slint from "slint-ui";
495
496
  * let ui = slint.loadFile("main.slint");
496
497
  * let main = new ui.Main();
497
498
  * main.greeting = "Hello friends";
498
499
  * ```
499
500
  *
500
- * @param filePath A path to the file to load. If the path is a relative path, then it is resolved
501
- * against the process' working directory.
502
- * @param options Use {@link LoadFileOptions} to configure additional Slint compilation aspects,
501
+ * @param filePath The path to the file to load. Relative paths are resolved against the process' current working directory.
502
+ * @param options An optional {@link LoadFileOptions} to configure additional Slint compilation settings,
503
503
  * such as include search paths, library imports, or the widget style.
504
- * @returns The returned object is sealed and provides a property by the name of the component exported
505
- * in the `.slint` file. In the above example the name of the property is `Main`. The property
506
- * is a constructor function. Use it with the new operator to instantiate the component.
507
- * The instantiated object exposes properties and callbacks, and implements the {@link ComponentHandle} interface.
508
- * For more details about the exposed properties, see [Instantiating A Component](../index.html#md:instantiating-a-component).
504
+ * @returns Returns an object that is immutable and provides a constructor function for each exported Window component found in the `.slint` file.
505
+ * For instance, in the example above, a `Main` property is available, which can be used to create instances of the `Main` component using the `new` keyword.
506
+ * These instances offer properties and event handlers, adhering to the {@link ComponentHandle} interface.
507
+ * For further information on the available properties, refer to [Instantiating A Component](../index.html#md:instantiating-a-component).
509
508
  * @throws {@link CompileError} if errors occur during compilation.
510
509
  */
511
510
  export declare function loadFile(filePath: string, options?: LoadFileOptions): Object;
512
511
  /**
513
- * Loads the given Slint source code and returns an object that contains a function to construct the exported
514
- * component of the Slint source code.
512
+ * Loads the given Slint source code and returns an object that contains a functions to construct the exported
513
+ * components of the Slint source code.
515
514
  *
516
515
  * The following example loads a "Hello World" style Slint source code and changes the Text label to a new greeting:
517
516
  * ```js
@@ -527,15 +526,14 @@ export declare function loadFile(filePath: string, options?: LoadFileOptions): O
527
526
  * main.greeting = "Hello friends";
528
527
  * ```
529
528
  * @param source The Slint source code to load.
530
- * @param filePath A path to the file to show log. If the path is a relative path, then it is resolved
531
- * against the process' working directory.
532
- * @param options Use {@link LoadFileOptions} to configure additional Slint compilation aspects,
529
+ * @param filePath A path to the file to show log and resolve relative import and images.
530
+ * Relative paths are resolved against the process' current working directory.
531
+ * @param options An optional {@link LoadFileOptions} to configure additional Slint compilation settings,
533
532
  * such as include search paths, library imports, or the widget style.
534
- * @returns The returned object is sealed and provides a property by the name of the component exported
535
- * in the `.slint` file. In the above example the name of the property is `Main`. The property
536
- * is a constructor function. Use it with the new operator to instantiate the component.
537
- * The instantiated object exposes properties and callbacks, and implements the {@link ComponentHandle} interface.
538
- * For more details about the exposed properties, see [Instantiating A Component](../index.html#md:instantiating-a-component).
533
+ * @returns Returns an object that is immutable and provides a constructor function for each exported Window component found in the `.slint` file.
534
+ * For instance, in the example above, a `Main` property is available, which can be used to create instances of the `Main` component using the `new` keyword.
535
+ * These instances offer properties and event handlers, adhering to the {@link ComponentHandle} interface.
536
+ * For further information on the available properties, refer to [Instantiating A Component](../index.html#md:instantiating-a-component).
539
537
  * @throws {@link CompileError} if errors occur during compilation.
540
538
  */
541
539
  export declare function loadSource(source: string, filePath: string, options?: LoadFileOptions): Object;
package/index.js CHANGED
@@ -452,7 +452,7 @@ function loadSlint(loadData) {
452
452
  compiler.libraryPaths = options.libraryPaths;
453
453
  }
454
454
  }
455
- let definition = loadData.from === 'file' ? compiler.buildFromPath(filePath) : compiler.buildFromSource(loadData.fileData.source, filePath);
455
+ let definitions = loadData.from === 'file' ? compiler.buildFromPath(filePath) : compiler.buildFromSource(loadData.fileData.source, filePath);
456
456
  let diagnostics = compiler.diagnostics;
457
457
  if (diagnostics.length > 0) {
458
458
  let warnings = diagnostics.filter((d) => d.level == napi.DiagnosticLevel.Warning);
@@ -465,154 +465,157 @@ function loadSlint(loadData) {
465
465
  }
466
466
  }
467
467
  let slint_module = Object.create({});
468
- Object.defineProperty(slint_module, definition.name.replace(/-/g, "_"), {
469
- value: function (properties) {
470
- let instance = definition.create();
471
- if (instance == null) {
472
- throw Error("Could not create a component handle for" + filePath);
473
- }
474
- for (var key in properties) {
475
- let value = properties[key];
476
- if (value instanceof Function) {
477
- instance.setCallback(key, value);
478
- }
479
- else {
480
- instance.setProperty(key, properties[key]);
481
- }
482
- }
483
- let componentHandle = new Component(instance);
484
- instance.definition().properties.forEach((prop) => {
485
- let propName = prop.name.replace(/-/g, "_");
486
- if (componentHandle[propName] !== undefined) {
487
- console.warn("Duplicated property name " + propName);
488
- }
489
- else {
490
- Object.defineProperty(componentHandle, propName, {
491
- get() {
492
- return instance.getProperty(prop.name);
493
- },
494
- set(value) {
495
- instance.setProperty(prop.name, value);
496
- },
497
- enumerable: true,
498
- });
499
- }
500
- });
501
- instance.definition().callbacks.forEach((cb) => {
502
- let callbackName = cb.replace(/-/g, "_");
503
- if (componentHandle[callbackName] !== undefined) {
504
- console.warn("Duplicated callback name " + callbackName);
505
- }
506
- else {
507
- Object.defineProperty(componentHandle, cb.replace(/-/g, "_"), {
508
- get() {
509
- return function () {
510
- return instance.invoke(cb, Array.from(arguments));
511
- };
512
- },
513
- set(callback) {
514
- instance.setCallback(cb, callback);
515
- },
516
- enumerable: true,
517
- });
468
+ Object.keys(definitions).forEach((key) => {
469
+ let definition = definitions[key];
470
+ Object.defineProperty(slint_module, definition.name.replace(/-/g, "_"), {
471
+ value: function (properties) {
472
+ let instance = definition.create();
473
+ if (instance == null) {
474
+ throw Error("Could not create a component handle for" + filePath);
518
475
  }
519
- });
520
- instance.definition().functions.forEach((cb) => {
521
- let functionName = cb.replace(/-/g, "_");
522
- if (componentHandle[functionName] !== undefined) {
523
- console.warn("Duplicated function name " + functionName);
524
- }
525
- else {
526
- Object.defineProperty(componentHandle, cb.replace(/-/g, "_"), {
527
- get() {
528
- return function () {
529
- return instance.invoke(cb, Array.from(arguments));
530
- };
531
- },
532
- enumerable: true,
533
- });
534
- }
535
- });
536
- // globals
537
- instance.definition().globals.forEach((globalName) => {
538
- if (componentHandle[globalName] !== undefined) {
539
- console.warn("Duplicated property name " + globalName);
476
+ for (var key in properties) {
477
+ let value = properties[key];
478
+ if (value instanceof Function) {
479
+ instance.setCallback(key, value);
480
+ }
481
+ else {
482
+ instance.setProperty(key, properties[key]);
483
+ }
540
484
  }
541
- else {
542
- let globalObject = Object.create({});
543
- instance.definition().globalProperties(globalName).forEach((prop) => {
544
- let propName = prop.name.replace(/-/g, "_");
545
- if (globalObject[propName] !== undefined) {
546
- console.warn("Duplicated property name " + propName + " on global " + global);
547
- }
548
- else {
549
- Object.defineProperty(globalObject, propName, {
550
- get() {
551
- return instance.getGlobalProperty(globalName, prop.name);
552
- },
553
- set(value) {
554
- instance.setGlobalProperty(globalName, prop.name, value);
555
- },
556
- enumerable: true,
557
- });
558
- }
559
- });
560
- instance.definition().globalCallbacks(globalName).forEach((cb) => {
561
- let callbackName = cb.replace(/-/g, "_");
562
- if (globalObject[callbackName] !== undefined) {
563
- console.warn("Duplicated property name " + cb + " on global " + global);
564
- }
565
- else {
566
- Object.defineProperty(globalObject, cb.replace(/-/g, "_"), {
567
- get() {
568
- return function () {
569
- return instance.invokeGlobal(globalName, cb, Array.from(arguments));
570
- };
571
- },
572
- set(callback) {
573
- instance.setGlobalCallback(globalName, cb, callback);
574
- },
575
- enumerable: true,
576
- });
577
- }
578
- });
579
- instance.definition().globalFunctions(globalName).forEach((cb) => {
580
- let functionName = cb.replace(/-/g, "_");
581
- if (globalObject[functionName] !== undefined) {
582
- console.warn("Duplicated function name " + cb + " on global " + global);
583
- }
584
- else {
585
- Object.defineProperty(globalObject, cb.replace(/-/g, "_"), {
586
- get() {
587
- return function () {
588
- return instance.invokeGlobal(globalName, cb, Array.from(arguments));
589
- };
590
- },
591
- enumerable: true,
592
- });
593
- }
594
- });
595
- Object.defineProperty(componentHandle, globalName, {
596
- get() {
597
- return globalObject;
598
- },
599
- enumerable: true,
600
- });
601
- }
602
- });
603
- return Object.seal(componentHandle);
604
- },
485
+ let componentHandle = new Component(instance);
486
+ instance.definition().properties.forEach((prop) => {
487
+ let propName = prop.name.replace(/-/g, "_");
488
+ if (componentHandle[propName] !== undefined) {
489
+ console.warn("Duplicated property name " + propName);
490
+ }
491
+ else {
492
+ Object.defineProperty(componentHandle, propName, {
493
+ get() {
494
+ return instance.getProperty(prop.name);
495
+ },
496
+ set(value) {
497
+ instance.setProperty(prop.name, value);
498
+ },
499
+ enumerable: true,
500
+ });
501
+ }
502
+ });
503
+ instance.definition().callbacks.forEach((cb) => {
504
+ let callbackName = cb.replace(/-/g, "_");
505
+ if (componentHandle[callbackName] !== undefined) {
506
+ console.warn("Duplicated callback name " + callbackName);
507
+ }
508
+ else {
509
+ Object.defineProperty(componentHandle, cb.replace(/-/g, "_"), {
510
+ get() {
511
+ return function () {
512
+ return instance.invoke(cb, Array.from(arguments));
513
+ };
514
+ },
515
+ set(callback) {
516
+ instance.setCallback(cb, callback);
517
+ },
518
+ enumerable: true,
519
+ });
520
+ }
521
+ });
522
+ instance.definition().functions.forEach((cb) => {
523
+ let functionName = cb.replace(/-/g, "_");
524
+ if (componentHandle[functionName] !== undefined) {
525
+ console.warn("Duplicated function name " + functionName);
526
+ }
527
+ else {
528
+ Object.defineProperty(componentHandle, cb.replace(/-/g, "_"), {
529
+ get() {
530
+ return function () {
531
+ return instance.invoke(cb, Array.from(arguments));
532
+ };
533
+ },
534
+ enumerable: true,
535
+ });
536
+ }
537
+ });
538
+ // globals
539
+ instance.definition().globals.forEach((globalName) => {
540
+ if (componentHandle[globalName] !== undefined) {
541
+ console.warn("Duplicated property name " + globalName);
542
+ }
543
+ else {
544
+ let globalObject = Object.create({});
545
+ instance.definition().globalProperties(globalName).forEach((prop) => {
546
+ let propName = prop.name.replace(/-/g, "_");
547
+ if (globalObject[propName] !== undefined) {
548
+ console.warn("Duplicated property name " + propName + " on global " + global);
549
+ }
550
+ else {
551
+ Object.defineProperty(globalObject, propName, {
552
+ get() {
553
+ return instance.getGlobalProperty(globalName, prop.name);
554
+ },
555
+ set(value) {
556
+ instance.setGlobalProperty(globalName, prop.name, value);
557
+ },
558
+ enumerable: true,
559
+ });
560
+ }
561
+ });
562
+ instance.definition().globalCallbacks(globalName).forEach((cb) => {
563
+ let callbackName = cb.replace(/-/g, "_");
564
+ if (globalObject[callbackName] !== undefined) {
565
+ console.warn("Duplicated property name " + cb + " on global " + global);
566
+ }
567
+ else {
568
+ Object.defineProperty(globalObject, cb.replace(/-/g, "_"), {
569
+ get() {
570
+ return function () {
571
+ return instance.invokeGlobal(globalName, cb, Array.from(arguments));
572
+ };
573
+ },
574
+ set(callback) {
575
+ instance.setGlobalCallback(globalName, cb, callback);
576
+ },
577
+ enumerable: true,
578
+ });
579
+ }
580
+ });
581
+ instance.definition().globalFunctions(globalName).forEach((cb) => {
582
+ let functionName = cb.replace(/-/g, "_");
583
+ if (globalObject[functionName] !== undefined) {
584
+ console.warn("Duplicated function name " + cb + " on global " + global);
585
+ }
586
+ else {
587
+ Object.defineProperty(globalObject, cb.replace(/-/g, "_"), {
588
+ get() {
589
+ return function () {
590
+ return instance.invokeGlobal(globalName, cb, Array.from(arguments));
591
+ };
592
+ },
593
+ enumerable: true,
594
+ });
595
+ }
596
+ });
597
+ Object.defineProperty(componentHandle, globalName, {
598
+ get() {
599
+ return globalObject;
600
+ },
601
+ enumerable: true,
602
+ });
603
+ }
604
+ });
605
+ return Object.seal(componentHandle);
606
+ },
607
+ });
605
608
  });
606
609
  return Object.seal(slint_module);
607
610
  }
608
611
  /**
609
- * Loads the given Slint file and returns an objects that contains a functions to construct the exported
610
- * component of the slint file.
612
+ * Loads the specified Slint file and returns an object containing functions to construct the exported
613
+ * components defined within the Slint file.
611
614
  *
612
615
  * The following example loads a "Hello World" style Slint file and changes the Text label to a new greeting:
613
- * `main.slint`:
616
+ * **`main.slint`**:
614
617
  * ```
615
- * export component Main {
618
+ * export component Main inherits Window {
616
619
  * in-out property <string> greeting <=> label.text;
617
620
  * label := Text {
618
621
  * text: "Hello World";
@@ -620,22 +623,21 @@ function loadSlint(loadData) {
620
623
  * }
621
624
  * ```
622
625
  *
623
- * ```js
626
+ * **`index.js`**:
627
+ * ```javascript
624
628
  * import * as slint from "slint-ui";
625
629
  * let ui = slint.loadFile("main.slint");
626
630
  * let main = new ui.Main();
627
631
  * main.greeting = "Hello friends";
628
632
  * ```
629
633
  *
630
- * @param filePath A path to the file to load. If the path is a relative path, then it is resolved
631
- * against the process' working directory.
632
- * @param options Use {@link LoadFileOptions} to configure additional Slint compilation aspects,
634
+ * @param filePath The path to the file to load. Relative paths are resolved against the process' current working directory.
635
+ * @param options An optional {@link LoadFileOptions} to configure additional Slint compilation settings,
633
636
  * such as include search paths, library imports, or the widget style.
634
- * @returns The returned object is sealed and provides a property by the name of the component exported
635
- * in the `.slint` file. In the above example the name of the property is `Main`. The property
636
- * is a constructor function. Use it with the new operator to instantiate the component.
637
- * The instantiated object exposes properties and callbacks, and implements the {@link ComponentHandle} interface.
638
- * For more details about the exposed properties, see [Instantiating A Component](../index.html#md:instantiating-a-component).
637
+ * @returns Returns an object that is immutable and provides a constructor function for each exported Window component found in the `.slint` file.
638
+ * For instance, in the example above, a `Main` property is available, which can be used to create instances of the `Main` component using the `new` keyword.
639
+ * These instances offer properties and event handlers, adhering to the {@link ComponentHandle} interface.
640
+ * For further information on the available properties, refer to [Instantiating A Component](../index.html#md:instantiating-a-component).
639
641
  * @throws {@link CompileError} if errors occur during compilation.
640
642
  */
641
643
  function loadFile(filePath, options) {
@@ -646,8 +648,8 @@ function loadFile(filePath, options) {
646
648
  }
647
649
  exports.loadFile = loadFile;
648
650
  /**
649
- * Loads the given Slint source code and returns an object that contains a function to construct the exported
650
- * component of the Slint source code.
651
+ * Loads the given Slint source code and returns an object that contains a functions to construct the exported
652
+ * components of the Slint source code.
651
653
  *
652
654
  * The following example loads a "Hello World" style Slint source code and changes the Text label to a new greeting:
653
655
  * ```js
@@ -663,15 +665,14 @@ exports.loadFile = loadFile;
663
665
  * main.greeting = "Hello friends";
664
666
  * ```
665
667
  * @param source The Slint source code to load.
666
- * @param filePath A path to the file to show log. If the path is a relative path, then it is resolved
667
- * against the process' working directory.
668
- * @param options Use {@link LoadFileOptions} to configure additional Slint compilation aspects,
668
+ * @param filePath A path to the file to show log and resolve relative import and images.
669
+ * Relative paths are resolved against the process' current working directory.
670
+ * @param options An optional {@link LoadFileOptions} to configure additional Slint compilation settings,
669
671
  * such as include search paths, library imports, or the widget style.
670
- * @returns The returned object is sealed and provides a property by the name of the component exported
671
- * in the `.slint` file. In the above example the name of the property is `Main`. The property
672
- * is a constructor function. Use it with the new operator to instantiate the component.
673
- * The instantiated object exposes properties and callbacks, and implements the {@link ComponentHandle} interface.
674
- * For more details about the exposed properties, see [Instantiating A Component](../index.html#md:instantiating-a-component).
672
+ * @returns Returns an object that is immutable and provides a constructor function for each exported Window component found in the `.slint` file.
673
+ * For instance, in the example above, a `Main` property is available, which can be used to create instances of the `Main` component using the `new` keyword.
674
+ * These instances offer properties and event handlers, adhering to the {@link ComponentHandle} interface.
675
+ * For further information on the available properties, refer to [Instantiating A Component](../index.html#md:instantiating-a-component).
675
676
  * @throws {@link CompileError} if errors occur during compilation.
676
677
  */
677
678
  function loadSource(source, filePath, options) {