windows.image: Type everything in this module.
This commit is contained in:
parent
400a8e01a3
commit
45066d0eef
1 changed files with 15 additions and 9 deletions
|
@ -16,27 +16,29 @@ from poezio.theming import get_theme, to_curses_attr
|
||||||
from poezio.xhtml import _parse_css_color
|
from poezio.xhtml import _parse_css_color
|
||||||
from poezio.config import config
|
from poezio.config import config
|
||||||
|
|
||||||
|
from typing import Tuple, Optional, Callable
|
||||||
|
|
||||||
|
|
||||||
class ImageWin(Win):
|
class ImageWin(Win):
|
||||||
"""
|
"""
|
||||||
A window which contains either an image or a border.
|
A window which contains either an image or a border.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self._image = None
|
self._image = None # type: Optional[Image]
|
||||||
Win.__init__(self)
|
Win.__init__(self)
|
||||||
if config.get('image_use_half_blocks'):
|
if config.get('image_use_half_blocks'):
|
||||||
self._display_avatar = self._display_avatar_half_blocks
|
self._display_avatar = self._display_avatar_half_blocks # type: Callable[[int, int], None]
|
||||||
else:
|
else:
|
||||||
self._display_avatar = self._display_avatar_full_blocks
|
self._display_avatar = self._display_avatar_full_blocks
|
||||||
|
|
||||||
def resize(self, height: int, width: int, y: int, x: int):
|
def resize(self, height: int, width: int, y: int, x: int) -> None:
|
||||||
self._resize(height, width, y, x)
|
self._resize(height, width, y, x)
|
||||||
if self._image is None:
|
if self._image is None:
|
||||||
return
|
return
|
||||||
self._display_avatar(width, height)
|
self._display_avatar(width, height)
|
||||||
|
|
||||||
def refresh(self, data):
|
def refresh(self, data: Optional[bytes]) -> None:
|
||||||
self._win.erase()
|
self._win.erase()
|
||||||
if data is not None and HAS_PIL:
|
if data is not None and HAS_PIL:
|
||||||
image_file = BytesIO(data)
|
image_file = BytesIO(data)
|
||||||
|
@ -51,7 +53,7 @@ class ImageWin(Win):
|
||||||
self._display_border()
|
self._display_border()
|
||||||
self._refresh()
|
self._refresh()
|
||||||
|
|
||||||
def _display_border(self):
|
def _display_border(self) -> None:
|
||||||
self._image = None
|
self._image = None
|
||||||
attribute = to_curses_attr(get_theme().COLOR_VERTICAL_SEPARATOR)
|
attribute = to_curses_attr(get_theme().COLOR_VERTICAL_SEPARATOR)
|
||||||
self._win.attron(attribute)
|
self._win.attron(attribute)
|
||||||
|
@ -62,7 +64,7 @@ class ImageWin(Win):
|
||||||
self._win.attroff(attribute)
|
self._win.attroff(attribute)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _compute_size(image_size, width: int, height: int):
|
def _compute_size(image_size: Tuple[int, int], width: int, height: int) -> Tuple[int, int]:
|
||||||
height *= 2
|
height *= 2
|
||||||
src_width, src_height = image_size
|
src_width, src_height = image_size
|
||||||
ratio = src_width / src_height
|
ratio = src_width / src_height
|
||||||
|
@ -74,7 +76,9 @@ class ImageWin(Win):
|
||||||
width = int(new_width)
|
width = int(new_width)
|
||||||
return width, height
|
return width, height
|
||||||
|
|
||||||
def _display_avatar_half_blocks(self, width: int, height: int):
|
def _display_avatar_half_blocks(self, width: int, height: int) -> None:
|
||||||
|
if self._image is None:
|
||||||
|
return
|
||||||
original_height = height
|
original_height = height
|
||||||
original_width = width
|
original_width = width
|
||||||
size = self._compute_size(self._image.size, width, height)
|
size = self._compute_size(self._image.size, width, height)
|
||||||
|
@ -95,7 +99,9 @@ class ImageWin(Win):
|
||||||
bot_color = _parse_css_color('#%02x%02x%02x' % (r, g, b))
|
bot_color = _parse_css_color('#%02x%02x%02x' % (r, g, b))
|
||||||
self.addstr('▄', to_curses_attr((bot_color, top_color)))
|
self.addstr('▄', to_curses_attr((bot_color, top_color)))
|
||||||
|
|
||||||
def _display_avatar_full_blocks(self, width: int, height: int):
|
def _display_avatar_full_blocks(self, width: int, height: int) -> None:
|
||||||
|
if self._image is None:
|
||||||
|
return
|
||||||
original_height = height
|
original_height = height
|
||||||
original_width = width
|
original_width = width
|
||||||
width, height = self._compute_size(self._image.size, width, height)
|
width, height = self._compute_size(self._image.size, width, height)
|
||||||
|
|
Loading…
Reference in a new issue