events.rs

   1use std::rc::Rc;
   2
   3use ::util::ResultExt;
   4use anyhow::Context as _;
   5use windows::{
   6    Win32::{
   7        Foundation::*,
   8        Graphics::Gdi::*,
   9        System::SystemServices::*,
  10        UI::{
  11            Controls::*,
  12            HiDpi::*,
  13            Input::{Ime::*, KeyboardAndMouse::*},
  14            WindowsAndMessaging::*,
  15        },
  16    },
  17    core::PCWSTR,
  18};
  19
  20use crate::*;
  21
  22pub(crate) const WM_GPUI_CURSOR_STYLE_CHANGED: u32 = WM_USER + 1;
  23pub(crate) const WM_GPUI_CLOSE_ONE_WINDOW: u32 = WM_USER + 2;
  24pub(crate) const WM_GPUI_TASK_DISPATCHED_ON_MAIN_THREAD: u32 = WM_USER + 3;
  25pub(crate) const WM_GPUI_DOCK_MENU_ACTION: u32 = WM_USER + 4;
  26
  27const SIZE_MOVE_LOOP_TIMER_ID: usize = 1;
  28const AUTO_HIDE_TASKBAR_THICKNESS_PX: i32 = 1;
  29
  30pub(crate) fn handle_msg(
  31    handle: HWND,
  32    msg: u32,
  33    wparam: WPARAM,
  34    lparam: LPARAM,
  35    state_ptr: Rc<WindowsWindowStatePtr>,
  36) -> LRESULT {
  37    let handled = match msg {
  38        WM_ACTIVATE => handle_activate_msg(handle, wparam, state_ptr),
  39        WM_CREATE => handle_create_msg(handle, state_ptr),
  40        WM_MOVE => handle_move_msg(handle, lparam, state_ptr),
  41        WM_SIZE => handle_size_msg(wparam, lparam, state_ptr),
  42        WM_ENTERSIZEMOVE | WM_ENTERMENULOOP => handle_size_move_loop(handle),
  43        WM_EXITSIZEMOVE | WM_EXITMENULOOP => handle_size_move_loop_exit(handle),
  44        WM_TIMER => handle_timer_msg(handle, wparam, state_ptr),
  45        WM_NCCALCSIZE => handle_calc_client_size(handle, wparam, lparam, state_ptr),
  46        WM_DPICHANGED => handle_dpi_changed_msg(handle, wparam, lparam, state_ptr),
  47        WM_DISPLAYCHANGE => handle_display_change_msg(handle, state_ptr),
  48        WM_NCHITTEST => handle_hit_test_msg(handle, msg, wparam, lparam, state_ptr),
  49        WM_PAINT => handle_paint_msg(handle, state_ptr),
  50        WM_CLOSE => handle_close_msg(state_ptr),
  51        WM_DESTROY => handle_destroy_msg(handle, state_ptr),
  52        WM_MOUSEMOVE => handle_mouse_move_msg(handle, lparam, wparam, state_ptr),
  53        WM_MOUSELEAVE | WM_NCMOUSELEAVE => handle_mouse_leave_msg(state_ptr),
  54        WM_NCMOUSEMOVE => handle_nc_mouse_move_msg(handle, lparam, state_ptr),
  55        WM_NCLBUTTONDOWN => {
  56            handle_nc_mouse_down_msg(handle, MouseButton::Left, wparam, lparam, state_ptr)
  57        }
  58        WM_NCRBUTTONDOWN => {
  59            handle_nc_mouse_down_msg(handle, MouseButton::Right, wparam, lparam, state_ptr)
  60        }
  61        WM_NCMBUTTONDOWN => {
  62            handle_nc_mouse_down_msg(handle, MouseButton::Middle, wparam, lparam, state_ptr)
  63        }
  64        WM_NCLBUTTONUP => {
  65            handle_nc_mouse_up_msg(handle, MouseButton::Left, wparam, lparam, state_ptr)
  66        }
  67        WM_NCRBUTTONUP => {
  68            handle_nc_mouse_up_msg(handle, MouseButton::Right, wparam, lparam, state_ptr)
  69        }
  70        WM_NCMBUTTONUP => {
  71            handle_nc_mouse_up_msg(handle, MouseButton::Middle, wparam, lparam, state_ptr)
  72        }
  73        WM_LBUTTONDOWN => handle_mouse_down_msg(handle, MouseButton::Left, lparam, state_ptr),
  74        WM_RBUTTONDOWN => handle_mouse_down_msg(handle, MouseButton::Right, lparam, state_ptr),
  75        WM_MBUTTONDOWN => handle_mouse_down_msg(handle, MouseButton::Middle, lparam, state_ptr),
  76        WM_XBUTTONDOWN => {
  77            handle_xbutton_msg(handle, wparam, lparam, handle_mouse_down_msg, state_ptr)
  78        }
  79        WM_LBUTTONUP => handle_mouse_up_msg(handle, MouseButton::Left, lparam, state_ptr),
  80        WM_RBUTTONUP => handle_mouse_up_msg(handle, MouseButton::Right, lparam, state_ptr),
  81        WM_MBUTTONUP => handle_mouse_up_msg(handle, MouseButton::Middle, lparam, state_ptr),
  82        WM_XBUTTONUP => handle_xbutton_msg(handle, wparam, lparam, handle_mouse_up_msg, state_ptr),
  83        WM_MOUSEWHEEL => handle_mouse_wheel_msg(handle, wparam, lparam, state_ptr),
  84        WM_MOUSEHWHEEL => handle_mouse_horizontal_wheel_msg(handle, wparam, lparam, state_ptr),
  85        WM_SYSKEYDOWN => handle_syskeydown_msg(wparam, lparam, state_ptr),
  86        WM_SYSKEYUP => handle_syskeyup_msg(wparam, state_ptr),
  87        WM_SYSCOMMAND => handle_system_command(wparam, state_ptr),
  88        WM_KEYDOWN => handle_keydown_msg(wparam, lparam, state_ptr),
  89        WM_KEYUP => handle_keyup_msg(wparam, state_ptr),
  90        WM_CHAR => handle_char_msg(wparam, lparam, state_ptr),
  91        WM_IME_STARTCOMPOSITION => handle_ime_position(handle, state_ptr),
  92        WM_IME_COMPOSITION => handle_ime_composition(handle, lparam, state_ptr),
  93        WM_SETCURSOR => handle_set_cursor(lparam, state_ptr),
  94        WM_SETTINGCHANGE => handle_system_settings_changed(handle, lparam, state_ptr),
  95        WM_GPUI_CURSOR_STYLE_CHANGED => handle_cursor_changed(lparam, state_ptr),
  96        _ => None,
  97    };
  98    if let Some(n) = handled {
  99        LRESULT(n)
 100    } else {
 101        unsafe { DefWindowProcW(handle, msg, wparam, lparam) }
 102    }
 103}
 104
 105fn handle_move_msg(
 106    handle: HWND,
 107    lparam: LPARAM,
 108    state_ptr: Rc<WindowsWindowStatePtr>,
 109) -> Option<isize> {
 110    let mut lock = state_ptr.state.borrow_mut();
 111    let origin = logical_point(
 112        lparam.signed_loword() as f32,
 113        lparam.signed_hiword() as f32,
 114        lock.scale_factor,
 115    );
 116    lock.origin = origin;
 117    let size = lock.logical_size;
 118    let center_x = origin.x.0 + size.width.0 / 2.;
 119    let center_y = origin.y.0 + size.height.0 / 2.;
 120    let monitor_bounds = lock.display.bounds();
 121    if center_x < monitor_bounds.left().0
 122        || center_x > monitor_bounds.right().0
 123        || center_y < monitor_bounds.top().0
 124        || center_y > monitor_bounds.bottom().0
 125    {
 126        // center of the window may have moved to another monitor
 127        let monitor = unsafe { MonitorFromWindow(handle, MONITOR_DEFAULTTONULL) };
 128        // minimize the window can trigger this event too, in this case,
 129        // monitor is invalid, we do nothing.
 130        if !monitor.is_invalid() && lock.display.handle != monitor {
 131            // we will get the same monitor if we only have one
 132            lock.display = WindowsDisplay::new_with_handle(monitor);
 133        }
 134    }
 135    if let Some(mut callback) = lock.callbacks.moved.take() {
 136        drop(lock);
 137        callback();
 138        state_ptr.state.borrow_mut().callbacks.moved = Some(callback);
 139    }
 140    Some(0)
 141}
 142
 143fn handle_size_msg(
 144    wparam: WPARAM,
 145    lparam: LPARAM,
 146    state_ptr: Rc<WindowsWindowStatePtr>,
 147) -> Option<isize> {
 148    let mut lock = state_ptr.state.borrow_mut();
 149
 150    // Don't resize the renderer when the window is minimized, but record that it was minimized so
 151    // that on restore the swap chain can be recreated via `update_drawable_size_even_if_unchanged`.
 152    if wparam.0 == SIZE_MINIMIZED as usize {
 153        lock.restore_from_minimized = lock.callbacks.request_frame.take();
 154        return Some(0);
 155    }
 156
 157    let width = lparam.loword().max(1) as i32;
 158    let height = lparam.hiword().max(1) as i32;
 159    let new_size = size(DevicePixels(width), DevicePixels(height));
 160    let scale_factor = lock.scale_factor;
 161    if lock.restore_from_minimized.is_some() {
 162        lock.renderer
 163            .update_drawable_size_even_if_unchanged(new_size);
 164        lock.callbacks.request_frame = lock.restore_from_minimized.take();
 165    } else {
 166        lock.renderer.update_drawable_size(new_size);
 167    }
 168    let new_size = new_size.to_pixels(scale_factor);
 169    lock.logical_size = new_size;
 170    if let Some(mut callback) = lock.callbacks.resize.take() {
 171        drop(lock);
 172        callback(new_size, scale_factor);
 173        state_ptr.state.borrow_mut().callbacks.resize = Some(callback);
 174    }
 175    Some(0)
 176}
 177
 178fn handle_size_move_loop(handle: HWND) -> Option<isize> {
 179    unsafe {
 180        let ret = SetTimer(
 181            Some(handle),
 182            SIZE_MOVE_LOOP_TIMER_ID,
 183            USER_TIMER_MINIMUM,
 184            None,
 185        );
 186        if ret == 0 {
 187            log::error!(
 188                "unable to create timer: {}",
 189                std::io::Error::last_os_error()
 190            );
 191        }
 192    }
 193    None
 194}
 195
 196fn handle_size_move_loop_exit(handle: HWND) -> Option<isize> {
 197    unsafe {
 198        KillTimer(Some(handle), SIZE_MOVE_LOOP_TIMER_ID).log_err();
 199    }
 200    None
 201}
 202
 203fn handle_timer_msg(
 204    handle: HWND,
 205    wparam: WPARAM,
 206    state_ptr: Rc<WindowsWindowStatePtr>,
 207) -> Option<isize> {
 208    if wparam.0 == SIZE_MOVE_LOOP_TIMER_ID {
 209        for runnable in state_ptr.main_receiver.drain() {
 210            runnable.run();
 211        }
 212        handle_paint_msg(handle, state_ptr)
 213    } else {
 214        None
 215    }
 216}
 217
 218fn handle_paint_msg(handle: HWND, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
 219    let mut lock = state_ptr.state.borrow_mut();
 220    if let Some(mut request_frame) = lock.callbacks.request_frame.take() {
 221        drop(lock);
 222        request_frame(Default::default());
 223        state_ptr.state.borrow_mut().callbacks.request_frame = Some(request_frame);
 224    }
 225    unsafe { ValidateRect(Some(handle), None).ok().log_err() };
 226    Some(0)
 227}
 228
 229fn handle_close_msg(state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
 230    let mut lock = state_ptr.state.borrow_mut();
 231    if let Some(mut callback) = lock.callbacks.should_close.take() {
 232        drop(lock);
 233        let should_close = callback();
 234        state_ptr.state.borrow_mut().callbacks.should_close = Some(callback);
 235        if should_close { None } else { Some(0) }
 236    } else {
 237        None
 238    }
 239}
 240
 241fn handle_destroy_msg(handle: HWND, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
 242    let callback = {
 243        let mut lock = state_ptr.state.borrow_mut();
 244        lock.callbacks.close.take()
 245    };
 246    if let Some(callback) = callback {
 247        callback();
 248    }
 249    unsafe {
 250        PostThreadMessageW(
 251            state_ptr.main_thread_id_win32,
 252            WM_GPUI_CLOSE_ONE_WINDOW,
 253            WPARAM(state_ptr.validation_number),
 254            LPARAM(handle.0 as isize),
 255        )
 256        .log_err();
 257    }
 258    Some(0)
 259}
 260
 261fn handle_mouse_move_msg(
 262    handle: HWND,
 263    lparam: LPARAM,
 264    wparam: WPARAM,
 265    state_ptr: Rc<WindowsWindowStatePtr>,
 266) -> Option<isize> {
 267    start_tracking_mouse(handle, &state_ptr, TME_LEAVE);
 268
 269    let mut lock = state_ptr.state.borrow_mut();
 270    if let Some(mut callback) = lock.callbacks.input.take() {
 271        let scale_factor = lock.scale_factor;
 272        drop(lock);
 273        let pressed_button = match MODIFIERKEYS_FLAGS(wparam.loword() as u32) {
 274            flags if flags.contains(MK_LBUTTON) => Some(MouseButton::Left),
 275            flags if flags.contains(MK_RBUTTON) => Some(MouseButton::Right),
 276            flags if flags.contains(MK_MBUTTON) => Some(MouseButton::Middle),
 277            flags if flags.contains(MK_XBUTTON1) => {
 278                Some(MouseButton::Navigate(NavigationDirection::Back))
 279            }
 280            flags if flags.contains(MK_XBUTTON2) => {
 281                Some(MouseButton::Navigate(NavigationDirection::Forward))
 282            }
 283            _ => None,
 284        };
 285        let x = lparam.signed_loword() as f32;
 286        let y = lparam.signed_hiword() as f32;
 287        let event = MouseMoveEvent {
 288            position: logical_point(x, y, scale_factor),
 289            pressed_button,
 290            modifiers: current_modifiers(),
 291        };
 292        let result = if callback(PlatformInput::MouseMove(event)).default_prevented {
 293            Some(0)
 294        } else {
 295            Some(1)
 296        };
 297        state_ptr.state.borrow_mut().callbacks.input = Some(callback);
 298        return result;
 299    }
 300    Some(1)
 301}
 302
 303fn handle_mouse_leave_msg(state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
 304    let mut lock = state_ptr.state.borrow_mut();
 305    lock.hovered = false;
 306    if let Some(mut callback) = lock.callbacks.hovered_status_change.take() {
 307        drop(lock);
 308        callback(false);
 309        state_ptr.state.borrow_mut().callbacks.hovered_status_change = Some(callback);
 310    }
 311
 312    Some(0)
 313}
 314
 315fn handle_syskeydown_msg(
 316    wparam: WPARAM,
 317    lparam: LPARAM,
 318    state_ptr: Rc<WindowsWindowStatePtr>,
 319) -> Option<isize> {
 320    // we need to call `DefWindowProcW`, or we will lose the system-wide `Alt+F4`, `Alt+{other keys}`
 321    // shortcuts.
 322    let keystroke = parse_syskeydown_msg_keystroke(wparam)?;
 323    let mut func = state_ptr.state.borrow_mut().callbacks.input.take()?;
 324    let event = KeyDownEvent {
 325        keystroke,
 326        is_held: lparam.0 & (0x1 << 30) > 0,
 327    };
 328    let result = if !func(PlatformInput::KeyDown(event)).propagate {
 329        state_ptr.state.borrow_mut().system_key_handled = true;
 330        Some(0)
 331    } else {
 332        None
 333    };
 334    state_ptr.state.borrow_mut().callbacks.input = Some(func);
 335
 336    result
 337}
 338
 339fn handle_syskeyup_msg(wparam: WPARAM, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
 340    // we need to call `DefWindowProcW`, or we will lose the system-wide `Alt+F4`, `Alt+{other keys}`
 341    // shortcuts.
 342    let keystroke = parse_syskeydown_msg_keystroke(wparam)?;
 343    let mut func = state_ptr.state.borrow_mut().callbacks.input.take()?;
 344    let event = KeyUpEvent { keystroke };
 345    let result = if func(PlatformInput::KeyUp(event)).default_prevented {
 346        Some(0)
 347    } else {
 348        Some(1)
 349    };
 350    state_ptr.state.borrow_mut().callbacks.input = Some(func);
 351
 352    result
 353}
 354
 355fn handle_keydown_msg(
 356    wparam: WPARAM,
 357    lparam: LPARAM,
 358    state_ptr: Rc<WindowsWindowStatePtr>,
 359) -> Option<isize> {
 360    let Some(keystroke_or_modifier) = parse_keystroke_from_vkey(wparam, false) else {
 361        return Some(1);
 362    };
 363    let mut lock = state_ptr.state.borrow_mut();
 364    let Some(mut func) = lock.callbacks.input.take() else {
 365        return Some(1);
 366    };
 367    drop(lock);
 368
 369    let event = match keystroke_or_modifier {
 370        KeystrokeOrModifier::Keystroke(keystroke) => PlatformInput::KeyDown(KeyDownEvent {
 371            keystroke,
 372            is_held: lparam.0 & (0x1 << 30) > 0,
 373        }),
 374        KeystrokeOrModifier::Modifier(modifiers) => {
 375            PlatformInput::ModifiersChanged(ModifiersChangedEvent { modifiers })
 376        }
 377    };
 378
 379    let result = if func(event).default_prevented {
 380        Some(0)
 381    } else {
 382        Some(1)
 383    };
 384    state_ptr.state.borrow_mut().callbacks.input = Some(func);
 385
 386    result
 387}
 388
 389fn handle_keyup_msg(wparam: WPARAM, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
 390    let Some(keystroke_or_modifier) = parse_keystroke_from_vkey(wparam, true) else {
 391        return Some(1);
 392    };
 393    let mut lock = state_ptr.state.borrow_mut();
 394    let Some(mut func) = lock.callbacks.input.take() else {
 395        return Some(1);
 396    };
 397    drop(lock);
 398
 399    let event = match keystroke_or_modifier {
 400        KeystrokeOrModifier::Keystroke(keystroke) => PlatformInput::KeyUp(KeyUpEvent { keystroke }),
 401        KeystrokeOrModifier::Modifier(modifiers) => {
 402            PlatformInput::ModifiersChanged(ModifiersChangedEvent { modifiers })
 403        }
 404    };
 405
 406    let result = if func(event).default_prevented {
 407        Some(0)
 408    } else {
 409        Some(1)
 410    };
 411    state_ptr.state.borrow_mut().callbacks.input = Some(func);
 412
 413    result
 414}
 415
 416fn handle_char_msg(
 417    wparam: WPARAM,
 418    lparam: LPARAM,
 419    state_ptr: Rc<WindowsWindowStatePtr>,
 420) -> Option<isize> {
 421    let Some(keystroke) = parse_char_msg_keystroke(wparam) else {
 422        return Some(1);
 423    };
 424    let mut lock = state_ptr.state.borrow_mut();
 425    let Some(mut func) = lock.callbacks.input.take() else {
 426        return Some(1);
 427    };
 428    drop(lock);
 429    let key_char = keystroke.key_char.clone();
 430    let event = KeyDownEvent {
 431        keystroke,
 432        is_held: lparam.0 & (0x1 << 30) > 0,
 433    };
 434    let dispatch_event_result = func(PlatformInput::KeyDown(event));
 435    state_ptr.state.borrow_mut().callbacks.input = Some(func);
 436
 437    if dispatch_event_result.default_prevented || !dispatch_event_result.propagate {
 438        return Some(0);
 439    }
 440    let Some(ime_char) = key_char else {
 441        return Some(1);
 442    };
 443    with_input_handler(&state_ptr, |input_handler| {
 444        input_handler.replace_text_in_range(None, &ime_char);
 445    });
 446
 447    Some(0)
 448}
 449
 450fn handle_mouse_down_msg(
 451    handle: HWND,
 452    button: MouseButton,
 453    lparam: LPARAM,
 454    state_ptr: Rc<WindowsWindowStatePtr>,
 455) -> Option<isize> {
 456    unsafe { SetCapture(handle) };
 457    let mut lock = state_ptr.state.borrow_mut();
 458    if let Some(mut callback) = lock.callbacks.input.take() {
 459        let x = lparam.signed_loword() as f32;
 460        let y = lparam.signed_hiword() as f32;
 461        let physical_point = point(DevicePixels(x as i32), DevicePixels(y as i32));
 462        let click_count = lock.click_state.update(button, physical_point);
 463        let scale_factor = lock.scale_factor;
 464        drop(lock);
 465
 466        let event = MouseDownEvent {
 467            button,
 468            position: logical_point(x, y, scale_factor),
 469            modifiers: current_modifiers(),
 470            click_count,
 471            first_mouse: false,
 472        };
 473        let result = if callback(PlatformInput::MouseDown(event)).default_prevented {
 474            Some(0)
 475        } else {
 476            Some(1)
 477        };
 478        state_ptr.state.borrow_mut().callbacks.input = Some(callback);
 479
 480        result
 481    } else {
 482        Some(1)
 483    }
 484}
 485
 486fn handle_mouse_up_msg(
 487    _handle: HWND,
 488    button: MouseButton,
 489    lparam: LPARAM,
 490    state_ptr: Rc<WindowsWindowStatePtr>,
 491) -> Option<isize> {
 492    unsafe { ReleaseCapture().log_err() };
 493    let mut lock = state_ptr.state.borrow_mut();
 494    if let Some(mut callback) = lock.callbacks.input.take() {
 495        let x = lparam.signed_loword() as f32;
 496        let y = lparam.signed_hiword() as f32;
 497        let click_count = lock.click_state.current_count;
 498        let scale_factor = lock.scale_factor;
 499        drop(lock);
 500
 501        let event = MouseUpEvent {
 502            button,
 503            position: logical_point(x, y, scale_factor),
 504            modifiers: current_modifiers(),
 505            click_count,
 506        };
 507        let result = if callback(PlatformInput::MouseUp(event)).default_prevented {
 508            Some(0)
 509        } else {
 510            Some(1)
 511        };
 512        state_ptr.state.borrow_mut().callbacks.input = Some(callback);
 513
 514        result
 515    } else {
 516        Some(1)
 517    }
 518}
 519
 520fn handle_xbutton_msg(
 521    handle: HWND,
 522    wparam: WPARAM,
 523    lparam: LPARAM,
 524    handler: impl Fn(HWND, MouseButton, LPARAM, Rc<WindowsWindowStatePtr>) -> Option<isize>,
 525    state_ptr: Rc<WindowsWindowStatePtr>,
 526) -> Option<isize> {
 527    let nav_dir = match wparam.hiword() {
 528        XBUTTON1 => NavigationDirection::Back,
 529        XBUTTON2 => NavigationDirection::Forward,
 530        _ => return Some(1),
 531    };
 532    handler(handle, MouseButton::Navigate(nav_dir), lparam, state_ptr)
 533}
 534
 535fn handle_mouse_wheel_msg(
 536    handle: HWND,
 537    wparam: WPARAM,
 538    lparam: LPARAM,
 539    state_ptr: Rc<WindowsWindowStatePtr>,
 540) -> Option<isize> {
 541    let modifiers = current_modifiers();
 542    let mut lock = state_ptr.state.borrow_mut();
 543    if let Some(mut callback) = lock.callbacks.input.take() {
 544        let scale_factor = lock.scale_factor;
 545        let wheel_scroll_amount = match modifiers.shift {
 546            true => lock.system_settings.mouse_wheel_settings.wheel_scroll_chars,
 547            false => lock.system_settings.mouse_wheel_settings.wheel_scroll_lines,
 548        };
 549        drop(lock);
 550        let wheel_distance =
 551            (wparam.signed_hiword() as f32 / WHEEL_DELTA as f32) * wheel_scroll_amount as f32;
 552        let mut cursor_point = POINT {
 553            x: lparam.signed_loword().into(),
 554            y: lparam.signed_hiword().into(),
 555        };
 556        unsafe { ScreenToClient(handle, &mut cursor_point).ok().log_err() };
 557        let event = ScrollWheelEvent {
 558            position: logical_point(cursor_point.x as f32, cursor_point.y as f32, scale_factor),
 559            delta: ScrollDelta::Lines(match modifiers.shift {
 560                true => Point {
 561                    x: wheel_distance,
 562                    y: 0.0,
 563                },
 564                false => Point {
 565                    y: wheel_distance,
 566                    x: 0.0,
 567                },
 568            }),
 569            modifiers: current_modifiers(),
 570            touch_phase: TouchPhase::Moved,
 571        };
 572        let result = if callback(PlatformInput::ScrollWheel(event)).default_prevented {
 573            Some(0)
 574        } else {
 575            Some(1)
 576        };
 577        state_ptr.state.borrow_mut().callbacks.input = Some(callback);
 578
 579        result
 580    } else {
 581        Some(1)
 582    }
 583}
 584
 585fn handle_mouse_horizontal_wheel_msg(
 586    handle: HWND,
 587    wparam: WPARAM,
 588    lparam: LPARAM,
 589    state_ptr: Rc<WindowsWindowStatePtr>,
 590) -> Option<isize> {
 591    let mut lock = state_ptr.state.borrow_mut();
 592    if let Some(mut callback) = lock.callbacks.input.take() {
 593        let scale_factor = lock.scale_factor;
 594        let wheel_scroll_chars = lock.system_settings.mouse_wheel_settings.wheel_scroll_chars;
 595        drop(lock);
 596        let wheel_distance =
 597            (-wparam.signed_hiword() as f32 / WHEEL_DELTA as f32) * wheel_scroll_chars as f32;
 598        let mut cursor_point = POINT {
 599            x: lparam.signed_loword().into(),
 600            y: lparam.signed_hiword().into(),
 601        };
 602        unsafe { ScreenToClient(handle, &mut cursor_point).ok().log_err() };
 603        let event = ScrollWheelEvent {
 604            position: logical_point(cursor_point.x as f32, cursor_point.y as f32, scale_factor),
 605            delta: ScrollDelta::Lines(Point {
 606                x: wheel_distance,
 607                y: 0.0,
 608            }),
 609            modifiers: current_modifiers(),
 610            touch_phase: TouchPhase::Moved,
 611        };
 612        let result = if callback(PlatformInput::ScrollWheel(event)).default_prevented {
 613            Some(0)
 614        } else {
 615            Some(1)
 616        };
 617        state_ptr.state.borrow_mut().callbacks.input = Some(callback);
 618
 619        result
 620    } else {
 621        Some(1)
 622    }
 623}
 624
 625fn retrieve_caret_position(state_ptr: &Rc<WindowsWindowStatePtr>) -> Option<POINT> {
 626    with_input_handler_and_scale_factor(state_ptr, |input_handler, scale_factor| {
 627        let caret_range = input_handler.selected_text_range(false)?;
 628        let caret_position = input_handler.bounds_for_range(caret_range.range)?;
 629        Some(POINT {
 630            // logical to physical
 631            x: (caret_position.origin.x.0 * scale_factor) as i32,
 632            y: (caret_position.origin.y.0 * scale_factor) as i32
 633                + ((caret_position.size.height.0 * scale_factor) as i32 / 2),
 634        })
 635    })
 636}
 637
 638fn handle_ime_position(handle: HWND, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
 639    unsafe {
 640        let ctx = ImmGetContext(handle);
 641
 642        let Some(caret_position) = retrieve_caret_position(&state_ptr) else {
 643            return Some(0);
 644        };
 645        {
 646            let config = COMPOSITIONFORM {
 647                dwStyle: CFS_POINT,
 648                ptCurrentPos: caret_position,
 649                ..Default::default()
 650            };
 651            ImmSetCompositionWindow(ctx, &config as _).ok().log_err();
 652        }
 653        {
 654            let config = CANDIDATEFORM {
 655                dwStyle: CFS_CANDIDATEPOS,
 656                ptCurrentPos: caret_position,
 657                ..Default::default()
 658            };
 659            ImmSetCandidateWindow(ctx, &config as _).ok().log_err();
 660        }
 661        ImmReleaseContext(handle, ctx).ok().log_err();
 662        Some(0)
 663    }
 664}
 665
 666fn handle_ime_composition(
 667    handle: HWND,
 668    lparam: LPARAM,
 669    state_ptr: Rc<WindowsWindowStatePtr>,
 670) -> Option<isize> {
 671    let ctx = unsafe { ImmGetContext(handle) };
 672    let result = handle_ime_composition_inner(ctx, lparam, state_ptr);
 673    unsafe { ImmReleaseContext(handle, ctx).ok().log_err() };
 674    result
 675}
 676
 677fn handle_ime_composition_inner(
 678    ctx: HIMC,
 679    lparam: LPARAM,
 680    state_ptr: Rc<WindowsWindowStatePtr>,
 681) -> Option<isize> {
 682    let mut ime_input = None;
 683    if lparam.0 as u32 & GCS_COMPSTR.0 > 0 {
 684        let (comp_string, string_len) = parse_ime_compostion_string(ctx)?;
 685        with_input_handler(&state_ptr, |input_handler| {
 686            input_handler.replace_and_mark_text_in_range(
 687                None,
 688                &comp_string,
 689                Some(string_len..string_len),
 690            );
 691        })?;
 692        ime_input = Some(comp_string);
 693    }
 694    if lparam.0 as u32 & GCS_CURSORPOS.0 > 0 {
 695        let comp_string = &ime_input?;
 696        let caret_pos = retrieve_composition_cursor_position(ctx);
 697        with_input_handler(&state_ptr, |input_handler| {
 698            input_handler.replace_and_mark_text_in_range(
 699                None,
 700                comp_string,
 701                Some(caret_pos..caret_pos),
 702            );
 703        })?;
 704    }
 705    if lparam.0 as u32 & GCS_RESULTSTR.0 > 0 {
 706        let comp_result = parse_ime_compostion_result(ctx)?;
 707        with_input_handler(&state_ptr, |input_handler| {
 708            input_handler.replace_text_in_range(None, &comp_result);
 709        })?;
 710        return Some(0);
 711    }
 712    // currently, we don't care other stuff
 713    None
 714}
 715
 716/// SEE: https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-nccalcsize
 717fn handle_calc_client_size(
 718    handle: HWND,
 719    wparam: WPARAM,
 720    lparam: LPARAM,
 721    state_ptr: Rc<WindowsWindowStatePtr>,
 722) -> Option<isize> {
 723    if !state_ptr.hide_title_bar || state_ptr.state.borrow().is_fullscreen() || wparam.0 == 0 {
 724        return None;
 725    }
 726
 727    let is_maximized = state_ptr.state.borrow().is_maximized();
 728    let insets = get_client_area_insets(handle, is_maximized, state_ptr.windows_version);
 729    // wparam is TRUE so lparam points to an NCCALCSIZE_PARAMS structure
 730    let mut params = lparam.0 as *mut NCCALCSIZE_PARAMS;
 731    let mut requested_client_rect = unsafe { &mut ((*params).rgrc) };
 732
 733    requested_client_rect[0].left += insets.left;
 734    requested_client_rect[0].top += insets.top;
 735    requested_client_rect[0].right -= insets.right;
 736    requested_client_rect[0].bottom -= insets.bottom;
 737
 738    // Fix auto hide taskbar not showing. This solution is based on the approach
 739    // used by Chrome. However, it may result in one row of pixels being obscured
 740    // in our client area. But as Chrome says, "there seems to be no better solution."
 741    if is_maximized {
 742        if let Some(ref taskbar_position) = state_ptr
 743            .state
 744            .borrow()
 745            .system_settings
 746            .auto_hide_taskbar_position
 747        {
 748            // Fot the auto-hide taskbar, adjust in by 1 pixel on taskbar edge,
 749            // so the window isn't treated as a "fullscreen app", which would cause
 750            // the taskbar to disappear.
 751            match taskbar_position {
 752                AutoHideTaskbarPosition::Left => {
 753                    requested_client_rect[0].left += AUTO_HIDE_TASKBAR_THICKNESS_PX
 754                }
 755                AutoHideTaskbarPosition::Top => {
 756                    requested_client_rect[0].top += AUTO_HIDE_TASKBAR_THICKNESS_PX
 757                }
 758                AutoHideTaskbarPosition::Right => {
 759                    requested_client_rect[0].right -= AUTO_HIDE_TASKBAR_THICKNESS_PX
 760                }
 761                AutoHideTaskbarPosition::Bottom => {
 762                    requested_client_rect[0].bottom -= AUTO_HIDE_TASKBAR_THICKNESS_PX
 763                }
 764            }
 765        }
 766    }
 767
 768    Some(0)
 769}
 770
 771fn handle_activate_msg(
 772    handle: HWND,
 773    wparam: WPARAM,
 774    state_ptr: Rc<WindowsWindowStatePtr>,
 775) -> Option<isize> {
 776    let activated = wparam.loword() > 0;
 777    if state_ptr.hide_title_bar {
 778        if let Some(titlebar_rect) = state_ptr.state.borrow().get_titlebar_rect().log_err() {
 779            unsafe {
 780                InvalidateRect(Some(handle), Some(&titlebar_rect), false)
 781                    .ok()
 782                    .log_err()
 783            };
 784        }
 785    }
 786    let this = state_ptr.clone();
 787    state_ptr
 788        .executor
 789        .spawn(async move {
 790            let mut lock = this.state.borrow_mut();
 791            if let Some(mut cb) = lock.callbacks.active_status_change.take() {
 792                drop(lock);
 793                cb(activated);
 794                this.state.borrow_mut().callbacks.active_status_change = Some(cb);
 795            }
 796        })
 797        .detach();
 798
 799    None
 800}
 801
 802fn handle_create_msg(handle: HWND, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
 803    if state_ptr.hide_title_bar {
 804        notify_frame_changed(handle);
 805        Some(0)
 806    } else {
 807        None
 808    }
 809}
 810
 811fn handle_dpi_changed_msg(
 812    handle: HWND,
 813    wparam: WPARAM,
 814    lparam: LPARAM,
 815    state_ptr: Rc<WindowsWindowStatePtr>,
 816) -> Option<isize> {
 817    let new_dpi = wparam.loword() as f32;
 818    let mut lock = state_ptr.state.borrow_mut();
 819    lock.scale_factor = new_dpi / USER_DEFAULT_SCREEN_DPI as f32;
 820    lock.border_offset.update(handle).log_err();
 821    drop(lock);
 822
 823    let rect = unsafe { &*(lparam.0 as *const RECT) };
 824    let width = rect.right - rect.left;
 825    let height = rect.bottom - rect.top;
 826    // this will emit `WM_SIZE` and `WM_MOVE` right here
 827    // even before this function returns
 828    // the new size is handled in `WM_SIZE`
 829    unsafe {
 830        SetWindowPos(
 831            handle,
 832            None,
 833            rect.left,
 834            rect.top,
 835            width,
 836            height,
 837            SWP_NOZORDER | SWP_NOACTIVATE,
 838        )
 839        .context("unable to set window position after dpi has changed")
 840        .log_err();
 841    }
 842
 843    Some(0)
 844}
 845
 846/// The following conditions will trigger this event:
 847/// 1. The monitor on which the window is located goes offline or changes resolution.
 848/// 2. Another monitor goes offline, is plugged in, or changes resolution.
 849///
 850/// In either case, the window will only receive information from the monitor on which
 851/// it is located.
 852///
 853/// For example, in the case of condition 2, where the monitor on which the window is
 854/// located has actually changed nothing, it will still receive this event.
 855fn handle_display_change_msg(handle: HWND, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
 856    // NOTE:
 857    // Even the `lParam` holds the resolution of the screen, we just ignore it.
 858    // Because WM_DPICHANGED, WM_MOVE, WM_SIZE will come first, window reposition and resize
 859    // are handled there.
 860    // So we only care about if monitor is disconnected.
 861    let previous_monitor = state_ptr.as_ref().state.borrow().display;
 862    if WindowsDisplay::is_connected(previous_monitor.handle) {
 863        // we are fine, other display changed
 864        return None;
 865    }
 866    // display disconnected
 867    // in this case, the OS will move our window to another monitor, and minimize it.
 868    // we deminimize the window and query the monitor after moving
 869    unsafe {
 870        let _ = ShowWindow(handle, SW_SHOWNORMAL);
 871    };
 872    let new_monitor = unsafe { MonitorFromWindow(handle, MONITOR_DEFAULTTONULL) };
 873    // all monitors disconnected
 874    if new_monitor.is_invalid() {
 875        log::error!("No monitor detected!");
 876        return None;
 877    }
 878    let new_display = WindowsDisplay::new_with_handle(new_monitor);
 879    state_ptr.as_ref().state.borrow_mut().display = new_display;
 880    Some(0)
 881}
 882
 883fn handle_hit_test_msg(
 884    handle: HWND,
 885    msg: u32,
 886    wparam: WPARAM,
 887    lparam: LPARAM,
 888    state_ptr: Rc<WindowsWindowStatePtr>,
 889) -> Option<isize> {
 890    if !state_ptr.is_movable {
 891        return None;
 892    }
 893    if !state_ptr.hide_title_bar {
 894        return None;
 895    }
 896
 897    // default handler for resize areas
 898    let hit = unsafe { DefWindowProcW(handle, msg, wparam, lparam) };
 899    if matches!(
 900        hit.0 as u32,
 901        HTNOWHERE
 902            | HTRIGHT
 903            | HTLEFT
 904            | HTTOPLEFT
 905            | HTTOP
 906            | HTTOPRIGHT
 907            | HTBOTTOMRIGHT
 908            | HTBOTTOM
 909            | HTBOTTOMLEFT
 910    ) {
 911        return Some(hit.0);
 912    }
 913
 914    if state_ptr.state.borrow().is_fullscreen() {
 915        return Some(HTCLIENT as _);
 916    }
 917
 918    let dpi = unsafe { GetDpiForWindow(handle) };
 919    let frame_y = unsafe { GetSystemMetricsForDpi(SM_CYFRAME, dpi) };
 920
 921    let mut cursor_point = POINT {
 922        x: lparam.signed_loword().into(),
 923        y: lparam.signed_hiword().into(),
 924    };
 925    unsafe { ScreenToClient(handle, &mut cursor_point).ok().log_err() };
 926    if !state_ptr.state.borrow().is_maximized() && cursor_point.y >= 0 && cursor_point.y <= frame_y
 927    {
 928        return Some(HTTOP as _);
 929    }
 930
 931    let titlebar_rect = state_ptr.state.borrow().get_titlebar_rect();
 932    if let Ok(titlebar_rect) = titlebar_rect {
 933        if cursor_point.y < titlebar_rect.bottom {
 934            let caption_btn_width = (state_ptr.state.borrow().caption_button_width().0
 935                * state_ptr.state.borrow().scale_factor) as i32;
 936            if cursor_point.x >= titlebar_rect.right - caption_btn_width {
 937                return Some(HTCLOSE as _);
 938            } else if cursor_point.x >= titlebar_rect.right - caption_btn_width * 2 {
 939                return Some(HTMAXBUTTON as _);
 940            } else if cursor_point.x >= titlebar_rect.right - caption_btn_width * 3 {
 941                return Some(HTMINBUTTON as _);
 942            }
 943
 944            return Some(HTCAPTION as _);
 945        }
 946    }
 947
 948    Some(HTCLIENT as _)
 949}
 950
 951fn handle_nc_mouse_move_msg(
 952    handle: HWND,
 953    lparam: LPARAM,
 954    state_ptr: Rc<WindowsWindowStatePtr>,
 955) -> Option<isize> {
 956    if !state_ptr.hide_title_bar {
 957        return None;
 958    }
 959
 960    start_tracking_mouse(handle, &state_ptr, TME_LEAVE | TME_NONCLIENT);
 961
 962    let mut lock = state_ptr.state.borrow_mut();
 963    if let Some(mut callback) = lock.callbacks.input.take() {
 964        let scale_factor = lock.scale_factor;
 965        drop(lock);
 966        let mut cursor_point = POINT {
 967            x: lparam.signed_loword().into(),
 968            y: lparam.signed_hiword().into(),
 969        };
 970        unsafe { ScreenToClient(handle, &mut cursor_point).ok().log_err() };
 971        let event = MouseMoveEvent {
 972            position: logical_point(cursor_point.x as f32, cursor_point.y as f32, scale_factor),
 973            pressed_button: None,
 974            modifiers: current_modifiers(),
 975        };
 976        let result = if callback(PlatformInput::MouseMove(event)).default_prevented {
 977            Some(0)
 978        } else {
 979            Some(1)
 980        };
 981        state_ptr.state.borrow_mut().callbacks.input = Some(callback);
 982
 983        result
 984    } else {
 985        None
 986    }
 987}
 988
 989fn handle_nc_mouse_down_msg(
 990    handle: HWND,
 991    button: MouseButton,
 992    wparam: WPARAM,
 993    lparam: LPARAM,
 994    state_ptr: Rc<WindowsWindowStatePtr>,
 995) -> Option<isize> {
 996    if !state_ptr.hide_title_bar {
 997        return None;
 998    }
 999
1000    let mut lock = state_ptr.state.borrow_mut();
1001    if let Some(mut callback) = lock.callbacks.input.take() {
1002        let scale_factor = lock.scale_factor;
1003        let mut cursor_point = POINT {
1004            x: lparam.signed_loword().into(),
1005            y: lparam.signed_hiword().into(),
1006        };
1007        unsafe { ScreenToClient(handle, &mut cursor_point).ok().log_err() };
1008        let physical_point = point(DevicePixels(cursor_point.x), DevicePixels(cursor_point.y));
1009        let click_count = lock.click_state.update(button, physical_point);
1010        drop(lock);
1011        let event = MouseDownEvent {
1012            button,
1013            position: logical_point(cursor_point.x as f32, cursor_point.y as f32, scale_factor),
1014            modifiers: current_modifiers(),
1015            click_count,
1016            first_mouse: false,
1017        };
1018        let result = if callback(PlatformInput::MouseDown(event)).default_prevented {
1019            Some(0)
1020        } else {
1021            None
1022        };
1023        state_ptr.state.borrow_mut().callbacks.input = Some(callback);
1024
1025        if result.is_some() {
1026            return result;
1027        }
1028    } else {
1029        drop(lock);
1030    };
1031
1032    // Since these are handled in handle_nc_mouse_up_msg we must prevent the default window proc
1033    if button == MouseButton::Left {
1034        match wparam.0 as u32 {
1035            HTMINBUTTON => state_ptr.state.borrow_mut().nc_button_pressed = Some(HTMINBUTTON),
1036            HTMAXBUTTON => state_ptr.state.borrow_mut().nc_button_pressed = Some(HTMAXBUTTON),
1037            HTCLOSE => state_ptr.state.borrow_mut().nc_button_pressed = Some(HTCLOSE),
1038            _ => return None,
1039        };
1040        Some(0)
1041    } else {
1042        None
1043    }
1044}
1045
1046fn handle_nc_mouse_up_msg(
1047    handle: HWND,
1048    button: MouseButton,
1049    wparam: WPARAM,
1050    lparam: LPARAM,
1051    state_ptr: Rc<WindowsWindowStatePtr>,
1052) -> Option<isize> {
1053    if !state_ptr.hide_title_bar {
1054        return None;
1055    }
1056
1057    let mut lock = state_ptr.state.borrow_mut();
1058    if let Some(mut callback) = lock.callbacks.input.take() {
1059        let scale_factor = lock.scale_factor;
1060        drop(lock);
1061        let mut cursor_point = POINT {
1062            x: lparam.signed_loword().into(),
1063            y: lparam.signed_hiword().into(),
1064        };
1065        unsafe { ScreenToClient(handle, &mut cursor_point).ok().log_err() };
1066        let event = MouseUpEvent {
1067            button,
1068            position: logical_point(cursor_point.x as f32, cursor_point.y as f32, scale_factor),
1069            modifiers: current_modifiers(),
1070            click_count: 1,
1071        };
1072        let result = if callback(PlatformInput::MouseUp(event)).default_prevented {
1073            Some(0)
1074        } else {
1075            None
1076        };
1077        state_ptr.state.borrow_mut().callbacks.input = Some(callback);
1078        if result.is_some() {
1079            return result;
1080        }
1081    } else {
1082        drop(lock);
1083    }
1084
1085    let last_pressed = state_ptr.state.borrow_mut().nc_button_pressed.take();
1086    if button == MouseButton::Left && last_pressed.is_some() {
1087        let last_button = last_pressed.unwrap();
1088        let mut handled = false;
1089        match wparam.0 as u32 {
1090            HTMINBUTTON => {
1091                if last_button == HTMINBUTTON {
1092                    unsafe { ShowWindowAsync(handle, SW_MINIMIZE).ok().log_err() };
1093                    handled = true;
1094                }
1095            }
1096            HTMAXBUTTON => {
1097                if last_button == HTMAXBUTTON {
1098                    if state_ptr.state.borrow().is_maximized() {
1099                        unsafe { ShowWindowAsync(handle, SW_NORMAL).ok().log_err() };
1100                    } else {
1101                        unsafe { ShowWindowAsync(handle, SW_MAXIMIZE).ok().log_err() };
1102                    }
1103                    handled = true;
1104                }
1105            }
1106            HTCLOSE => {
1107                if last_button == HTCLOSE {
1108                    unsafe {
1109                        PostMessageW(Some(handle), WM_CLOSE, WPARAM::default(), LPARAM::default())
1110                            .log_err()
1111                    };
1112                    handled = true;
1113                }
1114            }
1115            _ => {}
1116        };
1117        if handled {
1118            return Some(0);
1119        }
1120    }
1121
1122    None
1123}
1124
1125fn handle_cursor_changed(lparam: LPARAM, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
1126    let mut state = state_ptr.state.borrow_mut();
1127    let had_cursor = state.current_cursor.is_some();
1128
1129    state.current_cursor = if lparam.0 == 0 {
1130        None
1131    } else {
1132        Some(HCURSOR(lparam.0 as _))
1133    };
1134
1135    if had_cursor != state.current_cursor.is_some() {
1136        unsafe { SetCursor(state.current_cursor) };
1137    }
1138
1139    Some(0)
1140}
1141
1142fn handle_set_cursor(lparam: LPARAM, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
1143    if matches!(
1144        lparam.loword() as u32,
1145        HTLEFT | HTRIGHT | HTTOP | HTTOPLEFT | HTTOPRIGHT | HTBOTTOM | HTBOTTOMLEFT | HTBOTTOMRIGHT
1146    ) {
1147        return None;
1148    }
1149    unsafe {
1150        SetCursor(state_ptr.state.borrow().current_cursor);
1151    };
1152    Some(1)
1153}
1154
1155fn handle_system_settings_changed(
1156    handle: HWND,
1157    lparam: LPARAM,
1158    state_ptr: Rc<WindowsWindowStatePtr>,
1159) -> Option<isize> {
1160    let mut lock = state_ptr.state.borrow_mut();
1161    let display = lock.display;
1162    // system settings
1163    lock.system_settings.update(display);
1164    // mouse double click
1165    lock.click_state.system_update();
1166    // window border offset
1167    lock.border_offset.update(handle).log_err();
1168    drop(lock);
1169
1170    // lParam is a pointer to a string that indicates the area containing the system parameter
1171    // that was changed.
1172    let parameter = PCWSTR::from_raw(lparam.0 as _);
1173    if unsafe { !parameter.is_null() && !parameter.is_empty() } {
1174        if let Some(parameter_string) = unsafe { parameter.to_string() }.log_err() {
1175            log::info!("System settings changed: {}", parameter_string);
1176            match parameter_string.as_str() {
1177                "ImmersiveColorSet" => {
1178                    handle_system_theme_changed(handle, state_ptr);
1179                }
1180                _ => {}
1181            }
1182        }
1183    }
1184
1185    // Force to trigger WM_NCCALCSIZE event to ensure that we handle auto hide
1186    // taskbar correctly.
1187    notify_frame_changed(handle);
1188    Some(0)
1189}
1190
1191fn handle_system_command(wparam: WPARAM, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
1192    if wparam.0 == SC_KEYMENU as usize {
1193        let mut lock = state_ptr.state.borrow_mut();
1194        if lock.system_key_handled {
1195            lock.system_key_handled = false;
1196            return Some(0);
1197        }
1198    }
1199    None
1200}
1201
1202fn handle_system_theme_changed(
1203    handle: HWND,
1204    state_ptr: Rc<WindowsWindowStatePtr>,
1205) -> Option<isize> {
1206    let mut callback = state_ptr
1207        .state
1208        .borrow_mut()
1209        .callbacks
1210        .appearance_changed
1211        .take()?;
1212    callback();
1213    state_ptr.state.borrow_mut().callbacks.appearance_changed = Some(callback);
1214    configure_dwm_dark_mode(handle);
1215    Some(0)
1216}
1217
1218fn parse_syskeydown_msg_keystroke(wparam: WPARAM) -> Option<Keystroke> {
1219    let modifiers = current_modifiers();
1220    let vk_code = wparam.loword();
1221
1222    // on Windows, F10 can trigger this event, not just the alt key,
1223    // so when F10 was pressed, handle only it
1224    if !modifiers.alt {
1225        if vk_code == VK_F10.0 {
1226            let offset = vk_code - VK_F1.0;
1227            return Some(Keystroke {
1228                modifiers,
1229                key: format!("f{}", offset + 1),
1230                key_char: None,
1231            });
1232        } else {
1233            return None;
1234        }
1235    }
1236
1237    let key = match VIRTUAL_KEY(vk_code) {
1238        VK_BACK => "backspace",
1239        VK_RETURN => "enter",
1240        VK_TAB => "tab",
1241        VK_UP => "up",
1242        VK_DOWN => "down",
1243        VK_RIGHT => "right",
1244        VK_LEFT => "left",
1245        VK_HOME => "home",
1246        VK_END => "end",
1247        VK_PRIOR => "pageup",
1248        VK_NEXT => "pagedown",
1249        VK_BROWSER_BACK => "back",
1250        VK_BROWSER_FORWARD => "forward",
1251        VK_ESCAPE => "escape",
1252        VK_INSERT => "insert",
1253        VK_DELETE => "delete",
1254        VK_APPS => "menu",
1255        _ => {
1256            let basic_key = basic_vkcode_to_string(vk_code, modifiers);
1257            if basic_key.is_some() {
1258                return basic_key;
1259            } else {
1260                if vk_code >= VK_F1.0 && vk_code <= VK_F24.0 {
1261                    let offset = vk_code - VK_F1.0;
1262                    return Some(Keystroke {
1263                        modifiers,
1264                        key: format!("f{}", offset + 1),
1265                        key_char: None,
1266                    });
1267                } else {
1268                    return None;
1269                }
1270            }
1271        }
1272    }
1273    .to_owned();
1274
1275    Some(Keystroke {
1276        modifiers,
1277        key,
1278        key_char: None,
1279    })
1280}
1281
1282enum KeystrokeOrModifier {
1283    Keystroke(Keystroke),
1284    Modifier(Modifiers),
1285}
1286
1287fn parse_keystroke_from_vkey(wparam: WPARAM, is_keyup: bool) -> Option<KeystrokeOrModifier> {
1288    let vk_code = wparam.loword();
1289
1290    let modifiers = current_modifiers();
1291
1292    let key = match VIRTUAL_KEY(vk_code) {
1293        VK_BACK => "backspace",
1294        VK_RETURN => "enter",
1295        VK_TAB => "tab",
1296        VK_UP => "up",
1297        VK_DOWN => "down",
1298        VK_RIGHT => "right",
1299        VK_LEFT => "left",
1300        VK_HOME => "home",
1301        VK_END => "end",
1302        VK_PRIOR => "pageup",
1303        VK_NEXT => "pagedown",
1304        VK_BROWSER_BACK => "back",
1305        VK_BROWSER_FORWARD => "forward",
1306        VK_ESCAPE => "escape",
1307        VK_INSERT => "insert",
1308        VK_DELETE => "delete",
1309        VK_APPS => "menu",
1310        _ => {
1311            if is_modifier(VIRTUAL_KEY(vk_code)) {
1312                return Some(KeystrokeOrModifier::Modifier(modifiers));
1313            }
1314
1315            if modifiers.control || modifiers.alt || is_keyup {
1316                let basic_key = basic_vkcode_to_string(vk_code, modifiers);
1317                if let Some(basic_key) = basic_key {
1318                    return Some(KeystrokeOrModifier::Keystroke(basic_key));
1319                }
1320            }
1321
1322            if vk_code >= VK_F1.0 && vk_code <= VK_F24.0 {
1323                let offset = vk_code - VK_F1.0;
1324                return Some(KeystrokeOrModifier::Keystroke(Keystroke {
1325                    modifiers,
1326                    key: format!("f{}", offset + 1),
1327                    key_char: None,
1328                }));
1329            };
1330            return None;
1331        }
1332    }
1333    .to_owned();
1334
1335    Some(KeystrokeOrModifier::Keystroke(Keystroke {
1336        modifiers,
1337        key,
1338        key_char: None,
1339    }))
1340}
1341
1342fn parse_char_msg_keystroke(wparam: WPARAM) -> Option<Keystroke> {
1343    let first_char = char::from_u32((wparam.0 as u16).into())?;
1344    if first_char.is_control() {
1345        None
1346    } else {
1347        let mut modifiers = current_modifiers();
1348        // for characters that use 'shift' to type it is expected that the
1349        // shift is not reported if the uppercase/lowercase are the same and instead only the key is reported
1350        if first_char.to_ascii_uppercase() == first_char.to_ascii_lowercase() {
1351            modifiers.shift = false;
1352        }
1353        let key = match first_char {
1354            ' ' => "space".to_string(),
1355            first_char => first_char.to_lowercase().to_string(),
1356        };
1357        Some(Keystroke {
1358            modifiers,
1359            key,
1360            key_char: Some(first_char.to_string()),
1361        })
1362    }
1363}
1364
1365fn parse_ime_compostion_string(ctx: HIMC) -> Option<(String, usize)> {
1366    unsafe {
1367        let string_len = ImmGetCompositionStringW(ctx, GCS_COMPSTR, None, 0);
1368        if string_len >= 0 {
1369            let mut buffer = vec![0u8; string_len as usize + 2];
1370            ImmGetCompositionStringW(
1371                ctx,
1372                GCS_COMPSTR,
1373                Some(buffer.as_mut_ptr() as _),
1374                string_len as _,
1375            );
1376            let wstring = std::slice::from_raw_parts::<u16>(
1377                buffer.as_mut_ptr().cast::<u16>(),
1378                string_len as usize / 2,
1379            );
1380            let string = String::from_utf16_lossy(wstring);
1381            Some((string, string_len as usize / 2))
1382        } else {
1383            None
1384        }
1385    }
1386}
1387
1388#[inline]
1389fn retrieve_composition_cursor_position(ctx: HIMC) -> usize {
1390    unsafe { ImmGetCompositionStringW(ctx, GCS_CURSORPOS, None, 0) as usize }
1391}
1392
1393fn parse_ime_compostion_result(ctx: HIMC) -> Option<String> {
1394    unsafe {
1395        let string_len = ImmGetCompositionStringW(ctx, GCS_RESULTSTR, None, 0);
1396        if string_len >= 0 {
1397            let mut buffer = vec![0u8; string_len as usize + 2];
1398            ImmGetCompositionStringW(
1399                ctx,
1400                GCS_RESULTSTR,
1401                Some(buffer.as_mut_ptr() as _),
1402                string_len as _,
1403            );
1404            let wstring = std::slice::from_raw_parts::<u16>(
1405                buffer.as_mut_ptr().cast::<u16>(),
1406                string_len as usize / 2,
1407            );
1408            let string = String::from_utf16_lossy(wstring);
1409            Some(string)
1410        } else {
1411            None
1412        }
1413    }
1414}
1415
1416fn basic_vkcode_to_string(code: u16, modifiers: Modifiers) -> Option<Keystroke> {
1417    let mapped_code = unsafe { MapVirtualKeyW(code as u32, MAPVK_VK_TO_CHAR) };
1418
1419    let key = match mapped_code {
1420        0 => None,
1421        raw_code => char::from_u32(raw_code),
1422    }?
1423    .to_ascii_lowercase();
1424
1425    let key = if matches!(code as u32, 112..=135) {
1426        format!("f{key}")
1427    } else {
1428        key.to_string()
1429    };
1430
1431    Some(Keystroke {
1432        modifiers,
1433        key,
1434        key_char: None,
1435    })
1436}
1437
1438#[inline]
1439fn is_virtual_key_pressed(vkey: VIRTUAL_KEY) -> bool {
1440    unsafe { GetKeyState(vkey.0 as i32) < 0 }
1441}
1442
1443fn is_modifier(virtual_key: VIRTUAL_KEY) -> bool {
1444    matches!(
1445        virtual_key,
1446        VK_CONTROL | VK_MENU | VK_SHIFT | VK_LWIN | VK_RWIN
1447    )
1448}
1449
1450#[inline]
1451pub(crate) fn current_modifiers() -> Modifiers {
1452    Modifiers {
1453        control: is_virtual_key_pressed(VK_CONTROL),
1454        alt: is_virtual_key_pressed(VK_MENU),
1455        shift: is_virtual_key_pressed(VK_SHIFT),
1456        platform: is_virtual_key_pressed(VK_LWIN) || is_virtual_key_pressed(VK_RWIN),
1457        function: false,
1458    }
1459}
1460
1461fn get_client_area_insets(
1462    handle: HWND,
1463    is_maximized: bool,
1464    windows_version: WindowsVersion,
1465) -> RECT {
1466    // For maximized windows, Windows outdents the window rect from the screen's client rect
1467    // by `frame_thickness` on each edge, meaning `insets` must contain `frame_thickness`
1468    // on all sides (including the top) to avoid the client area extending onto adjacent
1469    // monitors.
1470    //
1471    // For non-maximized windows, things become complicated:
1472    //
1473    // - On Windows 10
1474    // The top inset must be zero, since if there is any nonclient area, Windows will draw
1475    // a full native titlebar outside the client area. (This doesn't occur in the maximized
1476    // case.)
1477    //
1478    // - On Windows 11
1479    // The top inset is calculated using an empirical formula that I derived through various
1480    // tests. Without this, the top 1-2 rows of pixels in our window would be obscured.
1481    let dpi = unsafe { GetDpiForWindow(handle) };
1482    let frame_thickness = get_frame_thickness(dpi);
1483    let top_insets = if is_maximized {
1484        frame_thickness
1485    } else {
1486        match windows_version {
1487            WindowsVersion::Win10 => 0,
1488            WindowsVersion::Win11 => (dpi as f32 / USER_DEFAULT_SCREEN_DPI as f32).round() as i32,
1489        }
1490    };
1491    RECT {
1492        left: frame_thickness,
1493        top: top_insets,
1494        right: frame_thickness,
1495        bottom: frame_thickness,
1496    }
1497}
1498
1499// there is some additional non-visible space when talking about window
1500// borders on Windows:
1501// - SM_CXSIZEFRAME: The resize handle.
1502// - SM_CXPADDEDBORDER: Additional border space that isn't part of the resize handle.
1503fn get_frame_thickness(dpi: u32) -> i32 {
1504    let resize_frame_thickness = unsafe { GetSystemMetricsForDpi(SM_CXSIZEFRAME, dpi) };
1505    let padding_thickness = unsafe { GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi) };
1506    resize_frame_thickness + padding_thickness
1507}
1508
1509fn notify_frame_changed(handle: HWND) {
1510    unsafe {
1511        SetWindowPos(
1512            handle,
1513            None,
1514            0,
1515            0,
1516            0,
1517            0,
1518            SWP_FRAMECHANGED
1519                | SWP_NOACTIVATE
1520                | SWP_NOCOPYBITS
1521                | SWP_NOMOVE
1522                | SWP_NOOWNERZORDER
1523                | SWP_NOREPOSITION
1524                | SWP_NOSENDCHANGING
1525                | SWP_NOSIZE
1526                | SWP_NOZORDER,
1527        )
1528        .log_err();
1529    }
1530}
1531
1532fn start_tracking_mouse(
1533    handle: HWND,
1534    state_ptr: &Rc<WindowsWindowStatePtr>,
1535    flags: TRACKMOUSEEVENT_FLAGS,
1536) {
1537    let mut lock = state_ptr.state.borrow_mut();
1538    if !lock.hovered {
1539        lock.hovered = true;
1540        unsafe {
1541            TrackMouseEvent(&mut TRACKMOUSEEVENT {
1542                cbSize: std::mem::size_of::<TRACKMOUSEEVENT>() as u32,
1543                dwFlags: flags,
1544                hwndTrack: handle,
1545                dwHoverTime: HOVER_DEFAULT,
1546            })
1547            .log_err()
1548        };
1549        if let Some(mut callback) = lock.callbacks.hovered_status_change.take() {
1550            drop(lock);
1551            callback(true);
1552            state_ptr.state.borrow_mut().callbacks.hovered_status_change = Some(callback);
1553        }
1554    }
1555}
1556
1557fn with_input_handler<F, R>(state_ptr: &Rc<WindowsWindowStatePtr>, f: F) -> Option<R>
1558where
1559    F: FnOnce(&mut PlatformInputHandler) -> R,
1560{
1561    let mut input_handler = state_ptr.state.borrow_mut().input_handler.take()?;
1562    let result = f(&mut input_handler);
1563    state_ptr.state.borrow_mut().input_handler = Some(input_handler);
1564    Some(result)
1565}
1566
1567fn with_input_handler_and_scale_factor<F, R>(
1568    state_ptr: &Rc<WindowsWindowStatePtr>,
1569    f: F,
1570) -> Option<R>
1571where
1572    F: FnOnce(&mut PlatformInputHandler, f32) -> Option<R>,
1573{
1574    let mut lock = state_ptr.state.borrow_mut();
1575    let mut input_handler = lock.input_handler.take()?;
1576    let scale_factor = lock.scale_factor;
1577    drop(lock);
1578    let result = f(&mut input_handler, scale_factor);
1579    state_ptr.state.borrow_mut().input_handler = Some(input_handler);
1580    result
1581}