1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
|
"""This module defines a class to display widgets"""
from copy import copy, deepcopy
from logging import getLogger
from wcwidth import wcswidth
from asciimatics.effects import Effect
from asciimatics.event import KeyboardEvent, MouseEvent
from asciimatics.exceptions import Highlander, InvalidFields
from asciimatics.screen import Screen, Canvas
from asciimatics.utilities import BoxTool
from asciimatics.widgets.scrollbar import _ScrollBar
from asciimatics.widgets.utilities import THEMES
# Logging
logger = getLogger(__name__)
class _BorderManager:
def __init__(self, frame, has_border, can_scroll):
"""
Helper class to manage the border and scroll bar attached to a frame.
Allows for different character to be used in the border and the
changes in sizing requirements with borders on and off with scroll
bars on and off.
:param frame: frame being manager
:param has_border: True if the frame has a border
:param can_scroll: True if the frame has a scroll bar
"""
self._frame = frame
self.has_border = has_border
self.scroll_bar = None
self.box = BoxTool(frame.canvas.unicode_aware)
if can_scroll:
scroll_y = 2
scroll_height = frame.canvas.height - 4
if not has_border:
scroll_height = frame.canvas.height - 2
scroll_y = 1
self.scroll_bar = _ScrollBar(
frame.canvas, frame.palette, frame.canvas.width - 1, scroll_y, scroll_height,
frame.get_scroll_pos, frame.set_scroll_pos, absolute=True
)
# Optimization for non-unicode displays to avoid slow unicode calls.
self.string_len = wcswidth if frame._canvas.unicode_aware else len
@property
def can_scroll(self):
return self.scroll_bar is not None
def get_rectangle(self):
"""
Returns the bounding box defined by the usable space left after
borders and/or scroll bars are accounted for.
:returns: Tuple containing, x, y, height and width of bounding box
"""
if self.has_border:
x = 1
y = self._frame.canvas.start_line + 1
h = self._frame.canvas.height - 2
w = self._frame.canvas.width - 2
else:
x = 0
y = self._frame.canvas.start_line
h = self._frame.canvas.height
w = self._frame.canvas.width
if self.can_scroll:
w -= 1
return x, y, h, w
def draw(self):
"""
Draws the border and/or scroll bars onto the frame managed by this
object.
"""
frame = self._frame
if self.has_border:
# Draw the basic border first.
(colour, attr, bg) = frame.palette["borders"]
for dy in range(frame.canvas.height):
y = frame.canvas.start_line + dy
if dy == 0:
frame.canvas.print_at(
self.box.box_top(frame.canvas.width), 0, y, colour, attr, bg
)
elif dy == frame.canvas.height - 1:
frame.canvas.print_at(
self.box.box_bottom(frame.canvas.width), 0, y, colour, attr, bg
)
else:
frame.canvas.print_at(self.box.v, 0, y, colour, attr, bg)
frame.canvas.print_at(self.box.v, frame.canvas.width - 1, y, colour, attr, bg)
# Now the title
(colour, attr, bg) = frame.palette["title"]
title_width = self.string_len(frame.title)
frame.canvas.print_at(
frame.title, (frame.canvas.width - title_width) // 2, frame.canvas.start_line,
colour, attr, bg
)
if self.can_scroll and frame.canvas.height > 5:
self.scroll_bar.update()
class Frame(Effect):
"""
A Frame is a special Effect for controlling and displaying Widgets.
It is similar to a window as used in native GUI applications. Widgets are text UI elements
that can be used to create an interactive application within your Frame.
"""
#: Colour palette for the widgets within the Frame. Each entry should be
#: a 3-tuple of (foreground colour, attribute, background colour).
palette = {}
def __init__(self, screen, height, width, data=None, on_load=None,
has_border=True, hover_focus=False, name=None, title=None,
x=None, y=None, has_shadow=False, reduce_cpu=False, is_modal=False,
can_scroll=True):
"""
:param screen: The Screen that owns this Frame.
:param width: The desired width of the Frame.
:param height: The desired height of the Frame.
:param data: optional data dict to initialize any widgets in the frame.
:param on_load: optional function to call whenever the Frame reloads.
:param has_border: Whether the frame has a border box. Defaults to True.
:param hover_focus: Whether hovering a mouse over a widget (i.e. mouse move events)
should change the input focus. Defaults to false.
:param name: Optional name to identify this Frame. This is used to reset data as needed
from on old copy after the screen resizes.
:param title: Optional title to display if has_border is True.
:param x: Optional x position for the top left corner of the Frame.
:param y: Optional y position for the top left corner of the Frame.
:param has_shadow: Optional flag to indicate if this Frame should have a shadow when
drawn.
:param reduce_cpu: Whether to minimize CPU usage (for use on low spec systems).
:param is_modal: Whether this Frame is "modal" - i.e. will stop all other Effects from
receiving input events.
:param can_scroll: Whether a scrollbar should be available on the border, or not.
"""
super().__init__(screen)
self._focus = 0
self._max_height = 0
self._layouts = []
self._effects = []
self._canvas = Canvas(screen, height, width, x, y)
self._data = None
self._on_load = on_load
self._hover_focus = hover_focus
self._initial_data = data if data else {}
self._title = None
self.title = title # Use property to re-format text as required.
self._has_shadow = has_shadow
self._reduce_cpu = reduce_cpu
self._is_modal = is_modal
self._has_focus = False
self._border_mgr = _BorderManager(self, has_border, can_scroll)
# A unique name is needed for cloning. Try our best to get one!
self._name = title if name is None else name
# Flag to catch recursive calls inside the data setting. This is
# typically caused by callbacks subsequently trying to re-use functions.
self._in_call = False
# Now set up any passed data - use the public property to trigger any
# necessary updates.
self.data = deepcopy(self._initial_data)
# Ensure that we have the default palette in place
self._theme = None
self.set_theme("default")
def get_scroll_pos(self):
"""
Get current position for scroll bar.
"""
if self._canvas.height >= self._max_height:
return 0
return self._canvas.start_line / (self._max_height - self._canvas.height + 1)
def set_scroll_pos(self, pos):
"""
Set current position for scroll bar.
"""
if self._canvas.height < self._max_height:
pos *= self._max_height - self._canvas.height + 1
pos = int(round(max(0, pos), 0))
self._canvas.scroll_to(pos)
def add_layout(self, layout):
"""
Add a Layout to the Frame.
:param layout: The Layout to be added.
"""
layout.register_frame(self)
self._layouts.append(layout)
def add_effect(self, effect):
"""
Add an Effect to the Frame.
:param effect: The Effect to be added.
"""
effect.register_scene(self._scene)
self._effects.append(effect)
def fix(self):
"""
Fix the layouts and calculate the locations of all the widgets.
This function should be called once all Layouts have been added to the Frame and all
widgets added to the Layouts.
"""
# Remove any focus now before we reset...
if self._has_focus:
try:
self._layouts[self._focus].blur()
except IndexError:
pass
# Do up to 2 passes in case we have a variable height Layout.
fill_layout = None
fill_height = y = 0
for _ in range(2):
# Pick starting point/height - varies for borders.
x, y, height, width = self._border_mgr.get_rectangle()
start_y = y
# Process each Layout in the Frame - getting required height for
# each.
for layout in self._layouts:
if layout.fill_frame:
if fill_layout is None:
# First pass - remember it for now.
fill_layout = layout
elif fill_layout == layout:
# Second pass - pass in max height
y = layout.fix(x, y, width, fill_height)
else:
# A second filler - this is a bug in the application.
raise Highlander("Too many Layouts filling Frame")
else:
y = layout.fix(x, y, width, height)
# Can stop now if no fill required.
if fill_layout is None:
break
# We hit a variable height Layout - figure out the available space and reset everything
# to the new values.
fill_height = max(1, start_y + height - y)
# Remember the resulting height of the underlying Layouts.
self._max_height = y
# Reset text
if self._has_focus:
while self._focus < len(self._layouts):
try:
self._layouts[self._focus].focus(force_first=True)
break
except IndexError:
self._focus += 1
self._clear()
def _clear(self):
"""
Clear the current canvas.
"""
# It's orders of magnitude faster to reset with a print like this
# instead of recreating the screen buffers.
(colour, attr, bg) = self.palette["background"]
self._canvas.clear_buffer(colour, attr, bg)
def _update(self, frame_no):
# TODO: Should really be in a separate Desktop Manager class - wait for v2.0
if self.scene and self.scene.effects[-1] != self:
if self._focus < len(self._layouts):
self._layouts[self._focus].blur()
self._has_focus = False
# Reset the canvas to prepare for next round of updates.
self._clear()
# Update all the widgets first.
for layout in self._layouts:
layout.update(frame_no)
# Then update any effects as needed.
for effect in self._effects:
effect.update(frame_no)
# Draw any border if needed.
self._border_mgr.draw()
# Now push it all to screen.
self._canvas.refresh()
# And finally - draw the shadow
if self._has_shadow:
(colour, _, bg) = self.palette["shadow"]
self._screen.highlight(
self._canvas.origin[0] + 1,
self._canvas.origin[1] + self._canvas.height,
self._canvas.width - 1,
1,
fg=colour, bg=bg, blend=50)
self._screen.highlight(
self._canvas.origin[0] + self._canvas.width,
self._canvas.origin[1] + 1,
1,
self._canvas.height,
fg=colour, bg=bg, blend=50)
def set_theme(self, theme):
"""
Pick a palette from the list of supported THEMES.
:param theme: The name of the theme to set.
"""
if theme in THEMES:
self._theme = theme
self.palette = THEMES[theme]
if self._border_mgr.can_scroll:
self._border_mgr.scroll_bar.palette = self.palette
@property
def title(self):
"""
Title for this Frame.
"""
return self._title
@title.setter
def title(self, new_value):
self._title = " " + new_value[0:self._canvas.width - 4] + " " if new_value else ""
@property
def data(self):
"""
Data dictionary containing values from the contained widgets.
"""
return self._data
@data.setter
def data(self, new_value):
# Don't allow this function to recurse.
if self._in_call:
return
self._in_call = True
# Do a key-by-key copy to allow for dictionary-like objects - e.g.
# sqlite3 Row class.
self._data = {}
if new_value is not None:
for key in list(new_value.keys()):
self._data[key] = new_value[key]
# Now update any widgets as needed.
for layout in self._layouts:
layout.update_widgets()
# All done - clear the recursion flag.
self._in_call = False
@property
def stop_frame(self):
# Widgets have no defined end - always return -1.
return -1
@property
def safe_to_default_unhandled_input(self):
# It is NOT safe to use the unhandled input handler on Frames as the
# default on space and enter is to go to the next Scene.
return False
@property
def canvas(self):
"""
The Canvas that backs this Frame.
"""
return self._canvas
@property
def focussed_widget(self):
"""
The widget that currently has the focus within this Frame.
"""
# If the frame has no focus, it can't have a focussed widget.
if not self._has_focus:
return None
try:
layout = self._layouts[self._focus]
return layout.get_current_widget()
except IndexError:
# If the current indexing is invalid it's because no widget is selected.
return None
@property
def frame_update_count(self):
"""
The number of frames before this Effect should be updated.
"""
result = 1000000
for layout in self._layouts:
if layout.frame_update_count > 0:
result = min(result, layout.frame_update_count)
for effect in self._effects:
if effect.frame_update_count > 0:
result = min(result, effect.frame_update_count)
return result
@property
def reduce_cpu(self):
"""
Whether this Frame should try to optimize refreshes to reduce CPU.
"""
return self._reduce_cpu
@property
def border_box(self):
"""
Instance of :class:`~asciimatics.utilities.BoxTool` that specifies the characters used to
draw the border to this frame. You can change the border character style by modifying the
:attr:`style` property on this object. Allowed styles are defined in
:mod:`~asciimatics.constants`.
"""
return self._border_mgr.box
def find_widget(self, name):
"""
Look for a widget with a specified name.
:param name: The name to search for.
:returns: The widget that matches or None if one couldn't be found.
"""
result = None
for layout in self._layouts:
result = layout.find_widget(name)
if result:
break
return result
def clone(self, _, scene):
"""
Create a clone of this Frame into a new Screen.
:param _: ignored.
:param scene: The new Scene object to clone into.
"""
# Assume that the application creates a new set of Frames and so we need to match up the
# data from the old object to the new (using the name).
if self._name is not None:
for effect in scene.effects:
if isinstance(effect, Frame):
# Using protected access to our own class type on purpose.
# pylint: disable=protected-access
logger.debug("Cloning: %s", effect._name)
if effect._name == self._name:
effect.set_theme(self._theme)
effect.data = self.data
for layout in self._layouts:
layout.update_widgets(new_frame=effect)
def reset(self):
# Reset form to default state.
self.data = deepcopy(self._initial_data)
# Now reset the individual widgets.
self._canvas.reset()
for layout in self._layouts:
layout.reset()
layout.blur()
# Then reset any effects as needed.
for effect in self._effects:
effect.reset()
# Set up active widget.
self._focus = 0
while self._focus < len(self._layouts):
try:
self._layouts[self._focus].focus(force_first=True)
break
except IndexError:
self._focus += 1
# Call the on_load function now if specified.
if self._on_load is not None:
self._on_load()
def save(self, validate=False):
"""
Save the current values in all the widgets back to the persistent data storage.
:param validate: Whether to validate the data before saving.
Calling this while setting the `data` field (e.g. in a widget callback) will have no
effect.
When validating data, it can throw an Exception for any
"""
# Don't allow this function to be called if we are already updating the
# data for the form.
if self._in_call:
return
# We're clear - pass on to all layouts/widgets.
invalid = []
for layout in self._layouts:
try:
layout.save(validate=validate)
except InvalidFields as exc:
invalid.extend(exc.fields)
# Check for any bad data and raise exception if needed.
if len(invalid) > 0:
raise InvalidFields(invalid)
def blur(self):
"""
Blur this Frame.
"""
# If the frame has no focus, it's a NOOP.
if not self._has_focus:
return
self._layouts[self._focus].blur()
self._has_focus = False
logger.debug("Blurred frame: %s", self)
def switch_focus(self, layout, column, widget):
"""
Switch focus to the specified widget.
:param layout: The layout that owns the widget.
:param column: The column the widget is in.
:param widget: The index of the widget to take the focus.
"""
# Find the layout to own the focus.
for i, l in enumerate(self._layouts):
if l is layout:
break
else:
# No matching layout - give up now
return
self._layouts[self._focus].blur()
self._focus = i
self._layouts[self._focus].focus(force_column=column,
force_widget=widget)
def move_to(self, x, y, h):
"""
Make the specified location visible. This is typically used by a widget to scroll the
canvas such that it is visible.
:param x: The x location to make visible.
:param y: The y location to make visible.
:param h: The height of the location to make visible.
"""
start_x, start_y, height, width = self._border_mgr.get_rectangle()
if ((start_x <= x < start_x + width) and (y >= start_y) and (y + h < start_y + height)):
# Already OK - quit now.
return
if y < start_y:
self._canvas.scroll_to(y - 1 if self._border_mgr.has_border else y)
else:
line = y + h - self._canvas.height + (1 if self._border_mgr.has_border else 0)
self._canvas.scroll_to(max(0, line))
def rebase_event(self, event):
"""
Rebase the coordinates of the passed event to frame-relative coordinates.
:param event: The event to be rebased.
:returns: A new event object appropriately re-based.
"""
new_event = copy(event)
if isinstance(new_event, MouseEvent):
origin = self._canvas.origin
new_event.x -= origin[0]
new_event.y -= origin[1] - self._canvas.start_line
logger.debug("New event: %s", new_event)
return new_event
def _outside_frame(self, event):
origin = self._canvas.origin
if (event.y < origin[1] or event.y >= origin[1] + self._canvas.height or
event.x < origin[0] or event.x >= origin[0] + self._canvas.width):
return True
return False
def _find_next_tab_stop(self, direction):
old_focus = self._focus
self._focus += direction
while self._focus != old_focus:
if self._focus < 0:
self._focus = len(self._layouts) - 1
if self._focus >= len(self._layouts):
self._focus = 0
logger.debug("Trying tab to layout %s", self._focus)
try:
if direction > 0:
self._layouts[self._focus].focus(force_first=True)
else:
self._layouts[self._focus].focus(force_last=True)
return
except IndexError:
self._focus += direction
# If we get here, we need to reset the layout focus
if direction > 0:
self._layouts[self._focus].focus(force_first=True)
else:
self._layouts[self._focus].focus(force_last=True)
def _switch_to_nearest_vertical_widget(self, direction):
"""
Find the nearest widget above or below the current widget with the focus.
This should only be called by the Frame when normal Layout navigation fails and so this needs to find
the nearest widget in the next available Layout. It will not search the existing Layout for a closer
match.
:param direction: The direction to move through the Layouts.
"""
# If no current widget, just find next one
current_widget = self._layouts[self._focus].get_current_widget()
if current_widget is None:
self._find_next_tab_stop(direction)
return
# Otherwise, find the nearest option
focus = self._focus
focus += direction
while self._focus != focus:
if focus < 0:
focus = len(self._layouts) - 1
if focus >= len(self._layouts):
focus = 0
match = self._layouts[focus].get_nearest_widget(current_widget, direction)
if match:
self.switch_focus(self._layouts[focus], match[1], match[2])
return
focus += direction
def _handle_desktop_ordering(self, event):
claimed_focus = False
if isinstance(event, MouseEvent) and event.buttons > 0:
# TODO: Should have Desktop Manager handling this - wait for v2.0
# Claim focus if mouse click is inside the Frame.
if (0 <= event.x < self._canvas.width and
self._canvas.start_line <= event.y < self._canvas.start_line + self._canvas.height):
self._scene.remove_effect(self)
self._scene.add_effect(self, reset=False)
if not self._has_focus and self._focus < len(self._layouts):
self._layouts[self._focus].focus()
self._has_focus = claimed_focus = True
logger.debug("Blurring: %s", self._scene.effects)
for effect in self._scene.effects:
if effect is not self:
try:
effect.blur()
except AttributeError:
pass
else:
return claimed_focus, True
elif isinstance(event, KeyboardEvent):
# TODO: Should have Desktop Manager handling this - wait for v2.0
# By this stage, if we're processing keys and topmost, we have the focus.
if self._scene.effects[-1] is not self:
return claimed_focus, True
if not self._has_focus and self._focus < len(self._layouts):
self._layouts[self._focus].focus()
self._has_focus = True
return claimed_focus, False
def process_event(self, event):
# Rebase any mouse events into Frame coordinates now.
old_event = event
event = self.rebase_event(event)
# Should we change z-order or quit now?
claimed_focus, quit_now = self._handle_desktop_ordering(event)
if quit_now:
return old_event
# No need to do anything if this Frame has no Layouts - and hence no
# widgets. Swallow all Keyboard events while we have focus.
#
# Also don't bother trying to process widgets if there is no defined
# focus. This means there is no enabled widget in the Frame.
if (self._focus < 0 or self._focus >= len(self._layouts) or
not self._layouts):
if event is not None and isinstance(event, KeyboardEvent):
return None
else:
# Don't allow events to bubble down if this window owns the Screen - as already
# calculated when taking the focus - or is modal.
return None if claimed_focus or self._is_modal else old_event
# Give the current widget in focus first chance to process the event.
event = self._layouts[self._focus].process_event(event, self._hover_focus)
logger.debug("Current widget left event: %s", event)
# If the underlying widgets did not process the event, try processing
# it now.
if event is not None:
if isinstance(event, KeyboardEvent):
if event.key_code == Screen.KEY_TAB:
# Move on to next widget.
self._layouts[self._focus].blur()
self._find_next_tab_stop(1)
# Find next tab stop will have already set the new focus.
old_event = None
elif event.key_code == Screen.KEY_BACK_TAB:
# Move on to previous widget.
self._layouts[self._focus].blur()
self._find_next_tab_stop(-1)
# Find next tab stop will have already set the new focus.
old_event = None
if event.key_code == Screen.KEY_DOWN:
# Move on to nearest vertical widget in the next Layout
self._switch_to_nearest_vertical_widget(1)
old_event = None
elif event.key_code == Screen.KEY_UP:
# Move on to nearest vertical widget in the next Layout
self._switch_to_nearest_vertical_widget(-1)
old_event = None
elif isinstance(event, MouseEvent):
# Give layouts/widgets first dibs on the mouse message.
for layout in self._layouts:
if layout.process_event(event, self._hover_focus) is None:
return None
# If no joy, check whether the scroll bar was clicked.
if self._border_mgr.can_scroll:
if self._border_mgr.scroll_bar.process_event(event):
return None
# Don't allow events to bubble down if this window owns the Screen (as already
# calculated when taking the focus) or if the Frame is modal or we handled the
# event.
return None if claimed_focus or self._is_modal or event is None else old_event
|