50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
# Copyright 2019 (c) Anna Schumaker.
|
|
from .. import gtk
|
|
from gi.repository import GdkPixbuf, Gtk, GLib
|
|
|
|
Default = Gtk.IconTheme.get_default().load_icon("applications-multimedia-symbolic", 512, 0)
|
|
IdleCount = 0
|
|
Image = gtk.Builder.get_object("album_artwork")
|
|
Paned = gtk.Builder.get_object("sidebar_pane")
|
|
Pixbuf = Default
|
|
|
|
def reset():
|
|
global Pixbuf
|
|
Pixbuf = Default
|
|
set_scaled_image_idle()
|
|
|
|
def resize(*args):
|
|
if Pixbuf != None:
|
|
set_scaled_image()
|
|
Paned.connect_after("notify::position", resize)
|
|
|
|
def set_image(data):
|
|
global Pixbuf
|
|
|
|
loader = GdkPixbuf.PixbufLoader()
|
|
loader.write(data)
|
|
Pixbuf = loader.get_pixbuf()
|
|
loader.close()
|
|
set_scaled_image_idle()
|
|
|
|
def set_scaled_image():
|
|
pos = Paned.get_position()
|
|
new_h = pos * 0.9
|
|
new_w = min((Pixbuf.get_width() * new_h) / Pixbuf.get_height(), pos)
|
|
if new_h > 0:
|
|
pix = Pixbuf.scale_simple(new_w, new_h, GdkPixbuf.InterpType.BILINEAR)
|
|
Image.set_from_pixbuf(pix)
|
|
set_scaled_image()
|
|
|
|
def __set_scaled_image_idle():
|
|
global IdleCount
|
|
if (IdleCount := IdleCount - 1) == 0:
|
|
set_scaled_image()
|
|
return GLib.SOURCE_REMOVE
|
|
return GLib.SOURCE_CONTINUE
|
|
|
|
def set_scaled_image_idle():
|
|
global IdleCount
|
|
if (IdleCount := IdleCount + 1) == 1:
|
|
GLib.idle_add(__set_scaled_image_idle, priority=GLib.PRIORITY_LOW)
|