Skip to content

armgit / WacOS

Public Imported from GitHub
0

A new style desktop shell for Windows 11: menu bar, Dock, Finder, Spotlight, Control Center and Liquid Glass, in native C++.

Rust
9.3 KB
Raw Blame
//! Porte de src\comum\ampliacao.h: o botão verde (maximizar com a animação do Shell), o menu do verde e a
//! animação de abrir/fechar feita pelo Shell. Sem o Shell aberto, tudo cai no comportamento normal do Windows.

use std::ffi::c_void;

use windows::core::PCWSTR;
use windows::Win32::Foundation::*;
use windows::Win32::Graphics::Dwm::*;
use windows::Win32::Graphics::Gdi::*;
use windows::Win32::System::DataExchange::COPYDATASTRUCT;
use windows::Win32::UI::HiDpi::GetDpiForWindow;
use windows::Win32::UI::Input::KeyboardAndMouse::*;
use windows::Win32::UI::Shell::*;
use windows::Win32::UI::WindowsAndMessaging::*;

use crate::{find_window, makelparam, pc, reg_msg, wz};

const T_SAFETY: usize = 0x4D4B;
const T_PHOTO: usize = 0x4D4C;
const SUBCLASS: usize = 0x414D504C;
const T_VERDE: usize = 0x4D4F;
const SUB_ABRIR: usize = 0x41425249;
const T_ABRIR: usize = 0x4D4D;
const T_ABRIR_SEG: usize = 0x4D4E;

fn prop(h: HWND, nome: &str) -> bool {
    let t = wz(nome);
    unsafe { !GetPropW(h, pc(&t)).0.is_null() }
}
fn set_prop(h: HWND, nome: &str) {
    let t = wz(nome);
    unsafe {
        let _ = SetPropW(h, pc(&t), HANDLE(1 as *mut c_void));
    }
}
fn remove_prop(h: HWND, nome: &str) -> bool {
    let t = wz(nome);
    unsafe { RemovePropW(h, pc(&t)).map(|v| !v.0.is_null()).unwrap_or(false) }
}
unsafe fn cloak(h: HWND, on: bool) -> bool {
    let v = BOOL::from(on);
    DwmSetWindowAttribute(h, DWMWA_CLOAK, &v as *const _ as *const c_void, 4).is_ok()
}
fn shell() -> Option<HWND> {
    find_window("ShellMensagens")
}

pub fn visivel(h: HWND) -> RECT {
    let mut r = RECT::default();
    unsafe {
        if DwmGetWindowAttribute(h, DWMWA_EXTENDED_FRAME_BOUNDS, &mut r as *mut _ as *mut c_void, std::mem::size_of::<RECT>() as u32).is_err() {
            let _ = GetWindowRect(h, &mut r);
        }
    }
    r
}

