|
| 1 | +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +pub mod windows { |
| 6 | + use cef::*; |
| 7 | + use windows::core::{w, PCWSTR}; |
| 8 | + use windows::Win32::Foundation::*; |
| 9 | + use windows::Win32::UI::WindowsAndMessaging::*; |
| 10 | + |
| 11 | + /// Same as [WNDPROC] but without the Option wrapper. |
| 12 | + type WindowProc = unsafe extern "system" fn(HWND, u32, WPARAM, LPARAM) -> LRESULT; |
| 13 | + |
| 14 | + const ORIGINAL_WND_PROP: PCWSTR = w!("TAURI_CEF_ORIGINAL_WND_PROC"); |
| 15 | + |
| 16 | + /// Subclasses the given window to handle draggable regions |
| 17 | + /// by replacing its window procedure with `root_window_proc` |
| 18 | + /// and storing the original procedure as a property to be called later. |
| 19 | + pub fn subclass_window_for_dragging(window: &mut cef::Window) { |
| 20 | + let hwnd = window.window_handle(); |
| 21 | + let hwnd = HWND(hwnd.0 as _); |
| 22 | + subclass_window(hwnd, root_window_proc); |
| 23 | + } |
| 24 | + |
| 25 | + /// Subclasses a window by replacing its window procedure with the given `proc` |
| 26 | + /// and storing the original procedure as a property for later use. |
| 27 | + fn subclass_window(hwnd: HWND, proc: WindowProc) { |
| 28 | + // If already subclassed, return early |
| 29 | + let orginial_wnd_proc = unsafe { GetPropW(hwnd, ORIGINAL_WND_PROP) }; |
| 30 | + if !orginial_wnd_proc.is_invalid() { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Reset last error |
| 35 | + unsafe { SetLastError(ERROR_SUCCESS) }; |
| 36 | + |
| 37 | + // Set the new window procedure and get the orginal one |
| 38 | + let original_wnd_proc = unsafe { SetWindowLongPtrW(hwnd, GWLP_WNDPROC, proc as isize) }; |
| 39 | + if original_wnd_proc == 0 && unsafe { GetLastError() } != ERROR_SUCCESS { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + unsafe { |
| 44 | + // Store the original window proc as a property for later use |
| 45 | + let _ = SetPropW( |
| 46 | + hwnd, |
| 47 | + ORIGINAL_WND_PROP, |
| 48 | + Some(HANDLE(original_wnd_proc as _)), |
| 49 | + ); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// The root window procedure to handle WM_NCLBUTTONDOWN |
| 54 | + /// by calling DefWindowProcW directly to allow dragging |
| 55 | + /// and forwarding other messages to the original CEF window procedure. |
| 56 | + unsafe extern "system" fn root_window_proc( |
| 57 | + hwnd: HWND, |
| 58 | + msg: u32, |
| 59 | + wparam: WPARAM, |
| 60 | + lparam: LPARAM, |
| 61 | + ) -> LRESULT { |
| 62 | + if msg == WM_NCLBUTTONDOWN { |
| 63 | + return unsafe { DefWindowProcW(hwnd, msg, wparam, lparam) }; |
| 64 | + } |
| 65 | + |
| 66 | + // For other messages, call the original CEF window procedure |
| 67 | + let original_wnd_proc = GetPropW(hwnd, ORIGINAL_WND_PROP); |
| 68 | + let original_wnd_proc = std::mem::transmute::<_, WindowProc>(original_wnd_proc.0); |
| 69 | + CallWindowProcW(Some(original_wnd_proc), hwnd, msg, wparam, lparam) |
| 70 | + } |
| 71 | +} |
0 commit comments