Skip to content

Commit 097ae8f

Browse files
feat(present): fix presentation size and add screen option (#232)
* feat(present): fix presentation size and add screen option This kinda fixes the issue of weird resizes between two presentations * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix(ci): typing --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 738c54f commit 097ae8f

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

manim_slides/logger.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"File",
2626
"Rendering",
2727
"Rendered",
28+
"Pressed key",
2829
]
2930

3031

manim_slides/present.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@
1414
from pydantic import ValidationError
1515
from pydantic_extra_types.color import Color
1616
from PySide6.QtCore import Qt, QThread, Signal, Slot
17-
from PySide6.QtGui import QCloseEvent, QIcon, QImage, QKeyEvent, QPixmap, QResizeEvent
17+
from PySide6.QtGui import (
18+
QCloseEvent,
19+
QIcon,
20+
QImage,
21+
QKeyEvent,
22+
QPixmap,
23+
QResizeEvent,
24+
QScreen,
25+
)
1826
from PySide6.QtWidgets import QApplication, QGridLayout, QLabel, QWidget
1927
from tqdm import tqdm
2028

@@ -351,7 +359,7 @@ class Display(QThread): # type: ignore
351359

352360
change_video_signal = Signal(np.ndarray)
353361
change_info_signal = Signal(dict)
354-
change_presentation_sigal = Signal()
362+
change_presentation_signal = Signal()
355363
finished = Signal()
356364

357365
def __init__(
@@ -402,7 +410,7 @@ def current_presentation_index(self, value: Optional[int]) -> None:
402410
if -len(self) <= value < len(self):
403411
self.__current_presentation_index = value
404412
self.current_presentation.release_cap()
405-
self.change_presentation_sigal.emit()
413+
self.change_presentation_signal.emit()
406414
else:
407415
logger.error(
408416
f"Could not load scene number {value}, playing first scene instead."
@@ -423,6 +431,10 @@ def current_background_color(self) -> Color:
423431
"""Returns the background color of the current presentation."""
424432
return self.current_presentation.background_color
425433

434+
def start(self) -> None:
435+
super().start()
436+
self.change_presentation_signal.emit()
437+
426438
def run(self) -> None:
427439
"""Runs a series of presentations until end or exit."""
428440
while self.run_flag:
@@ -651,10 +663,15 @@ def __init__(
651663
aspect_ratio: AspectRatio = AspectRatio.auto,
652664
resize_mode: Qt.TransformationMode = Qt.SmoothTransformation,
653665
background_color: str = "black",
666+
screen: Optional[QScreen] = None,
654667
**kwargs: Any,
655668
):
656669
super().__init__()
657670

671+
if screen:
672+
self.setScreen(screen)
673+
self.move(screen.geometry().topLeft())
674+
658675
self.setWindowTitle(WINDOW_NAME)
659676
self.icon = QIcon(":/icon.png")
660677
self.setWindowIcon(self.icon)
@@ -676,10 +693,6 @@ def __init__(
676693
if self.aspect_ratio == AspectRatio.auto:
677694
self.label.setScaledContents(True)
678695
self.label.setAlignment(Qt.AlignCenter)
679-
self.label.resize(self.display_width, self.display_height)
680-
self.label.setStyleSheet(
681-
f"background-color: {self.thread.current_background_color}"
682-
)
683696

684697
self.pixmap = QPixmap(self.width(), self.height())
685698
self.label.setPixmap(self.pixmap)
@@ -694,11 +707,13 @@ def __init__(
694707

695708
if fullscreen:
696709
self.showFullScreen()
710+
else:
711+
self.resize(self.display_width, self.display_height)
697712

698713
# connect signals
699714
self.thread.change_video_signal.connect(self.update_image)
700715
self.thread.change_info_signal.connect(self.info.update_info)
701-
self.thread.change_presentation_sigal.connect(self.update_canvas)
716+
self.thread.change_presentation_signal.connect(self.update_canvas)
702717
self.thread.finished.connect(self.closeAll)
703718
self.send_key_signal.connect(self.thread.set_key)
704719

@@ -756,8 +771,12 @@ def update_image(self, cv_img: np.ndarray) -> None:
756771
def update_canvas(self) -> None:
757772
"""Update the canvas when a presentation has changed."""
758773
logger.debug("Updating canvas")
759-
self.display_width, self.display_height = self.thread.current_resolution
760-
if not self.isFullScreen():
774+
w, h = self.thread.current_resolution
775+
776+
if not self.isFullScreen() and (
777+
self.display_width != w or self.display_height != h
778+
):
779+
self.display_width, self.display_height = w, h
761780
self.resize(self.display_width, self.display_height)
762781
self.label.setStyleSheet(
763782
f"background-color: {self.thread.current_background_color}"
@@ -998,6 +1017,14 @@ def str_to_int_or_none(value: str) -> Optional[int]:
9981017
default=0,
9991018
help="Start presenting at a given animation number (0 is first, -1 is last). This conflicts with slide number since animation number is absolute to the presentation.",
10001019
)
1020+
@click.option(
1021+
"--screen",
1022+
"screen_number",
1023+
metavar="NUMBER",
1024+
type=int,
1025+
default=None,
1026+
help="Presents content on the given screen (a.k.a. display).",
1027+
)
10011028
@click.help_option("-h", "--help")
10021029
@verbosity_option
10031030
def present(
@@ -1018,6 +1045,7 @@ def present(
10181045
start_at_scene_number: Optional[int],
10191046
start_at_slide_number: Optional[int],
10201047
start_at_animation_number: Optional[int],
1048+
screen_number: Optional[int] = None,
10211049
) -> None:
10221050
"""
10231051
Present SCENE(s), one at a time, in order.
@@ -1069,7 +1097,9 @@ def present(
10691097
ext = record_to.suffix
10701098
if ext.lower() != ".avi":
10711099
raise click.UsageError(
1072-
"Recording only support '.avi' extension. For other video formats, please convert the resulting '.avi' file afterwards."
1100+
"Recording only support '.avi' extension. "
1101+
"For other video formats, "
1102+
"please convert the resulting '.avi' file afterwards."
10731103
)
10741104

10751105
if start_at[0]:
@@ -1087,6 +1117,19 @@ def present(
10871117
app = QApplication.instance()
10881118

10891119
app.setApplicationName("Manim Slides")
1120+
1121+
if screen_number is not None:
1122+
try:
1123+
screen = app.screens()[screen_number]
1124+
except IndexError:
1125+
logger.error(
1126+
f"Invalid screen number {screen_number}, "
1127+
f"allowed values are from 0 to {len(app.screens())-1} (incl.)"
1128+
)
1129+
screen = None
1130+
else:
1131+
screen = None
1132+
10901133
a = App(
10911134
presentations,
10921135
config=config,
@@ -1101,7 +1144,9 @@ def present(
11011144
start_at_scene_number=start_at_scene_number,
11021145
start_at_slide_number=start_at_slide_number,
11031146
start_at_animation_number=start_at_animation_number,
1147+
screen=screen,
11041148
)
1149+
11051150
a.show()
11061151

11071152
# inform about CTRL+C

0 commit comments

Comments
 (0)