[Reader] Adjust image size when window is resized

This commit is contained in:
Valery Febvre
2019-03-30 01:51:57 +01:00
parent df7c65ca76
commit 85e093c3d3

View File

@@ -9,6 +9,9 @@ from gi.repository.GdkPixbuf import Pixbuf
class Reader(): class Reader():
pixbuf = None
size = None
def __init__(self, window): def __init__(self, window):
self.window = window self.window = window
self.builder = window.builder self.builder = window.builder
@@ -17,6 +20,7 @@ class Reader():
self.scrolledwindow = self.viewport.get_parent() self.scrolledwindow = self.viewport.get_parent()
self.image = Gtk.Image() self.image = Gtk.Image()
self.window.connect('check-resize', self.on_resize)
self.scrolledwindow.connect('button-press-event', self.on_button_press) self.scrolledwindow.connect('button-press-event', self.on_button_press)
def clear(self): def clear(self):
@@ -28,14 +32,11 @@ class Reader():
chapter.manga.update(dict(last_read=datetime.datetime.now())) 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) self.render_page(self.chapter.last_page_read_index or 0)
def on_button_press(self, widget, event): def on_button_press(self, widget, event):
if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 1: if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 1:
sw_width = self.scrolledwindow.get_allocated_width() if event.x > self.size.width / 2:
if event.x > sw_width / 2:
index = self.page_index - 1 index = self.page_index - 1
else: else:
index = self.page_index + 1 index = self.page_index + 1
@@ -46,40 +47,33 @@ class Reader():
# TODO: next or prev chapter # TODO: next or prev chapter
print('BEGIN or END') 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 render_page(self, index):
def get_page_image_path(): def get_page_image_path():
self.chapter.update() self.chapter.update()
page_path = self.chapter.get_page(self.page_index) 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: if page_path is None:
# TODO: Display not found page # TODO: Display not found page
return False return False
pixbuf = Pixbuf.new_from_file(page_path) self.pixbuf = Pixbuf.new_from_file(page_path)
self.size = self.viewport.get_allocated_size()[0]
sw_width = self.scrolledwindow.get_allocated_width() self.set_page_image_from_pixbuf()
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.clear() self.clear()
self.viewport.add(self.image) self.viewport.add(self.image)
self.image.show() 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 return False
self.page_index = index self.page_index = index
@@ -91,6 +85,23 @@ class Reader():
thread.daemon = True thread.daemon = True
thread.start() 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): def show_spinner(self):
self.clear() self.clear()
self.viewport.add(self.builder.get_object('spinner_box')) self.viewport.add(self.builder.get_object('spinner_box'))