[Reader] Adjust image size when window is resized
This commit is contained in:
@@ -9,6 +9,9 @@ from gi.repository.GdkPixbuf import Pixbuf
|
||||
|
||||
|
||||
class Reader():
|
||||
pixbuf = None
|
||||
size = None
|
||||
|
||||
def __init__(self, window):
|
||||
self.window = window
|
||||
self.builder = window.builder
|
||||
@@ -17,6 +20,7 @@ class Reader():
|
||||
self.scrolledwindow = self.viewport.get_parent()
|
||||
self.image = Gtk.Image()
|
||||
|
||||
self.window.connect('check-resize', self.on_resize)
|
||||
self.scrolledwindow.connect('button-press-event', self.on_button_press)
|
||||
|
||||
def clear(self):
|
||||
@@ -28,14 +32,11 @@ class Reader():
|
||||
|
||||
chapter.manga.update(dict(last_read=datetime.datetime.now()))
|
||||
|
||||
self.render_page(self.chapter.last_page_read_index)
|
||||
self.render_page(self.chapter.last_page_read_index or 0)
|
||||
|
||||
def on_button_press(self, widget, event):
|
||||
if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 1:
|
||||
sw_width = self.scrolledwindow.get_allocated_width()
|
||||
|
||||
if event.x > sw_width / 2:
|
||||
if event.x > self.size.width / 2:
|
||||
index = self.page_index - 1
|
||||
else:
|
||||
index = self.page_index + 1
|
||||
@@ -46,40 +47,33 @@ class Reader():
|
||||
# TODO: next or prev chapter
|
||||
print('BEGIN or END')
|
||||
|
||||
def on_resize(self, window):
|
||||
size = self.viewport.get_allocated_size()[0]
|
||||
|
||||
if self.size and (size.width != self.size.width or size.height != self.size.height):
|
||||
self.size = size
|
||||
self.set_page_image_from_pixbuf()
|
||||
|
||||
def render_page(self, index):
|
||||
def get_page_image_path():
|
||||
self.chapter.update()
|
||||
page_path = self.chapter.get_page(self.page_index)
|
||||
|
||||
GLib.idle_add(set_page_image, page_path)
|
||||
GLib.idle_add(show_page_image, page_path)
|
||||
|
||||
def set_page_image(page_path):
|
||||
def show_page_image(page_path):
|
||||
if page_path is None:
|
||||
# TODO: Display not found page
|
||||
return False
|
||||
|
||||
pixbuf = Pixbuf.new_from_file(page_path)
|
||||
|
||||
sw_width = self.scrolledwindow.get_allocated_width()
|
||||
width = pixbuf.get_width()
|
||||
height = pixbuf.get_height()
|
||||
|
||||
pixbuf = pixbuf.scale_simple(
|
||||
sw_width,
|
||||
height / (width / sw_width),
|
||||
InterpType.BILINEAR
|
||||
)
|
||||
self.image.set_from_pixbuf(pixbuf)
|
||||
self.pixbuf = Pixbuf.new_from_file(page_path)
|
||||
self.size = self.viewport.get_allocated_size()[0]
|
||||
self.set_page_image_from_pixbuf()
|
||||
|
||||
self.clear()
|
||||
self.viewport.add(self.image)
|
||||
self.image.show()
|
||||
|
||||
# if width > sw_width:
|
||||
# vadj.set_value((width - sw_width) / 2)
|
||||
# if height > sw_height:
|
||||
# hadj.set_value((height - sw_height) / 2)
|
||||
|
||||
return False
|
||||
|
||||
self.page_index = index
|
||||
@@ -91,6 +85,23 @@ class Reader():
|
||||
thread.daemon = True
|
||||
thread.start()
|
||||
|
||||
def set_page_image_from_pixbuf(self):
|
||||
width = self.pixbuf.get_width()
|
||||
height = self.pixbuf.get_height()
|
||||
|
||||
# Adjust image on width
|
||||
pixbuf = self.pixbuf.scale_simple(
|
||||
self.size.width,
|
||||
height / (width / self.size.width),
|
||||
InterpType.BILINEAR
|
||||
)
|
||||
self.image.set_from_pixbuf(pixbuf)
|
||||
|
||||
# if width > self.size.width:
|
||||
# vadj.set_value((width - self.size.width) / 2)
|
||||
# if height > self.size.height:
|
||||
# hadj.set_value((height - self.size.height) / 2)
|
||||
|
||||
def show_spinner(self):
|
||||
self.clear()
|
||||
self.viewport.add(self.builder.get_object('spinner_box'))
|
||||
|
Reference in New Issue
Block a user