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
33 changes: 31 additions & 2 deletions examples/plugin_clack/src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,23 @@ impl PluginGuiImpl for ExamplePluginMainThread<'_> {
})
}

fn adjust_size(&mut self, size: GuiSize) -> Option<GuiSize> {
Some(size) // Not supported yet
fn adjust_size(&mut self, mut size: GuiSize) -> Option<GuiSize> {
let Some(gui) = &self.gui else { return None };
let scale_factor = gui.handle.size().scale_factor;

if let Some(max_size) = gui.handle.max_size() {
let max_size = size_to_gui_size(max_size, scale_factor);
size.width = size.width.min(max_size.width);
size.height = size.height.min(max_size.height);
}

if let Some(min_size) = gui.handle.min_size() {
let min_size = size_to_gui_size(min_size, scale_factor);
size.width = size.width.max(min_size.width);
size.height = size.height.max(min_size.height);
}

Some(size)
}

fn set_size(&mut self, size: GuiSize) -> Result<(), PluginError> {
Expand Down Expand Up @@ -150,6 +165,20 @@ impl PluginGuiImpl for ExamplePluginMainThread<'_> {
}
}

fn size_to_gui_size(size: Size, scale_factor: f64) -> GuiSize {
#[cfg(target_os = "macos")]
{
let size = size.to_logical(scale_factor);
GuiSize { width: size.width, height: size.height }
}

#[cfg(not(target_os = "macos"))]
{
let size = size.to_physical(scale_factor);
GuiSize { width: size.width, height: size.height }
}
}

fn window_size_to_gui_size(size: WindowSize) -> GuiSize {
#[cfg(target_os = "macos")]
{
Expand Down
6 changes: 4 additions & 2 deletions examples/render_wgpu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,10 @@ impl WindowHandler for WgpuExample {

fn main() -> Result<(), baseview::Error> {
env_logger::builder().filter_level(LevelFilter::Debug).init();
let window_open_options =
WindowSettings::new().with_title("WGPU on Baseview").with_size(LogicalSize::new(512, 512));
let window_open_options = WindowSettings::new()
.with_title("WGPU on Baseview")
.with_size(LogicalSize::new(512, 512))
.with_max_size(LogicalSize::new(512, 512));

Window::create(window_open_options, |c| pollster::block_on(WgpuExample::new(c)))?
.run_until_closed()?;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ pub use window::*;
#[allow(unused)]
pub(crate) use tracing::*;

mod utils;
pub(crate) mod wrappers;
31 changes: 30 additions & 1 deletion src/platform/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::window::WindowSharedState;
use crate::host::Host;
use crate::platform::*;
use crate::tracing::warn;
use crate::utils::SizingStrategy;
use crate::window::WindowInitializer;
use crate::wrappers::appkit::*;
use crate::MouseEvent::{ButtonPressed, ButtonReleased};
Expand Down Expand Up @@ -85,7 +86,11 @@ impl BaseviewView {
let view_rect =
NSRect::new(NSPoint::ZERO, NSSize::new(final_size.width, final_size.height));

let state = Rc::new(WindowSharedState::new(final_size, 1.0, init.settings.resizable));
let state = Rc::new(WindowSharedState::new(
final_size,
1.0,
SizingStrategy::from_settings(&init.settings),
));

let inner = BaseviewView {
mtm,
Expand All @@ -111,6 +116,8 @@ impl BaseviewView {
view.state.scale_factor.set(view.view.backing_scale_factor());
view.state.size.set(view.view.size());

Self::apply_size_constraints(view);

#[cfg(feature = "opengl")]
if let Some(gl_config) = init.settings.gl_config {
let gl_context = super::gl::GlContext::create(view.view, gl_config, view.mtm)?;
Expand Down Expand Up @@ -244,6 +251,24 @@ impl BaseviewView {
Self::close(this, false);
}
}

fn apply_size_constraints(this: ViewRef<Self>) {
let ViewParentingType::Windowed { owned_window } = &*this.parenting.borrow() else {
return;
};
let Some(window) = owned_window.load() else { return };
let scale_factor = window.backingScaleFactor();

if let Some(min_size) = this.state.sizing_strategy.min_size() {
let min_size = min_size.to_logical(scale_factor);
window.setContentMinSize(NSSize::new(min_size.width, min_size.height));
}

if let Some(max_size) = this.state.sizing_strategy.max_size() {
let max_size = max_size.to_logical(scale_factor);
window.setContentMaxSize(NSSize::new(max_size.width, max_size.height));
}
}
}

impl Drop for BaseviewView {
Expand Down Expand Up @@ -290,6 +315,10 @@ impl ViewImpl for BaseviewView {
let current_size = this.view.size();
let current_scale_factor = this.view.backing_scale_factor();

if this.state.scale_factor.get() != current_scale_factor {
Self::apply_size_constraints(this);
}

// Only send the event when the window's size has actually changed to be in line with the
// other platform implementations
if this.state.scale_factor.get() != current_scale_factor
Expand Down
17 changes: 13 additions & 4 deletions src/platform/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::rc::Rc;
use crate::platform::macos::view::{BaseviewView, ViewParentingType};
use crate::platform::ParentWindowHandle;
use crate::platform::Result;
use crate::utils::SizingStrategy;
use crate::wrappers::appkit::{create_window, View};
use crate::*;

Expand Down Expand Up @@ -96,7 +97,15 @@ impl WindowHandle {
}

pub fn is_resizable(&self) -> bool {
self.state.resizable
self.state.sizing_strategy.is_resizable()
}

pub fn min_size(&self) -> Option<Size> {
self.state.sizing_strategy.min_size()
}

pub fn max_size(&self) -> Option<Size> {
self.state.sizing_strategy.max_size()
}

#[inline]
Expand Down Expand Up @@ -170,16 +179,16 @@ pub(crate) struct WindowSharedState {
pub closed: Cell<bool>,
pub size: Cell<LogicalSize<f64>>,
pub scale_factor: Cell<f64>,
pub resizable: bool,
pub sizing_strategy: SizingStrategy,
}

impl WindowSharedState {
pub fn new(size: LogicalSize<f64>, scale_factor: f64, resizable: bool) -> Self {
pub fn new(size: LogicalSize<f64>, scale_factor: f64, sizing_strategy: SizingStrategy) -> Self {
Self {
closed: false.into(),
size: size.into(),
scale_factor: scale_factor.into(),
resizable,
sizing_strategy,
}
}
}
Expand Down
54 changes: 43 additions & 11 deletions src/platform/win/window.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
use windows_core::{ComObject, HSTRING};
use windows_sys::Win32::{
Foundation::{LPARAM, LRESULT, RECT, WPARAM},
UI::{
Controls::WM_MOUSELEAVE,
WindowsAndMessaging::{
HTCLIENT, WHEEL_DELTA, WM_CHAR, WM_CLOSE, WM_DPICHANGED, WM_INPUTLANGCHANGE,
WM_KEYDOWN, WM_KEYUP, WM_KILLFOCUS, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MBUTTONDOWN,
WM_MBUTTONUP, WM_MOUSEHWHEEL, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_RBUTTONDOWN,
WM_RBUTTONUP, WM_SETCURSOR, WM_SETFOCUS, WM_SIZE, WM_SYSCHAR, WM_SYSKEYDOWN,
WM_SYSKEYUP, WM_TIMER, WM_USER, WM_XBUTTONDOWN, WM_XBUTTONUP,
},
},
UI::{Controls::WM_MOUSELEAVE, WindowsAndMessaging::*},
};

use crate::{warn, HandlerError};
use dpi::{PhysicalPosition, PhysicalSize, Size};
use std::cell::Cell;
use std::num::NonZeroUsize;
use windows_sys::Win32::Foundation::POINT;

pub(crate) const BV_WINDOW_MUST_CLOSE: u32 = WM_USER + 1;

Expand Down Expand Up @@ -69,7 +61,15 @@ impl WindowHandle {
}

pub fn is_resizable(&self) -> bool {
self.state.resizable
self.state.sizing_strategy.is_resizable()
}

pub fn min_size(&self) -> Option<Size> {
self.state.sizing_strategy.min_size()
}

pub fn max_size(&self) -> Option<Size> {
self.state.sizing_strategy.max_size()
}

pub fn size(&self) -> WindowSize {
Expand Down Expand Up @@ -627,6 +627,38 @@ unsafe fn wnd_proc_inner(
None
}
}
WM_GETMINMAXINFO => {
let sizing = window_state.shared.sizing_strategy;

// Only implement this message if we actually need to specify a min/max size
if let (None, None) = (sizing.min_size(), sizing.max_size()) {
return None;
}

let info = lparam as *mut MINMAXINFO;

let ctx = DpiAwarenessContext::new(&window_state.user32).unwrap();
let style = window.get_style().unwrap();
let dpi = window_state.shared.current_dpi.get();

if let Some(size) = sizing.min_size() {
let size = size.to_physical(window_state.shared.scale_factor());
let size =
ctx.client_area_to_nc_area(size.into(), style, dpi).unwrap().size().cast();
let pt = POINT { x: size.width, y: size.height };
(&raw mut (*info).ptMinTrackSize).write(pt);
}

if let Some(size) = sizing.max_size() {
let size = size.to_physical(window_state.shared.scale_factor());
let size =
ctx.client_area_to_nc_area(size.into(), style, dpi).unwrap().size().cast();
let pt = POINT { x: size.width, y: size.height };
(&raw mut (*info).ptMaxTrackSize).write(pt);
}

Some(0)
}
// NOTE: `WM_NCDESTROY` is handled in the outer function because this deallocates the window
// state
BV_WINDOW_MUST_CLOSE => {
Expand Down
5 changes: 3 additions & 2 deletions src/platform/win/window_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::platform::win::keyboard::KeyboardState;
use crate::platform::PlatformHandle;
use crate::utils::SizingStrategy;
use crate::wrappers::win32::cursor::SystemCursor;
use crate::wrappers::win32::h_instance::HInstance;
use crate::wrappers::win32::window::HWnd;
Expand Down Expand Up @@ -151,7 +152,7 @@ pub struct WindowSharedState {
pub destroy_host_originated: Cell<bool>,

pub user32: ExtendedUser32,
pub resizable: bool,
pub sizing_strategy: SizingStrategy,
}

impl WindowSharedState {
Expand All @@ -164,7 +165,7 @@ impl WindowSharedState {
fallback_scale_factor: settings.fallback_scale_factor.into(),
resize_host_originated: false.into(),
destroy_host_originated: false.into(),
resizable: settings.resizable,
sizing_strategy: SizingStrategy::from_settings(settings),
user32,
}
.into()
Expand Down
21 changes: 12 additions & 9 deletions src/platform/x11/window_shared.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::platform::x11::event_loop::EventLoop;
use crate::platform::x11::visual_info::WindowVisualConfig;
use crate::platform::x11::window_thread::WindowThreadShared;
use crate::platform::x11::xcb_connection::{get_size_hints, WmSizeHintsExt};
use crate::platform::x11::xcb_connection::get_size_hints;
use crate::platform::x11::xcb_window::XcbWindow;
use crate::platform::*;
use crate::utils::SizingStrategy;
use crate::{warn, MouseCursor, WindowHandler, WindowSettings, WindowSize};
use calloop::LoopSignal;
use dpi::{PhysicalSize, Size};
use raw_window_handle::{DisplayHandle, XlibWindowHandle};
use std::cell::Cell;
use std::rc::Rc;
use std::sync::Arc;
use x11rb::properties::WmSizeHints;
use x11rb::protocol::xproto::{ChangeWindowAttributesAux, ConnectionExt, InputFocus, Visualid};
use x11rb::CURRENT_TIME;

Expand Down Expand Up @@ -51,7 +51,7 @@ pub(crate) struct WindowInner {
pub(crate) scaling_factor: ScalingFactor,

window_size: Cell<PhysicalSize<u16>>,
pub(crate) is_resizable: bool,
pub(crate) sizing_strategy: SizingStrategy,
mouse_cursor: Cell<MouseCursor>,
pub(crate) visual_id: Visualid,

Expand All @@ -76,7 +76,9 @@ impl WindowInner {

let physical_size = options.size.to_physical(initial_scale_factor);

let size_hints = get_size_hints(&options, initial_scale_factor);
let sizing_strategy = SizingStrategy::from_settings(&options);

let size_hints = get_size_hints(&sizing_strategy, physical_size, initial_scale_factor);

#[cfg(feature = "opengl")]
let visual_info =
Expand Down Expand Up @@ -127,7 +129,7 @@ impl WindowInner {
system: scaling.into(),
suggested: options.fallback_scale_factor.into(),
},
is_resizable: options.resizable,
sizing_strategy,
mouse_cursor: MouseCursor::default().into(),
loop_signal: ev_loop.get_signal(),

Expand Down Expand Up @@ -193,8 +195,9 @@ impl WindowInner {
let new_physical_size = size.to_physical(self.scaling_factor.get());
self.xcb_window.resize(new_physical_size)?.check()?;

if !self.is_resizable {
let size_hints = WmSizeHints::new().with_fixed_size(new_physical_size.cast());
if !self.sizing_strategy.is_resizable() {
let size_hints =
get_size_hints(&self.sizing_strategy, new_physical_size, self.scale_factor());
self.xcb_window.set_size_hints(size_hints)?.check()?;
}

Expand Down Expand Up @@ -222,8 +225,8 @@ impl WindowInner {
}

self.xcb_window.resize(new_size.cast())?.check()?; // Will not call handler, as size is the same as above.
if !self.is_resizable {
let size_hints = WmSizeHints::new().with_fixed_size(new_size.cast());
if !self.sizing_strategy.is_resizable() {
let size_hints = get_size_hints(&self.sizing_strategy, new_size, self.scale_factor());
self.xcb_window.set_size_hints(size_hints)?.check()?;
}

Expand Down
Loading