Skip to content

Instantly share code, notes, and snippets.

@luc-tielen
Created October 28, 2017 15:42
Show Gist options
  • Save luc-tielen/361e034d9363948ea36fb3c31da9476a to your computer and use it in GitHub Desktop.
Save luc-tielen/361e034d9363948ea36fb3c31da9476a to your computer and use it in GitHub Desktop.
Bug in relm_attributes/outdated docs?
use relm::Widget;
use relm_attributes::widget;
use gtk;
use gtk::{WidgetExt, ButtonExt, LabelExt, OrientableExt};
use gtk::Orientation::Vertical;
// Had to add this code, even though docs say it can be auto generated?
pub struct Model {
counter: i32,
}
#[derive(Msg)]
pub enum Msg {
Increment,
Decrement,
Quit
}
use gui::Msg::*; // since this is located in 'gui.rs'; NOTE: can be removed if all messages are prepended with Msg::
// Up until here, down below is same as in README.
#[widget]
impl Widget for GUI {
fn model() -> Model {
Model {
counter: 0,
}
}
fn update(&mut self, event: Msg) {
match event {
// A call to self.label1.set_text() is automatically inserted by the
// attribute every time the model.counter attribute is updated.
Msg::Decrement => self.model.counter -= 1,
Msg::Increment => self.model.counter += 1,
Msg::Quit => gtk::main_quit(),
}
}
view! {
gtk::Window {
gtk::Box {
orientation: Vertical,
gtk::Button {
// By default, an event with one paramater is assumed.
clicked => Increment,
// Hence, the previous line is equivalent to:
// clicked(_) => Increment,
label: "+",
},
gtk::Label {
// Bind the text property of this Label to the counter attribute
// of the model.
// Every time the counter attribute is updated, the text property
// will be updated too.
text: &self.model.counter.to_string(),
},
gtk::Button {
clicked => Decrement,
label: "-",
},
},
// Use a tuple when you want to both send a message and return a value to
// the GTK+ callback.
delete_event(_, _) => (Quit, gtk::Inhibit(false)),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment