extern crate gtk;
#[macro_use]
extern crate relm;
#[macro_use]
extern crate relm_derive;
use gtk::{
ContainerExt,
EditableSignals,
Entry,
EntryExt,
Inhibit,
Label,
WidgetExt,
Window,
WindowType,
};
use gtk::Orientation::Vertical;
use relm::{RemoteRelm, Widget};
use self::Msg::*;
#[derive(Clone)]
struct Model {
content: String,
}
#[derive(Msg)]
enum Msg {
Change,
Quit,
}
#[derive(Clone)]
struct Win {
input: Entry,
label: Label,
window: Window,
}
impl Widget for Win {
type Container = Window;
type Model = Model;
type Msg = Msg;
fn container(&self) -> &Self::Container {
&self.window
}
fn model() -> Model {
Model {
content: String::new(),
}
}
fn update(&mut self, event: Msg, model: &mut Model) {
match event {
Change => {
model.content = self.input.get_text().unwrap().chars().rev().collect();
self.label.set_text(&model.content);
},
Quit => gtk::main_quit(),
}
}
fn view(relm: RemoteRelm<Msg>, _model: &Self::Model) -> Self {
let vbox = gtk::Box::new(Vertical, 0);
let input = Entry::new();
vbox.add(&input);
let label = Label::new(None);
vbox.add(&label);
let window = Window::new(WindowType::Toplevel);
window.add(&vbox);
window.show_all();
connect!(relm, input, connect_changed(_), Change);
connect!(relm, window, connect_delete_event(_, _) (Some(Quit), Inhibit(false)));
Win {
input: input,
label: label,
window: window,
}
}
}
fn main() {
relm::run::<Win>().unwrap();
}