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
6.4 KB
Raw Blame
//! anim.cpp: animações no jeito do macOS. Relógio de alta precisão (QueryPerformanceCounter), a curva padrão do Core
//! Animation, cubic-bezier(0.25, 0.1, 0.25, 1.0), e molas como as do SwiftUI (resposta e amortecimento).
#![allow(dead_code)]

use std::sync::atomic::{AtomicBool, Ordering};

use comum::windows::Win32::System::Performance::{QueryPerformanceCounter, QueryPerformanceFrequency};
use comum::windows::Win32::System::SystemInformation::GetTickCount;

/// Ajustes > Acessibilidade > Reduzir movimento (reduzir_movimento=1): o que move ou escala vira corte seco ou fade curto
pub static REDUCE_MOTION: AtomicBool = AtomicBool::new(false);
pub fn reduce_motion() -> bool {
    REDUCE_MOTION.load(Ordering::Relaxed)
}
pub const PANEL_OUT_MS: f64 = 150.0;

/// ms, QueryPerformanceCounter
pub fn now() -> f64 {
    unsafe {
        let (mut f, mut c) = (0i64, 0i64);
        let _ = QueryPerformanceFrequency(&mut f);
        let _ = QueryPerformanceCounter(&mut c);
        c as f64 * 1000.0 / f.max(1) as f64
    }
}
/// cubic-bezier (0,0)-(x1,y1)-(x2,y2)-(1,1): acha o parâmetro pelo x (Newton + bisseção) e devolve o y
pub fn bezier(t: f32, x1: f32, y1: f32, x2: f32, y2: f32) -> f32 {
    if t <= 0.0 {
        return 0.0;
    }
    if t >= 1.0 {
        return 1.0;
    }
    let bx = |u: f32| {
        let v = 1.0 - u;
        3.0 * v * v * u * x1 + 3.0 * v * u * u * x2 + u * u * u
    };
    let by = |u: f32| {
        let v = 1.0 - u;
        3.0 * v * v * u * y1 + 3.0 * v * u * u * y2 + u * u * u
    };
    let dx = |u: f32| {
        let v = 1.0 - u;
        3.0 * v * v * x1 + 6.0 * v * u * (x2 - x1) + 3.0 * u * u * (1.0 - x2)
    };
    let mut u = t;
    for _ in 0..6 {
        let (e, d) = (bx(u) - t, dx(u));
        if e.abs() < 1e-4 || d.abs() < 1e-6 {
            break;
        }
        u -= e / d;
    }
    if !(0.0..=1.0).contains(&u) || (bx(u) - t).abs() > 1e-3 {
        // garantia: bisseção
        let (mut lo, mut hi) = (0.0f32, 1.0f32);
        u = t;
        for _ in 0..30 {
            let x = bx(u);
            if (x - t).abs() < 1e-5 {
                break;
            }
            if x < t {
                lo = u;
            } else {
                hi = u;
            }
            u = (lo + hi) / 2.0;
        }
    }
    by(u)
}
/// curva padrão do sistema
pub fn ease(t: f32) -> f32 {
    bezier(t, 0.25, 0.1, 0.25, 1.0)
}
/// entra e sai devagar
pub fn ease_in_out(t: f32) -> f32 {
    bezier(t, 0.42, 0.0, 0.58, 1.0)
}
/// começa rápido
pub fn ease_out(t: f32) -> f32 {
    bezier(t, 0.0, 0.0, 0.58, 1.0)
}
/// Mola partindo de 0 e indo a 1. response: período sem amortecimento (s); damping: 1 = sem quique, < 1 = quica.
pub fn spring(t_sec: f32, response: f32, damping: f32) -> f32 {
    if t_sec <= 0.0 {
        return 0.0;
    }
    let w = 2.0 * std::f32::consts::PI / response;
    if damping >= 1.0 {
        return 1.0 - (1.0 + w * t_sec) * (-w * t_sec).exp(); // crítica: sem passar do ponto
    }
    let wd = w * (1.0 - damping * damping).sqrt();
    1.0 - (-damping * w * t_sec).exp() * ((wd * t_sec).cos() + damping * w / wd * (wd * t_sec).sin())
}
pub fn spring_default(t_sec: f32) -> f32 {
    spring(t_sec, 0.55, 0.825)
}
fn out_expo(t: f32) -> f32 {
    if t >= 1.0 {
        1.0
    } else {
        1.0 - 2f32.powf(-10.0 * t)
    }
}
/// A mola dos painéis do Shell: abrindo, aparece em 180 ms e cresce de 96% a 100%; fechando, 150 ms acelerando.
#[derive(Clone, Copy)]
pub struct Panel {
    pub fade: f32,
    pub scale: f32,
    pub done: bool,
}
impl Default for Panel {
    fn default() -> Self {
        Panel { fade: 1.0, scale: 1.0, done: true }
    }
}
/// Tahoe (gravado no macOS 26): fade de ~80 ms e cresce de 98 % a 100 % numa curva out-expo até ~300 ms
pub fn panel_in(ms: f64) -> Panel {
    let t = (ms / 1000.0) as f32;
    let mut p = Panel::default();
    if reduce_motion() {
        return p; // aparece na hora (o Spotlight do Mac também)
    }
    if crate::liquid::on() {
        p.fade = ease_out((t / 0.08).min(1.0));
        p.scale = 0.98 + 0.02 * out_expo((t / 0.30).min(1.0));
        p.done = ms >= 300.0;
        return p;
    }
    p.fade = ease((t / 0.18).min(1.0));
    p.scale = 0.96 + 0.04 * spring(t, 0.30, 0.78);
    p.done = ms >= 520.0;
    p
}
pub fn panel_out(ms: f64) -> Panel {
    if reduce_motion() {
        // fade curto, sem encolher
        let u = (ms / 120.0).min(1.0) as f32;
        return Panel { fade: 1.0 - u, scale: 1.0, done: ms >= 120.0 };
    }
    if crate::liquid::on() {
        let u = (ms / 100.0).min(1.0) as f32;
        return Panel { fade: 1.0 - out_expo(u), scale: 0.98 + 0.02 * (1.0 - u), done: ms >= 100.0 };
    }
    let t = 1.0 - (ms / PANEL_OUT_MS).min(1.0) as f32;
    let fade = t * t;
    Panel { fade, scale: 0.96 + 0.04 * fade, done: ms >= PANEL_OUT_MS }
}
/// Central de Controle do Tahoe: nasce com ~95 % da largura e cresce em ~100 ms; módulos com fade de ~250 ms
pub fn cc_in(ms: f64) -> Panel {
    let t = (ms / 1000.0) as f32;
    Panel {
        fade: ease_out((t / 0.25).min(1.0)),
        scale: if reduce_motion() { 1.0 } else { 0.95 + 0.05 * bezier((t / 0.10).min(1.0), 0.2, 0.9, 0.3, 1.0) },
        done: ms >= 260.0,
    }
}
/// fechando: fade linear de ~220 ms, encolhendo para ~88 %
pub fn cc_out(ms: f64) -> Panel {
    let d = if reduce_motion() { 270.0 } else { 220.0 };
    let u = (ms / d).min(1.0) as f32;
    Panel { fade: 1.0 - u, scale: if reduce_motion() { 1.0 } else { 1.0 - 0.12 * ease_in_out(u) }, done: ms >= d }
}
/// quanto tempo a mola leva para assentar (|1 - x| < 0,1%)
pub fn spring_duration(response: f32, damping: f32) -> f32 {
    let mut t = 0.05f32;
    while t < 4.0 {
        let mut settled = true;
        let mut u = t;
        while u < t + 0.3 {
            if (1.0 - spring(u, response, damping)).abs() > 0.001 {
                settled = false;
                break;
            }
            u += 0.02;
        }
        if settled {
            return t;
        }
        t += 0.01;
    }
    4.0
}
/// mesma base do GetTickCount, com fração de milissegundo (compara com marcas gravadas por GetTickCount)
pub fn tick() -> f64 {
    use comum::windows::Win32::System::WindowsProgramming::QueryInterruptTimePrecise;
    static OFFSET: comum::Global<Option<f64>> = comum::Global::new(None);
    unsafe {
        let t = QueryInterruptTimePrecise();
        let off = *OFFSET.get().get_or_insert_with(|| t as f64 / 10000.0 - GetTickCount() as f64); // alinha uma vez
        t as f64 / 10000.0 - off
    }
}