use crate::canvas::Canvas;
use crate::color::Color;
use crate::event::{Button, Cursor, Event, Key, Mods};
use crate::file_dialog::FileDialogRequest;
use crate::frameless::HitTest;
use crate::geom::{Rect, Size, pt, rect};
use crate::id::Id;
use crate::platform::{Clipboard, PicturesAndFiles, Window, WindowPlacement};
use crate::screens::{Screen, Spot, spot};
use crate::second_window::{WindowKind, WindowOpening};
use crate::text::{Face, Font, Metrics, TextSystem};
use crate::touch_windows::{self, WM_POINTERCAPTURECHANGED, WM_POINTERUPDATE};
use crate::ui::Ui;
use crate::uia;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::ffi::OsString;
use std::os::windows::ffi::{OsStrExt, OsStringExt};
use std::path::PathBuf;
use std::rc::Rc;
use std::time::Instant;
#[allow(non_snake_case, non_camel_case_types, clippy::upper_case_acronyms)]
pub(crate) mod sys {
pub type HANDLE = isize;
pub type WNDPROC = unsafe extern "system" fn(HANDLE, u32, usize, isize) -> isize;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct POINT {
pub x: i32,
pub y: i32,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct RECT {
pub left: i32,
pub top: i32,
pub right: i32,
pub bottom: i32,
}
#[repr(C)]
#[derive(Default)]
pub struct Stale {
pub header: [u32; 4],
pub around: RECT,
pub parts: [RECT; 8],
}
#[repr(C)]
pub struct WNDCLASSW {
pub style: u32,
pub proc: Option<WNDPROC>,
pub class_extra: i32,
pub window_extra: i32,
pub instance: HANDLE,
pub icon: HANDLE,
pub cursor: HANDLE,
pub background: HANDLE,
pub menu: *const u16,
pub name: *const u16,
}
#[repr(C)]
#[derive(Default)]
pub struct ICONINFO {
pub icon: i32,
pub x: u32,
pub y: u32,
pub mask: HANDLE,
pub color: HANDLE,
}
#[repr(C)]
#[derive(Default)]
pub struct MSG {
pub hwnd: HANDLE,
pub message: u32,
pub wparam: usize,
pub lparam: isize,
pub time: u32,
pub pt: POINT,
}
#[repr(C)]
pub struct PAINTSTRUCT {
pub hdc: HANDLE,
pub erase: i32,
pub paint: RECT,
pub restore: i32,
pub update: i32,
pub reserved: [u8; 32],
}
#[repr(C)]
#[derive(Default)]
pub struct BITMAPINFO {
pub size: u32,
pub width: i32,
pub height: i32,
pub planes: u16,
pub bits: u16,
pub compression: u32,
pub image_size: u32,
pub x_per_meter: i32,
pub y_per_meter: i32,
pub colors_used: u32,
pub colors_important: u32,
pub colors: [u32; 1],
}
#[repr(C)]
#[derive(Default)]
pub struct TEXTMETRICW {
pub height: i32,
pub ascent: i32,
pub descent: i32,
pub internal_leading: i32,
pub external_leading: i32,
pub ave_char_width: i32,
pub max_char_width: i32,
pub weight: i32,
pub overhang: i32,
pub aspect_x: i32,
pub aspect_y: i32,
pub chars: [u16; 4],
pub flags: [u8; 5],
}
#[repr(C)]
#[derive(Default)]
pub struct GLYPHMETRICS {
pub wide: u32,
pub high: u32,
pub origin: POINT,
pub advance: [i16; 2],
}
#[repr(C)]
pub struct TRACKMOUSEEVENT {
pub size: u32,
pub flags: u32,
pub hwnd: HANDLE,
pub hover_time: u32,
}
#[repr(C)]
pub struct MINMAXINFO {
pub reserved: POINT,
pub max_size: POINT,
pub max_position: POINT,
pub min_track: POINT,
pub max_track: POINT,
}
#[repr(C)]
pub struct COMPOSITIONFORM {
pub style: u32,
pub at: POINT,
pub area: RECT,
}
#[repr(C)]
pub struct CANDIDATEFORM {
pub index: u32,
pub style: u32,
pub at: POINT,
pub area: RECT,
}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct FORMATETC {
pub format: u16,
pub device: *const core::ffi::c_void,
pub aspect: u32,
pub index: i32,
pub medium: u32,
}
#[repr(C)]
pub struct STGMEDIUM {
pub medium: u32,
pub memory: HANDLE,
pub owner: *mut core::ffi::c_void,
}
#[repr(C)]
pub struct Layered {
pub size: u32,
pub screen: HANDLE,
pub at: *const POINT,
pub big: *const POINT,
pub picture: HANDLE,
pub from: *const POINT,
pub key: u32,
pub blend: *const [u8; 4],
pub flags: u32,
pub dirty: *const RECT,
}
#[repr(C)]
pub struct MONITORINFO {
pub size: u32,
pub all: RECT,
pub work: RECT,
pub flags: u32,
}
#[repr(C)]
pub struct IDataObject {
pub table: *const IDataObjectVtbl,
}
#[repr(C)]
pub struct IDataObjectVtbl {
pub unknown: [usize; 3],
pub get_data: unsafe extern "system" fn(*mut IDataObject, *const FORMATETC, *mut STGMEDIUM) -> i32,
pub get_data_here: usize,
pub query_get_data: unsafe extern "system" fn(*mut IDataObject, *const FORMATETC) -> i32,
}
#[link(name = "kernel32")]
unsafe extern "system" {
pub fn GetModuleHandleW(name: *const u16) -> HANDLE;
pub fn GlobalAlloc(flags: u32, bytes: usize) -> HANDLE;
pub fn GlobalLock(mem: HANDLE) -> *mut u16;
pub fn GlobalUnlock(mem: HANDLE) -> i32;
pub fn GlobalSize(mem: HANDLE) -> usize;
pub fn GlobalFree(mem: HANDLE) -> HANDLE;
pub fn MultiByteToWideChar(page: u32, flags: u32, bytes: *const u8, len: i32, units: *mut u16, room: i32) -> i32;
pub fn GetLocalTime(time: *mut [u16; 8]);
pub fn GetLogicalDrives() -> u32;
pub fn SetThreadErrorMode(mode: u32, was: *mut u32) -> i32;
}
#[link(name = "user32")]
unsafe extern "system" {
pub fn RegisterClassW(class: *const WNDCLASSW) -> u16;
pub fn CreateWindowExW(
ex: u32,
class: *const u16,
title: *const u16,
style: u32,
x: i32,
y: i32,
w: i32,
h: i32,
parent: HANDLE,
menu: HANDLE,
instance: HANDLE,
param: *mut core::ffi::c_void,
) -> HANDLE;
pub fn DestroyWindow(hwnd: HANDLE) -> i32;
pub fn DefWindowProcW(hwnd: HANDLE, msg: u32, w: usize, l: isize) -> isize;
pub fn GetMessageW(msg: *mut MSG, hwnd: HANDLE, min: u32, max: u32) -> i32;
pub fn TranslateMessage(msg: *const MSG) -> i32;
pub fn DispatchMessageW(msg: *const MSG) -> isize;
pub fn PostQuitMessage(code: i32);
pub fn PostMessageW(hwnd: HANDLE, msg: u32, w: usize, l: isize) -> i32;
pub fn SendMessageW(hwnd: HANDLE, msg: u32, w: usize, l: isize) -> isize;
pub fn SetWindowTextW(hwnd: HANDLE, text: *const u16) -> i32;
pub fn ShowWindow(hwnd: HANDLE, how: i32) -> i32;
pub fn IsWindowVisible(hwnd: HANDLE) -> i32;
pub fn GetForegroundWindow() -> HANDLE;
pub fn GetFocus() -> HANDLE;
pub fn GetWindowRect(hwnd: HANDLE, area: *mut RECT) -> i32;
pub fn MonitorFromWindow(hwnd: HANDLE, otherwise: u32) -> HANDLE;
pub fn MonitorFromRect(area: *const RECT, otherwise: u32) -> HANDLE;
pub fn IsZoomed(hwnd: HANDLE) -> i32;
pub fn IsIconic(hwnd: HANDLE) -> i32;
pub fn SetForegroundWindow(hwnd: HANDLE) -> i32;
pub fn GetWindowPlacement(hwnd: HANDLE, place: *mut [i32; 11]) -> i32;
pub fn SetWindowPlacement(hwnd: HANDLE, place: *const [i32; 11]) -> i32;
pub fn GetMonitorInfoW(screen: HANDLE, info: *mut MONITORINFO) -> i32;
pub fn EnumDisplayMonitors(dc: HANDLE, clip: *const RECT, each: unsafe extern "system" fn(HANDLE, HANDLE, *mut RECT, isize) -> i32, with: isize) -> i32;
pub fn EnableWindow(hwnd: HANDLE, on: i32) -> i32;
pub fn ShutdownBlockReasonCreate(hwnd: HANDLE, why: *const u16) -> i32;
pub fn ShutdownBlockReasonDestroy(hwnd: HANDLE) -> i32;
pub fn SetWindowPos(hwnd: HANDLE, after: HANDLE, x: i32, y: i32, w: i32, h: i32, flags: u32) -> i32;
pub fn InvalidateRect(hwnd: HANDLE, area: *const RECT, erase: i32) -> i32;
pub fn UpdateLayeredWindowIndirect(hwnd: HANDLE, how: *const Layered) -> i32;
pub fn GetUpdateRgn(hwnd: HANDLE, region: HANDLE, erase: i32) -> i32;
pub fn BeginPaint(hwnd: HANDLE, ps: *mut PAINTSTRUCT) -> HANDLE;
pub fn EndPaint(hwnd: HANDLE, ps: *const PAINTSTRUCT) -> i32;
pub fn GetClientRect(hwnd: HANDLE, area: *mut RECT) -> i32;
pub fn LoadCursorW(instance: HANDLE, name: *const u16) -> HANDLE;
pub fn LoadIconW(instance: HANDLE, name: *const u16) -> HANDLE;
pub fn CreateIconIndirect(parts: *const ICONINFO) -> HANDLE;
#[cfg_attr(target_pointer_width = "32", link_name = "SetClassLongW")]
pub fn SetClassLongPtrW(hwnd: HANDLE, which: i32, value: isize) -> usize;
pub fn GetSystemMetrics(which: i32) -> i32;
pub fn SetCursor(cursor: HANDLE) -> HANDLE;
pub fn SetCapture(hwnd: HANDLE) -> HANDLE;
pub fn GetCapture() -> HANDLE;
pub fn ReleaseCapture() -> i32;
pub fn TrackMouseEvent(track: *mut TRACKMOUSEEVENT) -> i32;
pub fn ScreenToClient(hwnd: HANDLE, point: *mut POINT) -> i32;
pub fn ClientToScreen(hwnd: HANDLE, point: *mut POINT) -> i32;
pub fn GetCursorPos(point: *mut POINT) -> i32;
pub fn WindowFromPoint(point: POINT) -> HANDLE;
pub fn GetKeyState(key: i32) -> i16;
pub fn SetTimer(hwnd: HANDLE, id: usize, ms: u32, proc: usize) -> usize;
pub fn KillTimer(hwnd: HANDLE, id: usize) -> i32;
pub fn GetDpiForWindow(hwnd: HANDLE) -> u32;
pub fn SetProcessDpiAwarenessContext(context: isize) -> i32;
pub fn AdjustWindowRectExForDpi(area: *mut RECT, style: u32, menu: i32, ex: u32, dpi: u32) -> i32;
#[cfg_attr(target_pointer_width = "32", link_name = "SetWindowLongW")]
pub fn SetWindowLongPtrW(hwnd: HANDLE, index: i32, value: isize) -> isize;
#[cfg_attr(target_pointer_width = "32", link_name = "GetWindowLongW")]
pub fn GetWindowLongPtrW(hwnd: HANDLE, index: i32) -> isize;
pub fn OpenClipboard(owner: HANDLE) -> i32;
pub fn CloseClipboard() -> i32;
pub fn EmptyClipboard() -> i32;
pub fn GetClipboardData(format: u32) -> HANDLE;
pub fn SetClipboardData(format: u32, mem: HANDLE) -> HANDLE;
pub fn RegisterClipboardFormatW(name: *const u16) -> u32;
}
#[link(name = "gdi32")]
unsafe extern "system" {
pub fn CreateCompatibleDC(dc: HANDLE) -> HANDLE;
pub fn CreateBitmap(width: i32, height: i32, planes: u32, bits: u32, pixels: *const u8) -> HANDLE;
pub fn CreateDIBSection(dc: HANDLE, info: *const BITMAPINFO, usage: u32, bits: *mut *mut u32, section: HANDLE, offset: u32) -> HANDLE;
pub fn SelectObject(dc: HANDLE, object: HANDLE) -> HANDLE;
pub fn GetStockObject(which: i32) -> HANDLE;
pub fn DeleteObject(object: HANDLE) -> i32;
pub fn DeleteDC(dc: HANDLE) -> i32;
pub fn CreateRectRgn(left: i32, top: i32, right: i32, bottom: i32) -> HANDLE;
#[cfg(test)]
pub fn CombineRgn(into: HANDLE, one: HANDLE, other: HANDLE, how: i32) -> i32;
pub fn GetRegionData(region: HANDLE, room: u32, data: *mut Stale) -> u32;
pub fn BitBlt(dest: HANDLE, x: i32, y: i32, w: i32, h: i32, src: HANDLE, sx: i32, sy: i32, op: u32) -> i32;
pub fn CreateFontW(
height: i32,
width: i32,
escapement: i32,
orientation: i32,
weight: i32,
italic: u32,
underline: u32,
strike: u32,
charset: u32,
out: u32,
clip: u32,
quality: u32,
pitch: u32,
face: *const u16,
) -> HANDLE;
pub fn SetTextColor(dc: HANDLE, color: u32) -> u32;
pub fn SetBkMode(dc: HANDLE, mode: i32) -> i32;
pub fn TextOutW(dc: HANDLE, x: i32, y: i32, text: *const u16, len: i32) -> i32;
pub fn GetTextExtentExPointW(dc: HANDLE, text: *const u16, len: i32, max: i32, fit: *mut i32, each: *mut i32, size: *mut POINT) -> i32;
pub fn GetTextMetricsW(dc: HANDLE, metrics: *mut TEXTMETRICW) -> i32;
pub fn GetTextFaceW(dc: HANDLE, room: i32, name: *mut u16) -> i32;
pub fn SaveDC(dc: HANDLE) -> i32;
pub fn RestoreDC(dc: HANDLE, saved: i32) -> i32;
pub fn IntersectClipRect(dc: HANDLE, left: i32, top: i32, right: i32, bottom: i32) -> i32;
pub fn SetViewportOrgEx(dc: HANDLE, x: i32, y: i32, old: *mut POINT) -> i32;
pub fn GdiFlush() -> i32;
pub fn GetGlyphOutlineW(dc: HANDLE, letter: u32, format: u32, metrics: *mut GLYPHMETRICS, size: u32, buffer: *mut u8, turn: *const [u16; 8]) -> u32;
}
#[link(name = "shcore")]
unsafe extern "system" {
pub fn GetDpiForMonitor(screen: HANDLE, kind: u32, x: *mut u32, y: *mut u32) -> i32;
}
#[link(name = "dwmapi")]
unsafe extern "system" {
pub fn DwmSetWindowAttribute(hwnd: HANDLE, attribute: u32, value: *const core::ffi::c_void, size: u32) -> i32;
}
#[link(name = "imm32")]
unsafe extern "system" {
pub fn ImmGetContext(hwnd: HANDLE) -> HANDLE;
pub fn ImmReleaseContext(hwnd: HANDLE, context: HANDLE) -> i32;
pub fn ImmSetCompositionWindow(context: HANDLE, form: *const COMPOSITIONFORM) -> i32;
pub fn ImmSetCandidateWindow(context: HANDLE, form: *const CANDIDATEFORM) -> i32;
pub fn ImmAssociateContextEx(hwnd: HANDLE, context: HANDLE, how: u32) -> i32;
}
#[link(name = "shell32")]
unsafe extern "system" {
pub fn DragFinish(drop: HANDLE);
pub fn SHCreateItemFromParsingName(name: *const u16, bind: *mut core::ffi::c_void, interface: *const [u32; 4], out: *mut *mut super::Com) -> i32;
}
#[link(name = "ole32")]
unsafe extern "system" {
pub fn OleInitialize(reserved: *mut core::ffi::c_void) -> i32;
pub fn CoInitializeEx(reserved: *mut core::ffi::c_void, how: u32) -> i32;
pub fn CoUninitialize();
pub fn CoCreateInstance(of: *const [u32; 4], outer: *mut core::ffi::c_void, context: u32, interface: *const [u32; 4], out: *mut *mut super::Com) -> i32;
pub fn CoTaskMemFree(memory: *mut core::ffi::c_void);
pub fn OleUninitialize();
pub fn RegisterDragDrop(hwnd: HANDLE, target: *mut core::ffi::c_void) -> i32;
pub fn RevokeDragDrop(hwnd: HANDLE) -> i32;
pub fn ReleaseStgMedium(medium: *mut STGMEDIUM);
}
}
use sys::*;
const WM_DESTROY: u32 = 0x0002;
const WM_MOVE: u32 = 0x0003;
const WM_SIZE: u32 = 0x0005;
const WM_SETTINGCHANGE: u32 = 0x001A;
const WM_QUERYENDSESSION: u32 = 0x0011;
const WM_ENDSESSION: u32 = 0x0016;
const WM_DISPLAYCHANGE: u32 = 0x007E;
const WM_GETDPISCALEDSIZE: u32 = 0x02E4;
const WM_SYSCOLORCHANGE: u32 = 0x0015;
const WM_DWMCOLORIZATIONCOLORCHANGED: u32 = 0x0320;
const WM_SETFOCUS: u32 = 0x0007;
const WM_KILLFOCUS: u32 = 0x0008;
const WM_PAINT: u32 = 0x000F;
const WM_CLOSE: u32 = 0x0010;
const WM_ERASEBKGND: u32 = 0x0014;
const WM_SETCURSOR: u32 = 0x0020;
const WM_GETMINMAXINFO: u32 = 0x0024;
const WM_NCCALCSIZE: u32 = 0x0083;
const WM_NCHITTEST: u32 = 0x0084;
const WM_GETOBJECT: u32 = 0x003D;
const WM_KEYDOWN: u32 = 0x0100;
const WM_CHAR: u32 = 0x0102;
const WM_SYSKEYDOWN: u32 = 0x0104;
const WM_KEYUP: u32 = 0x0101;
const WM_SYSKEYUP: u32 = 0x0105;
const WM_SYSCHAR: u32 = 0x0106;
const WM_SYSCOMMAND: u32 = 0x0112;
const SC_KEYMENU: usize = 0xF100;
const WM_IME_STARTCOMPOSITION: u32 = 0x010D;
const WM_IME_COMPOSITION: u32 = 0x010F;
const WM_TIMER: u32 = 0x0113;
const WM_MOUSEMOVE: u32 = 0x0200;
const WM_LBUTTONDOWN: u32 = 0x0201;
const WM_LBUTTONUP: u32 = 0x0202;
const WM_RBUTTONDOWN: u32 = 0x0204;
const WM_RBUTTONUP: u32 = 0x0205;
const WM_MBUTTONDOWN: u32 = 0x0207;
const WM_MBUTTONUP: u32 = 0x0208;
const WM_MOUSEWHEEL: u32 = 0x020A;
const MK_CONTROL: usize = 0x0008;
const WM_MOUSEHWHEEL: u32 = 0x020E;
const WM_DROPFILES: u32 = 0x0233;
const WM_MOUSELEAVE: u32 = 0x02A3;
const WM_DPICHANGED: u32 = 0x02E0;
const WM_WAKE: u32 = 0x8001;
const WM_ANSWERED: u32 = 0x8003;
const WS_OVERLAPPEDWINDOW: u32 = 0x00CF_0000;
const WS_PLAIN: u32 = 0x800A_0000;
pub(crate) fn wide(text: &str) -> Vec<u16> {
text.encode_utf16().chain([0]).collect()
}
struct Strip {
dc: HANDLE,
bitmap: Cell<HANDLE>,
pixels: Cell<*mut u32>,
width: Cell<i32>,
rows: Cell<i32>,
}
impl Strip {
fn fit(&self, width: i32) {
if width == self.width.get() || width <= 0 {
return;
}
let rows = (65_536 / width).clamp(16, 256);
let info = BITMAPINFO { size: 40, width, height: -rows, planes: 1, bits: 32, compression: 0, image_size: 0, x_per_meter: 0, y_per_meter: 0, colors_used: 0, colors_important: 0, colors: [0] };
let mut pixels = std::ptr::null_mut();
let bitmap = unsafe { CreateDIBSection(self.dc, &info, 0, &mut pixels, 0, 0) };
if bitmap == 0 || pixels.is_null() {
return;
}
unsafe {
SelectObject(self.dc, bitmap);
if self.bitmap.get() != 0 {
DeleteObject(self.bitmap.get());
}
}
self.bitmap.set(bitmap);
self.pixels.set(pixels);
self.width.set(width);
self.rows.set(rows);
}
}
impl Drop for Strip {
fn drop(&mut self) {
unsafe {
DeleteDC(self.dc);
DeleteObject(self.bitmap.get());
}
}
}
fn font_named(dc: HANDLE, name: &str, px: i32, weight: i32, italic: bool) -> Option<HANDLE> {
let mut got = [0u16; 32];
unsafe {
let handle = CreateFontW(-px, 0, 0, 0, weight, italic as u32, 0, 0, 1, 0, 0, 5, 0, wide(name).as_ptr());
SelectObject(dc, handle);
GetTextFaceW(dc, got.len() as i32, got.as_mut_ptr());
let chosen = String::from_utf16_lossy(got.split(|unit| *unit == 0).next().unwrap_or_default());
if chosen.eq_ignore_ascii_case(name) {
return Some(handle);
}
SelectObject(dc, GetStockObject(13));
DeleteObject(handle);
}
None
}
const MADE: usize = 256;
const GLYPHS: usize = 8192;
struct Fonts {
dc: HANDLE,
made: HashMap<(Face, i32), (HANDLE, Metrics)>,
utf16: Vec<u16>,
each: Vec<i32>,
pieces: Vec<(std::ops::Range<usize>, Face)>,
}
impl Fonts {
fn new(dc: HANDLE) -> Self {
Fonts { dc, made: HashMap::new(), utf16: Vec::new(), each: Vec::new(), pieces: Vec::new() }
}
fn font(&mut self, font: Font, scale: f32) -> (HANDLE, Metrics) {
let (px, dc) = ((font.size * scale).round() as i32, self.dc);
if self.made.len() >= MADE && !self.made.contains_key(&(font.face, px)) {
self.free();
}
*self.made.entry((font.face, px)).or_insert_with(|| {
let asked = crate::system_fonts::asked(font.face);
let px = ((px as f32 * asked.scale).round() as i32).max(1);
unsafe {
let handle = asked.names.iter().find_map(|name| font_named(dc, name, px, asked.weight, asked.italic)).unwrap_or_else(|| GetStockObject(17));
let (mut m, mut letter, upright) = (TEXTMETRICW::default(), GLYPHMETRICS::default(), [0, 1, 0, 0, 0, 0, 0, 1]);
SelectObject(dc, handle);
GetTextMetricsW(dc, &mut m);
let mut reach = |of: char| if GetGlyphOutlineW(dc, of as u32, 0, &mut letter, 0, std::ptr::null_mut(), &upright) == u32::MAX { 0.0 } else { letter.origin.y as f32 / scale };
(handle, Metrics { ascent: m.ascent as f32 / scale, descent: m.descent as f32 / scale, cap_height: reach('H'), x_height: reach('x') })
}
})
}
fn measure(&mut self, text: &str, font: Font, scale: f32, mut stops: Option<&mut Vec<f32>>) -> f32 {
if !crate::system_fonts::pieces(text, font.face, &mut self.pieces) {
return self.measure_one(text, font, scale, stops);
}
let (pieces, mut wide) = (std::mem::take(&mut self.pieces), 0.0);
for (bytes, face) in &pieces {
let from = stops.as_ref().map_or(0, |stops| stops.len());
let piece = self.measure_one(&text[bytes.clone()], Font { face: *face, ..font }, scale, stops.as_deref_mut());
stops.iter_mut().for_each(|stops| stops[from..].iter_mut().for_each(|stop| *stop += wide));
wide += piece;
}
self.pieces = pieces;
wide
}
fn measure_one(&mut self, text: &str, font: Font, scale: f32, stops: Option<&mut Vec<f32>>) -> f32 {
if text.is_empty() {
return 0.0;
}
let (handle, _) = self.font(font, scale);
self.utf16.clear();
self.utf16.extend(text.encode_utf16());
self.each.resize(self.utf16.len(), 0);
let mut size = POINT::default();
unsafe {
SelectObject(self.dc, handle);
GetTextExtentExPointW(self.dc, self.utf16.as_ptr(), self.utf16.len() as i32, 0, std::ptr::null_mut(), self.each.as_mut_ptr(), &mut size);
}
let ends = text.chars().scan(0, |unit, c| Some(std::mem::replace(unit, *unit + c.len_utf16()) + c.len_utf16() - 1));
stops.into_iter().for_each(|stops| stops.extend(ends.clone().map(|end| self.each[end] as f32 / scale)));
size.x as f32 / scale
}
fn free(&mut self) {
unsafe {
SelectObject(self.dc, GetStockObject(13));
self.made.drain().for_each(|(_, (font, _))| _ = DeleteObject(font));
}
}
}
struct GdiText {
strip: Rc<Strip>,
fonts: Fonts,
}
impl Drop for GdiText {
fn drop(&mut self) {
self.fonts.free();
}
}
impl TextSystem for GdiText {
fn metrics(&mut self, font: Font, scale: f32) -> Metrics {
self.fonts.font(font, scale).1
}
fn measure(&mut self, text: &str, font: Font, scale: f32, stops: Option<&mut Vec<f32>>) -> f32 {
self.fonts.measure(text, font, scale, stops)
}
fn draw(&mut self, canvas: &mut Canvas, text: &str, x: f32, y: f32, font: Font, color: Color) {
if !crate::system_fonts::pieces(text, font.face, &mut self.fonts.pieces) {
return self.draw_one(canvas, text, x, y, font, color);
}
let (pieces, scale, mut pen) = (std::mem::take(&mut self.fonts.pieces), canvas.scale(), x);
let ascent = self.fonts.font(font, scale).1.ascent;
for (bytes, face) in &pieces {
let piece = Font { face: *face, ..font };
let lower = ascent - self.fonts.font(piece, scale).1.ascent;
self.draw_one(canvas, &text[bytes.clone()], pen, y + lower, piece, color);
pen += self.fonts.measure_one(&text[bytes.clone()], piece, scale, None);
}
self.fonts.pieces = pieces;
}
}
impl GdiText {
fn draw_one(&mut self, canvas: &mut Canvas, text: &str, x: f32, y: f32, font: Font, color: Color) {
let scale = canvas.scale();
let (handle, _) = self.fonts.font(font, scale);
self.fonts.utf16.clear();
self.fonts.utf16.extend(text.encode_utf16());
let (dc, units) = (self.strip.dc, &self.fonts.utf16);
let (origin, clip) = (canvas.origin(), canvas.clip_pixels());
let ink = color.0;
unsafe {
let saved = SaveDC(dc);
SetViewportOrgEx(dc, -origin.0, -origin.1, std::ptr::null_mut());
IntersectClipRect(dc, clip[0], clip[1], clip[2], clip[3]);
SelectObject(dc, handle);
SetBkMode(dc, 1);
SetTextColor(dc, (ink >> 16 & 0xff) | (ink & 0xff00) | (ink & 0xff) << 16);
TextOutW(dc, (x * scale).round() as i32, (y * scale).round() as i32, units.as_ptr(), units.len() as i32);
RestoreDC(dc, saved);
GdiFlush();
}
}
}
struct Glyph {
width: usize,
left: i32,
top: i32,
advance: i32,
cover: Vec<u8>,
}
pub struct SystemText {
fonts: Fonts,
glyphs: HashMap<(Face, i32, char), Glyph>,
}
impl SystemText {
pub fn new() -> Option<Self> {
let dc = unsafe { CreateCompatibleDC(0) };
(dc != 0).then(|| SystemText { fonts: Fonts::new(dc), glyphs: HashMap::new() })
}
fn glyph(&mut self, font: Font, scale: f32, letter: char) -> &Glyph {
let ((handle, _), px, dc) = (self.fonts.font(font, scale), (font.size * scale).round() as i32, self.fonts.dc);
if self.glyphs.len() >= GLYPHS && !self.glyphs.contains_key(&(font.face, px, letter)) {
self.glyphs.clear();
}
self.glyphs.entry((font.face, px, letter)).or_insert_with(|| {
let (mut m, upright) = (GLYPHMETRICS::default(), [0, 1, 0, 0, 0, 0, 0, 1]);
let shades = unsafe {
SelectObject(dc, handle);
let size = GetGlyphOutlineW(dc, letter as u32, 6, &mut m, 0, std::ptr::null_mut(), &upright);
let mut shades = vec![0u8; if size == u32::MAX { 0 } else { size as usize }];
GetGlyphOutlineW(dc, letter as u32, 6, &mut m, shades.len() as u32, shades.as_mut_ptr(), &upright);
shades
};
let (width, row) = (m.wide as usize, (m.wide as usize + 3) & !3);
let cover = shades.chunks(row.max(1)).take(m.high as usize).flat_map(|row| &row[..width]).map(|shade| (*shade as u32 * 255 / 64) as u8).collect();
Glyph { width, left: m.origin.x, top: m.origin.y, advance: if shades.is_empty() && m.advance[0] == 0 { px / 3 } else { m.advance[0] as i32 }, cover }
})
}
}
impl std::fmt::Debug for SystemText {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SystemText").field("fonts", &self.fonts.made.len()).field("letters", &self.glyphs.len()).finish()
}
}
impl Drop for SystemText {
fn drop(&mut self) {
self.fonts.free();
unsafe { DeleteDC(self.fonts.dc) };
}
}
impl TextSystem for SystemText {
fn metrics(&mut self, font: Font, scale: f32) -> Metrics {
self.fonts.font(font, scale).1
}
fn measure(&mut self, text: &str, font: Font, scale: f32, stops: Option<&mut Vec<f32>>) -> f32 {
self.fonts.measure(text, font, scale, stops)
}
fn draw(&mut self, canvas: &mut Canvas, text: &str, x: f32, y: f32, font: Font, color: Color) {
let scale = canvas.scale();
let ascent = (self.fonts.font(font, scale).1.ascent * scale).round() as i32;
let (mut pen, line) = ((x * scale).round() as i32, (y * scale).round() as i32 + ascent);
for letter in text.chars() {
let glyph = self.glyph(Font { face: crate::system_fonts::face_for(font.face, letter), ..font }, scale, letter);
canvas.cover(pen + glyph.left, line - glyph.top, glyph.width, &glyph.cover, color);
pen += glyph.advance;
}
}
}
#[derive(Debug, Default)]
pub struct Messages {
high: u16,
window: isize,
}
impl Messages {
pub fn of(window: isize) -> Self {
Messages { high: 0, window }
}
pub unsafe fn events(&mut self, msg: u32, w: usize, l: isize, scale: f32, mut each: impl FnMut(Event)) {
if cfg!(feature = "touch") && (WM_POINTERUPDATE..=WM_POINTERCAPTURECHANGED).contains(&msg) {
return touch_windows::events(msg, w, scale, &mut each);
}
if cfg!(feature = "touch") && (WM_MOUSEMOVE..=WM_MBUTTONUP).contains(&msg) && touch_windows::made_up() {
return;
}
let at = Event::Move(pt((l & 0xffff) as i16 as f32 / scale, (l >> 16 & 0xffff) as i16 as f32 / scale));
let button = match msg {
WM_LBUTTONDOWN | WM_LBUTTONUP => Button::Left,
WM_RBUTTONDOWN | WM_RBUTTONUP => Button::Right,
_ => Button::Middle,
};
let turn = (w >> 16) as i16 as f32 / 120.0 * 48.0;
let events = match msg {
WM_MOUSEMOVE => [Some(at), None],
WM_MOUSELEAVE => [Some(Event::Leave), None],
WM_LBUTTONDOWN | WM_RBUTTONDOWN | WM_MBUTTONDOWN => [Some(at), Some(Event::Press(button, mods()))],
WM_LBUTTONUP | WM_RBUTTONUP | WM_MBUTTONUP => [Some(at), Some(Event::Release(button))],
WM_MOUSEHWHEEL => [self.wheel_at(l, scale), Some(Event::Wheel(turn, 0.0))],
WM_MOUSEWHEEL if w & MK_CONTROL != 0 => [self.wheel_at(l, scale), Some(Event::Zoom(crate::touch::wheel_zoom(turn / 48.0)))],
WM_MOUSEWHEEL if mods().shift => [self.wheel_at(l, scale), Some(Event::Wheel(-turn, 0.0))],
WM_MOUSEWHEEL => [self.wheel_at(l, scale), Some(Event::Wheel(0.0, -turn))],
WM_KEYDOWN | WM_SYSKEYDOWN if (0x10..=0x12).contains(&w) => [(l >> 30 & 1 == 0).then(|| Event::ModifiersChanged(held_with(w, true))), None],
WM_KEYUP | WM_SYSKEYUP if (0x10..=0x12).contains(&w) => [Some(Event::ModifiersChanged(held_with(w, false))), None],
WM_KEYDOWN | WM_SYSKEYDOWN => [key(w).map(|key| Event::Key(key, Mods { alt: mods().alt || l >> 29 & 1 == 1, ..mods() })), None],
WM_KEYUP | WM_SYSKEYUP => [key(w).map(Event::KeyUp), None],
WM_CHAR => [self.typed(w as u16).map(Event::Text), None],
WM_SETFOCUS => [Some(Event::Focused(true)), None],
WM_KILLFOCUS => [Some(Event::Focused(false)), None],
WM_DROPFILES => DROPS.get().map_or([None, None], |drops| unsafe { (drops.dropped)(w as HANDLE, scale) }),
_ => [None, None],
};
events.into_iter().flatten().for_each(&mut each);
}
fn wheel_at(&self, l: isize, scale: f32) -> Option<Event> {
pointer_at(self.window, POINT { x: (l & 0xffff) as i16 as i32, y: (l >> 16 & 0xffff) as i16 as i32 }, scale)
}
fn typed(&mut self, unit: u16) -> Option<String> {
if (0xD800..0xDC00).contains(&unit) {
self.high = unit;
return None;
}
let units = [std::mem::take(&mut self.high), unit];
let text = String::from_utf16_lossy(if units[0] != 0 { &units } else { &units[1..] });
(text.chars().all(|c| !c.is_control()) && (!mods().ctrl || mods().alt)).then_some(text)
}
}
fn pointer_at(window: HANDLE, mut screen: POINT, scale: f32) -> Option<Event> {
let known = window != 0 && unsafe { ScreenToClient(window, &mut screen) } != 0;
known.then(|| Event::Move(pt(screen.x as f32 / scale, screen.y as f32 / scale)))
}
unsafe fn dropped(drop: HANDLE, scale: f32) -> [Option<Event>; 2] {
let (Some(at), files) = (unsafe { dropped_files(drop) }) else { return [None, None] };
[Some(Event::Move(pt(at.x as f32 / scale, at.y as f32 / scale))), Some(Event::FilesDropped(files))]
}
unsafe fn dropped_files(drop: HANDLE) -> (Option<POINT>, Vec<PathBuf>) {
unsafe {
let start = GlobalLock(drop).cast::<u8>();
let block = if start.is_null() { &[][..] } else { std::slice::from_raw_parts(start, GlobalSize(drop)) };
let read = read_drop(block);
GlobalUnlock(drop);
read
}
}
fn read_drop(block: &[u8]) -> (Option<POINT>, Vec<PathBuf>) {
let number = |at: usize| block.get(at..).and_then(|rest| rest.first_chunk()).map_or(0, |four| i32::from_le_bytes(*four));
let at = (block.len() >= 20 && number(12) == 0).then_some(POINT { x: number(4), y: number(8) });
let start = number(0) as usize;
let names = block.get(start..).filter(|_| start >= 20).unwrap_or_default();
if number(16) == 0 {
return (at, names.split(|byte| *byte == 0).take_while(|name| !name.is_empty()).map(from_ansi).map(PathBuf::from).collect());
}
let units: Vec<u16> = names.as_chunks().0.iter().map(|pair| u16::from_le_bytes(*pair)).collect();
(at, units.split(|unit| *unit == 0).take_while(|name| !name.is_empty()).map(OsString::from_wide).map(PathBuf::from).collect())
}
fn from_ansi(name: &[u8]) -> OsString {
let mut units = vec![0u16; name.len()];
let len = unsafe { MultiByteToWideChar(0, 0, name.as_ptr(), name.len() as i32, units.as_mut_ptr(), units.len() as i32) };
OsString::from_wide(&units[..len.max(0) as usize])
}
fn cursor_pixels(cursor: Rect, scale: f32) -> RECT {
let pixel = |unit: f32| (unit * scale).round() as i32;
RECT { left: pixel(cursor.x), top: pixel(cursor.y), right: pixel(cursor.right()), bottom: pixel(cursor.bottom()) }
}
const CLASS: &str = "taut";
struct Open {
hwnd: HANDLE,
id: Id,
scale: f32,
wide: i32,
min_size: (f32, f32),
tracking: bool,
messages: Messages,
ime_spot: RECT,
normal: WindowPlacement,
ime_off: bool,
files: bool,
dark: bool,
high: i32,
edges: bool,
asks_hit_test: bool,
transparent: Option<SeeThrough>,
before_fullscreen: Option<(isize, [i32; 11])>,
blocked: bool,
withdrawn: bool,
#[cfg(feature = "gpu")]
shown: Option<crate::gpu_windows::Shown>,
#[cfg(feature = "gpu")]
cloaked: bool,
}
struct SeeThrough {
dc: HANDLE,
bitmap: HANDLE,
pixels: *mut u32,
size: [i32; 2],
}
impl SeeThrough {
fn new() -> SeeThrough {
SeeThrough { dc: unsafe { CreateCompatibleDC(0) }, bitmap: 0, pixels: std::ptr::null_mut(), size: [0; 2] }
}
fn fit(&mut self, size: [i32; 2]) -> bool {
if size == self.size || size[0] <= 0 || size[1] <= 0 {
return false;
}
let (info, mut pixels) = (BITMAPINFO { size: 40, width: size[0], height: -size[1], planes: 1, bits: 32, ..Default::default() }, std::ptr::null_mut());
unsafe {
let bitmap = CreateDIBSection(self.dc, &info, 0, &mut pixels, 0, 0);
if bitmap == 0 || pixels.is_null() {
return false;
}
SelectObject(self.dc, bitmap);
DeleteObject(self.bitmap);
(self.bitmap, self.pixels, self.size) = (bitmap, pixels, size);
}
true
}
fn draw(&mut self, ui: &mut Ui, area: Rect, scale: f32) {
if self.pixels.is_null() {
return;
}
let [wide, high] = self.size.map(|side| side as usize);
let block = unsafe { std::slice::from_raw_parts_mut(self.pixels, wide * high) };
let mut canvas = Canvas::new(block, wide, high, (0, 0), scale);
canvas.clip(area.snap_to_pixels(scale));
canvas.erase();
ui.paint(&mut canvas);
}
fn show(&self, hwnd: HANDLE, dirty: RECT) -> bool {
let (big, from, blend) = (POINT { x: self.size[0], y: self.size[1] }, POINT::default(), [0, 0, 255, 1]);
let how = Layered { size: size_of::<Layered>() as u32, screen: 0, at: std::ptr::null(), big: &big, picture: self.dc, from: &from, key: 0, blend: &blend, flags: 2, dirty: &dirty };
unsafe { UpdateLayeredWindowIndirect(hwnd, &how) != 0 }
}
}
impl Drop for SeeThrough {
fn drop(&mut self) {
unsafe {
DeleteDC(self.dc);
DeleteObject(self.bitmap);
}
}
}
unsafe fn show_minimized(hwnd: HANDLE, filling: bool) {
let mut place = [0i32; 11];
place[0] = 44;
unsafe {
GetWindowPlacement(hwnd, &mut place);
(place[1], place[2]) = (if filling { 2 } else { 0 }, 7);
SetWindowPlacement(hwnd, &place);
}
}
fn framed(w: f32, h: f32, dpi: u32, frame: Option<[u32; 2]>) -> RECT {
let scale = dpi as f32 / 96.0;
let mut area = RECT { left: 0, top: 0, right: (w * scale) as i32, bottom: (h * scale) as i32 };
if let Some([style, ex]) = frame {
unsafe { AdjustWindowRectExForDpi(&mut area, style, 0, ex, dpi) };
}
area
}
fn styles(kind: WindowKind, edges: bool, transparent: bool) -> [u32; 2] {
let style = if transparent && !edges { WS_PLAIN } else { WS_OVERLAPPEDWINDOW };
let (boxes, more) = match kind {
WindowKind::Plain => (0, 0),
WindowKind::Tool => (0x0003_0000, 0x80),
WindowKind::Dialog => (0x0003_0000, 1),
WindowKind::OnTop => (0, 8),
};
[style & !boxes, more | if transparent { 0x0008_0000 } else { 0 }]
}
fn styles_of(hwnd: HANDLE) -> Option<[u32; 2]> {
unsafe { (GetWindowLongPtrW(hwnd, 0) == 0).then(|| [GetWindowLongPtrW(hwnd, -16) as u32, GetWindowLongPtrW(hwnd, -20) as u32]) }
}
fn screens() -> Vec<Screen> {
let mut found = Vec::new();
unsafe { EnumDisplayMonitors(0, std::ptr::null(), each_screen, (&raw mut found) as isize) };
found
}
unsafe extern "system" fn each_screen(screen: HANDLE, _: HANDLE, _: *mut RECT, into: isize) -> i32 {
let (mut info, mut dpi) = (MONITORINFO { size: size_of::<MONITORINFO>() as u32, all: RECT::default(), work: RECT::default(), flags: 0 }, [96u32; 2]);
let area = |part: RECT| rect(part.left as f32, part.top as f32, (part.right - part.left) as f32, (part.bottom - part.top) as f32);
unsafe {
let found = &mut *(into as *mut Vec<Screen>);
if GetMonitorInfoW(screen, &mut info) != 0 {
GetDpiForMonitor(screen, 0, &mut dpi[0], &mut dpi[1]);
found.insert(if info.flags & 1 != 0 { 0 } else { found.len() }, Screen { rect: area(info.all), work_area: area(info.work), scale: dpi[0] as f32 / 96.0 });
}
}
1
}
unsafe fn all_inside(hwnd: HANDLE, l: isize) -> isize {
let (area, mut screen) = (l as *mut RECT, MONITORINFO { size: size_of::<MONITORINFO>() as u32, all: RECT::default(), work: RECT::default(), flags: 0 });
unsafe {
let beyond = IsZoomed(hwnd) != 0 && GetWindowLongPtrW(hwnd, -16) & 0x0004_0000 != 0;
if beyond && GetMonitorInfoW(MonitorFromRect(area, 2), &mut screen) != 0 {
*area = screen.work;
}
}
0
}
fn pixels_of(part: Rect, scale: f32) -> RECT {
RECT { left: (part.x * scale).floor() as i32, top: (part.y * scale).floor() as i32, right: (part.right() * scale).ceil() as i32, bottom: (part.bottom() * scale).ceil() as i32 }
}
fn place_on(screens: &[Screen], asked: Spot, size: Size, looks: Option<[u32; 2]>) -> Option<(u32, POINT)> {
let dpi = |scale: f32| (scale * 96.0).round() as u32;
let big = |scale: f32| {
let frame = framed(size.w, size.h, dpi(scale), looks);
[(frame.right - frame.left) as f32, (frame.bottom - frame.top) as f32]
};
let (nth, at) = spot(screens, asked, big)?;
Some((dpi(screens[nth].scale), POINT { x: at.x as i32, y: at.y as i32 }))
}
fn corner_of(hwnd: HANDLE) -> POINT {
let mut area = RECT::default();
unsafe { GetWindowRect(hwnd, &mut area) };
POINT { x: area.left, y: area.top }
}
impl Stale {
fn of(region: HANDLE) -> Stale {
let mut stale = Stale::default();
if unsafe { GetRegionData(region, size_of::<Stale>() as u32, &mut stale) } == 0 {
stale.header[2] = 0;
}
stale
}
}
impl Open {
fn make(title: &str, size: Size, opening: WindowOpening, transparent: bool, place: Option<(u32, POINT)>, id: Id, owner: HANDLE) -> Option<Open> {
let (edges, frameless, min_size) = (opening.frameless, opening.frameless || transparent, (opening.min_size.w, opening.min_size.h));
let [style, more] = styles(opening.kind, edges, transparent);
let looks = (!frameless).then_some([style, more]);
let (at, big) = match place {
Some((dpi, at)) => (at, framed(size.w, size.h, dpi, looks)),
None => (POINT { x: i32::MIN, y: i32::MIN }, RECT { left: 0, top: 0, right: i32::MIN, bottom: i32::MIN }),
};
unsafe {
let instance = GetModuleHandleW(std::ptr::null());
let hwnd = CreateWindowExW(more, wide(CLASS).as_ptr(), wide(title).as_ptr(), style, at.x, at.y, big.right - big.left, big.bottom - big.top, owner, 0, instance, std::ptr::null_mut());
if hwnd == 0 {
return None;
}
SetWindowLongPtrW(hwnd, 0, frameless as isize);
let (dpi, mut size) = (GetDpiForWindow(hwnd), size);
let frame = framed(size.w, size.h, dpi, looks);
let (mut wide, mut high, mut to, mut screen) =
(frame.right - frame.left, frame.bottom - frame.top, corner_of(hwnd), MONITORINFO { size: size_of::<MONITORINFO>() as u32, all: RECT::default(), work: RECT::default(), flags: 0 });
if GetMonitorInfoW(MonitorFromWindow(hwnd, 2), &mut screen) != 0 {
let (room, scale) = ([screen.work.right - screen.work.left, screen.work.bottom - screen.work.top], dpi as f32 / 96.0);
size = Size { w: size.w - (wide - room[0]).max(0) as f32 / scale, h: size.h - (high - room[1]).max(0) as f32 / scale };
(wide, high) = (wide.min(room[0]), high.min(room[1]));
to = POINT { x: to.x.min(screen.work.right - wide).max(screen.work.left), y: to.y.min(screen.work.bottom - high).max(screen.work.top) };
let middle = POINT { x: (screen.work.left + screen.work.right - wide) / 2, y: (screen.work.top + screen.work.bottom - high) / 2 };
to = if style == WS_PLAIN { middle } else { to };
}
let nowhere = MonitorFromWindow(hwnd, 0) == 0;
SetWindowPos(hwnd, 0, to.x, to.y, wide, high, if place.is_some() || nowhere { 0x0036 } else { 0x0034 });
let (corner, messages) = (corner_of(hwnd), Messages::of(hwnd));
let normal = WindowPlacement { position: (corner.x, corner.y), width: size.w, height: size.h, maximized: false };
let transparent = transparent.then(SeeThrough::new);
let mut made = Open {
hwnd,
id,
scale: dpi as f32 / 96.0,
wide: 0,
min_size,
tracking: false,
messages,
ime_spot: RECT::default(),
normal,
ime_off: false,
files: false,
dark: false,
high: 0,
edges,
asks_hit_test: false,
transparent,
before_fullscreen: None,
blocked: false,
withdrawn: false,
#[cfg(feature = "gpu")]
shown: None,
#[cfg(feature = "gpu")]
cloaked: false,
};
made.frame(true); Some(made)
}
}
fn bring_to_front(&mut self) {
unsafe {
if std::mem::take(&mut self.withdrawn) {
ShowWindow(self.hwnd, 5);
}
if IsIconic(self.hwnd) != 0 {
ShowWindow(self.hwnd, 9);
}
SetForegroundWindow(self.hwnd);
}
}
fn enter_fullscreen(&mut self) {
let (hwnd, mut place, mut screen) = (self.hwnd, [0i32; 11], MONITORINFO { size: size_of::<MONITORINFO>() as u32, all: RECT::default(), work: RECT::default(), flags: 0 });
place[0] = 44;
unsafe {
if IsIconic(hwnd) != 0 {
ShowWindow(hwnd, 9);
}
if GetWindowPlacement(hwnd, &mut place) == 0 || GetMonitorInfoW(MonitorFromWindow(hwnd, 2), &mut screen) == 0 {
return;
}
let (style, all, frame) = (GetWindowLongPtrW(hwnd, -16), screen.all, WS_OVERLAPPEDWINDOW as isize);
SetWindowLongPtrW(hwnd, -16, style & !frame);
SetWindowPos(hwnd, 0, all.left, all.top, all.right - all.left, all.bottom - all.top, 0x0234);
self.before_fullscreen = Some((style & frame, place));
}
}
fn leave_fullscreen(&mut self) {
let Some((frame, mut place)) = self.before_fullscreen.take() else { return };
unsafe {
if IsIconic(self.hwnd) != 0 {
(place[1], place[2]) = (if place[2] == 3 { 2 } else { 0 }, 7);
}
SetWindowLongPtrW(self.hwnd, -16, GetWindowLongPtrW(self.hwnd, -16) | frame);
SetWindowPlacement(self.hwnd, &place);
SetWindowPos(self.hwnd, 0, 0, 0, 0, 0, 0x0237);
}
}
fn apply_state(&mut self, minimize: bool, fill: Option<bool>, whole: Option<bool>) -> bool {
match whole {
Some(true) if self.before_fullscreen.is_none() => self.enter_fullscreen(),
Some(false) => self.leave_fullscreen(),
_ => {}
}
unsafe {
match fill.filter(|_| self.before_fullscreen.is_none()) {
Some(true) => _ = ShowWindow(self.hwnd, 3),
Some(false) if IsZoomed(self.hwnd) != 0 => _ = ShowWindow(self.hwnd, 9),
_ => {}
}
if minimize {
ShowWindow(self.hwnd, 6);
}
}
minimize || fill.is_some() || whole.is_some()
}
fn changed(&mut self, ui: &mut Ui) {
let (all, scale) = (rect(0.0, 0.0, ui.size().w, ui.size().h), self.scale);
let Some(picture) = &mut self.transparent else {
return ui.changes().for_each(|place| self.invalidate(place));
};
let afresh = picture.fit([self.wide, self.high]);
let (around, places) = if afresh { (all, 1) } else { (ui.around, ui.places.len()) };
for place in 0..places {
let area = if afresh { all } else { ui.places[place] };
picture.draw(ui, area, scale);
}
ui.changes();
if !around.is_empty() {
picture.show(self.hwnd, pixels_of(around.intersect(all), scale));
}
}
fn frame(&mut self, dark: bool) {
if std::mem::replace(&mut self.dark, dark) != dark {
let dark = dark as i32;
unsafe {
DwmSetWindowAttribute(self.hwnd, 20, (&raw const dark).cast(), 4);
SetWindowPos(self.hwnd, 0, 0, 0, 0, 0, 0x0037);
}
}
}
fn destroy(self) {
#[cfg(feature = "gpu")]
drop(self.shown);
uia::gone(self.hwnd);
unsafe {
if self.files {
RevokeDragDrop(self.hwnd);
}
SetWindowLongPtrW(self.hwnd, -21, 0);
DestroyWindow(self.hwnd);
}
}
fn stale(&self) -> Stale {
unsafe {
let region = CreateRectRgn(0, 0, 0, 0);
GetUpdateRgn(self.hwnd, region, 0);
let stale = Stale::of(region);
DeleteObject(region);
stale
}
}
fn invalidate(&self, part: Rect) {
if part.is_empty() {
return;
}
let area = pixels_of(part, self.scale);
unsafe { InvalidateRect(self.hwnd, &area, 0) };
}
fn take_files(&mut self, ole: bool) {
unsafe {
let target = DropTarget::new(self.hwnd);
if !ole || RegisterDragDrop(self.hwnd, target.cast()) < 0 {
SetWindowLongPtrW(self.hwnd, -20, GetWindowLongPtrW(self.hwnd, -20) | 16);
}
DropTarget::release(target);
}
self.files = true;
}
fn keep_ime_off(&mut self, hidden: bool) {
if std::mem::replace(&mut self.ime_off, hidden) == hidden {
return;
}
unsafe { ImmAssociateContextEx(self.hwnd, 0, if hidden { 0 } else { 0x10 }) };
}
fn follow_cursor(&mut self, cursor: Option<Rect>) {
let Some(cursor) = cursor else { return };
let spot = cursor_pixels(cursor, self.scale);
if std::mem::replace(&mut self.ime_spot, spot) == spot {
return;
}
let text = COMPOSITIONFORM { style: 2, at: POINT { x: spot.left, y: spot.top }, area: RECT::default() };
let list = CANDIDATEFORM { index: 0, style: 0x80, at: text.at, area: spot };
unsafe {
let context = ImmGetContext(self.hwnd);
ImmSetCompositionWindow(context, &text);
ImmSetCandidateWindow(context, &list);
ImmReleaseContext(self.hwnd, context);
}
}
fn follow_mouse(&mut self, msg: u32, w: usize) {
let mut ask = TRACKMOUSEEVENT { size: size_of::<TRACKMOUSEEVENT>() as u32, flags: 2, hwnd: self.hwnd, hover_time: 0 };
match msg {
WM_MOUSEMOVE if !std::mem::replace(&mut self.tracking, true) => _ = unsafe { TrackMouseEvent(&mut ask) },
WM_MOUSELEAVE => self.tracking = false,
WM_LBUTTONDOWN | WM_RBUTTONDOWN | WM_MBUTTONDOWN => _ = unsafe { SetCapture(self.hwnd) },
WM_LBUTTONUP | WM_RBUTTONUP | WM_MBUTTONUP if w & 0x13 == 0 => _ = unsafe { ReleaseCapture() },
_ => {}
}
}
}
fn lend(ui: &mut Ui, at: usize, id: Id, work: &mut dyn FnMut(&mut Ui)) {
match at {
0 => work(ui),
_ => _ = ui.in_window(id, work),
}
}
struct State {
ui: Ui,
app: Box<dyn FnMut(&mut Ui)>,
strip: Rc<Strip>,
windows: Vec<Open>,
start: Instant,
timer: bool,
ole: Option<bool>,
#[cfg(feature = "gpu")]
device: Option<crate::gpu_windows::Device>,
#[cfg(feature = "gpu")]
coming: Option<std::thread::JoinHandle<Option<crate::gpu_windows::Device>>>,
}
impl State {
fn in_window<T>(&mut self, at: usize, work: impl FnOnce(&mut Ui, &mut Open) -> T) -> Option<T> {
let (ui, window) = (&mut self.ui, &mut self.windows[at]);
let (id, mut work, mut done) = (window.id, Some(work), None);
lend(ui, at, id, &mut |ui| done = work.take().map(|work| work(ui, window)));
done
}
fn run(&mut self) {
self.ui.run(&mut self.app);
self.sync_windows();
let mut came = false;
for at in 0..self.windows.len() {
came |= self.apply_run(at);
}
came |= self.apply_desktop_requests();
if came {
let news = (0..self.windows.len()).fold(false, |news, at| self.back(at) | (self.fit(at) == Some(true)) | news);
if self.hear_keyboard() || news {
unsafe { PostMessageW(self.windows[0].hwnd, WM_WAKE, 0, 0) };
}
}
if self.ui.listening && uia::nobody_left() {
self.ui.set_screen_reader(false);
}
let (first, wait) = (self.windows[0].hwnd, self.ui.wake_in());
match wait {
Some(wait) => _ = unsafe { SetTimer(first, 1, (wait.as_millis() as u32).max(1), 0) },
None if self.timer => _ = unsafe { KillTimer(first, 1) },
None => {}
}
self.timer = wait.is_some();
if self.ui.quitting() {
unsafe { PostMessageW(first, WM_CLOSE, 0, 0) };
}
}
fn apply_run(&mut self, at: usize) -> bool {
let Some((title, takes, top, forward, stood, going)) = self.in_window(at, |ui, window| {
window.frame(ui.theme.bg.is_dark());
uia::tell(window.hwnd, ui);
uia::announce(window.hwnd, ui);
window.changed(ui);
ui.changed();
window.asks_hit_test = window.edges || ui.window_state.title_bar != 0;
window.keep_ime_off(ui.password_focused());
ui.take_file_dialog_request().into_iter().for_each(|ask| ASKED.with_borrow_mut(|asked| asked.push((window.hwnd, ask))));
let stood = window.apply_state(ui.take_minimize(), ui.take_maximized(), ui.take_full_screen());
let takes = ui.takes_files.get() || ui.crossing.takes_text.get();
let going = ui.take_drag_out().zip(OUTWARD.get()).map(|(what, outward)| ((outward.made)(what), outward));
(ui.take_title(), takes, ui.take_on_top(), ui.take_bring_to_front(), stood, going)
}) else {
return false;
};
let window = &mut self.windows[at];
if let Some(on) = top {
unsafe { SetWindowPos(window.hwnd, if on { -1 } else { -2 }, 0, 0, 0, 0, 0x0013) };
}
if forward {
window.bring_to_front();
}
if let Some(title) = title {
unsafe { SetWindowTextW(window.hwnd, wide(&title).as_ptr()) };
}
let ole = (takes && !window.files) || going.is_some();
let ole = ole && *self.ole.get_or_insert_with(|| unsafe { OleInitialize(std::ptr::null_mut()) } >= 0);
if let Some(drops) = DROPS.get().filter(|_| takes && !window.files) {
(drops.take)(window, ole);
}
if let Some((data, outward)) = going {
self.carry_out(at, data, outward, ole);
}
stood || forward
}
fn apply_desktop_requests(&mut self) -> bool {
let Some(desktop) = DESKTOP.get() else { return false };
let (asked, first) = ((desktop.after)(), &mut self.windows[0]);
let hide = asked.hide && !first.withdrawn;
if hide {
unsafe { ShowWindow(first.hwnd, 0) };
first.withdrawn = true;
}
if asked.forward {
first.bring_to_front();
}
hide || asked.forward
}
fn carry_out(&mut self, at: usize, data: *mut crate::ole_drag::Carried, outward: crate::ole_drag::Outward, ole: bool) {
let hwnd = self.windows[at].hwnd;
let gone = if ole { OUTGOING.replace(Some((hwnd, data, outward))).map(|(_, was, _)| was) } else { Some(data) };
if let Some(gone) = gone {
unsafe { crate::ole_drag::let_go(gone) };
}
if !ole {
self.in_window(at, |ui, _| ui.set_drop_effect(None));
}
}
fn sync_windows(&mut self) {
let was = self.windows.len();
self.destroy_gone_windows();
let kept = self.windows.len();
self.create_new_windows();
if (was, kept) == (kept, self.windows.len()) {
return;
}
self.keep_blocked();
let first = self.windows[0].hwnd;
let (shown, front) = unsafe { (IsWindowVisible(first) != 0, GetForegroundWindow()) };
let how = if self.windows.iter().any(|window| window.hwnd == front) { 1 } else { 4 };
let mut news = false;
for at in kept..self.windows.len() {
news |= self.fit(at) == Some(true);
if shown {
unsafe { ShowWindow(self.windows[at].hwnd, how) };
}
}
if self.hear_keyboard() || news {
unsafe { PostMessageW(first, WM_WAKE, 0, 0) };
}
}
fn destroy_gone_windows(&mut self) {
for at in (1..self.windows.len()).rev() {
if self.ui.windows().all(|(id, ..)| id != self.windows[at].id) {
self.keep_blocked();
self.windows.remove(at).destroy();
}
}
}
fn create_new_windows(&mut self) {
let state = self as *mut State as isize;
for nth in 0..self.ui.windows().count() {
let new = self.ui.windows().nth(nth).filter(|(id, ..)| self.windows[1..].iter().all(|window| window.id != *id));
let Some((id, title, size)) = new else { continue };
let opening = self.ui.window_opening(id).unwrap_or_default();
let owner = if matches!(opening.kind, WindowKind::Tool | WindowKind::Dialog) { self.windows[0].hwnd } else { 0 };
let Some(window) = Open::make(title, size, opening, false, self.place_for(size, opening), id, owner) else { continue };
unsafe { SetWindowLongPtrW(window.hwnd, -21, state) };
self.windows.push(window);
#[cfg(feature = "gpu")]
self.give_shown(self.windows.len() - 1, true);
}
}
fn corner(&self, size: Size, looks: Option<[u32; 2]>) -> Option<(u32, POINT)> {
let windows = || self.windows.iter().map(|window| window.hwnd);
unsafe {
let front = GetForegroundWindow();
let by = windows().filter(|hwnd| *hwnd == front).chain(windows().rev()).find(|hwnd| GetWindowLongPtrW(*hwnd, -16) & 0x2000_0000 == 0)?;
let dpi = GetDpiForWindow(by);
let (frame, title) = (framed(size.w, size.h, dpi, looks), framed(0.0, 0.0, dpi, Some([WS_OVERLAPPEDWINDOW, 0])));
let (mut at, step) = (corner_of(by), (-title.top).max(1));
while windows().any(|hwnd| corner_of(hwnd) == at) {
at = POINT { x: at.x + step, y: at.y + step };
}
let mut screen = MONITORINFO { size: size_of::<MONITORINFO>() as u32, all: RECT::default(), work: RECT::default(), flags: 0 };
if GetMonitorInfoW(MonitorFromWindow(by, 0), &mut screen) != 0 {
at.x = at.x.min(screen.work.right - (frame.right - frame.left)).max(screen.work.left);
at.y = at.y.min(screen.work.bottom - (frame.bottom - frame.top)).max(screen.work.top);
}
Some((dpi, at))
}
}
fn place_for(&self, size: Size, opening: WindowOpening) -> Option<(u32, POINT)> {
let looks = (!opening.frameless).then(|| styles(opening.kind, false, false));
let asked = match (opening.position, opening.screen, opening.kind) {
(Some((x, y)), ..) => Spot::At(x, y),
(None, Some(nth), _) => Spot::Middle(nth),
(None, None, WindowKind::Dialog) => Spot::Over(self.area_of(0)),
_ => return self.corner(size, looks),
};
place_on(self.ui.screens(), asked, size, looks)
}
fn area_of(&self, at: usize) -> Rect {
let (window, mut area) = (&self.windows[at], RECT::default());
if unsafe { IsIconic(window.hwnd) == 0 && GetWindowRect(window.hwnd, &mut area) != 0 } {
return rect(area.left as f32, area.top as f32, (area.right - area.left) as f32, (area.bottom - area.top) as f32);
}
let normal = window.normal;
rect(normal.position.0 as f32, normal.position.1 as f32, normal.width * window.scale, normal.height * window.scale)
}
fn keep_blocked(&mut self) {
for window in &mut self.windows {
let blocked = self.ui.window_blocked(window.id);
if std::mem::replace(&mut window.blocked, blocked) != blocked {
unsafe { EnableWindow(window.hwnd, !blocked as i32) };
}
}
}
fn ask_first(&mut self) -> Result<(), String> {
self.ui.request_close();
self.run();
self.ui.take_closing().unwrap_or(Ok(()))
}
fn screens_changed(&mut self) {
if self.ui.set_screens(screens()) {
self.run();
}
}
fn hear_keyboard(&mut self) -> bool {
let on = unsafe { GetFocus() };
let mut told = false;
for at in 0..self.windows.len() {
let has = self.windows[at].hwnd == on;
told |= self.in_window(at, |ui, _| ui.window_focused != has && ui.event(Event::Focused(has))) == Some(true);
}
told
}
fn input(&mut self, at: usize, msg: u32, w: usize, l: isize) {
let msg = if msg == WM_MOUSEMOVE && self.covered(at, l) { WM_MOUSELEAVE } else { msg };
let rerun = self.in_window(at, |ui, window| {
let mut rerun = false;
unsafe { window.messages.events(msg, w, l, window.scale, |event| rerun |= ui.event(event)) };
rerun
});
if rerun == Some(true) {
self.run();
}
}
fn covered(&mut self, at: usize, l: isize) -> bool {
let (hwnd, mut place) = (self.windows[at].hwnd, POINT { x: (l & 0xffff) as i16 as i32, y: (l >> 16 & 0xffff) as i16 as i32 });
self.in_window(at, |ui, _| ui.drag_leaves_window()) == Some(true) && unsafe { ClientToScreen(hwnd, &mut place) != 0 && WindowFromPoint(place) != hwnd }
}
fn carried(&mut self, at: usize, events: [Option<Event>; 2]) -> bool {
self.ui.set_time(self.start.elapsed());
let hwnd = self.windows[at].hwnd;
let rerun = self.in_window(at, |ui, _| events.into_iter().flatten().fold(false, |rerun, event| ui.event(event) | rerun));
if rerun == Some(true) {
self.run();
}
let at = self.windows.iter().position(|window| window.hwnd == hwnd);
at.is_none_or(|at| self.in_window(at, |ui, _| ui.take_drop_accepted()) == Some(true))
}
fn paint(&mut self, at: usize) {
self.strip.fit(self.windows.iter().map(|window| window.wide).max().unwrap_or(0));
let (hwnd, stale) = (self.windows[at].hwnd, self.windows[at].stale());
let mut ps = PAINTSTRUCT { hdc: 0, erase: 0, paint: RECT::default(), restore: 0, update: 0, reserved: [0; 32] };
let dc = unsafe { BeginPaint(hwnd, &mut ps) };
#[cfg(feature = "gpu")]
let dc = if self.windows[at].shown.is_some() { 0 } else { dc };
let parts = stale.parts.get(..stale.header[2] as usize).filter(|parts| !parts.is_empty()).unwrap_or(std::slice::from_ref(&ps.paint));
parts.iter().for_each(|part| self.draw(at, dc, *part));
#[cfg(feature = "gpu")]
self.present(at, parts);
unsafe { EndPaint(hwnd, &ps) };
#[cfg(feature = "gpu")]
self.uncloak(at);
}
fn draw(&mut self, at: usize, dc: HANDLE, area: RECT) {
let (s, from, pixels, width, rows) = (self.windows[at].scale, self.strip.dc, self.strip.pixels.get(), self.strip.width.get(), self.strip.rows.get());
if pixels.is_null() || width <= 0 {
return;
}
let part = rect(area.left as f32 / s, area.top as f32 / s, (area.right - area.left) as f32 / s, (area.bottom - area.top) as f32 / s);
let mut top = area.top;
while top < area.bottom {
let high = rows.min(area.bottom - top);
let block = unsafe { std::slice::from_raw_parts_mut(pixels, (width * high) as usize) };
let mut canvas = Canvas::new(block, width as usize, high as usize, (0, top), s);
canvas.clip(part);
self.in_window(at, |ui, _| ui.paint(&mut canvas));
#[cfg(feature = "gpu")]
if dc == 0 {
self.upload(at, area, top, high);
top += high;
continue;
}
unsafe { BitBlt(dc, area.left, top, area.right - area.left, high, from, area.left, 0, 0x00CC_0020) };
top += high;
}
}
fn fit(&mut self, at: usize) -> Option<bool> {
let (hwnd, mut area) = (self.windows[at].hwnd, RECT::default());
let (_, corner, zoomed) = unsafe { (GetClientRect(hwnd, &mut area), corner_of(hwnd), IsZoomed(hwnd) != 0) };
if area.right <= 0 || area.bottom <= 0 {
return None;
}
(self.windows[at].wide, self.windows[at].high) = (area.right, area.bottom);
#[cfg(feature = "gpu")]
self.fit_shown(at);
self.in_window(at, |ui, window| {
let size = Size { w: area.right as f32 / window.scale, h: area.bottom as f32 / window.scale };
if !zoomed && window.before_fullscreen.is_none() {
window.normal = WindowPlacement { position: (corner.x, corner.y), width: size.w, height: size.h, maximized: false };
}
let news = (ui.size(), ui.scale()) != (size, window.scale);
ui.resize(size, window.scale);
ui.set_window_placement(WindowPlacement { maximized: zoomed, ..window.normal }) | news
})
}
fn hit_test(&mut self, at: usize, l: isize) -> Option<isize> {
let (hwnd, scale, edges) = (self.windows[at].hwnd, self.windows[at].scale, self.windows[at].edges);
let (of_frame, sized) = unsafe { (DefWindowProcW(hwnd, WM_NCHITTEST, 0, l), edges && IsZoomed(hwnd) == 0) };
let place = pointer_at(hwnd, POINT { x: (l & 0xffff) as i16 as i32, y: (l >> 16 & 0xffff) as i16 as i32 }, scale);
let (Some(Event::Move(place)), 1) = (place, of_frame) else { return Some(of_frame) };
Some(match self.in_window(at, |ui, _| ui.hit_test(place, sized))? {
HitTest::Client => 1,
HitTest::TitleBar => 2,
HitTest::Edge(across, down) => [13, 12, 14, 10, 1, 11, 16, 15, 17][(down * 3 + across + 4) as usize],
})
}
fn resized(&mut self, at: usize) {
let told = self.back(at);
if self.fit(at).is_some() || told {
self.run();
}
}
fn back(&mut self, at: usize) -> bool {
let hidden = unsafe { IsIconic(self.windows[at].hwnd) } != 0 || self.windows[at].withdrawn;
self.in_window(at, |ui, window| (ui.window_minimized() != hidden && ui.event(Event::Minimized(hidden))) | ui.full_screen_now(window.before_fullscreen.is_some())) == Some(true)
}
fn settings_changed(&mut self, what: usize) {
if self.ui.set_preferences(crate::preferences::of_windows()) | (what == 0x2F && self.ui.set_screens(screens())) {
self.run();
}
}
fn moved(&mut self, at: usize) {
if self.fit(at) == Some(true) {
self.run();
}
}
}
#[cfg(feature = "gpu")]
const WM_GPU: u32 = 0x8010;
#[cfg(feature = "gpu")]
impl State {
fn show_by_gpu(&mut self, asked: crate::platform::Renderer, transparent: bool, cloak: bool) {
let asked = asked.chosen(std::env::var("TAUT_RENDERER").ok().as_deref());
if asked == crate::platform::Renderer::Cpu || transparent {
return;
}
if cloak {
self.cloak(0);
}
let hwnd = self.windows[0].hwnd;
#[cfg(test)]
let faked = crate::gpu_windows::tests::faked();
self.coming = Some(std::thread::spawn(move || {
#[cfg(test)]
crate::gpu_windows::tests::fake(faked);
let device = crate::gpu_windows::Device::gpu(asked, asked == crate::platform::Renderer::Auto);
unsafe { PostMessageW(hwnd, WM_GPU, 0, 0) };
device
}));
#[cfg(test)]
if !faked.later {
self.gpu_came();
}
}
fn gpu_came(&mut self) {
let Some(coming) = self.coming.take() else { return };
self.device = coming.join().ok().flatten();
#[cfg(test)]
crate::gpu_windows::tests::came();
for at in 0..self.windows.len() {
if self.windows[at].transparent.is_none() {
self.give_shown(at, false);
self.draw_whole(at);
}
}
if self.windows.iter().all(|window| window.shown.is_none()) {
self.device = None;
}
}
fn give_shown(&mut self, at: usize, cloak: bool) {
let (hwnd, size) = (self.windows[at].hwnd, [self.windows[at].wide, self.windows[at].high]);
let shown = self.device.as_ref().and_then(|device| crate::gpu_windows::Shown::new(device, hwnd, size));
let renderer = match (&shown, &self.device) {
(Some(_), Some(device)) => device.renderer(),
_ => crate::platform::Renderer::Cpu,
};
if cloak && shown.is_some() {
self.cloak(at);
}
self.windows[at].shown = shown;
self.in_window(at, |ui, _| ui.renderer = renderer);
}
fn draw_whole(&mut self, at: usize) {
let (window, widest) = (&self.windows[at], self.windows.iter().map(|window| window.wide).max().unwrap_or(0));
let all = RECT { left: 0, top: 0, right: window.wide, bottom: window.high };
if window.shown.is_some() {
self.strip.fit(widest);
self.draw(at, 0, all);
self.present(at, &[all]);
self.uncloak(at);
}
}
fn cloak(&mut self, at: usize) {
let window = &mut self.windows[at];
window.cloaked = unsafe { DwmSetWindowAttribute(window.hwnd, 13, (&1i32 as *const i32).cast(), 4) } >= 0;
}
fn uncloak(&mut self, at: usize) {
let window = &mut self.windows[at];
if std::mem::take(&mut window.cloaked) {
unsafe { DwmSetWindowAttribute(window.hwnd, 13, (&0i32 as *const i32).cast(), 4) };
}
}
fn fit_shown(&mut self, at: usize) {
let (Some(device), window) = (&self.device, &mut self.windows[at]) else { return };
let Some(shown) = &mut window.shown else { return };
match shown.fit(device, [window.wide, window.high]) {
Some(true) => _ = unsafe { InvalidateRect(window.hwnd, std::ptr::null(), 0) },
Some(false) => {}
None => self.lost(),
}
}
fn upload(&mut self, at: usize, area: RECT, top: i32, high: i32) {
let (Some(device), Some(shown)) = (&self.device, &mut self.windows[at].shown) else { return };
let (pixels, width) = (self.strip.pixels.get(), self.strip.width.get());
let first = pixels.wrapping_add(area.left.clamp(0, width) as usize);
unsafe { shown.upload(device, [area.left, top], [area.right - area.left, high], first, width as usize) };
}
fn present(&mut self, at: usize, parts: &[RECT]) {
let (Some(device), window) = (&self.device, &mut self.windows[at]) else { return };
let Some(shown) = &mut window.shown else { return };
if !shown.present(device, parts) && device.removed() {
self.lost();
}
}
fn lost(&mut self) {
let Some(old) = self.device.take() else { return };
let had: Vec<usize> = (0..self.windows.len()).filter(|at| self.windows[*at].shown.take().is_some()).collect();
self.device = crate::gpu_windows::Device::again(old);
for at in had {
self.give_shown(at, false);
unsafe { InvalidateRect(self.windows[at].hwnd, std::ptr::null(), 0) };
}
}
}
fn mods() -> Mods {
let down = |key: i32| unsafe { GetKeyState(key) } < 0;
Mods { shift: down(0x10), ctrl: down(0x11), alt: down(0x12) }
}
fn held_with(code: usize, down: bool) -> Mods {
let mut held = mods();
match code {
0x10 => held.shift = down,
0x11 => held.ctrl = down,
_ => held.alt = down,
}
held
}
fn key(code: usize) -> Option<Key> {
Some(match code {
0x08 => Key::Backspace,
0x09 => Key::Tab,
0x0D => Key::Enter,
0x1B => Key::Escape,
0x20 => Key::Space,
0x21 => Key::PageUp,
0x22 => Key::PageDown,
0x23 => Key::End,
0x24 => Key::Home,
0x25 => Key::Left,
0x26 => Key::Up,
0x27 => Key::Right,
0x28 => Key::Down,
0x2E => Key::Delete,
0x5D => Key::Menu,
0x30..=0x39 | 0x41..=0x5A => Key::Char((code as u8 as char).to_ascii_lowercase()),
0xBB => Key::Char('='),
0xBD | 0x6D => Key::Char('-'),
0x6B => Key::Char('+'),
0x70..=0x7B => Key::F((code - 0x6F) as u8),
_ => return None,
})
}
const FILES: FORMATETC = FORMATETC { format: 15, device: std::ptr::null(), aspect: 1, index: -1, medium: 1 };
const TEXT: FORMATETC = FORMATETC { format: 13, ..FILES };
const COPY: u32 = 1;
#[derive(Clone, Copy, Debug, PartialEq)]
enum Brings {
Nothing,
Files,
Text,
}
impl Brings {
fn over(self, on: bool) -> Event {
if self == Brings::Text { Event::TextHovered(on) } else { Event::FilesHovered(on) }
}
unsafe fn let_go(self, memory: HANDLE) -> Event {
match self {
Brings::Text => Event::TextDropped(unsafe { dropped_text(memory) }),
_ => Event::FilesDropped(unsafe { dropped_files(memory) }.1),
}
}
}
#[repr(C)]
struct DropTarget {
table: &'static DropTargetTable,
held: Cell<u32>,
hwnd: HANDLE,
brings: Cell<Brings>,
}
#[repr(C)]
struct DropTargetTable {
query: unsafe extern "system" fn(*mut DropTarget, *const [u32; 4], *mut *mut DropTarget) -> i32,
hold: unsafe extern "system" fn(*mut DropTarget) -> u32,
release: unsafe extern "system" fn(*mut DropTarget) -> u32,
enter: unsafe extern "system" fn(*mut DropTarget, *mut IDataObject, u32, POINT, *mut u32) -> i32,
over: unsafe extern "system" fn(*mut DropTarget, u32, POINT, *mut u32) -> i32,
leave: unsafe extern "system" fn(*mut DropTarget) -> i32,
let_go: unsafe extern "system" fn(*mut DropTarget, *mut IDataObject, u32, POINT, *mut u32) -> i32,
}
static DROP_TARGET: DropTargetTable = DropTargetTable {
query: DropTarget::query,
hold: DropTarget::hold,
release: DropTarget::release,
enter: DropTarget::enter,
over: DropTarget::over,
leave: DropTarget::leave,
let_go: DropTarget::let_go,
};
impl DropTarget {
fn new(hwnd: HANDLE) -> *mut DropTarget {
Box::into_raw(Box::new(DropTarget { table: &DROP_TARGET, held: Cell::new(1), hwnd, brings: Cell::new(Brings::Nothing) }))
}
unsafe extern "system" fn query(this: *mut DropTarget, name: *const [u32; 4], out: *mut *mut DropTarget) -> i32 {
unsafe {
let known = matches!(name.read_unaligned(), [0 | 0x122, 0, 0xC0, 0x4600_0000]);
*out = if known { this } else { std::ptr::null_mut() };
if !known {
return 0x8000_4002_u32 as i32; }
Self::hold(this);
}
0
}
unsafe extern "system" fn hold(this: *mut DropTarget) -> u32 {
let held = unsafe { &(*this).held };
held.set(held.get() + 1);
held.get()
}
unsafe extern "system" fn release(this: *mut DropTarget) -> u32 {
unsafe {
let left = (*this).held.get() - 1;
(*this).held.set(left);
if left == 0 {
drop(Box::from_raw(this));
}
left
}
}
unsafe extern "system" fn enter(this: *mut DropTarget, data: *mut IDataObject, _keys: u32, at: POINT, effect: *mut u32) -> i32 {
let (target, effect) = unsafe { (&*this, &mut *effect) };
let brings = if *effect & COPY == 0 { Brings::Nothing } else { target.brought(data) };
target.brings.set(brings);
target.tell(|window| [Some(brings.over(true)), pointer_at(window.hwnd, at, window.scale)]);
*effect = target.effect();
0
}
fn brought(&self, data: *mut IDataObject) -> Brings {
let has = |form: &FORMATETC| unsafe { ((*(*data).table).query_get_data)(data, form) == 0 };
let [files, text] = self.takes();
match () {
_ if crate::ole_drag::is_going(data.cast(), self.hwnd) => Brings::Nothing,
_ if files && has(&FILES) => Brings::Files,
_ if text && has(&TEXT) => Brings::Text,
_ => Brings::Nothing,
}
}
unsafe extern "system" fn over(this: *mut DropTarget, _keys: u32, at: POINT, effect: *mut u32) -> i32 {
let (target, effect) = unsafe { (&*this, &mut *effect) };
target.tell(|window| [pointer_at(window.hwnd, at, window.scale), None]);
*effect = target.effect();
0
}
unsafe extern "system" fn leave(this: *mut DropTarget) -> i32 {
let target = unsafe { &*this };
let brings = target.brings.get();
target.tell(|_| [Some(brings.over(false)), Some(Event::Leave)]);
target.brings.set(Brings::Nothing);
0
}
unsafe extern "system" fn let_go(this: *mut DropTarget, data: *mut IDataObject, _keys: u32, at: POINT, effect: *mut u32) -> i32 {
let mut handed = STGMEDIUM { medium: 0, memory: 0, owner: std::ptr::null_mut() };
let (target, effect, read) = unsafe {
let (target, brings) = (&*this, (*this).brings.get());
let form = if brings == Brings::Text { &TEXT } else { &FILES };
let got = brings != Brings::Nothing && ((*(*data).table).get_data)(data, form, &mut handed) == 0;
let read = if got && handed.medium == form.medium { brings.let_go(handed.memory) } else { brings.over(false) };
if got {
ReleaseStgMedium(&mut handed);
}
(target, &mut *effect, read)
};
let taken = target.tell(|window| [pointer_at(window.hwnd, at, window.scale), Some(read)]);
target.brings.set(Brings::Nothing);
*effect = if taken { COPY } else { 0 };
0
}
fn effect(&self) -> u32 {
if self.brings.get() == Brings::Nothing { 0 } else { COPY }
}
fn takes(&self) -> [bool; 2] {
let nested = ANSWERING.replace(true);
let state = if nested { None } else { unsafe { state_of(self.hwnd) } };
let takes = state.and_then(|(state, at)| state.in_window(at, |ui, _| [ui.takes_files.get(), ui.crossing.takes_text.get()]));
ANSWERING.set(nested);
takes.unwrap_or_default()
}
fn tell(&self, events: impl FnOnce(&Open) -> [Option<Event>; 2]) -> bool {
let nested = ANSWERING.replace(true);
let state = if nested || self.brings.get() == Brings::Nothing { None } else { unsafe { state_of(self.hwnd) } };
let taken = state.is_some_and(|(state, at)| state.carried(at, events(&state.windows[at])));
ANSWERING.set(nested);
taken
}
}
unsafe fn dropped_text(memory: HANDLE) -> String {
unsafe {
let start = GlobalLock(memory).cast::<u8>();
let block = if start.is_null() { &[][..] } else { std::slice::from_raw_parts(start, GlobalSize(memory)) };
let units: Vec<u16> = block.as_chunks().0.iter().map(|pair| u16::from_le_bytes(*pair)).take_while(|unit| *unit != 0).collect();
GlobalUnlock(memory);
String::from_utf16_lossy(&units)
}
}
pub(crate) fn carried_over(hwnd: HANDLE, at: Option<POINT>) {
let nested = ANSWERING.replace(true);
if let Some((state, at_window)) = if nested { None } else { unsafe { state_of(hwnd) } } {
let event = at.and_then(|at| pointer_at(hwnd, at, state.windows[at_window].scale)).unwrap_or(Event::Leave);
state.carried(at_window, [Some(event), None]);
}
ANSWERING.set(nested);
}
fn came_back(hwnd: HANDLE, outcome: crate::ole_drag::Outcome) {
let nested = ANSWERING.replace(true);
if let Some((state, at)) = if nested { None } else { unsafe { state_of(hwnd) } } {
let place = outcome.home.and_then(|home| pointer_at(hwnd, home, state.windows[at].scale)).unwrap_or(Event::Leave);
state.windows[at].tracking = false;
if unsafe { GetCapture() } == hwnd {
unsafe { ReleaseCapture() };
}
state.in_window(at, |ui, _| ui.set_drop_effect(outcome.taken));
state.carried(at, [Some(place), Some(Event::Release(Button::Left))]);
}
ANSWERING.set(nested);
}
pub(crate) fn hands_out() {
OUTWARD.set(Some(crate::ole_drag::OUTWARD));
}
fn carry_out_asked() {
while let Some((hwnd, data, outward)) = OUTGOING.take() {
let outcome = unsafe { (outward.carried)(hwnd, data) };
came_back(hwnd, outcome);
}
}
unsafe fn state_of<'a>(hwnd: HANDLE) -> Option<(&'a mut State, usize)> {
let state = unsafe { (GetWindowLongPtrW(hwnd, -21) as *mut State).as_mut() }?;
let at = state.windows.iter().position(|window| window.hwnd == hwnd)?;
Some((state, at))
}
thread_local! {
static ANSWERING: Cell<bool> = const { Cell::new(false) };
}
unsafe extern "system" fn window_proc(hwnd: HANDLE, msg: u32, w: usize, l: isize) -> isize {
if msg == WM_NCCALCSIZE && unsafe { GetWindowLongPtrW(hwnd, 0) } != 0 {
return unsafe { all_inside(hwnd, l) };
}
let nested = ANSWERING.replace(true);
let answer = if nested { None } else { unsafe { answer(hwnd, msg, w, l) } };
ANSWERING.set(nested);
answer.unwrap_or_else(|| unsafe { DefWindowProcW(hwnd, msg, w, l) })
}
unsafe fn answer(hwnd: HANDLE, msg: u32, w: usize, l: isize) -> Option<isize> {
let (state, at) = unsafe { state_of(hwnd) }?;
state.ui.set_time(state.start.elapsed());
match msg {
WM_PAINT if state.windows[at].transparent.is_none() => state.paint(at),
WM_ERASEBKGND => return Some(1),
#[cfg(feature = "gpu")]
WM_GPU => state.gpu_came(),
WM_NCHITTEST if state.windows[at].asks_hit_test => return state.hit_test(at, l),
WM_SIZE => state.resized(at),
WM_MOVE => state.moved(at),
WM_SETTINGCHANGE | WM_SYSCOLORCHANGE | WM_DWMCOLORIZATIONCOLORCHANGED if at == 0 => state.settings_changed(w),
WM_DISPLAYCHANGE if at == 0 => state.screens_changed(),
WM_ANSWERED => {
state.in_window(at, |ui, _| ui.file_dialog_answered(ANSWER.take()));
state.run();
}
WM_WAKE | WM_TIMER => state.run(),
WM_GETOBJECT => {
return uia::give(hwnd, w, l, state.ui.listening, || {
state.ui.set_screen_reader(true);
state.run();
});
}
uia::WM_READER => {
state.in_window(at, |ui, _| uia::work(hwnd, ui));
state.run();
}
WM_POINTERUPDATE..=WM_POINTERCAPTURECHANGED if cfg!(feature = "touch") && touch_windows::taken(msg, w) => {
state.input(at, msg, w, l);
return Some(0);
}
WM_MOUSEMOVE | WM_MOUSELEAVE | WM_LBUTTONDOWN | WM_RBUTTONDOWN | WM_MBUTTONDOWN | WM_LBUTTONUP | WM_RBUTTONUP | WM_MBUTTONUP => {
state.windows[at].follow_mouse(msg, w);
state.input(at, msg, w, l);
}
WM_MOUSEWHEEL | WM_MOUSEHWHEEL | WM_KEYDOWN | WM_KEYUP | WM_CHAR | WM_SETFOCUS | WM_KILLFOCUS => state.input(at, msg, w, l),
WM_SYSKEYDOWN | WM_SYSKEYUP => {
state.input(at, msg, w, l);
return None; }
WM_SYSCHAR if w != ' ' as usize && state.in_window(at, |ui, _| ui.takes_alt()) == Some(true) => return Some(0),
WM_SYSCOMMAND if w & 0xFFF0 == SC_KEYMENU && l == 0 && state.in_window(at, |ui, _| ui.takes_alt()) == Some(true) => return Some(0),
WM_IME_STARTCOMPOSITION | WM_IME_COMPOSITION => {
state.in_window(at, |ui, window| window.follow_cursor(ui.ime_area()));
return None; }
WM_DROPFILES => {
state.input(at, msg, w, l);
unsafe { DragFinish(w as HANDLE) };
}
WM_SETCURSOR if (l & 0xffff) == 1 => {
let shape = system_cursor(state.in_window(at, |ui, _| ui.cursor()).unwrap_or(Cursor::Arrow));
unsafe { SetCursor(LoadCursorW(0, std::ptr::without_provenance(shape))) };
return Some(1);
}
WM_GETMINMAXINFO => {
let window = &state.windows[at];
let frame = framed(window.min_size.0, window.min_size.1, (window.scale * 96.0).round() as u32, styles_of(hwnd));
unsafe { (*(l as *mut MINMAXINFO)).min_track = POINT { x: frame.right - frame.left, y: frame.bottom - frame.top } };
}
WM_GETDPISCALEDSIZE => {
let window = &state.windows[at];
let frame = framed(window.wide as f32 / window.scale, window.high as f32 / window.scale, w as u32, styles_of(hwnd));
unsafe { *(l as *mut POINT) = POINT { x: frame.right - frame.left, y: frame.bottom - frame.top } };
return Some(1);
}
WM_DPICHANGED => {
state.ui.set_screens(screens());
state.windows[at].scale = (w & 0xffff) as f32 / 96.0;
let to = unsafe { *(l as *const RECT) };
unsafe { SetWindowPos(hwnd, 0, to.left, to.top, to.right - to.left, to.bottom - to.top, 0x0014) };
state.resized(at);
}
WM_CLOSE if at > 0 => {
state.in_window(at, |ui, _| ui.request_close());
state.run();
}
WM_CLOSE if !state.ui.quitting() => {
if state.ask_first().is_ok() {
return None;
}
}
WM_QUERYENDSESSION if at == 0 => {
let answer = state.ask_first();
let why = wide(answer.as_ref().err().map_or("", String::as_str));
unsafe { if answer.is_ok() { ShutdownBlockReasonDestroy(hwnd) } else { ShutdownBlockReasonCreate(hwnd, why.as_ptr()) } };
return Some(answer.is_ok() as isize);
}
WM_ENDSESSION if at == 0 => _ = unsafe { ShutdownBlockReasonDestroy(hwnd) },
WM_DESTROY if at > 0 => {
state.in_window(at, |ui, _| ui.request_close());
state.windows.remove(at).destroy();
state.run();
}
WM_DESTROY => unsafe { first_destroyed(hwnd, state) },
_ => return None,
}
Some(0)
}
fn system_cursor(cursor: Cursor) -> usize {
match cursor {
Cursor::Arrow => 32512,
Cursor::Hand => 32649,
Cursor::Text => 32513,
Cursor::ResizeX => 32644,
Cursor::ResizeY => 32645,
Cursor::ResizeDown => 32642,
Cursor::ResizeUp => 32643,
Cursor::Move => 32646,
Cursor::No => 32648,
}
}
unsafe fn first_destroyed(hwnd: HANDLE, state: &mut State) {
if let Some(desktop) = DESKTOP.get() {
(desktop.ended)();
}
let ole = state.ole == Some(true) && !crate::ole_drag::carrying();
state.windows.drain(1..).for_each(Open::destroy);
uia::gone(hwnd);
_ = WINDOW.compare_exchange(hwnd, 0, std::sync::atomic::Ordering::Relaxed, std::sync::atomic::Ordering::Relaxed);
unsafe {
RevokeDragDrop(hwnd);
SetWindowLongPtrW(hwnd, -21, 0);
drop(Box::from_raw(state as *mut State));
ole.then(|| OleUninitialize());
PostQuitMessage(0);
}
}
pub fn run(window: Window, app: impl FnMut(&mut Ui) + 'static) {
unsafe {
let (filling, minimized) = (window.maximized, window.minimized);
let hwnd = open(window, Box::new(app));
match minimized {
true if hwnd != 0 => show_minimized(hwnd, filling),
_ => _ = ShowWindow(hwnd, if filling { 3 } else { 1 }),
}
let mut msg = MSG::default();
while hwnd != 0 && GetMessageW(&mut msg, 0, 0, 0) > 0 {
TranslateMessage(&msg);
DispatchMessageW(&msg);
if let Some(answer) = DIALOG.get() {
answer();
}
carry_out_asked();
}
}
}
fn icon_of(icons: &[(usize, &[u32])], side: i32) -> HANDLE {
let fits = icons.iter().filter(|(width, _)| *width as i32 >= side).min_by_key(|(width, _)| *width);
let Some((width, pixels)) = fits.or(icons.iter().max_by_key(|(width, _)| *width)) else { return 0 };
icon_made(*width, pixels)
}
#[inline]
pub(crate) fn icon_made(width: usize, pixels: &[u32]) -> HANDLE {
let (wide, high) = (width as i32, (pixels.len() / width.max(1)) as i32);
let nothing_hidden = vec![0u8; (wide as usize).div_ceil(16) * 2 * high as usize];
unsafe {
let (color, mask) = (CreateBitmap(wide, high, 1, 32, pixels.as_ptr().cast()), CreateBitmap(wide, high, 1, 1, nothing_hidden.as_ptr()));
let icon = CreateIconIndirect(&ICONINFO { icon: 1, x: 0, y: 0, mask, color });
DeleteObject(color);
DeleteObject(mask);
icon
}
}
pub(crate) fn open(window: Window, app: Box<dyn FnMut(&mut Ui)>) -> HANDLE {
unsafe {
SetProcessDpiAwarenessContext(-4); let instance = GetModuleHandleW(std::ptr::null());
RegisterClassW(&WNDCLASSW {
style: 0,
proc: Some(window_proc),
class_extra: 0,
window_extra: size_of::<isize>() as i32,
instance,
icon: LoadIconW(instance, std::ptr::without_provenance(1)),
cursor: 0,
background: 0,
menu: std::ptr::null(),
name: wide(CLASS).as_ptr(),
});
let (found, size) = (screens(), Size { w: window.width, h: window.height });
let opening =
WindowOpening { min_size: Size { w: window.min_width, h: window.min_height }, position: window.position, screen: window.screen, kind: WindowKind::Plain, frameless: window.frameless };
let asked = window.position.map(|(x, y)| Spot::At(x, y)).or(window.screen.map(Spot::Middle)).unwrap_or_default();
let looks = (!window.frameless && !window.transparent).then(|| styles(WindowKind::Plain, false, false));
let Some(first) = Open::make(window.title, size, opening, window.transparent, place_on(&found, asked, size, looks), 0, 0) else { return 0 };
let hwnd = first.hwnd;
if !window.icons.is_empty() {
SetClassLongPtrW(hwnd, -14, icon_of(window.icons, GetSystemMetrics(11)));
SetClassLongPtrW(hwnd, -34, icon_of(window.icons, GetSystemMetrics(49)));
}
let strip = Rc::new(Strip { dc: CreateCompatibleDC(0), bitmap: Cell::new(0), pixels: Cell::new(std::ptr::null_mut()), width: Cell::new(0), rows: Cell::new(0) });
let clear = if window.transparent { SystemText::new() } else { None };
let text: Box<dyn TextSystem> = match clear {
Some(text) => Box::new(text),
None => Box::new(GdiText { strip: strip.clone(), fonts: Fonts::new(strip.dc) }),
};
let mut ui = Ui::new(text, Box::new(SystemClipboard));
if window.transparent {
ui.theme.bg = Color::NONE;
}
ui.set_native_file_dialog(true);
ui.set_preferences(crate::preferences::of_windows());
ui.set_screens(found);
#[rustfmt::skip]
let mut state = Box::new(State { ui, app, strip, windows: vec![first], start: Instant::now(), timer: false, ole: None, #[cfg(feature = "gpu")] device: None, #[cfg(feature = "gpu")] coming: None });
state.fit(0);
#[cfg(feature = "gpu")]
state.show_by_gpu(window.renderer, window.transparent, !window.minimized);
SetWindowLongPtrW(hwnd, -21, Box::into_raw(state) as isize);
WINDOW.store(hwnd, std::sync::atomic::Ordering::Relaxed);
hwnd
}
}
pub(crate) fn local_time() -> [u16; 6] {
let mut time = [0u16; 8];
unsafe { GetLocalTime(&mut time) };
[time[0], time[1], time[3], time[4], time[5], time[6]]
}
pub(crate) fn drives() -> u32 {
unsafe { GetLogicalDrives() }
}
pub(crate) fn quietly<T>(work: impl FnOnce() -> T) -> T {
let mut was = 0;
unsafe { SetThreadErrorMode(1, &mut was) };
let done = work();
unsafe { SetThreadErrorMode(was, std::ptr::null_mut()) };
done
}
#[repr(C)]
pub(crate) struct Com {
table: *const usize,
}
impl Com {
pub(crate) unsafe fn function<F: Copy>(this: *mut Com, at: usize) -> F {
unsafe { std::mem::transmute_copy(&*(*this).table.add(at)) }
}
pub(crate) unsafe fn release(this: *mut Com) {
unsafe { Com::function::<unsafe extern "system" fn(*mut Com) -> u32>(this, 2)(this) };
}
}
pub(crate) const fn named(a: u32, b: u16, c: u16, d: [u8; 8]) -> [u32; 4] {
[a, b as u32 | (c as u32) << 16, u32::from_le_bytes([d[0], d[1], d[2], d[3]]), u32::from_le_bytes([d[4], d[5], d[6], d[7]])]
}
thread_local! {
static ASKED: RefCell<Vec<(HANDLE, FileDialogRequest)>> = const { RefCell::new(Vec::new()) };
static ANSWER: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
static OUTGOING: Cell<Option<(HANDLE, *mut crate::ole_drag::Carried, crate::ole_drag::Outward)>> = const { Cell::new(None) };
static OUTWARD: Cell<Option<crate::ole_drag::Outward>> = const { Cell::new(None) };
static DIALOG: Cell<Option<fn()>> = const { Cell::new(None) };
}
pub(crate) fn hand_file_dialog() {
DIALOG.set(Some(answer_asked));
}
fn answer_asked() {
while let Some((hwnd, ask)) = ASKED.with_borrow_mut(|asked| asked.pop()) {
ANSWER.set(pick_file(hwnd, &ask));
unsafe { SendMessageW(hwnd, WM_ANSWERED, 0, 0) };
}
}
fn pick_file(owner: HANDLE, ask: &FileDialogRequest) -> Option<PathBuf> {
const TO_OPEN: [u32; 4] = named(0xDC1C_5A9C, 0xE88A, 0x4DDE, [0xA5, 0xA1, 0x60, 0xF8, 0x2A, 0x20, 0xAE, 0xF7]);
const TO_SAVE: [u32; 4] = named(0xC0B4_E2F3, 0xBA21, 0x4773, [0x8D, 0xBA, 0x33, 0x5E, 0xC9, 0x46, 0xEB, 0x8B]);
const A_DIALOG: [u32; 4] = named(0x42F8_5136, 0xDB7E, 0x439C, [0x85, 0xF1, 0xE4, 0x07, 0x5D, 0x13, 0x5F, 0xC8]);
type Says = unsafe extern "system" fn(*mut Com, *const u16) -> i32;
let kinds = wide(&ask.endings.iter().map(|ending| format!("*{ending}")).collect::<Vec<_>>().join(";"));
let (kind, ending) = ([kinds.as_ptr(), kinds.as_ptr()], wide(ask.endings.first().map_or("", |ending| ending.trim_start_matches('.'))));
let (title, name, folder, mut dialog, mut options) = (wide(ask.title), wide(&ask.name), wide(&ask.folder.to_string_lossy()), std::ptr::null_mut(), 0u32);
unsafe {
let com = CoInitializeEx(std::ptr::null_mut(), 2);
if CoCreateInstance(if ask.saving { &TO_SAVE } else { &TO_OPEN }, std::ptr::null_mut(), 1, &A_DIALOG, &mut dialog) >= 0 && !dialog.is_null() {
Com::function::<unsafe extern "system" fn(*mut Com, *mut u32) -> i32>(dialog, 10)(dialog, &mut options);
Com::function::<unsafe extern "system" fn(*mut Com, u32) -> i32>(dialog, 9)(dialog, options | 0x40 | if ask.folders { 0x20 } else { 0 });
Com::function::<Says>(dialog, 17)(dialog, title.as_ptr());
Com::function::<Says>(dialog, 15)(dialog, name.as_ptr());
start_in(dialog, &folder);
if !ask.endings.is_empty() {
Com::function::<unsafe extern "system" fn(*mut Com, u32, *const [*const u16; 2]) -> i32>(dialog, 4)(dialog, 1, &kind);
Com::function::<Says>(dialog, 22)(dialog, ending.as_ptr());
}
}
let picked = if dialog.is_null() { None } else { picked_in(dialog, owner) };
if com >= 0 {
CoUninitialize();
}
picked
}
}
unsafe fn start_in(dialog: *mut Com, folder: &[u16]) {
const AN_ITEM: [u32; 4] = named(0x4382_6D1E, 0xE718, 0x42EE, [0xBC, 0x55, 0xA1, 0xE2, 0x61, 0xC3, 0x7B, 0xFE]);
let mut item = std::ptr::null_mut();
unsafe {
if SHCreateItemFromParsingName(folder.as_ptr(), std::ptr::null_mut(), &AN_ITEM, &mut item) >= 0 && !item.is_null() {
Com::function::<unsafe extern "system" fn(*mut Com, *mut Com) -> i32>(dialog, 12)(dialog, item);
Com::release(item);
}
}
}
unsafe fn picked_in(dialog: *mut Com, owner: HANDLE) -> Option<PathBuf> {
let (mut item, mut name): (*mut Com, *mut u16) = (std::ptr::null_mut(), std::ptr::null_mut());
unsafe {
let shown = Com::function::<unsafe extern "system" fn(*mut Com, HANDLE) -> i32>(dialog, 3)(dialog, owner);
if shown == 0 {
Com::function::<unsafe extern "system" fn(*mut Com, *mut *mut Com) -> i32>(dialog, 20)(dialog, &mut item);
}
Com::release(dialog);
if item.is_null() {
return None;
}
Com::function::<unsafe extern "system" fn(*mut Com, u32, *mut *mut u16) -> i32>(item, 5)(item, 0x8005_8000, &mut name);
Com::release(item);
if name.is_null() {
return None;
}
let length = (0..).take_while(|at| *name.add(*at) != 0).count();
let file = PathBuf::from(OsString::from_wide(std::slice::from_raw_parts(name, length)));
CoTaskMemFree(name.cast());
Some(file)
}
}
#[derive(Debug)]
pub struct SystemClipboard;
fn as_png() -> u32 {
unsafe { RegisterClipboardFormatW(wide("PNG").as_ptr()) }
}
fn taken(form: u32) -> Option<Vec<u8>> {
unsafe {
if OpenClipboard(0) == 0 {
return None;
}
let mem = GetClipboardData(form);
let start = if mem == 0 { std::ptr::null() } else { GlobalLock(mem).cast::<u8>().cast_const() };
let bytes = (!start.is_null()).then(|| std::slice::from_raw_parts(start, GlobalSize(mem)).to_vec());
GlobalUnlock(mem);
CloseClipboard();
bytes
}
}
fn put(blocks: &[(u32, Vec<u8>)]) {
unsafe {
if OpenClipboard(0) == 0 {
return;
}
EmptyClipboard();
let blocks = blocks.iter().map(|(form, block)| (*form, in_memory(block))).filter(|(_, mem)| *mem != 0);
blocks.filter(|(form, mem)| SetClipboardData(*form, *mem) == 0).for_each(|(_, mem)| _ = GlobalFree(mem));
CloseClipboard();
}
}
pub(crate) fn in_memory(block: &[u8]) -> HANDLE {
unsafe {
let mem = GlobalAlloc(2, block.len());
let to = GlobalLock(mem).cast::<u8>();
if !to.is_null() {
std::ptr::copy_nonoverlapping(block.as_ptr(), to, block.len());
}
GlobalUnlock(mem);
mem
}
}
pub(crate) fn to_block(width: usize, pixels: &[u32]) -> Vec<u8> {
let rows = pixels.chunks_exact(width.max(1));
let header = [40, width as i32, rows.len() as i32, 1 | 32 << 16, 0, (pixels.len() * 4) as i32, 0, 0, 0, 0];
header.iter().flat_map(|number| number.to_le_bytes()).chain(rows.rev().flatten().flat_map(|pixel| pixel.to_le_bytes())).collect()
}
fn from_block(block: &[u8]) -> Option<(usize, Vec<u32>)> {
let number = |at: usize| block.get(at..).and_then(|rest| rest.first_chunk()).map(|four| i32::from_le_bytes(*four));
let (size, width, height, depth, packing) = (number(0)? as usize, number(4)?, number(8)?, number(12)? >> 16, number(16)?);
let usual = [0x00ff_0000, 0x0000_ff00, 0x0000_00ff].iter().enumerate().all(|(at, bits)| number(size.saturating_add(at * 4)) == Some(*bits));
let start = match (depth, packing) {
(24 | 32, 0) => size,
(32, 3) if usual => size + 12,
_ => return None,
};
let (wide, step) = (usize::try_from(width).ok().filter(|wide| *wide > 0)?, depth as usize / 8);
let stride = wide.checked_mul(step)?.div_ceil(4) * 4;
let mut rows: Vec<&[u8]> = block.get(start..)?.chunks_exact(stride).take(height.unsigned_abs() as usize).collect();
if rows.len() < height.unsigned_abs() as usize || rows.is_empty() {
return None;
}
if height > 0 {
rows.reverse();
}
let pixel = |bytes: &[u8]| u32::from_le_bytes([bytes[0], bytes[1], bytes[2], if step == 4 { bytes[3] } else { 255 }]);
let mut pixels: Vec<u32> = rows.iter().flat_map(|row| row.chunks_exact(step).take(wide).map(pixel)).collect();
if pixels.iter().all(|pixel| pixel >> 24 == 0) {
pixels.iter_mut().for_each(|pixel| *pixel |= 0xff00_0000);
}
Some((wide, pixels))
}
pub(crate) fn to_drop(files: &[PathBuf]) -> Vec<u8> {
let names = files.iter().flat_map(|file| file.as_os_str().encode_wide().chain([0])).chain([0]);
[20, 0, 0, 0, 1].iter().flat_map(|number: &i32| number.to_le_bytes()).chain(names.flat_map(u16::to_le_bytes)).collect()
}
impl Clipboard for SystemClipboard {
fn get(&mut self) -> Option<String> {
let units: Vec<u16> = taken(13)?.as_chunks().0.iter().map(|pair| u16::from_le_bytes(*pair)).collect();
Some(String::from_utf16_lossy(units.split(|unit| *unit == 0).next().unwrap_or_default()))
}
fn set(&mut self, text: &str) {
put(&[(13, wide(text).into_iter().flat_map(u16::to_le_bytes).collect())]);
}
fn picture(&mut self) -> Option<(usize, Vec<u32>)> {
(PICTURES_AND_FILES.get()?.picture)(self)
}
fn set_picture(&mut self, width: usize, pixels: &[u32]) {
PICTURES_AND_FILES.get().map_or((), |its| (its.set_picture)(self, width, pixels))
}
fn files(&mut self) -> Vec<PathBuf> {
PICTURES_AND_FILES.get().map_or(Vec::new(), |its| (its.files)(self))
}
fn set_files(&mut self, files: &[PathBuf]) {
PICTURES_AND_FILES.get().map_or((), |its| (its.set_files)(self, files))
}
}
#[derive(Clone, Copy)]
struct Drops {
take: fn(&mut Open, bool),
dropped: unsafe fn(HANDLE, f32) -> [Option<Event>; 2],
}
pub(crate) fn hand_drops() {
DROPS.set(Some(Drops { take: Open::take_files, dropped }));
}
thread_local! {
static DROPS: Cell<Option<Drops>> = const { Cell::new(None) };
static PICTURES_AND_FILES: Cell<Option<PicturesAndFiles<SystemClipboard>>> = const { Cell::new(None) };
}
#[derive(Clone, Copy)]
struct Desktop {
after: fn() -> crate::windows_shell::Asked,
ended: fn(),
}
thread_local! {
static DESKTOP: Cell<Option<Desktop>> = const { Cell::new(None) };
}
pub(crate) fn hand_desktop() {
DESKTOP.set(Some(Desktop { after: crate::windows_shell::after, ended: crate::windows_shell::ended }));
}
pub(crate) fn hand_pictures_and_files() {
let (picture, set_picture) = (SystemClipboard::picture_on, SystemClipboard::put_picture);
PICTURES_AND_FILES.set(Some(PicturesAndFiles { picture, set_picture, files: SystemClipboard::files_on, set_files: SystemClipboard::put_files }));
}
impl SystemClipboard {
fn picture_on(&mut self) -> Option<(usize, Vec<u32>)> {
let png = taken(as_png()).and_then(|png| crate::png::decode(&png));
png.or_else(|| taken(8).and_then(|block| from_block(&block)))
}
fn put_picture(&mut self, width: usize, pixels: &[u32]) {
let Some(png) = crate::png::encode_rgba(width, pixels) else { return };
put(&[(as_png(), png), (8, to_block(width, pixels))]);
}
fn files_on(&mut self) -> Vec<PathBuf> {
taken(15).map_or(Vec::new(), |block| read_drop(&block).1)
}
fn put_files(&mut self, files: &[PathBuf]) {
put(&[(15, to_drop(files))]);
}
}
static WINDOW: std::sync::atomic::AtomicIsize = std::sync::atomic::AtomicIsize::new(0);
#[derive(Clone, Copy, Debug, Default)]
pub struct Waker;
impl Waker {
pub fn wake(&self) {
let hwnd = WINDOW.load(std::sync::atomic::Ordering::Relaxed);
if hwnd != 0 {
unsafe { PostMessageW(hwnd, WM_WAKE, 0, 0) };
}
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::geom::Point;
use crate::{DragData, DropEffect, Icon, Length, SecondWindow};
use std::cell::RefCell;
#[link(name = "kernel32")]
unsafe extern "system" {
fn GlobalFlags(mem: HANDLE) -> u32;
fn GetCurrentProcess() -> HANDLE;
fn GetCurrentThreadId() -> u32;
}
#[link(name = "user32")]
unsafe extern "system" {
fn GetPropW(hwnd: HANDLE, name: *const u16) -> HANDLE;
fn GetGuiResources(process: HANDLE, which: u32) -> u32;
fn IsWindow(hwnd: HANDLE) -> i32;
fn GetWindowTextW(hwnd: HANDLE, text: *mut u16, room: i32) -> i32;
fn PeekMessageW(msg: *mut MSG, hwnd: HANDLE, min: u32, max: u32, how: u32) -> i32;
fn SetKeyboardState(keys: *const [u8; 256]) -> i32;
fn GetUpdateRect(hwnd: HANDLE, area: *mut RECT, erase: i32) -> i32;
fn PrintWindow(hwnd: HANDLE, dc: HANDLE, how: u32) -> i32;
fn EnumThreadWindows(thread: u32, each: unsafe extern "system" fn(HANDLE, isize) -> i32, with: isize) -> i32;
}
#[link(name = "imm32")]
unsafe extern "system" {
fn ImmGetCompositionWindow(context: HANDLE, form: *mut COMPOSITIONFORM) -> i32;
fn ImmGetCandidateWindow(context: HANDLE, index: u32, form: *mut CANDIDATEFORM) -> i32;
}
#[link(name = "shell32")]
unsafe extern "system" {
fn ILCreateFromPathW(path: *const u16) -> *mut core::ffi::c_void;
fn ILFindLastID(item: *const core::ffi::c_void) -> *mut core::ffi::c_void;
fn ILFree(item: *mut core::ffi::c_void);
fn SHCreateDataObject(folder: *mut core::ffi::c_void, count: u32, names: *const *mut core::ffi::c_void, inner: *mut core::ffi::c_void, id: *const [u32; 4], out: *mut *mut IDataObject) -> i32;
}
const ANY: u32 = 7;
#[derive(Default)]
struct Seen {
over: bool,
pointer: Option<Point>,
got: Vec<PathBuf>,
target: Rect,
typed: Rect,
}
fn state<'a>(hwnd: HANDLE) -> &'a mut State {
unsafe { state_of(hwnd) }.unwrap().0
}
fn kept<'a>(hwnd: HANDLE) -> &'a mut Open {
let (state, at) = unsafe { state_of(hwnd) }.unwrap();
&mut state.windows[at]
}
fn window(seen: &Rc<RefCell<Seen>>) -> (HANDLE, bool) {
let (seen, mut text) = (seen.clone(), String::new());
let app = move |ui: &mut Ui| {
let mut seen = seen.borrow_mut();
ui.column().pad(10.0).gap(10.0).open();
let target = ui.selectable(false, "Let files go here");
let typed = ui.text_box(&mut text, "Type here");
if let Some(files) = ui.files_dropped(&target) {
seen.got = files;
}
(seen.over, seen.pointer, seen.target, seen.typed) = (ui.files_hovered(), ui.pointer(), target.rect, typed.rect);
ui.close();
};
let hwnd = open(Window::default(), Box::new(app));
assert!(hwnd != 0 && unsafe { IsWindowVisible(hwnd) } == 0);
unsafe { (window_proc(hwnd, WM_SIZE, 0, 0), window_proc(hwnd, WM_SIZE, 0, 0)) };
(hwnd, state(hwnd).ole == Some(true))
}
fn close((hwnd, _): (HANDLE, bool)) {
unsafe { DestroyWindow(hwnd) };
}
fn on_screen(hwnd: HANDLE, at: Point) -> POINT {
let scale = kept(hwnd).scale;
let mut place = POINT { x: (at.x * scale) as i32, y: (at.y * scale) as i32 };
unsafe { ClientToScreen(hwnd, &mut place) };
place
}
fn middle(of: Rect) -> Point {
pt((of.x + of.w / 2.0).floor(), (of.y + of.h / 2.0).floor())
}
fn block(at: (i32, i32), on_frame: bool, wide: bool, names: &[&str]) -> Vec<u8> {
let mut block: Vec<u8> = [20, at.0, at.1, on_frame as i32, wide as i32].iter().flat_map(|number| number.to_le_bytes()).collect();
for name in names {
match wide {
true => block.extend(name.encode_utf16().chain([0]).flat_map(u16::to_le_bytes)),
false => block.extend(name.bytes().chain([0])),
}
}
block.extend([0, 0]);
block
}
fn paths(names: &[&str]) -> Vec<PathBuf> {
names.iter().map(PathBuf::from).collect()
}
const SIX: [u32; 6] = [0xff10_2030, 0x8040_5060, 0x0070_8090, 0xffa0_b0c0, 0x01d0_e0f0, 0xfe01_0203];
#[test]
fn a_picture_goes_into_the_block_of_pixels_windows_knows_and_back() {
let block = to_block(2, &SIX);
assert_eq!((block.len(), &block[40..44]), (40 + 24, &0x01d0_e0f0_u32.to_le_bytes()[..]));
assert_eq!(from_block(&block), Some((2, SIX.to_vec())));
let numbers = |numbers: [i32; 10]| numbers.iter().flat_map(|number| number.to_le_bytes()).collect::<Vec<u8>>();
let three = [numbers([40, 1, -2, 1 | 24 << 16, 0, 0, 0, 0, 0, 0]), vec![0x30, 0x20, 0x10, 0, 0x60, 0x50, 0x40, 0]].concat();
assert_eq!(from_block(&three), Some((1, vec![0xff10_2030, 0xff40_5060])));
let unsaid = [numbers([40, 2, 1, 1 | 32 << 16, 0, 0, 0, 0, 0, 0]), vec![0x30, 0x20, 0x10, 0, 0x60, 0x50, 0x40, 0]].concat();
assert_eq!(from_block(&unsaid), Some((2, vec![0xff10_2030, 0xff40_5060])));
let said = |bits: [i32; 3]| [numbers([40, 1, 1, 1 | 32 << 16, 3, 0, 0, 0, 0, 0]), bits.iter().flat_map(|bits| bits.to_le_bytes()).collect(), vec![0x30, 0x20, 0x10, 0x80]].concat();
assert_eq!((from_block(&said([0xff_0000, 0xff00, 0xff])), from_block(&said([0xff, 0xff00, 0xff_0000]))), (Some((1, vec![0x8010_2030])), None));
assert_eq!((from_block(&block[..63]), from_block(&block[..39]), from_block(&[])), (None, None, None));
assert_eq!(from_block(&numbers([40, 2, 3, 1 | 8 << 16, 0, 0, 0, 0, 0, 0])), None);
for seed in 0..2000u32 {
let noise: Vec<u8> = (0..seed % 97).map(|at| ((at + seed).wrapping_mul(2_654_435_761) >> 11) as u8).collect();
from_block(&noise);
from_block(&[&numbers([40, (seed % 5) as i32 - 1, (seed % 7) as i32 - 3, 1 | [24, 32][seed as usize % 2] << 16, [0, 3][seed as usize % 2], 0, 0, 0, 0, 0])[..], &noise].concat());
}
}
#[test]
fn files_go_into_the_block_a_file_manager_reads_and_back() {
let files = paths(&["C:\\a\\one.txt", "D:\\zwei drei\\für.png"]);
assert_eq!(read_drop(&to_drop(&files)).1, files);
assert_eq!(read_drop(&to_drop(&[])).1, paths(&[]));
}
#[link(name = "user32")]
unsafe extern "system" {
fn CreateWindowStationW(name: *const u16, flags: u32, access: u32, security: *const std::ffi::c_void) -> HANDLE;
fn SetProcessWindowStation(station: HANDLE) -> i32;
fn GetProcessWindowStation() -> HANDLE;
fn CreateDesktopW(name: *const u16, device: *const u16, mode: *const std::ffi::c_void, flags: u32, access: u32, security: *const std::ffi::c_void) -> HANDLE;
fn SetThreadDesktop(desktop: HANDLE) -> i32;
fn GetThreadDesktop(thread: u32) -> HANDLE;
fn GetUserObjectInformationW(object: HANDLE, what: i32, value: *mut u16, room: u32, needed: *mut u32) -> i32;
}
pub(crate) const ALONE: &str = "TAUT_TEST_BY_ITSELF";
fn by_itself(test: &str) {
let ran = std::process::Command::new(std::env::current_exe().unwrap()).args(["--exact", test, "--ignored", "--nocapture"]).env(ALONE, "1").output().unwrap();
let said = String::from_utf8_lossy(&ran.stdout);
assert!(ran.status.success() && said.contains("1 passed"), "{said}\n{}", String::from_utf8_lossy(&ran.stderr));
}
#[repr(C)]
struct Start {
size: u32,
reserved: *const u16,
desktop: *const u16,
title: *const u16,
numbers: [u32; 8],
show: [u16; 2],
more: *const u8,
handles: [usize; 3],
}
#[link(name = "kernel32")]
unsafe extern "system" {
fn CreateProcessW(program: *const u16, line: *mut u16, a: usize, b: usize, inherit: i32, how: u32, env: usize, folder: usize, start: *const Start, made: *mut [usize; 4]) -> i32;
fn WaitForSingleObject(of: usize, millis: u32) -> u32;
fn GetExitCodeProcess(process: usize, code: *mut u32) -> i32;
fn TerminateProcess(process: usize, code: u32) -> i32;
fn CloseHandle(handle: usize) -> i32;
}
#[link(name = "user32")]
unsafe extern "system" {
fn CloseDesktop(desktop: HANDLE) -> i32;
fn FindWindowW(class: *const u16, title: *const u16) -> HANDLE;
}
const DESKTOP: &str = "taut tests";
pub(crate) fn on_a_desktop_of_its_own(test: &str) {
let (program, said) = (std::env::current_exe().unwrap(), std::env::temp_dir().join(format!("taut-{}-{test}.txt", std::process::id()).replace("::", "-")));
let line = format!("cmd.exe /c \"set {ALONE}=1&& \"{}\" --exact {test} --ignored --nocapture > \"{}\" 2>&1\"", program.display(), said.display());
static MADE: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
let name = format!("{DESKTOP} {} {}", std::process::id(), MADE.fetch_add(1, std::sync::atomic::Ordering::Relaxed));
let (mut line, place, mut made) = (wide(&line), wide(&format!("WinSta0\\{name}")), [0usize; 4]);
let start = Start {
size: size_of::<Start>() as u32,
reserved: std::ptr::null(),
desktop: place.as_ptr(),
title: std::ptr::null(),
numbers: [0; 8],
show: [0; 2],
more: std::ptr::null(),
handles: [0; 3],
};
let code = unsafe {
let desktop = CreateDesktopW(wide(&name).as_ptr(), std::ptr::null(), std::ptr::null(), 0, 0x1000_0000, std::ptr::null());
assert!(desktop != 0 && CreateProcessW(std::ptr::null(), line.as_mut_ptr(), 0, 0, 0, 0x0800_0000, 0, 0, &start, &mut made) != 0, "the test could not be started on a desktop of its own");
let (timed_out, mut code) = (WaitForSingleObject(made[0], 120_000) != 0, 1);
TerminateProcess(made[0], 1);
GetExitCodeProcess(made[0], &mut code);
[made[0], made[1]].iter().for_each(|handle| _ = CloseHandle(*handle));
CloseDesktop(desktop);
if timed_out { 258 } else { code }
};
let said = std::fs::read_to_string(&said).unwrap_or_default();
assert!(code == 0 && said.contains("1 passed"), "the test ended with {code}:\n{said}");
}
pub(crate) fn is_on_its_own_desktop() -> bool {
let mut name = [0u16; 64];
unsafe { GetUserObjectInformationW(GetThreadDesktop(GetCurrentThreadId()), 2, name.as_mut_ptr(), 126, std::ptr::null_mut()) };
String::from_utf16_lossy(name.split(|unit| *unit == 0).next().unwrap_or_default()).starts_with(DESKTOP)
}
#[test]
fn the_clipboard_takes_text_pictures_and_files_and_gives_them_back() {
by_itself("windows::tests::on_a_clipboard_that_is_nobodys");
}
#[test]
fn a_window_opens_filling_its_screen_and_goes_back_to_where_it_was() {
on_a_desktop_of_its_own("windows::tests::on_a_desktop_nobody_sees_a_window_fills_its_screen");
}
#[test]
#[ignore = "run by the test before it, in a process of its own that is started on a desktop of its own"]
fn on_a_desktop_nobody_sees_a_window_fills_its_screen() {
if std::env::var_os(ALONE).is_none() {
return;
}
assert!(is_on_its_own_desktop(), "this is the desktop the person works on: no window is shown on it");
let asked = Window { position: Some((120, 90)), width: 400.0, height: 240.0, maximized: true, ..Window::default() };
let hwnd = open(asked, Box::new(|ui: &mut Ui| _ = ui.label("One")));
unsafe { ShowWindow(hwnd, 3) };
pump();
let (placed, filled) = (state(hwnd).ui.window_placement().expect("the window was told where it is"), state(hwnd).ui.size());
assert_eq!(placed, WindowPlacement { position: (120, 90), width: 400.0, height: 240.0, maximized: true });
assert!(filled.w > 400.0 && filled.h > 240.0, "{filled:?} is no more than it asked for");
unsafe { ShowWindow(hwnd, 9) };
pump();
assert_eq!(
(state(hwnd).ui.window_placement(), state(hwnd).ui.size(), corner_of(hwnd)),
(Some(WindowPlacement { maximized: false, ..placed }), Size { w: 400.0, h: 240.0 }, POINT { x: 120, y: 90 })
);
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn fonts_and_letters_made_at_every_size_are_held_to_a_bound() {
let mut text = SystemText::new().unwrap();
let first = text.measure("Wide", Font::new(14.0), 1.0, None);
(1..=600).for_each(|px| _ = text.measure("Wide", Font::new(px as f32), 1.0, None));
assert!(text.fonts.made.len() <= MADE, "{} fonts kept", text.fonts.made.len());
for (px, letter) in (4..=40).flat_map(|px| (0x41..0x16d).filter_map(char::from_u32).map(move |letter| (px, letter))) {
text.glyph(Font::new(px as f32), 1.0, letter);
}
assert!(text.glyphs.len() <= GLYPHS, "{} pictures of letters kept", text.glyphs.len());
assert_eq!(text.measure("Wide", Font::new(14.0), 1.0, None), first);
}
#[test]
fn the_face_for_code_has_letters_that_are_all_as_wide() {
let mut text = SystemText::new().unwrap();
let wide = |text: &mut SystemText, letters: &str, font: Font| text.measure(letters, font, 1.0, None);
let (code, plain) = (Font::new(12.0).mono(), Font::new(12.0));
assert_eq!(wide(&mut text, "iiiiiiii", code), wide(&mut text, "MMMMMMMM", code));
assert!(wide(&mut text, "iiiiiiii", plain) < wide(&mut text, "MMMMMMMM", plain));
let dc = unsafe { CreateCompatibleDC(0) };
let (none, there) = (font_named(dc, "No font is called this", 12, 400, false), font_named(dc, "Courier New", 12, 400, false));
assert!(none.is_none() && there.is_some());
unsafe { (DeleteDC(dc), DeleteObject(there.unwrap_or_default())) };
}
#[test]
fn a_window_wears_the_frame_of_its_theme_and_knows_what_the_system_was_asked_for() {
let (light, runs) = (Rc::new(Cell::new(false)), Rc::new(Cell::new(0)));
let (app, ran) = (light.clone(), runs.clone());
let describe = move |ui: &mut Ui| {
ran.set(ran.get() + 1);
ui.theme = if app.get() { crate::Theme::light() } else { crate::Theme::dark() };
ui.label("One");
};
let hwnd = open(Window::default(), Box::new(describe));
let dark = || state(hwnd).windows[0].dark;
unsafe { window_proc(hwnd, WM_SIZE, 0, 0) };
assert!(dark() && !state(hwnd).ui.window_minimized());
light.set(true);
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
assert!(!dark());
light.set(false);
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
assert!(dark());
assert_eq!(state(hwnd).ui.preferences.current, crate::preferences::of_windows());
let before = runs.get();
for message in [WM_SETTINGCHANGE, WM_SYSCOLORCHANGE, WM_DWMCOLORIZATIONCOLORCHANGED] {
unsafe { window_proc(hwnd, message, 0, 0) };
}
assert_eq!(runs.get(), before);
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn a_window_follows_the_person_when_they_change_how_things_are_to_look() {
use crate::preferences::tests::PRETENDED;
use crate::{Color, ContrastColors, Preferences, Theme};
let runs = Rc::new(Cell::new(0));
let ran = runs.clone();
let describe = move |ui: &mut Ui| {
ran.set(ran.get() + 1);
ui.theme = ui.system_theme();
ui.switch(&mut true, "On");
};
PRETENDED.set(Some(Preferences::default()));
let hwnd = open(Window::default(), Box::new(describe));
unsafe { window_proc(hwnd, WM_SIZE, 0, 0) };
assert!(!state(hwnd).windows[0].dark && state(hwnd).ui.theme == Theme::light());
let before = runs.get();
let aquatic = ContrastColors {
background: Color::rgb(0x202020),
text: Color::WHITE,
link: Color::rgb(0x75e9fc),
disabled: Color::rgb(0xa6a6a6),
selected: Color::rgb(0x8ee3f0),
selected_text: Color::rgb(0x263b50),
button: Color::rgb(0x202020),
button_text: Color::WHITE,
};
let changes = [
(WM_SETTINGCHANGE, Preferences { dark: true, text_scale: 1.5, ..Preferences::default() }),
(WM_DWMCOLORIZATIONCOLORCHANGED, Preferences { dark: true, accent: Some(Color::rgb(0xe3008c)), ..Preferences::default() }),
(WM_SYSCOLORCHANGE, Preferences { high_contrast: Some(aquatic), reduced_motion: true, ..Preferences::default() }),
];
for (nth, (message, wishes)) in changes.into_iter().enumerate() {
PRETENDED.set(Some(wishes));
unsafe { window_proc(hwnd, message, 0, 0) };
assert_eq!((runs.get(), state(hwnd).ui.preferences.current, state(hwnd).ui.theme), (before + nth + 1, wishes, Theme::following(&wishes)), "{message:x}");
assert!(state(hwnd).windows[0].dark, "{message:x}");
}
assert!(state(hwnd).ui.reduced_motion() && state(hwnd).ui.wake_in().is_none());
unsafe { window_proc(hwnd, WM_SETTINGCHANGE, 0, 0) };
assert_eq!(runs.get(), before + 3);
PRETENDED.set(None);
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn a_window_opens_minimized_comes_forward_and_stays_over_the_others_when_its_app_asks() {
on_a_desktop_of_its_own("windows::tests::on_a_desktop_nobody_sees_a_window_opens_minimized_and_comes_forward");
}
#[test]
#[ignore = "run by the test before it, in a process of its own that is started on a desktop of its own"]
fn on_a_desktop_nobody_sees_a_window_opens_minimized_and_comes_forward() {
if std::env::var_os(ALONE).is_none() {
return;
}
assert!(is_on_its_own_desktop(), "this is the desktop the person works in: no window is shown on it");
let (forward, top, runs) = (Rc::new(Cell::new(false)), Rc::new(Cell::new(None::<bool>)), Rc::new(Cell::new(0)));
let (app, asked, ran) = (forward.clone(), top.clone(), runs.clone());
let describe = move |ui: &mut Ui| {
ran.set(ran.get() + 1);
if app.take() {
ui.bring_to_front();
}
asked.take().into_iter().for_each(|on| ui.set_on_top(on));
ui.label("One");
};
let hwnd = open(Window { position: Some((120, 90)), width: 400.0, height: 240.0, maximized: true, minimized: true, ..Window::default() }, Box::new(describe));
unsafe { show_minimized(hwnd, true) };
pump();
let ui = || &state(hwnd).ui;
assert!(unsafe { IsIconic(hwnd) } != 0 && ui().window_minimized() && runs.get() > 0);
assert_eq!(ui().size(), Size { w: 400.0, h: 240.0 });
forward.set(true);
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
pump();
assert!(unsafe { IsIconic(hwnd) } == 0 && !ui().window_minimized() && unsafe { GetFocus() } == hwnd && ui().window_focused());
assert!(ui().window_placement().is_some_and(|placed| placed.maximized) && ui().size().w > 400.0);
let over = || unsafe { GetWindowLongPtrW(hwnd, -20) } & 8 != 0;
assert!(!over());
top.set(Some(true));
unsafe { (window_proc(hwnd, WM_WAKE, 0, 0), window_proc(hwnd, WM_WAKE, 0, 0)) };
assert!(over());
top.set(Some(false));
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
assert!(!over());
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn the_systems_own_file_dialog_answers_an_app_that_asks_for_it() {
on_a_desktop_of_its_own("windows::tests::on_a_desktop_nobody_sees_the_systems_file_dialog_is_answered");
}
#[test]
#[ignore = "run by the test before it, in a process of its own that is started on a desktop of its own"]
fn on_a_desktop_nobody_sees_the_systems_file_dialog_is_answered() {
if std::env::var_os(ALONE).is_none() {
return;
}
assert!(is_on_its_own_desktop(), "this is the desktop the person works on: no dialog is shown on it");
let folder = std::env::temp_dir().join(format!("taut-files-{}", std::process::id()));
std::fs::create_dir_all(&folder).unwrap();
std::fs::write(folder.join("report.txt"), "x").unwrap();
let person = |title: &'static str, button: usize| {
std::thread::spawn(move || {
let dialog = || unsafe { FindWindowW(wide("#32770").as_ptr(), wide(title).as_ptr()) };
let mut seen = false;
for _ in 0..300 {
std::thread::sleep(std::time::Duration::from_millis(200));
match (dialog(), seen) {
(0, true) => return,
(0, false) => {}
(up, _) => seen = unsafe { PostMessageW(up, 0x0111, button, 0) } != 0 || seen,
}
}
std::process::exit(3);
})
};
let ask = |saving: bool, name: &str, title: &'static str| FileDialogRequest { saving, folders: false, folder: folder.clone(), name: name.into(), endings: vec![".txt".into()], title };
let pressed = person("Pick one", 1);
assert_eq!(pick_file(0, &ask(false, "report.txt", "Pick one")), Some(folder.join("report.txt")));
pressed.join().unwrap();
let pressed = person("Never mind", 2);
assert_eq!(pick_file(0, &ask(false, "report.txt", "Never mind")), None);
pressed.join().unwrap();
let pressed = person("Keep it", 1);
assert_eq!(pick_file(0, &ask(true, "new", "Keep it")), Some(folder.join("new.txt")));
pressed.join().unwrap();
let pressed = person("In here", 1);
assert_eq!(pick_file(0, &FileDialogRequest { folders: true, endings: Vec::new(), ..ask(false, "", "In here") }), Some(folder.clone()));
pressed.join().unwrap();
let got = Rc::new(RefCell::new((crate::FileDialog::native(), None::<PathBuf>, 0)));
let (app, start) = (got.clone(), folder.clone());
let describe = move |ui: &mut Ui| {
let mut app = app.borrow_mut();
let app = &mut *app;
if std::mem::replace(&mut app.2, 1) == 0 {
app.0.only(&[".txt"]);
app.0.pick(&start);
app.0.name = "report.txt".into();
}
ui.label("Under the dialog");
app.1 = ui.file_dialog(&mut app.0).or(app.1.take());
};
assert!(DIALOG.get().is_none(), "the dialog was handed over before anything asked for it");
let hwnd = open(Window::default(), Box::new(describe));
unsafe { window_proc(hwnd, WM_SIZE, 0, 0) };
assert!(got.borrow().0.shown && got.borrow().1.is_none() && state(hwnd).ui.visible_access_nodes().is_empty());
let pressed = person("Open a file", 1);
DIALOG.get().expect("the app's asking handed the dialog over")();
pressed.join().unwrap();
assert_eq!((got.borrow().1.clone(), got.borrow().0.shown, got.borrow().0.folder.clone()), (Some(folder.join("report.txt")), false, folder.clone()));
unsafe { DestroyWindow(hwnd) };
std::fs::remove_dir_all(&folder).unwrap();
}
#[test]
#[ignore = "run by the test before it, in a process of its own: it would put a clipboard of its own under every other test's windows"]
fn on_a_clipboard_that_is_nobodys() {
if std::env::var_os(ALONE).is_none() {
return;
}
unsafe {
let (station, mut name) = (CreateWindowStationW(std::ptr::null(), 0, 0x037F, std::ptr::null()), [0u16; 64]);
assert!(station != 0 && SetProcessWindowStation(station) != 0 && GetProcessWindowStation() == station, "no window station of its own");
assert!(GetUserObjectInformationW(station, 2, name.as_mut_ptr(), 126, std::ptr::null_mut()) != 0, "a station without a name");
let name = String::from_utf16_lossy(name.split(|unit| *unit == 0).next().unwrap_or_default());
assert!(!name.is_empty() && !name.eq_ignore_ascii_case("WinSta0"), "this is the station the person works in");
let desktop = CreateDesktopW(wide("taut").as_ptr(), std::ptr::null(), std::ptr::null(), 0, 0x1000_0000, std::ptr::null());
assert!(desktop != 0 && SetThreadDesktop(desktop) != 0, "no desktop of its own");
}
let (mut clipboard, files) = (SystemClipboard, paths(&["C:\\a\\one.txt", "D:\\zwei drei\\für.png"]));
put(&[]);
clipboard.set_picture(2, &SIX);
clipboard.set_files(&files);
assert_eq!((clipboard.picture(), clipboard.files(), taken(8), taken(15)), (None, paths(&[]), None, None));
hand_pictures_and_files();
assert_eq!((clipboard.get(), clipboard.picture(), clipboard.files()), (None, None, paths(&[])));
clipboard.set("words, and ünïcode");
assert_eq!((clipboard.get().as_deref(), clipboard.picture(), clipboard.files()), (Some("words, and ünïcode"), None, paths(&[])));
clipboard.set_picture(2, &SIX);
assert_eq!((clipboard.picture(), taken(8).and_then(|block| from_block(&block)), clipboard.get()), (Some((2, SIX.to_vec())), Some((2, SIX.to_vec())), None));
clipboard.set_files(&files);
assert_eq!((clipboard.files(), clipboard.picture(), clipboard.get()), (files, None, None));
put(&[]);
}
#[repr(C)]
struct Dragged {
object: IDataObject,
held: Cell<u32>,
files: HANDLE,
text: HANDLE,
}
impl Dragged {
fn new(table: &IDataObjectVtbl, files: HANDLE) -> Dragged {
Dragged { object: IDataObject { table }, held: Cell::new(1), files, text: 0 }
}
fn of_text(table: &IDataObjectVtbl, text: &str) -> Dragged {
let text = in_memory(&wide(text).into_iter().flat_map(u16::to_le_bytes).collect::<Vec<u8>>());
Dragged { text, ..Dragged::new(table, 0) }
}
fn holds(&self, asked: &FORMATETC) -> HANDLE {
let held = match asked.format {
15 => self.files,
13 => self.text,
_ => 0,
};
if (asked.aspect, asked.index, asked.medium & 1) == (1, -1, 1) { held } else { 0 }
}
fn table() -> IDataObjectVtbl {
IDataObjectVtbl { unknown: [0, Self::hold as *const () as usize, Self::release as *const () as usize], get_data: Self::get, get_data_here: 0, query_get_data: Self::has }
}
unsafe extern "system" fn hold(this: *mut Dragged) -> u32 {
let held = unsafe { &(*this).held };
held.set(held.get() + 1);
held.get()
}
unsafe extern "system" fn release(this: *mut Dragged) -> u32 {
let held = unsafe { &(*this).held };
held.set(held.get() - 1);
held.get()
}
unsafe extern "system" fn has(this: *mut IDataObject, asked: *const FORMATETC) -> i32 {
let (this, asked) = unsafe { (&*this.cast::<Dragged>(), &*asked) };
if this.holds(asked) != 0 { 0 } else { 0x8004_0064_u32 as i32 }
}
unsafe extern "system" fn get(this: *mut IDataObject, asked: *const FORMATETC, handed: *mut STGMEDIUM) -> i32 {
if unsafe { Self::has(this, asked) } != 0 {
return 0x8004_0064_u32 as i32;
}
unsafe {
Self::hold(this.cast());
*handed = STGMEDIUM { medium: 1, memory: (*this.cast::<Dragged>()).holds(&*asked), owner: this.cast() };
}
0
}
}
fn table(target: *mut DropTarget) -> &'static DropTargetTable {
unsafe { &**target.cast::<*const DropTargetTable>() }
}
#[test]
fn dropped_files_are_read_from_their_block() {
let names = ["C:\\one.txt", "D:\\späte Töne\\𝄞 zwei.txt"];
assert_eq!(read_drop(&block((30, 40), false, true, &names)), (Some(POINT { x: 30, y: 40 }), paths(&names)));
assert_eq!(read_drop(&block((30, 40), true, true, &names)), (None, paths(&names)), "let go on the frame");
assert_eq!(read_drop(&block((1, 2), false, false, &["C:\\old\\8.3"])), (Some(POINT { x: 1, y: 2 }), paths(&["C:\\old\\8.3"])), "named in bytes");
assert_eq!(read_drop(&block((0, 0), false, true, &[])).1, paths(&[]));
let whole = block((0, 0), false, true, &names);
assert_eq!(read_drop(&whole[..whole.len() - 5]).1.len(), 2);
assert_eq!(read_drop(&whole[..30]).1, paths(&["C:\\on"]));
assert_eq!(read_drop(&whole[..12]), (None, paths(&[])));
assert_eq!(read_drop(&[]), (None, paths(&[])));
let mut lying = whole.clone();
lying[..4].copy_from_slice(&4u32.to_le_bytes());
assert_eq!(read_drop(&lying).1, paths(&[]), "names that begin inside the five numbers");
lying[..4].copy_from_slice(&u32::MAX.to_le_bytes());
assert_eq!(read_drop(&lying).1, paths(&[]), "names that begin past the end");
}
#[test]
fn alt_going_down_and_coming_up_is_news_and_so_is_the_menu_key() {
let mut said = Vec::new();
let mut messages = Messages::default();
for (msg, w, l) in [(WM_SYSKEYDOWN, 0x12, 0), (WM_SYSKEYDOWN, 0x12, 1 << 30), (WM_KEYDOWN, 0x5D, 0), (WM_SYSKEYUP, 0x12, 0)] {
unsafe { messages.events(msg, w, l, 1.0, |event| said.push(event)) };
}
let alt: Vec<Option<bool>> = said.iter().map(|event| if let Event::ModifiersChanged(held) = event { Some(held.alt) } else { None }).collect();
assert_eq!(alt, [Some(true), None, Some(false)]);
assert!(matches!(said[1], Event::Key(Key::Menu, _)));
}
#[test]
fn the_wheel_with_ctrl_zooms_and_keys_that_come_up_say_so() {
let mut said = Vec::new();
let mut messages = Messages::default();
let notches = |notches: i16| ((notches * 120) as u16 as usize) << 16 | MK_CONTROL;
for (msg, w) in [(WM_MOUSEWHEEL, notches(1)), (WM_MOUSEWHEEL, notches(-2)), (WM_KEYUP, 0xBB), (WM_SYSKEYUP, 0x6D), (WM_KEYUP, 0x10)] {
unsafe { messages.events(msg, w, 0, 1.0, |event| said.push(event)) };
}
let zooms: Vec<f32> = said.iter().filter_map(|event| if let Event::Zoom(by) = event { Some(*by) } else { None }).collect();
assert!(zooms.len() == 2 && (zooms[0] - 1.25).abs() < 1e-5 && (zooms[1] - 0.64).abs() < 1e-5, "{zooms:?}");
let ups: Vec<&Event> = said.iter().filter(|event| matches!(event, Event::KeyUp(_) | Event::ModifiersChanged(_))).collect();
assert_eq!(ups[..2], [&Event::KeyUp(Key::Char('=')), &Event::KeyUp(Key::Char('-'))]);
assert!(ups.len() == 3 && matches!(ups[2], Event::ModifiersChanged(Mods { shift: false, .. })), "{ups:?}");
}
#[test]
fn a_message_of_dropped_files_becomes_a_move_and_the_files() {
let names = ["C:\\one.txt", "C:\\two\\𝄞.txt"];
let read = |handle: HANDLE| {
let mut events = Vec::new();
unsafe { Messages::default().events(WM_DROPFILES, handle as usize, 0, 2.0, |event| events.push(event)) };
events
};
let drop = in_memory(&block((30, 40), false, true, &names));
assert_eq!(read(drop), []);
hand_drops();
let said = [Event::Move(pt(15.0, 20.0)), Event::FilesDropped(paths(&names))];
assert_eq!(read(drop), said);
assert_eq!(read(drop), said, "reading a drop does not use it up");
let on_frame = in_memory(&block((30, 40), true, true, &names));
assert_eq!(read(on_frame), []);
unsafe { (GlobalFree(drop), GlobalFree(on_frame)) };
assert_eq!(read(0), []);
}
#[test]
fn files_dragged_over_the_window_reach_the_interface() {
let seen = Rc::new(RefCell::new(Seen::default()));
let (names, mut effect) = (["C:\\one.txt", "D:\\späte Töne\\𝄞 zwei.txt"], ANY);
let (hwnd, ole) = window(&seen);
let (box_at, beside) = (middle(seen.borrow().target), pt(300.0, 400.0));
let (data_table, files) = (Dragged::table(), in_memory(&block((0, 0), false, true, &names)));
let (mut dragged, mut other) = (Dragged::new(&data_table, files), Dragged::new(&data_table, 0));
let (data, no_files): (*mut IDataObject, *mut IDataObject) = (&raw mut dragged.object, &raw mut other.object);
let target = DropTarget::new(hwnd);
let calls = table(target);
let saw = || (seen.borrow().over, seen.borrow().pointer);
unsafe {
assert_eq!(((calls.enter)(target, data, 0, on_screen(hwnd, beside), &mut effect), effect), (0, COPY));
assert_eq!(saw(), (true, Some(beside)));
assert_eq!(((calls.over)(target, 0, on_screen(hwnd, box_at), &mut effect), effect), (0, COPY));
assert_eq!(saw(), (true, Some(box_at)));
assert_eq!((calls.leave)(target), 0);
assert_eq!(saw(), (false, None), "the pointer left with the files");
effect = ANY;
(calls.enter)(target, data, 0, on_screen(hwnd, beside), &mut effect);
assert_eq!(((calls.let_go)(target, data, 0, on_screen(hwnd, beside), &mut effect), effect), (0, 0));
assert_eq!((saw(), seen.borrow().got.len()), ((false, Some(beside)), 0));
effect = ANY;
(calls.enter)(target, data, 0, on_screen(hwnd, beside), &mut effect);
(calls.over)(target, 0, on_screen(hwnd, box_at), &mut effect);
assert_eq!(((calls.let_go)(target, data, 0, on_screen(hwnd, box_at), &mut effect), effect), (0, COPY));
assert_eq!((saw(), &seen.borrow().got), ((false, Some(box_at)), &paths(&names)));
assert_eq!(dragged.held.get(), 1, "what was handed over was given back, each time");
effect = ANY;
assert_eq!(((calls.enter)(target, no_files, 0, on_screen(hwnd, beside), &mut effect), effect), (0, 0));
assert_eq!(((calls.over)(target, 0, on_screen(hwnd, beside), &mut effect), effect), (0, 0));
assert_eq!(((calls.leave)(target), saw()), (0, (false, Some(box_at))), "the interface heard nothing of it");
effect = 2;
assert_eq!(((calls.enter)(target, data, 0, on_screen(hwnd, beside), &mut effect), effect), (0, 0));
assert_eq!(((calls.leave)(target), saw()), (0, (false, Some(box_at))));
let (mut same, drop_target, unknown, data_object) = (std::ptr::null_mut(), [0x122, 0, 0xC0, 0x4600_0000], [0, 0, 0xC0, 0x4600_0000], [0x10e, 0, 0xC0, 0x4600_0000]);
assert_eq!(((calls.query)(target, &drop_target, &mut same), same, (*target).held.get()), (0, target, 2));
assert_eq!(((calls.query)(target, &unknown, &mut same), same, (*target).held.get()), (0, target, 3));
assert_eq!(((calls.query)(target, &data_object, &mut same), same.is_null(), (*target).held.get()), (0x8000_4002_u32 as i32, true, 3));
assert_eq!(((calls.hold)(target), (calls.release)(target), (calls.release)(target), (calls.release)(target), (calls.release)(target)), (4, 3, 2, 1, 0));
GlobalFree(files);
}
close((hwnd, ole));
}
#[test]
fn the_shell_s_own_drag_is_understood() {
let seen = Rc::new(RefCell::new(Seen::default()));
let program = std::env::current_exe().unwrap();
let beside = std::fs::read_dir(program.parent().unwrap()).unwrap().map(|entry| entry.unwrap().path()).find(|path| *path != program).unwrap();
let files = vec![program.clone(), beside];
let item = |path: &std::path::Path| unsafe { ILCreateFromPathW(wide(path.to_str().unwrap()).as_ptr()) };
let (mut data, mut effect, data_object) = (std::ptr::null_mut(), ANY, [0x10e, 0, 0xC0, 0x4600_0000]);
let (hwnd, ole) = window(&seen);
unsafe {
let (folder, items) = (item(program.parent().unwrap()), [item(&files[0]), item(&files[1])]);
let names = [ILFindLastID(items[0]), ILFindLastID(items[1])];
assert_eq!(SHCreateDataObject(folder, 2, names.as_ptr(), std::ptr::null_mut(), &data_object, &mut data), 0);
let (target, at) = (DropTarget::new(hwnd), on_screen(hwnd, middle(seen.borrow().target)));
assert_eq!(((table(target).enter)(target, data, 0, at, &mut effect), effect, seen.borrow().over), (0, COPY, true));
assert_eq!(((table(target).let_go)(target, data, 0, at, &mut effect), effect, seen.borrow().over), (0, COPY, false));
assert_eq!(seen.borrow().got, files);
let release: unsafe extern "system" fn(*mut IDataObject) -> u32 = std::mem::transmute((*(*data).table).unknown[2]);
assert_eq!((release(data), (table(target).release)(target)), (0, 0));
[folder, items[0], items[1]].into_iter().for_each(|item| ILFree(item));
}
close((hwnd, ole));
}
#[test]
fn windows_holds_the_drop_target_until_the_window_is_destroyed() {
let seen = Rc::new(RefCell::new(Seen::default()));
let (hwnd, ole) = window(&seen);
unsafe {
assert_eq!((ole, GetWindowLongPtrW(hwnd, -20) & 16), (true, 0), "no need for files as a message");
let target = GetPropW(hwnd, wide("OleDropTargetInterface").as_ptr()) as *mut DropTarget;
assert!(!target.is_null() && std::ptr::eq(table(target), &DROP_TARGET));
assert_eq!(((*target).hwnd, (*target).held.get()), (hwnd, 1), "Windows alone holds it");
let second = DropTarget::new(hwnd);
assert_eq!((RegisterDragDrop(hwnd, second.cast()), (*second).held.get()), (0x8004_0101_u32 as i32, 1));
assert_eq!((table(second).release)(second), 0);
assert_eq!((table(target).hold)(target), 2);
DestroyWindow(hwnd);
assert_eq!((table(target).release)(target), 0, "Windows let go when the window went");
}
}
#[test]
fn a_window_whose_app_never_asks_about_files_is_spared_taking_them() {
let hwnd = open(Window::default(), Box::new(|ui: &mut Ui| _ = ui.button("Nothing to do with files")));
unsafe {
window_proc(hwnd, WM_SIZE, 0, 0);
window_proc(hwnd, WM_SIZE, 0, 0);
assert_eq!((state(hwnd).ole, kept(hwnd).files), (None, false));
assert_eq!((GetPropW(hwnd, wide("OleDropTargetInterface").as_ptr()), GetWindowLongPtrW(hwnd, -20) & 16), (0, 0), "no drop target, and no files as a message");
DestroyWindow(hwnd);
}
}
#[test]
fn where_ole_cannot_be_had_dropped_files_come_as_a_message() {
let seen = Rc::new(RefCell::new(Seen::default()));
let names = ["C:\\one.txt", "C:\\two.txt"];
unsafe {
assert_eq!(CoInitializeEx(std::ptr::null_mut(), 0), 0);
let (hwnd, ole) = window(&seen);
assert_eq!((ole, GetWindowLongPtrW(hwnd, -20) & 16), (false, 16), "no OLE: the window accepts files as a message");
assert_eq!(GetPropW(hwnd, wide("OleDropTargetInterface").as_ptr()), 0, "and has no drop target");
let (at, scale) = (middle(seen.borrow().target), kept(hwnd).scale);
let drop = in_memory(&block(((at.x * scale) as i32, (at.y * scale) as i32), false, true, &names));
let mut msg = MSG::default();
PostMessageW(hwnd, WM_DROPFILES, drop as usize, 0);
assert!(GetMessageW(&mut msg, hwnd, WM_DROPFILES, WM_DROPFILES) > 0);
DispatchMessageW(&msg);
assert_eq!((seen.borrow().pointer, &seen.borrow().got), (Some(at), &paths(&names)));
assert_eq!(GlobalFlags(drop), 0x8000, "the memory the files came in was freed");
close((hwnd, ole));
CoUninitialize();
}
}
#[test]
fn alt_and_its_letters_are_the_windows_own_while_it_has_a_bar_of_menus() {
keys_up();
let opened = Rc::new(std::cell::Cell::new(false));
let app = opened.clone();
let describe = move |ui: &mut Ui| {
ui.menu_bar();
app.set(ui.menu_button("&File"));
if app.get() {
ui.menu_item("&Open");
ui.close();
}
ui.close();
};
let hwnd = open(Window::default(), Box::new(describe));
unsafe { (window_proc(hwnd, WM_SIZE, 0, 0), window_proc(hwnd, WM_SYSKEYDOWN, 0x12, 1 << 29), window_proc(hwnd, WM_SYSKEYDOWN, 'F' as usize, 1 << 29)) };
assert!(opened.get());
assert_eq!(unsafe { answer(hwnd, WM_SYSCHAR, 'f' as usize, 1 << 29) }, Some(0));
assert_eq!(unsafe { (answer(hwnd, WM_SYSCOMMAND, SC_KEYMENU, 0), answer(hwnd, WM_SYSCHAR, ' ' as usize, 1 << 29)) }, (Some(0), None));
assert!(unsafe { answer(hwnd, WM_SYSCOMMAND, SC_KEYMENU, ' ' as isize) }.is_none());
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn input_methods_are_kept_out_of_a_box_for_a_secret() {
keys_up();
let typed = Rc::new(RefCell::new((String::new(), String::new())));
let app = typed.clone();
let describe = move |ui: &mut Ui| {
let mut app = app.borrow_mut();
ui.password_box(&mut app.0, "Password");
ui.text_box(&mut app.1, "Name");
};
let hwnd = open(Window::default(), Box::new(describe));
let types_through = || unsafe {
let context = ImmGetContext(hwnd);
ImmReleaseContext(hwnd, context);
context != 0
};
unsafe { (window_proc(hwnd, WM_SIZE, 0, 0), window_proc(hwnd, WM_SETFOCUS, 0, 0)) };
assert!(types_through(), "this thread has nothing to type through to begin with");
unsafe { (window_proc(hwnd, WM_KEYDOWN, 9, 0), window_proc(hwnd, WM_CHAR, 'p' as usize, 0)) };
assert!(state(hwnd).ui.password_focused() && !types_through() && typed.borrow().0 == "p");
unsafe { (window_proc(hwnd, WM_KEYDOWN, 9, 0), window_proc(hwnd, WM_CHAR, 'n' as usize, 0)) };
assert!(!state(hwnd).ui.password_focused() && types_through() && *typed.borrow() == ("p".to_string(), "n".to_string()));
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn the_typing_window_follows_the_text_cursor() {
keys_up();
assert_eq!(cursor_pixels(rect(10.4, 20.0, 1.0, 17.0), 1.5), RECT { left: 16, top: 30, right: 17, bottom: 56 });
let seen = Rc::new(RefCell::new(Seen::default()));
let (hwnd, ole) = window(&seen);
let nothing = COMPOSITIONFORM { style: 0, at: POINT::default(), area: RECT::default() };
assert_eq!(unsafe { (ImmSetCompositionWindow(0, ¬hing), ImmReleaseContext(hwnd, 0)) }, (0, 1), "with nothing to type through, telling fails and no more");
let told = || unsafe {
let (mut text, mut list) = (COMPOSITIONFORM { style: 0, at: POINT::default(), area: RECT::default() }, CANDIDATEFORM { index: 0, style: 0, at: POINT::default(), area: RECT::default() });
let context = ImmGetContext(hwnd);
assert!(context != 0, "this thread has no input context to read back from");
ImmGetCompositionWindow(context, &mut text);
let list_told = ImmGetCandidateWindow(context, 0, &mut list) != 0;
ImmReleaseContext(hwnd, context);
(text.style, (text.at.x, text.at.y), list_told.then_some((list.style, (list.at.x, list.at.y), list.area)))
};
let cursor = || state(hwnd).ui.ime_area();
let scale = kept(hwnd).scale;
unsafe { window_proc(hwnd, WM_IME_STARTCOMPOSITION, 0, 0) };
assert_eq!((cursor(), told().2), (None, None));
unsafe { (window_proc(hwnd, WM_KEYDOWN, 9, 0), window_proc(hwnd, WM_KEYDOWN, 9, 0)) };
let at = cursor().expect("the text box has the keyboard");
let spot = cursor_pixels(at, scale);
assert!(seen.borrow().typed.contains(pt(at.x, at.y)) && at.h > 0.0);
assert_eq!(told().2, None);
unsafe { window_proc(hwnd, WM_IME_STARTCOMPOSITION, 0, 0) };
assert_eq!(told(), (2, (spot.left, spot.top), Some((0x80, (spot.left, spot.top), spot))));
unsafe { (window_proc(hwnd, WM_CHAR, 'm' as usize, 0), window_proc(hwnd, WM_CHAR, 'm' as usize, 0)) };
let moved = cursor_pixels(cursor().unwrap(), scale);
assert!(moved.left > spot.left && moved.top == spot.top);
assert_eq!(told().1, (spot.left, spot.top));
unsafe { window_proc(hwnd, WM_IME_COMPOSITION, 0, 0) };
assert_eq!(told(), (2, (moved.left, moved.top), Some((0x80, (moved.left, moved.top), moved))));
close((hwnd, ole));
}
#[test]
fn the_pointer_is_kept_until_the_last_button_is_up() {
keys_up();
let seen = Rc::new(RefCell::new(Seen::default()));
let (hwnd, ole) = window(&seen);
unsafe { (window_proc(hwnd, WM_LBUTTONDOWN, 1, 0), window_proc(hwnd, WM_RBUTTONDOWN, 3, 0)) };
assert_eq!(unsafe { GetCapture() }, hwnd);
unsafe { window_proc(hwnd, WM_RBUTTONUP, 1, 0) };
assert_eq!(unsafe { GetCapture() }, hwnd, "the left button is still down");
unsafe { window_proc(hwnd, WM_LBUTTONUP, 0, 0) };
assert_eq!(unsafe { GetCapture() }, 0);
close((hwnd, ole));
}
#[test]
fn a_line_of_text_stands_with_its_letters_in_the_middle_of_its_box() {
let middle = |font: Font, letter: &str| {
let mut ui = Ui::new(crate::platform::system_text(), Box::new(crate::platform::LocalClipboard::default()));
ui.resize(Size { w: 40.0, h: 61.0 }, 1.0);
(0..2).for_each(|_| _ = ui.run(&mut |ui| _ = ui.el().size(40.0, 61.0).fill(Color::BLACK).text(letter, font, Color::WHITE).show()));
let drawn = crate::Surface::draw(&mut ui, 64);
let written: Vec<usize> = drawn.pixels.chunks(drawn.width).enumerate().filter(|(_, row)| row.iter().any(|pixel| pixel & 0xff > 0x20)).map(|(y, _)| y).collect();
[written[0] as f32, written[written.len() - 1] as f32]
};
for font in [10.5, 11.0, 11.5, 12.0, 13.0, 16.0, 24.0].into_iter().flat_map(|size| [Font::new(size), Font::new(size).bold(), Font::new(size).mono()]) {
let [top, line] = middle(font, "HEIGHT 12");
assert!(((top + line) / 2.0 - 30.0).abs() <= 0.5, "{font:?}: its capitals stand {} pixels under the middle of their box", (top + line) / 2.0 - 30.0);
let ([top, line], [small, _]) = (middle(font, "Hx"), middle(font, "x"));
let off = ((top + line) / 2.0 + (small + line) / 2.0) / 2.0 - 30.0;
assert!(off.abs() <= 0.5, "{font:?}: its letters stand {off} pixels under the middle of their box");
}
}
#[test]
fn text_for_a_surface_gives_back_what_it_took() {
let taken = || unsafe { GetGuiResources(GetCurrentProcess(), 0) };
let before = taken();
for _ in 0..1000 {
let mut text = SystemText::new().unwrap();
assert!(text.measure("Abc", Font::new(14.0), 1.0, None) > 0.0 && text.measure("Abc", Font::new(14.0).bold(), 1.0, None) > 0.0);
}
assert!(taken() < before + 600, "{} more than the {before} before", taken() - before);
}
struct Two {
prefs: SecondWindow,
log: SecondWindow,
pressed: u32,
dark: bool,
text: String,
tick: Rect,
typed: Rect,
asks: bool,
got: Vec<PathBuf>,
asks_first: bool,
keep: bool,
shuts: bool,
title: Option<&'static str>,
}
impl Two {
fn new() -> Rc<RefCell<Two>> {
let (prefs, log) = (SecondWindow::new(200.0, 120.0), SecondWindow { open: false, ..SecondWindow::new(150.0, 90.0) });
Rc::new(RefCell::new(Two {
prefs,
log,
pressed: 0,
dark: false,
text: String::new(),
tick: Rect::EMPTY,
typed: Rect::EMPTY,
asks: false,
got: Vec::new(),
asks_first: false,
keep: false,
shuts: false,
title: None,
}))
}
}
fn two(app: &Rc<RefCell<Two>>, corner: Option<POINT>) -> HANDLE {
let app = app.clone();
let describe = move |ui: &mut Ui| {
let mut app = app.borrow_mut();
let app = &mut *app;
app.pressed += ui.button("In the first window").clicked as u32;
_ = app.asks_first && ui.files_hovered();
if ui.window(&mut app.prefs, "Preferences") {
app.tick = ui.checkbox(&mut app.dark, "Dark").rect;
let typed = ui.text_box(&mut app.text, "Type here");
if app.asks {
app.got.extend(ui.files_dropped(&typed).into_iter().flatten());
}
app.typed = typed.rect;
app.title.take().into_iter().for_each(|title| ui.set_title(title));
ui.close();
}
app.prefs.open |= app.keep;
app.prefs.open &= !(app.shuts && !app.got.is_empty());
if ui.window(&mut app.log, "Log") {
ui.label("Nothing yet");
ui.close();
}
};
let hwnd = open(Window::default(), Box::new(describe));
unsafe {
corner.into_iter().for_each(|at| _ = SetWindowPos(hwnd, 0, at.x, at.y, 0, 0, 0x0015));
window_proc(hwnd, WM_SIZE, 0, 0);
window_proc(hwnd, WM_SIZE, 0, 0);
}
hwnd
}
const FAR: POINT = POINT { x: -30000, y: -30000 };
fn windows(first: HANDLE) -> Vec<HANDLE> {
state(first).windows.iter().map(|window| window.hwnd).collect()
}
fn title(hwnd: HANDLE) -> String {
let mut text = [0u16; 64];
let len = unsafe { GetWindowTextW(hwnd, text.as_mut_ptr(), text.len() as i32) };
String::from_utf16_lossy(&text[..len as usize])
}
fn inside(hwnd: HANDLE) -> (i32, i32) {
let mut area = RECT::default();
unsafe { GetClientRect(hwnd, &mut area) };
(area.right, area.bottom)
}
fn outside(hwnd: HANDLE) -> RECT {
let mut area = RECT::default();
unsafe { GetWindowRect(hwnd, &mut area) };
area
}
struct Sheet {
dc: HANDLE,
bitmap: HANDLE,
pixels: *mut u32,
wide: i32,
high: i32,
}
impl Sheet {
fn new(hwnd: HANDLE) -> Sheet {
let ((wide, high), mut pixels) = (inside(hwnd), std::ptr::null_mut());
let info = BITMAPINFO { size: 40, width: wide, height: -high, planes: 1, bits: 32, ..Default::default() };
unsafe {
let dc = CreateCompatibleDC(0);
let bitmap = CreateDIBSection(dc, &info, 0, &mut pixels, 0, 0);
SelectObject(dc, bitmap);
assert!(!pixels.is_null());
Sheet { dc, bitmap, pixels, wide, high }
}
}
fn draw(&self, hwnd: HANDLE, parts: &[RECT]) {
let state = state(hwnd);
state.strip.fit(state.windows.iter().map(|window| window.wide).max().unwrap_or(0));
parts.iter().for_each(|part| state.draw(0, self.dc, *part));
}
fn all(&self) -> RECT {
RECT { left: 0, top: 0, right: self.wide, bottom: self.high }
}
fn pixels(&self) -> Vec<u32> {
unsafe {
GdiFlush();
std::slice::from_raw_parts(self.pixels, (self.wide * self.high) as usize).to_vec()
}
}
}
impl Drop for Sheet {
fn drop(&mut self) {
unsafe {
DeleteDC(self.dc);
DeleteObject(self.bitmap);
}
}
}
pub(crate) fn picture(hwnd: HANDLE) -> (Vec<u32>, usize) {
let mut last: Option<(Vec<u32>, usize)> = None;
for _ in 0..10 {
let sheet = Sheet::new(hwnd);
assert!(unsafe { PrintWindow(hwnd, sheet.dc, 3) != 0 });
let now = (sheet.pixels(), sheet.wide as usize);
if last.as_ref() == Some(&now) {
return now;
}
last = Some(now);
}
panic!("ten pictures in a row of the window differed")
}
fn stale(hwnd: HANDLE) -> RECT {
let mut area = RECT::default();
unsafe { GetUpdateRect(hwnd, &mut area, 0) };
area
}
fn settled(hwnds: &[HANDLE]) {
for _ in 0..100 {
pump();
hwnds.iter().for_each(|hwnd| _ = picture(*hwnd));
if !waiting() && hwnds.iter().all(|hwnd| stale(*hwnd) == RECT::default()) {
return;
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
panic!("the windows never came to rest");
}
fn waiting() -> bool {
let mut msg = MSG::default();
unsafe { PeekMessageW(&mut msg, 0, 0, 0, 0) != 0 }
}
fn place(hwnd: HANDLE, at: Point) -> isize {
let scale = kept(hwnd).scale;
((at.y * scale) as isize) << 16 | (at.x * scale) as isize
}
fn keys_up() {
unsafe { SetKeyboardState(&[0; 256]) };
}
pub(crate) fn carries_device_making() -> bool {
let mut name = *b"D3D11CreateDevicf";
name[16] = std::hint::black_box(name[16]) - 1;
let program = std::fs::read(std::env::current_exe().unwrap()).unwrap();
program.windows(name.len()).any(|bytes| bytes == name)
}
#[test]
#[cfg(not(feature = "gpu"))]
fn without_the_feature_the_gpu_path_is_not_compiled_in() {
assert!(!carries_device_making());
}
pub(crate) fn pump() -> bool {
let (mut msg, mut end) = (MSG::default(), false);
while unsafe { PeekMessageW(&mut msg, 0, 0, 0, 1) } != 0 {
end |= msg.message == 18;
unsafe { DispatchMessageW(&msg) };
}
end
}
#[test]
fn a_second_window_is_a_window_of_its_own() {
keys_up();
let app = Two::new();
let first = two(&app, None);
let [_, second] = windows(first)[..] else { panic!("{} windows", windows(first).len()) };
let scale = kept(second).scale;
assert_eq!((title(second), inside(second)), ("Preferences".into(), ((200.0 * scale) as i32, (120.0 * scale) as i32)));
assert_eq!(unsafe { (GetWindowLongPtrW(second, -8), IsWindowVisible(second)) }, (0, 0));
let click = |hwnd: HANDLE| {
[(WM_MOUSEMOVE, 0), (WM_LBUTTONDOWN, 1), (WM_LBUTTONUP, 0)].into_iter().for_each(|(msg, w)| _ = unsafe { PostMessageW(hwnd, msg, w, place(hwnd, pt(12.0, 14.0))) });
pump();
};
click(second);
assert_eq!((app.borrow().dark, app.borrow().pressed), (true, 0));
click(first);
assert_eq!((app.borrow().dark, app.borrow().pressed), (true, 1));
app.borrow_mut().title = Some("Renamed");
unsafe { window_proc(first, WM_WAKE, 0, 0) };
assert_eq!((title(second), title(first)), ("Renamed".into(), String::new()));
unsafe { DestroyWindow(first) };
}
#[test]
fn a_second_window_goes_when_the_app_no_longer_describes_it() {
let app = Two::new();
let first = two(&app, None);
let second = windows(first)[1];
app.borrow_mut().keep = true;
unsafe { window_proc(second, WM_CLOSE, 0, 0) };
assert_eq!((app.borrow().prefs.open, unsafe { IsWindow(second) }, windows(first)), (true, 1, vec![first, second]));
app.borrow_mut().keep = false;
unsafe { window_proc(second, WM_CLOSE, 0, 0) };
assert_eq!((app.borrow().prefs.open, unsafe { IsWindow(second) }, windows(first)), (false, 0, vec![first]));
app.borrow_mut().prefs.open = true;
unsafe { window_proc(first, WM_WAKE, 0, 0) };
let again = windows(first)[1];
assert_eq!((title(again), unsafe { IsWindow(again) }), ("Preferences".into(), 1));
app.borrow_mut().prefs.open = false;
unsafe { window_proc(first, WM_WAKE, 0, 0) };
assert_eq!((unsafe { IsWindow(again) }, windows(first)), (0, vec![first]));
app.borrow_mut().log.open = true;
unsafe { window_proc(first, WM_WAKE, 0, 0) };
let log = windows(first)[1];
assert_eq!((title(log), unsafe { DestroyWindow(log) }), ("Log".into(), 1));
assert_eq!((app.borrow().log.open, unsafe { IsWindow(log) }, windows(first)), (false, 0, vec![first]));
unsafe { DestroyWindow(first) };
}
#[test]
fn closing_the_first_window_ends_the_others_and_the_program() {
let app = Two::new();
app.borrow_mut().log.open = true;
let first = two(&app, None);
let all = windows(first);
assert_eq!((all.len(), Rc::strong_count(&app)), (3, 2));
unsafe { window_proc(first, WM_CLOSE, 0, 0) };
assert_eq!(all.iter().map(|hwnd| unsafe { IsWindow(*hwnd) }).sum::<i32>(), 0, "every window is gone");
assert_eq!((Rc::strong_count(&app), pump()), (1, true), "what the windows ran is freed, and the loop is told to end");
}
#[test]
fn windows_that_go_leave_nothing_behind() {
unsafe extern "system" fn count(_: HANDLE, into: isize) -> i32 {
unsafe { *(into as *mut u32) += 1 };
1
}
let here = || {
let mut windows = 0u32;
unsafe { EnumThreadWindows(GetCurrentThreadId(), count, &raw mut windows as isize) };
windows
};
let taken = |which: u32| unsafe { GetGuiResources(GetCurrentProcess(), which) };
#[cfg(feature = "gpu")]
crate::gpu_windows::tests::fake(crate::gpu_windows::tests::Faked { no_gpu: true, ..Default::default() });
let round = || {
let app = Two::new();
app.borrow_mut().log.open = true;
let first = two(&app, None);
windows(first).into_iter().for_each(|hwnd| _ = unsafe { window_proc(hwnd, WM_PAINT, 0, 0) });
assert!(state(first).strip.bitmap.get() != 0 && windows(first).len() == 3);
app.borrow_mut().prefs.open = false;
unsafe { (window_proc(first, WM_WAKE, 0, 0), DestroyWindow(first)) };
assert!(pump() && Rc::strong_count(&app) == 1);
};
round();
let before = (here(), taken(0), taken(1));
(0..200).for_each(|_| round());
assert_eq!(here(), before.0, "windows of this thread");
assert!(taken(0) < before.1 + 100, "{} more to draw with than the {} before", taken(0) - before.1, before.1);
assert!(taken(1) < before.2 + 100, "{} more windows and the like than the {} before", taken(1) - before.2, before.2);
}
#[test]
fn what_changes_in_a_second_window_is_drawn_again_there() {
keys_up();
let app = Two::new();
let first = two(&app, Some(FAR));
let second = windows(first)[1];
for hwnd in [first, second] {
unsafe { (SetWindowLongPtrW(hwnd, -20, GetWindowLongPtrW(hwnd, -20) | 0x80 | 0x0800_0000), ShowWindow(hwnd, 4)) };
}
let click = |hwnd: HANDLE| [(WM_MOUSEMOVE, 0), (WM_LBUTTONDOWN, 1), (WM_LBUTTONUP, 0)].map(|(msg, w)| unsafe { window_proc(hwnd, msg, w, place(hwnd, pt(12.0, 14.0))) });
assert!(unsafe { IsWindowVisible(first) != 0 && IsWindowVisible(second) != 0 && MonitorFromWindow(second, 0) == 0 && GetForegroundWindow() != second });
settled(&[first, second]);
let (ground, wider) = (state(first).ui.theme.bg.0, inside(first).0.max(inside(second).0));
assert_eq!((picture(first).0.last(), picture(second).0.last(), state(first).strip.width.get()), (Some(&ground), Some(&ground), wider));
let (row, scale) = (cursor_pixels(app.borrow().tick, kept(second).scale), kept(second).scale);
let (x, y) = (row.left + (3.0 * scale) as i32, (row.top + row.bottom) / 2);
let in_box = || {
let (pixels, wide) = picture(second);
pixels[y as usize * wide + x as usize]
};
let unticked = in_box();
assert_eq!((stale(first), stale(second)), (RECT::default(), RECT::default()), "nothing is left to draw");
click(second);
let changed = stale(second);
assert!(app.borrow().dark && stale(first) == RECT::default());
assert!(changed.left >= row.left && changed.top >= row.top && changed.right <= row.right && changed.bottom <= row.bottom, "{changed:?} is not in {row:?}");
assert!(changed.left <= x && x < changed.right && changed.top <= y && y < changed.bottom, "{changed:?} leaves out the box");
assert_ne!(in_box(), unticked, "the tick shows in what the window draws");
settled(&[first, second]);
click(first);
assert!(app.borrow().pressed == 1 && stale(first) != RECT::default() && stale(second) == RECT::default());
unsafe { DestroyWindow(first) };
}
#[link(name = "user32")]
unsafe extern "system" {
fn GetClassLongPtrW(hwnd: HANDLE, which: i32) -> usize;
fn GetIconInfo(icon: HANDLE, parts: *mut ICONINFO) -> i32;
}
#[link(name = "gdi32")]
unsafe extern "system" {
fn GetObjectW(object: HANDLE, room: i32, into: *mut i32) -> i32;
}
fn icon_shown(hwnd: HANDLE, which: i32) -> i32 {
let (mut parts, mut picture) = (ICONINFO::default(), [0i32; 8]);
unsafe {
GetIconInfo(GetClassLongPtrW(hwnd, which) as HANDLE, &mut parts);
GetObjectW(parts.color, 32, picture.as_mut_ptr());
DeleteObject(parts.color);
DeleteObject(parts.mask);
}
picture[1]
}
#[test]
fn its_windows_have_the_programs_own_pictures_of_itself() {
let pictures = [16usize, 32, 48, 64].map(|side| (side, vec![0xff40_5060u32; side * side]));
let icons = pictures.each_ref().map(|(side, pixels)| (*side, pixels.as_slice()));
let hwnd = open(Window { position: Some((300, 200)), width: 400.0, height: 240.0, icons: &icons, ..Window::default() }, Box::new(|ui: &mut Ui| _ = ui.label("One")));
let fitting = |which: i32| [16, 32, 48, 64].into_iter().find(|side| *side >= unsafe { GetSystemMetrics(which) }).unwrap_or(64);
assert_eq!([icon_shown(hwnd, -14), icon_shown(hwnd, -34)], [fitting(11), fitting(49)]);
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn a_window_opens_where_its_app_says_and_its_app_is_told_where_it_is() {
let first = open(Window { position: Some((300, 200)), width: 400.0, height: 240.0, ..Window::default() }, Box::new(|ui: &mut Ui| _ = ui.label("One")));
unsafe { window_proc(first, WM_SIZE, 0, 0) };
let placed = || state(first).ui.window_placement().expect("the window was told where it is");
assert_eq!((corner_of(first), placed()), (POINT { x: 300, y: 200 }, WindowPlacement { position: (300, 200), width: 400.0, height: 240.0, maximized: false }));
unsafe { SetWindowPos(first, 0, 520, 360, 0, 0, 0x0015) };
assert_eq!(placed().position, (520, 360));
unsafe { (SetWindowLongPtrW(first, -16, GetWindowLongPtrW(first, -16) | 0x0100_0000), window_proc(first, WM_MOVE, 0, 0)) };
assert_eq!(placed(), WindowPlacement { position: (520, 360), width: 400.0, height: 240.0, maximized: true });
unsafe { DestroyWindow(first) };
let lost = open(Window { position: Some((-30000, -30000)), ..Window::default() }, Box::new(|ui: &mut Ui| _ = ui.label("One")));
assert!(corner_of(lost).x > -30000 && corner_of(lost).y > -30000);
unsafe { DestroyWindow(lost) };
let app = Two::new();
app.borrow_mut().prefs = SecondWindow::new(200.0, 120.0).position(640, 480).min_size(120.0, 60.0);
let first = two(&app, None);
let second = windows(first)[1];
assert_eq!((corner_of(second), kept(second).min_size, app.borrow().prefs.position), (POINT { x: 640, y: 480 }, (120.0, 60.0), Some((640, 480))));
unsafe { SetWindowPos(second, 0, 700, 500, 0, 0, 0x0015) };
assert_eq!((corner_of(second), app.borrow().prefs.position), (POINT { x: 700, y: 500 }, Some((700, 500))));
let log = windows(first).len();
app.borrow_mut().log.open = true;
unsafe { window_proc(first, WM_WAKE, 0, 0) };
pump();
let third = windows(first)[log];
assert_eq!(app.borrow().log.position.map(|(x, y)| POINT { x, y }), Some(corner_of(third)));
unsafe { DestroyWindow(first) };
}
#[test]
fn a_new_window_opens_a_step_from_the_one_before_it() {
let app = Two::new();
app.borrow_mut().log.open = true;
let first = two(&app, Some(FAR));
let [_, prefs, log] = windows(first)[..] else { panic!("{} windows", windows(first).len()) };
let step = -framed(0.0, 0.0, unsafe { GetDpiForWindow(first) }, Some([WS_OVERLAPPEDWINDOW, 0])).top;
assert!(step > 0);
let steps = |count: i32| POINT { x: FAR.x + count * step, y: FAR.y + count * step };
assert_eq!((corner_of(prefs), corner_of(log)), (steps(1), steps(2)));
unsafe { DestroyWindow(first) };
let app = Two::new();
let first = two(&app, Some(FAR));
unsafe { SetWindowPos(first, 0, steps(2).x, steps(2).y, 0, 0, 0x0015) };
app.borrow_mut().log.open = true;
unsafe { window_proc(first, WM_WAKE, 0, 0) };
assert_eq!(windows(first).iter().map(|hwnd| corner_of(*hwnd)).collect::<Vec<_>>(), [steps(2), steps(1), steps(3)]);
unsafe { DestroyWindow(first) };
let app = Two::new();
app.borrow_mut().prefs.open = false;
let first = two(&app, None);
let mut screen = MONITORINFO { size: size_of::<MONITORINFO>() as u32, all: RECT::default(), work: RECT::default(), flags: 0 };
unsafe {
assert!(GetMonitorInfoW(MonitorFromWindow(first, 0), &mut screen) != 0, "the system put the first window on no screen");
SetWindowPos(first, 0, screen.work.right - 10, screen.work.bottom - 10, 0, 0, 0x0015);
assert!(GetMonitorInfoW(MonitorFromWindow(first, 0), &mut screen) != 0);
}
app.borrow_mut().prefs.open = true;
unsafe { window_proc(first, WM_WAKE, 0, 0) };
let (second, work) = (outside(windows(first)[1]), screen.work);
assert!(work.left <= second.left && second.right <= work.right && work.top <= second.top && second.bottom <= work.bottom, "{second:?} is not on {work:?}");
unsafe { DestroyWindow(first) };
}
#[test]
fn each_window_has_its_own_scale_and_its_own_smallest_size() {
let app = Two::new();
app.borrow_mut().log.open = true;
let first = two(&app, Some(FAR));
let [_, prefs, log] = windows(first)[..] else { panic!("{} windows", windows(first).len()) };
let was = kept(first).scale;
let to = RECT { left: FAR.x, top: FAR.y + 1000, right: FAR.x + 480, bottom: FAR.y + 1360 };
unsafe { window_proc(prefs, WM_DPICHANGED, 144 | 144 << 16, &raw const to as isize) };
let (wide, high) = inside(prefs);
assert_eq!((outside(prefs), kept(prefs).scale, kept(first).scale, kept(log).scale), (to, 1.5, was, was));
let units = Size { w: wide as f32 / 1.5, h: high as f32 / 1.5 };
assert_eq!(state(first).in_window(1, |ui, _| (ui.size(), ui.scale())), Some((units, 1.5)));
assert_eq!((app.borrow().prefs.width, app.borrow().prefs.height), (units.w, units.h));
let smallest = |hwnd: HANDLE| {
let none = POINT::default();
let mut limits = MINMAXINFO { reserved: none, max_size: none, max_position: none, min_track: none, max_track: none };
unsafe { window_proc(hwnd, WM_GETMINMAXINFO, 0, &raw mut limits as isize) };
limits.min_track
};
let around = |w: f32, h: f32, dpi: u32| {
let frame = framed(w, h, dpi, Some([WS_OVERLAPPEDWINDOW, 0]));
POINT { x: frame.right - frame.left, y: frame.bottom - frame.top }
};
assert_eq!((smallest(prefs), smallest(log)), (around(200.0, 120.0, 144), around(150.0, 90.0, (was * 96.0) as u32)));
unsafe { DestroyWindow(first) };
}
#[test]
fn a_second_window_is_typed_into_by_itself() {
keys_up();
let app = Two::new();
let first = two(&app, None);
let second = windows(first)[1];
assert_eq!((state(first).ui.window_focused, state(first).in_window(1, |ui, _| ui.window_focused)), (false, Some(false)));
unsafe {
window_proc(second, WM_SETFOCUS, 0, 0);
window_proc(second, WM_KEYDOWN, 9, 0);
window_proc(second, WM_KEYDOWN, 9, 0);
window_proc(second, WM_CHAR, 'm' as usize, 0);
}
assert_eq!((app.borrow().text.as_str(), state(first).ui.window_focused), ("m", false));
assert!(state(first).timer, "its cursor blinks by the one timer, which is the first window's");
let cursor = state(first).in_window(1, |ui, _| ui.ime_area()).unwrap().expect("the text box has the keyboard");
let spot = cursor_pixels(cursor, kept(second).scale);
assert!(app.borrow().typed.contains(pt(cursor.x, cursor.y)));
let mut told = COMPOSITIONFORM { style: 0, at: POINT::default(), area: RECT::default() };
unsafe {
window_proc(second, WM_IME_STARTCOMPOSITION, 0, 0);
let context = ImmGetContext(second);
assert!(context != 0, "this thread has no input context to read back from");
ImmGetCompositionWindow(context, &mut told);
ImmReleaseContext(second, context);
}
assert_eq!((told.style, told.at, kept(second).ime_spot, kept(first).ime_spot), (2, POINT { x: spot.left, y: spot.top }, spot, RECT::default()));
unsafe { DestroyWindow(first) };
}
#[test]
fn files_are_let_go_on_the_window_whose_app_asks_for_them() {
let app = Two::new();
app.borrow_mut().asks = true;
let first = two(&app, None);
let second = windows(first)[1];
let (names, kept_as, mut effect) = (["C:\\one.txt", "C:\\two\\𝄞.txt"], wide("OleDropTargetInterface"), ANY);
let (data_table, files) = (Dragged::table(), in_memory(&block((0, 0), false, true, &names)));
let mut dragged = Dragged::new(&data_table, files);
unsafe {
assert_eq!((kept(first).files, kept(second).files, GetPropW(first, kept_as.as_ptr())), (false, true, 0));
let target = GetPropW(second, kept_as.as_ptr()) as *mut DropTarget;
assert!(!target.is_null() && (*target).hwnd == second);
let (calls, at) = (table(target), on_screen(second, middle(app.borrow().typed)));
assert_eq!(((calls.enter)(target, &raw mut dragged.object, 0, at, &mut effect), effect), (0, COPY));
assert_eq!(((calls.let_go)(target, &raw mut dragged.object, 0, at, &mut effect), effect), (0, COPY));
assert_eq!((&app.borrow().got, dragged.held.get()), (&paths(&names), 1));
GlobalFree(files);
assert_eq!((calls.hold)(target), 2);
app.borrow_mut().prefs.open = false;
window_proc(first, WM_WAKE, 0, 0);
assert_eq!((IsWindow(second), (calls.release)(target), CoInitializeEx(std::ptr::null_mut(), 0)), (0, 0, 0x8001_0106_u32 as i32));
app.borrow_mut().asks_first = true;
window_proc(first, WM_WAKE, 0, 0);
assert_eq!((kept(first).files, GetPropW(first, kept_as.as_ptr()) != 0), (true, true));
DestroyWindow(first);
assert_eq!(CoInitializeEx(std::ptr::null_mut(), 0), 0);
CoUninitialize();
}
}
#[test]
fn files_may_close_the_window_they_are_let_go_on() {
let app = Two::new();
(app.borrow_mut().asks, app.borrow_mut().shuts) = (true, true);
let first = two(&app, None);
let second = windows(first)[1];
let (names, kept_as, mut effect) = (["C:\\one.txt"], wide("OleDropTargetInterface"), ANY);
let (data_table, files) = (Dragged::table(), in_memory(&block((0, 0), false, true, &names)));
let mut dragged = Dragged::new(&data_table, files);
unsafe {
let target = GetPropW(second, kept_as.as_ptr()) as *mut DropTarget;
let (calls, at) = (table(target), on_screen(second, middle(app.borrow().typed)));
assert_eq!(((calls.hold)(target), (calls.enter)(target, &raw mut dragged.object, 0, at, &mut effect)), (2, 0));
assert_eq!(((calls.let_go)(target, &raw mut dragged.object, 0, at, &mut effect), effect), (0, COPY));
assert_eq!((&app.borrow().got, IsWindow(second), windows(first), dragged.held.get()), (&paths(&names), 0, vec![first], 1));
assert_eq!(((calls.release)(target), CoInitializeEx(std::ptr::null_mut(), 0)), (0, 0x8001_0106_u32 as i32));
GlobalFree(files);
DestroyWindow(first);
assert_eq!(CoInitializeEx(std::ptr::null_mut(), 0), 0);
CoUninitialize();
}
}
fn region(of: &[[i32; 4]]) -> HANDLE {
unsafe {
let all = CreateRectRgn(0, 0, 0, 0);
for [left, top, right, bottom] in of {
let one = CreateRectRgn(*left, *top, *right, *bottom);
CombineRgn(all, all, one, 2);
DeleteObject(one);
}
all
}
}
#[test]
fn what_windows_wants_drawn_is_read_as_the_rectangles_it_is_made_of() {
let rect = |[left, top, right, bottom]: [i32; 4]| RECT { left, top, right, bottom };
let two = region(&[[10, 10, 55, 38], [10, 262, 82, 290]]);
let stale = Stale::of(two);
assert_eq!((stale.header[2], stale.parts[0], stale.parts[1]), (2, rect([10, 10, 55, 38]), rect([10, 262, 82, 290])));
assert_eq!(stale.around, rect([10, 10, 82, 290]));
let (none, many) = (region(&[]), region(&[0, 1, 2, 3, 4, 5, 6, 7, 8].map(|step| [step * 20, step * 20, step * 20 + 10, step * 20 + 10])));
assert_eq!((Stale::of(none).header[2], Stale::of(many).header[2]), (0, 0));
unsafe { (DeleteObject(two), DeleteObject(none), DeleteObject(many)) };
}
#[test]
fn two_changes_far_apart_are_two_places_for_windows_to_draw() {
let at = Rc::new(Cell::new([Rect::default(); 2]));
let seen = at.clone();
let describe = move |ui: &mut Ui| {
ui.column().grow().pad(10.0).align(crate::layout::Align::Start, crate::layout::Align::Start).open();
let top = ui.button("Top").rect;
ui.spacer();
seen.set([top, ui.button("Bottom").rect]);
ui.close();
};
let hwnd = open(Window::default(), Box::new(describe));
unsafe {
SetWindowPos(hwnd, 0, FAR.x, FAR.y, 0, 0, 0x0015);
window_proc(hwnd, WM_SIZE, 0, 0);
window_proc(hwnd, WM_SIZE, 0, 0);
SetWindowLongPtrW(hwnd, -20, GetWindowLongPtrW(hwnd, -20) | 0x80 | 0x0800_0000);
ShowWindow(hwnd, 4);
}
pump();
let over = |which: usize| {
let button = at.get()[which];
unsafe { window_proc(hwnd, WM_MOUSEMOVE, 0, place(hwnd, pt(button.x + button.w / 2.0, button.y + button.h / 2.0))) };
};
let pixels = |which: usize| cursor_pixels(at.get()[which], kept(hwnd).scale);
picture(hwnd);
assert_eq!(kept(hwnd).stale().header[2], 0);
over(0);
let stale = kept(hwnd).stale();
assert_eq!((stale.header[2], stale.parts[0]), (1, pixels(0)));
over(1);
let stale = kept(hwnd).stale();
assert_eq!((stale.header[2], stale.parts[0], stale.parts[1]), (2, pixels(0), pixels(1)));
unsafe { window_proc(hwnd, WM_PAINT, 0, 0) };
assert_eq!(kept(hwnd).stale().header[2], 0);
unsafe { DestroyWindow(hwnd) };
}
fn wanted(hwnd: HANDLE) -> Vec<RECT> {
let stale = kept(hwnd).stale();
let mut around = RECT::default();
unsafe { GetUpdateRect(hwnd, &mut around, 0) };
let parts = stale.parts.get(..stale.header[2] as usize).filter(|parts| !parts.is_empty()).unwrap_or(std::slice::from_ref(&around));
parts.iter().filter(|part| part.right > part.left && part.bottom > part.top).copied().collect()
}
#[test]
fn what_is_drawn_part_by_part_is_what_drawing_it_all_gives() {
const MARK: Icon = Icon::line("M1.8 4.3a1 1 0 0 1 1-1h3l1.4 1.5h6a1 1 0 0 1 1 1v6.4a1 1 0 0 1-1 1H2.8a1 1 0 0 1-1-1z");
const PLUS: Icon = Icon::line("M8 3.5v9M3.5 8h9");
let at = Rc::new(RefCell::new(Vec::new()));
let seen = at.clone();
let describe = move |ui: &mut Ui| {
let (t, mut seen) = (ui.theme, seen.borrow_mut());
seen.clear();
ui.column().grow().open();
ui.row().w(Length::Grow(1.0)).h(Length::Px(44.0)).pad((8.0, 0.0)).gap(6.0).fill(t.panel).open();
for name in ["All notes", "Open all", "Save all", "Send all"] {
let state = ui.press(ui.next_id());
let edge = if state.hovered { t.text_faint } else { t.line_strong };
ui.row().h(Length::Px(28.0)).pad((10.0, 0.0)).gap(6.0).fill(t.raised).border(edge, 1.0).radius(6.0).sense().focusable().open();
ui.el().icon(MARK, 16.0, t.text_dim).show();
ui.el().text(name, t.font, t.text).show();
ui.close();
ui.tooltip(&state, "Saves every note that has changed, one after the other, and says how each went");
seen.push(state.rect);
}
ui.close();
for row in 0..14 {
let state = ui.press(ui.next_id());
let fill = if state.hovered { t.hover } else { Color::NONE };
ui.row().w(Length::Grow(1.0)).h(Length::Px(30.0)).pad((8.0, 0.0)).gap(8.0).fill(fill).sense().focusable().open();
ui.el().icon(MARK, 16.0, t.text_dim).show();
ui.el().text(&format!("Note number {row}"), t.font.bold(), t.text).show();
ui.el().text("C:\\Notes\\with a long path to it", t.small_font, t.text_faint).show();
ui.spacer();
if state.hovered {
ui.el().icon(PLUS, 13.0, t.text_dim).show();
ui.el().icon(PLUS, 13.0, t.text).show();
}
ui.el().text("M", t.font.bold(), t.amber).show();
ui.close();
ui.tooltip(&state, "D:\\Code\\with a long path to it\\and what else there is to say about this row of the list");
seen.push(state.rect);
}
ui.close();
};
let hwnd = open(Window { width: 520.0, height: 480.0, ..Default::default() }, Box::new(describe));
unsafe {
SetWindowPos(hwnd, 0, FAR.x, FAR.y, 0, 0, 0x0015);
window_proc(hwnd, WM_SIZE, 0, 0);
window_proc(hwnd, WM_SIZE, 0, 0);
SetWindowLongPtrW(hwnd, -20, GetWindowLongPtrW(hwnd, -20) | 0x80 | 0x0800_0000);
ShowWindow(hwnd, 4);
}
pump();
kept(hwnd).tracking = true;
let (shown, fresh) = (Sheet::new(hwnd), Sheet::new(hwnd));
picture(hwnd);
shown.draw(hwnd, &[shown.all()]);
let step = |x: f32, y: f32, rest: u64| {
unsafe { window_proc(hwnd, WM_MOUSEMOVE, 0, place(hwnd, pt(x, y))) };
if rest > 0 {
std::thread::sleep(std::time::Duration::from_millis(rest));
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
}
shown.draw(hwnd, &wanted(hwnd));
unsafe { window_proc(hwnd, WM_PAINT, 0, 0) };
};
let differ = || {
fresh.draw(hwnd, &[fresh.all()]);
shown.pixels().iter().zip(&fresh.pixels()).filter(|(have, want)| have != want).count()
};
let places: Vec<Rect> = at.borrow().clone();
assert_eq!(differ(), 0);
let mut wrong = Vec::new();
for turn in 0..3 {
for which in (0..places.len()).map(|i| (i * 7 + turn * 3) % places.len()) {
let r = places[which];
step(r.x + r.w / 2.0, r.y + r.h / 2.0, 0);
}
wrong.push(("jumps", differ()));
}
for y in (40..470).step_by(3) {
step(120.0 + (y % 7) as f32, y as f32, 0);
}
wrong.push(("down", differ()));
for which in [1, 6, 2, 11, 17, 0] {
let r = places[which];
step(r.x + r.w / 2.0, r.y + r.h / 2.0, 0);
step(r.x + r.w / 2.0 + 1.0, r.y + r.h / 2.0, 560);
wrong.push(("rested", differ()));
step(r.x + r.w / 2.0 + 2.0, r.y + r.h / 2.0 + 1.0, 0);
}
step(-10.0, -10.0, 0);
unsafe { window_proc(hwnd, WM_MOUSELEAVE, 0, 0) };
shown.draw(hwnd, &wanted(hwnd));
wrong.push(("left", differ()));
unsafe { DestroyWindow(hwnd) };
assert!(wrong.iter().all(|(_, wrong)| *wrong == 0), "pixels that are not what a fresh drawing gives: {wrong:?}");
}
#[derive(Default)]
struct Form {
loud: bool,
level: f32,
name: String,
saved: u32,
more: bool,
choice: u8,
}
fn form(app: &Rc<RefCell<Form>>) -> HANDLE {
let app = app.clone();
let describe = move |ui: &mut Ui| {
let mut app = app.borrow_mut();
let app = &mut *app;
ui.label("Sound");
ui.checkbox(&mut app.loud, "Loud");
ui.slider(&mut app.level, 0.0..=10.0);
ui.text_box(&mut app.name, "Your name");
if ui.button("Save").clicked {
app.saved += 1;
ui.announce("Saved");
}
if ui.fold(&mut app.more, "More") {
ui.label("Under the fold");
}
ui.radio(&mut app.choice, 0, "One");
ui.radio(&mut app.choice, 1, "Two");
};
let hwnd = open(Window::default(), Box::new(describe));
unsafe { (window_proc(hwnd, WM_SIZE, 0, 0), window_proc(hwnd, WM_SIZE, 0, 0)) };
hwnd
}
const ROOT: isize = -25;
const TO_PRESS: i32 = 10000;
const TO_TYPE: i32 = 10002;
const TO_SLIDE: i32 = 10003;
const TO_FOLD: i32 = 10005;
const TO_PICK: i32 = 10010;
const TO_TICK: i32 = 10015;
fn told(hwnd: HANDLE) -> Vec<String> {
let (state, at) = unsafe { state_of(hwnd) }.unwrap();
state.in_window(at, |ui, _| ui.visible_access_nodes().iter().map(|said| said.name.clone()).collect()).unwrap()
}
#[test]
fn a_window_that_nobody_asks_keeps_no_list_for_screen_readers() {
let app = Rc::new(RefCell::new(Form::default()));
let hwnd = form(&app);
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
assert!(!state(hwnd).ui.listening && told(hwnd).is_empty());
unsafe { window_proc(hwnd, WM_GETOBJECT, 0, -12) };
assert!(!state(hwnd).ui.listening && told(hwnd).is_empty());
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn a_window_that_is_asked_what_is_in_it_keeps_the_list_from_then_on() {
let app = Two::new();
let first = two(&app, None);
let second = windows(first)[1];
unsafe {
assert!(window_proc(second, WM_GETOBJECT, 0, ROOT) != 0 && state(first).ui.listening);
assert_eq!((told(first), told(second)), (vec!["In the first window".to_string()], vec!["Dark".to_string(), "Type here".to_string()]));
assert_eq!(CoInitializeEx(std::ptr::null_mut(), 0), 0x8001_0106_u32 as i32);
assert!(window_proc(second, WM_GETOBJECT, 0, ROOT) != 0 && window_proc(first, WM_GETOBJECT, 0, ROOT) != 0);
app.borrow_mut().prefs.open = false;
window_proc(first, WM_WAKE, 0, 0);
assert_eq!((IsWindow(second), CoInitializeEx(std::ptr::null_mut(), 0)), (0, 0x8001_0106_u32 as i32));
DestroyWindow(first);
assert_eq!(CoInitializeEx(std::ptr::null_mut(), 0), 0);
CoUninitialize();
}
}
#[test]
fn where_windows_could_not_keep_to_the_thread_it_is_handed_no_provider() {
unsafe {
assert_eq!(CoInitializeEx(std::ptr::null_mut(), 0), 0);
let app = Rc::new(RefCell::new(Form::default()));
let hwnd = form(&app);
assert_eq!((window_proc(hwnd, WM_GETOBJECT, 0, ROOT), state(hwnd).ui.listening), (0, false));
DestroyWindow(hwnd);
CoUninitialize();
}
}
#[test]
fn a_screen_reader_brings_what_is_out_of_view_into_view() {
let describe = |ui: &mut Ui| {
ui.column().h(crate::Length::Px(100.0)).scroll().open();
for row in 0..40 {
ui.button(&format!("Row {row}"));
}
ui.close();
};
let hwnd = open(Window::default(), Box::new(describe));
unsafe { (window_proc(hwnd, WM_SIZE, 0, 0), window_proc(hwnd, WM_SIZE, 0, 0), window_proc(hwnd, WM_GETOBJECT, 0, ROOT)) };
let shown = |name: &str| state(hwnd).ui.access_nodes().iter().find(|said| said.name == name).map(|said| said.visible);
assert_eq!((shown("Row 0"), shown("Row 30")), (Some(true), Some(false)));
assert_eq!(uia::tests::act(hwnd, "Row 30", 10017, None, None), 0);
assert_eq!((shown("Row 0"), shown("Row 30")), (Some(false), Some(true)));
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn a_screen_reader_selects_part_of_what_is_typed_and_the_box_has_it() {
let app = Rc::new(RefCell::new(Form::default()));
let hwnd = form(&app);
unsafe { window_proc(hwnd, WM_GETOBJECT, 0, ROOT) };
assert_eq!(uia::tests::act(hwnd, "Your name", TO_TYPE, Some("Ada Lovelace"), None), 0);
let caret = || state(hwnd).ui.visible_access_nodes().iter().find(|said| said.name == "Your name").and_then(|said| said.caret);
assert_eq!((app.borrow().name.clone(), caret()), ("Ada Lovelace".to_string(), Some([12, 12])));
assert_eq!((uia::tests::select(hwnd, "Your name", [4, 8]), caret()), (0, Some([4, 8])));
unsafe { window_proc(hwnd, WM_CHAR, 'x' as usize, 0) };
assert_eq!((app.borrow().name.clone(), caret()), ("Ada xlace".to_string(), Some([5, 5])));
assert_eq!((uia::tests::select(hwnd, "Your name", [1, 1]), caret()), (0, Some([1, 1])));
assert_eq!((uia::tests::select(hwnd, "Your name", [3, 99]), caret()), (0, Some([3, 9])));
assert_eq!(uia::tests::act(hwnd, "Loud", TO_TICK, None, None), 0);
assert_eq!(caret(), None);
assert_eq!((uia::tests::select(hwnd, "Your name", [0, 3]), caret()), (0, Some([0, 3])));
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn what_a_screen_reader_asks_of_a_thing_is_done_in_its_window() {
let app = Rc::new(RefCell::new(Form { level: 4.0, ..Form::default() }));
let hwnd = form(&app);
unsafe { window_proc(hwnd, WM_GETOBJECT, 0, ROOT) };
let act = |name: &str, pattern: i32| uia::tests::act(hwnd, name, pattern, None, None);
let stands = || {
let app = app.borrow();
(app.loud, app.level, app.name.clone(), app.more, app.choice, app.saved)
};
assert_eq!((act("Loud", TO_TICK), stands()), (0, (true, 4.0, String::new(), false, 0, 0)));
assert_eq!((uia::tests::act(hwnd, "Your name", TO_TYPE, Some("Ada"), None), stands()), (0, (true, 4.0, "Ada".into(), false, 0, 0)));
assert_eq!((uia::tests::act(hwnd, "", TO_SLIDE, None, Some(6.0)), stands()), (0, (true, 6.0, "Ada".into(), false, 0, 0)));
assert_eq!((act("More", TO_FOLD), stands()), (0, (true, 6.0, "Ada".into(), true, 0, 0)));
assert_eq!((act("Two", TO_PICK), act("Save", TO_PRESS), stands()), (0, 0, (true, 6.0, "Ada".into(), true, 1, 1)));
assert_eq!((state(hwnd).ui.announced(), uia::announce(hwnd, &state(hwnd).ui)), (&["Saved".to_string()][..], 0));
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
assert!(state(hwnd).ui.announced().is_empty());
assert_eq!((act("More", TO_FOLD), act("Two", TO_PICK), uia::tests::act(hwnd, "", TO_SLIDE, None, Some(7.2)), stands()), (0, 0, 0, (true, 7.2, "Ada".into(), true, 1, 1)));
assert_eq!((uia::tests::act(hwnd, "", TO_SLIDE, None, Some(10.5)), app.borrow().level), (0x8007_0057_u32 as i32, 7.2));
assert_eq!((uia::tests::act(hwnd, "", TO_SLIDE, None, Some(0.0)), app.borrow().level), (0, 0.0));
assert_eq!(told(hwnd), ["Sound", "Loud", "", "Your name", "Save", "More", "Under the fold", "One", "Two"]);
assert!(state(hwnd).ui.visible_access_nodes()[2].focused);
ANSWERING.set(true);
assert_eq!((act("Save", TO_PRESS), app.borrow().saved), (0x8004_0200_u32 as i32, 1));
ANSWERING.set(false);
assert_eq!((act("Save", TO_PRESS), app.borrow().saved), (0, 2));
unsafe { DestroyWindow(hwnd) };
let app = Two::new();
let first = two(&app, None);
let second = windows(first)[1];
unsafe { (window_proc(first, WM_GETOBJECT, 0, ROOT), window_proc(second, WM_GETOBJECT, 0, ROOT)) };
assert_eq!((uia::tests::act(second, "Dark", TO_TICK, None, None), app.borrow().dark, app.borrow().pressed), (0, true, 0));
assert_eq!((uia::tests::act(first, "In the first window", TO_PRESS, None, None), app.borrow().dark, app.borrow().pressed), (0, true, 1));
assert_eq!((uia::tests::act(second, "Type here", TO_TYPE, Some("m"), None), app.borrow().text.as_str()), (0, "m"));
unsafe { DestroyWindow(first) };
}
const READER: &str = r#"
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName UIAutomationClient, UIAutomationTypes
$uia = 'System.Windows.Automation.'
$element = [type]($uia + 'AutomationElement')
$walker = ([type]($uia + 'TreeWalker'))::RawViewWalker
$window = $element::FromHandle([IntPtr]WINDOW)
function Way($thing, $kind) { $way = $null; if ($thing.TryGetCurrentPattern(([type]($uia + $kind + 'Pattern'))::Pattern, [ref]$way)) { $way } }
function Tell($thing, $indent) {
$is = $thing.Current
$line = $indent + $is.ControlType.ProgrammaticName.Substring(12) + ' [' + $is.Name + ']'
$way = Way $thing 'Toggle'; if ($way) { $line += ' ' + $way.Current.ToggleState }
$way = Way $thing 'SelectionItem'; if ($way) { $line += ' picked: ' + $way.Current.IsSelected }
$way = Way $thing 'ExpandCollapse'; if ($way) { $line += ' ' + $way.Current.ExpandCollapseState }
$way = Way $thing 'Value'; if ($way) { $line += ' holds [' + $way.Current.Value + ']' }
$way = Way $thing 'RangeValue'; if ($way) { $line += ' at ' + $way.Current.Value + ' of ' + $way.Current.Minimum + ' to ' + $way.Current.Maximum }
if (Way $thing 'Invoke') { $line += ' to press' }
if ($is.HasKeyboardFocus) { $line += ' <- keyboard' }
$line
$inside = $walker.GetFirstChild($thing)
while ($inside) { Tell $inside ($indent + ' '); $inside = $walker.GetNextSibling($inside) }
}
function All { $inside = $walker.GetFirstChild($window); while ($inside) { Tell $inside ''; $inside = $walker.GetNextSibling($inside) } }
function Find($property, $is) { $window.FindFirst('Descendants', (New-Object ($uia + 'PropertyCondition') $property, $is)) }
function Named($name) { Find $element::NameProperty $name }
"#;
fn reader(hwnd: HANDLE, script: &str) -> (std::process::ChildStdin, std::sync::mpsc::Receiver<String>) {
use std::io::BufRead;
use std::process::Stdio;
let script = format!("{READER}{script}").replace("WINDOW", &hwnd.to_string());
let mut reader = std::process::Command::new("powershell.exe");
reader.args(["-NoProfile", "-NonInteractive", "-Command", &script]).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::inherit());
let mut reader = reader.spawn().expect("Windows PowerShell, to read the window with");
let (written, lines) = (reader.stdout.take().unwrap(), std::sync::mpsc::channel());
let to_reader = reader.stdin.take().unwrap();
std::thread::spawn(move || {
std::io::BufReader::new(written).lines().map_while(Result::ok).for_each(|line| _ = lines.0.send(line));
_ = reader.wait();
});
(to_reader, lines.1)
}
fn read(lines: &std::sync::mpsc::Receiver<String>, last: &str) -> String {
let (mut read, started) = (Vec::new(), Instant::now());
loop {
pump();
match lines.try_recv() {
Ok(line) if line.starts_with(last) => return read.join("\n"),
Ok(line) => read.push(line),
Err(std::sync::mpsc::TryRecvError::Disconnected) => return read.join("\n"),
Err(_) => assert!(started.elapsed().as_secs() < 120, "the reader does not come to an end"),
}
std::thread::sleep(std::time::Duration::from_millis(1));
}
}
const AT_FIRST: &str = "Text [Sound]
CheckBox [Loud] Off
Slider [] at 4 of 0 to 10
Edit [Your name] holds []
Button [Save] to press
Button [More] Collapsed
RadioButton [One] picked: True
RadioButton [Two] picked: False";
#[test]
fn a_program_that_reads_windows_out_is_told_what_is_in_the_window() {
use std::io::Write;
let app = Rc::new(RefCell::new(Form { level: 4.0, ..Form::default() }));
let hwnd = form(&app);
let script = "All; $kept = Named 'Save'; 'kept [' + $kept.Current.Name + '] to press: ' + [bool](Way $kept 'Invoke'); 'waiting'; $null = [Console]::In.ReadLine()
try { $name = $kept.Current.Name; $press = [bool](Way $kept 'Invoke') } catch { $name = ''; $press = $false }
'kept [' + $name + '] to press: ' + $press";
let (mut to_reader, lines) = reader(hwnd, script);
assert_eq!(read(&lines, "waiting"), format!("{AT_FIRST}\nkept [Save] to press: True"));
assert!(state(hwnd).ui.listening);
unsafe { DestroyWindow(hwnd) };
writeln!(to_reader).unwrap();
assert_eq!(read(&lines, "the end"), "kept [] to press: False");
}
#[test]
fn a_program_that_reads_windows_out_works_the_window() {
on_a_desktop_of_its_own("windows::tests::on_a_desktop_nobody_sees_a_reader_works_the_window");
}
#[test]
#[ignore = "run by the test before it, in a process of its own that is started on a desktop of its own"]
fn on_a_desktop_nobody_sees_a_reader_works_the_window() {
if std::env::var_os(ALONE).is_none() {
return;
}
assert!(is_on_its_own_desktop(), "this is the desktop the person works on: nothing is worked on it");
let app = Rc::new(RefCell::new(Form { level: 4.0, ..Form::default() }));
let hwnd = form(&app);
let script = "
(Way (Named 'Loud') 'Toggle').Toggle()
(Way (Named 'Your name') 'Value').SetValue('Ada')
(Way (Find $element::ControlTypeProperty ([type]($uia + 'ControlType'))::Slider) 'RangeValue').SetValue(6)
(Way (Named 'More') 'ExpandCollapse').Expand()
(Way (Named 'Two') 'SelectionItem').Select()
(Way (Named 'Save') 'Invoke').Invoke()
All";
let (_to_reader, lines) = reader(hwnd, script);
let after = "Text [Sound]
CheckBox [Loud] On
Slider [] at 6 of 0 to 10
Edit [Your name] holds [Ada]
Button [Save] to press <- keyboard
Button [More] Expanded
Text [Under the fold]
RadioButton [One] picked: False
RadioButton [Two] picked: True";
assert_eq!(read(&lines, "the end"), after);
let stands = (app.borrow().loud, app.borrow().level, app.borrow().name.clone(), app.borrow().more, app.borrow().choice, app.borrow().saved);
assert_eq!(stands, (true, 6.0, "Ada".to_string(), true, 1, 1));
unsafe { DestroyWindow(hwnd) };
}
#[derive(Default)]
struct Kinds {
strip: Rect,
button: Rect,
no_strip: bool,
minimize: bool,
forward: bool,
fill: Option<bool>,
over_all: Option<bool>,
fills: bool,
whole: bool,
}
fn of_a_kind(asked: Window, app: &Rc<RefCell<Kinds>>) -> HANDLE {
let app = app.clone();
let describe = move |ui: &mut Ui| {
let mut app = app.borrow_mut();
let strip = ui.row().h(Length::Px(30.0)).sense().open();
ui.label("A title");
ui.spacer();
app.button = ui.button("Close").rect;
ui.close();
if !app.no_strip {
ui.set_title_bar(&strip);
}
if std::mem::take(&mut app.minimize) {
ui.minimize();
}
if std::mem::take(&mut app.forward) {
ui.bring_to_front();
}
app.fill.take().into_iter().for_each(|on| ui.set_maximized(on));
app.over_all.take().into_iter().for_each(|on| ui.set_full_screen(on));
(app.strip, app.fills, app.whole) = (strip.rect, ui.maximized(), ui.full_screen());
ui.label("The page");
};
let hwnd = open(asked, Box::new(describe));
unsafe { (window_proc(hwnd, WM_SIZE, 0, 0), window_proc(hwnd, WM_SIZE, 0, 0)) };
hwnd
}
fn lies_at(hwnd: HANDLE, at: Point) -> isize {
let place = on_screen(hwnd, at);
unsafe { window_proc(hwnd, WM_NCHITTEST, 0, (place.y as u16 as isize) << 16 | place.x as u16 as isize) }
}
fn inside_on_screen(hwnd: HANDLE) -> RECT {
let ((wide, high), mut corner) = (inside(hwnd), POINT::default());
unsafe { ClientToScreen(hwnd, &mut corner) };
RECT { left: corner.x, top: corner.y, right: corner.x + wide, bottom: corner.y + high }
}
fn frame_of(hwnd: HANDLE) -> u32 {
unsafe { GetWindowLongPtrW(hwnd, -16) as u32 & WS_OVERLAPPEDWINDOW }
}
#[test]
fn a_window_with_no_frame_is_all_inside_and_says_what_lies_at_each_place() {
let app = Rc::new(RefCell::new(Kinds::default()));
let hwnd = of_a_kind(Window { width: 300.0, height: 200.0, frameless: true, ..Window::default() }, &app);
let (scale, all) = (kept(hwnd).scale, outside(hwnd));
assert_eq!((inside(hwnd), inside_on_screen(hwnd)), (((300.0 * scale) as i32, (200.0 * scale) as i32), all));
assert_eq!(frame_of(hwnd), WS_OVERLAPPEDWINDOW);
let (button, on_strip) = (app.borrow().button, pt(150.0, 15.0));
assert_eq!([lies_at(hwnd, on_strip), lies_at(hwnd, middle(button)), lies_at(hwnd, pt(150.0, 100.0))], [2, 1, 1]);
let edges = [(0.0, 100.0, 10), (299.0, 100.0, 11), (150.0, 0.0, 12), (0.0, 0.0, 13), (299.0, 0.0, 14), (150.0, 199.0, 15), (0.0, 199.0, 16), (299.0, 199.0, 17)];
for (x, y, part) in edges {
assert_eq!(lies_at(hwnd, pt(x, y)), part, "at {x}, {y}");
}
let mut limits = MINMAXINFO { reserved: POINT::default(), max_size: POINT::default(), max_position: POINT::default(), min_track: POINT::default(), max_track: POINT::default() };
unsafe { window_proc(hwnd, WM_GETMINMAXINFO, 0, &raw mut limits as isize) };
assert_eq!(limits.min_track, POINT { x: (200.0 * scale) as i32, y: (120.0 * scale) as i32 });
app.borrow_mut().no_strip = true;
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
assert_eq!([lies_at(hwnd, on_strip), lies_at(hwnd, pt(0.0, 100.0))], [1, 10]);
unsafe { DestroyWindow(hwnd) };
let app = Rc::new(RefCell::new(Kinds::default()));
let hwnd = of_a_kind(Window { width: 300.0, height: 200.0, ..Window::default() }, &app);
let (within, all) = (inside_on_screen(hwnd), outside(hwnd));
assert!(within.top > all.top && within.left > all.left && frame_of(hwnd) == WS_OVERLAPPEDWINDOW, "{within:?} in {all:?}");
assert_eq!([lies_at(hwnd, on_strip), lies_at(hwnd, pt(0.0, 100.0)), lies_at(hwnd, pt(150.0, 100.0)), lies_at(hwnd, pt(150.0, -4.0))], [2, 1, 1, 2]);
app.borrow_mut().no_strip = true;
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
assert!(!kept(hwnd).asks_hit_test && lies_at(hwnd, on_strip) == 1);
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn a_window_is_minimized_fills_its_screen_and_lies_over_all_of_it_when_its_app_asks() {
on_a_desktop_of_its_own("windows::tests::on_a_desktop_nobody_sees_a_window_stands_as_its_app_asks");
}
#[test]
#[ignore = "run by the test before it, in a process of its own that is started on a desktop of its own"]
fn on_a_desktop_nobody_sees_a_window_stands_as_its_app_asks() {
if std::env::var_os(ALONE).is_none() {
return;
}
assert!(is_on_its_own_desktop(), "this is the desktop the person works on: no window is shown on it");
[true, false].into_iter().for_each(stands_as_asked);
}
fn stands_as_asked(bare: bool) {
let app = Rc::new(RefCell::new(Kinds::default()));
let hwnd = of_a_kind(Window { position: Some((120, 90)), width: 400.0, height: 240.0, frameless: bare, ..Window::default() }, &app);
unsafe { ShowWindow(hwnd, 1) };
pump();
let ui = || &state(hwnd).ui;
let asks = |ask: &dyn Fn(&mut Kinds)| {
ask(&mut app.borrow_mut());
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
pump();
};
let (zoomed, iconic) = (|| unsafe { IsZoomed(hwnd) } != 0, || unsafe { IsIconic(hwnd) } != 0);
let stands = || (app.borrow().fills, app.borrow().whole, ui().maximized(), ui().full_screen());
let mut screen = MONITORINFO { size: size_of::<MONITORINFO>() as u32, all: RECT::default(), work: RECT::default(), flags: 0 };
assert!(unsafe { GetMonitorInfoW(MonitorFromWindow(hwnd, 2), &mut screen) } != 0);
let (there, small, within, on_strip) = (WindowPlacement { position: (120, 90), width: 400.0, height: 240.0, maximized: false }, outside(hwnd), inside_on_screen(hwnd), pt(200.0, 15.0));
assert_eq!((ui().window_placement(), stands(), bare, lies_at(hwnd, on_strip)), (Some(there), (false, false, false, false), within == small, 2));
asks(&|app| app.minimize = true);
assert!(iconic() && ui().window_minimized());
asks(&|app| app.forward = true);
assert!(!iconic() && !ui().window_minimized() && outside(hwnd) == small);
asks(&|app| app.fill = Some(true));
assert_eq!((zoomed(), stands(), ui().window_placement()), (true, (true, false, true, false), Some(WindowPlacement { maximized: true, ..there })));
let filled = (outside(hwnd), inside_on_screen(hwnd));
assert!(!bare || (filled.1 == screen.work && filled.0.left < screen.work.left && filled.0.bottom > screen.work.bottom), "{filled:?} on {:?}", screen.work);
assert_eq!([lies_at(hwnd, pt(1.0, 1.0)), lies_at(hwnd, on_strip)], [2, 2]);
unsafe { window_proc(hwnd, 0x00A3, 2, 0) };
pump();
assert_eq!((zoomed(), stands(), ui().window_placement(), outside(hwnd)), (false, (false, false, false, false), Some(there), small));
unsafe { window_proc(hwnd, 0x00A3, 2, 0) };
pump();
assert_eq!((zoomed(), stands(), outside(hwnd)), (true, (true, false, true, false), filled.0));
asks(&|app| app.fill = Some(false));
assert_eq!((zoomed(), stands(), ui().window_placement(), outside(hwnd)), (false, (false, false, false, false), Some(there), small));
asks(&|app| app.over_all = Some(true));
assert_eq!((outside(hwnd), inside_on_screen(hwnd), frame_of(hwnd)), (screen.all, screen.all, 0));
assert_eq!((stands(), ui().window_placement(), [lies_at(hwnd, on_strip), lies_at(hwnd, pt(0.0, 0.0))]), ((false, true, false, true), Some(there), [1, 1]));
asks(&|app| app.fill = Some(true));
assert_eq!((zoomed(), outside(hwnd)), (false, screen.all));
asks(&|app| app.over_all = Some(false));
assert_eq!((outside(hwnd), inside_on_screen(hwnd), frame_of(hwnd)), (small, within, WS_OVERLAPPEDWINDOW));
assert_eq!((stands(), ui().window_placement(), lies_at(hwnd, on_strip)), ((false, false, false, false), Some(there), 2));
asks(&|app| app.fill = Some(true));
asks(&|app| app.over_all = Some(true));
assert_eq!((outside(hwnd), inside_on_screen(hwnd), stands(), ui().window_placement()), (screen.all, screen.all, (true, true, true, true), Some(WindowPlacement { maximized: true, ..there })));
asks(&|app| app.over_all = Some(false));
assert_eq!((zoomed(), outside(hwnd), inside_on_screen(hwnd), stands()), (true, filled.0, filled.1, (true, false, true, false)));
asks(&|app| app.over_all = Some(true));
asks(&|app| app.minimize = true);
assert!(iconic() && ui().window_minimized() && ui().full_screen());
asks(&|app| app.over_all = Some(false));
assert!(iconic() && !ui().full_screen());
asks(&|app| app.forward = true);
assert_eq!((iconic(), zoomed(), outside(hwnd), frame_of(hwnd)), (false, true, filled.0, WS_OVERLAPPEDWINDOW));
asks(&|app| app.fill = Some(false));
assert_eq!((outside(hwnd), ui().window_placement()), (small, Some(there)));
unsafe { DestroyWindow(hwnd) };
}
fn pictured(hwnd: HANDLE) -> (Vec<u32>, usize) {
let clear = kept(hwnd).transparent.as_ref().expect("a see-through window");
let [wide, high] = clear.size.map(|side| side as usize);
(unsafe { std::slice::from_raw_parts(clear.pixels, wide * high) }.to_vec(), wide)
}
fn afresh(hwnd: HANDLE) -> Vec<u32> {
state(hwnd).in_window(0, |ui, _| crate::Surface::draw(ui, 64)).unwrap().pixels
}
fn see_through(asked: Window, ticked: &Rc<Cell<bool>>) -> HANDLE {
let ticked = ticked.clone();
let describe = move |ui: &mut Ui| {
let mut on = ticked.get();
ui.el().size(40.0, 40.0).fill(Color::rgb(0xff0000)).show();
ui.el().size(40.0, 40.0).fill(Color::rgb(0x0000ff).with_alpha(0.5)).show();
ui.label("Written");
ui.checkbox(&mut on, "Ticked");
};
let hwnd = open(Window { width: 200.0, height: 160.0, transparent: true, ..asked }, Box::new(describe));
unsafe { (window_proc(hwnd, WM_SIZE, 0, 0), window_proc(hwnd, WM_SIZE, 0, 0)) };
hwnd
}
#[test]
fn a_see_through_window_is_shown_from_a_picture_that_is_clear_where_nothing_is_drawn() {
let ticked = Rc::new(Cell::new(false));
let hwnd = see_through(Window::default(), &ticked);
let (scale, all) = (kept(hwnd).scale, outside(hwnd));
assert!(unsafe { GetWindowLongPtrW(hwnd, -20) } & 0x0008_0000 != 0 && unsafe { GetWindowLongPtrW(hwnd, -16) } as u32 & (WS_PLAIN | WS_OVERLAPPEDWINDOW) == WS_PLAIN);
assert_eq!((inside(hwnd), inside_on_screen(hwnd), lies_at(hwnd, pt(0.0, 80.0))), (((200.0 * scale) as i32, (160.0 * scale) as i32), all, 1));
let ((pixels, wide), px) = (pictured(hwnd), |unit: f32| (unit * scale) as usize);
let at = |x: f32, y: f32| pixels[px(y) * wide + px(x)];
assert_eq!((pixels.len(), wide), ((all.right - all.left) as usize * (all.bottom - all.top) as usize, (all.right - all.left) as usize));
assert_eq!((at(20.0, 20.0), at(150.0, 20.0), at(150.0, 150.0)), (0xffff_0000, 0, 0));
let half = at(20.0, 60.0);
assert!((0x7f..=0x80).contains(&(half >> 24)) && half & 0xff == half >> 24 && half & 0x00ff_ff00 == 0, "{half:08x}");
let solid = |pixel: &u32| pixel >> 24;
assert!(pixels.iter().all(|pixel| [16, 8, 0].iter().all(|shift| pixel >> shift & 0xff <= solid(pixel))), "a colour not multiplied by how solid it is");
let row = px(90.0) * wide..px(100.0) * wide;
assert!(pixels[row].iter().any(|pixel| (1..255).contains(&solid(pixel))), "no letters in the picture");
assert!(pixels == afresh(hwnd));
ticked.set(true);
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
let (ticked_pixels, _) = pictured(hwnd);
assert!(ticked_pixels != pixels && ticked_pixels == afresh(hwnd));
let whole = RECT { left: 0, top: 0, right: all.right - all.left, bottom: all.bottom - all.top };
assert!(kept(hwnd).transparent.as_ref().is_some_and(|clear| clear.show(hwnd, whole)));
unsafe { SetWindowPos(hwnd, 0, 0, 0, whole.right + 30, whole.bottom + 20, 0x0016) };
pump();
let (larger, wider) = pictured(hwnd);
assert_eq!((wider, larger.len()), (wide + 30, (wide + 30) * (whole.bottom as usize + 20)));
assert!(larger == afresh(hwnd));
unsafe { DestroyWindow(hwnd) };
let hwnd = see_through(Window { frameless: true, ..Window::default() }, &ticked);
assert_eq!((frame_of(hwnd), lies_at(hwnd, pt(0.0, 80.0)), inside_on_screen(hwnd)), (WS_OVERLAPPEDWINDOW, 10, outside(hwnd)));
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn a_second_window_has_no_frame_where_its_app_asks_for_that() {
let kept_by_app = Rc::new(RefCell::new((SecondWindow::new(200.0, 120.0), false)));
let app = kept_by_app.clone();
let describe = move |ui: &mut Ui| {
let (tools, close) = &mut *app.borrow_mut();
ui.label("The first window");
if ui.frameless_window(tools, "Tools") {
let strip = ui.row().h(Length::Px(24.0)).sense().open();
ui.label("Tools");
ui.close();
ui.set_title_bar(&strip);
if *close {
ui.quit();
}
ui.close();
}
};
let first = open(Window::default(), Box::new(describe));
unsafe { (window_proc(first, WM_SIZE, 0, 0), window_proc(first, WM_SIZE, 0, 0)) };
let [_, second] = windows(first)[..] else { panic!("{} windows", windows(first).len()) };
let scale = kept(second).scale;
assert_eq!((inside(second), inside_on_screen(second)), (((200.0 * scale) as i32, (120.0 * scale) as i32), outside(second)));
assert_eq!([lies_at(second, pt(100.0, 12.0)), lies_at(second, pt(100.0, 60.0)), lies_at(second, pt(199.0, 60.0)), lies_at(second, pt(199.0, 119.0))], [2, 1, 11, 17]);
assert!(inside_on_screen(first) != outside(first) && !kept(first).asks_hit_test);
kept_by_app.borrow_mut().1 = true;
unsafe { window_proc(first, WM_WAKE, 0, 0) };
pump();
assert_eq!((kept_by_app.borrow().0.open, windows(first), unsafe { IsWindow(second) }), (false, vec![first], 0));
unsafe { DestroyWindow(first) };
}
#[test]
fn what_is_clear_of_a_see_through_window_lets_the_pointer_through() {
on_a_desktop_of_its_own("windows::tests::on_a_desktop_nobody_sees_a_see_through_window_lets_the_pointer_through");
}
#[test]
#[ignore = "run by the test before it, in a process of its own that is started on a desktop of its own"]
fn on_a_desktop_nobody_sees_a_see_through_window_lets_the_pointer_through() {
if std::env::var_os(ALONE).is_none() {
return;
}
assert!(is_on_its_own_desktop(), "this is the desktop the person works on: no window is shown on it");
let under = of_a_kind(Window { position: Some((100, 100)), width: 500.0, height: 400.0, ..Window::default() }, &Rc::new(RefCell::new(Kinds::default())));
let hwnd = see_through(Window { position: Some((200, 200)), ..Window::default() }, &Rc::new(Cell::new(false)));
unsafe { (ShowWindow(under, 1), ShowWindow(hwnd, 1)) };
pump();
let scale = kept(hwnd).scale;
assert!(unsafe { IsWindowVisible(hwnd) } != 0 && corner_of(hwnd) == POINT { x: 200, y: 200 });
let over = |x: f32, y: f32| unsafe { WindowFromPoint(POINT { x: 200 + (x * scale) as i32, y: 200 + (y * scale) as i32 }) };
assert_eq!([over(20.0, 20.0), over(20.0, 60.0), over(150.0, 20.0), over(150.0, 150.0)], [hwnd, hwnd, under, under]);
unsafe { (DestroyWindow(hwnd), DestroyWindow(under)) };
}
#[link(name = "user32")]
unsafe extern "system" {
fn GetWindow(hwnd: HANDLE, which: u32) -> HANDLE;
fn IsWindowEnabled(hwnd: HANDLE) -> i32;
fn MonitorFromPoint(at: POINT, otherwise: u32) -> HANDLE;
fn ShutdownBlockReasonQuery(hwnd: HANDLE, why: *mut u16, room: *mut u32) -> i32;
}
#[link(name = "kernel32")]
unsafe extern "system" {
fn K32GetProcessMemoryInfo(process: HANDLE, counters: *mut [usize; 10], size: u32) -> i32;
}
#[derive(Default)]
struct Owned {
windows: Vec<(SecondWindow, &'static str)>,
unsaved: bool,
asked: Vec<&'static str>,
runs: u32,
}
fn owned(asked: Window, app: &Rc<RefCell<Owned>>) -> HANDLE {
let app = app.clone();
let describe = move |ui: &mut Ui| {
let mut app = app.borrow_mut();
let app = &mut *app;
app.runs += 1;
let mut hear = |ui: &mut Ui, name: &'static str| {
if ui.close_requested() && !app.asked.contains(&name) {
app.asked.push(name);
}
if app.unsaved {
ui.cancel_close("There are unsaved changes");
}
};
hear(ui, "first");
ui.label("The first window");
for (state, title) in app.windows.iter_mut() {
if ui.window(state, title) {
hear(ui, title);
ui.label(title);
ui.close();
}
}
};
let hwnd = open(asked, Box::new(describe));
unsafe { (window_proc(hwnd, WM_SIZE, 0, 0), window_proc(hwnd, WM_SIZE, 0, 0)) };
hwnd
}
fn screen_at(found: &[Screen], x: f32, y: f32) -> Screen {
let monitor = unsafe { MonitorFromPoint(POINT { x: x as i32, y: y as i32 }, 2) };
let mut info = MONITORINFO { size: size_of::<MONITORINFO>() as u32, all: RECT::default(), work: RECT::default(), flags: 0 };
unsafe { GetMonitorInfoW(monitor, &mut info) };
*found.iter().find(|screen| screen.rect.x as i32 == info.all.left && screen.rect.y as i32 == info.all.top).expect("the screen is in the list")
}
#[test]
fn the_screens_are_the_ones_windows_has_and_a_window_hears_when_they_change() {
let found = screens();
eprintln!("{found:#?}");
assert_eq!((found.len(), found[0].rect.x, found[0].rect.y), (unsafe { GetSystemMetrics(80) } as usize, 0.0, 0.0));
for screen in &found {
let (monitor, mut dpi) = (unsafe { MonitorFromPoint(POINT { x: (screen.rect.x + 1.0) as i32, y: (screen.rect.y + 1.0) as i32 }, 0) }, [0u32; 2]);
unsafe { GetDpiForMonitor(monitor, 0, &mut dpi[0], &mut dpi[1]) };
assert!(screen.rect.intersect(screen.work_area) == screen.work_area && !screen.work_area.is_empty(), "{screen:?}");
assert_eq!(screen.scale, dpi[0] as f32 / 96.0);
}
let app = Rc::new(RefCell::new(Owned::default()));
let first = owned(Window::default(), &app);
assert_eq!(state(first).ui.screens(), &found[..]);
let runs = app.borrow().runs;
unsafe { window_proc(first, WM_DISPLAYCHANGE, 32, 0) };
assert_eq!(app.borrow().runs, runs);
for (message, what) in [(WM_DISPLAYCHANGE, 32), (WM_SETTINGCHANGE, 0x2F)] {
state(first).ui.set_screens(Vec::new());
unsafe { window_proc(first, message, what, 0) };
assert!(state(first).ui.screens() == &found[..] && app.borrow().runs > runs, "{message:x}");
}
unsafe { DestroyWindow(first) };
}
#[test]
fn a_window_opens_in_the_middle_of_the_screen_its_app_names_at_that_screens_scale() {
for (nth, screen) in screens().into_iter().enumerate() {
let app = Rc::new(RefCell::new(Owned::default()));
let first = owned(Window { screen: Some(nth), width: 400.0, height: 240.0, ..Window::default() }, &app);
let (area, work) = (outside(first), screen.work_area);
let off = |from: i32, to: i32, start: f32, long: f32| ((from + to) as f32 / 2.0 - (start + long / 2.0)).abs();
assert!(off(area.left, area.right, work.x, work.w) <= 1.0 && off(area.top, area.bottom, work.y, work.h) <= 1.0, "{area:?} is not in the middle of {work:?}");
assert_eq!((kept(first).scale, state(first).ui.screen(), state(first).ui.size()), (screen.scale, Some(nth), Size { w: 400.0, h: 240.0 }));
unsafe { DestroyWindow(first) };
}
let first = owned(Window { screen: Some(99), ..Window::default() }, &Rc::new(RefCell::new(Owned::default())));
assert_eq!(state(first).ui.screen(), Some(0));
unsafe { DestroyWindow(first) };
}
#[test]
fn a_window_opens_where_it_was_on_the_screen_it_was_on_or_else_on_the_nearest() {
let found = screens();
for (nth, screen) in found.iter().enumerate() {
let at = ((screen.work_area.x + 40.0) as i32, (screen.work_area.y + 30.0) as i32);
let first = owned(Window { position: Some(at), width: 400.0, height: 240.0, ..Window::default() }, &Rc::new(RefCell::new(Owned::default())));
assert_eq!((corner_of(first), kept(first).scale, state(first).ui.screen()), (POINT { x: at.0, y: at.1 }, screen.scale, Some(nth)));
unsafe { DestroyWindow(first) };
}
let right = found.iter().map(|screen| screen.rect.right()).fold(f32::MIN, f32::max);
let bottom = found.iter().map(|screen| screen.rect.bottom()).fold(f32::MIN, f32::max);
for at in [(right as i32 + 5000, 200), (300, bottom as i32 + 5000)] {
let first = owned(Window { position: Some(at), ..Window::default() }, &Rc::new(RefCell::new(Owned::default())));
let (area, nth) = (outside(first), crate::screens::nearest(&found, rect(at.0 as f32, at.1 as f32, 160.0, 30.0)).unwrap());
let work = found[nth].work_area;
assert!(area.left >= work.x as i32 && area.top >= work.y as i32 && area.right <= work.right() as i32 && area.bottom <= work.bottom() as i32, "{area:?} is not in {work:?}");
assert_eq!((state(first).ui.screen(), kept(first).scale), (Some(nth), found[nth].scale));
assert_eq!(screen_at(&found, (area.left + 50) as f32, (area.top + 50) as f32), found[nth]);
unsafe { DestroyWindow(first) };
}
}
#[test]
fn second_windows_of_each_kind_stand_to_the_first_as_people_expect() {
let app = Rc::new(RefCell::new(Owned::default()));
app.borrow_mut().windows = vec![
(SecondWindow::new(300.0, 200.0), "Plain"),
(SecondWindow::new(160.0, 300.0).tool(), "Tools"),
(SecondWindow::new(200.0, 100.0).on_top(), "Clock"),
(SecondWindow { open: false, ..SecondWindow::new(320.0, 140.0).dialog() }, "Save?"),
];
let first = owned(Window { position: Some((200, 150)), width: 700.0, height: 500.0, ..Window::default() }, &app);
let [_, plain, tools, clock] = windows(first)[..] else { panic!("{} windows", windows(first).len()) };
let owner = |hwnd: HANDLE| unsafe { GetWindow(hwnd, 4) };
let (style, more) = (|hwnd: HANDLE| unsafe { GetWindowLongPtrW(hwnd, -16) } as u32, |hwnd: HANDLE| unsafe { GetWindowLongPtrW(hwnd, -20) } as u32);
assert_eq!((owner(plain), more(plain) & 0x89), (0, 0));
assert_eq!((owner(tools), more(tools) & 0x80, style(tools) & 0x0003_0000), (first, 0x80, 0));
let (area, (wide, high)) = (outside(tools), inside(tools));
let frame = framed(wide as f32 / kept(tools).scale, high as f32 / kept(tools).scale, unsafe { GetDpiForWindow(tools) }, styles_of(tools));
assert_eq!((area.right - area.left, area.bottom - area.top), (frame.right - frame.left, frame.bottom - frame.top));
assert_eq!((owner(clock), more(clock) & 8), (0, 8));
let enabled = || windows(first).iter().map(|hwnd| unsafe { IsWindowEnabled(*hwnd) } != 0).collect::<Vec<_>>();
assert_eq!(enabled(), [true; 4]);
app.borrow_mut().windows[3].0.open = true;
unsafe { window_proc(first, WM_WAKE, 0, 0) };
let dialog = windows(first)[4];
assert_eq!((owner(dialog), more(dialog) & 1, style(dialog) & 0x0003_0000), (first, 1, 0));
let (over, under) = (outside(dialog), outside(first));
assert!((over.left + over.right - under.left - under.right).abs() <= 1 && (over.top + over.bottom - under.top - under.bottom).abs() <= 1, "{over:?} is not in the middle of {under:?}");
assert_eq!(enabled(), [false, false, false, false, true]);
app.borrow_mut().windows[3].0.open = false;
unsafe { window_proc(first, WM_WAKE, 0, 0) };
assert_eq!((windows(first).len(), enabled()), (4, vec![true; 4]));
unsafe { DestroyWindow(first) };
}
fn reason(hwnd: HANDLE) -> Option<String> {
let (mut text, mut room) = ([0u16; 256], 256u32);
let said = unsafe { ShutdownBlockReasonQuery(hwnd, text.as_mut_ptr(), &mut room) } != 0;
said.then(|| String::from_utf16_lossy(&text[..room.min(256) as usize]).trim_end_matches('\0').into())
}
#[test]
fn the_app_is_asked_before_a_window_closes_and_before_the_system_logs_off() {
let app = Rc::new(RefCell::new(Owned::default()));
app.borrow_mut().windows = vec![(SecondWindow::new(300.0, 200.0), "Notes")];
let first = owned(Window::default(), &app);
let notes = windows(first)[1];
app.borrow_mut().unsaved = true;
unsafe { (window_proc(notes, WM_CLOSE, 0, 0), window_proc(first, WM_CLOSE, 0, 0)) };
assert_eq!((app.borrow().asked.clone(), unsafe { (IsWindow(first), IsWindow(notes)) }), (vec!["Notes", "first"], (1, 1)));
app.borrow_mut().asked.clear();
assert_eq!(unsafe { window_proc(first, WM_QUERYENDSESSION, 0, 0) }, 0);
assert_eq!((app.borrow().asked.clone(), reason(first)), (vec!["first"], Some("There are unsaved changes".into())));
app.borrow_mut().unsaved = false;
assert_eq!((unsafe { window_proc(first, WM_QUERYENDSESSION, 0, 0) }, reason(first)), (1, None));
unsafe { window_proc(first, WM_ENDSESSION, 0, 0) };
unsafe { window_proc(notes, WM_CLOSE, 0, 0) };
assert_eq!((unsafe { IsWindow(notes) }, app.borrow().windows[0].0.open), (0, false));
unsafe { window_proc(first, WM_CLOSE, 0, 0) };
assert_eq!((unsafe { IsWindow(first) }, pump()), (0, true));
}
#[test]
fn a_window_moved_onto_a_screen_of_another_scale_keeps_its_size_in_units() {
let app = Rc::new(RefCell::new(Owned::default()));
app.borrow_mut().windows = vec![(SecondWindow::new(300.0, 200.0).tool(), "Tools")];
let first = owned(Window { width: 400.0, height: 240.0, frameless: true, ..Window::default() }, &app);
for (at, hwnd) in windows(first).into_iter().enumerate() {
for dpi in [144usize, 192, 120, 96] {
let units = state(first).in_window(at, |ui, _| ui.size()).unwrap();
let mut big = POINT::default();
assert_eq!(unsafe { window_proc(hwnd, WM_GETDPISCALEDSIZE, dpi, &raw mut big as isize) }, 1);
let frame = framed(units.w, units.h, dpi as u32, styles_of(hwnd));
assert_eq!(big, POINT { x: frame.right - frame.left, y: frame.bottom - frame.top });
let to = RECT { left: FAR.x, top: FAR.y, right: FAR.x + big.x, bottom: FAR.y + big.y };
unsafe { window_proc(hwnd, WM_DPICHANGED, dpi | dpi << 16, &raw const to as isize) };
let (scale, (wide, high)) = (dpi as f32 / 96.0, inside(hwnd));
let size = if styles_of(hwnd).is_none() { units } else { Size { w: wide as f32 / scale, h: high as f32 / scale } };
assert_eq!((outside(hwnd), state(first).in_window(at, |ui, _| (ui.size(), ui.scale()))), (to, Some((size, scale))), "{dpi}");
}
}
unsafe { DestroyWindow(first) };
}
#[test]
#[ignore = "a measurement: run it by itself, so that no other test's windows are counted"]
fn how_long_ten_second_windows_take_and_what_they_hold() {
let held = || {
let mut counters = [0usize; 10];
counters[0] = size_of::<[usize; 10]>();
unsafe { K32GetProcessMemoryInfo(GetCurrentProcess(), &mut counters, size_of::<[usize; 10]>() as u32) };
let taken = |which: u32| unsafe { GetGuiResources(GetCurrentProcess(), which) } as usize;
[counters[9], taken(1), taken(0)]
};
let titles = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
let app = Rc::new(RefCell::new(Owned::default()));
app.borrow_mut().windows = titles.iter().map(|title| (SecondWindow { open: false, ..SecondWindow::new(400.0, 300.0) }, *title)).collect();
let first = owned(Window::default(), &app);
let timed = |rounds: u32| {
let started = Instant::now();
(0..rounds).for_each(|_| _ = unsafe { window_proc(first, WM_WAKE, 0, 0) });
started.elapsed() / rounds
};
let open = |on: bool| {
app.borrow_mut().windows.iter_mut().for_each(|(state, _)| state.open = on);
unsafe { window_proc(first, WM_WAKE, 0, 0) };
};
let none = held();
open(true);
let ten = held();
open(false);
let alone = timed(500);
open(true);
let busy = timed(500);
open(false);
let (after, closed) = (held(), timed(500));
let each = |was: [usize; 3], now: [usize; 3]| now.map(|n| n as f64).iter().zip(was).map(|(now, was)| (now - was as f64) / 10.0).collect::<Vec<_>>();
eprintln!("a run with no second window: {alone:?}; with ten: {busy:?}; with the ten closed again: {closed:?}");
eprintln!("held with none: {none:?}; with ten: {ten:?}; after: {after:?}; an interface is {} bytes", size_of::<Ui>());
eprintln!("each window (bytes of memory, windows and the like, things to draw with): {:?}", each(none, ten));
assert_eq!(windows(first).len(), 1);
assert!(after[1] <= none[1] && after[2] <= none[2], "closed windows left something behind: {none:?} {after:?}");
unsafe { DestroyWindow(first) };
}
#[test]
fn text_dragged_over_a_window_whose_app_takes_text_reaches_it() {
let seen = Rc::new(RefCell::new((false, None::<String>, Rect::EMPTY)));
let described = seen.clone();
let app = move |ui: &mut Ui| {
let mut seen = described.borrow_mut();
let on = ui.button("Let text go here");
seen.0 = ui.text_hovered();
seen.1 = seen.1.take().or(ui.text_dropped(&on));
seen.2 = on.rect;
};
let hwnd = open(Window::default(), Box::new(app));
unsafe { (window_proc(hwnd, WM_SIZE, 0, 0), window_proc(hwnd, WM_SIZE, 0, 0)) };
assert!(state(hwnd).ole == Some(true) && kept(hwnd).files, "a window that takes text takes it the way it takes files");
let (data_table, files) = (Dragged::table(), in_memory(&block((0, 0), false, true, &["C:\\one.txt"])));
let (mut text, mut file) = (Dragged::of_text(&data_table, "words, ünïcode 𝄞"), Dragged::new(&data_table, files));
let (text, file): (*mut IDataObject, *mut IDataObject) = (&raw mut text.object, &raw mut file.object);
let (target, at) = (DropTarget::new(hwnd), on_screen(hwnd, middle(seen.borrow().2)));
let (calls, mut effect) = (table(target), ANY);
unsafe {
assert_eq!(((calls.enter)(target, file, 0, at, &mut effect), effect, seen.borrow().0), (0, 0, false));
(calls.leave)(target);
effect = ANY;
assert_eq!(((calls.enter)(target, text, 0, at, &mut effect), effect, seen.borrow().0), (0, COPY, true));
assert_eq!(((calls.let_go)(target, text, 0, at, &mut effect), effect), (0, COPY));
assert_eq!((seen.borrow().0, seen.borrow().1.as_deref()), (false, Some("words, ünïcode 𝄞")));
(effect, seen.borrow_mut().1) = (ANY, None);
(calls.enter)(target, text, 0, at, &mut effect);
assert_eq!(((calls.let_go)(target, text, 0, on_screen(hwnd, pt(500.0, 400.0)), &mut effect), effect, seen.borrow().1.clone()), (0, 0, None));
assert_eq!((calls.release)(target), 0);
GlobalFree(files);
DestroyWindow(hwnd);
}
}
#[test]
fn text_handed_over_ends_at_its_first_zero_or_its_block() {
let block = |units: &[u16]| in_memory(&units.iter().flat_map(|unit| unit.to_le_bytes()).collect::<Vec<u8>>());
let (whole, cut, odd) = (block(&[0x77, 0xfc, 0, 0x78]), block(&[0x77, 0xd834]), in_memory(&[0x77, 0, 0x78]));
assert_eq!(unsafe { [dropped_text(whole), dropped_text(cut), dropped_text(odd), dropped_text(0)] }, ["wü", "w\u{fffd}", "w", ""]);
unsafe { [whole, cut, odd].into_iter().for_each(|block| _ = GlobalFree(block)) };
}
#[derive(Default)]
struct Out {
what: Option<DragData<'static>>,
from: Rect,
on: Rect,
taken: Vec<DropEffect>,
dropped: Option<u64>,
takes: SecondWindow,
took: Rect,
files: Vec<PathBuf>,
text: Option<String>,
}
fn carrier(app: &Rc<RefCell<Out>>, corner: Option<(i32, i32)>) -> HANDLE {
let app = app.clone();
let describe = move |ui: &mut Ui| {
let mut app = app.borrow_mut();
let app = &mut *app;
ui.column().pad(10.0).gap(10.0).open();
let from = ui.button("Carry me");
if ui.drag_source(&from, 7) {
ui.label("seven");
ui.close();
}
app.taken.extend(app.what.and_then(|what| ui.drag_out(&from, what)));
let on = ui.button("Let go here");
app.dropped = app.dropped.or(ui.dropped(&on));
_ = ui.files_hovered();
(app.from, app.on) = (from.rect, on.rect);
ui.close();
if ui.window(&mut app.takes, "Takes") {
let took = ui.button("Let go in here");
app.files.extend(ui.files_dropped(&took).into_iter().flatten());
app.text = app.text.take().or(ui.text_dropped(&took));
app.took = took.rect;
ui.close();
}
};
let hwnd = open(Window { position: corner, width: 300.0, height: 200.0, ..Window::default() }, Box::new(describe));
unsafe { (window_proc(hwnd, WM_SIZE, 0, 0), window_proc(hwnd, WM_SIZE, 0, 0)) };
hwnd
}
fn take_out(hwnd: HANDLE, app: &Rc<RefCell<Out>>, to: Point) {
let at = middle(app.borrow().from);
unsafe {
window_proc(hwnd, WM_MOUSEMOVE, 0, place(hwnd, at));
window_proc(hwnd, WM_LBUTTONDOWN, 1, place(hwnd, at));
window_proc(hwnd, WM_MOUSEMOVE, 1, place(hwnd, pt(at.x + 10.0, at.y)));
window_proc(hwnd, WM_MOUSEMOVE, 1, place(hwnd, to));
}
}
thread_local! {
static SAW: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
static OVER: Cell<(HANDLE, POINT)> = const { Cell::new((0, POINT { x: 0, y: 0 })) };
}
const LET_GO: i32 = 0x0004_0100;
const GIVEN_UP: i32 = 0x0004_0101;
unsafe fn function<F: Copy>(data: *mut IDataObject, at: usize) -> F {
unsafe { std::mem::transmute_copy(&*(*data.cast::<*const usize>()).add(at)) }
}
unsafe fn moved_by_a_file_manager(data: *mut crate::ole_drag::Carried, source: *mut crate::ole_drag::Source, allowed: u32, effect: *mut u32) -> i32 {
let (data, mut handed) = (data.cast::<IDataObject>(), STGMEDIUM { medium: 0, memory: 0, owner: std::ptr::null_mut() });
unsafe {
assert_eq!((((*(*data).table).query_get_data)(data, &FILES), ((*(*data).table).get_data)(data, &FILES, &mut handed)), (0, 0));
SAW.with_borrow_mut(|saw| saw.extend(dropped_files(handed.memory).1.iter().map(|file| file.display().to_string())));
ReleaseStgMedium(&mut handed);
assert_eq!((crate::ole_drag::go_on(source, false, 1), crate::ole_drag::go_on(source, false, 0), allowed), (0, LET_GO, 7));
let set_data: unsafe extern "system" fn(*mut IDataObject, *const FORMATETC, *mut STGMEDIUM, i32) -> i32 = function(data, 7);
let said = FORMATETC { format: RegisterClipboardFormatW(wide("Logical Performed DropEffect").as_ptr()) as u16, ..FILES };
let mut moved = STGMEDIUM { medium: 1, memory: in_memory(&2u32.to_le_bytes()), owner: std::ptr::null_mut() };
assert_eq!(set_data(data, &said, &mut moved, 1), 0);
*effect = 0;
}
LET_GO
}
unsafe fn given_up(_: *mut crate::ole_drag::Carried, source: *mut crate::ole_drag::Source, _: u32, _: *mut u32) -> i32 {
assert_eq!(crate::ole_drag::go_on(source, true, 1), GIVEN_UP);
GIVEN_UP
}
unsafe fn let_go_over_a_window(data: *mut crate::ole_drag::Carried, source: *mut crate::ole_drag::Source, allowed: u32, effect: *mut u32) -> i32 {
let ((hwnd, at), data) = (OVER.get(), data.cast::<IDataObject>());
unsafe {
let target = GetPropW(hwnd, wide("OleDropTargetInterface").as_ptr()) as *mut DropTarget;
let (calls, mut may) = (table(target), allowed);
(calls.enter)(target, data, 1, at, &mut may);
(calls.over)(target, 1, at, &mut may);
SAW.with_borrow_mut(|saw| saw.push(format!("may: {may}")));
assert_eq!(crate::ole_drag::go_on(source, false, 0), LET_GO);
*effect = 0;
if may == 0 {
(calls.leave)(target);
return LET_GO;
}
may = allowed;
(calls.let_go)(target, data, 0, at, &mut may);
*effect = may;
}
LET_GO
}
#[test]
fn files_carried_out_of_a_window_go_to_windows_between_messages_and_its_app_hears_they_were_moved() {
let paths: &'static [PathBuf] = Box::leak(Box::new([PathBuf::from("C:\\a\\one.txt"), PathBuf::from("D:\\zwei drei\\für.png")]));
let app = Rc::new(RefCell::new(Out { what: Some(DragData::Files(paths)), ..Out::default() }));
let hwnd = carrier(&app, None);
take_out(hwnd, &app, pt(320.0, 50.0));
assert_eq!(OUTGOING.get().map(|(from, ..)| from), Some(hwnd));
assert!(!state(hwnd).ui.drag_leaves_window(), "it is out already");
let was = crate::ole_drag::CARRY.replace(moved_by_a_file_manager);
carry_out_asked();
crate::ole_drag::CARRY.set(was);
assert!(OUTGOING.get().is_none());
assert_eq!(SAW.take(), ["C:\\a\\one.txt", "D:\\zwei drei\\für.png"]);
assert_eq!((app.borrow().taken.clone(), app.borrow().dropped), (vec![DropEffect::Moved], None));
unsafe { window_proc(hwnd, WM_WAKE, 0, 0) };
assert_eq!(app.borrow().taken, [DropEffect::Moved]);
assert!(state(hwnd).ui.drag_payload().is_none() && !state(hwnd).ui.wants_pointer() && unsafe { GetCapture() } != hwnd);
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn what_is_given_up_with_escape_is_let_go_nowhere() {
let app = Rc::new(RefCell::new(Out { what: Some(DragData::Text("words")), ..Out::default() }));
let hwnd = carrier(&app, None);
take_out(hwnd, &app, pt(150.0, 250.0));
let was = crate::ole_drag::CARRY.replace(given_up);
carry_out_asked();
crate::ole_drag::CARRY.set(was);
assert_eq!((app.borrow().taken.clone(), app.borrow().dropped), (vec![], None));
assert!(state(hwnd).ui.drag_payload().is_none() && unsafe { GetCapture() } != hwnd);
take_out(hwnd, &app, pt(150.0, 250.0));
carry_out_asked();
assert_eq!((app.borrow().taken.clone(), app.borrow().dropped, state(hwnd).ui.drag_payload()), (vec![], None, None));
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn what_one_window_carries_out_another_window_of_the_program_takes() {
let app = Rc::new(RefCell::new(Out { what: Some(DragData::Text("über alles")), takes: SecondWindow::new(200.0, 120.0), ..Out::default() }));
let first = carrier(&app, None);
let [_, second] = windows(first)[..] else { panic!("{} windows", windows(first).len()) };
assert!(state(first).ole == Some(true) && kept(second).files, "the second window takes files and text");
take_out(first, &app, pt(320.0, 50.0));
OVER.set((second, on_screen(second, middle(app.borrow().took))));
let was = crate::ole_drag::CARRY.replace(let_go_over_a_window);
carry_out_asked();
assert_eq!((SAW.take(), app.borrow().text.clone(), app.borrow().taken.clone()), (vec!["may: 1".to_string()], Some("über alles".into()), vec![DropEffect::Copied]));
app.borrow_mut().taken.clear();
take_out(first, &app, pt(320.0, 50.0));
OVER.set((first, on_screen(first, middle(app.borrow().on))));
carry_out_asked();
crate::ole_drag::CARRY.set(was);
assert_eq!((SAW.take(), app.borrow().taken.clone(), app.borrow().dropped), (vec!["may: 0".to_string()], vec![], None));
let paths: &'static [PathBuf] = Box::leak(Box::new([PathBuf::from("C:\\a\\one.txt")]));
app.borrow_mut().what = Some(DragData::Files(paths));
take_out(first, &app, pt(320.0, 50.0));
OVER.set((second, on_screen(second, middle(app.borrow().took))));
let was = crate::ole_drag::CARRY.replace(let_go_over_a_window);
carry_out_asked();
crate::ole_drag::CARRY.set(was);
assert_eq!((SAW.take(), app.borrow().files.clone(), app.borrow().taken.clone()), (vec!["may: 1".to_string()], paths.to_vec(), vec![DropEffect::Copied]));
unsafe { DestroyWindow(first) };
}
#[test]
fn what_is_carried_out_comes_back_over_its_window_and_goes_out_over_a_window_lying_on_it() {
on_a_desktop_of_its_own("windows::tests::on_a_desktop_nobody_sees_what_is_carried_out_come_back_and_go_out_over_a_window_on_it");
}
unsafe fn home_again(_: *mut crate::ole_drag::Carried, source: *mut crate::ole_drag::Source, _: u32, effect: *mut u32) -> i32 {
let (hwnd, at) = OVER.get();
let off = POINT { x: at.x, y: at.y + 2000 };
unsafe {
for place in [off, at] {
let looks = (*source).looks_at(place);
SAW.with_borrow_mut(|saw| saw.push(format!("{looks:x} {:?} {:?}", state(hwnd).ui.pointer(), state(hwnd).ui.drag_payload())));
}
assert_eq!(crate::ole_drag::go_on(source, false, 0), LET_GO);
*effect = 0;
}
LET_GO
}
#[test]
#[ignore = "run by the test before it, in a process of its own that is started on a desktop of its own"]
fn on_a_desktop_nobody_sees_what_is_carried_out_come_back_and_go_out_over_a_window_on_it() {
if std::env::var_os(ALONE).is_none() {
return;
}
assert!(is_on_its_own_desktop(), "this is the desktop the person works on: no window is shown on it");
let app = Rc::new(RefCell::new(Out { what: Some(DragData::Text("words")), takes: SecondWindow::new(200.0, 120.0).position(300, 150), ..Out::default() }));
let first = carrier(&app, Some((100, 100)));
let [_, second] = windows(first)[..] else { panic!("{} windows", windows(first).len()) };
unsafe { (ShowWindow(first, 1), ShowWindow(second, 1)) };
pump();
let scale = kept(first).scale;
let (mut corner, beside) = (POINT::default(), pt(20.0, 150.0));
unsafe { ClientToScreen(first, &mut corner) };
let covered = pt((320 - corner.x) as f32 / scale, (200 - corner.y) as f32 / scale);
assert_eq!(unsafe { (WindowFromPoint(POINT { x: 320, y: 200 }), WindowFromPoint(on_screen(first, beside))) }, (second, first));
take_out(first, &app, beside);
assert!(OUTGOING.get().is_none() && state(first).ui.drag_payload() == Some(7) && state(first).ui.drag_leaves_window());
unsafe { window_proc(first, WM_MOUSEMOVE, 1, place(first, covered)) };
assert_eq!(OUTGOING.get().map(|(from, ..)| from), Some(first));
let on = on_screen(first, middle(app.borrow().on));
OVER.set((first, on));
let was = crate::ole_drag::CARRY.replace(home_again);
carry_out_asked();
crate::ole_drag::CARRY.set(was);
let at = middle(app.borrow().on);
assert_eq!(SAW.take(), ["40102 None Some(7)".to_string(), format!("0 Some({at:?}) Some(7)")]);
assert_eq!((app.borrow().dropped, app.borrow().taken.clone()), (Some(7), vec![]));
assert!(state(first).ui.drag_payload().is_none() && unsafe { GetCapture() } != first);
unsafe { DestroyWindow(first) };
}
#[test]
fn a_see_through_window_with_no_picture_draws_nothing() {
let mut ui = Ui::new(crate::platform::system_text(), Box::new(crate::platform::LocalClipboard::default()));
let mut clear = SeeThrough::new();
clear.draw(&mut ui, rect(0.0, 0.0, 10.0, 10.0), 1.0);
assert!(clear.pixels.is_null());
let app = |ui: &mut Ui| _ = ui.el().size(10.0, 10.0).fill(Color::BLACK).show();
let hwnd = open(Window { transparent: true, width: 0.0, height: 0.0, ..Default::default() }, Box::new(app));
assert!(hwnd != 0 && unsafe { IsWindowVisible(hwnd) } == 0);
unsafe { (window_proc(hwnd, WM_WAKE, 0, 0), window_proc(hwnd, WM_WAKE, 0, 0)) };
unsafe { DestroyWindow(hwnd) };
}
#[test]
fn a_waker_wakes_no_window_once_the_window_is_gone() {
let hwnd = open(Window::default(), Box::new(|ui: &mut Ui| _ = ui.label("Woken")));
unsafe { DestroyWindow(hwnd) };
assert_ne!(WINDOW.load(std::sync::atomic::Ordering::Relaxed), hwnd);
Waker.wake();
}
}