Added PIL support for images like webp; changed emit name

This commit is contained in:
2022-09-30 23:30:38 -05:00
parent bdd532060a
commit d3e42b3ae0
12 changed files with 38 additions and 16 deletions

View File

@@ -36,11 +36,11 @@ class Controller(UIMixin, KeyboardSignalsMixin, IPCSignalsMixin, ExceptionHookMi
for arg in unknownargs:
if os.path.isdir(arg):
message = f"FILE|{arg}"
event_system.post_event("post_file_to_ipc", message)
event_system.emit("post_file_to_ipc", message)
if args.new_tab and os.path.isdir(args.new_tab):
message = f"FILE|{args.new_tab}"
event_system.post_event("post_file_to_ipc", message)
event_system.emit("post_file_to_ipc", message)
def _subscribe_to_events(self):

View File

@@ -154,7 +154,7 @@ class Controller_Data:
# if self.to_cut_files:
# state.to_cut_files = self.format_to_uris(state.store, state.wid, state.tid, self.to_cut_files, True)
event_system.post_event("update_state_info_plugins", state)
event_system.emit("update_state_info_plugins", state)
return state

View File

@@ -2,10 +2,17 @@
import os, subprocess, threading, hashlib
from os.path import isfile
# Gtk imports
# Lib imports
import gi
gi.require_version('GdkPixbuf', '2.0')
from gi.repository import GdkPixbuf
from gi.repository import GdkPixbuf, GLib
try:
from PIL import Image as PImage
except Exception as e:
PImage = None
# Application imports
from .mixins.desktopiconmixin import DesktopIconMixin
@@ -67,12 +74,27 @@ class Icon(DesktopIconMixin, VideoIconMixin):
.get_static_image() \
.scale_simple(wxh[0], wxh[1], GdkPixbuf.InterpType.BILINEAR)
else:
return GdkPixbuf.Pixbuf.new_from_file_at_scale(path, wxh[0], wxh[1], True)
if PImage and path.lower().endswith(".webp"):
return self.image2pixbuf(path, wxh)
else:
return GdkPixbuf.Pixbuf.new_from_file_at_scale(path, wxh[0], wxh[1], True)
except Exception as e:
print("Image Scaling Issue:")
print( repr(e) )
return None
def image2pixbuf(self, path, wxh):
"""Convert Pillow image to GdkPixbuf"""
im = PImage.open(path)
data = im.tobytes()
data = GLib.Bytes.new(data)
w, h = im.size
pixbuf = GdkPixbuf.Pixbuf.new_from_bytes(data, GdkPixbuf.Colorspace.RGB,
False, 8, w, h, w * 3)
return pixbuf.scale_simple(wxh[0], wxh[1], 2) # BILINEAR = 2
def create_from_file(self, path):
try:
return GdkPixbuf.Pixbuf.new_from_file(path)

View File

@@ -18,7 +18,7 @@ class EventSystem:
def subscribe(self, event_type, fn):
self.subscribers[event_type].append(fn)
def post_event(self, event_type, data):
def emit(self, event_type, data = None):
if event_type in self.subscribers:
for fn in self.subscribers[event_type]:
if data:

View File

@@ -65,7 +65,7 @@ class IPCServer:
if "FILE|" in msg:
file = msg.split("FILE|")[1].strip()
if file:
event_system.post_event("handle_file_from_ipc", file)
event_system.emit("handle_file_from_ipc", file)
conn.close()
break