Skip to main content

vivi_ui/
lib.rs

1// SPDX-FileCopyrightText: 2024 vivi developers <vivi-ui@tuta.io>
2// SPDX-License-Identifier: MIT
3
4/*!
5 * # vivi
6 *
7 * `vivi` is a custom component library for [Slint](https://slint.dev/).
8 *
9 * ## How to use
10 *
11 * First of all you have to add `slint` as dependency to the `Cargo.toml` file of your project and `vivi_ui` and `slint-build` as build
12 * dependency.
13 *
14 * ### Cargo.toml
15 *
16 * ```toml
17 * [package]
18 * ...
19 * build = "build.rs"
20 * edition = "2021"
21 *
22 * [dependencies]
23 * slint = { version = "1.7" }
24 * ...
25 *
26 * [build-dependencies]
27 * slint-build = { version = "1.7" }
28 * vivi_ui = "0.2.0"
29 * ```
30 * As next use the API of the slint-build crate in the `build.rs` file and vivi to include the import paths.
31 *
32 * ### build.rs
33 *
34 * ```ignore
35 * fn main() {
36 *   slint_build::compile_with_config(
37 *          "ui/index.slint",
38 *          slint_build::CompilerConfiguration::new().with_library_paths(vivi_ui::import_paths()),
39 *      )
40 *      .unwrap();
41 * }
42 * ```
43 *
44 * `vivi` can now be used inside of your Slint files:
45 *
46 * ### index.slint
47 *
48 * ```slint
49 * import { MagicVerticalBox, FilledButton } from "@vivi/magic.slint";
50 *
51 * export component HelloWorld inherits Window {
52 *     MagicVerticalBox {
53 *         FilledButton {
54 *             text: "Click Me";
55 *             clicked => { self.text = "Hello World"; }
56 *         }
57 *    }
58 * }
59 * ````
60 *
61 * Finally, use the [`include_modules!`] macro in your `main.rs`:
62 *
63 * ### main.rs
64 *
65 * ```ignore
66 * slint::include_modules!();
67 * fn main() {
68 *     HelloWorld::new().unwrap().run().unwrap();
69 * }
70 * ```
71 *
72 */
73
74use std::env;
75use std::path::PathBuf;
76
77use std::collections::HashMap;
78
79const LIB_NAME: &str = "vivi";
80
81/// Provides the `vivi` library paths used by slint-build to make them accessible through `@vivi` in Slint.
82///
83/// ```ignore
84/// fn main() {
85///   slint_build::compile_with_config(
86///          "ui/index.slint",
87///          slint_build::CompilerConfiguration::new().with_library_paths(vivi::import_paths()),
88///      )
89///      .unwrap();
90/// }
91/// ```
92///
93/// Import `vivi` library in `ui/main.slint`
94///
95/// ```slint
96/// import { FilledButton } from "@vivi/magic.slint";
97/// ````
98pub fn import_paths() -> HashMap<String, PathBuf> {
99    let mut import_paths = HashMap::new();
100
101    let ui_lib_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui");
102
103    import_paths.insert(LIB_NAME.to_string(), ui_lib_path);
104
105    import_paths
106}