A new style desktop shell for Windows 11: menu bar, Dock, Finder, Spotlight, Control Center and Liquid Glass, in native C++.
Rust
Animated wallpaper: measure the name colours off the UI thread
Eternity070628259cd7877e15
diff --git a/src/shell/mesa.cpp b/src/shell/mesa.cppindex 0157162..f0bc10d 100644--- a/src/shell/mesa.cpp+++ b/src/shell/mesa.cpp@@ -850,43 +850,63 @@ void DrawIcons(ID2D1RenderTarget* rt) {bool g_live;// Papel de parede animado: o papel de parede é uma janela de outro programa atrás da mesa transparente, então o// brilho é medido na tela de verdade, atrás do nome. BitBlt da tela sem CAPTUREBLT não inclui janelas em camadas, e a-// mesa transparente é uma: sai o papel de parede sem os nossos ícones. Medir a cada desenho seria caro (no modo-// transparente cada desenho é a tela inteira) e o papel muda o tempo todo: o Tick mede a cada ~2 s e guarda por-// posição do nome. Mediana (não média): o que sobrar de texto por cima não decide. Com folga (escurece acima de 0,72,-// volta abaixo de 0,62) para o nome não ficar trocando de cor num papel de parede que oscila perto do limite.-std::map<std::pair<int, int>, bool> g_liveLight;+// mesa transparente é uma: sai o papel de parede sem os nossos ícones. A cópia da tela espera o DWM e a placa de+// vídeo: medido com o Wallpaper Engine rodando, 13 ms por nome na mediana e até 1,5 s, e um pedaço de 1100×900 até+// 1 s. Por isso nada disso roda na thread da interface (lá travava a mesa, a barra e o Dock a cada 2 s até o vigia+// reiniciar o Shell): uma thread própria copia, a cada ~2 s, só o retângulo que junta os nomes, numa cópia só, e+// guarda a mediana do brilho de cada nome (o que sobrar de texto por cima não decide). O Tick só lê o resultado e+// decide a cor com folga (escurece acima de 0,72, volta abaixo de 0,62), para o nome não trocar de cor sem parar.+std::map<std::pair<int, int>, bool> g_liveLight; // só a thread da interfacestd::pair<int, int> LabelKey(const D2D1_RECT_F& lb) { return { (int)std::lround(lb.left), (int)std::lround(lb.top) }; }-bool MeasureLiveLum(const Mon& m, const D2D1_RECT_F& lb, float* lum) {- int x = m.r.left + (int)lb.left, y = m.r.top + (int)lb.top;- int w = std::clamp((int)(lb.right - lb.left), 1, 400), h = std::clamp((int)(lb.bottom - lb.top), 1, 80);- HDC scr = GetDC(nullptr);- if (!scr) return false;- HDC dc = CreateCompatibleDC(scr);- BITMAPINFO bi{}; bi.bmiHeader = { sizeof(BITMAPINFOHEADER), w, -h, 1, 32, BI_RGB };- BYTE* bits = nullptr;- HBITMAP bmp = dc ? CreateDIBSection(dc, &bi, DIB_RGB_COLORS, (void**)&bits, nullptr, 0) : nullptr;- bool ok = false;- if (bmp && bits) {- HGDIOBJ old = SelectObject(dc, bmp);- if (BitBlt(dc, 0, 0, w, h, scr, x, y, SRCCOPY)) { // sem CAPTUREBLT: as janelas em camadas ficam de fora- GdiFlush();- UINT32 hist[256] = {};- for (int i = 0; i < w * h; i++) {- const BYTE* p = bits + i * 4; // BGRA- hist[std::min(255, (int)std::lround(0.2126f * p[2] + 0.7152f * p[1] + 0.0722f * p[0]))]++;+struct LiveLabel { std::pair<int, int> key; RECT r; }; // r: na tela+std::mutex g_lsMx;+std::vector<LiveLabel> g_lsPedido; // os nomes a medir (a interface atualiza)+std::map<std::pair<int, int>, float> g_lsBrilho; // o último brilho medido de cada nome (a thread atualiza)+std::atomic<bool> g_lsRodando{ false }, g_lsThread{ false };+void LiveSampler() {+ HDC dc = CreateCompatibleDC(nullptr);+ HBITMAP bmp = nullptr; BYTE* bits = nullptr; int bw = 0, bh = 0;+ while (g_lsRodando) {+ std::vector<LiveLabel> pedido;+ { std::lock_guard<std::mutex> l(g_lsMx); pedido = g_lsPedido; }+ RECT u{ LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN };+ for (auto& p : pedido) { u.left = std::min(u.left, p.r.left); u.top = std::min(u.top, p.r.top); u.right = std::max(u.right, p.r.right); u.bottom = std::max(u.bottom, p.r.bottom); }+ int w = (int)(u.right - u.left), h = (int)(u.bottom - u.top);+ if (dc && !pedido.empty() && w > 0 && h > 0 && w <= 16384 && h <= 16384) {+ if (w != bw || h != bh) {+ if (bmp) { DeleteObject(bmp); bmp = nullptr; bits = nullptr; }+ BITMAPINFO bi{}; bi.bmiHeader = { sizeof(BITMAPINFOHEADER), w, -h, 1, 32, BI_RGB };+ bmp = CreateDIBSection(dc, &bi, DIB_RGB_COLORS, (void**)&bits, nullptr, 0);+ if (bmp) SelectObject(dc, bmp);+ bw = bmp ? w : 0; bh = bmp ? h : 0;+ }+ HDC scr = bmp ? GetDC(nullptr) : nullptr;+ bool ok = scr && BitBlt(dc, 0, 0, w, h, scr, u.left, u.top, SRCCOPY); // sem CAPTUREBLT: janelas em camadas fora+ if (scr) ReleaseDC(nullptr, scr);+ if (ok) {+ GdiFlush();+ std::map<std::pair<int, int>, float> res;+ for (auto& p : pedido) {+ UINT32 hist[256] = {}, n = 0;+ for (LONG y = std::max(p.r.top, u.top); y < std::min(p.r.bottom, u.bottom); y++)+ for (LONG x = std::max(p.r.left, u.left); x < std::min(p.r.right, u.right); x++) {+ const BYTE* px = bits + ((size_t)(y - u.top) * w + (x - u.left)) * 4; // BGRA+ hist[std::min(255, (int)std::lround(0.2126f * px[2] + 0.7152f * px[1] + 0.0722f * px[0]))]++; n++;+ }+ if (!n) continue;+ UINT32 acc = 0; int med = 0;+ for (; med < 255 && (acc += hist[med]) <= n / 2; med++) {}+ res[p.key] = med / 255.f;+ }+ std::lock_guard<std::mutex> l(g_lsMx); g_lsBrilho.swap(res);}- UINT32 half = (UINT32)(w * h) / 2, acc = 0;- int med = 0;- for (; med < 255 && (acc += hist[med]) <= half; med++) {}- *lum = med / 255.f;- ok = true;}- SelectObject(dc, old);+ for (int i = 0; i < 20 && g_lsRodando; i++) Sleep(100);}if (bmp) DeleteObject(bmp);if (dc) DeleteDC(dc);- ReleaseDC(nullptr, scr);- return ok;+ { std::lock_guard<std::mutex> l(g_lsMx); g_lsBrilho.clear(); }+ g_lsThread = false;}void RefreshLiveLabels(); // abaixo, depois do LabelBox@@ -1802,21 +1822,35 @@ bool AnyUwpWindow() {return any;}-// a cor de cada nome sobre o papel de parede animado (LabelOnLight): mede de novo e redesenha se alguma mudou+// a cor de cada nome sobre o papel de parede animado (LabelOnLight): passa os nomes para a thread que mede (LiveSampler,+// acima), lê o que ela já mediu e redesenha se alguma cor mudou. Nada de cópia da tela aqui (thread da interface).void RefreshLiveLabels() {Mon* p = Primary();- if (!g_live || !p) { g_liveLight.clear(); return; }- std::map<std::pair<int, int>, bool> novo;- bool mudou = false;+ if (!g_live || !p) {+ g_lsRodando = false;+ g_liveLight.clear();+ std::lock_guard<std::mutex> l(g_lsMx); g_lsPedido.clear();+ return;+ }+ std::vector<LiveLabel> pedido;for (auto& it : g_items) {if (it.col < 0 && it.row < 0) continue; // ainda sem lugar na mesaD2D1_RECT_F lb = LabelBox(it);- auto k = LabelKey(lb);- auto a = g_liveLight.find(k);+ RECT r{ p->r.left + (LONG)lb.left, p->r.top + (LONG)lb.top, p->r.left + (LONG)lb.right, p->r.top + (LONG)lb.bottom };+ if (r.right > r.left && r.bottom > r.top) pedido.push_back({ LabelKey(lb), r });+ }+ std::map<std::pair<int, int>, float> brilho;+ { std::lock_guard<std::mutex> l(g_lsMx); g_lsPedido = pedido; brilho = g_lsBrilho; }+ g_lsRodando = true;+ if (!g_lsThread.exchange(true)) std::thread(LiveSampler).detach();+ std::map<std::pair<int, int>, bool> novo;+ bool mudou = false;+ for (auto& q : pedido) {+ auto a = g_liveLight.find(q.key);bool antes = a != g_liveLight.end() && a->second;- float lum = 0;- bool claro = MeasureLiveLum(*p, lb, &lum) ? (antes ? lum > 0.62f : lum > 0.72f) : antes;- novo[k] = claro;+ auto b = brilho.find(q.key);+ bool claro = b == brilho.end() ? antes : (antes ? b->second > 0.62f : b->second > 0.72f);+ novo[q.key] = claro;if (claro != antes) mudou = true;}if (novo.size() != g_liveLight.size()) mudou = true;