Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Switch from async callbacks to the with keyword
  • Loading branch information
antoyo committed Aug 15, 2017
1 parent fb25e28 commit 4356d37
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 609 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Expand Up @@ -19,14 +19,12 @@ branch = "master"
repository = "antoyo/relm"

[dependencies]
futures = "^0.1.14"
glib = "^0.1.2"
glib-sys = "^0.3.4"
gobject-sys = "^0.3.3"
gtk = "^0.1.1"
gtk-sys = "^0.3.3"
libc = "^0.2.22"
log = "^0.3.8"

[dependencies.futures-glib]
git = "https://github.com/antoyo/futures-glib-rs"
Expand Down
105 changes: 0 additions & 105 deletions examples/async-callback-attribute.rs

This file was deleted.

151 changes: 0 additions & 151 deletions examples/async-callback.rs

This file was deleted.

26 changes: 15 additions & 11 deletions examples/key-events-attribute.rs
Expand Up @@ -21,7 +21,6 @@

#![feature(proc_macro)]

extern crate futures;
extern crate futures_glib;
extern crate gtk;
#[macro_use]
Expand All @@ -30,48 +29,49 @@ extern crate relm_attributes;
#[macro_use]
extern crate relm_derive;

use std::cell::Cell;
use std::rc::Rc;

use gtk::{
Inhibit,
WidgetExt,
};
use relm::{Relm, Resolver, Widget};
use relm::{Relm, Widget};
use relm_attributes::widget;

use self::Msg::*;

#[derive(Msg)]
pub enum Msg {
Delete(Resolver<Inhibit>),
Delete,
Press,
Release,
Quit,
}

pub struct Model {
press_count: i32,
press_count: Rc<Cell<i32>>,
relm: Relm<Win>,
}

#[widget]
impl Widget for Win {
fn model(relm: &Relm<Self>, _: ()) -> Model {
Model {
press_count: 0,
press_count: Rc::new(Cell::new(0)),
relm: relm.clone(),
}
}

fn update(&mut self, event: Msg) {
match event {
Delete(mut resolver) => {
let inhibit = self.model.press_count > 3;
resolver.resolve(Inhibit(inhibit));
if !inhibit {
Delete => {
if self.model.press_count.get() <= 3 {
self.model.relm.stream().emit(Quit);
}
},
Press => {
self.model.press_count += 1;
self.model.press_count.set(self.model.press_count.get() + 1);
println!("Press");
},
Release => {
Expand All @@ -85,11 +85,15 @@ impl Widget for Win {
gtk::Window {
key_press_event(_, key) => (Press, Inhibit(false)),
key_release_event(_, key) => (Release, Inhibit(false)),
delete_event(_, _) => async Delete,
delete_event(_, _) with (press_count) => (Delete, inhibit_delete_event(&press_count)),
}
}
}

fn inhibit_delete_event(press_count: &Rc<Cell<i32>>) -> Inhibit {
Inhibit(press_count.get() > 3)
}

fn main() {
Win::run(()).unwrap();
}

0 comments on commit 4356d37

Please sign in to comment.