Adapt raw_window_handle 0.4 for nih_plug_iced

I tried updating iced instead, but after a couple hours porting things I
gave up. Supporting newer iced versions will probably require rebuilding
the crate from scratch.
This commit is contained in:
Robbert van der Helm
2023-11-05 19:35:13 +01:00
parent a16cbd6aad
commit 76ef4d0ff9
3 changed files with 38 additions and 25 deletions

View File

@@ -59,11 +59,14 @@ smol = ["iced_baseview/smol"]
nih_plug = { path = ".." }
nih_plug_assets = { git = "https://github.com/robbert-vdh/nih_plug_assets.git" }
# 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"
atomic_refcell = "0.1"
baseview = { git = "https://github.com/RustAudio/baseview.git" }
baseview = { git = "https://github.com/RustAudio/baseview.git", rev = "1d9806d5bd92275d0d8142d9c9c90198757b9b25" }
crossbeam = "0.8"
# Upstream doesn't work with the current iced version, this branch also contains
# additional features
# This targets iced 0.4
iced_baseview = { git = "https://github.com/robbert-vdh/iced_baseview.git", branch = "feature/update-baseview", default_features = false }
# To make the state persistable
serde = { version = "1.0", features = ["derive"] }

View File

@@ -5,6 +5,7 @@ use crossbeam::atomic::AtomicCell;
use crossbeam::channel;
pub use iced_baseview::*;
use nih_plug::prelude::{Editor, GuiContext, ParentWindowHandle};
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use std::sync::atomic::Ordering;
use std::sync::Arc;
@@ -24,6 +25,32 @@ pub(crate) struct IcedEditorWrapper<E: IcedEditor> {
pub(crate) parameter_updates_receiver: Arc<channel::Receiver<ParameterUpdate>>,
}
/// 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<E: IcedEditor> Editor for IcedEditorWrapper<E> {
fn spawn(
&self,
@@ -36,7 +63,7 @@ impl<E: IcedEditor> Editor for IcedEditorWrapper<E> {
// TODO: iced_baseview does not have gracefuly error handling for context creation failures.
// This will panic if the context could not be created.
let window = IcedWindow::<wrapper::IcedEditorWrapperApplication<E>>::open_parented(
&parent,
&ParentWindowHandleAdapter(parent),
Settings {
window: WindowOpenOptions {
title: String::from("iced window"),