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

🪟🫷Raw Window Handle Support #329

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
121 changes: 108 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ notan_text = { workspace = true, optional = true }
notan_audio = { workspace = true, optional = true }
notan_extra = { workspace = true, optional = true }
notan_random = { workspace = true, optional = true }
raw-window-handle = "0.5.2"

[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.26.0"
objc = "0.2.7"


[features]
default = ["backend", "log", "draw", "random", "glsl-to-spirv"]
Expand Down Expand Up @@ -330,6 +336,10 @@ required-features = ["draw"]
name = "log_basic"
required-features = ["log"]

[[example]]
name = "raw_window_handle"
required-features = ["draw"]

[[example]]
name = "renderer_instancing_cubes"
required-features = ["random"]
Expand Down
1 change: 1 addition & 0 deletions crates/notan_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde = { workspace = true, optional = true }
downcast-rs = "1.2.0"
indexmap = "2.0.2"
futures = "0.3.28"
raw-window-handle = "0.5.2"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
platter2 = "0.1.6"
Expand Down
3 changes: 3 additions & 0 deletions crates/notan_app/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use downcast_rs::{impl_downcast, Downcast};
use futures::prelude::*;
use futures::Future;
use notan_graphics::DeviceBackend;
use raw_window_handle::RawWindowHandle;

#[cfg(feature = "audio")]
use notan_audio::AudioBackend;
Expand Down Expand Up @@ -213,4 +214,6 @@ pub trait WindowBackend {

/// Returns if touch as mouse is enabled
fn touch_as_mouse(&self) -> bool;

fn raw_window_handle(&self) -> Option<RawWindowHandle>;
}
5 changes: 5 additions & 0 deletions crates/notan_app/src/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
App, Backend, BackendSystem, CursorIcon, EventIterator, FrameState, InitializeFn, WindowBackend,
};
use notan_graphics::prelude::*;
use raw_window_handle::RawWindowHandle;
use std::any::Any;

#[cfg(feature = "audio")]
Expand Down Expand Up @@ -137,6 +138,10 @@ impl WindowBackend for EmptyWindowBackend {
fn touch_as_mouse(&self) -> bool {
self.touch_as_mouse
}

fn raw_window_handle(&self) -> Option<RawWindowHandle> {
None
}
}

#[derive(Default)]
Expand Down
6 changes: 6 additions & 0 deletions crates/notan_winit/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;
use crate::gl_manager::GlManager;
use notan_app::WindowConfig;
use notan_app::{CursorIcon, WindowBackend};
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalPosition};
use winit::event_loop::EventLoop;
use winit::window::Fullscreen::Borderless;
Expand All @@ -26,6 +27,11 @@ pub struct WinitWindowBackend {
}

impl WindowBackend for WinitWindowBackend {

fn raw_window_handle(&self) -> Option<RawWindowHandle> {
Some(self.window().raw_window_handle())
}

fn capture_cursor(&self) -> bool {
self.captured
}
Expand Down
51 changes: 51 additions & 0 deletions examples/raw_window_handle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use notan::draw::*;
use notan::prelude::*;
use raw_window_handle::{AppKitWindowHandle, RawWindowHandle};

#[notan_main]
fn main() -> Result<(), String> {
let win = WindowConfig::default()
.set_transparent(true)
.set_decorations(false);
notan::init_with(setup)
.add_config(win)
.add_config(DrawConfig) // Simple way to add the draw extension
.draw(draw)
.build()
}

fn setup(app: &mut App) {
let raw_window_handle = app.window().raw_window_handle().unwrap();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol yeah just unwrap it, what could possibly go wrong?


match raw_window_handle {
RawWindowHandle::AppKit(AppKitWindowHandle { ns_view, .. }) => {
#[cfg(target_os = "macos")]
unsafe {
use cocoa::appkit::NSWindow;
use cocoa::base::NO;
use objc::runtime::Object;
use objc::{msg_send, sel, sel_impl};

// I told you it was unsafe.
let ns_view: *mut Object = ns_view as *mut Object;
let ns_window: *mut Object = msg_send![ns_view, window];

// NOTE: remove shadow to stop artifacts in transparent window
ns_window.setHasShadow_(NO);
ns_window.setLevel_(1001);
println!("{:?}", raw_window_handle)
}
}
_ => {
println!("{:?}", raw_window_handle)
}
}
}

fn draw(gfx: &mut Graphics) {
let mut draw = gfx.create_draw();
draw.clear(Color::TRANSPARENT);
draw.triangle((400.0, 100.0), (100.0, 500.0), (700.0, 500.0))
.color(Color::MAGENTA);
gfx.render(&draw);
}