Skip to content

Commit

Permalink
cache the passthrough status, so that we avoid redundant set_passthro…
Browse files Browse the repository at this point in the history
…ugh calls
  • Loading branch information
coderedart committed Jan 2, 2024
1 parent ee50119 commit 06e9e31
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crates/egui_window_glfw_passthrough/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "egui_window_glfw_passthrough"
description = "egui windowing backend using Glfw"
version.workspace = true
version = "0.6.1"
repository.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
15 changes: 15 additions & 0 deletions crates/egui_window_glfw_passthrough/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub struct GlfwBackend {
pub cursor_inside_bounds: bool,
pub title: String,
pub focused: bool,
/// if the window is mouse_passthrough or not.
/// We cache this, to avoid redundant calls to [glfw::Window::set_mouse_passthrough]
pub passthrough: bool,
}
impl Drop for GlfwBackend {
fn drop(&mut self) {
Expand Down Expand Up @@ -237,6 +240,7 @@ impl GlfwBackend {
pixels_per_virtual_unit: {pixels_per_virtual_unit};
"
);
let pass = window.is_mouse_passthrough();
Self {
glfw: glfw_context,
events_receiver,
Expand All @@ -258,6 +262,7 @@ impl GlfwBackend {
window_position,
title: window_title,
focused: focus,
passthrough: pass,
}
}
/// returns raw input and scale. `scale` is only Some, if it changed (or if first frame). Otherwise it just returns None.
Expand Down Expand Up @@ -335,6 +340,16 @@ impl GlfwBackend {
self.title = title;
self.window.set_title(&self.title);
}
pub fn is_passthrough(&self) -> bool {
self.passthrough
}
pub fn set_passthrough(&mut self, passthrough: bool) {
if self.passthrough == passthrough {
return;
}
self.window.set_mouse_passthrough(passthrough);
self.passthrough = passthrough;
}
}

impl GlfwBackend {
Expand Down
6 changes: 4 additions & 2 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ impl EguiOverlay for HelloWorld {

// here you decide if you want to be passthrough or not.
if egui_context.wants_pointer_input() || egui_context.wants_keyboard_input() {
glfw_backend.window.set_mouse_passthrough(false);
// we need input, so we need the window to be NOT passthrough
glfw_backend.set_passthrough(false);
} else {
glfw_backend.window.set_mouse_passthrough(true);
// we don't care about input, so the window can be passthrough now
glfw_backend.set_passthrough(true)
}
egui_context.request_repaint();
}
Expand Down
6 changes: 4 additions & 2 deletions examples/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ impl EguiOverlay for HelloWorld {
});
// here you decide if you want to be passthrough or not.
if egui_context.wants_pointer_input() || egui_context.wants_keyboard_input() {
glfw_backend.window.set_mouse_passthrough(false);
// we need input, so we need the window to be NOT passthrough
glfw_backend.set_passthrough(false);
} else {
glfw_backend.window.set_mouse_passthrough(true);
// we don't care about input, so the window can be passthrough now
glfw_backend.set_passthrough(true)
}
egui_context.request_repaint();
}
Expand Down

0 comments on commit 06e9e31

Please sign in to comment.