Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions mpfmc/uix/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List, Union, Optional

from kivy.uix.floatlayout import FloatLayout
from kivy.graphics.instructions import Callback

from kivy.clock import Clock
from kivy.uix.screenmanager import (ScreenManager, NoTransition,
Expand All @@ -18,7 +19,6 @@
from mpfmc.uix.widget import WidgetContainer, Widget
from mpfmc.uix.slide import Slide


MYPY = False
if MYPY: # pragma: no cover
from mpfmc.core.mc import MpfMc
Expand Down Expand Up @@ -588,7 +588,6 @@ def _post_active_slide_event(self, dt) -> None:


class DisplayOutput(Scatter):

"""Show a display as a widget."""

def __init__(self, parent: "KivyWidget", display: "Display", **kwargs):
Expand Down Expand Up @@ -627,14 +626,24 @@ def add_display_source(self, widget):
"""
if not isinstance(widget, Display):
raise KivyWidgetException(
'add_widget_multi_parent() can be used only with instances'
'add_display_source() can be used only with instances'
' of the Display class.')

widget = widget.__self__
if widget is self:
raise KivyWidgetException(
'Widget instances cannot be added to themselves.')

# This DisplayOutput instance will become the actual parent of the display. Other
# DisplayOutput instances connected to the same display will be stored in a list
# of parents and will be redrawn whenever this instance is redrawn.

if widget.parents:
previous_parent = widget.parents[-1]
previous_parent.canvas.after.clear()
with self.canvas.after: # pylint: disable=not-context-manager
Callback(self.on_draw_display_source)

widget.parent = self
widget.parents.append(self)

Expand All @@ -648,15 +657,33 @@ def remove_display_source(self, widget):
'remove_display_source() can be used only with instances'
' of the Display class.')
widget.parents.remove(self)
widget.parent = None
self.canvas.after.clear()

if widget.parents:
widget.parent = widget.parents[-1]

if len(widget.parents) > 1:
with widget.parent.canvas.after:
Callback(widget.parent.on_draw_display_source)
else:
widget.parent = None

self.canvas.remove(widget.container.canvas)

def __repr__(self) -> str: # pragma: no cover
try:
return '<DisplayOutput size={}, pos={}, source={}>'.format(
self.size, self.pos, self.display.name)
return '<DisplayOutput size={}, pos={}, source={}, id={}>'.format(
self.size, self.pos, self.display.name, id(self))
except AttributeError:
return '<DisplayOutput size={}, source=(none)>'.format(self.size)
return '<DisplayOutput size={}, source=(none), id={}>'.format(self.size, id(self))

def on_draw_display_source(self, instr):
"""Callback function when primary display source is redrawn."""
del instr

for display_source in self.display.parents:
if display_source != self:
display_source.canvas.ask_update()

def on_parent_resize(self, *args):
"""Fit to parent on resize."""
Expand Down