Skip to content
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
54 changes: 8 additions & 46 deletions examples/plugin_clack/src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ impl PluginGuiImpl for ExamplePluginMainThread<'_> {
return None;
};

Some(window_size_to_gui_size(gui.handle.size()))
let size = gui.handle.size().to_native_size();

Some(GuiSize { width: size.width, height: size.height })
}

fn can_resize(&mut self) -> bool {
Expand All @@ -96,13 +98,13 @@ impl PluginGuiImpl for ExamplePluginMainThread<'_> {
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);
let max_size = NativeSize::from_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);
let min_size = NativeSize::from_size(min_size, scale_factor);
size.width = size.width.max(min_size.width);
size.height = size.height.max(min_size.height);
}
Expand All @@ -115,8 +117,7 @@ impl PluginGuiImpl for ExamplePluginMainThread<'_> {
return Err(PluginError::Message("set_size called without a GUI active"));
};

let size = gui_size_to_window_size(size);
gui.handle.resize(size)?;
gui.handle.resize(NativeSize { width: size.width, height: size.height })?;

Ok(())
}
Expand Down Expand Up @@ -162,45 +163,6 @@ 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")]
{
let size = size.logical.cast();
GuiSize { width: size.width, height: size.height }
}

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

fn gui_size_to_window_size(size: GuiSize) -> Size {
#[cfg(target_os = "macos")]
{
Size::Logical(LogicalSize::new(size.width, size.height).cast())
}
#[cfg(not(target_os = "macos"))]
{
Size::Physical(PhysicalSize::new(size.width, size.height))
}
}

struct MainThreadHandler {
host: HostSharedHandle<'static>,
}
Expand All @@ -218,8 +180,8 @@ struct HostGuiCallbacks {

impl HostCallbacks for HostGuiCallbacks {
fn request_resize(&mut self, new_size: WindowSize) -> Result<(), HandlerError> {
let size = window_size_to_gui_size(new_size);
self.ext.request_resize(&self.host, size.width, size.height)?;
let new_size = new_size.to_native_size();
self.ext.request_resize(&self.host, new_size.width, new_size.height)?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use crate::dpi::Size;
use crate::{platform, MouseCursor, WindowSize};
use dpi::Size;
use raw_window_handle::{
DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, WindowHandle,
};
Expand Down
99 changes: 99 additions & 0 deletions src/dpi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use crate::WindowSize;
pub use dpi::*;

/// A size represented in the platform's native pixels.
///
/// This size is represented in physical pixels on Windows and Linux, and in logical pixels on macOS.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct NativeSize<P> {
pub width: P,
pub height: P,
}

impl<P> NativeSize<P> {
#[inline]
pub const fn new(width: P, height: P) -> Self {
NativeSize { width, height }
}
}

impl<P: Pixel> NativeSize<P> {
#[inline]
pub fn from_size(size: Size, scale_factor: f64) -> Self {
#[cfg(target_os = "macos")]
{
let size = size.to_logical(scale_factor);
Self { width: size.width, height: size.height }
}

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

#[inline]
pub fn cast<X: Pixel>(&self) -> NativeSize<X> {
NativeSize { width: self.width.cast(), height: self.height.cast() }
}

#[inline]
pub fn to_physical(self, scale_factor: f64) -> PhysicalSize<P> {
#[cfg(target_os = "macos")]
{
let size = LogicalSize { width: self.width, height: self.height };
size.to_physical(scale_factor)
}
#[cfg(not(target_os = "macos"))]
{
let _ = scale_factor;
PhysicalSize { width: self.width, height: self.height }
}
}

#[inline]
pub fn to_logical(self, scale_factor: f64) -> LogicalSize<P> {
#[cfg(target_os = "macos")]
{
let _ = scale_factor;
LogicalSize { width: self.width, height: self.height }
}
#[cfg(not(target_os = "macos"))]
{
let size = PhysicalSize { width: self.width, height: self.height };
size.to_logical(scale_factor)
}
}
}

impl<P: Pixel> From<WindowSize> for NativeSize<P> {
#[inline]
fn from(size: WindowSize) -> Self {
#[cfg(target_os = "macos")]
{
let size = size.logical.cast();
Self { width: size.width, height: size.height }
}

#[cfg(not(target_os = "macos"))]
{
let size = size.physical.cast();
Self { width: size.width, height: size.height }
}
}
}

impl<P: Pixel> From<NativeSize<P>> for Size {
#[inline]
fn from(size: NativeSize<P>) -> Self {
#[cfg(target_os = "macos")]
{
Size::Logical(LogicalSize::new(size.width, size.height).cast())
}
#[cfg(not(target_os = "macos"))]
{
Size::Physical(PhysicalSize::new(size.width, size.height).cast())
}
}
}
2 changes: 1 addition & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dpi::PhysicalPosition;
use crate::dpi::PhysicalPosition;
use keyboard_types::{KeyboardEvent, Modifiers};
use std::path::PathBuf;

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod clipboard;
mod context;
pub mod dpi;
mod error;
mod event;
mod handler;
Expand All @@ -17,7 +18,6 @@ pub mod gl;

pub use clipboard::*;
pub use context::{PlatformHandle, WindowContext};
pub use dpi;
pub use error::*;
pub use event::*;
pub use handler::WindowHandler;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/macos/context.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::dpi::Size;
use crate::platform::macos::cursor::Cursor;
use crate::platform::macos::view::BaseviewView;
use crate::platform::Result;
use crate::platform::{PlatformHandle, WindowSharedState};
use crate::wrappers::appkit::{View, ViewRef};
use crate::*;
use dispatch2::MainThreadBound;
use dpi::Size;
use objc2::rc::Weak;
use objc2::runtime::NSObjectProtocol;
use objc2::{MainThreadMarker, Message};
Expand Down
2 changes: 1 addition & 1 deletion src/platform/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use super::keyboard::{make_modifiers, KeyboardState};
use super::window::WindowSharedState;
use crate::dpi::{LogicalPosition, LogicalSize, Size};
use crate::host::Host;
use crate::platform::*;
use crate::tracing::warn;
Expand All @@ -13,7 +14,6 @@ use crate::{
DropData, DropEffect, Event, EventStatus, MouseButton, MouseEvent, ScrollDelta, WindowEvent,
WindowHandler, WindowSize,
};
use dpi::{LogicalPosition, LogicalSize, Size};
use objc2::__framework_prelude::Retained;
use objc2::rc::Weak;
use objc2::runtime::{NSObjectProtocol, ProtocolObject};
Expand Down
2 changes: 1 addition & 1 deletion src/platform/macos/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dpi::{LogicalSize, Size};
use crate::dpi::{LogicalSize, Size};
use objc2::rc::{autoreleasepool, Retained, Weak};
use objc2::MainThreadMarker;
use objc2_app_kit::{NSApplication, NSPasteboard, NSPasteboardTypeString, NSView, NSWindow};
Expand Down
2 changes: 1 addition & 1 deletion src/platform/win/drop_target.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![expect(clippy::indexing_slicing, reason = "To be refactored later")]

use dpi::PhysicalPosition;
use crate::dpi::PhysicalPosition;
use std::cell::{Cell, RefCell};
use std::ffi::OsString;
use std::os::windows::prelude::OsStringExt;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/win/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use windows_sys::Win32::{
UI::{Controls::WM_MOUSELEAVE, WindowsAndMessaging::*},
};

use crate::dpi::{PhysicalPosition, PhysicalSize, Size};
use crate::{warn, EventStatus, HandlerError, WindowHandler};
use dpi::{PhysicalPosition, PhysicalSize, Size};
use std::cell::{Cell, OnceCell};
use std::num::NonZeroUsize;
use windows_sys::Win32::Foundation::POINT;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/win/window_state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::dpi::{PhysicalSize, Size};
use crate::platform::win::keyboard::KeyboardState;
use crate::platform::PlatformHandle;
use crate::utils::SizingStrategy;
Expand All @@ -7,7 +8,6 @@ use crate::wrappers::win32::window::HWnd;
use crate::wrappers::win32::{Dpi, ExtendedUser32};
use crate::WindowSettings;
use crate::{MouseCursor, WindowSize};
use dpi::{PhysicalSize, Size};
use raw_window_handle::{DisplayHandle, Win32WindowHandle};
use std::cell::{Cell, Ref, RefCell};
use std::num::NonZeroIsize;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/x11/drag_n_drop.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::xcb_connection::{Atoms, GetPropertyError};
use super::*;
use crate::dpi::PhysicalPosition;
use crate::handler::WindowHandler;
use crate::platform::x11::error::ReplyExt;
use crate::warn;
use crate::{DropData, Event, MouseEvent};
use core::result::Result;
use dpi::PhysicalPosition;
use keyboard_types::Modifiers;
use percent_encoding::percent_decode;
use std::error::Error;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/x11/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::keyboard::{convert_key_press_event, convert_key_release_event, key_mo
use super::*;
use std::result::Result;

use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::host::HostMainThreadCaller;
use crate::platform::x11::error::FatalError;
use crate::platform::x11::window_thread::{
Expand All @@ -14,7 +15,6 @@ use crate::{Event, MouseButton, MouseEvent, ScrollDelta, WindowEvent, WindowHand
use calloop::generic::Generic;
use calloop::timer::{TimeoutAction, Timer};
use calloop::{Interest, LoopHandle, LoopSignal, Mode, PostAction};
use dpi::{PhysicalPosition, PhysicalSize};
use std::rc::Rc;
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/x11/window_shared.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::dpi::{PhysicalSize, Size};
use crate::platform::x11::event_loop::EventLoop;
use crate::platform::x11::visibility_tree::AncestorVisibilityState;
use crate::platform::x11::visual_info::WindowVisualConfig;
Expand All @@ -8,7 +9,6 @@ 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;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/x11/window_thread.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::dpi::{PhysicalSize, Size};
use crate::handler::WindowHandlerBuilder;
use crate::host::HostCallbacks;
use crate::platform::x11::event_loop::{EventLoop, MainThreadCaller};
Expand All @@ -8,7 +9,6 @@ use crate::warn;
use crate::window::WindowInitializer;
use crate::{WindowContext, WindowSettings, WindowSize};
use calloop::LoopSignal;
use dpi::{PhysicalSize, Size};
use std::cell::{Cell, RefCell};
use std::panic::resume_unwind;
use std::rc::Rc;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/x11/xcb_window.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::dpi::PhysicalSize;
use crate::platform::x11::error::CookieExt;
use crate::platform::x11::visual_info::WindowVisualConfig;
use crate::platform::X11Connection;
use dpi::PhysicalSize;
use std::num::{NonZero, NonZeroU32};
use std::rc::Rc;
use x11rb::connection::Connection;
Expand Down
2 changes: 1 addition & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::dpi::{LogicalSize, Size};
#[cfg(feature = "opengl")]
use crate::gl::GlConfig;
use crate::platform;
use dpi::{LogicalSize, Size};
use raw_window_handle::HasWindowHandle;

/// Settings used when creating a new window.
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::dpi::Size;
use crate::WindowSettings;
use dpi::Size;

#[derive(Copy, Clone)]
pub(crate) enum SizingStrategy {
Expand Down
7 changes: 6 additions & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::dpi::*;
use crate::handler::WindowHandlerBuilder;
use crate::host::Host;
use crate::platform;
use crate::*;
use dpi::{LogicalSize, PhysicalSize, Pixel, Size};
use std::marker::PhantomData;

/// A handle to a Window created by baseview.
Expand Down Expand Up @@ -262,6 +262,11 @@ impl WindowSize {
pub fn from_logical(logical: LogicalSize<f64>, scale_factor: f64) -> Self {
Self { physical: logical.to_physical(scale_factor), logical, scale_factor }
}

#[inline]
pub fn to_native_size<P: Pixel>(&self) -> NativeSize<P> {
(*self).into()
}
}

impl<P: Pixel> From<WindowSize> for PhysicalSize<P> {
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/appkit/view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dpi::LogicalSize;
use crate::dpi::LogicalSize;
use objc2::__framework_prelude::{Allocated, AnyClass, ProtocolObject, Retained};
use objc2::rc::Weak;
use objc2::runtime::{AnyObject, Ivar};
Expand Down
Loading