Skip to content

Instantly share code, notes, and snippets.

@zzeroo
Created November 22, 2017 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzeroo/c63499d1a320b4eff5325728600e7524 to your computer and use it in GitHub Desktop.
Save zzeroo/c63499d1a320b4eff5325728600e7524 to your computer and use it in GitHub Desktop.
First part of relm README
/// First, add this to you Cargo.toml:
// gtk = "^0.2.0"
// relm = "^0.10.0"
// relm-derive = "^0.10.0"
/// Next, add this to your crate:
extern crate gtk;
#[macro_use]
extern crate relm;
#[macro_use]
extern crate relm_derive;
use relm::{Relm, Update, Widget};
use gtk::prelude::*;
use gtk::{Window, Inhibit, WindowType};
/// Then, create your model:
struct Model {
// ...
}
/// The model contains the data related to a `Widget`. It may be updated by the `Widget::update` function.
///
/// Create your message enum:
#[derive(Msg)]
enum Msg {
Quit,
}
/// Messages are sent to `Widget::update` to indicate that an event happened. The model can be updated when an event is received.
///
/// Create a `struct` which represents a `Widget` which contains the GTK+ widgets (in this case, the main window of the application) and the model:
struct Win {
// ...
model: Model,
window: Window,
}
/// To make this `struct` a relm `Widget` that can be shown by the library, implement the `Update` and `Widget` traits:
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.
// Futures and streams can be connected to send a message when a value is ready.
fn update(&mut self, event: Msg) {
match event {
// Msg::SomeEvent => {
// let future = create_future();
// relm.connect_exec_ignore_err(future, SomeEvent);
// },
Msg::Quit => gtk::main_quit(),
}
}
// // The next method is optional.
// // Futures and streams can be connected when the `Widget` is created in the
// // `subscriptions()` method.
// fn subscriptions(&mut self, relm: &Relm<Self>) {
// let stream = Interval::new(Duration::from_secs(1));
// relm.connct_exec_ignore_err(stream, Tick);
// }
}
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: window,
}
}
}
/// Finally, show this Widget by calling Win::run():
fn main() {
Win::run(()).unwrap();
}
Copy link

ghost commented Mar 4, 2018

Thanks you for sparing me the trouble of cleaning the example code myself 😬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment