Commit a099b553 authored by Daniel García Moreno's avatar Daniel García Moreno

image: Widget creation with multiple methods for config

I've added an option for fit to width and as we've a lot of options in
the constructor I've split the construction into several methods that
and a finish method called build that construct the real widget. This
way we've default values and the code is easier to read.
parent 657c5d51
Pipeline #15465 passed with stages
in 20 minutes and 30 seconds
......@@ -60,13 +60,9 @@ impl AppOp {
.get_object::<gtk::Viewport>("media_viewport")
.expect("Cant find media_viewport in ui file.");
let image = image::Image::new(&self.backend,
&url,
None,
image::Thumb(false),
image::Circle(false),
image::Fixed(true),
image::Centered(true));
let image = image::Image::new(&self.backend, &url)
.fit_to_width(true)
.fixed(true).center(true).build();
media_viewport.add(&image.widget);
media_viewport.show_all();
......@@ -119,13 +115,9 @@ impl AppOp {
media_viewport.remove(&child);
}
let image = image::Image::new(&self.backend,
&url,
None,
image::Thumb(false),
image::Circle(false),
image::Fixed(false),
image::Centered(true));
let image = image::Image::new(&self.backend, &url)
.fit_to_width(true)
.center(true).build();
image.widget.show();
media_viewport.add(&image.widget);
......@@ -168,13 +160,9 @@ impl AppOp {
media_viewport.remove(&child);
}
let image = image::Image::new(&self.backend,
&url,
None,
image::Thumb(false),
image::Circle(false),
image::Fixed(false),
image::Centered(true));
let image = image::Image::new(&self.backend, &url)
.fit_to_width(true)
.center(true).build();
image.widget.show();
media_viewport.add(&image.widget);
......
......@@ -160,9 +160,9 @@ impl AppOp {
}
let backend = self.backend.clone();
let image = widgets::image::Image::new(&backend, &img.thumbnail.clone(), Some((size, size)),
widgets::image::Thumb(true), widgets::image::Circle(false),
widgets::image::Fixed(true), widgets::image::Centered(false));
let image = widgets::image::Image::new(&backend, &img.thumbnail.clone())
.size(Some((size, size)))
.thumb(true).fixed(true).build();
let eb = gtk::EventBox::new();
eb.add(&image.widget);
bx.add(&eb);
......
......@@ -19,11 +19,6 @@ use self::gdk_pixbuf::PixbufAnimationExt;
use backend::BKCommand;
use std::sync::mpsc::TryRecvError;
pub struct Thumb(pub bool);
pub struct Circle(pub bool);
pub struct Fixed(pub bool);
pub struct Centered(pub bool);
#[derive(Clone, Debug)]
pub struct Image {
pub path: String,
......@@ -34,6 +29,7 @@ pub struct Image {
/// useful to avoid the scale_simple call on every draw
pub scaled: Arc<Mutex<Option<Pixbuf>>>,
pub zoom_level: Arc<Mutex<Option<f64>>>,
pub fit_to_width: bool,
pub thumb: bool,
pub circle: bool,
pub fixed_size: bool,
......@@ -41,11 +37,19 @@ pub struct Image {
}
impl Image {
pub fn new(backend: &Sender<BKCommand>, path: &str, size: Option<(i32, i32)>,
Thumb(thumb): Thumb, Circle(circle): Circle, Fixed(fixed_size): Fixed,
Centered(centered): Centered)
-> Image {
/// Image constructor this return an Image but not initialized, to
/// have a working image you should call to the build method
///
/// # Examples
///
/// ```
/// let img = Image::new(backend, "mxc://matrix.org/HASDH")
/// .circle(true)
/// .fixed(true)
/// .size(Some((50, 50)))
/// .build();
/// ```
pub fn new(backend: &Sender<BKCommand>, path: &str) -> Image {
let da = DrawingArea::new();
// gdk::EventMask::BUTTON_PRESS_MASK = 256
da.add_events(256);
......@@ -69,23 +73,57 @@ impl Image {
Inhibit(false)
});
let img = Image {
Image {
path: path.to_string(),
max_size: size,
max_size: None,
widget: da,
pixbuf: Arc::new(Mutex::new(None)),
scaled: Arc::new(Mutex::new(None)),
zoom_level: Arc::new(Mutex::new(None)),
thumb: thumb,
circle: circle,
thumb: false,
circle: false,
backend: backend.clone(),
fixed_size: fixed_size,
centered: centered,
};
img.draw();
img.load_async();
fixed_size: false,
centered: false,
fit_to_width: false,
}
}
pub fn fit_to_width(mut self, f: bool) -> Image {
self.fit_to_width = f;
self
}
pub fn center(mut self, c: bool) -> Image {
self.centered = c;
self
}
pub fn fixed(mut self, f: bool) -> Image {
self.fixed_size = f;
self
}
pub fn circle(mut self, c: bool) -> Image {
self.circle = c;
self
}
pub fn thumb(mut self, t: bool) -> Image {
self.thumb = t;
self
}
img
pub fn size(mut self, size: Option<(i32, i32)>) -> Image {
self.max_size = size;
self
}
pub fn build(self) -> Image {
self.draw();
self.load_async();
self
}
pub fn draw(&self) {
......@@ -125,6 +163,7 @@ impl Image {
let is_circle = self.circle.clone();
let fixed_size = self.fixed_size;
let centered = self.centered;
let fit_to_width = self.fit_to_width;
da.connect_draw(move |da, g| {
let widget_w = da.get_allocated_width();
let widget_h = da.get_allocated_height();
......@@ -162,7 +201,10 @@ impl Image {
pw = (pb.get_width() as f64 * zl) as i32;
ph = (pb.get_height() as f64 * zl) as i32;
},
None => *zoom_level_guard = Some(pw as f64 / pb.get_width() as f64),
None if fit_to_width => {
*zoom_level_guard = Some(pw as f64 / pb.get_width() as f64);
},
_ => {}
}
}
......
......@@ -273,11 +273,8 @@ impl<'a> MessageBox<'a> {
Some(ref m) => m.clone(),
None => msg.url.clone().unwrap_or_default(),
};
let image = widgets::image::Image::new(&backend, &img_path,
Some((600, 400)), widgets::image::Thumb(false),
widgets::image::Circle(false),
widgets::image::Fixed(false),
widgets::image::Centered(false));
let image = widgets::image::Image::new(&backend, &img_path)
.size(Some((600, 400))).build();
let image_name = msg.body.clone();
let room_id = self.room.id.clone();
......@@ -303,11 +300,9 @@ impl<'a> MessageBox<'a> {
let msg = self.msg;
let bx = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let backend = self.op.backend.clone();
let image = widgets::image::Image::new(&backend, &msg.url.clone().unwrap_or_default(),
Some((600, 400)), widgets::image::Thumb(false),
widgets::image::Circle(false),
widgets::image::Fixed(false),
widgets::image::Centered(false));
let image = widgets::image::Image::new(&backend,
&msg.url.clone().unwrap_or_default())
.size(Some((600, 400))).build();
let w = image.widget.clone();
w.set_tooltip_text(&self.msg.body[..]);
......
......@@ -23,7 +23,7 @@ use util::glib_thread_prelude::*;
use appop::AppOp;
use widgets::image::{Image, Thumb, Circle, Fixed, Centered};
use widgets::image::Image;
use self::gtk::WidgetExt;
const AVATAR_SIZE: i32 = 60;
......@@ -52,9 +52,11 @@ impl<'a> RoomBox<'a> {
if room.avatar.clone().unwrap_or_default().is_empty() {
make_identicon(&avatar, AVATAR_SIZE, room.id.clone(), room.name.clone().unwrap_or_default());
} else {
let mut avatar_widget = Image::new(&self.op.backend, &room.avatar.clone().unwrap_or_default(),
Some((AVATAR_SIZE, AVATAR_SIZE)), Thumb(true),
Circle(true), Fixed(true), Centered(false));
let mut avatar_widget = Image::new(&self.op.backend,
&room.avatar.clone().unwrap_or_default())
.size(Some((AVATAR_SIZE, AVATAR_SIZE)))
.thumb(true).circle(true)
.fixed(true).build();
avatar_widget.fixed_size = true;
avatar = avatar_widget.widget;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment