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
14.0 KB
Raw Blame
//! Porte de src\comum\barra_video.h: a barra de controles de vídeo do macOS 27 (QuickTime e Quick Look) e o vidro
//! fosco por trás dela (a janela de origem copiada com PrintWindow e desfocada pelo efeito Gaussiano do Direct2D).

use std::ffi::c_void;

use windows::core::Interface;
use windows::Win32::Foundation::*;
use windows::Win32::Graphics::Direct2D::Common::*;
use windows::Win32::Graphics::Direct2D::*;
use windows::Win32::Graphics::DirectWrite::*;
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_B8G8R8A8_UNORM;
use windows::Win32::Graphics::Gdi::*;
use windows::Win32::Storage::Xps::{PrintWindow, PRINT_WINDOW_FLAGS};
use windows::Win32::System::SystemInformation::GetTickCount;
use windows::Win32::UI::WindowsAndMessaging::*;

use crate::{col, pt, rf, rr, w, Global, Ponto, RectF};

pub const VOL: i32 = 1;
pub const BACK: i32 = 2;
pub const PLAY: i32 = 3;
pub const FWD: i32 = 4;
pub const FULL: i32 = 5;
pub const SCRUB: i32 = 6;
pub const LARGURA: f32 = 458.0;
pub const ALTURA: f32 = 98.0;

#[derive(Clone, Copy)]
pub struct Hit {
    pub id: i32,
    pub r: RectF,
}

unsafe fn geo(rt: &ID2D1RenderTarget, build: impl FnOnce(&ID2D1GeometrySink)) -> Option<ID2D1PathGeometry> {
    let g = rt.GetFactory().ok()?.CreatePathGeometry().ok()?;
    let sk = g.Open().ok()?;
    build(&sk);
    sk.Close().ok()?;
    Some(g)
}
fn arc(p: Ponto, r: f32) -> D2D1_ARC_SEGMENT {
    D2D1_ARC_SEGMENT { point: p, size: D2D_SIZE_F { width: r, height: r }, rotationAngle: 0.0, sweepDirection: D2D1_SWEEP_DIRECTION_CLOCKWISE, arcSize: D2D1_ARC_SIZE_SMALL }
}
fn quad(a: Ponto, b: Ponto) -> D2D1_QUADRATIC_BEZIER_SEGMENT {
    D2D1_QUADRATIC_BEZIER_SEGMENT { point1: a, point2: b }
}

/// desenha a barra em (0, 0, W, H) do alvo; devolve os retângulos clicáveis (em pt)
#[allow(clippy::too_many_arguments)]
pub unsafe fn desenha(rt: &ID2D1RenderTarget, b: &ID2D1SolidColorBrush, wd: f32, h: f32, volume: f32, playing: bool, pos: f64, len: f64, tf_l: &IDWriteTextFormat, tf_r: &IDWriteTextFormat) -> Vec<Hit> {
    let mut hits = Vec::new();
    b.SetColor(&col(0.36, 0.34, 0.31, 0.52));
    rt.FillRoundedRectangle(&rr(rf(0.0, 0.0, wd, h), 22.0, 22.0), b);
    b.SetColor(&col(1.0, 1.0, 1.0, 0.20));
    rt.DrawRoundedRectangle(&rr(rf(0.5, 0.5, wd - 0.5, h - 0.5), 21.5, 21.5), b, 1.0, None);
    let fill = |build: &dyn Fn(&ID2D1GeometrySink)| {
        if let Some(g) = geo(rt, |s| build(s)) {
            rt.FillGeometry(&g, b, None);
        }
    };
    let estilo = rt.GetFactory().ok().and_then(|f| {
        let sp = D2D1_STROKE_STYLE_PROPERTIES { startCap: D2D1_CAP_STYLE_ROUND, endCap: D2D1_CAP_STYLE_ROUND, dashCap: D2D1_CAP_STYLE_ROUND, lineJoin: D2D1_LINE_JOIN_ROUND, miterLimit: 10.0, dashStyle: D2D1_DASH_STYLE_SOLID, dashOffset: 0.0 };
        f.CreateStrokeStyle(&sp, None).ok()
    });
    let stroke = |build: &dyn Fn(&ID2D1GeometrySink), wd: f32| {
        if let Some(g) = geo(rt, |s| build(s)) {
            rt.DrawGeometry(&g, b, wd, estilo.as_ref());
        }
    };
    let tri = |a: Ponto, c: Ponto, d: Ponto| {
        fill(&|s| {
            s.BeginFigure(a, D2D1_FIGURE_BEGIN_FILLED);
            s.AddLine(c);
            s.AddLine(d);
            s.EndFigure(D2D1_FIGURE_END_CLOSED);
        })
    };
    let knob = |x: f32, y: f32| {
        for k in (1..=3).rev() {
            let k = k as f32;
            b.SetColor(&col(0.0, 0.0, 0.0, 0.07));
            rt.FillRoundedRectangle(&rr(rf(x - 10.0 - k * 0.4, y - 7.5 + k * 0.4, x + 10.0 + k * 0.4, y + 7.5 + k * 0.8), 7.5 + k * 0.4, 7.5 + k * 0.4), b);
        }
        b.SetColor(&col(0.96, 0.96, 0.95, 1.0));
        rt.FillRoundedRectangle(&rr(rf(x - 10.0, y - 7.5, x + 10.0, y + 7.5), 7.5, 7.5), b);
    };
    let track = |r: RectF, f: f32| {
        b.SetColor(&col(1.0, 1.0, 1.0, 0.26));
        rt.FillRoundedRectangle(&rr(r, 2.5, 2.5), b);
        let kx = r.left + 10.0 + (r.right - r.left - 20.0) * f;
        b.SetColor(&col(1.0, 1.0, 1.0, 0.94));
        rt.FillRoundedRectangle(&rr(rf(r.left, r.top, kx, r.bottom), 2.5, 2.5), b);
        knob(kx, (r.top + r.bottom) / 2.0);
    };
    let ic = col(1.0, 1.0, 1.0, 0.86);
    let (cy, cx) = (30.5f32, wd / 2.0);
    let mid_l = cx - 41.0 - 11.0 - 12.0;
    let mid_r = cx + 39.0 + 11.0 + 12.0;
    let show_air = wd - 124.5 - 10.0 > mid_r;
    let show_sub = wd - 81.0 - 10.0 > mid_r;
    let vol_r = 117.5f32.min(mid_l);
    let vol_on = vol_r - 40.5 >= 24.0;
    b.SetColor(&ic);
    fill(&|s| {
        s.BeginFigure(pt(20.0, cy - 2.6), D2D1_FIGURE_BEGIN_FILLED);
        for p in [(23.2, cy - 2.6), (27.5, cy - 6.5), (27.5, cy + 6.5), (23.2, cy + 2.6), (20.0, cy + 2.6)] {
            s.AddLine(pt(p.0, p.1));
        }
        s.EndFigure(D2D1_FIGURE_END_CLOSED);
    });
    for k in 0..2 {
        if volume <= if k == 1 { 0.5 } else { 0.01 } {
            break;
        }
        let (rr_, a) = if k == 1 { (6.5f32, 0.95f32) } else { (3.6, 0.85) };
        stroke(
            &|s| {
                s.BeginFigure(pt(28.5 + rr_ * 0.6, cy - rr_ * a), D2D1_FIGURE_BEGIN_HOLLOW);
                s.AddArc(&arc(pt(28.5 + rr_ * 0.6, cy + rr_ * a), rr_));
                s.EndFigure(D2D1_FIGURE_END_OPEN);
            },
            1.4,
        );
    }
    if vol_on {
        let vol = rf(40.5, cy - 2.5, vol_r, cy + 2.5);
        track(vol, volume);
        hits.push(Hit { id: VOL, r: rf(vol.left - 6.0, cy - 12.0, vol.right + 6.0, cy + 12.0) });
    }
    b.SetColor(&ic);
    let (bx, fx) = (cx - 41.0, cx + 39.0);
    tri(pt(bx - 11.0, cy), pt(bx, cy - 6.5), pt(bx, cy + 6.5));
    tri(pt(bx, cy), pt(bx + 11.0, cy - 6.5), pt(bx + 11.0, cy + 6.5));
    tri(pt(fx + 11.0, cy), pt(fx, cy - 6.5), pt(fx, cy + 6.5));
    tri(pt(fx, cy), pt(fx - 11.0, cy - 6.5), pt(fx - 11.0, cy + 6.5));
    b.SetColor(&col(1.0, 1.0, 1.0, 0.94));
    if playing {
        rt.FillRoundedRectangle(&rr(rf(cx - 8.5, cy - 12.5, cx - 2.5, cy + 12.5), 1.5, 1.5), b);
        rt.FillRoundedRectangle(&rr(rf(cx + 2.5, cy - 12.5, cx + 8.5, cy + 12.5), 1.5, 1.5), b);
    } else {
        fill(&|s| {
            s.BeginFigure(pt(cx - 9.0, cy - 12.0), D2D1_FIGURE_BEGIN_FILLED);
            s.AddQuadraticBezier(&quad(pt(cx - 9.0, cy - 14.5), pt(cx - 6.8, cy - 13.2)));
            s.AddLine(pt(cx + 13.0, cy - 1.4));
            s.AddQuadraticBezier(&quad(pt(cx + 15.0, cy), pt(cx + 13.0, cy + 1.4)));
            s.AddLine(pt(cx - 6.8, cy + 13.2));
            s.AddQuadraticBezier(&quad(pt(cx - 9.0, cy + 14.5), pt(cx - 9.0, cy + 12.0)));
            s.EndFigure(D2D1_FIGURE_END_CLOSED);
        });
    }
    hits.push(Hit { id: BACK, r: rf(bx - 16.0, cy - 14.0, bx + 16.0, cy + 14.0) });
    hits.push(Hit { id: PLAY, r: rf(cx - 18.0, cy - 18.0, cx + 18.0, cy + 18.0) });
    hits.push(Hit { id: FWD, r: rf(fx - 16.0, cy - 14.0, fx + 16.0, cy + 14.0) });
    b.SetColor(&col(1.0, 1.0, 1.0, 0.78));
    let (ax, sx, mx) = (wd - 124.5, wd - 81.0, wd - 26.0);
    if show_air {
        stroke(
            &|s| {
                s.BeginFigure(pt(ax - 4.5, cy + 5.5), D2D1_FIGURE_BEGIN_HOLLOW);
                for p in [(ax - 9.0, cy + 5.5), (ax - 9.0, cy - 7.0), (ax + 9.0, cy - 7.0), (ax + 9.0, cy + 5.5), (ax + 4.5, cy + 5.5)] {
                    s.AddLine(pt(p.0, p.1));
                }
                s.EndFigure(D2D1_FIGURE_END_OPEN);
            },
            1.3,
        );
        tri(pt(ax, cy + 1.0), pt(ax + 5.0, cy + 8.0), pt(ax - 5.0, cy + 8.0));
    }
    if show_sub {
        stroke(
            &|s| {
                s.BeginFigure(pt(sx - 6.0, cy + 5.0), D2D1_FIGURE_BEGIN_HOLLOW);
                s.AddLine(pt(sx - 7.0, cy + 5.0));
                s.AddArc(&arc(pt(sx - 9.0, cy + 3.0), 2.0));
                s.AddLine(pt(sx - 9.0, cy - 5.0));
                s.AddArc(&arc(pt(sx - 7.0, cy - 7.0), 2.0));
                s.AddLine(pt(sx + 7.0, cy - 7.0));
                s.AddArc(&arc(pt(sx + 9.0, cy - 5.0), 2.0));
                s.AddLine(pt(sx + 9.0, cy + 3.0));
                s.AddArc(&arc(pt(sx + 7.0, cy + 5.0), 2.0));
                s.AddLine(pt(sx - 2.0, cy + 5.0));
                s.AddLine(pt(sx - 6.0, cy + 8.5));
                s.EndFigure(D2D1_FIGURE_END_CLOSED);
            },
            1.3,
        );
        rt.DrawLine(pt(sx - 5.0, cy - 1.5), pt(sx + 5.0, cy - 1.5), b, 1.2, None);
        rt.DrawLine(pt(sx - 5.0, cy + 1.5), pt(sx + 2.0, cy + 1.5), b, 1.2, None);
    }
    stroke(
        &|s| {
            for o in [-3.5f32, 2.5] {
                s.BeginFigure(pt(mx + o - 2.5, cy - 5.5), D2D1_FIGURE_BEGIN_HOLLOW);
                s.AddLine(pt(mx + o + 2.5, cy));
                s.AddLine(pt(mx + o - 2.5, cy + 5.5));
                s.EndFigure(D2D1_FIGURE_END_OPEN);
            }
        },
        1.4,
    );
    hits.push(Hit { id: FULL, r: rf(mx - 14.0, cy - 14.0, mx + 14.0, cy + 14.0) });
    let tc = |v: f64| {
        let v = v.max(0.0);
        let t2 = v as i64;
        let fr = ((v - t2 as f64) * 30.0) as i64;
        format!("{:02}:{:02}:{:02}:{:02}", t2 / 3600, t2 / 60 % 60, t2 % 60, fr)
    };
    let mss = |v: f64| {
        let t2 = v.max(0.0) as i64;
        format!("{}:{:02}", t2 / 60, t2 % 60)
    };
    let by = 73.0;
    b.SetColor(&col(1.0, 1.0, 1.0, 0.72));
    let short_tc = wd - 113.0 - 113.5 < 120.0;
    let (a1, a2) = if short_tc { (mss(pos), mss(len)) } else { (tc(pos), tc(len)) };
    let tw = if short_tc { 44.0 } else { 103.0 };
    rt.DrawText(&w(&a1), tf_l, &rf(17.0, by - 10.0, 17.0 + tw, by + 10.0), b, D2D1_DRAW_TEXT_OPTIONS_NONE, DWRITE_MEASURING_MODE_NATURAL);
    rt.DrawText(&w(&a2), tf_r, &rf(wd - 17.0 - tw, by - 10.0, wd - 17.0, by + 10.0), b, D2D1_DRAW_TEXT_OPTIONS_NONE, DWRITE_MEASURING_MODE_NATURAL);
    let bar = if short_tc { rf(17.0 + tw + 8.0, by - 2.5, wd - 17.0 - tw - 8.0, by + 2.5) } else { rf(113.5, by - 2.5, wd - 113.0, by + 2.5) };
    track(bar, if len > 0.0 { (pos / len).clamp(0.0, 1.0) as f32 } else { 0.0 });
    hits.push(Hit { id: SCRUB, r: rf(bar.left, by - 12.0, bar.right, by + 12.0) });
    hits
}

