Coverage for src/ipyvizzu/animationcontrol.py: 100%
26 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-02-26 10:12 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-02-26 10:12 +0000
1"""A module for working with animation control."""
3from typing import Union, Callable
4import uuid
6from ipyvizzu.template import DisplayTemplate
7from ipyvizzu.animation import Animation
10class AnimationControl:
11 """
12 A class for controlling animations.
13 """
15 def __init__(self, prev_id: str, last_id: str, display_method: Callable):
16 """
17 AnimationControl constructor.
19 Args:
20 prev_id: Id of the previous animation promise.
21 last_id: Id of the animation to be controlled.
22 display_method: Displaying function.
23 """
25 self._ids = ", ".join([f"'{prev_id}'", f"'{last_id}'"])
26 self._display = display_method
28 def cancel(self) -> None:
29 """Cancels the animation, will reject the animation promise."""
31 self._display(
32 DisplayTemplate.CONTROL.format(
33 method="cancel",
34 params=self._ids,
35 )
36 )
38 def pause(self) -> None:
39 """Pauses the controlled animation."""
41 self._display(
42 DisplayTemplate.CONTROL.format(
43 method="pause",
44 params=self._ids,
45 )
46 )
48 def play(self) -> None:
49 """Plays/resumes playing of the controlled animation."""
51 self._display(
52 DisplayTemplate.CONTROL.format(
53 method="play",
54 params=self._ids,
55 )
56 )
58 def reverse(self) -> None:
59 """Changes the direction of the controlled animation."""
61 self._display(
62 DisplayTemplate.CONTROL.format(
63 method="reverse",
64 params=self._ids,
65 )
66 )
68 def seek(self, value: Union[int, str]) -> None:
69 """
70 Seeks the animation to the position specified by time or progress percentage.
72 Args:
73 value: The position specified by time or progress percentage.
74 """
76 params = ", ".join([self._ids, f"'{value}'"])
77 self._display(
78 DisplayTemplate.CONTROL.format(
79 method="seek",
80 params=params,
81 )
82 )
84 def stop(self) -> None:
85 """Stops the current animation seeking it back to its start position."""
87 self._display(
88 DisplayTemplate.CONTROL.format(
89 method="stop",
90 params=self._ids,
91 )
92 )
94 def store(self) -> Animation:
95 """
96 A method for saving and storing the actual state of the animation.
98 Returns:
99 An `Animation` object wich stores the actual state of the animation.
100 """
102 animation_id = uuid.uuid4().hex[:7]
103 params = ", ".join([self._ids, f"'{animation_id}'"])
104 self._display(
105 DisplayTemplate.CONTROL.format(
106 method="store",
107 params=params,
108 )
109 )
110 return Animation(animation_id)