修复停止播放时获取音量为空导致程序出错的bug; 封面图按1:1裁剪
parent
334fe09ffc
commit
ed7eeb97ae
42
ui/image.py
42
ui/image.py
|
@ -13,7 +13,7 @@ class ScaleImage(Gtk.Image):
|
||||||
self.reset(filepath)
|
self.reset(filepath)
|
||||||
|
|
||||||
|
|
||||||
def reset(self, filepath):
|
def reset(self, filepath, crop = False):
|
||||||
if type(filepath) == str:
|
if type(filepath) == str:
|
||||||
self.origin = GdkPixbuf.Pixbuf.new_from_file(filepath)
|
self.origin = GdkPixbuf.Pixbuf.new_from_file(filepath)
|
||||||
else:
|
else:
|
||||||
|
@ -24,11 +24,17 @@ class ScaleImage(Gtk.Image):
|
||||||
|
|
||||||
self.width = self.origin.get_width()
|
self.width = self.origin.get_width()
|
||||||
self.height = self.origin.get_height()
|
self.height = self.origin.get_height()
|
||||||
|
self.set_from_pixbuf(self.pixbuf)
|
||||||
else:
|
else:
|
||||||
self.pixbuf = self.origin.scale_simple(self.width, self.height, GdkPixbuf.InterpType.BILINEAR)
|
w = self.origin.get_width()
|
||||||
|
h = self.origin.get_height()
|
||||||
|
if crop and w != h:
|
||||||
|
size = min(self.width, self.height)
|
||||||
|
self.clip_resize(size)
|
||||||
|
else:
|
||||||
|
self.pixbuf = self.origin.scale_simple(self.width, self.height, GdkPixbuf.InterpType.BILINEAR)
|
||||||
|
|
||||||
self.set_from_pixbuf(self.pixbuf)
|
return self
|
||||||
return self
|
|
||||||
|
|
||||||
|
|
||||||
def resize(self, width, height):
|
def resize(self, width, height):
|
||||||
|
@ -38,6 +44,34 @@ class ScaleImage(Gtk.Image):
|
||||||
self.set_from_pixbuf(self.pixbuf)
|
self.set_from_pixbuf(self.pixbuf)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def clip_resize(self, size):
|
||||||
|
w = self.origin.get_width()
|
||||||
|
h = self.origin.get_height()
|
||||||
|
_size = min(w, h) # 按小边裁剪成正方形
|
||||||
|
size = min(size, _size) # 缩放按最小值来
|
||||||
|
self.width = size
|
||||||
|
self.height = size
|
||||||
|
|
||||||
|
surface = cairo.ImageSurface(cairo.Format.ARGB32, w, h)
|
||||||
|
ctx = cairo.Context(surface)
|
||||||
|
Gdk.cairo_set_source_pixbuf(ctx, self.origin, 0, 0)
|
||||||
|
|
||||||
|
ctx.line_to(0, 0)
|
||||||
|
ctx.line_to(_size, 0)
|
||||||
|
ctx.line_to(_size, _size)
|
||||||
|
ctx.line_to(0, _size)
|
||||||
|
|
||||||
|
ctx.close_path()
|
||||||
|
|
||||||
|
ctx.clip()
|
||||||
|
ctx.paint()
|
||||||
|
|
||||||
|
pixbuf = Gdk.pixbuf_get_from_surface(surface, 0, 0, _size, _size)
|
||||||
|
|
||||||
|
self.pixbuf = pixbuf.scale_simple(size, size, GdkPixbuf.InterpType.BILINEAR)
|
||||||
|
self.set_from_pixbuf(self.pixbuf)
|
||||||
|
|
||||||
|
return self
|
||||||
|
|
||||||
def set_radius(self, radius = 0):
|
def set_radius(self, radius = 0):
|
||||||
w = self.width
|
w = self.width
|
||||||
|
|
|
@ -72,7 +72,7 @@ class SonistWindow(Gtk.Window):
|
||||||
album = ScaleImage(album_img)
|
album = ScaleImage(album_img)
|
||||||
|
|
||||||
disk.resize(192, 192)
|
disk.resize(192, 192)
|
||||||
album.resize(128, 128).set_radius(64)
|
album.clip_resize(128).set_radius(64)
|
||||||
|
|
||||||
handler.resize(48, 96)
|
handler.resize(48, 96)
|
||||||
self.handler = handler
|
self.handler = handler
|
||||||
|
@ -279,7 +279,7 @@ class SonistWindow(Gtk.Window):
|
||||||
|
|
||||||
if first:
|
if first:
|
||||||
self.update_play_stat(played == 'play')
|
self.update_play_stat(played == 'play')
|
||||||
self.update_volume(int(self.stat.get('volume')))
|
self.update_volume(int(self.stat.get('volume') or 100))
|
||||||
|
|
||||||
if self.stat.get('single') == '1':
|
if self.stat.get('single') == '1':
|
||||||
self.ctrl_box.toggle_mode_btn(mode = 'single')
|
self.ctrl_box.toggle_mode_btn(mode = 'single')
|
||||||
|
@ -332,6 +332,6 @@ class SonistWindow(Gtk.Window):
|
||||||
@idle
|
@idle
|
||||||
def update_album(self, filepath = './usr/share/sonist/avatar.jpg'):
|
def update_album(self, filepath = './usr/share/sonist/avatar.jpg'):
|
||||||
self.set_background_image(filepath)
|
self.set_background_image(filepath)
|
||||||
self.album.reset(filepath).set_radius(64)
|
self.album.reset(filepath, True).set_radius(64)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue