0. 環境の準備
今話題の Rust で GUI を作ります。
Slint は商用(業務用含む)かつソース非公開なら有料 (開発者サブスク)、それ以外なら無償のフレームワークです。
下記動作環境を参照して LXC + Xvfb を構築しておいてください。
(LXC じゃなくても動作します)
動作環境 (Rust や Xvfb はコンテナの中でインストール&動作させる)
コンテナ OS (LXC) AlmaLinux release 9.7 (Moss Jungle Cat) AlmaLinux10 は Xvfb の環境に対応していない IP アドレス 10.0.3.20 lxc-net のアドレス VNC ポート 5901 Xvfb :1 にするとポート番号は 5901 になる Rust rustc, cargo 1.88.0 本稿記述時の最新版 GUI Slint 1.14.1 x11vnc 0.9.17 Xvfb 1.20.11 親 OS AlmaLinux release 10.1 (Heliotrope Lion) 本稿記述時の最新版
重要なのは Rust と Slint が動作する環境なので、親 OS はこだわらないIP アドレス 192.168.11.20 - VNC ポート (転送) 2059 コンテナの 5901 ポートへ転送
1. Rust をインストール
1-1. Rust をインストールする。
# dnf install -y rust cargo1-2. Rust のプロジェクトを作成する。(CUI)
$ cargo new app11-3. 実行する。
$ cd app1
app1$ cat Cargo.toml
app1$ cat ./src/main.rs
[package] name = "app1" version = "0.1.0" edition = "2024" [dependencies]
fn main() { println!("Hello, world!"); }
app1$ cargo run
Hello, world!
2. Slint の組み込み
2-1. Rust のプロジェクトに Slint を組み込む。
app1$ cargo add slint2-2. Slint の画面とアクションを作成する。
app1$ cat Cargo.toml
Updating crates.io index Adding slint v1.14.1 to dependencies Features: + accessibility + backend-default + compat-1-2 + i-slint-backend-qt + renderer-femtovg + renderer-software + std 33 deactivated features Updating crates.io index Locking 537 packages to latest Rust 1.88.0 compatible versions Adding ab_glyph v0.2.32 Adding ab_glyph_rasterizer v0.1.10 …長いので省略… Adding zvariant_derive v5.8.0 Adding zvariant_utils v3.2.1
[package] name = "app1" version = "0.1.0" edition = "2024" [dependencies] slint = "1.14.1"
app1$ vim ./src/main.rs2-3. プロジェクトをビルドする。
原型がなくなるほど書き換える…
slint::slint! { // ボタンの定義を取り込む import { Button } from "std-widgets.slint"; // ウィンドウの宣言 export component MainWindow inherits Window { // コールバック定義は on_*() が自動生成される callback ev_exit(); // プロパティ定義は get_*() set_*() が自動生成される // Slint:int -> Rust:i32 in-out property<int> win_width: 320; in-out property<int> win_height: 200; width: win_width * 1px; height: win_height * 1px; VerticalLayout { alignment: center; // 全体を中央寄せ spacing: 10px; // 部品間を 10 ピクセルにする Text { text: "Hello,Slint"; horizontal-alignment: center; // 左右を中央寄せ } Button { text: "終了ボタン"; clicked => { root.ev_exit(); // 終了イベントを発生 } } } } }
fn main() { // 画面の中央座標を定義 let (center_x, center_y) = (640.0 / 2.0, 400.0 / 2.0); // ウィンドウサイズを定義 let (win_w, win_h) = (200, 100); // ウィンドウを用意 let ui = MainWindow::new().unwrap(); ui.set_win_width(win_w); ui.set_win_height(win_h); // ウィンドウの表示位置を指定 let (posx, posy) = ( center_x - (win_w as f32) / 2.0, center_y - (win_h as f32) / 2.0); let winpos = slint::LogicalPosition::new(posx, posy); ui.window().set_position(winpos); // 終了イベント処理を無名関数で実装 ui.on_ev_exit( || { std::process::exit(0); }); // イベントループの実行 ui.run().unwrap(); }
app1$ cargo build2-4. X サーバを準備する。
Compiling proc-macro2 v1.0.103 Compiling quote v1.0.42 …長いので省略… Compiling app1 v0.1.0 (/home/admin/app1) Finished `dev` profile [unoptimized + debuginfo] target(s) in 2m 46s
• 仮想 X サーバと VNC サーバを起動する。(ポート 5901)2-5. 実行する。
app1$ Xvfb :1 -screen 0 640x480x24 &
app1$ x11vnc -display :1.0 -rfbport 5901 -nopw &
• Openbox を画面「:1.0」に起動する。(ディスプレイ番号1、画面番号0)
app1$ DISPLAY=:1.0 openbox &
• xterm を画面「:1.0」、座標 (10,20) に起動する。(お好みで)
app1$ DISPLAY=:1.0 xterm -geometry 80x24+10+20 &
• マインスイーパーを画面「:1.0」、座標 (380,100) に起動する。(お好みで)
app1$ DISPLAY=:1.0 kmines -geometry 240x320+380+100 &
app1$ DISPLAY=:1.0 cargo run
親 OS のポート 2059 に接続する。Xvfb で動かしているせいで、カラースキーム (画面テーマ) にアクセスできずエラーが表示される。
Compiling app1 v0.1.0 (/home/admin/app1) Finished `dev` profile [unoptimized + debuginfo] target(s) in 12.53s Running `target/debug/app1` Error watching for xdg color schemes: org.freedesktop.DBus.Error.UnknownMethod: No such method “ReadOne”
ここでは TightVNC を使用する。
⇓
− □ × >_ Windows PowerShell × + | ∨
Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. 新機能と改善のために最新の PowerShell をインストールしてください!https://aka.ms/PSWindows PS C:\> # TightVNC Viewer を起動する PS C:\> & "C:\Program Files\TightVNC\tvnviewer.exe" 192.168.11.20:2059 ⏎ PS C:\> exit ⏎