0
0

More than 1 year has passed since last update.

gtk4-rsのexampleを試す[Ubuntu 23.04 on Docker]

Posted at

この記事は

gtk-rs/gtk4-rsのexamplesを1個ずつ試した際
いくつかの例題がエラーで動かなかった

この記事は、gtk-rsリポジトリの例題のうち、dialog例を動かすようにしたまでのメモ

【課題】 cargo run --example dialogが動かない

gtk::AlertDialogが見つかりません」というエラーが出る

cargo run --example dialog
   Compiling hello-gtk v0.1.0 (/app/hello-gtk)
error[E0433]: failed to resolve: could not find `AlertDialog` in `gtk`
  --> examples/dialog/main.rs:48:32
   |
48 |     let question_dialog = gtk::AlertDialog::builder()
   |                                ^^^^^^^^^^^ could not find `AlertDialog` in `gtk`
   |

AlertDialogが使えるのはv4.10から

Struct gtk4::AlertDialogにそう書かれている

Available on crate feature v4_10 only.

寄り道~2023.08.07時点のGTK4バージョン

GTK4.12が最新

image.png

LinuxマシンとGTK4バージョンはどうなっている?

エラーが出ているマシンの状態、および使用しているライブラリを調査する

Linuxのバージョン

Ubuntu 22.04 です

cat /etc/issue
Ubuntu 22.04.3 LTS \n \l

GTK4のバージョン

そのマシンで動いているGTK4のバージョンは4.6.9です

GUI development with Rust and GTK 4 - Project Setup

pkg-config --modversion gtk4
4.6.9

Dockerfileのapt install libgtk-4-devでインストールしたバージョンは4.6.9

apt list --installed | grep libgtk-4-dev
libgtk-4-dev/jammy-updates,now 4.6.9+ds-0ubuntu0.22.04.1 amd64 [installed]

ほかにインストールできるバージョンはあるか?

4.6.9しかない! 4.10がえらべない!

apt list libgtk-4-dev -a
Listing... Done
libgtk-4-dev/jammy-updates,now 4.6.9+ds-0ubuntu0.22.04.1 amd64 [installed]
libgtk-4-dev/jammy 4.6.2+ds-1ubuntu2 amd64

Ubuntu packages

本家大本を見に行く
libgtk-4-dev パッケージによると、以下の対応とのこと

  • ubuntu 22.04: 4.6.9
  • ubuntu 23.04: 4.10.1

image.png

母艦のubuntuのバージョンを上げる必要がありそう(22.04はもう古いのか・・・)

Dockerfileのベースイメージを変更する

Dockerfileの全容は付録に記載する

FROM ubuntu:23.04

Dockerfileを更新したら、docker compose up -d --buildでビルドしなおす
ビルドがおわったら、docker compose exec mylinux bashでコンテナに入る

GTK4バージョン確認

apt list --installed | grep libgtk-4-dev
libgtk-4-dev/lunar-updates,now 4.10.4+ds-0ubuntu1 amd64 [installed]

4.10.4までバージョンが上がった!

【課題再掲】 cargo run --example dialogを試す

依存関係に書いたGTKのfeatures = ["v4_6"]features = ["v4_10"]に修正する

Cargo.toml
[dependencies]
gtk = { version = "0.7.1", package = "gtk4", features = ["v4_10"] }

動いた!

Animation2.gif

おわりに

糸冬了!!

付録

Cargo.toml

  • pnetなど、不要なクレートも一部混じっている
  • GTK4.10に上がったことで、不要になったクレートもあるかも
[dependencies]
gtk = { version = "0.7.1", package = "gtk4", features = ["v4_10"] }
default-net = "0.15"
pnet = "0.33.0"
log = "0.4.19"
env_logger = "0.10.0"
once_cell = "1.18.0"
glib = "0.18.1"
chrono = "0.4.26"
femtovg = "0.7.1"
glow = "0.12.3"
libloading = "0.8.0"
epoxy = "0.1.0"
image = "0.24.6"
glium = "0.32.1"

Dockerfile

FROM ubuntu:23.04

RUN apt update
RUN apt install -y \
    vim \
    curl \
    libgtk-4-dev \
    build-essential

# Rust install
ENV RUST_HOME /usr/local/lib/rust
ENV RUSTUP_HOME ${RUST_HOME}/rustup
ENV CARGO_HOME ${RUST_HOME}/cargo

RUN mkdir /usr/local/lib/rust && \
    chmod 0755 $RUST_HOME
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > ${RUST_HOME}/rustup.sh \
    && chmod +x ${RUST_HOME}/rustup.sh \
    && ${RUST_HOME}/rustup.sh -y --default-toolchain nightly --no-modify-path
ENV PATH $PATH:$CARGO_HOME/bin

# Rust build
ENV CARGO_BUILD_TARGET_DIR=/tmp/target

RUN USER=root cargo new --bin app
WORKDIR /app
COPY ./hello-gtk/Cargo.toml Cargo.toml
COPY ./hello-gtk/Cargo.lock Cargo.lock
RUN cargo build && rm src/*.rs

example実行用コード

cargo run --example basics
cargo run --example builder_pattern
cargo run --example clipboard
cargo run --example clock
cargo run --example column_view_datagrid
cargo run --example composite_template
cargo run --example confetti_snapshot_animation
cargo run --example content_provider
cargo run --example css
cargo run --example custom_application
cargo run --example custom_buildable
cargo run --example custom_editable
cargo run --example custom_layout_manager
cargo run --example custom_orientable
cargo run --example custom_paintable
cargo run --example custom_widget
cargo run --example dialog
cargo run --example entry_completion
cargo run --example entry_undo
cargo run --example expressions
cargo run --example femtovg_area
cargo run --example flow_box
cargo run --example gif_paintable
cargo run --example glium_gl_area
cargo run --example grid_packing
cargo run --example gtk_builder
cargo run --example list_box_model
cargo run --example list_view_apps_launcher
cargo run --example rotation_bin
cargo run --example scale_bin
cargo run --example search_bar
cargo run --example squares
cargo run --example squeezer_bin
cargo run --example text_viewer
cargo run --example video_player
cargo run --example virtual_methods
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0