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
5.9 KB
Raw Blame
//! cantos.cpp: Cantos Ativos, como no Mac: levar o ponteiro até um canto faz uma ação (canto_se/sd/ie/id com
//! nada|mission|janelas|mesa|launchpad|notificacoes|central|spotlight|bloquear|tela|nota). Dispara uma vez ao chegar
//! (~120 ms no canto), não com botão apertado nem com app em tela cheia. Lê o ponteiro a cada 50 ms perto de um canto
//! e 150 ms longe.

use std::sync::atomic::{AtomicI32, AtomicU32, AtomicUsize, Ordering};

use comum::windows::Win32::Foundation::*;
use comum::windows::Win32::Graphics::Gdi::{GetMonitorInfoW, MonitorFromPoint, MONITORINFO, MONITOR_DEFAULTTONEAREST};
use comum::windows::Win32::System::SystemInformation::GetTickCount;
use comum::windows::Win32::UI::Input::KeyboardAndMouse::*;
use comum::windows::Win32::UI::WindowsAndMessaging::*;

use crate::missioncontrol;
use crate::bloqueio;
use crate::{apps, central, launchpad, log, mesa, notificacoes, shell, spotlight};

const T_CANTO: usize = 0x4341;
static TIMER: AtomicUsize = AtomicUsize::new(0);
static RITMO: AtomicU32 = AtomicU32::new(50);
static ARMED: AtomicI32 = AtomicI32::new(-1);
static PENDING: AtomicI32 = AtomicI32::new(-1);
static ARRIVE_AT: AtomicU32 = AtomicU32::new(0);
const KEYS: [&str; 4] = ["canto_se", "canto_sd", "canto_ie", "canto_id"];

fn action_of(c: usize) -> String {
    // issue #439: nenhum canto faz nada por padrão (no Windows o canto de baixo à direita é o "Mostrar a área de trabalho" e a
    // Nota Rápida abria sem querer); quem escolheu nos Ajustes continua com a escolha
    shell::setting(KEYS[c], "nada")
}
fn fullscreen_in_front(m: &RECT) -> bool {
    unsafe {
        let fg = GetForegroundWindow();
        if fg.0.is_null() || mesa::is_desk_class(&shell::class_name(fg)) {
            return false;
        }
        let mut r = RECT::default();
        let _ = GetWindowRect(fg, &mut r);
        r.left <= m.left && r.top <= m.top && r.right >= m.right && r.bottom >= m.bottom && GetWindowLongW(fg, GWL_STYLE) as u32 & WS_CAPTION.0 == 0
    }
}
fn run(c: usize) {
    let a = action_of(c);
    log!("cantos: {} -> {a}", KEYS[c]);
    match a.as_str() {
        "mission" => missioncontrol::toggle(),
        "janelas" => unsafe {
            let fg = GetForegroundWindow();
            let root = if fg.0.is_null() { HWND::default() } else { GetAncestor(fg, GA_ROOTOWNER) };
            apps::refresh();
            if !root.0.is_null() && !apps::is_shell_window(root) {
                if let Some(ap) = apps::of_window(root) {
                    missioncontrol::toggle_app(&apps::expose_id_of(&ap.id, &ap.exe), &ap.name.clone());
                }
            }
        },
        "mesa" => mesa::win_key('D' as u16),
        "launchpad" => launchpad::toggle(),
        "notificacoes" => notificacoes::toggle(),
        "central" => central::toggle(),
        "spotlight" => spotlight::toggle(),
        "bloquear" => bloqueio::lock(),
        "tela" => unsafe {
            let _ = PostMessageW(HWND_BROADCAST, WM_SYSCOMMAND, WPARAM(SC_MONITORPOWER as usize), LPARAM(2)); // descansar a tela
        },
        _ => {}
    }
}
unsafe extern "system" fn tick(_: HWND, _: u32, _: usize, _: u32) {
    let mut p = POINT::default();
    if GetCursorPos(&mut p).is_err() {
        return;
    }
    let mut mi = MONITORINFO { cbSize: std::mem::size_of::<MONITORINFO>() as u32, ..Default::default() };
    if !GetMonitorInfoW(MonitorFromPoint(p, MONITOR_DEFAULTTONEAREST), &mut mi).as_bool() {
        return;
    }
    let m = mi.rcMonitor;
    // 50 ms perto de um canto (a menos de 240 px); longe, 150 ms (23/11/2026)
    let perto = (p.x <= m.left + 240 || p.x >= m.right - 240) && (p.y <= m.top + 240 || p.y >= m.bottom - 240);
    let ms = if perto || PENDING.load(Ordering::Relaxed) >= 0 {
        50
    } else if crate::leve::economia() {
        300 // na bateria (issue #490)
    } else {
        150
    };
    let t = TIMER.load(Ordering::Relaxed);
    if ms != RITMO.load(Ordering::Relaxed) && t != 0 {
        RITMO.store(ms, Ordering::Relaxed);
        TIMER.store(SetTimer(None, t, ms, Some(tick)), Ordering::Relaxed);
    }
    const E: i32 = 2;
    let (l, r, top, b) = (p.x <= m.left + E, p.x >= m.right - 1 - E, p.y <= m.top + E, p.y >= m.bottom - 1 - E);
    let c: i32 = match (top, b, l, r) {
        (true, _, true, _) => 0,
        (true, _, _, true) => 1,
        (_, true, true, _) => 2,
        (_, true, _, true) => 3,
        _ => -1,
    };
    if c < 0 {
        ARMED.store(-1, Ordering::Relaxed);
        PENDING.store(-1, Ordering::Relaxed);
        return;
    }
    if c == ARMED.load(Ordering::Relaxed) {
        return; // já disparou neste canto
    }
    let a = action_of(c as usize);
    if a == "nada" || a == "nota" || GetAsyncKeyState(VK_LBUTTON.0 as i32) < 0 || GetAsyncKeyState(VK_RBUTTON.0 as i32) < 0 || fullscreen_in_front(&m) {
        ARMED.store(c, Ordering::Relaxed); // "nota": a Nota Rápida cuida (quadradinho)
        return;
    }
    if PENDING.load(Ordering::Relaxed) != c {
        PENDING.store(c, Ordering::Relaxed);
        ARRIVE_AT.store(GetTickCount(), Ordering::Relaxed);
        return;
    }
    if GetTickCount().wrapping_sub(ARRIVE_AT.load(Ordering::Relaxed)) < 120 {
        return; // ~2 leituras no canto: chegou de propósito
    }
    ARMED.store(c, Ordering::Relaxed);
    PENDING.store(-1, Ordering::Relaxed);
    run(c as usize);
}
pub fn start() {
    if shell::foto() {
        return;
    }
    TIMER.store(unsafe { SetTimer(None, T_CANTO, 50, Some(tick)) }, Ordering::Relaxed);
    RITMO.store(50, Ordering::Relaxed);
}
/// modo jogo: sem ler o ponteiro enquanto o jogo roda
pub fn pausar(p: bool) {
    let t = TIMER.load(Ordering::Relaxed);
    unsafe {
        if p && t != 0 {
            let _ = KillTimer(None, t);
            TIMER.store(0, Ordering::Relaxed);
        } else if !p && t == 0 {
            TIMER.store(SetTimer(None, T_CANTO, 50, Some(tick)), Ordering::Relaxed);
            RITMO.store(50, Ordering::Relaxed);
        }
    }
}
pub fn nota_rapida() -> bool {
    action_of(3) == "nota"
}