Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Throttle InteractiveViewer update events #4704

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions client/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -988,18 +988,18 @@ packages:
dependency: transitive
description:
name: rive
sha256: b44b62feb908610ca6c85e05f4573a66118a23867425926cf06152d171236141
sha256: "2551a44fa766a7ed3f52aa2b94feda6d18d00edc25dee5f66e72e9b365bb6d6c"
url: "https://pub.dev"
source: hosted
version: "0.13.17"
version: "0.13.20"
rive_common:
dependency: transitive
description:
name: rive_common
sha256: a3e5786f8d85c89977062b9ceeb3b72a7c28f81e32fb68497744042ce20bee2f
sha256: "2ba42f80d37a4efd0696fb715787c4785f8a13361e8aea9227c50f1e78cf763a"
url: "https://pub.dev"
source: hosted
version: "0.4.12"
version: "0.4.15"
safe_local_storage:
dependency: transitive
description:
Expand Down
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: 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