#![feature(proc_macro)]
extern crate gtk;
#[macro_use]
extern crate relm;
extern crate relm_attributes;
#[macro_use]
extern crate relm_derive;
use std::fmt::Display;
use gtk::{
ButtonExt,
Inhibit,
LabelExt,
OrientableExt,
WidgetExt,
};
use gtk::Orientation::{Horizontal, Vertical};
use relm::Widget;
use relm_attributes::widget;
use self::CounterMsg::*;
use self::Msg::*;
pub trait IncDec {
fn dec(&self) -> Self;
fn inc(&self) -> Self;
}
impl IncDec for i32 {
fn dec(&self) -> Self {
*self - 1
}
fn inc(&self) -> Self {
*self + 1
}
}
impl IncDec for i64 {
fn dec(&self) -> Self {
*self - 1
}
fn inc(&self) -> Self {
*self + 1
}
}
pub struct Model<S, T> {
counter1: S,
_counter2: T,
}
#[derive(Msg)]
pub enum CounterMsg {
Decrement,
Increment,
}
#[widget]
impl<S: Clone + Display + IncDec, T: Clone + Display + IncDec> Widget for Counter<S, T> {
fn model((value1, value2): (S, T)) -> Model<S, T> {
Model {
counter1: value1,
_counter2: value2,
}
}
fn update(&mut self, event: CounterMsg) {
match event {
Decrement => {
self.model.counter1 = self.model.counter1.dec();
},
Increment => {
self.model.counter1 = self.model.counter1.inc();
},
}
}
view! {
gtk::Box {
orientation: Vertical,
gtk::Button {
label: "+",
clicked => Increment,
},
gtk::Label {
text: &self.model.counter1.to_string(),
},
gtk::Button {
label: "-",
clicked => Decrement,
},
}
}
}
#[derive(Msg)]
pub enum Msg {
Quit,
}
#[widget]
impl Widget for Win {
fn model() -> () {
()
}
fn update(&mut self, event: Msg) {
match event {
Quit => gtk::main_quit(),
}
}
view! {
gtk::Window {
gtk::Box {
orientation: Horizontal,
Counter<i32, i64>(2, 3),
Counter<i32, i64>(3, 4),
},
delete_event(_, _) => (Quit, Inhibit(false)),
}
}
}
fn main() {
Win::run(()).unwrap();
}