Don't refresh pads with invalid coordinates. The window should be fully resizable, closes #20
This commit is contained in:
@ -50,7 +50,8 @@ class Display:
|
||||
self.y = y
|
||||
self.width = width
|
||||
self.height = height
|
||||
if hasattr(self, "pad") and resize_pad:
|
||||
if hasattr(self, "pad") and resize_pad and \
|
||||
self.height >= 0 and self.width >= 0:
|
||||
self.pad.resize(self.height + 1, self.width + 1)
|
||||
|
||||
def refresh(self, *args, resize_pad: bool = True) -> None:
|
||||
@ -58,6 +59,26 @@ class Display:
|
||||
self.resize(*args, resize_pad)
|
||||
self.display()
|
||||
|
||||
def refresh_pad(self, pad: Any, top_y: int, top_x: int,
|
||||
window_y: int, window_x: int,
|
||||
last_y: int, last_x: int) -> None:
|
||||
"""
|
||||
Refresh a pad on a part of the window.
|
||||
The refresh starts at coordinates (top_y, top_x) from the pad,
|
||||
and is drawn from (window_y, window_x) to (last_y, last_x).
|
||||
If coordinates are invalid (negative indexes/length..., then nothing
|
||||
is drawn and no error is raised.
|
||||
"""
|
||||
top_y, top_x = max(0, top_y), max(0, top_x)
|
||||
window_y, window_x = max(0, window_y), max(0, window_x)
|
||||
screen_max_y, screen_max_x = self.screen.getmaxyx()
|
||||
last_y, last_x = min(screen_max_y - 1, last_y), \
|
||||
min(screen_max_x - 1, last_x)
|
||||
|
||||
if last_y >= window_y and last_x >= window_x:
|
||||
# Refresh the pad only if coordinates are valid
|
||||
pad.refresh(top_y, top_x, window_y, window_x, last_y, last_x)
|
||||
|
||||
def display(self) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
@ -87,7 +108,8 @@ class VerticalSplit(Display):
|
||||
def display(self) -> None:
|
||||
for i in range(self.height):
|
||||
self.addstr(self.pad, i, 0, "┃")
|
||||
self.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1, self.x)
|
||||
self.refresh_pad(self.pad, 0, 0, self.y, self.x,
|
||||
self.y + self.height - 1, self.x)
|
||||
|
||||
|
||||
class HorizontalSplit(Display):
|
||||
@ -107,7 +129,8 @@ class HorizontalSplit(Display):
|
||||
def display(self) -> None:
|
||||
for i in range(self.width):
|
||||
self.addstr(self.pad, 0, i, "━")
|
||||
self.pad.refresh(0, 0, self.y, self.x, self.y, self.x + self.width - 1)
|
||||
self.refresh_pad(self.pad, 0, 0, self.y, self.x, self.y,
|
||||
self.x + self.width - 1)
|
||||
|
||||
|
||||
class Box(Display):
|
||||
@ -123,5 +146,5 @@ class Box(Display):
|
||||
self.addstr(self.pad, i, self.width - 1, "┃")
|
||||
self.addstr(self.pad, self.height - 1, 0,
|
||||
"┗" + "━" * (self.width - 2) + "┛")
|
||||
self.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1,
|
||||
self.x + self.width - 1)
|
||||
self.refresh_pad(self.pad, 0, 0, self.y, self.x,
|
||||
self.y + self.height - 1, self.x + self.width - 1)
|
||||
|
Reference in New Issue
Block a user