Kirigami med Rust
Installera Kirigami
Innan vi börjar måste vi installera Kirigami och Rust på datorn.
sudo pacman -S cargo cmake extra-cmake-modules kirigami breeze qqc2-desktop-style | |
sudo zypper install cargo cmake kf6-extra-cmake-modules kf6-kirigami-devel qt6-quickcontrols2-devel kf6-qqc2-desktop-style | |
sudo dnf install cargo cmake extra-cmake-modules kf6-kirigami2-devel kf6-qqc2-desktop-style |
Ytterligare information för andra distributioner finns här.
Projektstruktur
Först skapar vi vår projektkatalog (du kan använda kommandona nedan). Vi kallar vår kirigami_rust/
.
kirigami_rust/
├── CMakeLists.txt
├── Cargo.toml
├── build.rs
├── org.kde.kirigami_rust.desktop
└── src/
├── main.rs
└── qml/
└── Main.qml
Projektet använder CMake för att anropa Cargo, som i sin tur bygger projektet.
Om CMake och Cargo
This is not the traditional way of building a Rust project: technically, only Cargo is needed to build it, usually with cargo build
and cargo run
.
For desktop applications however, CMake (or an equivalent like Meson used by GNOME or Just used by COSMIC) is needed to install because Cargo lacks the necessary features to install GUI desktop applications.
The project will be called kirigami_rust
and it will generate an executable called kirigami_hello
.
💡 Tips
För att snabbt skapa katalogstrukturen, kör bara:mkdir -p kirigami_rust/src/qml/
.org.kde.kirigami_rust.desktop
Det primära syftet med Desktop Entry-filer är att visa programmet i programstart på Linux. En annan anledning att ha dem är att ha fönsterikoner på Wayland, eftersom de måste tala om för sammansättningen att "det här fönstret hör ihop med den här ikonen".
Det måste följa ett omvänt domännamnsschema följt av tillägget .desktop
såsom org.kde.kirigami_rust.desktop
:
|
|
CMakeLists.txt
The CMakeLists.txt
file is going to be used to run Cargo and to install the necessary files together with our application. It also provides certain quality of life features, such as making sure that Kirigami is installed during compilation and to signal Linux distributions to install the necessary dependencies with the application.
|
|
The first thing we do is add KDE's Extra CMake Modules (ECM) to our project so we can use ecm_find_qml_module to check that Kirigami is installed when trying to build the application, and if it's not, fail immediately. Another useful ECM feature is ECMUninstallTarget, which allows to easily uninstall the application with CMake if desired.
We also use CMake's find_package() to make sure we have qqc2-desktop-style, KDE's QML style for the desktop. This is one of the two reasons we use CMake in this tutorial.
Typically Rust projects are built with Cargo, and it won't be different here. We create a target that will simply run Cargo when run, and mark it with ALL
so it builds by default. Cargo will build the executable inside CMake's binary directory (typically build/
).
For more information about CMake, targets, and the binary directory, see Building KDE software manually.
After this, we simply install the kirigami_rust
executable generated by Cargo in the binary directory and install it to the BINDIR
, which is usually /usr/bin
, /usr/local/bin
or ~/.local/bin
. We also install the necessary desktop file to APPDIR
, which is usually /usr/share/applications
or ~/.local/share/applications
. This is the second reason we use CMake in this tutorial.
For more information about where KDE software is installed, see Building KDE software manually: The install step.
Nu när vi har tagit hand om CMake, låt oss titta på filerna vi ska spendera den största delen av tiden att arbeta med.
Cargo.toml
Därefter har vi en väldigt enkel Cargo.toml
:
|
|
It consists of project metadata and a list of dependencies that will be pulled automatically by Cargo, namely cxx
and cxx-qt
, which are necessary to run Qt applications written in Rust.
build.rs
Where in C++ you'd typically register QML elements with QML_ELEMENT and ecm_add_qml_module using declarative registration, with Rust you'll need to declare it in a build script build.rs file:
|
|
This is necessary to make the QML file available in the entrypoint for our application, main.rs
.
src/main.rs
The file kirigami_rust/src/main.rs
initializes the project and then loads the QML file, which will consist of the user interface for the application.
|
|
The first part that is marked with the #[cxx_qt::bridge] Rust macro just creates a dummy QObject out of a dummy Rust struct. This is needed just to complete the use of QmlModule in the previous build script build.rs
. This will play a larger part in a future tutorial teaching how to expose Rust code to QML, but for now you can ignore it.
Efter det börjar den viktiga delen:
Raderna 12-13 importerar de nödvändiga Qt-biblioteken som exponeras via cxx-qt.
Vi skapar först en ny instans av en QApplication
och utför sedan några integreringar på raderna 20-26.
Sedan kommer den del som faktiskt skapar programfönstret:
|
|
Den långa webbadressen qrc:/qt/qml/org/kde/tutorial/src/qml/Main.qml
motsvarar filen Main.qml
enligt Qt:s resurssystem, och följer det här schemat: <resurs_prefix><import_webbadress><QML_katalog><fil>
.
In other words: the default resource prefix qrc:/qt/qml/
+ the import URI org/kde/tutorial
(set in build.rs
, separated by slashes instead of dots) + the QML dir src/qml/
+ the QML file Main.qml
.
src/qml/Main.qml
|
|
Här är stället där vi hanterar vårt förgrundsprogram.
Om du kan lite Javascript ser mycket av QML bekant ut (även om det har sina egenheter). Qt:s dokumentation har en omfattande mängd material om språket om du känner för att prova något på egen hand. Genom alla de här handledningarna kommer vi att fokusera mycket av vår uppmärksamhet på vår QML-kod, där vi kan använda Kirigami för att få ut så mycket som möjligt av den.
Låt oss för tillfället fokusera på Main.qml
. Först importerar vi ett antal viktiga moduler:
- QtQuick, standardbiblioteket som används i QML-program.
- QtQuick Controls, som tillhandahåller ett antal standardkontroller som vi kan använda för att göra vårt program interaktivt.
- QtQuick Layouts, som tillhandahåller verktyg för att placera komponenter inom programmets fönster.
- Kirigami, som tillhandahåller ett antal komponenter lämpade för att skapa program som fungerar över apparater av olika form och storlek.
Anmärkning
Att placera QtQuick-kontroller och Kirigami-import i separata namnrymder med nyckelordetas
är bästa praxis för att säkerställa att inga komponenter med samma namn kan orsaka konflikt. Du kan se olika namn för QtQuick Controls i verkligheten, såsom "QQC" eller "QQC2". Vi använder "Controls" i handledningen för tydlighetens skull.Sedan kommer vi till vårt baselement, Kirigami.ApplicationWindow som tillhandahåller några grundfunktioner nödvändiga för alla Kirigami-program. Det är fönstret som kommer att innehålla alla våra sidor, huvuddelen av vårt användargränssnitt.
Vi ställer sedan in fönstrets egenskap id
till 'root'. Identifierare är användbara eftersom de låter oss referera till en komponent unikt, även om vi har flera av samma typ.
Vi ställer också in fönstrets egenskap title
till "Hello World".
Därefter anger vi första sidan i vår sidstapel. Del flesta Kirigami-program är organiserade som en stapel av sidor, där varje sida innehåller relaterade komponenter anpassade för en viss uppgift. För närvarande låter vi det vara enkelt, och håller oss till en enda sida. pageStack är en sidstapel som initialt är tom som Kirigami.ApplicationWindow tillhandahåller, och med pageStack.initialPage: Kirigami.Page {...}
ställer vi in den första sidan som presenteras när programmet laddas i en Kirigami.Page. Sidan står för allt vårt innehåll.
Slutligen inkluderar vi en Controls.Label på vår sida, som låter oss placera text på den. Vi använder anchors.centerIn: parent
för att centrera vår beteckning horisontellt och vertikalt inom vårt överliggande element. I det här fallet är det överliggande elementet för Controls.Label
vår Kirigami.Page. Det sista vi behöver göra är att ange texten: text: "Hello World!"
.
Kompilera och installera programmet
You should find the generated executable kirigami_hello
under build/debug/kirigami_hello
and you may run it directly or with cargo run
, but it will lack a Window icon. To address this, we'll install the application first.
Kör följande:
cmake -B build --install-prefix ~/.local
cmake --build build/
cmake --install build/
With the first command, CMake will search for Kirigami and qqc2-desktop-style.
With the second command, CMake will build the kirigami_rust
target, which just calls cargo build --target-dir build/
. This step will take a while to complete, but the next time you repeat the second CMake command it will be faster or you will not need to compile at all.
In the third step, CMake will install the executable kirigami_hello
under ~/.local/bin/kirigami_hello
and the desktop file under ~/.local/share/applications
, and a new entry named "Kirigami Tutorial in Rust" will appear on your menu.
Öppna menyalternativet och voilà! Nu dyker ditt allra första Kirigami-program upp framför dina ögon.
För att köra den nya QML-applikationen i mobilläge kan man använda QT_QUICK_CONTROLS_MOBILE=1
:
QT_QUICK_CONTROLS_MOBILE=1 kirigami_hello
Om du har kompilerat projektet manuellt med CMake och av någon anledning vill avinstallera projektet kan du köra:
cmake --build build/ --target uninstall