[niepce] gtk4: Use texture directly in LibraryCellRenderer



commit f6e3fc2244bea17088f6e21bcb60e799591d7e63
Author: Hubert Figuière <hub figuiere net>
Date:   Mon May 23 20:14:27 2022 -0400

    gtk4: Use texture directly in LibraryCellRenderer

 Cargo.lock                                         |  5 +-
 crates/npc-fwk/Cargo.toml                          |  2 +-
 niepce-main/Cargo.toml                             |  3 +-
 niepce-main/src/niepce/ui/library_cell_renderer.rs | 87 +++++++++++++---------
 4 files changed, 58 insertions(+), 39 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 1d9a58e..296f62f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -712,6 +712,7 @@ dependencies = [
  "graphene-rs",
  "gtk4",
  "gtk4-sys",
+ "lazy_static",
  "libc",
  "npc-engine",
  "npc-fwk",
@@ -794,9 +795,9 @@ dependencies = [
 
 [[package]]
 name = "once_cell"
-version = "1.10.0"
+version = "1.12.0"
 source = "registry+https://github.com/rust-lang/crates.io-index";
-checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
+checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225"
 
 [[package]]
 name = "pango"
diff --git a/crates/npc-fwk/Cargo.toml b/crates/npc-fwk/Cargo.toml
index f555ec1..788f008 100644
--- a/crates/npc-fwk/Cargo.toml
+++ b/crates/npc-fwk/Cargo.toml
@@ -26,7 +26,7 @@ libc = "0.2.39"
 libopenraw-rs = { path = "../../../libopenraw-rs/libopenraw-rs" }
 # { git = "https://gitlab.freedesktop.org/libopenraw/libopenraw-rs.git";, rev = 
"edae9f8771fa4f4b577ef2e56d9dbd6f68d97105" }
 multimap = "0.4.0"
-once_cell = "1.8.0"
+once_cell = "^1.11.0"
 rexiv2 = "^0.9.1"
 
 
diff --git a/niepce-main/Cargo.toml b/niepce-main/Cargo.toml
index 4ee7992..c829d59 100644
--- a/niepce-main/Cargo.toml
+++ b/niepce-main/Cargo.toml
@@ -7,7 +7,7 @@ edition = "2018"
 
 [dependencies]
 async-channel = "1.6.1"
-once_cell = "^1.8.0"
+once_cell = "^1.11.0"
 gettext-rs = "0.3.0"
 glib = "*"
 gio-sys = "*"
@@ -19,6 +19,7 @@ gdk-pixbuf-sys = "*"
 graphene-rs = "0.15.1"
 gtk4-sys = "*"
 gtk4 = "*"
+lazy_static = "^1.4.0"
 libc = "0.2.39"
 #gphoto = "0.1.1"
 
diff --git a/niepce-main/src/niepce/ui/library_cell_renderer.rs 
b/niepce-main/src/niepce/ui/library_cell_renderer.rs
index 14d0988..35ce172 100644
--- a/niepce-main/src/niepce/ui/library_cell_renderer.rs
+++ b/niepce-main/src/niepce/ui/library_cell_renderer.rs
@@ -18,12 +18,11 @@
  */
 
 use libc::c_void;
-use once_cell::unsync::Lazy;
 use std::cell::{Cell, RefCell};
 use std::ptr;
 
 use gdk4::prelude::*;
-use gdk_pixbuf::Pixbuf;
+use gdk4::Texture;
 use glib::subclass::prelude::*;
 use glib::subclass::Signal;
 use glib::translate::*;
@@ -41,26 +40,28 @@ use npc_fwk::{dbg_out, err_out, on_err_out};
 const CELL_PADDING: f32 = 4.0;
 
 struct Emblems {
-    raw: Pixbuf,
-    raw_jpeg: Pixbuf,
-    img: Pixbuf,
-    video: Pixbuf,
-    unknown: Pixbuf,
-    status_missing: Pixbuf,
-    flag_reject: Pixbuf,
-    flag_pick: Pixbuf,
+    raw: Texture,
+    raw_jpeg: Texture,
+    img: Texture,
+    video: Texture,
+    unknown: Texture,
+    status_missing: Texture,
+    flag_reject: Texture,
+    flag_pick: Texture,
 }
 
-const EMBLEMS: Lazy<Emblems> = Lazy::new(|| Emblems {
-    raw: Pixbuf::from_resource("/org/gnome/Niepce/pixmaps/niepce-raw-fmt.png").unwrap(),
-    raw_jpeg: Pixbuf::from_resource("/org/gnome/Niepce/pixmaps/niepce-rawjpeg-fmt.png").unwrap(),
-    img: Pixbuf::from_resource("/org/gnome/Niepce/pixmaps/niepce-img-fmt.png").unwrap(),
-    video: Pixbuf::from_resource("/org/gnome/Niepce/pixmaps/niepce-video-fmt.png").unwrap(),
-    unknown: Pixbuf::from_resource("/org/gnome/Niepce/pixmaps/niepce-unknown-fmt.png").unwrap(),
-    status_missing: Pixbuf::from_resource("/org/gnome/Niepce/pixmaps/niepce-missing.png").unwrap(),
-    flag_reject: Pixbuf::from_resource("/org/gnome/Niepce/pixmaps/niepce-flag-reject.png").unwrap(),
-    flag_pick: Pixbuf::from_resource("/org/gnome/Niepce/pixmaps/niepce-flag-pick.png").unwrap(),
-});
+lazy_static::lazy_static! {
+    static ref EMBLEMS: Emblems = Emblems {
+        raw: Texture::from_resource("/org/gnome/Niepce/pixmaps/niepce-raw-fmt.png"),
+        raw_jpeg: Texture::from_resource("/org/gnome/Niepce/pixmaps/niepce-rawjpeg-fmt.png"),
+        img: Texture::from_resource("/org/gnome/Niepce/pixmaps/niepce-img-fmt.png"),
+        video: Texture::from_resource("/org/gnome/Niepce/pixmaps/niepce-video-fmt.png"),
+        unknown: Texture::from_resource("/org/gnome/Niepce/pixmaps/niepce-unknown-fmt.png"),
+        status_missing: Texture::from_resource("/org/gnome/Niepce/pixmaps/niepce-missing.png"),
+        flag_reject: Texture::from_resource("/org/gnome/Niepce/pixmaps/niepce-flag-reject.png"),
+        flag_pick: Texture::from_resource("/org/gnome/Niepce/pixmaps/niepce-flag-pick.png"),
+    };
+}
 
 glib::wrapper! {
     pub struct LibraryCellRenderer(
@@ -260,41 +261,57 @@ impl LibraryCellRendererPriv {
         snapshot.restore();
     }
 
-    fn do_draw_flag(cr: &cairo::Context, flag: i32, r: &Rect) {
+    fn do_draw_flag(snapshot: &gtk4::Snapshot, flag: i32, r: &Rect) {
         if flag == 0 {
             return;
         }
-        let pixbuf = match flag {
+        let texture = match flag {
             -1 => EMBLEMS.flag_reject.clone(),
             1 => EMBLEMS.flag_pick.clone(),
             _ => return,
         };
 
-        let w = pixbuf.width() as f32;
+        let w = texture.width() as f32;
         let x = r.x() + r.width() - CELL_PADDING - w;
         let y = r.y() + CELL_PADDING;
-        cr.set_source_pixbuf(&pixbuf, x.into(), y.into());
-        on_err_out!(cr.paint());
+        snapshot.save();
+        snapshot.translate(&graphene::Point::new(x, y));
+        texture.snapshot(
+            snapshot.upcast_ref::<gdk4::Snapshot>(),
+            texture.width() as f64,
+            texture.height() as f64,
+        );
+        snapshot.restore();
     }
 
-    fn do_draw_status(cr: &cairo::Context, status: FileStatus, r: &Rect) {
+    fn do_draw_status(snapshot: &gtk4::Snapshot, status: FileStatus, r: &Rect) {
         if status == FileStatus::Ok {
             return;
         }
         let x = r.x() + CELL_PADDING;
         let y = r.y() + CELL_PADDING;
-        cr.set_source_pixbuf(&EMBLEMS.status_missing, x.into(), y.into());
-        on_err_out!(cr.paint());
+        snapshot.save();
+        snapshot.translate(&graphene::Point::new(x, y));
+        let texture = &EMBLEMS.status_missing;
+        texture.snapshot(
+            snapshot.upcast_ref::<gdk4::Snapshot>(),
+            texture.width() as f64,
+            texture.height() as f64,
+        );
+        snapshot.restore();
     }
 
-    fn do_draw_format_emblem(cr: &cairo::Context, emblem: &Pixbuf, r: &Rect) -> f32 {
+    fn do_draw_format_emblem(snapshot: &gtk4::Snapshot, emblem: &Texture, r: &Rect) -> f32 {
         let w = emblem.width() as f32;
         let h = emblem.height() as f32;
         let left = CELL_PADDING + w;
         let x = r.x() + r.width() - left;
         let y = r.y() + r.height() - CELL_PADDING - h;
-        cr.set_source_pixbuf(emblem, x.into(), y.into());
-        on_err_out!(cr.paint());
+        snapshot.save();
+        snapshot.translate(&graphene::Point::new(x, y));
+        emblem.snapshot(snapshot.upcast_ref::<gdk4::Snapshot>(), w as f64, h as f64);
+        snapshot.restore();
+
         left
     }
 
@@ -539,14 +556,14 @@ impl CellRendererImpl for LibraryCellRendererPriv {
         }
         if self.draw_flag.get() {
             match &*file {
-                Some(f) => Self::do_draw_flag(&cr, f.0.flag(), &r),
+                Some(f) => Self::do_draw_flag(snapshot, f.0.flag(), &r),
                 None => {}
             }
         }
 
         let status = self.status.get();
         if self.draw_status.get() && status != FileStatus::Ok {
-            Self::do_draw_status(&cr, status, &r);
+            Self::do_draw_status(snapshot, status, &r);
         }
 
         if self.draw_emblem.get() {
@@ -554,14 +571,14 @@ impl CellRendererImpl for LibraryCellRendererPriv {
                 Some(f) => f.0.file_type(),
                 None => FileType::Unknown,
             };
-            let emblem: Pixbuf = match file_type {
+            let emblem: Texture = match file_type {
                 FileType::Raw => EMBLEMS.raw.clone(),
                 FileType::RawJpeg => EMBLEMS.raw_jpeg.clone(),
                 FileType::Image => EMBLEMS.img.clone(),
                 FileType::Video => EMBLEMS.video.clone(),
                 FileType::Unknown => EMBLEMS.unknown.clone(),
             };
-            let left = Self::do_draw_format_emblem(&cr, &emblem, &r);
+            let left = Self::do_draw_format_emblem(snapshot, &emblem, &r);
 
             if self.draw_label.get() {
                 let label_id = match &*file {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]