A new style desktop shell for Windows 11: menu bar, Dock, Finder, Spotlight, Control Center and Liquid Glass, in native C++.
Rust
//! main.cpp (6ª parte): barra de rolagem do Mac, campo de renomear, controle de tamanho dos ícones, texto da barra de
//! status e barra de caminho.
#![allow(dead_code)]
use comum::windows::Win32::Graphics::Direct2D::*;
use comum::windows::Win32::Graphics::DirectWrite::*;
use comum::windows::Win32::System::SystemInformation::GetTickCount;
use comum::*;
use comum::windows::core::Interface;
use crate::base::*;
use crate::desenho::*;
use crate::estado::*;
use crate::pasta::*;
/// Barra de rolagem do Mac: aparece ao rolar e com o mouse na borda direita; dá para clicar e arrastar
pub fn scroll_thumb() -> Option<(RectF, RectF)> {
let max_s = max_scroll();
if max_s <= 0.0 || s().view == View::Gallery {
return None;
}
let c = content_rect();
let h = c.bottom - c.top;
let th = 30f32.max(h * h / doc_height());
let y = c.top + 3.0 + (h - 6.0 - th) * (s().scroll / max_s);
Some((rf(c.right - 15.0, y, c.right, y + th), rf(c.right - 15.0, c.top, c.right, c.bottom)))
}
/// top: posição desejada do polegar
pub fn scroll_to_thumb_y(top: f32) {
let Some((thumb, _)) = scroll_thumb() else { return };
let c = content_rect();
let (h, th) = (c.bottom - c.top, thumb.bottom - thumb.top);
let k = ((top - c.top - 3.0) / (h - 6.0 - th).max(1.0)).clamp(0.0, 1.0);
let v = k * max_scroll();
s().scroll = v;
s().scroll_target = v;
s().scroll_shown_at = unsafe { GetTickCount() };
crate::set_timer(crate::T_SCROLLBAR, 50);
crate::invalidate();
}
pub unsafe fn draw_scrollbar(rt: &ID2D1RenderTarget, pal: &Palette, b: &ID2D1SolidColorBrush) {
if !s().tahoe && crate::leopard_theme() {
let Some((thumb, track)) = scroll_thumb() else { return };
let lin = |r: RectF, st: &[(f32, Cor)], rad: f32| {
if let Some(lb) = gradient(rt, st, pt(r.left, 0.0), pt(r.right, 0.0)) {
rt.FillRoundedRectangle(&rr(r, rad, rad), &lb);
}
};
let tr_ = rf(track.right - 15.0, track.top, track.right, track.bottom);
lin(tr_, &[(0.0, col(0.8, 0.8, 0.8, 1.0)), (0.35, col(0.95, 0.95, 0.95, 1.0)), (1.0, col(0.99, 0.99, 0.99, 1.0))], 0.0);
b.SetColor(&col(0.0, 0.0, 0.0, 0.18));
rt.FillRectangle(&rf(tr_.left, tr_.top, tr_.left + 1.0, tr_.bottom), b);
let th = rf(tr_.left + 1.5, thumb.top + 1.0, tr_.right - 1.5, thumb.bottom - 1.0);
let on = s().win_active; // atrás: polegar cinza (como no Mac)
let w_ = th.right - th.left;
lin(th, &[(0.0, if on { col(0.3, 0.5, 0.84, 1.0) } else { col(0.62, 0.62, 0.64, 1.0) }), (0.5, if on { col(0.7, 0.84, 0.99, 1.0) } else { col(0.9, 0.9, 0.91, 1.0) }), (1.0, if on { col(0.36, 0.57, 0.88, 1.0) } else { col(0.66, 0.66, 0.68, 1.0) })], w_ / 2.0);
let w3 = w_ * 0.3; // o brilho de gel do lado esquerdo
fill_rr(rt, b, rf(th.left + 1.5, th.top + 4.0, th.left + 1.5 + w3, th.bottom - 4.0), w3 / 2.0, col(1.0, 1.0, 1.0, 0.55));
stroke_rr(rt, b, th, w_ / 2.0, if on { col(0.15, 0.3, 0.6, 0.85) } else { col(0.4, 0.4, 0.42, 0.85) }, 0.8);
return;
}
let wide = s().scroll_drag || s().scroll_hover;
let since = if wide { 0 } else { GetTickCount().wrapping_sub(s().scroll_shown_at) };
let Some((thumb, track)) = scroll_thumb() else { return };
if since >= 1200 || crate::modo_foto() {
return;
}
let alpha = if since < 900 { 1.0 } else { 1.0 - (since - 900) as f32 / 300.0 };
if wide {
// trilho visível com o mouse em cima, como no Mac
b.SetColor(&col(pal.text.r, pal.text.g, pal.text.b, 0.06));
rt.FillRectangle(&rf(track.left + 1.0, track.top, track.right, track.bottom), b);
}
let w_ = if wide { 9.0 } else { 6.0 };
fill_rr(rt, b, rf(thumb.right - 3.0 - w_, thumb.top, thumb.right - 3.0, thumb.bottom), w_ / 2.0, col(pal.text.r, pal.text.g, pal.text.b, (if wide { 0.55 } else { 0.4 }) * alpha));
}
pub unsafe fn draw_rename_field(rt: &ID2D1RenderTarget, pal: &Palette, b: &ID2D1SolidColorBrush) {
if s().rename_idx < 0 || s().rename_idx as usize >= s().items.len() {
return;
}
let r = rename_rect();
fill_rr(rt, b, r, 4.0, if s().dark { col(0.17, 0.17, 0.18, 1.0) } else { col(1.0, 1.0, 1.0, 1.0) });
stroke_rr(rt, b, inflate(r, 1.0), 5.0, col(pal.accent.r, pal.accent.g, pal.accent.b, 0.8), 2.0);
if let (Ok(sel), Ok(txt)) = (rt.CreateSolidColorBrush(&col(pal.accent.r, pal.accent.g, pal.accent.b, 0.3), None), rt.CreateSolidColorBrush(&pal.text, None)) {
let (sel, txt): (ID2D1Brush, ID2D1Brush) = (sel.cast().unwrap(), txt.cast().unwrap());
let tf = s().f.rename.clone();
let caret = s().caret_on;
let mut f = std::mem::take(&mut s().rename);
f.draw(rt, app2d::dw(), &tf, rf(r.left + 4.0, r.top, r.right - 4.0, r.bottom), &txt, &sel, &txt, caret, true);
s().rename = f;
}
}
pub unsafe fn draw_slider(rt: &ID2D1RenderTarget, _pal: &Palette, b: &ID2D1SolidColorBrush) {
let r = slider_rect();
let cy = (r.top + r.bottom) / 2.0;
let dark = s().dark;
fill_rr(rt, b, rf(r.left, cy - 1.5, r.right, cy + 1.5), 1.5, if dark { col(1.0, 1.0, 1.0, 0.18) } else { col(0.0, 0.0, 0.0, 0.14) });
let t = (s().icon - lay::ICON_MIN) / (lay::ICON_MAX - lay::ICON_MIN);
let kx = r.left + t * (r.right - r.left);
b.SetColor(&col(0.0, 0.0, 0.0, 0.18));
rt.FillEllipse(&ell(pt(kx, cy + 0.6), 6.8, 6.8), b);
b.SetColor(&if dark { col(0.85, 0.85, 0.86, 1.0) } else { col(1.0, 1.0, 1.0, 1.0) });
rt.FillEllipse(&ell(pt(kx, cy), 6.2, 6.2), b);
}
pub fn status_text() -> String {
if !s().flash.is_empty() && unsafe { GetTickCount() }.wrapping_sub(s().flash_at) < 4000 && !crate::modo_foto() {
return s().flash.clone();
}
let n = s().items.len();
if s().tag_view >= 0 {
let base = format!("{n}{}", tr(if n == 1 { " item" } else { " itens" }));
// issue #443: Este Computador diz o espaço do disco escolhido (ou do único), como o rodapé das pastas
if s().tag_view == VIEW_COMPUTER {
let sel = s().items.iter().find(|e| e.sel).or_else(|| if n == 1 { s().items.first() } else { None });
if let Some(d) = sel.map(|e| drive_info(&e.path)).filter(|d| !d.is_empty()) {
return format!("{base}, {d}");
}
}
return base;
}
if s().searching {
let sc = sel_count();
let mut t = if sc > 0 { format!("{sc}{}{n}{}", tr(" de "), tr(if sc > 1 { " selecionados" } else { " selecionado" })) } else { format!("{n}{}", tr(if n == 1 { " resultado" } else { " resultados" })) };
if sc == 1 {
if let Some(e) = s().items.iter().find(|e| e.sel) {
t = parent(&e.path); // onde está o item escolhido
}
}
if s().search_busy {
t += &tr(" \u{2014} buscando\u{2026}");
} else if n >= 3000 {
t += &tr(" (limite)");
}
return t;
}
let mut fr = 0u64;
let fw = wz(&s().folder);
let free = unsafe {
if comum::windows::Win32::Storage::FileSystem::GetDiskFreeSpaceExW(pc(&fw), Some(&mut fr), None, None).is_ok() {
free_size(fr)
} else {
"?".into()
}
};
let sc = sel_count();
if sc > 0 {
cf!(tr("%d de %d selecionado%ls, %ls disponíveis"), sc, n, if sc > 1 { "s" } else { "" }, free)
} else if !s().search.is_empty() {
cf!(tr("%d de %zu itens, %ls disponíveis"), n, s().all.len(), free)
} else {
cf!(tr("%d %ls, %ls disponíveis"), n, if n == 1 { "item".to_string() } else { tr("itens") }, free)
}
}
/// Barra de caminho: "Disco Local (C:) › Users › <conta> › Downloads". Duplo clique num trecho abre a pasta.
#[derive(Clone, Default)]
pub struct PathSeg {
pub label: String,
pub path: String,
pub w: f32,
pub rect: RectF,
}
/// a barra de caminho está à vista: ligada, ou revelada pelo Alt (issue #152)
pub fn path_bar_on() -> bool {
s().path_bar || s().alt_path
}
/// refaz a altura do rodapé (status + barra de caminho, se à vista)
pub fn sync_footer_h() {
s().lay.footer_h = s().lay.status_h + if path_bar_on() { lay::PATH_H } else { 0.0 };
}
/// Alt apertado: revela a barra de caminho enquanto estiver segurado
pub fn alt_path_down() {
if s().alt_path || s().path_bar || s().rename_idx >= 0 {
return;
}
use comum::windows::Win32::UI::Input::KeyboardAndMouse::{GetKeyState, VK_CONTROL, VK_SHIFT};
if unsafe { GetKeyState(VK_CONTROL.0 as i32) < 0 || GetKeyState(VK_SHIFT.0 as i32) < 0 } {
return; // AltGr manda Ctrl+Alt; combos com Alt não contam
}
s().alt_path = true;
sync_footer_h();
crate::invalidate();
}
/// Alt solto (ou a janela perdeu o foco): some de novo
pub fn alt_path_up() {
if !s().alt_path {
return;
}
s().alt_path = false;
sync_footer_h();
crate::invalidate();
}
pub unsafe fn draw_path_bar(rt: &ID2D1RenderTarget, pal: &Palette, b: &ID2D1SolidColorBrush) {
if !path_bar_on() {
s().path_segs.clear();
return;
}
let (sw, sh) = client_dip();
let sbw = s().lay.sidebar_w;
let top = sh - s().lay.footer_h;
let cy = top + lay::PATH_H / 2.0;
b.SetColor(&pal.line); // linha entre a lista e o rodapé, e entre caminho e status
rt.DrawLine(pt(sbw, top + 0.5), pt(sw, top + 0.5), b, 1.0, None);
rt.DrawLine(pt(sbw, top + lay::PATH_H + 0.5), pt(sw, top + lay::PATH_H + 0.5), b, 0.6, None);
// monta os trechos da raiz até a pasta aberta
let mut segs: Vec<PathSeg> = Vec::new();
let mut p = s().folder.clone();
let tfp = s().f.path.clone();
while !p.is_empty() {
let label = if p.chars().count() <= 3 { display_name(&p) } else { file_name(&p) };
segs.insert(0, PathSeg { w: text_width(&label, &tfp), label, path: p.clone(), rect: Default::default() });
p = parent(&p);
}
let (icon, gap, sep) = (14.0, 4.0, 16.0);
let mut total: f32 = segs.iter().map(|g_| icon + gap + g_.w + sep).sum();
let avail = sw - sbw - 24.0;
while total > avail && segs.len() > 2 {
// não cabe: some com os trechos do meio
total -= icon + gap + segs[1].w + sep;
segs.remove(1);
}
let bb = br(b);
let mut x = sbw + 12.0;
let n = segs.len();
for (i, seg) in segs.iter_mut().enumerate() {
if i as i32 == s().drop_seg {
// vai receber o que está sendo arrastado
b.SetColor(&pal.hover);
rt.FillRoundedRectangle(&rr(rf(x - 2.0, top, x + icon + gap + seg.w + 2.0, top + lay::PATH_H), 4.0, 4.0), b);
}
let ir = rf(x, cy - icon / 2.0, x + icon, cy + icon / 2.0);
if seg.path.chars().count() <= 3 {
// disco
b.SetColor(&pal.text2);
rt.DrawRoundedRectangle(&rr(rf(ir.left + 1.0, cy - 3.5, ir.right - 1.0, cy + 3.5), 1.5, 1.5), b, 1.1, None);
rt.FillEllipse(&ell(pt(ir.right - 4.0, cy), 0.9, 0.9), b);
} else {
draw_folder(rt, ir, 0, b);
}
let tx = ir.right + gap;
b.SetColor(&if i + 1 == n { pal.text } else { pal.text2 });
text(rt, &seg.label, &tfp, rf(tx, top, tx + seg.w + 2.0, top + lay::PATH_H), &bb);
seg.rect = rf(x - 2.0, top, tx + seg.w + 2.0, top + lay::PATH_H);
x = tx + seg.w;
if i + 1 < n {
b.SetColor(&pal.text2);
let cx = x + sep / 2.0;
line(rt, &bb, cx - 1.5, cy - 3.0, cx + 1.5, cy, 1.2);
line(rt, &bb, cx + 1.5, cy, cx - 1.5, cy + 3.0, 1.2);
x += sep;
}
}
s().path_segs = segs;
}
#[allow(unused)]
fn _uso(_: &IDWriteTextFormat) {}