在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):antoyo/relm开源软件地址(OpenSource Url):https://github.com/antoyo/relm开源编程语言(OpenSource Language):Rust 100.0%开源软件介绍(OpenSource Introduction):RelmRequirementsSince relm is based on GTK+, you need this library on your system in order to use it. See this page for information on how to install GTK+. UsageFirst, add this to your gtk = "^0.15.0"
relm = "^0.23.0"
relm-derive = "^0.23.0" Next, add this to your crate: use relm::{connect, Relm, Update, Widget};
use gtk::prelude::*;
use gtk::{Window, Inhibit, WindowType};
use relm_derive::Msg; Then, create your model: struct Model {
// …
} The model contains the data related to a Create your message #[derive(Msg)]
enum Msg {
// …
Quit,
} Messages are sent to Create a struct Win {
// …
model: Model,
window: Window,
} To make this impl Update for Win {
// Specify the model used for this widget.
type Model = Model;
// Specify the model parameter used to init the model.
type ModelParam = ();
// Specify the type of the messages sent to the update function.
type Msg = Msg;
// Return the initial model.
fn model(_: &Relm<Self>, _: ()) -> Model {
Model {
}
}
// The model may be updated when a message is received.
// Widgets may also be updated in this function.
fn update(&mut self, event: Msg) {
match event {
Msg::Quit => gtk::main_quit(),
}
}
}
impl Widget for Win {
// Specify the type of the root widget.
type Root = Window;
// Return the root widget.
fn root(&self) -> Self::Root {
self.window.clone()
}
// Create the widgets.
fn view(relm: &Relm<Self>, model: Self::Model) -> Self {
// GTK+ widgets are used normally within a `Widget`.
let window = Window::new(WindowType::Toplevel);
// Connect the signal `delete_event` to send the `Quit` message.
connect!(relm, window, connect_delete_event(_, _), return (Some(Msg::Quit), Inhibit(false)));
// There is also a `connect!()` macro for GTK+ events that do not need a
// value to be returned in the callback.
window.show_all();
Win {
model,
window,
}
}
} Finally, show this fn main() {
Win::run(()).unwrap();
}
|
Note
|
The struct Win is now automatically created by the attribute, as are the function root() and the associated types Model , ModelParam , Msg and Container .
You can still provide the method and the associated types if needed, but you cannot create the struct .
|
Warning
|
The #[widget] makes the generated struct public: hence, the corresponding model and message types must be public too.
|
Warning
|
Your program might be slower when using this attribute because the code generation is simple. For instance, the following code fn update(&mut self, event: Msg) {
for _ in 0..100 {
self.model.counter += 1;
}
} will generate this function: fn update(&mut self, event: Msg) {
for _ in 0..100 {
self.model.counter += 1;
self.label1.set_text(&self.model.counter.to_string());
}
} |
Warning
|
Also, the fn update(&mut self, event: Msg) {
self.model.text.push_str("Text");
} will not work as expected. Please use the following variation if needed. fn update(&mut self, event: Msg) {
self.model.text += "Text";
} |
For more information about how you can use relm, you can take a look at the examples.
relm
If you want to add your project to this list, please create a pull request.
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论