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

Response.hovered() is always false when drag file and hovering on the widget. #2749

Open
AllenDang opened this issue Feb 18, 2023 · 2 comments
Labels
bug Something is broken

Comments

@AllenDang
Copy link

AllenDang commented Feb 18, 2023

Describe the bug
I'm trying to create some "file drop zone" widgets, like user could drag file into the main window and drop to specified widget.
That means there could be multiple widgets in the window which expect a file drop, and ideally I could use Reponse.hovered() to check which widget is actually get the dropped file.

But once the file is dragged into main window, Response.hovered() will always be false.

Here is a minimal code.

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release

use eframe::egui;

fn main() -> Result<(), eframe::Error> {
    let options = eframe::NativeOptions {
        drag_and_drop_support: true,
        initial_window_size: Some(egui::vec2(800.0, 600.0)),
        ..Default::default()
    };

    eframe::run_native(
        "Dnd test",
        options,
        Box::new(|_cc| Box::new(MyApp::default())),
    )
}

#[derive(Default)]
struct MyApp {
    mac_path: String,
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.label("Mac Pak File");

            let resp = ui.text_edit_singleline(&mut self.mac_path);

            let rect = resp.rect.expand(1.0);
            if !ctx.input(|i| i.raw.hovered_files.is_empty()) {
                if resp.hovered() {
                    ui.painter()
                        .rect_stroke(rect, 1.0, ui.visuals().widgets.active.fg_stroke);
                }
            }
        });
    }
}

To Reproduce
Steps to reproduce the behavior:

  1. Drag a file into main window.
  2. Hover that file on text edit.

Expected behavior
A highlight rect will be drawn.

Desktop (please complete the following information):

  • OS: macOS v13.1
  • Version: egui 0.21.3
  • Rust edition: 2021
@AllenDang AllenDang added the bug Something is broken label Feb 18, 2023
@gitsmol
Copy link

gitsmol commented Aug 22, 2023

It seems that when the egui window (ie winit) doesn't have focus, there is no way to get a pointer position. There is an open issue for winit suggesting as much: rust-windowing/winit#1550. Looks like there is an open PR that addresses this.

One workaround is to use dropped_files instead of hovered_files. If you use dropped_files there is an instance where winit can read both the pointer position (because the window has become active) and the dropped files. In the above example, this change made the whole thing work but only when the mouse is moving when the files are dropped. Not great.

Another workaround might be to store Vec as soon as the mouse enters the screen, then as soon as we receive a pointer position we assume that is where the user wants to drag and drop those files. Also not great but a little better.

@amPerl
Copy link
Contributor

amPerl commented Mar 13, 2024

Winit PR that aims to fix this: rust-windowing/winit#2615

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken
Projects
None yet
Development

No branches or pull requests

3 participants