Skip to content
Closed
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
14 changes: 13 additions & 1 deletion examples/gain-plugin/src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::rc::Rc;

use plinth_plugin::{raw_window_handle::RawWindowHandle, Editor, Host};
use plugin_canvas_slint::{editor::{EditorHandle, SlintEditor}, plugin_canvas::window::WindowAttributes};
use plugin_canvas_slint::{editor::{EditorHandle, SlintEditor}, plugin_canvas::{Window, window::WindowAttributes}};

use crate::{parameters::GainParameters, view::GainPluginView};

Expand Down Expand Up @@ -48,6 +48,18 @@ impl Editor for GainPluginEditor {
self.editor_handle = None;
}

fn scale(&self) -> f64 {
if let Some(editor_handle) = self.editor_handle.as_ref() {
editor_handle.scale()
} else {
Window::sceen_scale()
}
}

fn set_scale(&mut self, _scale: f64) {
// ignore
}

fn on_frame(&mut self) {
if let Some(editor_handle) = self.editor_handle.as_ref() {
editor_handle.on_frame();
Expand Down
13 changes: 12 additions & 1 deletion plinth-plugin/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ pub trait Editor {
fn open(&mut self, parent: RawWindowHandle);
fn close(&mut self);

/// Get applied window scale. This should be the total applied scale: os window scale * custom window scales, if any
fn scale(&self) -> f64 { 1.0 }

/// Returns current window size
fn window_size(&self) -> (f64, f64) {
Self::DEFAULT_SIZE
if cfg!(target_os = "windows") {
// on windows, window sizes are physical sizes
let (width, height) = Self::DEFAULT_SIZE;
let scale = self.scale();
(width * scale, height * scale)
}
else {
Self::DEFAULT_SIZE
}
}

fn can_resize(&self) -> bool {
Expand Down
8 changes: 5 additions & 3 deletions plugin-canvas-slint/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::rc::{Rc, Weak};
use std::sync::Arc;

use raw_window_handle::RawWindowHandle;
use plugin_canvas::{event::EventResponse, window::WindowAttributes, Event};
use plugin_canvas::{event::EventResponse, window::WindowAttributes, Event, Window};
use slint::platform::WindowAdapter;

use crate::{platform::PluginCanvasPlatform, view::PluginView, window_adapter::{PluginCanvasWindowAdapter, WINDOW_ADAPTER_FROM_SLINT, WINDOW_TO_SLINT}};
Expand Down Expand Up @@ -87,9 +87,11 @@ impl EditorHandle {
}
}

pub fn set_scale(&self, scale: f64) {
pub fn scale(&self) -> f64 {
if let Some(window_adapter) = self.window_adapter() {
window_adapter.set_scale(scale);
window_adapter.scale()
} else {
Window::sceen_scale()
}
}

Expand Down
14 changes: 4 additions & 10 deletions plugin-canvas-slint/src/window_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl PluginCanvasWindowAdapter {
let window_attributes = plugin_canvas_window.attributes();

let scale = window_attributes.scale();
let combined_scale = scale * plugin_canvas_window.os_scale();
let combined_scale = scale * plugin_canvas_window.scale();
let plugin_canvas_size = window_attributes.size() * combined_scale;

let slint_size = slint::PhysicalSize {
Expand Down Expand Up @@ -95,14 +95,8 @@ impl PluginCanvasWindowAdapter {
*self.view.borrow_mut() = Some(view);
}

pub fn set_scale(&self, scale: f64) {
self.scale.store(scale, Ordering::Release);

let combined_scale = scale * self.plugin_canvas_window.os_scale();

self.slint_window.dispatch_event(
WindowEvent::ScaleFactorChanged { scale_factor: combined_scale as f32 }
);
pub fn scale(&self) -> f64 {
self.scale.load(Ordering::Relaxed) * self.plugin_canvas_window.scale()
}

pub fn close(&self) {
Expand Down Expand Up @@ -342,7 +336,7 @@ impl WindowAdapter for PluginCanvasWindowAdapter {

fn set_size(&self, size: slint::WindowSize) {
let scale = self.scale.load(Ordering::Acquire);
let os_scale = self.plugin_canvas_window.os_scale();
let os_scale = self.plugin_canvas_window.scale();

let physical_size = size.to_physical(os_scale as _);
let logical_size = size.to_logical(os_scale as _);
Expand Down
3 changes: 2 additions & 1 deletion plugin-canvas/src/platform/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub(crate) trait OsWindowInterface: HasDisplayHandle + HasWindowHandle + Sized {
event_callback: Box<EventCallback>,
) -> Result<OsWindowHandle, Error>;

fn os_scale(&self) -> f64;
fn sceen_scale() -> f64;
fn scale(&self) -> f64;

fn resized(&self, size: LogicalSize);

Expand Down
15 changes: 14 additions & 1 deletion plugin-canvas/src/platform/mac/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,20 @@ impl OsWindowInterface for OsWindow {
Ok(OsWindowHandle::new(window))
}

fn os_scale(&self) -> f64 {
fn sceen_scale() -> f64 {
if let Some(main_thread_marker) = MainThreadMarker::new() {
if let Some(screen) = NSScreen::mainScreen(main_thread_marker) {
return screen.backingScaleFactor()
}
}
else {
#[cfg(debug_assertions)]
panic!("Calling screen_scale from an unexpected thread");
}
1.0
}
Comment on lines +116 to +127
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about to simplify this to:

Suggested change
fn sceen_scale() -> f64 {
if let Some(main_thread_marker) = MainThreadMarker::new() {
if let Some(screen) = NSScreen::mainScreen(main_thread_marker) {
return screen.backingScaleFactor()
}
}
else {
#[cfg(debug_assertions)]
panic!("Calling screen_scale from an unexpected thread");
}
1.0
}
fn screen_scale() -> f64 {
let Some(main_thread_marker) = MainThreadMarker::new() else {
debug_assert!(false, "Calling screen_scale from an unexpected thread");
return 1.0;
};
NSScreen::mainScreen(main_thread_marker)
.map_or(1.0, |screen| screen.backingScaleFactor())
}


fn scale(&self) -> f64 {
self.view()
.window()
.map(|window| window.backingScaleFactor())
Expand Down
2 changes: 1 addition & 1 deletion plugin-canvas/src/platform/win32/drop_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl DropTarget {
return LogicalPosition::default();
};

let scale = window.os_scale();
let scale = window.scale();

// It looks like MapWindowPoints isn't DPI aware (and neither is ScreenToClient),
// so we need to pre-scale the point here?
Expand Down
30 changes: 25 additions & 5 deletions plugin-canvas/src/platform/win32/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cursor_icon::CursorIcon;
use keyboard_types::Code;
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle, Win32WindowHandle};
use uuid::Uuid;
use windows::Win32::Graphics::Gdi::{GetDC, GetDeviceCaps, LOGPIXELSX, LOGPIXELSY, ReleaseDC};
use windows::{core::PCWSTR, Win32::UI::Input::KeyboardAndMouse::{VK_LWIN, VK_RWIN}};
use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, POINT, WPARAM};
use windows::Win32::Graphics::{Dwm::{DwmFlush, DwmIsCompositionEnabled}, Dxgi::{CreateDXGIFactory, IDXGIFactory, IDXGIOutput}, Gdi::{ClientToScreen, MonitorFromWindow, ScreenToClient, HBRUSH, MONITOR_DEFAULTTOPRIMARY}};
Expand Down Expand Up @@ -33,6 +34,20 @@ pub struct OsWindow {
keyboard_modifiers: RefCell<KeyboardModifiers>,
}

fn window_scale(hwnd: Option<HWND>) -> f64 {
// Could use `GetDpiForWindow` here, but that's available for Windows 10 only
unsafe {
let hdc = GetDC(hwnd);
if !hdc.is_invalid() {
let dpi = GetDeviceCaps(Some(hdc), LOGPIXELSX).min(GetDeviceCaps(Some(hdc), LOGPIXELSY));
ReleaseDC(hwnd, hdc);
dpi.max(96) as f64 / 96.0
} else {
1.0
}
}
}

impl OsWindow {
pub(super) fn send_event(&self, event: Event) -> EventResponse {
(self.event_callback)(event)
Expand All @@ -59,7 +74,7 @@ impl OsWindow {
}

fn logical_mouse_position(&self, lparam: LPARAM) -> LogicalPosition {
let scale = self.os_scale();
let scale = self.scale();

PhysicalPosition {
x: (lparam.0 & 0xFFFF) as i16 as i32,
Expand Down Expand Up @@ -100,7 +115,8 @@ impl OsWindowInterface for OsWindow {
};

let class_name = to_wstr("plugin-canvas-".to_string() + &Uuid::new_v4().simple().to_string());
let size = Size::with_logical_size(window_attributes.size, window_attributes.scale);
let os_scale = window_scale(Some(HWND(parent_window_handle.hwnd.get() as _)));
let size = Size::with_logical_size(window_attributes.size, window_attributes.scale * os_scale);

let cursor = unsafe { LoadCursorW(None, IDC_ARROW).unwrap() };

Expand Down Expand Up @@ -209,8 +225,12 @@ impl OsWindowInterface for OsWindow {
Ok(OsWindowHandle::new(window))
}

fn os_scale(&self) -> f64 {
1.0
fn sceen_scale() -> f64 {
window_scale(None)
}

fn scale(&self) -> f64 {
window_scale(Some(self.hwnd()))
}

fn resized(&self, size: LogicalSize) {
Expand Down Expand Up @@ -271,7 +291,7 @@ impl OsWindowInterface for OsWindow {
}

fn warp_mouse(&self, position: LogicalPosition) {
let scale = self.os_scale();
let scale = self.scale();
let physical_position = position.to_physical(scale);

let mut point = POINT {
Expand Down
8 changes: 7 additions & 1 deletion plugin-canvas/src/platform/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,13 @@ impl OsWindowInterface for OsWindow {
Ok(OsWindowHandle::new(Arc::new(window.into())))
}

fn os_scale(&self) -> f64 {
fn sceen_scale() -> f64 {
// TODO: implement me
return 1.0
}

fn scale(&self) -> f64 {
// TODO: implement me
1.0
}

Expand Down
12 changes: 8 additions & 4 deletions plugin-canvas/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ impl Window {
})
}

pub fn attributes(&self) -> &WindowAttributes {
&self.attributes
pub fn sceen_scale() -> f64 {
OsWindow::sceen_scale()
}

pub fn scale(&self) -> f64 {
self.os_window_handle.scale()
}

pub fn os_scale(&self) -> f64 {
self.os_window_handle.os_scale()
pub fn attributes(&self) -> &WindowAttributes {
&self.attributes
}

pub fn resized(&self, size: LogicalSize) {
Expand Down