From aa00ea8a12d1a02a6301030c068143bc6d187a1d Mon Sep 17 00:00:00 2001 From: Florian Blasius Date: Wed, 9 Oct 2024 10:03:10 +0200 Subject: [PATCH 1/6] material: base setup components import --- Cargo.toml | 3 +- examples/material-gallery/Cargo.toml | 42 +++++++++++++++++++ examples/material-gallery/build.rs | 6 +++ examples/material-gallery/src/main.rs | 14 +++++++ examples/material-gallery/ui/index.slint | 11 +++++ internal/compiler/build.rs | 5 +++ internal/compiler/components/headless.slint | 4 ++ .../components/headless/button-base.slint | 10 +++++ internal/compiler/components/material.slint | 4 ++ .../components/material/filled-button.slint | 6 +++ internal/compiler/lib.rs | 7 +++- 11 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 examples/material-gallery/Cargo.toml create mode 100644 examples/material-gallery/build.rs create mode 100644 examples/material-gallery/src/main.rs create mode 100644 examples/material-gallery/ui/index.slint create mode 100644 internal/compiler/components/headless.slint create mode 100644 internal/compiler/components/headless/button-base.slint create mode 100644 internal/compiler/components/material.slint create mode 100644 internal/compiler/components/material/filled-button.slint diff --git a/Cargo.toml b/Cargo.toml index c7676958e1c..c144969a855 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ members = [ 'docs/reference/src/quickstart', 'examples/7guis', 'examples/gallery', + 'examples/material-gallery', 'examples/imagefilter/rust', 'examples/maps', 'examples/memory', @@ -70,7 +71,7 @@ members = [ default-members = [ 'api/rs/build', 'api/rs/slint', - 'examples/gallery', + 'examples/material-gallery', 'examples/memory', 'examples/printerdemo_old/rust', 'examples/printerdemo/rust', diff --git a/examples/material-gallery/Cargo.toml b/examples/material-gallery/Cargo.toml new file mode 100644 index 00000000000..b35df64a250 --- /dev/null +++ b/examples/material-gallery/Cargo.toml @@ -0,0 +1,42 @@ +# Copyright © SixtyFPS GmbH +# SPDX-License-Identifier: MIT + +[package] +name = "material-gallery" +version = "1.9.0" +authors = ["Slint Developers "] +edition = "2021" +build = "build.rs" +license = "MIT" +publish = false +description = "Slint Widgets Gallery Example" + +[[bin]] +path = "src/main.rs" +name = "material-gallery" + +[dependencies] +slint = { path = "../../api/rs/slint" } + +# Disable gettext on macOS due to https://github.com/Koka/gettext-rs/issues/114 +[target.'cfg(not(target_os = "macos"))'.dependencies] +slint = { path = "../../api/rs/slint", features=["gettext"] } + +[build-dependencies] +slint-build = { path = "../../api/rs/build" } + +# Remove the `#wasm#` to uncomment the wasm build. +# This is commented out by default because we don't want to build it as a library by default +# The CI has a script that does sed "s/#wasm# //" to generate the wasm build. + +#wasm# [lib] +#wasm# crate-type = ["cdylib"] +#wasm# path = "main.rs" +#wasm# +#wasm# [target.'cfg(target_arch = "wasm32")'.dependencies] +#wasm# wasm-bindgen = { version = "0.2" } +#wasm# web-sys = { version = "0.3", features=["console"] } +#wasm# console_error_panic_hook = "0.1.5" + +[package.metadata.bundle] +identifier = "com.slint.examples.material-gallery" diff --git a/examples/material-gallery/build.rs b/examples/material-gallery/build.rs new file mode 100644 index 00000000000..f93082aca2f --- /dev/null +++ b/examples/material-gallery/build.rs @@ -0,0 +1,6 @@ +// Copyright © SixtyFPS GmbH +// SPDX-License-Identifier: MIT + +fn main() { + slint_build::compile("ui/index.slint").unwrap(); +} diff --git a/examples/material-gallery/src/main.rs b/examples/material-gallery/src/main.rs new file mode 100644 index 00000000000..d5713103e15 --- /dev/null +++ b/examples/material-gallery/src/main.rs @@ -0,0 +1,14 @@ +#[cfg(target_arch = "wasm32")] +use wasm_bindgen::prelude::*; + +slint::include_modules!(); + +#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))] +pub fn main() { + // This provides better error messages in debug mode. + // It's disabled in release mode so it doesn't bloat up the file size. + #[cfg(all(debug_assertions, target_arch = "wasm32"))] + console_error_panic_hook::set_once(); + + MaterialGallery::new().unwrap().run().unwrap(); +} diff --git a/examples/material-gallery/ui/index.slint b/examples/material-gallery/ui/index.slint new file mode 100644 index 00000000000..a389652f328 --- /dev/null +++ b/examples/material-gallery/ui/index.slint @@ -0,0 +1,11 @@ +// Copyright © SixtyFPS GmbH +// SPDX-License-Identifier: MIT + +import { FilledButton } from "@slint/material.slint"; + +export component MaterialGallery inherits Window { + preferred-width: 600px; + preferred-height: 400px; + + FilledButton { } +} \ No newline at end of file diff --git a/internal/compiler/build.rs b/internal/compiler/build.rs index 844525ed191..1fb05af7c5c 100644 --- a/internal/compiler/build.rs +++ b/internal/compiler/build.rs @@ -42,6 +42,11 @@ fn widget_library() -> &'static [(&'static str, &'static BuiltinDirectory<'stati println!("cargo:rustc-env=SLINT_WIDGETS_LIBRARY={}", output_file_path.display()); + let mut slint_dir = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()); + slint_dir.push("components"); + + println!("cargo:rustc-env=SLINT_COMPONENTS_LIBRARY={}", slint_dir.display()); + Ok(()) } diff --git a/internal/compiler/components/headless.slint b/internal/compiler/components/headless.slint new file mode 100644 index 00000000000..811a56c206f --- /dev/null +++ b/internal/compiler/components/headless.slint @@ -0,0 +1,4 @@ +// Copyright © SixtyFPS GmbH +// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 + +export { ButtonBase } from "headless/button-base.slint"; \ No newline at end of file diff --git a/internal/compiler/components/headless/button-base.slint b/internal/compiler/components/headless/button-base.slint new file mode 100644 index 00000000000..c48c4215f70 --- /dev/null +++ b/internal/compiler/components/headless/button-base.slint @@ -0,0 +1,10 @@ +// Copyright © SixtyFPS GmbH +// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 + +export component ButtonBase { + callback clicked <=> touch-area.clicked; + + touch-area := TouchArea {} + + @children +} diff --git a/internal/compiler/components/material.slint b/internal/compiler/components/material.slint new file mode 100644 index 00000000000..5138d73e423 --- /dev/null +++ b/internal/compiler/components/material.slint @@ -0,0 +1,4 @@ +// Copyright © SixtyFPS GmbH +// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 + +export { FilledButton } from "material/filled-button.slint"; \ No newline at end of file diff --git a/internal/compiler/components/material/filled-button.slint b/internal/compiler/components/material/filled-button.slint new file mode 100644 index 00000000000..68b40f3191a --- /dev/null +++ b/internal/compiler/components/material/filled-button.slint @@ -0,0 +1,6 @@ +// Copyright © SixtyFPS GmbH +// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 + +import { ButtonBase } from "@slint/headless.slint"; + +export component FilledButton inherits ButtonBase { } \ No newline at end of file diff --git a/internal/compiler/lib.rs b/internal/compiler/lib.rs index 776846ef2eb..8d5cdffec32 100644 --- a/internal/compiler/lib.rs +++ b/internal/compiler/lib.rs @@ -38,7 +38,7 @@ pub mod typeregister; pub mod passes; use crate::generator::OutputFormat; -use std::path::Path; +use std::path::{Path, PathBuf}; /// Specify how the resources are embedded by the compiler #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -199,10 +199,13 @@ impl CompilerConfiguration { _ => None, }; + let mut library_paths = HashMap::new(); + library_paths.insert("slint".to_string(), PathBuf::from(env!("SLINT_COMPONENTS_LIBRARY"))); + Self { embed_resources, include_paths: Default::default(), - library_paths: Default::default(), + library_paths, style: Default::default(), open_import_fallback: None, resource_url_mapper: None, From dbe1194f522c6e8eb6cf606a906bff264a5d0c34 Mon Sep 17 00:00:00 2001 From: FloVanGH Date: Wed, 9 Oct 2024 09:09:50 +0000 Subject: [PATCH 2/6] Update examples/material-gallery/src/main.rs Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- examples/material-gallery/src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/material-gallery/src/main.rs b/examples/material-gallery/src/main.rs index d5713103e15..0c7e30c3fc4 100644 --- a/examples/material-gallery/src/main.rs +++ b/examples/material-gallery/src/main.rs @@ -1,4 +1,7 @@ #[cfg(target_arch = "wasm32")] +// Copyright © SixtyFPS GmbH +// SPDX-License-Identifier: MIT + use wasm_bindgen::prelude::*; slint::include_modules!(); From d01aa4561242376c4b9aeba8d4083584cec027ae Mon Sep 17 00:00:00 2001 From: FloVanGH Date: Wed, 9 Oct 2024 09:22:24 +0000 Subject: [PATCH 3/6] Update examples/material-gallery/src/main.rs Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- examples/material-gallery/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/material-gallery/src/main.rs b/examples/material-gallery/src/main.rs index 0c7e30c3fc4..985ea7155e1 100644 --- a/examples/material-gallery/src/main.rs +++ b/examples/material-gallery/src/main.rs @@ -1,7 +1,6 @@ #[cfg(target_arch = "wasm32")] // Copyright © SixtyFPS GmbH // SPDX-License-Identifier: MIT - use wasm_bindgen::prelude::*; slint::include_modules!(); From c653ba861b85651781b677d3b257e98c6302370e Mon Sep 17 00:00:00 2001 From: Florian Blasius Date: Wed, 9 Oct 2024 11:24:14 +0200 Subject: [PATCH 4/6] do not overwrite @slint imports --- internal/interpreter/api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/interpreter/api.rs b/internal/interpreter/api.rs index a0bd4a76e25..3bc77ef0135 100644 --- a/internal/interpreter/api.rs +++ b/internal/interpreter/api.rs @@ -694,7 +694,7 @@ impl Compiler { /// Sets the library paths used for looking up `@library` imports to the specified map of library names to paths. pub fn set_library_paths(&mut self, library_paths: HashMap) { - self.config.library_paths = library_paths; + self.config.library_paths.extend(library_paths); } /// Returns the library paths the component compiler is currently configured with. From 51350e1b89c2bf297ca2f4937a132a3343cc98b2 Mon Sep 17 00:00:00 2001 From: Florian Blasius Date: Wed, 9 Oct 2024 11:42:40 +0200 Subject: [PATCH 5/6] adjust node test --- api/node/__test__/compiler.spec.mts | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/api/node/__test__/compiler.spec.mts b/api/node/__test__/compiler.spec.mts index 62b68b86e45..549c8f08be8 100644 --- a/api/node/__test__/compiler.spec.mts +++ b/api/node/__test__/compiler.spec.mts @@ -27,10 +27,11 @@ test("get/set library paths", (t) => { libdir: "third_party/libbar/ui/", }; - t.deepEqual(compiler.libraryPaths, { - "libfile.slint": "third_party/libfoo/ui/lib.slint", - libdir: "third_party/libbar/ui/", - }); + t.deepEqual( + compiler.libraryPaths["libfile.slint"], + "third_party/libfoo/ui/lib.slint" + ); + t.deepEqual(compiler.libraryPaths.libdir, "third_party/libbar/ui/"); }); test("get/set style", (t) => { @@ -55,7 +56,7 @@ test("constructor error ComponentDefinition and ComponentInstance", (t) => { }); t.is( componentDefinitionError?.message, - "ComponentDefinition can only be created by using ComponentCompiler.", + "ComponentDefinition can only be created by using ComponentCompiler." ); const componentInstanceError = t.throws(() => { @@ -63,7 +64,7 @@ test("constructor error ComponentDefinition and ComponentInstance", (t) => { }); t.is( componentInstanceError?.message, - "ComponentInstance can only be created by using ComponentCompiler.", + "ComponentInstance can only be created by using ComponentCompiler." ); }); @@ -82,7 +83,7 @@ test("properties ComponentDefinition", (t) => { in-out property string-property; in-out property struct-property; }`, - "", + "" ); t.not(definition.App, null); @@ -132,7 +133,7 @@ test("callbacks ComponentDefinition", (t) => { callback first-callback(); callback second-callback(); }`, - "", + "" ); t.not(definition.App, null); @@ -164,7 +165,7 @@ test("globalProperties ComponentDefinition", (t) => { export component App { }`, - "", + "" ); t.not(definition.App, null); @@ -221,7 +222,7 @@ test("globalCallbacks ComponentDefinition", (t) => { } export component App { }`, - "", + "" ); t.not(definition.App, null); @@ -244,9 +245,9 @@ test("compiler diagnostics", (t) => { `export component App { garbage }`, - "testsource.slint", + "testsource.slint" ), - {}, + {} ); const diags = compiler.diagnostics; @@ -267,7 +268,7 @@ test("non-existent properties and callbacks", (t) => { export component App { }`, - "", + "" ); t.not(definition.App, null); @@ -286,6 +287,6 @@ test("non-existent properties and callbacks", (t) => { t.is(callback_err!.code, "GenericFailure"); t.is( callback_err!.message, - "Callback non-existent-callback not found in the component", + "Callback non-existent-callback not found in the component" ); }); From 64f7e5401483e3e59eedbbdabaad26f208562a48 Mon Sep 17 00:00:00 2001 From: Florian Blasius Date: Wed, 9 Oct 2024 11:46:07 +0200 Subject: [PATCH 6/6] fix formatting --- api/node/__test__/compiler.spec.mts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/api/node/__test__/compiler.spec.mts b/api/node/__test__/compiler.spec.mts index 549c8f08be8..857a4e5cec6 100644 --- a/api/node/__test__/compiler.spec.mts +++ b/api/node/__test__/compiler.spec.mts @@ -29,7 +29,7 @@ test("get/set library paths", (t) => { t.deepEqual( compiler.libraryPaths["libfile.slint"], - "third_party/libfoo/ui/lib.slint" + "third_party/libfoo/ui/lib.slint", ); t.deepEqual(compiler.libraryPaths.libdir, "third_party/libbar/ui/"); }); @@ -56,7 +56,7 @@ test("constructor error ComponentDefinition and ComponentInstance", (t) => { }); t.is( componentDefinitionError?.message, - "ComponentDefinition can only be created by using ComponentCompiler." + "ComponentDefinition can only be created by using ComponentCompiler.", ); const componentInstanceError = t.throws(() => { @@ -64,7 +64,7 @@ test("constructor error ComponentDefinition and ComponentInstance", (t) => { }); t.is( componentInstanceError?.message, - "ComponentInstance can only be created by using ComponentCompiler." + "ComponentInstance can only be created by using ComponentCompiler.", ); }); @@ -83,7 +83,7 @@ test("properties ComponentDefinition", (t) => { in-out property string-property; in-out property struct-property; }`, - "" + "", ); t.not(definition.App, null); @@ -133,7 +133,7 @@ test("callbacks ComponentDefinition", (t) => { callback first-callback(); callback second-callback(); }`, - "" + "", ); t.not(definition.App, null); @@ -165,7 +165,7 @@ test("globalProperties ComponentDefinition", (t) => { export component App { }`, - "" + "", ); t.not(definition.App, null); @@ -222,7 +222,7 @@ test("globalCallbacks ComponentDefinition", (t) => { } export component App { }`, - "" + "", ); t.not(definition.App, null); @@ -245,9 +245,9 @@ test("compiler diagnostics", (t) => { `export component App { garbage }`, - "testsource.slint" + "testsource.slint", ), - {} + {}, ); const diags = compiler.diagnostics; @@ -268,7 +268,7 @@ test("non-existent properties and callbacks", (t) => { export component App { }`, - "" + "", ); t.not(definition.App, null); @@ -287,6 +287,6 @@ test("non-existent properties and callbacks", (t) => { t.is(callback_err!.code, "GenericFailure"); t.is( callback_err!.message, - "Callback non-existent-callback not found in the component" + "Callback non-existent-callback not found in the component", ); });