Better channel sync for member avatar

This commit is contained in:
Daniel García Moreno 2017-12-24 19:00:54 +01:00
parent fbbd6a6e5c
commit 61e592b2f9
4 changed files with 30 additions and 15 deletions

View file

@ -19,6 +19,7 @@ use self::secret_service::EncryptionType;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::channel;
use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc::TryRecvError;
use std::collections::HashMap;
use std::process::Command;
use std::thread;
@ -286,7 +287,11 @@ impl AppOp {
.get_object::<gtk::MenuButton>("user_menu_button")
.expect("Can't find user_menu_button in ui file.");
button.set_image(&widgets::Avatar::circle_avatar(String::from(fname), Some(20)));
let eb = gtk::EventBox::new();
let w = widgets::Avatar::circle_avatar(String::from(fname), Some(20));
eb.connect_button_press_event(move |_, _| { Inhibit(false) });
eb.add(&w);
button.set_image(&eb);
}
pub fn disconnect(&self) {
@ -996,7 +1001,8 @@ impl AppOp {
let bk = self.backend.clone();
let m = msg.clone();
gtk::timeout_add(50, move || match rx.try_recv() {
Err(_) => gtk::Continue(true),
Err(TryRecvError::Empty) => gtk::Continue(true),
Err(TryRecvError::Disconnected) => gtk::Continue(false),
Ok((name, avatar)) => {
let summary = format!("@{} / {}", name, roomname);

View file

@ -1,7 +1,6 @@
extern crate gtk;
extern crate gdk;
extern crate gdk_pixbuf;
extern crate cairo;
use self::gtk::prelude::*;
pub use self::gtk::DrawingArea;
@ -30,6 +29,7 @@ impl AvatarExt for gtk::Box {
fn create_da(&self, size: Option<i32>) -> DrawingArea {
let da = DrawingArea::new();
let s = size.unwrap_or(40);
da.set_size_request(s, s);
self.pack_start(&da, true, true, 0);
@ -75,17 +75,21 @@ impl AvatarExt for gtk::Box {
let context = da.get_style_context().unwrap();
gtk::render_background(&context, g, 0.0, 0.0, width, height);
g.set_antialias(cairo::Antialias::Best);
let icon = gtk::IconTheme::get_default().unwrap()
.load_icon(&icon[..], s, gtk::IconLookupFlags::empty())
.unwrap();
if let None = icon {
eprintln!("BAD IMAGE");
let pb;
if let Some(i1) = gtk::IconTheme::get_default() {
if let Ok(Some(i2)) = i1.load_icon(&icon[..], s, gtk::IconLookupFlags::empty()) {
pb = i2;
} else {
eprintln!("Bad icon");
return Inhibit(false);
}
} else {
eprintln!("Error getting icon theme");
return Inhibit(false);
}
let pb = icon.unwrap();
let hpos: f64 = (width - (pb.get_height()) as f64) / 2.0;
g.set_source_pixbuf(&pb, 0.0, hpos);
@ -95,7 +99,7 @@ impl AvatarExt for gtk::Box {
g.arc(width / 2.0, height / 2.0, width.min(height) / 2.5, 0.0, 2.0 * PI);
g.clip();
Inhibit(false)
Inhibit(true)
});
}
@ -132,7 +136,7 @@ impl AvatarExt for gtk::Box {
da.queue_draw();
}
Inhibit(false)
Inhibit(true)
});
}
}

View file

@ -11,6 +11,7 @@ use backend::BKCommand;
use std::sync::mpsc::channel;
use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc::TryRecvError;
use app::AppOp;
@ -70,7 +71,8 @@ pub fn get_member_avatar(backend: Sender<BKCommand>,
let (tx, rx): (Sender<String>, Receiver<String>) = channel();
backend.send(BKCommand::GetAvatarAsync(m.clone(), tx)).unwrap();
gtk::timeout_add(50, move || match rx.try_recv() {
Err(_) => gtk::Continue(true),
Err(TryRecvError::Empty) => gtk::Continue(true),
Err(TryRecvError::Disconnected) => gtk::Continue(false),
Ok(avatar) => {
if let Ok(_) = Pixbuf::new_from_file_at_scale(&avatar, size, size, false) {
img.circle(avatar, Some(size));
@ -95,7 +97,8 @@ pub fn get_member_info(backend: Sender<BKCommand>,
let (tx, rx): (Sender<(String, String)>, Receiver<(String, String)>) = channel();
backend.send(BKCommand::GetUserInfoAsync(sender.clone(), tx)).unwrap();
gtk::timeout_add(100, move || match rx.try_recv() {
Err(_) => gtk::Continue(true),
Err(TryRecvError::Empty) => gtk::Continue(true),
Err(TryRecvError::Disconnected) => gtk::Continue(false),
Ok((name, avatar)) => {
if let Ok(_) = Pixbuf::new_from_file_at_scale(&avatar, size, size, false) {
img.circle(avatar, Some(size));

View file

@ -14,6 +14,7 @@ use util;
use std::sync::mpsc::channel;
use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc::TryRecvError;
use app::AppOp;
@ -49,7 +50,8 @@ impl<'a> RoomBox<'a> {
let (tx, rx): (Sender<String>, Receiver<String>) = channel();
self.op.backend.send(BKCommand::GetThumbAsync(r.avatar.clone().unwrap_or_default().clone(), tx)).unwrap();
gtk::timeout_add(50, move || match rx.try_recv() {
Err(_) => gtk::Continue(true),
Err(TryRecvError::Empty) => gtk::Continue(true),
Err(TryRecvError::Disconnected) => gtk::Continue(false),
Ok(fname) => {
let mut f = fname.clone();
if f.is_empty() {