Skip to content

Commit

Permalink
feat: Throttle InteractiveViewer update events (#4704)
Browse files Browse the repository at this point in the history
* initial commit

* update pubspec.lock

* rename to interaction_update_interval

* reformat

---------

Co-authored-by: Feodor Fitsner <[email protected]>
  • Loading branch information
ndonkoHenri and FeodorFitsner authored Jan 16, 2025
1 parent 5ac5753 commit b98e0a0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
41 changes: 25 additions & 16 deletions packages/flet/lib/src/controls/interactive_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class _InteractiveViewerControlState extends State<InteractiveViewerControl>
late AnimationController _animationController;
Animation<Matrix4>? _animation;
Matrix4? _savedMatrix;
int _interactionUpdateTimestamp = DateTime.now().millisecondsSinceEpoch;

@override
void initState() {
Expand Down Expand Up @@ -158,22 +159,30 @@ class _InteractiveViewerControlState extends State<InteractiveViewerControl>
: null,
onInteractionUpdate: !disabled
? (ScaleUpdateDetails details) {
debugPrint(
"InteractiveViewer ${widget.control.id} onInteractionUpdate");
widget.backend.triggerControlEvent(
widget.control.id,
"interaction_update",
jsonEncode({
"pc": details.pointerCount,
"fp_x": details.focalPoint.dx,
"fp_y": details.focalPoint.dy,
"lfp_x": details.localFocalPoint.dx,
"lfp_y": details.localFocalPoint.dy,
"s": details.scale,
"hs": details.horizontalScale,
"vs": details.verticalScale,
"rot": details.rotation,
}));
var interactionUpdateInterval =
widget.control.attrInt("interactionUpdateInterval", 0)!;
var now = DateTime.now().millisecondsSinceEpoch;
if (now - _interactionUpdateTimestamp >
interactionUpdateInterval) {
debugPrint(
"InteractiveViewer ${widget.control.id} onInteractionUpdate");
_interactionUpdateTimestamp = now;
widget.backend.triggerControlEvent(
widget.control.id,
"interaction_update",
jsonEncode({
"pc": details.pointerCount,
"fp_x": details.focalPoint.dx,
"fp_y": details.focalPoint.dy,
"lfp_x": details.localFocalPoint.dx,
"lfp_y": details.localFocalPoint.dy,
"s": details.scale,
"hs": details.horizontalScale,
"vs": details.verticalScale,
"rot": details.rotation,
}));
;
}
}
: null,
child: contentCtrls.isNotEmpty
Expand Down
11 changes: 11 additions & 0 deletions sdk/python/packages/flet/src/flet/core/interactive_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(
clip_behavior: Optional[ClipBehavior] = None,
alignment: Optional[Alignment] = None,
boundary_margin: Optional[MarginValue] = None,
interaction_update_interval: Optional[int] = None,
on_interaction_start: Optional[
Callable[[InteractiveViewerInteractionStartEvent], None]
] = None,
Expand Down Expand Up @@ -189,6 +190,7 @@ def __init__(
self.on_interaction_start = on_interaction_start
self.on_interaction_end = on_interaction_end
self.on_interaction_update = on_interaction_update
self.interaction_update_interval = interaction_update_interval

def _get_control_name(self):
return "interactiveviewer"
Expand Down Expand Up @@ -228,6 +230,15 @@ def min_scale(self) -> float:
def min_scale(self, value: OptionalNumber):
self._set_attr("minScale", value)

# interaction_update_interval
@property
def interaction_update_interval(self) -> int:
return self._get_attr("interactionUpdateInterval", data_type="int", def_value=0)

@interaction_update_interval.setter
def interaction_update_interval(self, value: Optional[int]):
self._set_attr("interactionUpdateInterval", value)

# max_scale
@property
def max_scale(self) -> float:
Expand Down

0 comments on commit b98e0a0

Please sign in to comment.