2

I am using Rust stable 1.30 and Relm. I figured out how to set up the default window size, but don't know how to set up the icon, at least inside the view! macro.

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

I mean, how to set up the icon defined in main to the window as its icon ? Notice that the icon has been instantiated with rsvg crate.

4

1 回答 1

2

最后我成功了:

  • 从 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");
}
于 2018-11-07T20:58:14.340 回答