Skip to content
Merged
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
1 change: 1 addition & 0 deletions examples/open_parented/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl ParentWindowHandler {
.with_title("baseview child");

let child_window = Window::create(window_open_options, ChildWindowHandler::new)?;
child_window.show()?;

Ok(Self { surface: surface.into(), damaged: true.into(), child_window })
}
Expand Down
2 changes: 1 addition & 1 deletion examples/plugin_clack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ crate-type = ["cdylib"]

[dependencies]
clack-plugin = "0.1.0"
clack-extensions = { version = "0.1.0", features = ["gui", "clack-plugin", "raw-window-handle_06"] }
clack-extensions = { version = "0.1.0", features = ["gui", "state", "clack-plugin", "raw-window-handle_06"] }
baseview = { path = "../..", features = ["opengl"] }
softbuffer = "0.4.8"
raw-window-handle = "0.6.2"
Expand Down
1 change: 1 addition & 0 deletions examples/plugin_clack/src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl PluginGuiImpl for ExamplePluginMainThread<'_> {

fn create(&mut self, _configuration: GuiConfiguration) -> Result<(), PluginError> {
let options = WindowSettings::new()
.parented()
.with_size(PhysicalSize::new(400, 200))
.with_gl_config(GlConfig::default());

Expand Down
14 changes: 13 additions & 1 deletion examples/plugin_clack/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::audio::ExamplePluginAudioProcessor;
use crate::gui::ExamplePluginGui;
use clack_extensions::gui::{HostGui, PluginGui};
use clack_extensions::state::{PluginState, PluginStateImpl};
use clack_plugin::prelude::*;
use clack_plugin::stream::{InputStream, OutputStream};

mod audio;
mod gui;
Expand All @@ -18,7 +20,7 @@ impl Plugin for ExamplePlugin {
type MainThread<'a> = ExamplePluginMainThread<'a>;

fn declare_extensions(builder: &mut PluginExtensions<Self>, _shared: Option<&()>) {
builder.register::<PluginGui>();
builder.register::<PluginGui>().register::<PluginState>();
}
}

Expand Down Expand Up @@ -59,4 +61,14 @@ impl<'a> PluginMainThread<'a, ()> for ExamplePluginMainThread<'a> {
}
}

impl PluginStateImpl for ExamplePluginMainThread<'_> {
fn save(&mut self, _output: &mut OutputStream) -> Result<(), PluginError> {
Ok(())
}

fn load(&mut self, _input: &mut InputStream) -> Result<(), PluginError> {
Ok(())
}
}

clack_export_entry!(SinglePluginEntry<ExamplePlugin>);
12 changes: 10 additions & 2 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ pub trait WindowHandler: 'static {
fn on_frame(&self) -> core::result::Result<(), HandlerError>;
/// Informs the handler that the window has been resized.
///
/// If this returns an error, the resize operation will be reverted in order to keep the current
/// size.
/// # Errors
///
/// This operation can fail, in which case an [`HandlerError`] can be returned.
/// This can happen if e.g. an underlying buffer could not be resized, or some kind of driver error.
///
/// In case this `resized` operation fails, `baseview` will assume that it did not meaningfully
/// change anything, and that the window is still able to render and operate at the previous size.
///
/// It will also attempt to resize the underlying platform window and parent window back to the
/// previous size, but this is only a best-effort attempt since those operations can also fail.
fn resized(&self, new_size: WindowSize) -> core::result::Result<(), HandlerError>;
fn on_event(&self, event: Event) -> EventStatus;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ mod handler;
pub mod host;
mod keyboard;
mod mouse_cursor;
mod settings;
mod tracing;
mod window;
mod window_open_options;

pub(crate) mod platform;

Expand All @@ -22,8 +22,8 @@ pub use error::*;
pub use event::*;
pub use handler::WindowHandler;
pub use mouse_cursor::MouseCursor;
pub use settings::*;
pub use window::*;
pub use window_open_options::*;

#[allow(unused)]
pub(crate) use tracing::*;
Expand Down
14 changes: 7 additions & 7 deletions src/platform/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

use super::keyboard::{make_modifiers, KeyboardState};
use super::window::WindowSharedState;
use crate::handler::WindowHandlerBuilder;
use crate::host::Host;
use crate::platform::*;
use crate::tracing::warn;
use crate::window::WindowInitializer;
use crate::wrappers::appkit::*;
use crate::MouseEvent::{ButtonPressed, ButtonReleased};
use crate::{
DropData, DropEffect, Event, EventStatus, MouseButton, MouseEvent, ScrollDelta, WindowEvent,
WindowHandler, WindowSettings, WindowSize,
WindowHandler, WindowSize,
};
use dpi::{LogicalPosition, LogicalSize, Size};
use objc2::__framework_prelude::Retained;
Expand Down Expand Up @@ -79,8 +79,8 @@ pub(crate) struct BaseviewView {

impl BaseviewView {
pub fn new(
_options: WindowSettings, builder: WindowHandlerBuilder, parenting: ViewParentingType,
host: Host, final_size: LogicalSize<f64>, mtm: MainThreadMarker,
init: WindowInitializer, parenting: ViewParentingType, final_size: LogicalSize<f64>,
mtm: MainThreadMarker,
) -> Result<(Retained<View<Self>>, Rc<WindowSharedState>)> {
let view_rect =
NSRect::new(NSPoint::ZERO, NSSize::new(final_size.width, final_size.height));
Expand All @@ -96,7 +96,7 @@ impl BaseviewView {
window_handler: WindowHandlerContainer::new(),
notification_center_observer: None.into(),
parenting: ViewParentingType::Uninitialized.into(),
host,
host: init.host,
lifetime_tied_to_app: None.into(),

#[cfg(feature = "opengl")]
Expand All @@ -112,13 +112,13 @@ impl BaseviewView {
view.state.size.set(view.view.size());

#[cfg(feature = "opengl")]
if let Some(gl_config) = _options.gl_config {
if let Some(gl_config) = init.settings.gl_config {
let gl_context = super::gl::GlContext::create(view.view, gl_config, view.mtm)?;
let Ok(()) = view.gl_context.set(gl_context) else { unreachable!() };
}

let context = WindowContext::new(view);
let handler = builder.build(crate::WindowContext::new(context))?;
let handler = init.builder.build(crate::WindowContext::new(context))?;

// Initialize handler
view.window_handler.set(handler);
Expand Down
32 changes: 10 additions & 22 deletions src/platform/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use objc2_foundation::{NSSize, NSString};
use std::cell::Cell;
use std::rc::Rc;

use crate::handler::WindowHandlerBuilder;
use crate::host::Host;
use crate::platform::macos::view::{BaseviewView, ViewParentingType};
use crate::platform::ParentWindowHandle;
use crate::platform::Result;
Expand All @@ -31,9 +29,7 @@ impl Drop for WindowHandle {
}

impl WindowHandle {
pub fn create_window(
mut options: WindowSettings, handler: WindowHandlerBuilder, host: Host,
) -> Result<Self> {
pub fn create_window(mut init: WindowInitializer) -> Result<Self> {
autoreleasepool(|_| {
let Some(mtm) = MainThreadMarker::new() else {
panic!("macOS: Windows can only be created on the main thread!")
Expand All @@ -42,48 +38,40 @@ impl WindowHandle {
// Creates the global NSApplication instance, if it doesn't exist yet
let _ = NSApplication::sharedApplication(mtm);

if let Some(parent) = options.parent.take() {
return Self::create_window_parented(
options,
handler,
host,
parent.inner.view,
mtm,
);
if let Some(parent) = init.settings.parent.take() {
return Self::create_window_parented(init, parent.inner.view, mtm);
}

Self::create_window_standalone(options, handler, host, mtm)
Self::create_window_standalone(init, mtm)
})
}

pub fn create_window_parented(
builder: WindowSettings, handler: WindowHandlerBuilder, host: Host,
parent_view: Retained<NSView>, mtm: MainThreadMarker,
init: WindowInitializer, parent_view: Retained<NSView>, mtm: MainThreadMarker,
) -> Result<Self> {
let parenting =
ViewParentingType::Parented { parent_view: Weak::from_retained(&parent_view) };

let backing_scale_factor =
parent_view.window().map(|w| w.backingScaleFactor()).unwrap_or(1.0);
let final_size = builder.size.to_logical(backing_scale_factor);
let final_size = init.settings.size.to_logical(backing_scale_factor);

let (ns_view, state) =
BaseviewView::new(builder, handler, parenting, host, final_size, mtm)?;
let (ns_view, state) = BaseviewView::new(init, parenting, final_size, mtm)?;

Ok(Self { mtm, state, _window: None, view: Weak::from_retained(&ns_view) })
}

pub fn create_window_standalone(
builder: WindowSettings, handler: WindowHandlerBuilder, host: Host, mtm: MainThreadMarker,
init: WindowInitializer, mtm: MainThreadMarker,
) -> Result<Self> {
let window = create_window_with_options(&builder, mtm);
let window = create_window_with_options(&init.settings, mtm);

let final_size = window.contentRectForFrameRect(window.frame()).size;
let final_size = LogicalSize::new(final_size.width, final_size.height);

let parenting = ViewParentingType::Windowed { owned_window: Weak::from_retained(&window) };

let (view, state) = BaseviewView::new(builder, handler, parenting, host, final_size, mtm)?;
let (view, state) = BaseviewView::new(init, parenting, final_size, mtm)?;

Ok(Self { mtm, state, view: Weak::from_retained(&view), _window: Some(window) })
}
Expand Down
Loading