extern crate chrono;
extern crate gtk;
#[macro_use]
extern crate relm;
#[macro_use]
extern crate relm_derive;
extern crate simplelog;
extern crate tokio_core;
use std::time::Duration;
use chrono::Local;
use gtk::{
Button,
ButtonExt,
ContainerExt,
Dialog,
DialogExt,
Inhibit,
Label,
WidgetExt,
Window,
WindowType,
DIALOG_MODAL,
};
use gtk::Orientation::Vertical;
use relm::{Relm, RemoteRelm, Widget};
use simplelog::{Config, TermLogger};
use simplelog::LogLevelFilter::Warn;
use tokio_core::reactor::Interval;
use self::Msg::*;
#[derive(Msg)]
enum Msg {
Open(i32),
Quit,
Tick(()),
}
#[derive(Clone)]
struct Win {
label: Label,
num_label: Label,
window: Window,
}
impl Widget for Win {
type Container = Window;
type Model = ();
type Msg = Msg;
fn container(&self) -> &Self::Container {
&self.window
}
fn model() -> () {
()
}
fn subscriptions(relm: &Relm<Msg>) {
let stream = Interval::new(Duration::from_secs(1), relm.handle()).unwrap();
relm.connect_exec_ignore_err(stream, Tick);
}
fn update(&mut self, event: Msg, _model: &mut ()) {
match event {
Open(num) => {
self.num_label.set_text(&num.to_string());
},
Tick(()) => {
let time = Local::now();
self.label.set_text(&format!("{}", time.format("%H:%M:%S")));
},
Quit => gtk::main_quit(),
}
}
fn view(relm: RemoteRelm<Msg>, _model: &Self::Model) -> Self {
let button = Button::new_with_label("Open");
let label = Label::new(None);
let num_label = Label::new(None);
let window = Window::new(WindowType::Toplevel);
let vbox = gtk::Box::new(Vertical, 0);
vbox.add(&label);
vbox.add(&button);
vbox.add(&num_label);
window.add(&vbox);
window.show_all();
{
let window = window.clone();
connect!(relm, button, connect_clicked(_), {
let num = dialog(&window);
Open(num)
});
}
connect!(relm, window, connect_delete_event(_, _) (Some(Quit), Inhibit(false)));
let mut win = Win {
label: label,
num_label: num_label,
window: window,
};
win.update(Tick(()), &mut ());
win
}
}
fn dialog(window: &Window) -> i32 {
let buttons = &[("Yes", 1), ("No", 2)];
let dialog = Dialog::new_with_buttons(Some("Dialog"), Some(window), DIALOG_MODAL, buttons);
let result = dialog.run();
dialog.destroy();
result
}
fn main() {
TermLogger::init(Warn, Config::default()).unwrap();
relm::run::<Win>().unwrap();
}