pub fn volume_em(hit: &RectF, x: f32) -> f32 {
    ((x - hit.left - 16.0) / (hit.right - hit.left - 32.0)).clamp(0.0, 1.0)
}
pub fn posicao_em(hit: &RectF, x: f32) -> f32 {
    ((x - hit.left - 10.0) / (hit.right - hit.left - 20.0)).clamp(0.0, 1.0)
}

struct Copia {
    mem: HDC,
    bmp: HBITMAP,
    bits: *mut c_void,
    bw: i32,
    bh: i32,
    last: HWND,
    at: u32,
}
static COPIA: Global<Option<Copia>> = Global::new(None);

/// vidro fosco: copia o que `src` mostra no retângulo de tela `scr`, desfoca e desenha em `dst` recortado no canto `rad`
pub unsafe fn fundo_borrado(rt: &ID2D1RenderTarget, src: HWND, scr: RECT, sc: f32, dst: RectF, rad: f32, sigma: f32) -> bool {
    if src.0.is_null() || !IsWindowVisible(src).as_bool() {
        return false;
    }
    let mut wr = RECT::default();
    let _ = GetWindowRect(src, &mut wr);
    let (ww, wh) = (wr.right - wr.left, wr.bottom - wr.top);
    if ww <= 0 || wh <= 0 {
        return false;
    }
    let c = COPIA.get().get_or_insert_with(|| Copia { mem: CreateCompatibleDC(None), bmp: HBITMAP::default(), bits: std::ptr::null_mut(), bw: 0, bh: 0, last: HWND::default(), at: 0 });
    if ww != c.bw || wh != c.bh {
        if !c.bmp.0.is_null() {
            let _ = DeleteObject(c.bmp);
        }
        let mut bi = BITMAPINFO::default();
        bi.bmiHeader = BITMAPINFOHEADER { biSize: std::mem::size_of::<BITMAPINFOHEADER>() as u32, biWidth: ww, biHeight: -wh, biPlanes: 1, biBitCount: 32, ..Default::default() };
        match CreateDIBSection(c.mem, &bi, DIB_RGB_COLORS, &mut c.bits, None, 0) {
            Ok(b) => {
                c.bmp = b;
                SelectObject(c.mem, c.bmp);
                c.bw = ww;
                c.bh = wh;
                c.last = HWND::default();
            }
            Err(_) => {
                c.bw = 0;
                c.bh = 0;
                return false;
            }
        }
    }
    let now = GetTickCount();
    if c.last != src || now.wrapping_sub(c.at) > 100 {
        if !PrintWindow(src, c.mem, PRINT_WINDOW_FLAGS(2)).as_bool() {
            return false;
        }
        let _ = GdiFlush();
        c.last = src;
        c.at = now;
    }
    let x0 = (scr.left - wr.left).clamp(0, ww);
    let y0 = (scr.top - wr.top).clamp(0, wh);
    let x1 = (scr.right - wr.left).clamp(0, ww);
    let y1 = (scr.bottom - wr.top).clamp(0, wh);
    if x1 - x0 < 2 || y1 - y0 < 2 {
        return false;
    }
    let bp = D2D1_BITMAP_PROPERTIES { pixelFormat: D2D1_PIXEL_FORMAT { format: DXGI_FORMAT_B8G8R8A8_UNORM, alphaMode: D2D1_ALPHA_MODE_IGNORE }, dpiX: 96.0 * sc, dpiY: 96.0 * sc };
    let ini = (c.bits as *const u8).add(((y0 * ww + x0) * 4) as usize);
    let Ok(b) = rt.CreateBitmap(D2D_SIZE_U { width: (x1 - x0) as u32, height: (y1 - y0) as u32 }, Some(ini as *const c_void), (ww * 4) as u32, &bp) else { return false };
    let at2 = pt(dst.left + (x0 - (scr.left - wr.left)) as f32 / sc, dst.top + (y0 - (scr.top - wr.top)) as f32 / sc);
    let rg = rt.GetFactory().ok().and_then(|f| f.CreateRoundedRectangleGeometry(&rr(dst, rad, rad)).ok());
    if let Some(g) = &rg {
        let lp = D2D1_LAYER_PARAMETERS {
            contentBounds: rf(-1e9, -1e9, 1e9, 1e9),
            geometricMask: std::mem::ManuallyDrop::new(Some(g.cast().unwrap())),
            maskAntialiasMode: D2D1_ANTIALIAS_MODE_PER_PRIMITIVE,
            maskTransform: crate::mat::ident(),
            opacity: 1.0,
            opacityBrush: std::mem::ManuallyDrop::new(None),
            layerOptions: D2D1_LAYER_OPTIONS_NONE,
        };
        rt.PushLayer(&lp, None);
    }
    let mut ok = false;
    if let Ok(dc) = rt.cast::<ID2D1DeviceContext>() {
        if let Ok(fx) = dc.CreateEffect(&CLSID_D2D1GaussianBlur) {
            fx.SetInput(0, &b, true);
            let _ = fx.SetValue(D2D1_GAUSSIANBLUR_PROP_STANDARD_DEVIATION.0 as u32, D2D1_PROPERTY_TYPE_FLOAT, &sigma.to_le_bytes());
            let _ = fx.SetValue(D2D1_GAUSSIANBLUR_PROP_BORDER_MODE.0 as u32, D2D1_PROPERTY_TYPE_ENUM, &(D2D1_BORDER_MODE_HARD.0 as u32).to_le_bytes());
            if let Ok(img) = fx.cast::<ID2D1Image>() {
                dc.DrawImage(&img, Some(&at2), None, D2D1_INTERPOLATION_MODE_LINEAR, D2D1_COMPOSITE_MODE_SOURCE_OVER);
                ok = true;
            }
        }
    }
    if rg.is_some() {
        rt.PopLayer();
    }
    ok
}