yanom blog

様々な技術について書きます

Rustでicedを試してみた

この記事はアドベントカレンダー2021 Rust 18日目の記事です。

はじめに

皆さんこんにちは。寒いですね。
早いもので今年もあと少し、最後まで気を引き締めて行きましょう。

ここでは、RustのGUIライブラリであるicedを使ってGUIツールを作ったという、
紹介記事を書くつもりでしたが、諸般の都合で手がつけれず・・・。
icedのサンプルを動かすところまでになります。

環境

OS: Windows 10
rustc 1.56.0 (09c42c458 2021-10-18)
cargo 1.56.0 (4ed5d137b 2021-10-04)

iced

github.com

RustでGUIツールが簡単に作れるOSSです。
他の人が紹介している記事が沢山あるので、ここでは割愛します。

サンプルを動かしてみる

githubサンプルが沢山あるので、その中のeventsを動かしてみます。

cargo new コマンドで適当にプロジェクトを作った後、cargo.tomlに以下を追記します。

[dependencies]
iced = "0.3"
iced_native = "0.4"

その後、main.rsを以下とします。

use iced::{
    button, executor, Align, Application, Button, Checkbox,
    Clipboard, Column, Command, Container, Element, HorizontalAlignment, Length, Settings, Subscription, Text,
};
use iced_native::{window, Event};

pub fn main() -> iced::Result {
    Events::run(Settings {
        exit_on_close_request: false,
        ..Settings::default()
    })
}

#[derive(Debug, Default)]
struct Events {
    last: Vec<iced_native::Event>,
    enabled: bool,
    exit: button::State,
    should_exit: bool,
}

#[derive(Debug, Clone)]
enum Message {
    EventOccurred(iced_native::Event),
    Toggled(bool),
    Exit,
}

impl Application for Events {
    type Executor = executor::Default;
    type Message = Message;
    type Flags = ();

    fn new(_flags: ()) -> (Events, Command<Message>) {
        (Events::default(), Command::none())
    }

    fn title(&self) -> String {
        String::from("Events - Iced")
    }

    // fn update(&mut self, message: Message) -> Command<Message> {
    fn update(&mut self, message: Message, _clip: &mut Clipboard) -> Command<Message> {
        match message {
            Message::EventOccurred(event) if self.enabled => {
                self.last.push(event);

                if self.last.len() > 5 {
                    let _ = self.last.remove(0);
                }
            }
            Message::EventOccurred(event) => {
                if let Event::Window(window::Event::CloseRequested) = event {
                    self.should_exit = true;
                }
            }
            Message::Toggled(enabled) => {
                self.enabled = enabled;
            }
            Message::Exit => {
                self.should_exit = true;
            }
        };

        Command::none()
    }

    fn subscription(&self) -> Subscription<Message> {
        iced_native::subscription::events().map(Message::EventOccurred)
    }

    fn should_exit(&self) -> bool {
        self.should_exit
    }

    fn view(&mut self) -> Element<Message> {
        let events = self.last.iter().fold(
            Column::new().spacing(10),
            |column, event| {
                column.push(Text::new(format!("{:?}", event)).size(40))
            },
        );

        let toggle = Checkbox::new(
            self.enabled,
            "Listen to runtime events",
            Message::Toggled,
        );

        let exit = Button::new(
            &mut self.exit,
            Text::new("Exit")
                .width(Length::Fill)
                // .horizontal_alignment(alignment::Horizontal::Center),
                .horizontal_alignment(HorizontalAlignment::Center),
        )
        .width(Length::Units(100))
        .padding(10)
        .on_press(Message::Exit);

        let content = Column::new()
            // .align_items(Alignment::Center)
            .align_items(Align::Center)
            .spacing(20)
            .push(events)
            .push(toggle)
            .push(exit);

        Container::new(content)
            .width(Length::Fill)
            .height(Length::Fill)
            .center_x()
            .center_y()
            .into()
    }
}

コメントアウトした部分はサンプルが古くエラーとなっていましたので、書き換えています。

あとは、cargo runを実行すれば動くはずです。

f:id:yanom20:20211218190007p:plain
実行した様子

簡単ですね。
これを使って、冬休みにgrepアプリでも作って見ようと思います。