Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove GSource #171

Open
wants to merge 1 commit into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+1,640 −579
Diff settings

Always

Just for now

@@ -18,14 +18,13 @@ branch = "master"
repository = "antoyo/relm"

[dependencies]
cairo-rs = "^0.6.0"
cairo-rs = "^0.6.0" # TODO: make these dependencies optional (to have something like relm-state)?
glib = "^0.7.0"
glib-sys = "^0.8.0"
gobject-sys = "^0.8.0"
gtk = "^0.6.0"
libc = "^0.2.54"
log = "^0.4.6"
quote = "0.6"
slab = "^0.4.2"

[dependencies.syn]
features = ["full"]
@@ -41,7 +41,7 @@ extern crate relm;
#[macro_use]
extern crate relm_derive;
use relm::{Relm, Update, Widget};
use relm::{Loop, Relm, Update, Widget};
use gtk::prelude::*;
use gtk::{Window, Inhibit, WindowType};
----
@@ -103,7 +103,7 @@ impl Update for Win {
// Widgets may also be updated in this function.
fn update(&mut self, event: Msg) {
match event {
Msg::Quit => gtk::main_quit(),
Msg::Quit => Loop::quit(),
}
}
}
@@ -194,7 +194,7 @@ impl Widget for Win {
// 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(),
Msg::Quit => Loop::quit(),
}
}
@@ -88,7 +88,7 @@ impl Widget for Win {
},
OpenApp => self.open_app(),
OpenFile => self.open_file(),
Quit => gtk::main_quit(),
Quit => Loop::quit(),
ReadError(error) => println!("Read error: {}", error),
}
}
@@ -24,7 +24,7 @@ extern crate gtk;
extern crate relm;
extern crate relm_derive;
#[cfg_attr(test, macro_use)]
extern crate gtk_test;
extern crate relm_test;

use gtk::{
ButtonExt,
@@ -34,7 +34,7 @@ use gtk::{
WidgetExt,
};
use gtk::Orientation::Vertical;
use relm::Widget;
use relm::{Loop, Widget};
use relm_derive::{Msg, widget};

use self::Msg::*;
@@ -68,7 +68,7 @@ impl Widget for Win {
#[cfg(test)] Test => (),
Decrement => self.model.counter -= 1,
Increment => self.model.counter += 1,
Quit => gtk::main_quit(),
Quit => Loop::quit(),
}
}

