epi: drag native window (#728)

* feat: drag window

* Update epi/src/lib.rs

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>

* Update egui_demo_lib/src/backend_panel.rs

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>

* cargo fmt

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
zu1k 2021-09-28 23:34:58 +08:00 committed by GitHub
parent 1b36863248
commit f6fb4d942a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 0 deletions

View file

@ -208,6 +208,13 @@ impl BackendPanel {
if ui.button("Quit").clicked() {
frame.quit();
}
if ui
.button("Drag me to drag window")
.is_pointer_button_down_on()
{
frame.drag_window();
}
}
}

View file

@ -328,6 +328,7 @@ pub fn run(mut app: Box<dyn epi::App>, native_options: &epi::NativeOptions) {
quit,
window_size,
decorated,
drag_window,
} = app_output;
if let Some(decorated) = decorated {
@ -344,6 +345,10 @@ pub fn run(mut app: Box<dyn epi::App>, native_options: &epi::NativeOptions) {
);
}
if drag_window {
let _ = display.gl_window().window().drag_window();
}
if quit {
running = false;
} else if needs_repaint {

View file

@ -259,6 +259,7 @@ impl AppRunner {
quit: _, // Can't quit a web page
window_size: _, // Can't resize a web page
decorated: _, // Can't show decorations
drag_window: _, // Can't be dragged
} = app_output;
}

View file

@ -252,6 +252,14 @@ impl<'a> Frame<'a> {
self.0.output.decorated = Some(decorated);
}
/// When called, the native window will follow the
/// movement of the cursor while the primary mouse button is down.
///
/// Does not work on the web, and works badly on Mac.
pub fn drag_window(&mut self) {
self.0.output.drag_window = true;
}
/// If you need to request a repaint from another thread, clone this and send it to that other thread.
pub fn repaint_signal(&self) -> std::sync::Arc<dyn RepaintSignal> {
self.0.repaint_signal.clone()
@ -410,5 +418,8 @@ pub mod backend {
/// Set to some bool to change window decorations
pub decorated: Option<bool>,
/// Set to true to drap window
pub drag_window: bool,
}
}