A new style desktop shell for Windows 11: menu bar, Dock, Finder, Spotlight, Control Center and Liquid Glass, in native C++.
Rust
//! main.cpp (7ª parte): o Quick Look (barra de espaço), no visual do macOS: painel arredondado por cima do Finder, com
//! barra de título (fechar, nome, compartilhar, "Abrir com <app>"). Fotos ocupam o painel todo (a roda amplia); texto
//! aparece numa folha que rola; áudio e vídeo tocam no painel; o resto mostra ícone grande e informações. No Golden Gate
//! (e para PDF, SVG, Office e fontes) vai para o Quick Look do Shell. Leopard: o HUD preto do 10.5.
#![allow(dead_code)]
use std::ffi::c_void;
use comum::windows::core::*;
use comum::windows::Win32::Foundation::*;
use comum::windows::Win32::UI::Input::KeyboardAndMouse::SetCapture;
use comum::windows::Win32::Storage::EnhancedStorage::{PKEY_Music_Artist, PKEY_Title};
use comum::windows::Win32::Graphics::Direct2D::*;
use comum::windows::Win32::System::Com::CoTaskMemFree;
use comum::windows::Win32::System::DataExchange::COPYDATASTRUCT;
use comum::windows::Win32::System::SystemInformation::GetTickCount;
use comum::windows::Win32::UI::Shell::PropertiesSystem::*;
use comum::windows::Win32::UI::Shell::*;
use comum::windows::Win32::UI::WindowsAndMessaging::*;
use comum::*;
use crate::base::*;
use crate::desenho::*;
use crate::estado::*;
use crate::media;
use crate::pasta::*;
use crate::vistas::child_items;
const QL_ANIM_MS: f32 = 240.0;
const QL_CLOSE_MS: f32 = 190.0;
pub fn ql_title_h() -> f32 {
if semaforo::aqua() {
38.0
} else if !s().tahoe && crate::leopard_theme() {
24.0 // Leopard: a faixa de 24 do HUD
} else {
40.0
}
}
pub fn is_text_file(e: &Entry) -> bool {
const EXTS: [&str; 37] = ["txt", "md", "log", "json", "xml", "csv", "ini", "cfg", "yaml", "yml", "cpp", "c", "h", "hpp", "cs", "py", "js", "ts", "tsx", "jsx", "html", "htm", "css", "cmd", "bat", "ps1", "sh", "toml", "rs", "go", "java", "kt", "lua", "sql", "gitignore", "reg", "srt"];
EXTS.contains(&ext_of(e).as_str())
}
/// Primeiro 1 MB como texto (UTF-16 com BOM, UTF-8 ou a página de código do Windows).
// issue #117: a leitura rodava dentro do desenho (caminho do WM_PAINT): 1 MB de leitura + decodificação na thread da
// janela a cada troca de arquivo. Em disco lento, compartilhamento de rede ou arquivo sendo gravado, a pintura (e a
// entrada) congelava. Agora a leitura vai para uma thread; a folha mostra "Carregando…" e o texto entra quando a
// WM_APP_QLTEXT chega. No modo foto (sem janela) continua direto.
pub const WM_APP_QLTEXT: u32 = WM_APP + 32; // lParam: Box<(caminho, linhas)>
/// caminho com leitura em andamento (mexido só na thread da janela)
static QL_TEXT_LOADING: Global<String> = Global::new(String::new());
pub fn load_ql_text(path: &str) {
if path == s().ql_text_path {
return;
}
s().ql_text_path = path.into();
s().ql_lines.clear();
s().ql_text_scroll = 0.0;
if s().foto.is_some() {
s().ql_lines = ler_texto(path);
return;
}
if *QL_TEXT_LOADING.get() == path {
return; // a leitura deste arquivo já está a caminho
}
*QL_TEXT_LOADING.get() = path.into();
let (p, hw) = (path.to_string(), s().hwnd.0 as isize);
std::thread::spawn(move || {
let linhas = ler_texto(&p);
let r = Box::into_raw(Box::new((p, linhas)));
unsafe {
if PostMessageW(HWND(hw as *mut _), WM_APP_QLTEXT, WPARAM(0), LPARAM(r as isize)).is_err() {
drop(Box::from_raw(r)); // a janela já fechou
}
}
});
}
/// o texto lido pela thread (WM_APP_QLTEXT)
pub unsafe fn ql_text_chegou(l: LPARAM) {
let r = Box::from_raw(l.0 as *mut (String, Vec<String>));
let (p, linhas) = *r;
if *QL_TEXT_LOADING.get() == p {
QL_TEXT_LOADING.get().clear();
}
if p == s().ql_text_path {
s().ql_lines = linhas;
s().ql_text_scroll = 0.0;
if s().ql {
crate::invalidate();
}
}
}
/// Primeiros 1 MB como texto (UTF-16 com BOM, UTF-8 ou a página de código do Windows), em linhas
fn ler_texto(path: &str) -> Vec<String> {
let mut out = Vec::new();
let Ok(mut b) = std::fs::read(path) else { return out };
b.truncate(1024 * 1024);
let text = if b.len() >= 2 && b[0] == 0xFF && b[1] == 0xFE {
let u: Vec<u16> = b[2..].chunks_exact(2).map(|c| u16::from_le_bytes([c[0], c[1]])).collect();
String::from_utf16_lossy(&u)
} else {
let off = if b.len() >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF { 3 } else { 0 };
match std::str::from_utf8(&b[off..]) {
Ok(t) => t.to_string(),
Err(e) if e.valid_up_to() + 4 >= b.len() - off => String::from_utf8_lossy(&b[off..off + e.valid_up_to()]).to_string(), // não corta uma letra UTF-8
Err(_) => b[off..].iter().map(|&c| c as char).collect(), // a página de código do Windows (Latin-1 aproximado)
}
};
for l in text.split('\n') {
if out.len() >= 20000 {
break;
}
out.push(l.trim_end_matches('\r').replace('\t', " "));
}
out
}
/// as linhas visíveis da folha, a partir da rolagem, e a barrinha fina à direita quando há mais texto
unsafe fn ql_text_lines(rt: &ID2D1RenderTarget, b: &ID2D1SolidColorBrush, page: RectF, top: f32, left: f32, dark: bool) {
let lh = 17.0;
if s().ql_lines.is_empty() && !s().ql_text_path.is_empty() && *QL_TEXT_LOADING.get() == s().ql_text_path {
// o texto chega pela WM_APP_QLTEXT
s().ql_text_shown = true;
s().ql_text_max = 0.0;
let mono = s().f.mono.clone();
text(rt, &tr("Carregando…"), &mono, rf(page.left + left, page.top + top, page.right - 10.0, page.top + top + lh), &br(b));
return;
}
let vis = page.bottom - page.top;
s().ql_text_max = (s().ql_lines.len() as f32 * lh + top + 12.0 - vis).max(0.0);
s().ql_text_scroll = s().ql_text_scroll.clamp(0.0, s().ql_text_max);
s().ql_text_shown = true;
let first = (s().ql_text_scroll / lh) as usize;
let mut y = page.top + top - s().ql_text_scroll + first as f32 * lh;
let mono = s().f.mono.clone();
let bb = br(b);
for ln in s().ql_lines.iter().skip(first) {
if y > page.bottom {
break;
}
text(rt, ln, &mono, rf(page.left + left, y, page.right - 10.0, y + lh), &bb);
y += lh;
}
let mx = s().ql_text_max;
if mx > 0.0 {
let tot = vis + mx;
let th = 28f32.max(vis * vis / tot);
let ty = page.top + 3.0 + (vis - 6.0 - th) * (s().ql_text_scroll / mx);
let old = b.GetColor();
fill_rr(rt, b, rf(page.right - 12.0, ty, page.right - 7.5, ty + th), 2.25, if dark { col(1.0, 1.0, 1.0, 0.35) } else { col(0.0, 0.0, 0.0, 0.3) });
b.SetColor(&old);
}
}
pub fn ql_item() -> i32 {
let f = s().focus;
if f >= 0 && (f as usize) < s().items.len() && s().items[f as usize].sel {
return f;
}
s().items.iter().position(|e| e.sel).map(|x| x as i32).unwrap_or(-1)
}
/// Nome do app padrão para o tipo ("Fotos", "Bloco de Notas"...), para o botão "Abrir com".
pub fn default_app_name(e: &Entry) -> String {
if e.dir {
return String::new();
}
// issue #117: o WM_MOUSEMOVE sobre o botão consultava o Registro a cada pixel movido; o app padrão não muda com o
// Finder aberto, então o resultado é guardado por extensão
static CACHE: Global<Option<std::collections::HashMap<String, String>>> = Global::new(None);
let chave = format!(".{}", ext_of(e));
let c = CACHE.get().get_or_insert_with(Default::default);
if let Some(r) = c.get(&chave) {
return r.clone();
}
let ext = wz(&chave);
let mut name = [0u16; 256];
let mut n = 256u32;
let mut r = String::new();
unsafe {
if AssocQueryStringW(ASSOCF_INIT_IGNOREUNKNOWN, ASSOCSTR_FRIENDLYAPPNAME, pc(&ext), PCWSTR::null(), PWSTR(name.as_mut_ptr()), &mut n).is_ok() && name[0] != 0 {
r = from_w(&name);
}
}
c.insert(chave, r.clone());
r
}
/// miniatura grande, se já chegou (senão a pequena)
pub fn ql_image(e: &Entry) -> Option<ID2D1Bitmap> {
if e.dir || !s().show_thumbs || s().kinds.contains_key(&e.key()) {
return None;
}
for key in [format!("{}|G", e.key()), e.key()] {
if let Some(Some(b)) = crate::image_for(&key) {
return Some(b);
}
}
None
}
/// áudio e vídeo: tocam no próprio painel, com a barra de controles do Mac
pub fn is_media_file(e: &Entry) -> Option<bool> {
const VIDS: [&str; 11] = ["mp4", "m4v", "mov", "mkv", "avi", "wmv", "webm", "3gp", "mts", "m2ts", "ts"];
const AUDS: [&str; 11] = ["mp3", "m4a", "aac", "wav", "flac", "wma", "ogg", "opus", "aiff", "aif", "alac"];
if e.dir {
return None;
}
let x = ext_of(e);
if VIDS.contains(&x.as_str()) {
Some(true)
} else if AUDS.contains(&x.as_str()) {
Some(false)
} else {
None
}
}
fn ql_zoom_reset() {
s().ql_zoom = 1.0;
s().ql_pan = (0.0, 0.0);
s().ql_panning = false;
}
fn ql_photo_rect(body: RectF, iw: u32, ih: u32, fill: bool) -> RectF {
let (bw, bh) = (body.right - body.left, body.bottom - body.top);
if iw == 0 || ih == 0 || bw <= 0.0 || bh <= 0.0 {
return body;
}
let (iw, ih) = (iw as f32, ih as f32);
let k = (if fill { (bw / iw).max(bh / ih) } else { (bw / iw).min(bh / ih) }) * s().ql_zoom;
let (w_, h) = (iw * k, ih * k);
let (mx, my) = (((w_ - bw) / 2.0).max(0.0), ((h - bh) / 2.0).max(0.0));
s().ql_pan.0 = s().ql_pan.0.clamp(-mx, mx);
s().ql_pan.1 = s().ql_pan.1.clamp(-my, my);
let (x, y) = (body.left + (bw - w_) / 2.0 + s().ql_pan.0, body.top + (bh - h) / 2.0 + s().ql_pan.1);
rf(x, y, x + w_, y + h)
}
fn item_prop(path: &str, key: &PROPERTYKEY) -> String {
unsafe {
let p = wz(path);
let Ok(si) = SHCreateItemFromParsingName::<_, _, IShellItem2>(pc(&p), None) else { return String::new() };
match si.GetString(key) {
Ok(v) => {
let r = from_w(v.as_wide());
CoTaskMemFree(Some(v.0 as *const c_void));
r
}
Err(_) => String::new(),
}
}
}
/// Abre/fecha o player conforme o item do Quick Look (chamado antes de cada quadro).
pub fn ql_sync_media() {
let mut want = String::new();
if s().ql {
let i = ql_item();
if i >= 0 && is_media_file(&s().items[i as usize]).is_some() {
want = s().items[i as usize].path.clone();
}
}
if want == s().ql_media_path {
return;
}
media::close();
crate::kill_timer(crate::T_MEDIA);
s().ql_seek_drag = false;
s().ql_media_path = want.clone();
s().ql_media_title.clear();
s().ql_media_artist.clear();
if want.is_empty() || crate::modo_foto() {
return;
}
// título e artista das informações do arquivo
s().ql_media_title = item_prop(&want, &PKEY_Title);
s().ql_media_artist = item_prop(&want, &PKEY_Music_Artist);
media::open(&want);
s().ql_mouse_at = unsafe { GetTickCount() };
crate::set_timer(crate::T_MEDIA, 15); // o tempo, o fim e os quadros novos
}
fn ql_media_on(e: &Entry) -> bool {
!s().ql_media_path.is_empty() && s().ql_media_path == e.path && media::active() && !media::failed()
}
fn ql_video_on(e: &Entry) -> bool {
ql_media_on(e) && media::has_video()
}
fn ql_audio_on(e: &Entry) -> bool {
let v = is_media_file(e) == Some(true);
ql_media_on(e) && if media::ready() { !media::has_video() } else { !v }
}
fn ql_controls_visible() -> bool {
s().ql_seek_drag || media::paused() || unsafe { GetTickCount() }.wrapping_sub(s().ql_mouse_at) < 2500
}
fn clock_text(t: f64) -> String {
let s_ = t.max(0.0) as i32;
if s_ >= 3600 {
format!("{}:{:02}:{:02}", s_ / 3600, s_ / 60 % 60, s_ % 60)
} else {
format!("{}:{:02}", s_ / 60, s_ % 60)
}
}
/// Tamanho do painel: fotos ganham o formato da foto; texto e outros, tamanhos fixos.
pub fn ql_panel() -> RectF {
let (sw, sh) = client_dip();
let (max_w, max_h) = ((sw - 80.0).min(960.0), (sh - 70.0).min(720.0));
let (mut w_, mut h) = (540.0, 440.0);
let th = ql_title_h();
let i = ql_item();
if i >= 0 {
let e = s().items[i as usize].clone();
if !e.dir && is_text_file(&e) {
w_ = 760.0;
h = 580.0;
} else if ql_video_on(&e) {
let (vw, vh) = (media::video_w() as f32, media::video_h() as f32);
let k = (max_w / vw).min((max_h - th) / vh).min(1.5);
w_ = 420f32.max(vw * k);
h = vh * k + th;
} else if ql_audio_on(&e) {
w_ = 460.0;
h = 430.0;
} else if let Some(bmp) = ql_image(&e) {
let sz = unsafe { bmp.GetPixelSize() };
let k = (max_w / sz.width as f32).min((max_h - th) / sz.height as f32).min(1.4); // a miniatura tem no máximo ~512 px
w_ = 420f32.max(sz.width as f32 * k);
h = sz.height as f32 * k + th;
}
}
w_ = w_.min(max_w);
h = h.min(max_h);
let (cx, cy) = (sw / 2.0, sh / 2.0 + 8.0);
rf(cx - w_ / 2.0, cy - h / 2.0, cx + w_ / 2.0, cy + h / 2.0)
}
pub fn ql_close_rect() -> RectF {
let r = ql_panel();
if !s().tahoe && crate::leopard_theme() {
return rf(r.left + 5.0, r.top + 4.0, r.left + 21.0, r.top + 20.0); // Leopard: o X redondo do HUD
}
if semaforo::aqua() {
return rf(r.left + 9.0, r.top + 11.0, r.left + 25.0, r.top + 27.0);
}
rf(r.left + 10.0, r.top + 10.0, r.left + 30.0, r.top + 30.0)
}
pub fn ql_open_rect() -> RectF {
let r = ql_panel();
if !s().tahoe && crate::leopard_theme() {
return rf(0.0, 0.0, 0.0, 0.0); // o Quick Look do 10.5 não tinha "Abrir com" nem Compartilhar
}
let i = ql_item();
let app = if i >= 0 { default_app_name(&s().items[i as usize]) } else { String::new() };
let lbl = if app.is_empty() { tr("Abrir") } else { format!("{}{app}", tr("Abrir com ")) };
let w_ = (text_width(&lbl, &s().f.ql_button) + 22.0).clamp(60.0, 220.0);
if semaforo::aqua() {
return rf(r.right - 7.0 - w_ - 8.0, r.top + 6.0, r.right - 7.0, r.top + 32.0);
}
rf(r.right - 12.0 - w_, r.top + 8.0, r.right - 12.0, r.top + 32.0)
}
pub fn ql_share_rect() -> RectF {
let o = ql_open_rect();
if o.right <= o.left {
return rf(0.0, 0.0, 0.0, 0.0);
}
rf(o.left - 36.0, o.top, o.left - 8.0, o.bottom)
}
fn ql_ctl_bar() -> RectF {
let r = ql_panel();
let w_ = (r.right - r.left - 32.0).min(560.0);
let cx = (r.left + r.right) / 2.0;
rf(cx - w_ / 2.0, r.bottom - 54.0, cx + w_ / 2.0, r.bottom - 14.0)
}
fn ql_play_rect() -> RectF {
let b = ql_ctl_bar();
rf(b.left + 6.0, b.top + 4.0, b.left + 38.0, b.bottom - 4.0)
}
fn ql_mute_rect() -> RectF {
let b = ql_ctl_bar();
rf(b.right - 38.0, b.top + 4.0, b.right - 6.0, b.bottom - 4.0)
}
fn ql_track_rect() -> RectF {
let b = ql_ctl_bar();
rf(b.left + 38.0 + 52.0, b.top, b.right - 38.0 - 56.0, b.bottom)
}
pub fn ql_seek_at(x: f32) {
let t = ql_track_rect();
let d = media::duration();
if d > 0.0 {
media::seek(d * ((x - t.left) / (t.right - t.left)).clamp(0.0, 1.0) as f64);
}
crate::invalidate();
}
/// barra de controles: tocar/pausar, tempo, linha do tempo, tempo restante, som
unsafe fn draw_ql_controls(rt: &ID2D1RenderTarget, pal: &Palette, b: &ID2D1SolidColorBrush, over_video: bool) {
let bar = ql_ctl_bar();
let fg = if over_video { col(1.0, 1.0, 1.0, 0.95) } else { pal.text };
let dim = if over_video { col(1.0, 1.0, 1.0, 0.65) } else { pal.text2 };
if over_video {
fill_rr(rt, b, bar, 10.0, col(0.12, 0.12, 0.13, 0.72));
stroke_rr(rt, b, inflate(bar, -0.5), 9.5, col(1.0, 1.0, 1.0, 0.1), 1.0);
}
let bb = br(b);
let g = s().f.ql_glyph.clone();
b.SetColor(&fg);
draw_glyph(rt, if media::paused() { 0xE768 } else { 0xE769 }, ql_play_rect(), &bb, Some(&g));
draw_glyph(rt, if media::muted() { 0xE74F } else { 0xE767 }, ql_mute_rect(), &bb, Some(&g));
let d = media::duration();
let t = if media::ended() { d } else { media::time() };
let tr_ = ql_track_rect();
let tf = s().f.ql_time.clone();
b.SetColor(&dim);
text(rt, &clock_text(t), &tf, rf(ql_play_rect().right, bar.top, tr_.left - 4.0, bar.bottom), &bb);
text(rt, &format!("-{}", clock_text(d - t)), &tf, rf(tr_.right + 4.0, bar.top, ql_mute_rect().left, bar.bottom), &bb);
let cy = (bar.top + bar.bottom) / 2.0;
let f = if d > 0.0 { (t / d).clamp(0.0, 1.0) as f32 } else { 0.0 };
let x = tr_.left + (tr_.right - tr_.left) * f;
fill_rr(rt, b, rf(tr_.left, cy - 2.0, tr_.right, cy + 2.0), 2.0, if over_video { col(1.0, 1.0, 1.0, 0.25) } else { pal.line });
fill_rr(rt, b, rf(tr_.left, cy - 2.0, x, cy + 2.0), 2.0, fg);
rt.FillEllipse(&ell(pt(x, cy), 5.5, 5.5), b);
}
/// volta para o ícone; o timer desenha e no fim some
pub fn ql_close() {
if !s().ql || s().ql_closing {
return;
}
s().ql_closing = true;
s().ql_closed_at = unsafe { GetTickCount() };
crate::set_timer(crate::T_QL, 15);
crate::invalidate();
}
/// Golden Gate: o Quick Look é o do Shell (janela própria de vidro fosco, por cima de tudo, como no Mac)
fn ql_shell_wnd() -> Option<HWND> {
find_window("ShellQuickLook").filter(|h| unsafe { IsWindowVisible(*h).as_bool() })
}
fn ql_send(p: &str) -> bool {
if crate::modo_foto() {
return false;
}
let Some(sh) = find_window("ShellMensagens") else { return false };
let pw = wz(p);
let cd = COPYDATASTRUCT { dwData: 0x514C4F4B, cbData: (pw.len() * 2) as u32, lpData: pw.as_ptr() as *mut c_void };
let mut r = 0usize;
unsafe {
if SendMessageTimeoutW(sh, WM_COPYDATA, WPARAM(s().hwnd.0 as usize), LPARAM(&cd as *const _ as isize), SMTO_ABORTIFHUNG, 800, Some(&mut r)).0 == 0 || r == 0 {
return false;
}
}
s().ql_ext_path = p.into();
true
}
/// a seleção mudou com o Quick Look do Shell aberto: mostra o novo
pub fn ql_sync_shell() {
if s().ql_ext_path.is_empty() {
return;
}
if ql_shell_wnd().is_none() {
s().ql_ext_path.clear();
return;
}
let i = ql_item();
if i < 0 || s().items[i as usize].path.eq_ignore_ascii_case(&s().ql_ext_path) {
return;
}
let p = s().items[i as usize].path.clone();
ql_send(&p);
}
/// PDF, SVG, Word/PowerPoint/Excel e fontes: o Quick Look do Shell desenha a página (issue #8)
fn ql_needs_shell(e: &Entry) -> bool {
!e.dir && ["pdf", "svg", "svgz", "doc", "docx", "docm", "dot", "dotx", "odt", "ppt", "pptx", "pptm", "pps", "ppsx", "odp", "xls", "xlsx", "xlsm", "ods", "vsd", "vsdx", "pub", "ttf", "otf", "ttc"].contains(&ext_of(e).as_str())
}
pub fn ql_toggle() {
if !s().ql_ext_path.is_empty() && ql_shell_wnd().is_some() {
ql_send("");
return;
}
if s().ql {
ql_close();
return;
}
let i = ql_item();
if i < 0 {
return;
}
let e = s().items[i as usize].clone();
if (crate::golden() || ql_needs_shell(&e)) && ql_send(&e.path) {
return; // tudo no Quick Look do Shell (vídeo e música também)
}
s().ql = true;
s().ql_closing = false;
let mut from = icon_doc(i as usize);
let c = content_rect();
let fv = to_view(from);
if fv.bottom < c.top || fv.top > c.bottom || from.right - from.left < 4.0 {
from = rf(0.0, 0.0, 0.0, 0.0); // o ícone fora da área visível (rolou): cresce do centro
} else {
from = fv;
}
s().ql_from = from;
s().ql_opened_at = unsafe { GetTickCount() };
crate::set_timer(crate::T_QL, 15); // anima a abertura
crate::invalidate();
}
fn fit(body: RectF, vw: f32, vh: f32) -> RectF {
let k = ((body.right - body.left) / vw).min((body.bottom - body.top) / vh);
let (w_, h) = (vw * k, vh * k);
let (cx, cy) = ((body.left + body.right) / 2.0, (body.top + body.bottom) / 2.0);
rf(cx - w_ / 2.0, cy - h / 2.0, cx + w_ / 2.0, cy + h / 2.0)
}
/// Mac OS X Leopard: o Quick Look do 10.5 — um HUD preto translúcido de cantos redondos com o fio claro, o título branco
/// no meio da faixa de cima, o X branco num círculo cinza à esquerda, a prévia no corpo e, nos vídeos, a barra escura
unsafe fn draw_quick_look_leopard(rt: &ID2D1RenderTarget, b: &ID2D1SolidColorBrush, e: &Entry, r: RectF) {
for k in 1..=8 {
let k = k as f32; // sombra
fill_rr(rt, b, rf(r.left - k * 2.0, r.top - k + 8.0, r.right + k * 2.0, r.bottom + k * 2.5 + 8.0), 10.0 + k * 2.0, col(0.0, 0.0, 0.0, 0.035));
}
let rr_ = 10.0;
fill_rr(rt, b, r, rr_, col(0.07, 0.07, 0.08, 0.88));
stroke_rr(rt, b, inflate(r, -0.5), rr_ - 0.5, col(1.0, 1.0, 1.0, 0.3), 1.0);
let bb = br(b);
let (tft, tfi) = (s().f.leo_ql_title.clone(), s().f.leo_ql_info.clone());
let th = ql_title_h();
b.SetColor(&col(1.0, 1.0, 1.0, 0.92));
text(rt, &e.label, &tft, rf(r.left + 30.0, r.top, r.right - 30.0, r.top + th), &bb);
let cr = ql_close_rect();
let (ccx, ccy) = ((cr.left + cr.right) / 2.0, (cr.top + cr.bottom) / 2.0);
b.SetColor(&col(0.42, 0.42, 0.44, 1.0));
rt.FillEllipse(&ell(pt(ccx, ccy), 6.5, 6.5), b);
b.SetColor(&col(1.0, 1.0, 1.0, 0.55));
rt.DrawEllipse(&ell(pt(ccx, ccy), 6.5, 6.5), b, 0.8, None);
b.SetColor(&col(0.07, 0.07, 0.08, 1.0));
line(rt, &bb, ccx - 2.4, ccy - 2.4, ccx + 2.4, ccy + 2.4, 1.5);
line(rt, &bb, ccx - 2.4, ccy + 2.4, ccx + 2.4, ccy - 2.4, 1.5);
let body = rf(r.left + 8.0, r.top + th, r.right - 8.0, r.bottom - 8.0);
let bmp = ql_image(e);
let txt = !e.dir && is_text_file(e);
let video = ql_video_on(e);
let audio = !video && ql_audio_on(e);
if video {
let vr = fit(body, media::video_w().max(1) as f32, media::video_h().max(1) as f32);
if let Some(fr) = media::frame().or(bmp) {
rt.DrawBitmap(&fr, Some(&vr), 1.0, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, None);
}
} else if bmp.is_some() && !audio {
let sz = bmp.as_ref().unwrap().GetPixelSize();
draw_big_icon(rt, e, fit(body, sz.width as f32, sz.height as f32), b);
} else if txt {
load_ql_text(&e.path);
let page = rf(body.left + 6.0, body.top + 2.0, body.right - 6.0, body.bottom - 2.0);
b.SetColor(&col(1.0, 1.0, 1.0, 1.0));
rt.FillRectangle(&page, b);
rt.PushAxisAlignedClip(&inflate(page, -8.0), D2D1_ANTIALIAS_MODE_ALIASED);
b.SetColor(&col(0.0, 0.0, 0.0, 0.9));
ql_text_lines(rt, b, page, 10.0, 12.0, false);
rt.PopAxisAlignedClip();
} else {
// ícone grande e, embaixo, o nome e as informações em branco
let size = 200f32.min((body.bottom - body.top) - 110.0);
let cx = (body.left + body.right) / 2.0;
let top = body.top + 8f32.max(((body.bottom - body.top) - size - 90.0) / 2.0);
match (&bmp, audio) {
(Some(bm), true) => {
let sz = bm.GetPixelSize();
let k = (size / sz.width as f32).min(size / sz.height as f32);
let (w_, h) = (sz.width as f32 * k, sz.height as f32 * k);
rt.DrawBitmap(bm, Some(&rf(cx - w_ / 2.0, top + (size - h) / 2.0, cx + w_ / 2.0, top + (size + h) / 2.0)), 1.0, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, None);
}
_ => draw_big_icon(rt, e, rf(cx - size / 2.0, top, cx + size / 2.0, top + size), b),
}
let y = top + size + 14.0;
let name = if audio && !s().ql_media_title.is_empty() { s().ql_media_title.clone() } else { e.label.clone() };
b.SetColor(&col(1.0, 1.0, 1.0, 0.95));
text(rt, &name, &tft, rf(body.left + 10.0, y, body.right - 10.0, y + 20.0), &bb);
let mut info = if audio { s().ql_media_artist.clone() } else { kind_name(e) };
if !audio {
if e.dir {
let n = child_items(&e.path).len();
info += &format!(" · {n}{}", tr(if n == 1 { " item" } else { " itens" }));
} else {
info += &format!(" · {}", size_text(e));
}
}
b.SetColor(&col(1.0, 1.0, 1.0, 0.6));
text(rt, &info, &tfi, rf(body.left, y + 22.0, body.right, y + 38.0), &bb);
if !audio {
text(rt, &format!("{}{}", tr("Modificado: "), date_text(e.mtime)), &tfi, rf(body.left, y + 38.0, body.right, y + 54.0), &bb);
}
}
if (video && ql_controls_visible()) || audio {
// os controles: barra escura com ⏵/⏸, tempos, linha do tempo e o som
let bar = ql_ctl_bar();
fill_rr(rt, b, bar, 8.0, col(0.0, 0.0, 0.0, 0.62));
stroke_rr(rt, b, inflate(bar, -0.5), 7.5, col(1.0, 1.0, 1.0, 0.25), 1.0);
let pr = ql_play_rect();
let (pcx, pcy) = ((pr.left + pr.right) / 2.0, (pr.top + pr.bottom) / 2.0);
b.SetColor(&col(1.0, 1.0, 1.0, 0.95));
if media::paused() {
fill_poly(rt, &bb, &[pt(pcx - 4.0, pcy - 6.0), pt(pcx + 6.0, pcy), pt(pcx - 4.0, pcy + 6.0)]);
} else {
rt.FillRectangle(&rf(pcx - 5.0, pcy - 6.0, pcx - 1.5, pcy + 6.0), b);
rt.FillRectangle(&rf(pcx + 1.5, pcy - 6.0, pcx + 5.0, pcy + 6.0), b);
}
let mr = ql_mute_rect();
let (mcx, mcy) = ((mr.left + mr.right) / 2.0, (mr.top + mr.bottom) / 2.0); // alto-falante
rt.FillRectangle(&rf(mcx - 6.0, mcy - 2.5, mcx - 3.0, mcy + 2.5), b);
line(rt, &bb, mcx - 3.0, mcy - 2.5, mcx + 1.0, mcy - 6.0, 1.6);
line(rt, &bb, mcx + 1.0, mcy - 6.0, mcx + 1.0, mcy + 6.0, 1.6);
line(rt, &bb, mcx + 1.0, mcy + 6.0, mcx - 3.0, mcy + 2.5, 1.6);
if !media::muted() {
rt.DrawEllipse(&ell(pt(mcx + 2.0, mcy), 4.5, 4.5), b, 1.2, None);
}
let d = media::duration();
let t = if media::ended() { d } else { media::time() };
let tr_ = ql_track_rect();
let ty = (tr_.top + tr_.bottom) / 2.0;
let f = if d > 0.0 { (t / d).clamp(0.0, 1.0) as f32 } else { 0.0 };
fill_rr(rt, b, rf(tr_.left, ty - 2.0, tr_.right, ty + 2.0), 2.0, col(1.0, 1.0, 1.0, 0.28));
fill_rr(rt, b, rf(tr_.left, ty - 2.0, tr_.left + (tr_.right - tr_.left) * f, ty + 2.0), 2.0, col(1.0, 1.0, 1.0, 0.95));
rt.FillEllipse(&ell(pt(tr_.left + (tr_.right - tr_.left) * f, ty), 4.5, 4.5), b);
text(rt, &clock_text(t), &tfi, rf(pr.right, tr_.top, tr_.left - 4.0, tr_.bottom), &bb);
text(rt, &format!("-{}", clock_text(d - t)), &tfi, rf(tr_.right + 4.0, tr_.top, mr.left, tr_.bottom), &bb);
}
}
pub unsafe fn draw_quick_look(rt: &ID2D1RenderTarget, pal: &Palette, b: &ID2D1SolidColorBrush) {
s().ql_text_shown = false; // a folha de texto marca de novo se for desenhada
let i = ql_item();
if i < 0 {
s().ql = false;
return;
}
let e = s().items[i as usize].clone();
let r = ql_panel();
let now = GetTickCount();
let foto = crate::modo_foto();
let mut ease = if s().ql_closing {
// fechando: acelera de volta para o ícone
let t = (now.wrapping_sub(s().ql_closed_at) as f32 / QL_CLOSE_MS).min(1.0);
if t >= 1.0 {
s().ql = false;
s().ql_closing = false;
return;
}
1.0 - t * t
} else if foto {
1.0
} else {
let t = (now.wrapping_sub(s().ql_opened_at) as f32 / QL_ANIM_MS).min(1.0);
let u = 1.0 - t;
1.0 - u * u * u * u // sai rápido e assenta
};
let mid = pt((r.left + r.right) / 2.0, (r.top + r.bottom) / 2.0);
let mut old = Default::default();
rt.GetTransform(&mut old);
let (pw, ph) = (r.right - r.left, r.bottom - r.top);
let from = s().ql_from;
if from.right > from.left && pw > 1.0 && ph > 1.0 {
// do ícone até o painel: escala e centro andam juntos
let s0 = ((from.right - from.left) / pw).max((from.bottom - from.top) / ph);
let scale = s0 + (1.0 - s0) * ease;
let (fx, fy) = ((from.left + from.right) / 2.0, (from.top + from.bottom) / 2.0);
let (cx, cy) = (fx + (mid.x - fx) * ease, fy + (mid.y - fy) * ease);
rt.SetTransform(&mat::mul(&mat::mul(&mat::scale(scale, scale, mid.x, mid.y), &mat::trans(cx - mid.x, cy - mid.y)), &old));
} else {
let scale = 0.94 + 0.06 * ease;
rt.SetTransform(&mat::mul(&mat::scale(scale, scale, mid.x, mid.y), &old));
}
ease = (ease * 1.6).min(1.0); // a opacidade chega antes do tamanho
let layered = ease < 1.0;
if layered {
let lp = D2D1_LAYER_PARAMETERS { contentBounds: rf(-1e7, -1e7, 1e7, 1e7), geometricMask: std::mem::ManuallyDrop::new(None), maskAntialiasMode: D2D1_ANTIALIAS_MODE_PER_PRIMITIVE, maskTransform: mat::ident(), opacity: ease, opacityBrush: std::mem::ManuallyDrop::new(None), layerOptions: D2D1_LAYER_OPTIONS_NONE };
rt.PushLayer(&lp, None);
}
let end = |rt: &ID2D1RenderTarget| {
if layered {
rt.PopLayer();
}
rt.SetTransform(&old);
};
if !s().tahoe && crate::leopard_theme() {
draw_quick_look_leopard(rt, b, &e, r);
end(rt);
return;
}
for k in 1..=10 {
let k = k as f32; // sombra grande e suave, como as janelas do Mac
fill_rr(rt, b, rf(r.left - k * 2.5, r.top - k * 1.5 + 10.0, r.right + k * 2.5, r.bottom + k * 3.0 + 10.0), 12.0 + k * 2.0, col(0.0, 0.0, 0.0, 0.03));
}
let gg = semaforo::aqua();
let dark = s().dark;
let bg = if gg {
if dark {
col(0.18, 0.18, 0.19, 1.0)
} else {
col(0.945, 0.94, 0.935, 1.0)
}
} else if dark {
col(0.11, 0.11, 0.12, 1.0)
} else {
col(0.97, 0.97, 0.975, 1.0)
};
let th = ql_title_h();
fill_rr(rt, b, r, if gg { 16.0 } else { 12.0 }, bg);
if gg {
// vidro: a barra escurece no meio e clareia em cima e perto da foto, fio claro em cima
let f1 = if dark { col(0.13, 0.13, 0.14, 1.0) } else { col(0.8, 0.78, 0.76, 1.0) };
if let Some(lb) = gradient(rt, &[(0.0, bg), (0.45, f1), (1.0, bg)], pt(0.0, r.top), pt(0.0, r.top + th)) {
rt.FillRoundedRectangle(&rr(rf(r.left, r.top, r.right, r.top + th + 16.0), 16.0, 16.0), &lb);
}
b.SetColor(&bg);
rt.FillRectangle(&rf(r.left, r.top + th, r.right, r.top + th + 16.0), b);
stroke_rr(rt, b, inflate(r, -0.5), 15.5, col(1.0, 1.0, 1.0, if dark { 0.18 } else { 0.7 }), 1.0);
}
let bmp = ql_image(&e);
let txt = !e.dir && is_text_file(&e);
let video = ql_video_on(&e);
let audio = !video && ql_audio_on(&e);
let body = if gg { rf(r.left + 5.0, r.top + th, r.right - 5.0, r.bottom - 5.0) } else { rf(r.left, r.top + th, r.right, r.bottom) };
let bb = br(b);
if video {
// vídeo: quadro atual (ou a miniatura enquanto o primeiro não chega) no corpo todo
if crate::push_round_clip(rt, r, 12.0) {
b.SetColor(&col(0.0, 0.0, 0.0, 1.0));
rt.FillRectangle(&body, b);
let vr = fit(body, media::video_w() as f32, media::video_h() as f32);
if let Some(fr) = media::frame().or(bmp.clone()) {
rt.DrawBitmap(&fr, Some(&vr), 1.0, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, None);
}
if ql_controls_visible() {
draw_ql_controls(rt, pal, b, true);
}
rt.PopLayer();
}
} else if audio {
// música: capa (ou ícone), título, artista e controles
let (size, cx, top) = (210.0, (body.left + body.right) / 2.0, body.top + 26.0);
let art = rf(cx - size / 2.0, top, cx + size / 2.0, top + size);
if let Some(bm) = &bmp {
let sz = bm.GetPixelSize();
let k = (size / sz.width as f32).min(size / sz.height as f32);
let (w_, h) = (sz.width as f32 * k, sz.height as f32 * k);
let ar = rf(cx - w_ / 2.0, top + (size - h) / 2.0, cx + w_ / 2.0, top + (size + h) / 2.0);
for s2 in 1..=4 {
fill_rr(rt, b, inflate(ar, s2 as f32 * 1.5), 6.0 + s2 as f32, col(0.0, 0.0, 0.0, 0.05));
}
rt.DrawBitmap(bm, Some(&ar), 1.0, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, None);
} else {
draw_big_icon(rt, &e, art, b);
}
let title = if s().ql_media_title.is_empty() { e.label.clone() } else { s().ql_media_title.clone() };
let y = art.bottom + 18.0;
b.SetColor(&pal.text);
text(rt, &title, &s().f.ql_title.clone(), rf(body.left + 24.0, y, body.right - 24.0, y + 20.0), &bb);
if !s().ql_media_artist.is_empty() {
b.SetColor(&pal.text2);
text(rt, &s().ql_media_artist.clone(), &s().f.ql_artist.clone(), rf(body.left + 24.0, y + 22.0, body.right - 24.0, y + 42.0), &bb);
}
draw_ql_controls(rt, pal, b, false);
} else if let Some(bm) = &bmp {
// foto: ocupa o corpo todo; macOS 27: a foto preenche a moldura; nos outros, cabe nela. A roda amplia.
if crate::push_round_clip(rt, if gg { body } else { r }, 12.0) {
let sz = bm.GetPixelSize();
if s().ql_zoom_path != e.path {
ql_zoom_reset();
s().ql_zoom_path = e.path.clone();
}
s().ql_body = body;
s().ql_zoom_fill = gg;
s().ql_zoom_wh = (sz.width, sz.height);
draw_big_icon(rt, &e, ql_photo_rect(body, sz.width, sz.height, gg), b);
rt.PopLayer();
}
} else if txt {
// texto: folha clara com fonte monoespaçada
load_ql_text(&e.path);
let page = rf(body.left + 14.0, body.top + 4.0, body.right - 14.0, body.bottom - 14.0);
fill_rr(rt, b, page, 6.0, if dark { col(0.16, 0.16, 0.17, 1.0) } else { col(1.0, 1.0, 1.0, 1.0) });
rt.PushAxisAlignedClip(&inflate(page, -10.0), D2D1_ANTIALIAS_MODE_ALIASED);
b.SetColor(&pal.text);
ql_text_lines(rt, b, page, 12.0, 14.0, dark);
rt.PopAxisAlignedClip();
} else {
// pasta ou arquivo sem prévia: ícone grande e informações
let size = 200f32.min((body.bottom - body.top) - 120.0);
let cx = (body.left + body.right) / 2.0;
let top = body.top + 8f32.max(((body.bottom - body.top) - size - 100.0) / 2.0);
draw_big_icon(rt, &e, rf(cx - size / 2.0, top, cx + size / 2.0, top + size), b);
let y = top + size + 16.0;
let (pn, pi) = (s().f.preview_name.clone(), s().f.preview_info.clone());
b.SetColor(&pal.text);
text(rt, &e.label, &pn, rf(body.left + 20.0, y, body.right - 20.0, y + 40.0), &bb);
let mut info = kind_name(&e);
if e.dir {
let n = child_items(&e.path).len();
info += &format!(" · {n}{}", tr(if n == 1 { " item" } else { " itens" }));
} else {
info += &format!(" · {}", size_text(&e));
}
b.SetColor(&pal.text2);
text(rt, &info, &pi, rf(body.left, y + 42.0, body.right, y + 60.0), &bb);
text(rt, &format!("{}{}", tr("Modificado: "), date_text(e.mtime)), &pi, rf(body.left, y + 60.0, body.right, y + 78.0), &bb);
}
// barra de título (por cima da foto, translúcida, como no Mac)
let bar = rf(r.left, r.top, r.right, r.top + th);
if !gg {
rt.PushAxisAlignedClip(&bar, D2D1_ANTIALIAS_MODE_ALIASED);
let a_ = if bmp.is_some() || video { 0.86 } else { 1.0 };
fill_rr(rt, b, r, 12.0, if dark { col(0.19, 0.19, 0.2, a_) } else { col(0.93, 0.93, 0.935, a_) });
rt.PopAxisAlignedClip();
b.SetColor(&pal.line);
rt.DrawLine(pt(r.left, bar.bottom - 0.5), pt(r.right, bar.bottom - 0.5), b, 1.0, None);
}
let cr = ql_close_rect();
let (ccx, ccy) = ((cr.left + cr.right) / 2.0, (cr.top + cr.bottom) / 2.0);
if gg {
// macOS 27: círculo escuro cheio com o × claro
b.SetColor(&if dark { col(1.0, 1.0, 1.0, 0.75) } else { col(0.24, 0.22, 0.21, 0.9) });
rt.FillEllipse(&ell(pt(ccx, ccy), 8.0, 8.0), b);
b.SetColor(&bg);
line(rt, &bb, ccx - 2.8, ccy - 2.8, ccx + 2.8, ccy + 2.8, 1.6);
line(rt, &bb, ccx - 2.8, ccy + 2.8, ccx + 2.8, ccy - 2.8, 1.6);
} else {
b.SetColor(&if dark { col(1.0, 1.0, 1.0, 0.16) } else { col(0.0, 0.0, 0.0, 0.1) });
rt.FillEllipse(&ell(pt(ccx, ccy), 9.0, 9.0), b);
b.SetColor(&pal.text2);
line(rt, &bb, ccx - 3.0, ccy - 3.0, ccx + 3.0, ccy + 3.0, 1.5);
line(rt, &bb, ccx - 3.0, ccy + 3.0, ccx + 3.0, ccy - 3.0, 1.5);
}
let (orr, sr) = (ql_open_rect(), ql_share_rect());
b.SetColor(&pal.text);
let tft = if gg { s().f.gg_ql_title.clone() } else { s().f.ql_title.clone() };
text(rt, &e.label, &tft, rf(cr.right + if gg { 9.0 } else { 12.0 }, bar.top, sr.left - 8.0, bar.bottom), &bb);
fill_rr(rt, b, orr, if gg { 13.0 } else { 6.0 }, if dark { col(1.0, 1.0, 1.0, 0.12) } else { col(0.0, 0.0, 0.0, 0.07) });
let app = default_app_name(&e);
let open_label = if e.dir || app.is_empty() { tr("Abrir") } else { format!("{}{app}", tr("Abrir com ")) };
b.SetColor(&pal.text);
text(rt, &open_label, &s().f.ql_button.clone(), orr, &bb);
b.SetColor(&pal.text2);
draw_tool_icon(rt, TB_SHARE, sr, &bb);
end(rt);
}
/// a foto do Quick Look aberta agora (não texto, vídeo, ícone): a roda amplia
fn ql_zoomable() -> bool {
if !s().ql || s().ql_closing || (!s().tahoe && crate::leopard_theme()) {
return false;
}
let i = ql_item();
if i < 0 {
return false;
}
let e = s().items[i as usize].clone();
!e.dir && !is_text_file(&e) && !ql_media_on(&e) && is_media_file(&e).is_none() && ql_image(&e).is_some() && s().ql_zoom_path == e.path && s().ql_zoom_wh.0 > 0 && s().ql_body.right > s().ql_body.left
}
/// true: a roda foi do zoom
pub fn ql_wheel(p: Ponto, delta: i32) -> bool {
if !ql_zoomable() || !inr(&ql_panel(), p) {
return false;
}
let z0 = s().ql_zoom;
let z1 = (z0 * 1.0018f32.powi(delta)).clamp(1.0, 8.0);
if z1 == z0 {
return true;
}
let (body, (w_, h), fill) = (s().ql_body, s().ql_zoom_wh, s().ql_zoom_fill);
let a_ = ql_photo_rect(body, w_, h, fill);
let fx = (p.x - a_.left) / (a_.right - a_.left).max(1.0);
let fy = (p.y - a_.top) / (a_.bottom - a_.top).max(1.0);
s().ql_zoom = z1;
let b = ql_photo_rect(body, w_, h, fill); // o ponto sob o mouse fica parado
s().ql_pan.0 += (p.x - fx * (b.right - b.left)) - b.left;
s().ql_pan.1 += (p.y - fy * (b.bottom - b.top)) - b.top;
if s().ql_zoom <= 1.001 {
ql_zoom_reset();
}
crate::invalidate();
true
}
/// Clique com o Quick Look aberto: true quando o clique foi no painel (consumido).
pub fn ql_click(p: Ponto) -> bool {
if !s().ql || !inr(&ql_panel(), p) {
return false; // fora do painel: o Finder segue normal (como no Mac)
}
if ql_zoomable() && s().ql_zoom > 1.001 && inr(&s().ql_body, p) {
// ampliada: arrastar move a foto
s().ql_panning = true;
s().ql_pan_from = p;
s().ql_pan0 = s().ql_pan;
unsafe {
SetCapture(s().hwnd);
}
return true;
}
let i = ql_item();
let panel = ql_panel();
if i >= 0 && ql_media_on(&s().items[i as usize].clone()) && !inr(&rf(panel.left, panel.top, panel.right, panel.top + ql_title_h()), p) {
let vid = ql_video_on(&s().items[i as usize].clone());
s().ql_mouse_at = unsafe { GetTickCount() };
let bar = inr(&ql_ctl_bar(), p) && (!vid || ql_controls_visible());
if bar && inr(&ql_play_rect(), p) {
media::toggle_play();
} else if bar && inr(&ql_mute_rect(), p) {
media::set_muted(!media::muted());
} else if bar && inr(&inflate(ql_track_rect(), 2.0), p) {
s().ql_seek_drag = true;
unsafe {
SetCapture(s().hwnd);
}
ql_seek_at(p.x);
} else if vid && !bar {
media::toggle_play(); // clique no vídeo pausa/continua
}
crate::invalidate();
return true;
}
if inr(&ql_close_rect(), p) {
ql_close();
} else if i >= 0 && inr(&ql_open_rect(), p) {
s().ql = false;
crate::vistas::open(i);
} else if i >= 0 && inr(&ql_share_rect(), p) {
crate::acoes::run_verb("windows.modernshare");
}
true
}
pub fn ql_pan_move(p: Ponto) {
s().ql_pan = (s().ql_pan0.0 + p.x - s().ql_pan_from.x, s().ql_pan0.1 + p.y - s().ql_pan_from.y);
crate::invalidate();
}