slint/internal/interpreter/lib.rs
Tobias Hunger 9142336d5c interpreter: Add highlighting feature-gated functions
The preview can not leave it up to the interpreter to handle element selection
and highlighting. So add new functions to the interpreter (behind the
"highlight" feature-gate) to query positions of elements.

This exposes some of the code that is used by the existing highlighting code
and extends it where needed.

Two use-cases need to be covered:

1. Query the positions of a component (given by source file path and offset).
   This is then used to highlight all occurences of a component as the
   cursor position in a source file changes.

2. Query the position of an element (given as `ElementRc`).
   This is used when selecting elements in the UI. We need to work at
   the element level for this, not at the component level.

Also make the `highlight` module public but feature-gated, so that we
can put helper-types there.
2023-12-20 19:28:35 +01:00

94 lines
2.9 KiB
Rust

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
/*!
# Slint interpreter library
With this crate, you can load a .slint file at runtime and show its UI.
You only need to use this crate if you do not want to use pre-compiled .slint
code, which is the normal way to use Slint, using the `slint` crate
The entry point for this crate is the [`ComponentCompiler`] type, which you can
use to create [`ComponentDefinition`] with the [`ComponentCompiler::build_from_source`] or [`ComponentCompiler::build_from_path`]
functions.
### Note about `async` functions
Compiling a component is `async` but in practice, this is only asynchronous if [`ComponentCompiler::set_file_loader`]
is set and its future is actually asynchronous. If that is not used, then it is fine to use a very simple
executor, such as the one provided by the `spin_on` crate
## Examples
This example loads a `.slint` dynamically from a path and show errors if any:
```rust
use slint_interpreter::{ComponentDefinition, ComponentCompiler, ComponentHandle};
let mut compiler = ComponentCompiler::default();
let definition =
spin_on::spin_on(compiler.build_from_path("hello.slint"));
# #[cfg(feature="print_diagnostics")]
slint_interpreter::print_diagnostics(&compiler.diagnostics());
if let Some(definition) = definition {
let instance = definition.create().unwrap();
instance.run().unwrap();
}
```
This example load a `.slint` from a string and set some properties:
```rust
# i_slint_backend_testing::init();
use slint_interpreter::{ComponentDefinition, ComponentCompiler, Value, SharedString, ComponentHandle};
let code = r#"
export component MyWin inherits Window {
in property <string> my_name;
Text {
text: "Hello, " + my_name;
}
}
"#;
let mut compiler = ComponentCompiler::default();
let definition =
spin_on::spin_on(compiler.build_from_source(code.into(), Default::default()));
assert!(compiler.diagnostics().is_empty());
let instance = definition.unwrap().create().unwrap();
instance.set_property("my_name", Value::from(SharedString::from("World"))).unwrap();
# return; // we don't want to call run in the tests
instance.run().unwrap();
```
*/
//! ## Feature flags
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
#![warn(missing_docs)]
#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
#[cfg(not(feature = "compat-1-2"))]
compile_error!(
"The feature `compat-1-2` must be enabled to ensure \
forward compatibility with future version of this crate"
);
mod api;
mod dynamic_item_tree;
mod dynamic_type;
mod eval;
mod eval_layout;
mod global_component;
#[cfg(feature = "highlight")]
pub mod highlight;
mod value_model;
#[doc(inline)]
pub use api::*;
/// (Re-export from corelib.)
#[doc(inline)]
pub use i_slint_core::{Brush, Color, SharedString, SharedVector};
#[cfg(test)]
mod tests;