Commit 17b5b388 authored by Eisha Chen-yen-su's avatar Eisha Chen-yen-su

Image: Add the ability to center the image

This adds the ability to center the selected image in the media
viewer.

See https://gitlab.gnome.org/World/fractal/issues/13
parent 2876eb6c
......@@ -45,7 +45,8 @@ impl AppOp {
None,
image::Thumb(false),
image::Circle(false),
image::Fixed(true));
image::Fixed(true),
image::Centered(true));
media_viewport.add(&image.widget);
media_viewport.show_all();
......@@ -88,7 +89,8 @@ impl AppOp {
None,
image::Thumb(false),
image::Circle(false),
image::Fixed(false));
image::Fixed(false),
image::Centered(true));
image.widget.show();
media_viewport.add(&image.widget);
......@@ -119,7 +121,8 @@ impl AppOp {
None,
image::Thumb(false),
image::Circle(false),
image::Fixed(false));
image::Fixed(false),
image::Centered(true));
image.widget.show();
media_viewport.add(&image.widget);
......
......@@ -162,7 +162,7 @@ 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::Fixed(true), widgets::image::Centered(false));
let eb = gtk::EventBox::new();
eb.add(&image.widget);
bx.add(&eb);
......
......@@ -22,6 +22,7 @@ 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 {
......@@ -35,11 +36,13 @@ pub struct Image {
pub thumb: bool,
pub circle: bool,
pub fixed_size: bool,
pub centered: bool,
}
impl Image {
pub fn new(backend: &Sender<BKCommand>, path: &str, size: Option<(i32, i32)>,
Thumb(thumb): Thumb, Circle(circle): Circle, Fixed(fixed_size): Fixed)
Thumb(thumb): Thumb, Circle(circle): Circle, Fixed(fixed_size): Fixed,
Centered(centered): Centered)
-> Image {
let da = DrawingArea::new();
......@@ -75,6 +78,7 @@ impl Image {
circle: circle,
backend: backend.clone(),
fixed_size: fixed_size,
centered: centered,
};
img.draw();
img.load_async();
......@@ -117,6 +121,7 @@ impl Image {
let scaled = self.scaled.clone();
let is_circle = self.circle.clone();
let fixed_size = self.fixed_size;
let centered = self.centered;
da.connect_draw(move |da, g| {
let widget_w = da.get_allocated_width();
let widget_h = da.get_allocated_height();
......@@ -173,8 +178,18 @@ impl Image {
g.clip();
}
g.set_source_pixbuf(&sc, 0.0, 0.0);
g.rectangle(0.0, 0.0, pw as f64, ph as f64);
let x = if centered {
(width / 2.0) - (pw as f64 / 2.0)
} else {
0.0
};
let y = if centered {
(height / 2.0) - (ph as f64 / 2.0)
} else {
0.0
};
g.set_source_pixbuf(&sc, x, y);
g.rectangle(x, y, pw as f64, ph as f64);
g.fill();
*scaled.lock().unwrap() = Some(sc);
}
......
......@@ -275,7 +275,9 @@ impl<'a> MessageBox<'a> {
};
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::Circle(false),
widgets::image::Fixed(false),
widgets::image::Centered(false));
let room_id = self.room.id.clone();
image.widget.connect_button_press_event(move |_, _| {
......@@ -301,7 +303,9 @@ impl<'a> MessageBox<'a> {
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::Circle(false),
widgets::image::Fixed(false),
widgets::image::Centered(false));
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};
use widgets::image::{Image, Thumb, Circle, Fixed, Centered};
use self::gtk::WidgetExt;
const AVATAR_SIZE: i32 = 60;
......@@ -54,7 +54,7 @@ impl<'a> RoomBox<'a> {
} 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));
Circle(true), Fixed(true), Centered(false));
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