Skip to content

Commit

Permalink
Fix: #239 Update webview2-com and windows crates (#240)
Browse files Browse the repository at this point in the history
* Replace webivew2-com-sys with prebuilt windows

* Use windows utility instead of direct GetLastError

* Bump windows version and add changelog

* Run cargo fmt

* Restore inverted matches macro

* Scope constants in match arms

* Fix inverted null check

* Update src/platform_impl/windows/util.rs

Co-authored-by: Amr Bashir <[email protected]>
  • Loading branch information
wravery and amrbashir authored Nov 10, 2021
1 parent 2294e31 commit 1632772
Show file tree
Hide file tree
Showing 26 changed files with 679 additions and 648 deletions.
5 changes: 5 additions & 0 deletions .changes/windows-0.25.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Update the `windows` crate to 0.25.0, which comes with pre-built libraries. Tao no longer depends on `webview2-com-sys` to generate bindings shared with WRY.
33 changes: 31 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,37 @@ cc = "1"
[target."cfg(target_os = \"windows\")".dependencies]
parking_lot = "0.11"
unicode-segmentation = "1.8.0"
webview2-com-sys = "0.4.0"
windows = "0.19.0"

[target."cfg(target_os = \"windows\")".dependencies.windows]
version = "0.25.0"
features = [
"build",
"Win32_Devices_HumanInterfaceDevice",
"Win32_Foundation",
"Win32_Globalization",
"Win32_Graphics_Dwm",
"Win32_Graphics_Gdi",
"Win32_System_Com",
"Win32_System_Com_StructuredStorage",
"Win32_System_DataExchange",
"Win32_System_Diagnostics_Debug",
"Win32_System_LibraryLoader",
"Win32_System_Memory",
"Win32_System_Ole",
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",
"Win32_UI_Accessibility",
"Win32_UI_Controls",
"Win32_UI_HiDpi",
"Win32_UI_Input_Ime",
"Win32_UI_Input_KeyboardAndMouse",
"Win32_UI_Input_Pointer",
"Win32_UI_Input_Touch",
"Win32_UI_Shell",
"Win32_UI_TextServices",
"Win32_UI_WindowsAndMessaging",
]

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
cairo-rs = "0.14"
Expand Down
2 changes: 1 addition & 1 deletion examples/parentwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
window::WindowBuilder,
};
#[cfg(target_os = "windows")]
use webview2_com_sys::Windows::Win32::Foundation::HWND;
use windows::Win32::Foundation::HWND;
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();
let mut windows = HashMap::new();
Expand Down
6 changes: 3 additions & 3 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use crate::{
window::{BadIcon, Icon, Theme, Window, WindowBuilder},
};
use libc;
use webview2_com_sys::Windows::Win32::{
use windows::Win32::{
Foundation::HWND,
UI::{KeyboardAndMouseInput::*, WindowsAndMessaging::*},
UI::{Input::KeyboardAndMouse::*, WindowsAndMessaging::*},
};

/// Additional methods on `EventLoop` that are specific to Windows.
Expand Down Expand Up @@ -186,7 +186,7 @@ pub trait WindowBuilderExtWindows {
///
/// Parent and menu are mutually exclusive; a child window cannot have a menu!
///
/// The menu must have been manually created beforehand with [`webview2_com_sys::Windows::Win32::UI::WindowsAndMessaging::CreateMenu`]
/// The menu must have been manually created beforehand with [`windows::Win32::UI::WindowsAndMessaging::CreateMenu`]
/// or similar.
///
/// Note: Dark mode cannot be supported for win32 menus, it's simply not possible to change how the menus look.
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/windows/accelerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

use lazy_static::lazy_static;

use webview2_com_sys::Windows::Win32::{Foundation::HWND, UI::WindowsAndMessaging::*};
use windows::Win32::{Foundation::HWND, UI::WindowsAndMessaging::*};

// NOTE:
// https://docs.microsoft.com/en-us/windows/win32/wsw/thread-safety
Expand Down
10 changes: 5 additions & 5 deletions src/platform_impl/windows/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::clipboard::{ClipboardFormat, FormatId};
use std::{ffi::OsStr, os::windows::ffi::OsStrExt, ptr};
use webview2_com_sys::Windows::Win32::{
use windows::Win32::{
Foundation::{HANDLE, HWND, PSTR, PWSTR},
System::{
DataExchange::{
Expand All @@ -28,7 +28,7 @@ impl Clipboard {
pub(crate) fn read_text(&self) -> Option<String> {
with_clipboard(|| unsafe {
let handle = GetClipboardData(CF_UNICODETEXT.0);
if handle.is_null() {
if handle.0 == 0 {
None
} else {
let unic_str = PWSTR(GlobalLock(handle.0) as *mut _);
Expand Down Expand Up @@ -63,11 +63,11 @@ impl Clipboard {
}
};
let result = SetClipboardData(format_id, handle);
if result.is_null() {
if result.0 == 0 {
println!(
"failed to set clipboard for fmt {}, error: {}",
&format.identifier,
windows::HRESULT::from_thread().0
windows::runtime::Error::from_win32().code().0
);
}
}
Expand All @@ -89,7 +89,7 @@ fn register_identifier(ident: &str) -> Option<u32> {
unsafe {
let pb_format = RegisterClipboardFormatA(ident);
if pb_format == 0 {
let err = windows::HRESULT::from_thread().0;
let err = windows::runtime::Error::from_win32().code().0;
println!(
"failed to register clipboard format '{}'; error {}.",
ident, err
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/windows/dark_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// This is a simple implementation of support for Windows Dark Mode,
/// which is inspired by the solution in https://github.com/ysc3839/win32-darkmode
use webview2_com_sys::Windows::Win32::{
use windows::Win32::{
Foundation::{BOOL, HWND, PSTR, PWSTR},
System::LibraryLoader::*,
UI::{Accessibility::*, Controls::*, WindowsAndMessaging::*},
Expand Down Expand Up @@ -158,7 +158,7 @@ fn should_apps_use_dark_mode() -> bool {

let module = LoadLibraryA("uxtheme.dll");

if module.is_null() {
if module.0 == 0 {
return None;
}

Expand Down
8 changes: 4 additions & 4 deletions src/platform_impl/windows/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use std::sync::Once;

use webview2_com_sys::Windows::Win32::{
use windows::Win32::{
Foundation::HWND,
Graphics::Gdi::*,
UI::{HiDpi::*, WindowsAndMessaging::*},
Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn dpi_to_scale_factor(dpi: u32) -> f64 {

pub unsafe fn hwnd_dpi(hwnd: HWND) -> u32 {
let hdc = GetDC(hwnd);
if hdc.is_null() {
if hdc.0 == 0 {
panic!("[tao] `GetDC` returned null!");
}
if let Some(GetDpiForWindow) = *GET_DPI_FOR_WINDOW {
Expand All @@ -82,7 +82,7 @@ pub unsafe fn hwnd_dpi(hwnd: HWND) -> u32 {
} else if let Some(GetDpiForMonitor) = *GET_DPI_FOR_MONITOR {
// We are on Windows 8.1 or later.
let monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
if monitor.is_null() {
if monitor.0 == 0 {
return BASE_DPI;
}

Expand All @@ -98,7 +98,7 @@ pub unsafe fn hwnd_dpi(hwnd: HWND) -> u32 {
if IsProcessDPIAware().as_bool() {
// If the process is DPI aware, then scaling must be handled by the application using
// this DPI value.
GetDeviceCaps(hdc, GET_DEVICE_CAPS_INDEX(LOGPIXELSX)) as u32
GetDeviceCaps(hdc, LOGPIXELSX) as u32
} else {
// If the process is DPI unaware, then scaling is performed by the OS; we thus return
// 96 (scale factor 1.0) to prevent the window from being re-scaled by both the
Expand Down
35 changes: 16 additions & 19 deletions src/platform_impl/windows/drop_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@

use std::{ffi::OsString, os::windows::ffi::OsStringExt, path::PathBuf, ptr};

use webview2_com_sys::Windows::{
self,
use windows::{
self as Windows,
runtime::implement,
Win32::{
Foundation::{HWND, POINTL, PWSTR},
System::Com::{IDataObject, DROPEFFECT_COPY, DROPEFFECT_NONE},
Foundation::{self as win32f, HWND, POINTL, PWSTR},
System::{
Com::{IDataObject, DVASPECT_CONTENT, FORMATETC, TYMED_HGLOBAL},
Ole::{DROPEFFECT_COPY, DROPEFFECT_NONE},
SystemServices::CF_HDROP,
},
UI::Shell::{DragFinish, DragQueryFileW, HDROP},
},
};
Expand All @@ -16,7 +21,7 @@ use crate::platform_impl::platform::WindowId;

use crate::{event::Event, window::WindowId as SuperWindowId};

#[windows::implement(Windows::Win32::System::Com::IDropTarget)]
#[implement(Windows::Win32::System::Ole::IDropTarget)]
pub struct FileDropHandler {
window: HWND,
send_event: Box<dyn Fn(Event<'static, ()>)>,
Expand All @@ -41,7 +46,7 @@ impl FileDropHandler {
_grfKeyState: u32,
_pt: POINTL,
pdwEffect: *mut u32,
) -> windows::Result<()> {
) -> windows::runtime::Result<()> {
use crate::event::WindowEvent::HoveredFile;
let hdrop = Self::iterate_filenames(pDataObj, |filename| {
(self.send_event)(Event::WindowEvent {
Expand All @@ -64,12 +69,12 @@ impl FileDropHandler {
_grfKeyState: u32,
_pt: POINTL,
pdwEffect: *mut u32,
) -> windows::Result<()> {
) -> windows::runtime::Result<()> {
*pdwEffect = self.cursor_effect;
Ok(())
}

unsafe fn DragLeave(&self) -> windows::Result<()> {
unsafe fn DragLeave(&self) -> windows::runtime::Result<()> {
use crate::event::WindowEvent::HoveredFileCancelled;
if self.hovered_is_valid {
(self.send_event)(Event::WindowEvent {
Expand All @@ -86,7 +91,7 @@ impl FileDropHandler {
_grfKeyState: u32,
_pt: POINTL,
_pdwEffect: *mut u32,
) -> windows::Result<()> {
) -> windows::runtime::Result<()> {
use crate::event::WindowEvent::DroppedFile;
let hdrop = Self::iterate_filenames(pDataObj, |filename| {
(self.send_event)(Event::WindowEvent {
Expand All @@ -104,15 +109,7 @@ impl FileDropHandler {
where
F: Fn(PathBuf),
{
use Windows::Win32::{
Foundation as win32f,
System::{
Com::{DVASPECT_CONTENT, FORMATETC, TYMED_HGLOBAL},
SystemServices::CF_HDROP,
},
};

let mut drop_format = FORMATETC {
let drop_format = FORMATETC {
cfFormat: CF_HDROP.0 as u16,
ptd: ptr::null_mut(),
dwAspect: DVASPECT_CONTENT.0 as u32,
Expand All @@ -123,7 +120,7 @@ impl FileDropHandler {
match data_obj
.as_ref()
.expect("Received null IDataObject")
.GetData(&mut drop_format)
.GetData(&drop_format)
{
Ok(medium) => {
let hglobal = medium.Anonymous.hGlobal;
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/windows/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use std::{

use crate::event::{ModifiersState, ScanCode, VirtualKeyCode};

use webview2_com_sys::Windows::Win32::{
use windows::Win32::{
Foundation::{HWND, LPARAM, WPARAM},
UI::{
KeyboardAndMouseInput::*,
Input::KeyboardAndMouse::*,
TextServices::HKL,
WindowsAndMessaging::{self as win32wm, *},
},
Expand Down
Loading

0 comments on commit 1632772

Please sign in to comment.