image/scrollable: Abstract adjustment access
This commit is contained in:
parent
3ab4d82d36
commit
d69d0d5e47
3 changed files with 35 additions and 25 deletions
|
|
@ -144,11 +144,8 @@ impl imp::LpImage {
|
||||||
|
|
||||||
drag_gesture.connect_drag_update(glib::clone!(@weak obj => move |_, x1, y1| {
|
drag_gesture.connect_drag_update(glib::clone!(@weak obj => move |_, x1, y1| {
|
||||||
if let Some((x0, y0)) = obj.imp().last_drag_value.get() {
|
if let Some((x0, y0)) = obj.imp().last_drag_value.get() {
|
||||||
let hadjustment = obj.hadjustment();
|
obj.set_hadj_value(obj.hadj_value() - x1 + x0);
|
||||||
let vadjustment = obj.vadjustment();
|
obj.set_vadj_value(obj.vadj_value() - y1 + y0);
|
||||||
|
|
||||||
hadjustment.set_value(hadjustment.value() - x1 + x0);
|
|
||||||
vadjustment.set_value(vadjustment.value() - y1 + y0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
obj.imp().last_drag_value.set(Some((x1, y1)));
|
obj.imp().last_drag_value.set(Some((x1, y1)));
|
||||||
|
|
@ -213,16 +210,14 @@ impl imp::LpImage {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
zoom_gesture.connect_scale_changed(glib::clone!(@weak obj => move |gesture, scale| {
|
zoom_gesture.connect_scale_changed(glib::clone!(@weak obj => move |gesture, scale| {
|
||||||
let hadjustment = obj.hadjustment();
|
|
||||||
let vadjustment = obj.vadjustment();
|
|
||||||
let zoom = obj.imp().zoom_target.get() * scale;
|
let zoom = obj.imp().zoom_target.get() * scale;
|
||||||
|
|
||||||
// Move image with fingers on touchscreens
|
// Move image with fingers on touchscreens
|
||||||
if gesture.device().map(|x| x.source()) == Some(gdk::InputSource::Touchscreen) {
|
if gesture.device().map(|x| x.source()) == Some(gdk::InputSource::Touchscreen) {
|
||||||
if let p1 @ Some((x1, y1)) = gesture.bounding_box_center() {
|
if let p1 @ Some((x1, y1)) = gesture.bounding_box_center() {
|
||||||
if let Some((x0, y0)) = obj.imp().zoom_gesture_center.get() {
|
if let Some((x0, y0)) = obj.imp().zoom_gesture_center.get() {
|
||||||
hadjustment.set_value(hadjustment.value() + x0 - x1);
|
obj.set_hadj_value(obj.hadj_value() + x0 - x1);
|
||||||
vadjustment.set_value(vadjustment.value() + y0 - y1);
|
obj.set_vadj_value(obj.vadj_value()+ y0 - y1);
|
||||||
} else {
|
} else {
|
||||||
log::warn!("Zoom bounding box center: No previous value");
|
log::warn!("Zoom bounding box center: No previous value");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,9 @@ use super::*;
|
||||||
impl ScrollableImpl for imp::LpImage {}
|
impl ScrollableImpl for imp::LpImage {}
|
||||||
|
|
||||||
impl imp::LpImage {
|
impl imp::LpImage {
|
||||||
|
/// Adjustment setter needed for Scrollable implementation
|
||||||
|
///
|
||||||
|
/// The adjustment get's set by the `GtkScrolledWindow`
|
||||||
pub fn set_hadjustment(&self, hadjustment: Option<gtk::Adjustment>) {
|
pub fn set_hadjustment(&self, hadjustment: Option<gtk::Adjustment>) {
|
||||||
let obj = self.obj();
|
let obj = self.obj();
|
||||||
|
|
||||||
|
|
@ -100,6 +103,26 @@ impl LpImage {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_hadj_value(&self, value: f64) {
|
||||||
|
let hadjustment = self.hadjustment();
|
||||||
|
let value = value.clamp(0., hadjustment.upper() - hadjustment.page_size());
|
||||||
|
hadjustment.set_value(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_vadj_value(&self, value: f64) {
|
||||||
|
let vadjustment = self.vadjustment();
|
||||||
|
let value = value.clamp(0., vadjustment.upper() - vadjustment.page_size());
|
||||||
|
self.vadjustment().set_value(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hadj_value(&self) -> f64 {
|
||||||
|
self.hadjustment().value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn vadj_value(&self) -> f64 {
|
||||||
|
self.vadjustment().value()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn max_hadjustment_value(&self) -> f64 {
|
pub fn max_hadjustment_value(&self) -> f64 {
|
||||||
f64::max(self.image_displayed_width() - self.widget_width(), 0.)
|
f64::max(self.image_displayed_width() - self.widget_width(), 0.)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -137,30 +137,24 @@ impl LpImage {
|
||||||
|
|
||||||
if self.imp().zoom_hscrollbar_transition.get() {
|
if self.imp().zoom_hscrollbar_transition.get() {
|
||||||
if zoom_ratio < 1. {
|
if zoom_ratio < 1. {
|
||||||
self.hadjustment()
|
self.set_hadj_value(self.max_hadjustment_value() / 2.);
|
||||||
.set_value(self.max_hadjustment_value() / 2.);
|
|
||||||
} else {
|
} else {
|
||||||
// move towards center
|
// move towards center
|
||||||
self.hadjustment()
|
self.set_hadj_value(self.hadjustment_corrected_for_zoom(zoom_ratio, center_x));
|
||||||
.set_value(self.hadjustment_corrected_for_zoom(zoom_ratio, center_x));
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.hadjustment()
|
self.set_hadj_value(self.hadjustment_corrected_for_zoom(zoom_ratio, x));
|
||||||
.set_value(self.hadjustment_corrected_for_zoom(zoom_ratio, x));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.imp().zoom_vscrollbar_transition.get() {
|
if self.imp().zoom_vscrollbar_transition.get() {
|
||||||
if zoom_ratio < 1. {
|
if zoom_ratio < 1. {
|
||||||
self.vadjustment()
|
self.set_vadj_value(self.max_vadjustment_value() / 2.);
|
||||||
.set_value(self.max_vadjustment_value() / 2.);
|
|
||||||
} else {
|
} else {
|
||||||
// move towards center
|
// move towards center
|
||||||
self.vadjustment()
|
self.set_vadj_value(self.vadjustment_corrected_for_zoom(zoom_ratio, center_y));
|
||||||
.set_value(self.vadjustment_corrected_for_zoom(zoom_ratio, center_y));
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.vadjustment()
|
self.set_vadj_value(self.vadjustment_corrected_for_zoom(zoom_ratio, y));
|
||||||
.set_value(self.vadjustment_corrected_for_zoom(zoom_ratio, y));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.notify_zoom();
|
self.notify_zoom();
|
||||||
|
|
@ -301,7 +295,6 @@ impl LpImage {
|
||||||
/// in the image at the same place in the widget, the returned value is
|
/// in the image at the same place in the widget, the returned value is
|
||||||
/// the correct value for hadjustment to achieve that.
|
/// the correct value for hadjustment to achieve that.
|
||||||
pub fn hadjustment_corrected_for_zoom(&self, zoom_delta: f64, x: f64) -> f64 {
|
pub fn hadjustment_corrected_for_zoom(&self, zoom_delta: f64, x: f64) -> f64 {
|
||||||
let adj = self.hadjustment();
|
|
||||||
// Width of bars to the left and right of the image
|
// Width of bars to the left and right of the image
|
||||||
let border = if self.widget_width() > self.image_displayed_width() {
|
let border = if self.widget_width() > self.image_displayed_width() {
|
||||||
(self.widget_width() - self.image_displayed_width()) / 2.
|
(self.widget_width() - self.image_displayed_width()) / 2.
|
||||||
|
|
@ -309,12 +302,11 @@ impl LpImage {
|
||||||
0.
|
0.
|
||||||
};
|
};
|
||||||
|
|
||||||
f64::max((x + adj.value() - border) / zoom_delta - x, 0.)
|
f64::max((x + self.hadj_value() - border) / zoom_delta - x, 0.)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same but for vertical adjustment
|
/// Same but for vertical adjustment
|
||||||
pub fn vadjustment_corrected_for_zoom(&self, zoom_delta: f64, y: f64) -> f64 {
|
pub fn vadjustment_corrected_for_zoom(&self, zoom_delta: f64, y: f64) -> f64 {
|
||||||
let adj = self.vadjustment();
|
|
||||||
// Width of bars to the top and bottom of the image
|
// Width of bars to the top and bottom of the image
|
||||||
let border = if self.widget_height() > self.image_displayed_height() {
|
let border = if self.widget_height() > self.image_displayed_height() {
|
||||||
(self.widget_height() - self.image_displayed_height()) / 2.
|
(self.widget_height() - self.image_displayed_height()) / 2.
|
||||||
|
|
@ -322,6 +314,6 @@ impl LpImage {
|
||||||
0.
|
0.
|
||||||
};
|
};
|
||||||
|
|
||||||
f64::max((y + adj.value() - border) / zoom_delta - y, 0.)
|
f64::max((y + self.vadj_value() - border) / zoom_delta - y, 0.)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue