Change Rust Quickstart to use the Rust template

This reduces duplicated documentation and fixes two bugs. One minor additional change is that Rust code from the game logic chapter can now safely be copied, because the tiles variable is cloned in the previous step.

Fixes #5825
Fixes #5824
This commit is contained in:
Simon Hausmann 2024-08-13 14:39:35 +02:00 committed by Simon Hausmann
parent 636de6fc7c
commit 227bd78150
5 changed files with 23 additions and 107 deletions

View file

@ -19,61 +19,15 @@ The <span class="hljs-keyword">for</span> loop is declarative and automatically
the model changes. The loop instantiates all the <span class="hljs-title">MemoryTile</span> elements and places them on a grid based on their
index with spacing between the tiles.
::::{tab-set}
:::{tab-item} C++
:sync: cpp
First, add the tile data structure definition at the top of the `ui/appwindow.slint` file:
:::
:::{tab-item} NodeJS
:sync: nodejs
First, add the tile data structure definition at the top of the `ui/appwindow.slint` file:
:::
:::{tab-item} Rust
:sync: rust
:selected: true
First, add the tile data structure definition at the top of the `slint!` macro:
:::
::::
:::{literalinclude} main_multiple_tiles.rs
:language: slint,no-preview
:lines: 11-15
:::
::::{tab-set}
:::{tab-item} C++
:sync: cpp
Next, replace the _export component <span class="hljs-title">MainWindow</span> inherits Window { ... }_ section at the bottom of the `ui/appwindow.slint` file with the following:
:::
:::{tab-item} NodeJS
:sync: nodejs
Next, replace the _export component <span class="hljs-title">MainWindow</span> inherits Window { ... }_ section at the bottom of the `ui/appwindow.slint` file with the following:
:::
:::{tab-item} Rust
:sync: rust
:selected: true
Next, replace the _export component <span class="hljs-title">MainWindow</span> inherits Window { ... }_ section at the bottom of the `slint!` macro with the following:
:::
::::
:::{literalinclude} main_multiple_tiles.rs
:language: slint,no-preview
:lines: 63-90

View file

@ -118,7 +118,7 @@ a player can't click anything during this time.
Add this code before the `main_window.run().unwrap();` call:
:::{literalinclude} main_game_logic_in_rust.rs
:lines: 21-52
:lines: 25-52
:::
The code uses a [Weak](https://slint.dev/docs/rust/slint/struct.Weak) pointer of the `main_window`. This is

View file

@ -28,7 +28,7 @@ cd memory
The `CMakeLists.txt` uses the line `add_executable(my_application src/main.cpp)` to set `src/main.cpp` as the main C++ code file.
Change the content of `src/main.cpp` to the following:
Replace the content of `src/main.cpp` with the following:
:::{literalinclude} main_initial.cpp
:lines: 9-13
@ -38,7 +38,7 @@ Also in `CMakeLists.txt` the line
`slint_target_sources(my_application ui/appwindow.slint)` is a Slint function used to
add the `appwindow.slint` file to the target.
Change the contents of `ui/appwindow.slint` to the following:
Replace the contents of `ui/appwindow.slint` with the following:
:::{literalinclude} appwindow.slint
:language: slint,no-preview
@ -90,7 +90,7 @@ my_application
::::{tab-item} NodeJS
:sync: nodejs
Clone the template with the following command:
Clone or download the template repository:
```sh
git clone https://github.com/slint-ui/slint-nodejs-template memory
@ -134,20 +134,33 @@ Run the example with `npm start` and a window appears with the green "Hello Worl
We recommend using [rust-analyzer](https://rust-analyzer.github.io) and [our editor integrations for Slint](https://github.com/slint-ui/slint/tree/master/editors) for following this tutorial.
Let's create a new Rust application and add `slint` as a dependency
Install the [template](https://github.com/slint-ui/slint-rust-template) with the following commands:
```sh
cargo new memory
cargo install cargo-generate
cargo generate --git https://github.com/slint-ui/slint-rust-template --name memory
cd memory
cargo add slint
```
### Configure the project
Replace the contents of `src/main.rs` with the following:
:::{literalinclude} main_initial.rs
:lines: 6-17
```rust
slint::include_modules!();
fn main() -> Result<(), slint::PlatformError> {
let main_window = MainWindow::new()?;
main_window.run()
}
```
Replace the contents of `ui/appwindow.slint` with the following:
:::{literalinclude} memory.slint
:language: slint,no-preview
:lines: 6-11
:::
### Run the application

View file

@ -20,7 +20,7 @@ fn main() {
// Assign the shuffled Vec to the model property
let tiles_model = std::rc::Rc::new(slint::VecModel::from(tiles));
main_window.set_memory_tiles(tiles_model.into());
main_window.set_memory_tiles(tiles_model.clone().into());
main_window.run().unwrap();
}

View file

@ -12,32 +12,8 @@ Lengths in Slint have a unit, here, the `px` suffix.
This makes the code easier to read and the compiler can detect when you accidentally
mix values with different units attached to them.
::::{tab-set}
:::{tab-item} C++
:sync: cpp
Copy the following code into `ui/appwindow.slint` file, replacing the current content:
:::
:::{tab-item} NodeJS
:sync: nodejs
Copy the following code into `ui/appwindow.slint` file, replacing the current content:
:::
:::{tab-item} Rust
:sync: rust
:selected: true
Copy the following code inside of the `slint!` macro, replacing the current content:
:::
::::
:::{literalinclude} memory_tile.slint
:language: slint,no-preview
:lines: 5-19
@ -45,36 +21,9 @@ Copy the following code inside of the `slint!` macro, replacing the current cont
This exports the <span class="hljs-title">MainWindow</span> component so that the game logic code can access it later.
::::{tab-set}
:::{tab-item} C++
:sync: cpp
Inside the <span class="hljs-built_in">Rectangle</span> place an <span class="hljs-built_in">Image</span> element that
loads an icon with the <span class="hljs-built_in">@image-url()</span> macro. The path is relative to the location of `ui/appwindow.slint`.
:::
:::{tab-item} NodeJS
:sync: nodejs
Inside the <span class="hljs-built_in">Rectangle</span> place an <span class="hljs-built_in">Image</span> element that
loads an icon with the <span class="hljs-built_in">@image-url()</span> macro. The path is relative to the location of `ui/appwindow.slint`.
:::
:::{tab-item} Rust
:sync: rust
:selected: true
Inside the <span class="hljs-built_in">Rectangle</span> place an <span class="hljs-built_in">Image</span> element that
loads an icon with the <span class="hljs-built_in">@image-url()</span> macro.
When using the `slint!` macro, the path is relative to the folder that contains the `Cargo.toml` file.
When using Slint files, it's relative to the folder of the Slint file containing it.
:::
::::
You need to install this icon and others you use later first. You can download a pre-prepared
[Zip archive](https://slint.dev/blog/memory-game-tutorial/icons.zip) to the `ui` folder,