简体   繁体   English

拖动移动闪烁 window 的 position

[英]Drag move flickers position of window

I'm implementing a Rust alternative to .NET's DragMove method however the result causes the application to flicker between two relative positions.我正在实现 Rust 替代 .NET 的DragMove方法,但是结果导致应用程序在两个相对位置之间闪烁。

See screencast and sample project .请参阅截屏视频示例项目

Code I'm using to perform the drag move:我用来执行拖动移动的代码:

let mut mouse_down = false;
let mut last_pos: Option<PhysicalPosition<f64>> = None;
event_loop.run(move |event, _, control_flow| match event {
Event::WindowEvent {
        event: WindowEvent::CursorMoved {
            position,
            ..
        },
        ..
    } => {
        let gl_window = display.gl_window();
        let window = gl_window.window();
        if mouse_down {
            if last_pos.is_some() {
                let previous_pos = last_pos.unwrap();
                let delta_x = previous_pos.x - position.x;
                let delta_y = previous_pos.y - position.y;
                window.set_outer_position(PhysicalPosition::new(position.x + delta_x, position.y + delta_y));
            }
            last_pos = Some(position);
        }
    }
    Event::WindowEvent {
        event: WindowEvent::MouseInput{
            state,
            button,
            ..
        },
        ..
    } => {
        mouse_down = button == MouseButton::Left && state == ElementState::Pressed;
        if !mouse_down {
            last_pos = None;
        }
    }
    _ => {}
});

CursorMoved reports CursorMoved报告

(x,y) coords in pixels relative to the top-left corner of the window . (x,y)相对于 window 左上角的像素坐标。

When you're later using that position to set_outer_position , you are essentially reinterpreting window-relative coordinates as screen-relative.当您稍后将 position 用于set_outer_position时,您实际上是将窗口相对坐标重新解释为屏幕相对坐标。

You should instead apply the offset to the position returned from outer_position .您应该将偏移量应用于从 external_position 返回的position

While that fixes the immediate problem, I'm not sure it is enough to account for the window movement.虽然这解决了眼前的问题,但我不确定它是否足以解释 window 运动。 When you're handling the next CursorMoved event, the coordinates are still window-relative, but the window has since moved.当您处理下一个CursorMoved事件时,坐标仍然与窗口相关,但 window 已经移动。 That may produce artifacts all over.这可能会产生伪影。

A more robust solution would store the window's position when the drag-move operation starts, and offset that position by the accumulated deltas.更强大的解决方案是在拖动移动操作开始时存储窗口的 position,并通过累积的增量偏移 position。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM