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

时间:2018-11-07 19:43:29

标签: rust gtk rsvg

我正在使用Rust稳定版1.30和Relm。我想出了如何设置默认窗口大小,但至少在视图内部,我不知道如何设置图标!宏。

这是我的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中定义的图标设置为其图标? 请注意,该图标已使用rsvg crate实例化。

1 个答案:

答案 0 :(得分:1)

最后我管理了:

  • 从svg文件中创建句柄
  • 将对结果的引用传递到icon属性中。

哪个给我:

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");
}