1
0
Fork 0

Adapt raw_window_handle 0.4 for nih_plug_egui

NIH-plug switched to version 0.5, so this adapter is needed for this
version of nih_plug_egui.
This commit is contained in:
Robbert van der Helm 2023-11-05 16:43:10 +01:00
parent b794cdeafe
commit a16cbd6aad
3 changed files with 33 additions and 1 deletions

1
Cargo.lock generated
View file

@ -2919,6 +2919,7 @@ dependencies = [
"lazy_static",
"nih_plug",
"parking_lot 0.12.1",
"raw-window-handle 0.4.3",
"serde",
]

View file

@ -19,6 +19,10 @@ opengl = []
[dependencies]
nih_plug = { path = ".." }
# The currently targeted version of baseview uses a different version of
# `raw_window_handle` than NIH-plug, so we need to manually convert between them
raw-window-handle = "0.4"
baseview = { git = "https://github.com/RustAudio/baseview.git", rev = "1d9806d5bd92275d0d8142d9c9c90198757b9b25" }
crossbeam = "0.8"
# The `egui-default-features` feature enables the default features. This makes

View file

@ -7,6 +7,7 @@ use egui::Context;
use egui_baseview::EguiWindow;
use nih_plug::prelude::{Editor, GuiContext, ParamSetter, ParentWindowHandle};
use parking_lot::RwLock;
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use std::sync::atomic::Ordering;
use std::sync::Arc;
@ -28,6 +29,32 @@ pub(crate) struct EguiEditor<T> {
pub(crate) scaling_factor: AtomicCell<Option<f32>>,
}
/// This version of `baseview` uses a different version of `raw_window_handle than NIH-plug, so we
/// need to adapt it ourselves.
struct ParentWindowHandleAdapter(nih_plug::editor::ParentWindowHandle);
unsafe impl HasRawWindowHandle for ParentWindowHandleAdapter {
fn raw_window_handle(&self) -> RawWindowHandle {
match self.0 {
ParentWindowHandle::X11Window(window) => {
let mut handle = raw_window_handle::XcbHandle::empty();
handle.window = window;
RawWindowHandle::Xcb(handle)
}
ParentWindowHandle::AppKitNsView(ns_view) => {
let mut handle = raw_window_handle::AppKitHandle::empty();
handle.ns_view = ns_view;
RawWindowHandle::AppKit(handle)
}
ParentWindowHandle::Win32Hwnd(hwnd) => {
let mut handle = raw_window_handle::Win32Handle::empty();
handle.hwnd = hwnd;
RawWindowHandle::Win32(handle)
}
}
}
}
impl<T> Editor for EguiEditor<T>
where
T: 'static + Send + Sync,
@ -44,7 +71,7 @@ where
let (unscaled_width, unscaled_height) = self.egui_state.size();
let scaling_factor = self.scaling_factor.load();
let window = EguiWindow::open_parented(
&parent,
&ParentWindowHandleAdapter(parent),
WindowOpenOptions {
title: String::from("egui window"),
// Baseview should be doing the DPI scaling for us