pub fn mostrar(h: HWND) {
    unsafe {
        let _ = KillTimer(h, T_SAFETY);
        cloak(h, false);
        let _ = RedrawWindow(h, None, None, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
        let _ = SetWindowPos(h, None, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
        if let Some(s) = shell() {
            let _ = PostMessageW(s, reg_msg("ShellAmpliarPronto"), WPARAM(h.0 as usize), LPARAM(0));
        }
    }
}

extern "system" fn seguranca(h: HWND, _: u32, _: usize, _: u32) {
    mostrar(h);
}
extern "system" fn foto(h: HWND, _: u32, id: usize, _: u32) {
    unsafe {
        let _ = KillTimer(h, id);
        if let Some(s) = shell() {
            let _ = PostMessageW(s, reg_msg("ShellAmpliarFoto"), WPARAM(h.0 as usize), LPARAM(0));
        }
    }
}
extern "system" fn sub(h: HWND, m: u32, w: WPARAM, l: LPARAM, _: usize, _: usize) -> LRESULT {
    let msg = reg_msg("MacJanelaMostrar");
    if m == msg && msg != 0 {
        mostrar(h);
        return LRESULT(1);
    }
    unsafe { DefSubclassProc(h, m, w, l) }
}

extern "system" fn verde_tick(h: HWND, _: u32, id: usize, _: u32) {
    unsafe {
        let _ = KillTimer(h, id);
        if !remove_prop(h, "MacVerdeEspera") || GetAsyncKeyState(VK_LBUTTON.0 as i32) < 0 {
            return;
        }
        let mut c = POINT::default();
        let _ = GetCursorPos(&mut c);
        let mut p = c;
        let _ = ScreenToClient(h, &mut p);
        let mut k = GetDpiForWindow(h) as f32 / 96.0;
        if k <= 0.0 {
            k = 1.0;
        }
        if (p.x as f32) < 54.0 * k || (p.x as f32) > 84.0 * k || p.y < 0 || (p.y as f32) > 52.0 * k {
            return;
        }
        if let Some(sh) = shell() {
            let _ = PostMessageW(sh, reg_msg("ShellMenuVerde"), WPARAM(h.0 as usize), makelparam(c.x, c.y + 14));
        }
    }
}

/// o ponteiro em cima do verde (ou não): pede o menu de encaixe ao Shell depois de ~0,65 s
pub fn verde(h: HWND, over: bool) {
    unsafe {
        if over {
            if !prop(h, "MacVerdeEspera") {
                set_prop(h, "MacVerdeEspera");
                SetTimer(h, T_VERDE, 650, Some(verde_tick));
            }
        } else if remove_prop(h, "MacVerdeEspera") {
            let _ = KillTimer(h, T_VERDE);
        }
    }
}

#[repr(C)]
struct Msg {
    hwnd: u64,
    from: RECT,
    to: RECT,
    flags: i32,
    cap: u32,
}

/// maximizar/restaurar com a animação do Shell
pub fn alternar(h: HWND) {
    verde(h, false);
    unsafe {
        let cmd = if IsZoomed(h).as_bool() { SW_RESTORE } else { SW_MAXIMIZE };
        let _ = SetWindowSubclass(h, Some(sub), SUBCLASS, 0);
        let no = BOOL(1);
        let _ = DwmSetWindowAttribute(h, DWMWA_TRANSITIONS_FORCEDISABLED, &no as *const _ as *const c_void, 4);
        let from = visivel(h);
        let m = Msg { hwnd: h.0 as u64, from, to: from, flags: 4, cap: 0 };
        let cd = COPYDATASTRUCT { dwData: 0x414D504C, cbData: std::mem::size_of::<Msg>() as u32, lpData: &m as *const _ as *mut c_void };
        let mut ok = 0usize;
        let anim = match shell() {
            Some(sh) if IsWindowVisible(h).as_bool() && !IsIconic(h).as_bool() => {
                SendMessageTimeoutW(sh, WM_COPYDATA, WPARAM(h.0 as usize), LPARAM(&cd as *const _ as isize), SMTO_ABORTIFHUNG, 400, Some(&mut ok)).0 != 0 && ok != 0
            }
            _ => false,
        };
        if anim {
            cloak(h, true);
            SetTimer(h, T_SAFETY, 1500, Some(seguranca));
        }
        let _ = ShowWindow(h, cmd);
        if anim {
            let _ = UpdateWindow(h);
            SetTimer(h, T_PHOTO, 40, Some(foto));
        }
    }
}

fn abrir_mostrar(h: HWND) {
    unsafe {
        let _ = KillTimer(h, T_ABRIR);
        let _ = KillTimer(h, T_ABRIR_SEG);
        remove_prop(h, "MacJanelaAbrindoAvisou");
        if !remove_prop(h, "MacJanelaAbrindo") {
            return;
        }
        cloak(h, false);
        let _ = SetWindowPos(h, None, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
    }
}
extern "system" fn abrir_seg(h: HWND, _: u32, _: usize, _: u32) {
    abrir_mostrar(h);
}
extern "system" fn abrir_pronto(h: HWND, _: u32, id: usize, _: u32) {
    unsafe {
        let _ = KillTimer(h, id);
        if !prop(h, "MacJanelaAbrindo") || prop(h, "MacJanelaAbrindoAvisou") {
            return;
        }
        set_prop(h, "MacJanelaAbrindoAvisou");
        let _ = DwmFlush();
        match shell() {
            Some(sh) if PostMessageW(sh, reg_msg("ShellJanelaAbrir"), WPARAM(h.0 as usize), LPARAM(0)).is_ok() => {}
            _ => abrir_mostrar(h),
        }
    }
}
fn anima_janela(h: HWND) -> bool {
    unsafe {
        if IsZoomed(h).as_bool() || IsIconic(h).as_bool() || GetAsyncKeyState(VK_LBUTTON.0 as i32) < 0 {
            return false;
        }
        let mut r = RECT::default();
        let _ = GetWindowRect(h, &mut r);
        if r.right - r.left < 200 || r.bottom - r.top < 120 {
            return false;
        }
        shell().is_some()
    }
}
extern "system" fn sub_abrir(h: HWND, m: u32, w: WPARAM, l: LPARAM, _: usize, _: usize) -> LRESULT {
    unsafe {
        let abriu = reg_msg("MacJanelaAbriu");
        if m == abriu && abriu != 0 {
            abrir_mostrar(h);
            return LRESULT(1);
        }
        let wp = if l.0 != 0 && (m == WM_WINDOWPOSCHANGING) { Some(&*(l.0 as *const WINDOWPOS)) } else { None };
        let showing = (m == WM_SHOWWINDOW && w.0 != 0) || wp.map(|p| (p.flags & SWP_SHOWWINDOW).0 != 0 && !IsWindowVisible(h).as_bool()).unwrap_or(false);
        if showing && !prop(h, "MacJanelaAbrindo") && anima_janela(h) {
            let mut ja = BOOL(0);
            let _ = DwmGetWindowAttribute(h, DWMWA_CLOAKED, &mut ja as *mut _ as *mut c_void, 4);
            if cloak(h, true) {
                set_prop(h, "MacJanelaAbrindo");
                SetTimer(h, T_ABRIR, if ja.as_bool() { 400 } else { 250 }, Some(abrir_pronto));
                SetTimer(h, T_ABRIR_SEG, 1200, Some(abrir_seg));
            }
        }
        if m == WM_PAINT && prop(h, "MacJanelaAbrindo") && !prop(h, "MacJanelaAbrindoAvisou") {
            let r = DefSubclassProc(h, m, w, l);
            SetTimer(h, T_ABRIR, 16, Some(abrir_pronto));
            return r;
        }
        if let Some(p) = wp {
            if (p.flags & SWP_HIDEWINDOW).0 != 0 && IsWindowVisible(h).as_bool() && !IsIconic(h).as_bool() && !prop(h, "MacJanelaAbrindo") {
                let mut cl = BOOL(0);
                let ok = DwmGetWindowAttribute(h, DWMWA_CLOAKED, &mut cl as *mut _ as *mut c_void, 4).is_ok() && cl.as_bool();
                if !ok {
                    if let Some(sh) = shell() {
                        let mut r = 0usize;
                        SendMessageTimeoutW(sh, reg_msg("ShellJanelaFechar"), WPARAM(h.0 as usize), LPARAM(0), SMTO_ABORTIFHUNG, 150, Some(&mut r));
                    }
                }
            }
        }
        if m == WM_NCDESTROY {
            let _ = RemoveWindowSubclass(h, Some(sub_abrir), SUB_ABRIR);
        }
        DefSubclassProc(h, m, w, l)
    }
}

/// chamar logo depois de criar a janela, antes de mostrar
pub fn janela(h: HWND) {
    if !h.0.is_null() {
        unsafe {
            let _ = SetWindowSubclass(h, Some(sub_abrir), SUB_ABRIR, 0);
        }
    }
}

/// para os apps que se escondem até o primeiro quadro
pub fn revelar(h: HWND) {
    if prop(h, "MacJanelaAbrindo") {
        if !prop(h, "MacJanelaAbrindoAvisou") {
            abrir_pronto(h, 0, T_ABRIR, 0);
        }
        return;
    }
    unsafe {
        cloak(h, false);
    }
    let _ = PCWSTR::null();
}