@@ -110,7 +110,7 @@ mod tests {
use gtk::{ButtonExt, LabelExt};

use relm;
use gtk_test::click;
use relm_test::click;

use Win;

@@ -25,7 +25,7 @@ extern crate relm;
#[macro_use]
extern crate relm_derive;
#[macro_use]
extern crate gtk_test;
extern crate relm_test;

use gtk::{
ButtonExt,
@@ -69,7 +69,7 @@ impl Widget for Win {
match event {
Decrement => self.model.counter -= 1,
Increment => self.model.counter += 1,
Quit => gtk::main_quit(),
Quit => Loop::quit(),
Show => self.dec_button.set_visible(true),
}
}
@@ -112,7 +112,7 @@ mod tests {
use gtk::LabelExt;

use relm;
use gtk_test::click;
use relm_test::click;

use Win;

@@ -25,7 +25,7 @@ extern crate relm;
#[macro_use]
extern crate relm_derive;
#[cfg_attr(test, macro_use)]
extern crate gtk_test;
extern crate relm_test;

use gtk::{
Button,
@@ -39,7 +39,7 @@ use gtk::{
WindowType,
};
use gtk::Orientation::Vertical;
use relm::{Relm, Update, Widget, WidgetTest};
use relm::{Loop, Relm, Update, Widget, WidgetTest};

struct Model {
counter: i32,
@@ -93,7 +93,7 @@ impl Update for Win {
self.model.counter += 1;
label.set_text(&self.model.counter.to_string());
},
Msg::Quit => gtk::main_quit(),
Msg::Quit => Loop::quit(),
}
}
}
@@ -160,7 +160,7 @@ mod tests {
use gtk::LabelExt;

use relm;
use gtk_test::click;
use relm_test::click;

use Win;

@@ -25,15 +25,15 @@ extern crate gtk;
extern crate relm;
#[macro_use]
extern crate relm_derive;
extern crate gtk_test;
extern crate relm_test;

use chrono::{DateTime, Local};
use gtk::{
Inhibit,
LabelExt,
WidgetExt,
};
use relm::{Relm, Widget, interval};
use relm::{Loop, Relm, Widget, interval};
use relm_derive::widget;

use self::Msg::*;
@@ -63,7 +63,7 @@ impl Widget for Win {
fn update(&mut self, event: Msg) {
match event {
Tick => self.model.time = Local::now(),
Quit => gtk::main_quit(),
Quit => Loop::quit(),
}
}

@@ -88,7 +88,7 @@ mod tests {
use gtk::LabelExt;

use relm;
use gtk_test::wait;
use relm_test::wait;

use Win;

@@ -25,7 +25,7 @@ extern crate gtk;
extern crate relm;
#[macro_use]
extern crate relm_derive;
extern crate gtk_test;
extern crate relm_test;

use chrono::Local;
use gtk::{
@@ -37,7 +37,14 @@ use gtk::{
Window,
WindowType,
};
use relm::{Relm, Update, Widget, WidgetTest, interval};
use relm::{
Loop,
Relm,
Update,
Widget,
WidgetTest,
interval,
};

use self::Msg::*;

@@ -72,7 +79,7 @@ impl Update for Win {
let time = Local::now();
self.label.set_text(&format!("{}", time.format("%H:%M:%S")));
},
Quit => gtk::main_quit(),
Quit => Loop::quit(),
}
}
}
@@ -123,7 +130,7 @@ mod tests {
use gtk::LabelExt;

use relm;
use gtk_test::wait;
use relm_test::wait;

use Win;

@@ -42,6 +42,7 @@ use gtk::Orientation::Vertical;
use rand::Rng;
use relm::{
DrawHandler,
Loop,
Relm,
Widget,
interval,
@@ -138,7 +139,7 @@ impl Widget for Win {
}
},
MoveCursor(pos) => self.model.cursor_pos = pos,
Quit => gtk::main_quit(),
Quit => Loop::quit(),
UpdateDrawBuffer => {
let context = self.model.draw_handler.get_context();
context.set_source_rgb(1.0, 1.0, 1.0);
@@ -61,11 +61,11 @@ use gtk::{
};
use gtk::Orientation::Vertical;
use relm::{
Loop,
Relm,
Update,
UpdateNew,
Widget,
execute,
};
use relm_derive::widget;
use simplelog::{Config, TermLogger};
@@ -80,38 +80,38 @@ const READ_SIZE: usize = 1024;

pub struct Model {
button_enabled: bool,
event_loop: Loop,
loader: PixbufLoader,
relm: Relm<Win>,
topic: String,
text: String,
}

#[derive(Msg)]
pub enum Msg {
DownloadCompleted,
DownloadCompleted(usize),
FetchUrl,
HttpError(String),
ImageChunk(Vec<u8>),
NewGif(Vec<u8>),
NewGif(Vec<u8>, usize),
Quit,
}

#[widget]
impl Widget for Win {
fn model(relm: &Relm<Self>, (): ()) -> Model {
let topic = "cats";
Model {
button_enabled: true,
event_loop: relm.event_loop().clone(),
loader: PixbufLoader::new(),
relm: relm.clone(),
topic: topic.to_string(),
text: topic.to_string(),
text: String::new(),
}
}

fn update(&mut self, event: Msg) {
match event {
DownloadCompleted => {
DownloadCompleted(entry) => {
self.model.event_loop.remove_stream(entry);
self.model.button_enabled = true;
self.button.grab_focus();
self.model.loader.close().ok();
@@ -124,10 +124,11 @@ impl Widget for Win {
// loader.
self.model.button_enabled = false;

let url = format!("https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag={}",
self.model.topic);
let http = execute::<Http>(url);
connect_stream!(http@ReadDone(ref buffer), self.model.relm.stream(), NewGif(buffer.take()));
let url = format!("http://aws.random.cat/meow");
let http = self.model.event_loop.execute::<Http>(url);
let entry = self.model.event_loop.reserve();
connect_stream!(http@ReadDone(ref buffer), self.model.relm.stream(), NewGif(buffer.take(), entry));
self.model.event_loop.set_stream(entry, http);
},
HttpError(error) => {
self.model.button_enabled = true;
@@ -137,19 +138,22 @@ impl Widget for Win {
ImageChunk(chunk) => {
//self.model.loader.write(&chunk).expect("write failure");
if let Err(error) = self.model.loader.write(&chunk) {
println!("{}", error);
eprintln!("{}", error);
}
},
NewGif(buffer) => {
NewGif(buffer, entry) => {
self.model.event_loop.remove_stream(entry);
if let Ok(body) = String::from_utf8(buffer) {
let mut json = json::parse(&body).expect("invalid json");
let url = json["data"]["image_url"].take_string().expect("take_string failed");
let http = execute::<Http>(url);
let url = json["file"].take_string().expect("take_string failed");
let http = self.model.event_loop.execute::<Http>(url);
let entry = self.model.event_loop.reserve();
connect_stream!(http@DataRead(ref buffer), self.model.relm.stream(), ImageChunk(buffer.take()));
connect_stream!(http@ReadDone(_), self.model.relm.stream(), DownloadCompleted);
connect_stream!(http@ReadDone(_), self.model.relm.stream(), DownloadCompleted(entry));
self.model.event_loop.set_stream(entry, http);
}
},
Quit => gtk::main_quit(),
Quit => Loop::quit(),
}
}

@@ -254,7 +258,7 @@ impl Update for Http {
if let Ok(uri) = HttpUri::new(&self.model.url) {
let path = uri.resource.path;
let query = uri.resource.query.unwrap_or_default();
let buffer = format!("GET {}?{}\r\nHost: {}\r\n\r\n", path, query, uri.authority);
let buffer = format!("GET {}?{} HTTP/1.1\r\nHost: {}\r\n\r\n", path, query, uri.authority);
connect_async!(writer, write_async(buffer.into_bytes(), PRIORITY_DEFAULT), self.model.relm,
|_| Wrote);
}
@@ -29,7 +29,7 @@ use gtk::{
Inhibit,
WidgetExt,
};
use relm::{Component, Widget, init};
use relm::{Component, Loop, Widget, init};
use relm_derive::widget;

use self::Msg::*;
@@ -68,7 +68,7 @@ impl Widget for Win {

fn update(&mut self, event: Msg) {
match event {
Quit => gtk::main_quit(),
Quit => Loop::quit(),
}
}

Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.