Skip to content

Commit fc94312

Browse files
add slint course
1 parent 38b707c commit fc94312

File tree

10 files changed

+1193
-0
lines changed

10 files changed

+1193
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Introduction to Slint
2+
3+
## What is Slint?
4+
Slint is a **declarative UI language** designed to make building modern, efficient, and portable user interfaces simple and intuitive.
5+
It allows you to describe *what* your UI should look like and *how* it should behave, without dealing with low-level implementation details.
6+
7+
Slint is designed to work seamlessly with languages like **Rust**, **C++**, and **JavaScript**, keeping your **business logic** separate from the UI code.
8+
9+
10+
## Core Concepts
11+
12+
### Elements and Properties
13+
Slint uses **elements** (such as `Text`, `Rectangle`, `Button`) followed by curly braces `{}` to define UI components.
14+
Inside the braces, you set **properties** that describe the appearance or behavior of that element.
15+
16+
Example:
17+
```slint
18+
Text {
19+
text: "Hello World!";
20+
font-size: 24px;
21+
color: #0044ff;
22+
}
23+
```
24+
25+
### Nesting Elements
26+
Elements can be placed inside one another to build hierarchical UIs.
27+
28+
Example:
29+
```slint
30+
Rectangle {
31+
width: 150px;
32+
height: 60px;
33+
background: white;
34+
border-radius: 10px;
35+
36+
Text {
37+
text: "Hello World!";
38+
font-size: 24px;
39+
color: black;
40+
}
41+
}
42+
```
43+
44+
### Reactivity
45+
Slint has **built-in reactivity**: when a property changes, all dependent UI elements automatically update.
46+
47+
Example with a counter:
48+
```slint
49+
property <int> counter: 0;
50+
51+
Rectangle {
52+
width: 150px;
53+
height: 60px;
54+
background: white;
55+
border-radius: 10px;
56+
57+
Text {
58+
text: "Count: " + counter;
59+
font-size: 24px;
60+
color: black;
61+
}
62+
63+
TouchArea {
64+
clicked => {
65+
counter += 1;
66+
}
67+
}
68+
}
69+
```
70+
71+
72+
## Why Slint?
73+
74+
- **Pure declarative language** built for UI from the ground up.
75+
- **Easy to read and write** for both developers and designers.
76+
- **Portable** works on desktop, embedded systems, and web.
77+
- **Clear separation** of UI and business logic.
78+
- **Reactivity** makes dynamic UIs effortless.
79+
80+
Compared to traditional UI approaches (like HTML/JavaScript or XML-based layouts), Slint is more concise, readable, and easier to maintain.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Slint Reactivity
2+
3+
## Introduction
4+
**Reactivity** is a core concept in Slint. It allows you to create complex, dynamic user interfaces with far less code.
5+
In Slint, UI elements automatically update when the properties they depend on change — without requiring manual refresh logic.
6+
7+
## Example: Mouse Tracking and Color Change
8+
```slint
9+
export component MyComponent {
10+
width: 400px; height: 400px;
11+
12+
Rectangle {
13+
background: #151515;
14+
}
15+
16+
ta := TouchArea {}
17+
18+
myRect := Rectangle {
19+
x: ta.mouse-x;
20+
y: ta.mouse-y;
21+
width: 40px;
22+
height: 40px;
23+
background: ta.pressed ? orange : white;
24+
}
25+
26+
Text {
27+
x: 5px; y: 5px;
28+
text: "x: " + myRect.x / 1px;
29+
color: white;
30+
}
31+
32+
Text {
33+
x: 5px; y: 15px;
34+
text: "y: " + myRect.y / 1px;
35+
color: white;
36+
}
37+
}
38+
```
39+
40+
### How It Works
41+
- The **rectangle follows the mouse** using `x: ta.mouse-x;` and `y: ta.mouse-y;`.
42+
- **Color changes on click**: `background: ta.pressed ? orange : white;`.
43+
- Text labels **automatically update** to show the rectangle's current position.
44+
45+
This works because:
46+
1. The `TouchArea` exposes `mouse-x`, `mouse-y`, and `pressed` properties.
47+
2. When these properties change, all bound expressions are automatically re-evaluated.
48+
3. The UI updates only where dependencies have changed.
49+
50+
51+
## Performance and Dependency Tracking
52+
Slint evaluates bindings lazily:
53+
- Dependencies are registered when a property is accessed during evaluation.
54+
- When a property changes, only dependent expressions are re-evaluated.
55+
- This ensures high performance, even for complex UIs.
56+
57+
58+
## Property Expressions
59+
Property bindings can be simple or complex:
60+
61+
```slint
62+
// Tracks foo.x
63+
x: foo.x;
64+
65+
// Conditional expression
66+
x: foo.x > 100px ? 0px : 400px;
67+
68+
// Clamped value
69+
x: clamp(foo.x, 0px, 400px);
70+
```
71+
72+
You can also use **functions** for clarity:
73+
74+
```slint
75+
export component MyComponent {
76+
width: 400px; height: 400px;
77+
78+
pure function lengthToInt(n: length) -> int {
79+
return (n / 1px);
80+
}
81+
82+
ta := TouchArea {}
83+
84+
myRect := Rectangle {
85+
x: ta.mouse-x;
86+
y: ta.mouse-y;
87+
width: 40px;
88+
height: 40px;
89+
background: ta.pressed ? orange : white;
90+
}
91+
92+
Text {
93+
x: 5px; y: 5px;
94+
text: "x: " + lengthToInt(myRect.x);
95+
}
96+
Text {
97+
x: 5px; y: 15px;
98+
text: "y: " + lengthToInt(myRect.y);
99+
}
100+
}
101+
```
102+
103+
## Purity in Bindings
104+
For reactivity to work correctly, bindings must be **pure**:
105+
- Evaluating a property should not change any other observable state.
106+
- The Slint compiler enforces purity in binding expressions, pure functions, and pure callbacks.
107+
108+
Example:
109+
```slint
110+
export component Example {
111+
pure callback foo() -> int;
112+
public pure function bar(x: int) -> int {
113+
return x + foo();
114+
}
115+
}
116+
```
117+
118+
## Two-Way Bindings
119+
Two-way bindings keep two properties in sync using `<=>`:
120+
121+
```slint
122+
export component Example {
123+
in property<brush> rect-color <=> r.background;
124+
in property rect-color2 <=> r.background;
125+
126+
r:= Rectangle {
127+
width: parent.width;
128+
height: parent.height;
129+
background: blue;
130+
}
131+
}
132+
```
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# The `.slint` File
2+
3+
## Introduction
4+
In Slint, you define user interfaces in the **Slint language** and save them in files with the `.slint` extension.
5+
6+
Each `.slint` file defines **one or more components**. Components declare a tree of elements and can be reused to build your own set of UI controls. You can use each declared component by its name in other components.
7+
8+
9+
## Example: Components and Elements
10+
```slint
11+
component MyButton inherits Text {
12+
color: black;
13+
// ...
14+
}
15+
16+
export component MyApp inherits Window {
17+
preferred-width: 200px;
18+
preferred-height: 100px;
19+
Rectangle {
20+
width: 200px;
21+
height: 100px;
22+
background: green;
23+
}
24+
MyButton {
25+
x: 0; y: 0;
26+
text: "hello";
27+
}
28+
MyButton {
29+
y: 0;
30+
x: 50px;
31+
text: "world";
32+
}
33+
}
34+
```
35+
36+
- `MyButton` and `MyApp` are components.
37+
- `Window` and `Rectangle` are **built-in elements**.
38+
- Components can **reuse** other components.
39+
40+
41+
## Naming Elements
42+
You can assign names to elements using `:=`:
43+
44+
```slint
45+
export component MyApp inherits Window {
46+
preferred-width: 200px;
47+
preferred-height: 100px;
48+
49+
hello := MyButton {
50+
x: 0; y: 0;
51+
text: "hello";
52+
}
53+
world := MyButton {
54+
y: 0;
55+
text: "world";
56+
x: 50px;
57+
}
58+
}
59+
```
60+
61+
### Reserved Names
62+
- `root` → outermost element of a component.
63+
- `self` → current element.
64+
- `parent` → parent element.
65+
66+
67+
## Comments
68+
- **Single-line**: `// comment`
69+
- **Multi-line**: `/* ... */`
70+
71+
```slint
72+
// Single line comment
73+
/*
74+
Multi-line comment
75+
*/
76+
```
77+
78+
---
79+
80+
## Elements and Components
81+
- **Elements** → basic building blocks (e.g., `Text`, `Rectangle`).
82+
- **Components** → can be built from multiple elements.
83+
- Declared as: `ElementName { ... }`
84+
85+
Valid:
86+
```slint
87+
Text {}
88+
Text {
89+
}
90+
```
91+
92+
Invalid:
93+
```slint
94+
Text {};
95+
```
96+
97+
98+
## The Root Element
99+
The root element in a `.slint` file must be a **component**.
100+
101+
```slint
102+
component MyApp {
103+
Text {
104+
text: "Hello World";
105+
font-size: 24px;
106+
}
107+
}
108+
```
109+
110+
## Properties
111+
Set with `property-name: value;`.
112+
113+
### Identifiers
114+
- Letters, numbers, `_`, or `-`.
115+
- Cannot start with number or `-`.
116+
- `_` and `-` are considered equivalent (`foo_bar` = `foo-bar`).
117+
118+
119+
## Conditional Elements
120+
Use `if` to create elements conditionally.
121+
122+
```slint
123+
export component Example inherits Window {
124+
preferred-width: 50px;
125+
preferred-height: 50px;
126+
if area.pressed : foo := Rectangle { background: blue; }
127+
if !area.pressed : Rectangle { background: red; }
128+
area := TouchArea {}
129+
}
130+
```
131+
132+
## Modules (Import/Export)
133+
By default, components are **private**. Use `export` to make them available in other files.
134+
135+
```slint
136+
component ButtonHelper inherits Rectangle { }
137+
component Button inherits Rectangle {
138+
ButtonHelper { }
139+
}
140+
export { Button }
141+
```
142+
143+
Rename on export:
144+
```slint
145+
component Button inherits Rectangle { }
146+
export { Button as ColorButton }
147+
```
148+
149+
Export directly:
150+
```slint
151+
export component Button inherits Rectangle { }
152+
```
153+
154+
Import from other files:
155+
```slint
156+
import { Button } from "./button.slint";
157+
```
158+
159+
Rename on import:
160+
```slint
161+
import { Button as CoolButton } from "../theme/button.slint";
162+
```
163+
164+
## Module Syntax
165+
### Import
166+
```slint
167+
import { MyButton } from "module.slint";
168+
import { MyButton, MySwitch } from "module.slint";
169+
import { MyButton as OtherButton } from "module.slint";
170+
```
171+
172+
### Export
173+
```slint
174+
export component MyButton inherits Rectangle { }
175+
component MySwitch inherits Rectangle { }
176+
export { MySwitch }
177+
export { MySwitch as Alias1, MyButton as Alias2 }
178+
export * from "other_module.slint";
179+
```

0 commit comments

Comments
 (0)