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

Fix: Add mouse pass through option to window #116

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions examples/mouse_passthrough.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2021 QuantumBadger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#![deny(warnings)]

use speedy2d::color::Color;
use speedy2d::font::{Font, FormattedTextBlock, TextLayout, TextOptions};
use speedy2d::window::{WindowCreationOptions, WindowHandler, WindowHelper, WindowSize};
use speedy2d::{Graphics2D, Window};

fn main()
{
simple_logger::SimpleLogger::new().init().unwrap();

let window = Window::new_with_options(
"Title",
WindowCreationOptions::new_windowed(WindowSize::MarginPhysicalPixels(0), None)
.with_always_on_top(true)
.with_decorations(false)
.with_maximized(true)
.with_transparent(true).with_mouse_passthrough(true)
)
.unwrap();

let font = Font::new(include_bytes!("../assets/fonts/NotoSans-Regular.ttf")).unwrap();

let text = font.layout_text("Hello world!", 64.0, TextOptions::new());

window.run_loop(MyWindowHandler { text })
}

struct MyWindowHandler
{
text: FormattedTextBlock
}

impl WindowHandler for MyWindowHandler
{
fn on_draw(&mut self, helper: &mut WindowHelper, graphics: &mut Graphics2D)
{
graphics.clear_screen(Color::WHITE);

graphics.draw_circle((150.0, 120.0), 75.0, Color::from_rgb(0.8, 0.9, 1.0));

graphics.draw_text((290.0, 90.0), Color::BLACK, &self.text);

// Request that we draw another frame once this one has finished
helper.request_redraw();
}
}
21 changes: 19 additions & 2 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,12 @@ impl<UserEventType> WindowHelper<UserEventType>
{
self.inner.create_user_event_sender()
}

/// Sets whether mouse events should pass through the window.
pub fn set_mouse_passthrough(&self, passthrough: bool)
{
self.inner.set_mouse_passthrough(passthrough)
}
}

#[cfg(any(doc, doctest, not(target_arch = "wasm32")))]
Expand Down Expand Up @@ -1079,7 +1085,8 @@ pub struct WindowCreationOptions
pub(crate) resizable: bool,
pub(crate) maximized: bool,
pub(crate) transparent: bool,
pub(crate) decorations: bool
pub(crate) decorations: bool,
pub(crate) mouse_passthrough: bool
}

impl WindowCreationOptions
Expand Down Expand Up @@ -1112,7 +1119,8 @@ impl WindowCreationOptions
resizable: true,
maximized: false,
decorations: true,
transparent: false
transparent: false,
mouse_passthrough: false
}
}

Expand Down Expand Up @@ -1194,6 +1202,15 @@ impl WindowCreationOptions
self.transparent = transparent;
self
}

/// Set whether mouse events should pass through the window.
#[inline]
#[must_use]
pub fn with_mouse_passthrough(mut self, mouse_passthrough: bool) -> Self
{
self.mouse_passthrough = mouse_passthrough;
self
}
}

/// Type representing a keyboard scancode.
Expand Down
4 changes: 4 additions & 0 deletions src/window_internal_glutin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ impl<UserEventType> WindowHelperGlutin<UserEventType>
{
UserEventSender::new(UserEventSenderGlutin::new(self.event_proxy.clone()))
}

pub fn set_mouse_passthrough(&self, passthrough: bool) {
self.window.set_cursor_hittest(!passthrough).ok();
}
}

pub(crate) struct WindowGlutin<UserEventType: 'static>
Expand Down