print: Use LpImage for preview

This commit is contained in:
Sophie Herold 2023-12-25 20:18:26 +01:00
parent e387d8dc66
commit 6827a9c176
9 changed files with 143 additions and 44 deletions

View file

@ -22,10 +22,20 @@ impl LpImage {
///
/// Returns the default color if no one has been guessed yet
pub fn background_color(&self) -> gdk::RGBA {
(*self.imp().background_color.borrow()).unwrap_or_else(Self::default_background_color)
let imp = self.imp();
if let Some(fixed) = *imp.fixed_background_color.borrow() {
return fixed;
}
(*imp.background_color.borrow()).unwrap_or_else(Self::default_background_color)
}
pub fn set_background_color(&self, color: Option<gdk::RGBA>) {
pub fn set_fixed_background_color(&self, color: Option<gdk::RGBA>) {
self.imp().fixed_background_color.replace(color);
}
pub(super) fn set_background_color(&self, color: Option<gdk::RGBA>) {
self.imp().background_color.replace(color);
}
@ -51,6 +61,12 @@ impl LpImage {
/// For non-transparent images this always returns
/// `BACKGROUND_COLOR_DEFAULT`
pub async fn background_color_guess(&self) -> Option<gdk::RGBA> {
let imp = self.imp();
if imp.fixed_background_color.borrow().is_some() {
return None;
}
// Shortcut for formats that don't support transparency
if !self
.metadata()

View file

@ -146,9 +146,14 @@ impl LpImage {
self.emmit_metadata_changed();
}
DecoderUpdate::Animated => {
let callback_id = self
if imp.still.get() {
// Just load the first frame
self.imp().frame_buffer.next_frame();
} else {
let callback_id = self
.add_tick_callback(glib::clone!(@weak self as obj => @default-return glib::ControlFlow::Break, move |_, clock| obj.tick_callback(clock)));
imp.tick_callback.replace(Some(callback_id));
imp.tick_callback.replace(Some(callback_id));
}
}
DecoderUpdate::UnsupportedFormat => {
self.set_unsupported(true);

View file

@ -49,16 +49,23 @@ impl LpImage {
let texture_aspect_ratio = image_width / image_height;
let widget_aspect_ratio = self.width() as f64 / self.height() as f64;
let max_zoom_factor = match self.imp().fit_mode.get() {
// Do not allow to zoom larger than original size
FitMode::BestFit => 1.,
// Allow arbitrary zoom
FitMode::LargeFit => f64::MAX,
};
let default_zoom = if texture_aspect_ratio > widget_aspect_ratio {
(self.width() as f64 / image_width).min(1.)
(self.width() as f64 / image_width).min(max_zoom_factor)
} else {
(self.height() as f64 / image_height).min(1.)
(self.height() as f64 / image_height).min(max_zoom_factor)
};
let rotated_zoom = if 1. / texture_aspect_ratio > widget_aspect_ratio {
(self.width() as f64 / image_height).min(1.)
(self.width() as f64 / image_height).min(max_zoom_factor)
} else {
(self.height() as f64 / image_width).min(1.)
(self.height() as f64 / image_width).min(max_zoom_factor)
};
rotated * rotated_zoom + (1. - rotated) * default_zoom
@ -80,6 +87,11 @@ impl LpImage {
self.imp().best_fit.get()
}
pub fn set_fit_mode(&self, fit_mode: FitMode) {
self.imp().fit_mode.replace(fit_mode);
self.configure_best_fit();
}
pub(super) fn applicable_zoom(&self) -> f64 {
decoder::tiling::zoom_normalize(self.zoom()) / self.scaling()
}