Skip to content

Commit 783f740

Browse files
authored
Build wasm version of Speedometer example (#9754)
* Build wasm version of Speedometer example * Tweak demo
1 parent 55a45a6 commit 783f740

File tree

9 files changed

+141
-16
lines changed

9 files changed

+141
-16
lines changed

.github/workflows/nightly_snapshot.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ jobs:
388388
rm -rf $output_path/demos
389389
mkdir -p $output_path/demos
390390
391-
for demo_subdir in examples,gallery, demos,printerdemo,rust examples,todo,rust examples,todo-mvc,rust examples,slide_puzzle, examples,memory, examples,imagefilter,rust examples,plotter, examples,opengl_underlay, examples,carousel,rust demos,energy-monitor, demos,weather-demo, demos,home-automation,rust demos,usecases,rust; do
391+
for demo_subdir in examples,gallery, demos,printerdemo,rust examples,todo,rust examples,todo-mvc,rust examples,slide_puzzle, examples,speedometer,rust examples,memory, examples,imagefilter,rust examples,plotter, examples,opengl_underlay, examples,carousel,rust demos,energy-monitor, demos,weather-demo, demos,home-automation,rust demos,usecases,rust; do
392392
IFS=',' read example_or_demo demo subdir <<< "${demo_subdir}"
393393
394394
mkdir -p $output_path/demos/$demo

.github/workflows/wasm_demos.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ jobs:
7676
examples/carousel/rust/
7777
examples/memory/
7878
examples/slide_puzzle/
79+
examples/speedometer/rust/
7980
examples/imagefilter/rust
8081
examples/plotter/
8182
examples/opengl_underlay/

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ members = [
2525
'demos/printerdemo/rust',
2626
'demos/printerdemo_mcu',
2727
'examples/slide_puzzle',
28+
'examples/speedometer/rust',
2829
'examples/todo/rust',
2930
'examples/todo-mvc/rust',
3031
'examples/virtual_keyboard/rust',
@@ -82,6 +83,7 @@ default-members = [
8283
'examples/memory',
8384
'demos/printerdemo/rust',
8485
'examples/slide_puzzle',
86+
'examples/speedometer/rust',
8587
'examples/todo/rust',
8688
'examples/virtual_keyboard/rust',
8789
'examples/carousel/rust',

examples/speedometer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
Demonstrate how to create a speedometer with Slint using conic gradients, scale and rotation.
77

8-
[Online Preview](https://slint.dev/snapshots/master/editor/preview.html?load_url=https://raw.githubusercontent.com/slint-ui/slint/master/examples/speedometer/demo.slint)
8+
| [Online simulation](https://slint.dev/snapshots/master/demos/speedometer/) |
99
[Online code editor](https://slint.dev/snapshots/master/editor/index.html?load_url=https://raw.githubusercontent.com/slint-ui/slint/master/examples/speedometer/demo.slint)

examples/speedometer/demo.slint

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,35 @@ component MiniDot {
1010
Rectangle {
1111
y: 160px;
1212
x: -(self.width / 2);
13-
width: 4px;
13+
width: 3px;
1414
height: 6px;
1515
background: #47505b;
1616
border-radius: self.width / 2;
1717
}
1818
}
1919

20+
component MediumDot {
21+
width: 0;
22+
height: 0;
23+
Rectangle {
24+
y: 158px;
25+
x: -(self.width / 2);
26+
width: 4px;
27+
height: 10px;
28+
background: #505b63;
29+
border-radius: self.width / 2;
30+
}
31+
}
32+
2033
component BigDot {
2134
width: 0;
2235
height: 0;
2336
Rectangle {
24-
y: 159px;
37+
y: 154px;
2538
x: -(self.width / 2);
2639
width: 5px;
27-
height: 10px;
28-
background: #a5adb3;
40+
height: 14px;
41+
background: #b2c7d8;
2942
border-radius: self.width / 2;
3043
}
3144
}
@@ -34,7 +47,7 @@ component KPHText {
3447
width: 0;
3548
height: 0;
3649
Text {
37-
y: 127px;
50+
y: 122px;
3851
width: 36px;
3952
color: white;
4053
text: (root.transform-rotation / 1deg).floor();
@@ -49,6 +62,11 @@ component KPHText {
4962
export component MainWindow inherits Window {
5063
property <color> background-color: #020414;
5164
property <int> speed: 0;
65+
property <int> max-speed: 260;
66+
property <int> total-mini-dots: max-speed / 2;
67+
property <int> total-medium-dots: total-mini-dots / 10;
68+
property <int> total-big-dots: total-mini-dots / 10;
69+
property <int> kph-indicators: total-mini-dots / 10;
5270

5371
width: 500px;
5472
height: 500px;
@@ -80,21 +98,15 @@ export component MainWindow inherits Window {
8098
height: 0px;
8199
transform-rotation: 50deg;
82100

83-
property <int> max-speed: 260;
84-
property <int> total-mini-dots: max-speed / 2;
85-
property <int> total-big-dots: total-mini-dots / 5;
86-
property <int> kph-indicators: total-big-dots / 2;
87-
88101
for i in total-mini-dots: MiniDot {
89102
transform-rotation: 260deg * (i / total-mini-dots);
90103
}
104+
for i in total-medium-dots: MediumDot {
105+
transform-rotation: 260deg * (i / total-medium-dots) + 10deg;
106+
}
91107
for i in total-big-dots + 1: BigDot {
92108
transform-rotation: 260deg * (i / total-big-dots);
93109
}
94-
95-
for i in kph-indicators + 1: KPHText {
96-
transform-rotation: 260deg * (i / kph-indicators);
97-
}
98110
}
99111

100112
holder := Rectangle {
@@ -139,6 +151,12 @@ export component MainWindow inherits Window {
139151
}
140152
}
141153
}
154+
Rectangle {
155+
transform-rotation: 50deg;
156+
for i in kph-indicators + 1: KPHText {
157+
transform-rotation: 260deg * (i / kph-indicators);
158+
}
159+
}
142160

143161
control-buttons := HorizontalBox {
144162
spacing: 10px;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright © SixtyFPS GmbH <[email protected]>
2+
# SPDX-License-Identifier: MIT
3+
4+
[package]
5+
name = "speedometer"
6+
version = "1.14.0"
7+
authors = ["Slint Developers <[email protected]>"]
8+
edition = "2021"
9+
publish = false
10+
license = "MIT"
11+
12+
[[bin]]
13+
path = "main.rs"
14+
name = "speedometer"
15+
16+
[lib]
17+
crate-type = ["lib", "cdylib"]
18+
path = "lib.rs"
19+
name = "speedometer_lib"
20+
21+
[dependencies]
22+
slint = { path = "../../../api/rs/slint" }
23+
chrono = "0.4"
24+
25+
[features]
26+
sw-renderer = []
27+
28+
[target.'cfg(target_arch = "wasm32")'.dependencies]
29+
wasm-bindgen = { version = "0.2" }
30+
web-sys = { version = "0.3", features = ["console"] }
31+
console_error_panic_hook = "0.1.5"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<!-- Copyright © SixtyFPS GmbH <[email protected]> -->
3+
<!-- SPDX-License-Identifier: MIT -->
4+
<html>
5+
<head>
6+
<style>
7+
#loading {
8+
margin-top: 20px;
9+
text-align: center;
10+
}
11+
.overlay {
12+
position: absolute;
13+
display: flex;
14+
top: 0;
15+
width: 100%;
16+
}
17+
canvas {
18+
top: 0;
19+
padding-left: 0;
20+
padding-right: 0;
21+
margin-left: auto;
22+
margin-right: auto;
23+
margin-top: 20px;
24+
display: block;
25+
}
26+
</style>
27+
</head>
28+
<body>
29+
<div id="loading">Loading...</div>
30+
<div class="overlay">
31+
<!-- canvas required by the Slint runtime -->
32+
<canvas id="canvas" data-slint-auto-resize-to-preferred="true" unselectable="on"></canvas>
33+
<script type="module">
34+
// import the generated file.
35+
import init from "./pkg/speedometer_lib.js";
36+
init();
37+
</script>
38+
</div>
39+
</body>
40+
</html>

examples/speedometer/rust/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright © SixtyFPS GmbH <[email protected]>
2+
// SPDX-License-Identifier: MIT
3+
4+
#[cfg(target_arch = "wasm32")]
5+
use wasm_bindgen::prelude::*;
6+
7+
#[cfg(feature = "sw-renderer")]
8+
slint::slint! {
9+
export { MainWindow } from "../demo.slint";
10+
}
11+
12+
#[cfg(not(feature = "sw-renderer"))]
13+
slint::slint! {
14+
export { MainWindow } from "../demo.slint";
15+
}
16+
17+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
18+
pub fn main() {
19+
// This provides better error messages in debug mode.
20+
// It's disabled in release mode so it doesn't bloat up the file size.
21+
#[cfg(all(debug_assertions, target_arch = "wasm32"))]
22+
console_error_panic_hook::set_once();
23+
24+
let app = MainWindow::new().expect("MainWindow::new() failed");
25+
26+
app.run().expect("MainWindow::run() failed");
27+
}

examples/speedometer/rust/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright © SixtyFPS GmbH <[email protected]>
2+
// SPDX-License-Identifier: MIT
3+
4+
fn main() {
5+
speedometer_lib::main();
6+
}

0 commit comments

Comments
 (0)