flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.32k stars 439 forks source link

Gesture detector e.delta_y, e.global_y, e.local_y, e.delta_x, e.global_x, e.local_x appears to give slightly inaccurate results when the on_vertical_drag_update, on_horizontal_drag_update, on_pan_update callbacks are executed. #825

Open ItsCubeTime opened 1 year ago

ItsCubeTime commented 1 year ago

Demo

https://youtu.be/DBS9pVDReQs

Sample

External module "mouse" used for demonstrating correct behavior can be installed via pip install mouse

import flet as ft
mousePosPreviousTick: list[float] = None
def main(page: ft.Page):
    def on_pan_update1(e: ft.DragUpdateEvent):
        c.top = max(0, c.top + e.delta_y)
        c.left = max(0, c.left + e.delta_x)
        c.update()

    def on_pan_update2(e: ft.DragUpdateEvent):
        e.control.top = max(0, e.control.top + e.delta_y)
        e.control.left = max(0, e.control.left + e.delta_x)
        e.control.update()

    def on_pan_update_window(e: ft.DragUpdateEvent):
        page: ft.Page = e.page
        page.window_top += e.delta_y
        page.window_left += e.delta_x
        page.update()

    # def on_pan_update_window(e: ft.DragUpdateEvent):
    #     import mouse
    #     global mousePosPreviousTick
    #     if mousePosPreviousTick != None:
    #         page: ft.Page = e.page
    #         page.window_top +=  mouse.get_position()[1] - mousePosPreviousTick[1]
    #         page.window_left += mouse.get_position()[0] - mousePosPreviousTick[0]
    #     mousePosPreviousTick = mouse.get_position()
    #     page.update()

    gd = ft.GestureDetector(
        mouse_cursor=ft.MouseCursor.MOVE,
        drag_interval=50,
        on_pan_update=on_pan_update1,
    )

    c = ft.Container(gd, bgcolor=ft.colors.AMBER, width=50, height=50, left=0, top=0)

    gd1 = ft.GestureDetector(
        mouse_cursor=ft.MouseCursor.MOVE,
        drag_interval=10,
        on_vertical_drag_update=on_pan_update2,
        left=100,
        top=100,
        content=ft.Container(bgcolor=ft.colors.BLUE, width=50, height=50),
    )

    gd_window = ft.GestureDetector(
        mouse_cursor=ft.MouseCursor.MOVE,
        drag_interval=10,
        on_vertical_drag_update=on_pan_update_window,
        # , on_pan_end=self.resizeWindowDragHandlesEND
        left=200,
        top=200,
        content=ft.Container(bgcolor=ft.colors.PINK, width=50, height=50),
    )

    page.add( ft.Stack([c, gd1, gd_window], width=1000, height=500))

ft.app(target=main)

Motivation/usecase (I'm seeking workarounds if this cant be fixed as the "mouse" module I'm using to demonstrate isnt fully cross-platform)

I wanted to create my own window resize handles at the borders of the window. Dont really know of any other way that this can be achieved. Are there any alternative methods of getting the cursor position in flet? Note, that Im looking to resize the window at the end of the day, so WindowDragArea isnt quite what I need (unless it can be updated to include this functionality).

Thanks for reading!💖

feb-cloud commented 2 months ago

I am now also in order to achieve this effect and worry, I would like to ask you have a solution?