gpt4 book ai didi

rust - 如何在 Relm 的 View 中设置图标!宏观?

转载 作者:行者123 更新时间:2023-11-29 08:18:19 25 4
gpt4 key购买 nike

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

这是我的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 实例化。

最佳答案

我终于成功了:

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

关于rust - 如何在 Relm 的 View 中设置图标!宏观?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53196677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com