flet icon indicating copy to clipboard operation
flet copied to clipboard

bug: The on_interaction_update event of the InteractiveViewer component has an error in the data returned

Open Qye-Leisure opened this issue 6 months ago • 3 comments

Duplicate Check

  • [x] I have searched the opened issues and there are no duplicates

Describe the bug

In the ft.InteractiveViewer() component, if interaction_update_interval is set to 200 milliseconds, the returned data should include all events that occurred within that interval. For example, if I trigger three scroll wheel events within 200 milliseconds, it should return the cumulative result of those three scroll events. However, Flet actually returns the data for only one event. To get correct data, you need to set interaction_update_interval to 0, but this causes significant performance overhead.

Code sample

Code

import flet as ft
from typing import  cast


@ft.control
class BaseCanvas(ft.GestureDetector):
    max_scale: float = 3
    scale_step: float = 0.25
    min_scale: float = 0.25

    def init(self):
        self.cumulative_offset_x = self.cumulative_offset_y = 0
        self.current_scale = 1

        self.window_container_size = ()
        self.canvas_size=()
        
        
        self.scaled_canvas_size = ()
        self.scale_offset_x = 0
        self.scale_offset_y = 0

        self.canvas_container = ft.Container(
            bgcolor=ft.Colors.BLUE_100,
            content=ft.Stack(
                controls=[
                    self.module_container((50, 50)),
                    self.module_container((50, 600)),
                    self.module_container((1000, 50)),
                    self.module_container((1000, 600)),
                ],
            ),
        )

        self.content = ft.InteractiveViewer(
            expand=True,
            clip_behavior= ft.ClipBehavior.HARD_EDGE,
            content=self.canvas_container,
            on_size_change = lambda e: self.get_window_container_size(e),
            constrained = False,
            on_interaction_update=lambda e:  print(e),

        )


        self.on_hover = self.mouse_hover_event

    def module_container(self, pos):
        module = ft.Container(
            bgcolor=ft.Colors.GREEN,
            width=70,
            content=ft.Column(
                [
                    ft.Container(width=70, content=ft.Text(f"module {j}"))
                    for j in range(10)
                ]
            ),
        )

        return ft.GestureDetector(
            mouse_cursor=ft.MouseCursor.MOVE,
            drag_interval=5,
            left=pos[0],
            top=pos[1],
            content=module,
        )

    def get_window_container_size(self, e):

        self.window_container_size = (e.width, e.height)
        if not self.canvas_size or self.canvas_size<self.window_container_size:
            self.canvas_size=self.window_container_size


            self.canvas_container.width,self.canvas_container.height=self.window_container_size
            self.canvas_container=cast(ft.Container,self.canvas_container)
            self.canvas_container.update()
        print(self.window_container_size)

    def mouse_hover_event(self, e: ft.HoverEvent[ft.GestureDetector]) -> None:
        """Handle mouse hover, compute canvas coordinates"""
        if e.local_delta is None:
            return







if __name__ == "__main__":

    def main(page: ft.Page):
        canvas = BaseCanvas()
        page.add(
            ft.Container(expand=True,bgcolor=ft.Colors.AMBER, content=canvas),
        )

    ft.run(main)


To reproduce

  1. Run the code
  2. Scale to the maximum size
  3. Observe the scale data returned by the on_interaction_update event.

Expected behavior

No response

Screenshots / Videos

Captures

[Upload media here]

Operating System

Windows

Operating system details

11

Flet version

0.85

Regression

No, it isn't

Suggestions

No response

Logs

Logs
[Paste your logs here]

Additional details

No response

Qye-Leisure avatar May 05 '26 21:05 Qye-Leisure

The reason why the data does not match is that the interaction_update_interval default is 200, but the screen is not updated according to this interval, assuming that the animation is triggered 3 times within 200 milliseconds, but only once the data is returned, resulting in the data not being aligned, I set it to 0

Qye-Leisure avatar May 08 '26 02:05 Qye-Leisure

This problem still needs to be solved; otherwise, the control will not function properly and its performance will be compromised in many areas.

Qye-Leisure avatar May 12 '26 08:05 Qye-Leisure

Addressed in Flet 1.1.0 by #6939 (how to upgrade).

on_interaction_update still reports one update per interval, with a scale relative to that gesture. To track the real zoom, use the new on_transform_changed. It follows interaction_update_interval too, but when changes stop, it always sends a final event with the transform actually applied. So a burst of wheel ticks ends with the correct total scale, without setting the interval to 0.

def on_transform(e: ft.InteractiveViewerTransformEvent):
    print(e.scale, e.translation_x, e.translation_y)

ft.InteractiveViewer(..., on_transform_changed=on_transform)

get_scale() and get_transform() return the current values on demand.

ndonkoHenri avatar Nov 05 '26 23:11 ndonkoHenri