from fabric import Service, Signal, Property
from gi.repository import GLib, Gtk
def finished(self) -> None: ...
@Property(tuple[float, float, float, float], "read-write")
def bezier_curve(self) -> tuple[float, float, float, float]:
return self._bezier_curve
def bezier_curve(self, value: tuple[float, float, float, float]):
self._bezier_curve = value
@Property(float, "read-write")
def value(self, value: float):
@Property(float, "read-write")
def max_value(self, value: float):
@Property(float, "read-write")
def min_value(self, value: float):
@Property(bool, "read-write", default_value=False)
def playing(self, value: bool):
@Property(bool, "read-write", default_value=False)
def repeat(self, value: bool):
bezier_curve: tuple[float, float, float, float],
tick_widget: Gtk.Widget | None = None,
super().__init__(**kwargs)
self._bezier_curve = (1, 0, 1, 1)
self.bezier_curve = bezier_curve
self.min_value = min_value
self.max_value = max_value
self._tick_handler = None
self._tick_widget = tick_widget
def do_get_time_now(self):
return GLib.get_monotonic_time() / 1_000_000
def do_lerp(self, start: float, end: float, time: float) -> float:
return start + (end - start) * time
def do_interpolate_cubic_bezier(self, time: float) -> float:
y_points = (0, self.bezier_curve[1], self.bezier_curve[3], 1)
(1 - time) ** 3 * y_points[0]
+ 3 * (1 - time) ** 2 * time * y_points[1]
+ 3 * (1 - time) * time**2 * y_points[2]
def do_ease(self, time: float) -> float:
self.min_value, self.max_value, self.do_interpolate_cubic_bezier(time)
def do_update_value(self, delta_time: float):
elapsed_time = delta_time - cast(float, self._start_time)
self._timeline_pos = min(1, elapsed_time / self.duration)
self.value = self.do_ease(self._timeline_pos)
if not self._timeline_pos >= 1:
self.value = self.max_value
self._start_time = delta_time
def do_handle_tick(self, *_):
current_time = self.do_get_time_now()
self.do_update_value(current_time)
def do_remove_tick_handlers(self):
self._tick_widget.remove_tick_callback(self._tick_handler)
GLib.source_remove(self._tick_handler)
self._tick_handler = None
self._start_time = self.do_get_time_now()
if not self._tick_handler:
self._tick_handler = self._tick_widget.add_tick_callback(
self._tick_handler = GLib.timeout_add(16, self.do_handle_tick)
return self.do_remove_tick_handlers()
if not self._tick_handler:
return self.do_remove_tick_handlers()