WIP GIF support

This commit is contained in:
itdominator 2023-04-27 17:57:23 -05:00
parent 0259727784
commit 599d1f5145
4 changed files with 32 additions and 28 deletions

View File

@ -7,6 +7,7 @@ gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0') gi.require_version('Gdk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import Gdk from gi.repository import Gdk
from gi.repository import GLib
# Application imports # Application imports
from ..widgets.image_view import ImageView from ..widgets.image_view import ImageView
@ -105,5 +106,6 @@ class ImageViewScroll(Gtk.ScrolledWindow):
event_system.emit("load_image_list", (path, img_list)) event_system.emit("load_image_list", (path, img_list))
@daemon_threaded
def _size_request_change(self, widget = None, eve = None): def _size_request_change(self, widget = None, eve = None):
event_system.emit("size_allocate") GLib.idle_add(event_system.emit, *("size_allocate",))

View File

@ -100,17 +100,13 @@ class ButtonControls(Gtk.ButtonBox):
event_system.emit("vertical_flip") event_system.emit("vertical_flip")
def _scale_1_two_1(self, widget = None, eve = None): def _scale_1_two_1(self, widget = None, eve = None):
if eve.button == 1:
self._unset_class(self.fit_button) self._unset_class(self.fit_button)
self._set_class(self.one2one_button) self._set_class(self.one2one_button)
event_system.emit("scale_1_two_1") event_system.emit("scale_1_two_1")
def _fit_to_container(self, widget = None, eve = None): def _fit_to_container(self, widget = None, eve = None):
if eve.button == 1:
self._unset_class(self.one2one_button) self._unset_class(self.one2one_button)
self._set_class(self.fit_button) self._set_class(self.fit_button)
event_system.emit("fit_to_container") event_system.emit("fit_to_container")
def _horizontal_flip(self, widget = None, eve = None): def _horizontal_flip(self, widget = None, eve = None):

View File

@ -66,6 +66,10 @@ class Image(Gtk.EventBox):
if PImage and path.endswith(".webp"): if PImage and path.endswith(".webp"):
return self.image2pixbuf(path, w, h) return self.image2pixbuf(path, w, h)
if path.endswith(".gif"):
pixbuf = GdkPixbuf.PixbufAnimation.new_from_file(path).get_static_image()
if not pixbuf:
try: try:
pixbuf = Gtk.Image.new_from_file(path).get_pixbuf() pixbuf = Gtk.Image.new_from_file(path).get_pixbuf()
except Exception: except Exception:

View File

@ -4,6 +4,7 @@
import gi import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0') gi.require_version('Gdk', '3.0')
gi.require_version('GdkPixbuf', '2.0')
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import Gdk from gi.repository import Gdk
from gi.repository import GdkPixbuf from gi.repository import GdkPixbuf
@ -65,13 +66,14 @@ class ImageView(Gtk.Image):
self.animation = None self.animation = None
if path.endswith(".gif"): if path.endswith(".gif"):
self.work_pixbuf = GdkPixbuf.PixbufAnimation.new_from_file(path).get_static_image()
try: try:
self.animation = Gtk.Image.new_from_file(path).get_animation() self.animation = Gtk.Image.new_from_file(path).get_animation()
except Exception: except Exception:
self.animation = Gtk.Image.new_from_resource(path).get_animation() self.animation = Gtk.Image.new_from_resource(path).get_animation()
return
if not self.work_pixbuf:
try: try:
self.work_pixbuf = Gtk.Image.new_from_file(path).get_pixbuf() self.work_pixbuf = Gtk.Image.new_from_file(path).get_pixbuf()
except Exception: except Exception:
@ -87,7 +89,7 @@ class ImageView(Gtk.Image):
def _zoom_out(self): def _zoom_out(self):
if self.work_pixbuf: if self.work_pixbuf and self.pixbuf:
# TODO: Setup scale factor setting to pull from settings... # TODO: Setup scale factor setting to pull from settings...
stepx = self.work_pixbuf.get_width() * 0.05 stepx = self.work_pixbuf.get_width() * 0.05
stepy = self.work_pixbuf.get_height() * 0.05 stepy = self.work_pixbuf.get_height() * 0.05
@ -99,26 +101,26 @@ class ImageView(Gtk.Image):
self.set_from_pixbuf(self.work_pixbuf) self.set_from_pixbuf(self.work_pixbuf)
def _rotate_left(self): def _rotate_left(self):
if self.work_pixbuf: if self.work_pixbuf and self.pixbuf:
self.work_pixbuf = self.work_pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE) self.work_pixbuf = self.work_pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
self.pixbuf = self.pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE) self.pixbuf = self.pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
self.set_from_pixbuf(self.work_pixbuf) self.set_from_pixbuf(self.work_pixbuf)
def _vertical_flip(self): def _vertical_flip(self):
if self.work_pixbuf: if self.work_pixbuf and self.pixbuf:
self.work_pixbuf = self.work_pixbuf.flip(True) self.work_pixbuf = self.work_pixbuf.flip(True)
self.pixbuf = self.pixbuf.flip(True) self.pixbuf = self.pixbuf.flip(True)
self.set_from_pixbuf(self.work_pixbuf) self.set_from_pixbuf(self.work_pixbuf)
def _scale_1_two_1(self): def _scale_1_two_1(self):
self.fit_to_win = False self.fit_to_win = False
if self.work_pixbuf: if self.work_pixbuf and self.pixbuf:
self.work_pixbuf = self.pixbuf self.work_pixbuf = self.pixbuf
self.set_from_pixbuf(self.work_pixbuf) self.set_from_pixbuf(self.work_pixbuf)
def _fit_to_container(self): def _fit_to_container(self):
self.fit_to_win = True self.fit_to_win = True
if self.work_pixbuf: if self.work_pixbuf and self.pixbuf:
parent_aloc = self.get_parent().get_parent().get_allocation() parent_aloc = self.get_parent().get_parent().get_allocation()
pw = parent_aloc.width pw = parent_aloc.width
ph = parent_aloc.height ph = parent_aloc.height
@ -142,19 +144,19 @@ class ImageView(Gtk.Image):
self.set_from_pixbuf(self.work_pixbuf) self.set_from_pixbuf(self.work_pixbuf)
def _horizontal_flip(self): def _horizontal_flip(self):
if self.work_pixbuf: if self.work_pixbuf and self.pixbuf:
self.work_pixbuf = self.work_pixbuf.flip(False) self.work_pixbuf = self.work_pixbuf.flip(False)
self.pixbuf = self.pixbuf.flip(False) self.pixbuf = self.pixbuf.flip(False)
self.set_from_pixbuf(self.work_pixbuf) self.set_from_pixbuf(self.work_pixbuf)
def _rotate_right(self): def _rotate_right(self):
if self.work_pixbuf: if self.work_pixbuf and self.pixbuf:
self.work_pixbuf = self.work_pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE) self.work_pixbuf = self.work_pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
self.pixbuf = self.pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE) self.pixbuf = self.pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
self.set_from_pixbuf(self.work_pixbuf) self.set_from_pixbuf(self.work_pixbuf)
def _zoom_in(self): def _zoom_in(self):
if self.work_pixbuf: if self.work_pixbuf and self.pixbuf:
# TODO: Setup scale factor setting to pull from settings... # TODO: Setup scale factor setting to pull from settings...
stepx = self.work_pixbuf.get_width() * 0.05 stepx = self.work_pixbuf.get_width() * 0.05
stepy = self.work_pixbuf.get_height() * 0.05 stepy = self.work_pixbuf.get_height() * 0.05