Skip to content

Commit

Permalink
win32: set DPI awareness when AppMode is Owner
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Sep 27, 2023
1 parent 29ec6d2 commit afb3d69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/backend/win32/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::dpi::DpiFns;
use super::timer::{TimerHandleInner, Timers};
use super::window::wnd_proc;
use super::{class_name, hinstance, to_wstring, OsError};
use crate::{App, AppContext, AppOptions, Error, IntoInnerError, Result};
use crate::{App, AppContext, AppMode, AppOptions, Error, IntoInnerError, Result};

pub struct AppState {
pub class: u16,
Expand All @@ -39,7 +39,7 @@ pub struct AppInner<T> {
}

impl<T: 'static> AppInner<T> {
pub fn new<F>(_options: &AppOptions, build: F) -> Result<AppInner<T>>
pub fn new<F>(options: &AppOptions, build: F) -> Result<AppInner<T>>
where
F: FnOnce(&AppContext<T>) -> Result<T>,
T: 'static,
Expand Down Expand Up @@ -71,6 +71,9 @@ impl<T: 'static> AppInner<T> {
};

let dpi = DpiFns::load();
if options.mode == AppMode::Owner {
dpi.set_dpi_aware();
}

let timers = Timers::new()?;

Expand Down
23 changes: 21 additions & 2 deletions src/backend/win32/dpi.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::mem;

use windows_sys::core::HRESULT;
use windows_sys::Win32::Foundation::{BOOL, HWND, RECT};
use windows_sys::Win32::Foundation::{BOOL, FALSE, HWND, RECT};
use windows_sys::Win32::Graphics::Gdi::HMONITOR;
use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA};
use windows_sys::Win32::UI::HiDpi::{
DPI_AWARENESS_CONTEXT, MONITOR_DPI_TYPE, PROCESS_DPI_AWARENESS,
DPI_AWARENESS_CONTEXT, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE,
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, MONITOR_DPI_TYPE, PROCESS_DPI_AWARENESS,
PROCESS_PER_MONITOR_DPI_AWARE,
};
use windows_sys::Win32::UI::WindowsAndMessaging::{WINDOW_EX_STYLE, WINDOW_STYLE};

Expand Down Expand Up @@ -70,4 +72,21 @@ impl DpiFns {
}
}
}

pub fn set_dpi_aware(&self) {
#[allow(non_snake_case)]
unsafe {
if let Some(SetProcessDpiAwarenessContext) = self.SetProcessDpiAwarenessContext {
let res = SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);

if res == FALSE {
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
}
} else if let Some(SetProcessDpiAwareness) = self.SetProcessDpiAwareness {
SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
} else if let Some(SetProcessDPIAware) = self.SetProcessDPIAware {
SetProcessDPIAware();
}
}
}
}

0 comments on commit afb3d69

Please sign in to comment.