Skip to content

Commit

Permalink
#791 Try to fix second Windows 7/8 compatibility issue
Browse files Browse the repository at this point in the history
  • Loading branch information
helgoboss committed Jan 13, 2023
1 parent e4e7a73 commit 68c5243
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion main/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "realearn"
version = "2.14.2"
version = "2.14.3"
authors = ["Benjamin Klum <[email protected]>"]
edition = "2021"
build = "build.rs"
Expand Down
1 change: 1 addition & 0 deletions swell-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ raw-window-handle = "0.4.2"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winuser", "uxtheme"] }
palette = "0.5"
libloading = "0.7"

[target.'cfg(target_os = "macos")'.dependencies]
objc2 = "0.3.0-beta.3"
Expand Down
3 changes: 3 additions & 0 deletions swell-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ pub mod menu_tree;

#[cfg(target_os = "macos")]
mod macos;

#[cfg(target_os = "windows")]
mod win;
29 changes: 29 additions & 0 deletions swell-ui/src/win.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use libloading::{Library, Symbol};
use winapi::shared::minwindef::UINT;
use winapi::shared::windef::HWND;

/// Provides access to some Win32 API functions that are not available in older Windows versions.
///
/// This is better than eagerly linking to these functions because then the resulting binary
/// wouldn't work *at all* in the older Windows versions, whereas with this approach, we can
/// fall back to alternative logic or alternative values on a case-by-case basis.
pub struct DynamicWinApi {
user32_library: Library,
}

impl DynamicWinApi {
pub fn load() -> Self {
unsafe {
Self {
user32_library: Library::new("user32.dll").unwrap(),
}
}
}

/// Should be available from Windows 10 onwards.
pub fn get_dpi_for_window(&self) -> Option<Symbol<GetDpiForWindow>> {
unsafe { self.user32_library.get(b"GetDpiForWindow").ok() }
}
}

type GetDpiForWindow = extern "stdcall" fn(hwnd: HWND) -> UINT;
7 changes: 6 additions & 1 deletion swell-ui/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,12 @@ impl Window {
pub fn dpi(&self) -> u32 {
#[cfg(target_family = "windows")]
{
unsafe { winapi::um::winuser::GetDpiForWindow(self.raw as _) }
let dynamic_win_api = crate::win::DynamicWinApi::load();
if let Some(f) = dynamic_win_api.get_dpi_for_window() {
f(self.raw as _)
} else {
96
}
}
#[cfg(target_family = "unix")]
96
Expand Down

0 comments on commit 68c5243

Please sign in to comment.