如何在 Relm 的视图中设置图标!宏观?

How do I set up an icon inside Relm's view! macro?

我正在使用 Rust 稳定版 1.30 和 Relm。我想出了如何设置默认 window 大小,但不知道如何设置图标,至少在视图中是这样!宏.

这是我的main.rs

extern crate gtk;
extern crate chessground;
#[macro_use]
extern crate relm;
extern crate relm_attributes;
#[macro_use]
extern crate relm_derive;
extern crate shakmaty;
extern crate rsvg;

use gtk::prelude::*;
use gtk::GtkWindowExt;
use relm::Widget;
use relm_attributes::widget;

use shakmaty::{Square, Board};
use chessground::{Ground, UserMove, SetBoard};

use rsvg::{Handle};

use self::Msg::*;

#[derive(Msg)]
pub enum Msg {
    Quit,
    PieceMoved(Square, Square),
}

#[widget]
impl Widget for Win {
    fn model() -> Board {
        Board::default()
    }

    fn update(&mut self, event: Msg) {
        match event {
            Quit => gtk::main_quit(),
            PieceMoved(orig, dest) => {
                if let Some(piece) = self.model.remove_piece_at(orig)     {
                    self.model.set_piece_at(dest, piece, false);
                    self.ground.emit(SetBoard(self.model.clone()));
                }
            }
        }
    }

    view! {
        gtk::Window {
            title: "Chessground",
            property_default_width: 600,
            property_default_height: 600,
            #[name="ground"]
            Ground {
                UserMove(orig, dest, _) => PieceMoved(orig, dest),
            },
            delete_event(_, _) => (Quit, Inhibit(false)),
        }
    }
}

fn main() {
    let icon = Handle::new_from_str(include_str!("wQ.svg")).expect("Could not find icon !");
    // how to set up the given icon ?
    Win::run(()).expect("initialized gtk");
}

我的意思是,如何将main中定义的图标设置为window作为它的图标? 请注意,该图标已使用 rsvg crate 实例化。

我终于成功了:

  • 从 svg 文件制作句柄
  • 将对结果的引用传递到图标中 属性。

这给了我:

extern crate gtk;
extern crate chessground;
#[macro_use]
extern crate relm;
extern crate relm_attributes;
#[macro_use]
extern crate relm_derive;
extern crate shakmaty;
extern crate rsvg;

use gtk::prelude::*;
use gtk::GtkWindowExt;
use relm::Widget;
use relm_attributes::widget;

use shakmaty::{Square, Board};
use chessground::{Ground, UserMove, SetBoard};

use rsvg::{Handle};
use rsvg::HandleExt;  

use self::Msg::*;

#[derive(Msg)]
pub enum Msg {
    Quit,
    PieceMoved(Square, Square),
}

#[widget]
impl Widget for Win {
    fn model() -> Board {
        Board::default()
    }

    fn update(&mut self, event: Msg) {
        match event {
            Quit => gtk::main_quit(),
            PieceMoved(orig, dest) => {
                if let Some(piece) = self.model.remove_piece_at(orig) {
                    self.model.set_piece_at(dest, piece, false);
                    self.ground.emit(SetBoard(self.model.clone()));
                }
            }
        }
    }

    view! {
        gtk::Window {
            title: "Chessground",
            property_default_width: 600,
            property_default_height: 600,
            // HERE
            icon: &Handle::new_from_str(include_str!("wQ.svg"))
                .expect("Could not find icon !")
                .get_pixbuf()
                .expect("No pixbuf for the icon !"),
            #[name="ground"]
            Ground {
                UserMove(orig, dest, _) => PieceMoved(orig, dest),
            },
            delete_event(_, _) => (Quit, Inhibit(false)),
        }
    }
}

fn main() {
    Win::run(()).expect("initialized gtk");
}