Skip to content

Commit

Permalink
Attempt to fix windows build after upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
vv9k committed Sep 1, 2022
1 parent 5894e55 commit 1997b2f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 80 deletions.
50 changes: 25 additions & 25 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ once_cell = "1"
x11rb = { version = "0.9", features = ["image", "cursor", "resource_manager"] }

[target.'cfg(windows)'.dependencies]
windows = "0.32"

[target.'cfg(windows)'.build-dependencies]
windows = "0.32"
windows = { version = "0.39", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_System_LibraryLoader", "Win32_UI_WindowsAndMessaging"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
native-dialog = "0.6"
Expand Down
15 changes: 0 additions & 15 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
fn main() {
#[cfg(windows)]
windows::build! {
Windows::Win32::Foundation::{HINSTANCE, LPARAM, LRESULT, POINT, PWSTR, WPARAM, HWND},
Windows::Win32::Graphics::Gdi::{
BitBlt, CreateCompatibleBitmap, CreateCompatibleDC, DeleteDC, GetDC, GetPixel, ReleaseDC,
SelectObject, SetStretchBltMode, UpdateWindow, CLR_INVALID, HBITMAP, HDC,
StretchBlt, Rectangle
},
Windows::Win32::System::LibraryLoader::GetModuleHandleW,
Windows::Win32::UI::WindowsAndMessaging::{
CreateWindowExW, DefWindowProcW, DestroyWindow, GetCursorPos, GetDesktopWindow, MoveWindow,
SHOW_WINDOW_CMD, WINDOW_EX_STYLE, WINDOW_STYLE, RegisterClassExW, ShowWindow, WNDCLASSEXW,
}
}

#[cfg(target_os = "macos")]
{
println!("cargo:rustc-link-lib=framework=Foundation");
Expand Down
2 changes: 1 addition & 1 deletion src/display_picker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn init_display_picker() -> Option<Rc<dyn DisplayPickerExt>> {
.ok()
.map(|conn| Rc::new(conn) as Rc<dyn DisplayPickerExt>);
#[cfg(windows)]
return Some(Rc::new(windows::WinConn::new()) as Rc<dyn DisplayPickerExt>);
return Some(Rc::new(windows::WinConn::new().ok()?) as Rc<dyn DisplayPickerExt>);
#[cfg(target_os = "macos")]
return Some(Rc::new(macos::MacConn) as Rc<dyn DisplayPickerExt>);
#[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
Expand Down
59 changes: 32 additions & 27 deletions src/display_picker/windows.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
#![cfg(windows)]

windows::include_bindings!();

use std::ptr::{null, null_mut};
use std::ptr::null;

use crate::color::Color;
use crate::display_picker::DisplayPicker;
use anyhow::{Error, Result};
use anyhow::{Context, Error, Result};
use egui::Color32;
use Windows::Win32::Foundation::{HINSTANCE, LPARAM, LRESULT, POINT, PWSTR, WPARAM};
use Windows::Win32::Graphics::Gdi::{
BitBlt, CreateCompatibleBitmap, CreateCompatibleDC, DeleteDC, GetDC, GetPixel, Rectangle,
ReleaseDC, SelectObject, SetStretchBltMode, StretchBlt, UpdateWindow, CLR_INVALID,
COLORONCOLOR, HBITMAP, HDC, SRCCOPY,
};
use Windows::Win32::System::LibraryLoader::GetModuleHandleW;
use Windows::Win32::UI::WindowsAndMessaging::{
CreateWindowExW, DefWindowProcW, DestroyWindow, GetCursorPos, GetDesktopWindow, MoveWindow,
RegisterClassExW, ShowWindow, WNDCLASSEXW,
use windows::{
core::PCWSTR,
Win32::Foundation::{HINSTANCE, LPARAM, LRESULT, POINT, WPARAM},
Win32::Graphics::Gdi::{
BitBlt, CreateCompatibleBitmap, CreateCompatibleDC, DeleteDC, GetDC, GetPixel, Rectangle,
ReleaseDC, SelectObject, SetStretchBltMode, StretchBlt, UpdateWindow, CLR_INVALID,
COLORONCOLOR, HBITMAP, HDC, SRCCOPY,
},
Win32::System::LibraryLoader::GetModuleHandleW,
Win32::UI::WindowsAndMessaging::{
CreateWindowExW, DefWindowProcW, DestroyWindow, GetCursorPos, GetDesktopWindow, MoveWindow,
RegisterClassExW, ShowWindow, WNDCLASSEXW,
},
};

pub use Windows::Win32::Foundation::HWND;
pub use Windows::Win32::UI::WindowsAndMessaging::{
SHOW_WINDOW_CMD, SW_SHOWDEFAULT, WINDOW_EX_STYLE, WINDOW_STYLE, WS_BORDER, WS_POPUP,
pub use windows::{
Win32::Foundation::HWND,
Win32::UI::WindowsAndMessaging::{
SHOW_WINDOW_CMD, SW_SHOWDEFAULT, WINDOW_EX_STYLE, WINDOW_STYLE, WS_BORDER, WS_POPUP,
},
};

macro_rules! handle_winapi_call {
Expand Down Expand Up @@ -102,11 +105,13 @@ pub struct WinConn {
}

impl WinConn {
pub fn new() -> Self {
WinConn {
pub fn new() -> Result<Self> {
Ok(WinConn {
device_context: unsafe { GetDC(None) },
hinstance: unsafe { GetModuleHandleW(PWSTR(null_mut())) },
}
hinstance: unsafe {
GetModuleHandleW(PCWSTR::null()).context("failed to get module handle")?
},
})
}

pub fn get_cursor_pos(&self) -> Result<POINT> {
Expand All @@ -121,7 +126,7 @@ impl WinConn {
}

pub fn get_pixel(&self, pos: POINT) -> Result<u32> {
let color = unsafe { GetPixel(&self.device_context, pos.x, pos.y) };
let color = unsafe { GetPixel(self.device_context, pos.x, pos.y) };
if color == CLR_INVALID {
return Err(Error::msg("failed to get pixel"));
}
Expand Down Expand Up @@ -164,25 +169,25 @@ impl DisplayPickerExt for WinConn {
height: i32,
style: WINDOW_STYLE,
) -> Result<HWND> {
let mut class_name = class_name.encode_utf16().collect::<Vec<_>>();
let class_name = class_name.encode_utf16().collect::<Vec<_>>();

let mut class = WNDCLASSEXW {
cbSize: std::mem::size_of::<WNDCLASSEXW>() as u32,
..Default::default()
};
class.lpszClassName = PWSTR(class_name.as_mut_ptr());
class.lpszClassName = PCWSTR::from_raw(class_name.as_ptr());
class.hInstance = self.hinstance;
class.lpfnWndProc = Some(wnd_proc);

let mut window_name = window_name.encode_utf16().collect::<Vec<_>>();
let window_name = window_name.encode_utf16().collect::<Vec<_>>();

unsafe { RegisterClassExW(&class as *const WNDCLASSEXW) };

unsafe {
Ok(CreateWindowExW(
WINDOW_EX_STYLE(0),
PWSTR(class_name.as_mut_ptr()),
PWSTR(window_name.as_mut_ptr()),
PCWSTR::from_raw(class_name.as_ptr()),
PCWSTR::from_raw(window_name.as_ptr()),
style,
x,
y,
Expand Down
17 changes: 9 additions & 8 deletions src/zoom_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ use x11rb::protocol::xproto;

#[cfg(windows)]
use crate::display_picker::windows::{HWND, SW_SHOWDEFAULT, WS_BORDER, WS_POPUP};

#[cfg(any(target_os = "linux"))]
const ZOOM_IMAGE_WIDTH: u16 = ZOOM_WIN_WIDTH / ZOOM_SCALE as u16;
#[cfg(any(target_os = "linux"))]
const ZOOM_IMAGE_HEIGHT: u16 = ZOOM_WIN_HEIGHT / ZOOM_SCALE as u16;
#[cfg(any(target_os = "linux"))]
const ZOOM_WIN_BORDER_WIDTH: u32 = 2;
#[cfg(any(target_os = "linux", windows))]
static CURSOR_PICKER_WINDOW_NAME: &str = "epick - cursor picker";
#[cfg(any(target_os = "linux", windows))]
Expand All @@ -24,18 +31,12 @@ const ZOOM_WIN_WIDTH: u16 = 160;
#[cfg(any(target_os = "linux", windows))]
const ZOOM_WIN_HEIGHT: u16 = 160;
#[cfg(any(target_os = "linux", windows))]
const ZOOM_IMAGE_WIDTH: u16 = ZOOM_WIN_WIDTH / ZOOM_SCALE as u16;
#[cfg(any(target_os = "linux", windows))]
const ZOOM_IMAGE_HEIGHT: u16 = ZOOM_WIN_HEIGHT / ZOOM_SCALE as u16;
#[cfg(any(target_os = "linux", windows))]
const ZOOM_WIN_OFFSET: i32 = 50;
#[cfg(any(target_os = "linux", windows))]
const ZOOM_WIN_POINTER_DIAMETER: u16 = 10;
#[cfg(windows)]
const ZOOM_WIN_POINTER_RADIUS: u16 = ZOOM_WIN_POINTER_DIAMETER / 2;
#[cfg(any(target_os = "linux", windows))]
const ZOOM_WIN_BORDER_WIDTH: u32 = 2;
#[cfg(any(target_os = "linux", windows))]
const ZOOM_IMAGE_X_OFFSET: i32 = ((ZOOM_WIN_WIDTH / 2) as f32 / ZOOM_SCALE) as i32;
#[cfg(any(target_os = "linux", windows))]
const ZOOM_IMAGE_Y_OFFSET: i32 = ((ZOOM_WIN_HEIGHT / 2) as f32 / ZOOM_SCALE) as i32;
Expand Down Expand Up @@ -105,8 +106,8 @@ impl ZoomPicker {
if let Ok(window) = picker.spawn_window(
"EPICK_DIALOG",
CURSOR_PICKER_WINDOW_NAME,
(cursor_pos.0 - ZOOM_IMAGE_X_OFFSET),
(cursor_pos.1 - ZOOM_IMAGE_Y_OFFSET),
cursor_pos.0 - ZOOM_IMAGE_X_OFFSET,
cursor_pos.1 - ZOOM_IMAGE_Y_OFFSET,
ZOOM_WIN_WIDTH as i32,
ZOOM_WIN_HEIGHT as i32,
WS_POPUP | WS_BORDER,
Expand Down

0 comments on commit 1997b2f

Please sign in to comment.