use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use uzor::render::RenderContext;
use uzor::ui::style::Sheet;
use uzor::ui::tray::{self, TrayLabels, TrayLook, TrayModel, TrayWidget};
use uzor::{Rect, RgbaIcon};
use uzor_render_tiny_skia::TinySkiaCpuRenderContext;
use super::tray::IconRect;
use crate::types::ids::TrayItemId;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FlyoutTrigger {
#[default]
Hover,
Click,
}
#[derive(Clone, Debug, PartialEq)]
pub struct TrayWidgetConfig {
pub widget: Option<TrayWidget>,
pub model: TrayModel,
pub labels: TrayLabels,
pub dark: bool,
pub glass: bool,
pub glass_bg: Option<String>,
pub glass_line: Option<String>,
pub trigger: FlyoutTrigger,
pub thumbnail: bool,
pub badge: bool,
pub menu_label: Option<String>,
pub reduced_motion: bool,
}
impl Default for TrayWidgetConfig {
fn default() -> Self {
TrayWidgetConfig {
widget: Some(TrayWidget::Combined),
model: TrayModel::default(),
labels: TrayLabels::default(),
dark: true,
glass: true,
glass_bg: None,
glass_line: None,
trigger: FlyoutTrigger::Hover,
thumbnail: true,
badge: true,
menu_label: None,
reduced_motion: false,
}
}
}
impl TrayWidgetConfig {
pub fn flyout_look(&self) -> TrayLook {
TrayLook { glass_bg: self.glass_bg.clone(), glass_line: self.glass_line.clone(), ..if self.glass { TrayLook::flyout(self.dark) } else { TrayLook::solid(self.dark) } }
}
}
pub const FLYOUT_MENU_ID: TrayItemId = TrayItemId(u32::MAX - 7);
#[derive(Default)]
struct Shared {
cfg: TrayWidgetConfig,
rev: u64,
show: Option<bool>,
}
#[derive(Clone, Default)]
pub struct TrayWidgetHandle {
inner: Arc<Mutex<Shared>>,
}
impl std::fmt::Debug for TrayWidgetHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TrayWidgetHandle").field("rev", &self.lock().rev).finish()
}
}
impl TrayWidgetHandle {
pub fn new(cfg: TrayWidgetConfig) -> Self {
TrayWidgetHandle { inner: Arc::new(Mutex::new(Shared { cfg, rev: 1, show: None })) }
}
fn lock(&self) -> std::sync::MutexGuard<'_, Shared> {
self.inner.lock().unwrap_or_else(|e| e.into_inner())
}
pub fn update(&self, f: impl FnOnce(&mut TrayWidgetConfig)) {
let changed = {
let mut g = self.lock();
let before = g.cfg.clone();
f(&mut g.cfg);
let ch = g.cfg != before;
if ch {
g.rev += 1;
}
ch
};
if changed {
crate::platform::wake();
}
}
pub fn set_widget(&self, w: Option<TrayWidget>) {
self.update(|c| c.widget = w);
}
pub fn set_model(&self, m: TrayModel) {
self.update(|c| c.model = m);
}
pub fn set_labels(&self, l: TrayLabels) {
self.update(|c| c.labels = l);
}
pub fn set_dark(&self, dark: bool) {
self.update(|c| c.dark = dark);
}
pub fn config(&self) -> TrayWidgetConfig {
self.lock().cfg.clone()
}
pub fn show(&self) {
self.lock().show = Some(true);
crate::platform::wake();
}
pub fn hide(&self) {
self.lock().show = Some(false);
crate::platform::wake();
}
pub fn snapshot(&self, scale: f64, look: Option<TrayLook>, t: f64) -> Option<RgbaIcon> {
let cfg = self.config();
let w = cfg.widget?;
let look = look.unwrap_or_else(|| TrayLook::solid(cfg.dark));
Some(render_rgba(w, &cfg, &look, scale, t))
}
fn take(&self, seen: u64) -> Option<(u64, TrayWidgetConfig)> {
let g = self.lock();
(g.rev != seen).then(|| (g.rev, g.cfg.clone()))
}
fn take_show(&self) -> Option<bool> {
self.lock().show.take()
}
}
thread_local! {
static SHEET: Sheet = tray::sheet();
}
pub(crate) fn guarded<R>(site: &'static str, f: impl FnOnce() -> R) -> Option<R> {
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
Ok(r) => Some(r),
Err(p) => {
static SEEN: Mutex<Vec<&'static str>> = Mutex::new(Vec::new());
let mut seen = SEEN.lock().unwrap_or_else(|e| e.into_inner());
if !seen.contains(&site) {
seen.push(site);
let why = p.downcast_ref::<&str>().map(|s| s.to_string()).or_else(|| p.downcast_ref::<String>().cloned()).unwrap_or_default();
log::error!("{site}: drawing panicked ({why}); a static image is shown instead");
}
None
}
}
}
pub fn static_card(pw: u32, ph: u32, cfg: &TrayWidgetConfig) -> RgbaIcon {
let bg = if cfg.dark { [0x1e, 0x1e, 0x22, 0xff] } else { [0xf4, 0xf4, 0xf6, 0xff] };
let (pw, ph) = (pw.max(1), ph.max(1));
let card = RgbaIcon::from_rgba(pw, ph, bg.iter().copied().cycle().take((pw * ph * 4) as usize).collect());
tray::badge(&card, cfg.model.status.rgba())
}
pub fn render_rgba(w: TrayWidget, cfg: &TrayWidgetConfig, look: &TrayLook, scale: f64, t: f64) -> RgbaIcon {
let (wd, ht) = w.size();
let scale = if scale.is_finite() && scale > 0.05 { scale } else { 1.0 };
let (pw, ph) = ((wd * scale).round().max(1.0) as u32, (ht * scale).round().max(1.0) as u32);
let mut ctx = TinySkiaCpuRenderContext::new(pw, ph, scale);
uzor::render::Painter::scale(&mut ctx, scale, scale);
let drawn = guarded("tray widget", || {
SHEET.with(|sheet| {
tray::render(&mut ctx, sheet, w, &cfg.model, &cfg.labels, look, 0.0, 0.0, t);
})
});
if drawn.is_none() {
return static_card(pw, ph, cfg);
}
let pm = ctx.pixmap();
let mut px = pm.data().to_vec();
for p in px.chunks_exact_mut(4) {
let a = p[3] as u32;
if a > 0 && a < 255 {
for c in &mut p[..3] {
*c = ((*c as u32 * 255 + a / 2) / a).min(255) as u8;
}
}
}
RgbaIcon::from_rgba(pw, ph, px)
}
pub fn rasterize_icon(px: u32, draw: impl FnOnce(&mut dyn RenderContext, Rect)) -> RgbaIcon {
let px = px.max(1);
let mut ctx = TinySkiaCpuRenderContext::new(px, px, 1.0);
draw(&mut ctx, Rect::new(0.0, 0.0, px as f64, px as f64));
let mut data = ctx.pixmap().data().to_vec();
for p in data.chunks_exact_mut(4) {
let a = p[3] as u32;
if a > 0 && a < 255 {
for c in &mut p[..3] {
*c = ((*c as u32 * 255 + a / 2) / a).min(255) as u8;
}
}
}
RgbaIcon::from_rgba(px, px, data)
}
pub fn flyout_position(icon: Option<IconRect>, mon: (f64, f64, f64, f64), size: (f64, f64), scale: f64) -> (i32, i32) {
let (mx, my, mw, mh) = mon;
let (w, h) = size;
let gap = 8.0 * scale;
let (x, y) = match icon.filter(|r| r.width > 0.0 || r.height > 0.0) {
Some(r) => {
let (cx, cy) = (r.x + r.width / 2.0, r.y + r.height / 2.0);
if cx < mx + mw * 0.08 {
(r.x + r.width + gap, cy - h / 2.0)
} else if cx > mx + mw * 0.92 && cy > my + mh * 0.1 && cy < my + mh * 0.9 {
(r.x - w - gap, cy - h / 2.0)
} else if cy > my + mh / 2.0 {
(cx - w / 2.0, r.y - h - gap)
} else {
(cx - w / 2.0, r.y + r.height + gap)
}
}
None => {
let edge = 12.0 * scale;
if cfg!(target_os = "windows") {
(mx + mw - w - edge, my + mh - h - 56.0 * scale)
} else {
(mx + mw - w - edge, my + 36.0 * scale)
}
}
};
let x = x.clamp(mx, (mx + mw - w).max(mx));
let y = y.clamp(my, (my + mh - h).max(my));
(x.round() as i32, y.round() as i32)
}
const HIDE_DELAY: Duration = Duration::from_millis(350);
const IDLE_HIDE: Duration = Duration::from_secs(8);
const FRAME: Duration = Duration::from_millis(33);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum FlyoutAction {
None,
OpenMain,
}
struct Flyout {
window: Arc<winit::window::Window>,
rs: uzor_render_hub::WindowRenderState,
widget: TrayWidget,
visible: bool,
inside: bool,
entered: bool,
hide_at: Option<Instant>,
dirty: bool,
last_paint: Option<Instant>,
}
pub(crate) struct TrayWidgets {
handle: TrayWidgetHandle,
rev: u64,
cfg: TrayWidgetConfig,
flyout: Option<Flyout>,
base_icon: Option<RgbaIcon>,
badge_on: Option<Option<[u8; 4]>>,
start: Instant,
#[cfg(target_os = "windows")]
thumb: Option<win::Thumb>,
#[cfg(target_os = "macos")]
dock: Option<Option<[u8; 4]>>,
}
impl TrayWidgets {
pub(crate) fn new(handle: TrayWidgetHandle) -> Self {
let cfg = handle.config();
TrayWidgets {
handle,
rev: 0,
cfg,
flyout: None,
base_icon: None,
badge_on: None,
start: Instant::now(),
#[cfg(target_os = "windows")]
thumb: None,
#[cfg(target_os = "macos")]
dock: None,
}
}
pub(crate) fn menu_item(&self) -> Option<super::tray::TrayMenuItem> {
self.cfg.menu_label.clone().map(|label| super::tray::TrayMenuItem { id: FLYOUT_MENU_ID, label, enabled: true })
}
pub(crate) fn set_base_icon(&mut self, icon: Option<RgbaIcon>) {
self.base_icon = icon;
self.badge_on = None;
}
pub(crate) fn attach_main(&mut self, window: &winit::window::Window) {
#[cfg(target_os = "windows")]
if self.thumb.is_none() {
if let Some(hwnd) = super::win32::extract_hwnd(window) {
self.thumb = Some(win::Thumb::install(hwnd, self.handle.clone()));
self.thumb.as_ref().map(|t| t.set_enabled(self.cfg.thumbnail && self.cfg.widget.is_some()));
}
}
let _ = window;
}
pub(crate) fn owns(&self, id: winit::window::WindowId) -> bool {
self.flyout.as_ref().is_some_and(|f| f.window.id() == id)
}
fn now(&self) -> f64 {
self.start.elapsed().as_secs_f64()
}
pub(crate) fn poll(&mut self, el: &winit::event_loop::ActiveEventLoop, hub: &uzor_render_hub::RenderHub, tray: Option<&mut super::tray::TrayHandle>, icon: Option<IconRect>) {
if let Some((rev, cfg)) = self.handle.take(self.rev) {
self.rev = rev;
self.cfg = cfg;
if let Some(f) = self.flyout.as_mut() {
f.dirty = true;
}
if self.cfg.widget.is_none() {
self.hide();
}
#[cfg(target_os = "windows")]
if let Some(t) = self.thumb.as_ref() {
t.set_enabled(self.cfg.thumbnail && self.cfg.widget.is_some());
t.invalidate();
}
}
let want = if self.cfg.badge { self.cfg.model.status.rgba() } else { None };
if let (Some(t), Some(base)) = (tray, self.base_icon.as_ref()) {
if self.badge_on != Some(want) {
let icon = guarded("tray badge", || tray::badge(base, want)).unwrap_or_else(|| base.clone());
let _ = t.set_icon(icon);
self.badge_on = Some(want);
}
}
#[cfg(target_os = "macos")]
{
let dot = if self.cfg.thumbnail { self.cfg.model.status.rgba() } else { None };
if self.dock != Some(dot) {
if let Some(base) = self.base_icon.as_ref() {
if guarded("dock icon", || mac::set_dock_icon(&tray::badge(base, dot))).is_none() {
let _ = guarded("dock icon", || mac::set_dock_icon(base));
}
self.dock = Some(dot);
}
}
}
match self.handle.take_show() {
Some(true) => self.show(el, hub, icon, false),
Some(false) => self.hide(),
None => {}
}
}
pub(crate) fn on_tray(&mut self, ev: &super::tray::TrayEvent, icon: Option<IconRect>, el: &winit::event_loop::ActiveEventLoop, hub: &uzor_render_hub::RenderHub) {
use super::tray::TrayEvent;
match ev {
TrayEvent::Enter if self.cfg.trigger == FlyoutTrigger::Hover => self.show(el, hub, icon, true),
TrayEvent::Leave => {
if let Some(f) = self.flyout.as_mut().filter(|f| f.visible && !f.inside) {
f.hide_at = Some(Instant::now() + HIDE_DELAY);
}
}
TrayEvent::LeftClick => {
if self.flyout.as_ref().is_some_and(|f| f.visible) && self.cfg.trigger == FlyoutTrigger::Click {
self.hide();
} else {
self.show(el, hub, icon, true);
}
}
TrayEvent::MenuClick(id) if *id == FLYOUT_MENU_ID => self.show(el, hub, icon, false),
_ => {}
}
}
fn show(&mut self, el: &winit::event_loop::ActiveEventLoop, hub: &uzor_render_hub::RenderHub, icon: Option<IconRect>, hovering: bool) {
let Some(w) = self.cfg.widget else { return };
if self.flyout.as_ref().is_some_and(|f| f.widget != w) {
self.flyout = None;
}
if self.flyout.is_none() {
match make_flyout(el, hub, w, self.cfg.glass) {
Ok(f) => self.flyout = Some(f),
Err(e) => {
log::warn!("tray flyout: {e}");
return;
}
}
}
let Some(f) = self.flyout.as_mut() else { return };
let scale = f.window.scale_factor();
let (wd, ht) = w.size();
let size = (wd * scale, ht * scale);
let mon = monitor_for(el, icon);
let (x, y) = flyout_position(icon, mon, size, scale);
f.window.set_outer_position(winit::dpi::PhysicalPosition::new(x, y));
f.hide_at = if hovering { None } else { Some(Instant::now() + IDLE_HIDE) };
f.entered = false;
f.dirty = true;
if !f.visible {
f.visible = true;
f.window.set_visible(true);
}
f.window.request_redraw();
}
fn hide(&mut self) {
if let Some(f) = self.flyout.as_mut().filter(|f| f.visible) {
f.visible = false;
f.inside = false;
f.hide_at = None;
f.window.set_visible(false);
}
}
pub(crate) fn window_event(&mut self, ev: &winit::event::WindowEvent) -> FlyoutAction {
use winit::event::{ElementState, WindowEvent};
let Some(f) = self.flyout.as_mut() else { return FlyoutAction::None };
match ev {
WindowEvent::CursorEntered { .. } => {
f.inside = true;
f.entered = true;
f.hide_at = None;
}
WindowEvent::CursorLeft { .. } => {
f.inside = false;
f.hide_at = Some(Instant::now() + HIDE_DELAY);
}
WindowEvent::MouseInput { state: ElementState::Released, .. } => {
self.hide();
return FlyoutAction::OpenMain;
}
WindowEvent::KeyboardInput { event, .. } if event.state == ElementState::Pressed => {
if event.logical_key == winit::keyboard::Key::Named(winit::keyboard::NamedKey::Escape) {
self.hide();
}
}
WindowEvent::Focused(false) if f.visible && !f.inside && f.entered => self.hide(),
WindowEvent::Resized(sz) => {
f.rs.resize_surface(sz.width.max(1), sz.height.max(1));
f.dirty = true;
}
WindowEvent::ScaleFactorChanged { .. } | WindowEvent::RedrawRequested => f.dirty = true,
WindowEvent::CloseRequested => self.hide(),
_ => {}
}
FlyoutAction::None
}
pub(crate) fn frame(&mut self, hub: &uzor_render_hub::RenderHub) -> Option<Instant> {
let now = Instant::now();
if self.flyout.as_ref().is_some_and(|f| f.visible && f.hide_at.is_some_and(|t| t <= now)) {
self.hide();
}
let t = self.now();
let cfg = &self.cfg;
let f = self.flyout.as_mut().filter(|f| f.visible)?;
let moving = cfg.widget.is_some_and(|w| cfg.model.animating(w)) && !cfg.reduced_motion;
let due = f.dirty || (moving && f.last_paint.map_or(true, |p| now.duration_since(p) >= FRAME));
if due {
paint_flyout(f, cfg, hub, t);
f.dirty = false;
f.last_paint = Some(now);
}
let next_frame = moving.then(|| f.last_paint.unwrap_or(now) + FRAME);
match (next_frame, f.hide_at) {
(Some(a), Some(b)) => Some(a.min(b)),
(a, b) => a.or(b),
}
}
}
fn monitor_for(el: &winit::event_loop::ActiveEventLoop, icon: Option<IconRect>) -> (f64, f64, f64, f64) {
let rect = |m: &winit::monitor::MonitorHandle| {
let p = m.position();
let s = m.size();
(f64::from(p.x), f64::from(p.y), f64::from(s.width), f64::from(s.height))
};
if let Some(r) = icon {
let (cx, cy) = (r.x + r.width / 2.0, r.y + r.height / 2.0);
if let Some(m) = el.available_monitors().find(|m| {
let (x, y, w, h) = rect(m);
cx >= x && cx < x + w && cy >= y && cy < y + h
}) {
return rect(&m);
}
}
el.primary_monitor().or_else(|| el.available_monitors().next()).map(|m| rect(&m)).unwrap_or((0.0, 0.0, 1920.0, 1080.0))
}
fn make_flyout(el: &winit::event_loop::ActiveEventLoop, hub: &uzor_render_hub::RenderHub, w: TrayWidget, glass: bool) -> Result<Flyout, String> {
use winit::dpi::LogicalSize;
use winit::window::{Window, WindowLevel};
let (wd, ht) = w.size();
#[allow(unused_mut)]
let mut attrs = Window::default_attributes()
.with_title("")
.with_inner_size(LogicalSize::new(wd, ht))
.with_decorations(false)
.with_resizable(false)
.with_transparent(true)
.with_blur(glass)
.with_visible(false)
.with_active(false)
.with_window_level(WindowLevel::AlwaysOnTop);
#[cfg(target_os = "windows")]
{
use winit::platform::windows::WindowAttributesExtWindows;
attrs = attrs.with_skip_taskbar(true);
}
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios", target_os = "android"))))]
{
use winit::platform::x11::WindowAttributesExtX11;
attrs = attrs.with_override_redirect(true);
}
#[cfg(target_os = "macos")]
{
use winit::platform::macos::WindowAttributesExtMacOS;
attrs = attrs.with_has_shadow(true);
}
let window = Arc::new(el.create_window(attrs).map_err(|e| e.to_string())?);
#[cfg(target_os = "windows")]
{
super::win32::set_corner_style(&window, uzor::CornerStyle::Rounded);
if glass {
if let Some(h) = super::win32::extract_hwnd(&window) {
win::acrylic(h);
}
}
}
let px = super::window_create::surface_px(&window);
let size = uzor_render_hub::SurfaceSize { width: px.width.max(1), height: px.height.max(1) };
let rs = super::window_create::create_surface(hub, &window, size).map_err(|e| e.to_string())?;
Ok(Flyout { window, rs, widget: w, visible: false, inside: false, entered: false, hide_at: None, dirty: true, last_paint: None })
}
fn paint_flyout(f: &mut Flyout, cfg: &TrayWidgetConfig, hub: &uzor_render_hub::RenderHub, t: f64) {
let look = cfg.flyout_look();
let w = f.widget;
f.rs.begin_frame();
let _ = f.rs.with_render_context(|ctx| {
let _ = guarded("tray flyout", || {
SHEET.with(|sheet| {
let (wd, ht) = w.size();
let d = uzor::ui::dom::lay_out_in(ctx, sheet, &tray::tray_widget(w, &cfg.model, &cfg.labels, &look), 0.0, 0.0, wd, Some(Rect::new(0.0, 0.0, wd, ht)), &|_| uzor::ui::style::State::NONE);
tray::paint(ctx, &d, &cfg.model, t, cfg.reduced_motion);
})
});
});
let out = uzor_render_hub::submit_frame(
&mut f.rs,
uzor_render_hub::SubmitParams { base_color: color::AlphaColor::new([0.0, 0.0, 0.0, 0.0]), msaa_samples: hub.settings().msaa_samples },
);
if out.surface_lost {
log::warn!("tray flyout: surface lost");
}
super::app_handler::flush_x11(&f.window);
}
#[cfg(target_os = "windows")]
mod win {
use super::TrayWidgetHandle;
type Hwnd = isize;
type Hbitmap = isize;
#[repr(C)]
struct BitmapInfoHeader {
size: u32,
width: i32,
height: i32,
planes: u16,
bit_count: u16,
compression: u32,
size_image: u32,
x_ppm: i32,
y_ppm: i32,
clr_used: u32,
clr_important: u32,
}
#[repr(C)]
struct BitmapInfo {
header: BitmapInfoHeader,
colors: [u32; 1],
}
#[repr(C)]
#[derive(Default)]
struct Rect {
left: i32,
top: i32,
right: i32,
bottom: i32,
}
#[repr(C)]
struct Margins {
left: i32,
right: i32,
top: i32,
bottom: i32,
}
#[link(name = "dwmapi")]
extern "system" {
fn DwmSetWindowAttribute(hwnd: Hwnd, attr: u32, pv: *const core::ffi::c_void, cb: u32) -> i32;
fn DwmSetIconicThumbnail(hwnd: Hwnd, hbmp: Hbitmap, flags: u32) -> i32;
fn DwmSetIconicLivePreviewBitmap(hwnd: Hwnd, hbmp: Hbitmap, client: *const core::ffi::c_void, flags: u32) -> i32;
fn DwmInvalidateIconicBitmaps(hwnd: Hwnd) -> i32;
fn DwmExtendFrameIntoClientArea(hwnd: Hwnd, m: *const Margins) -> i32;
}
#[link(name = "gdi32")]
extern "system" {
fn CreateDIBSection(hdc: isize, bmi: *const BitmapInfo, usage: u32, bits: *mut *mut core::ffi::c_void, section: isize, offset: u32) -> Hbitmap;
fn DeleteObject(h: isize) -> i32;
}
#[link(name = "user32")]
extern "system" {
fn GetClientRect(hwnd: Hwnd, r: *mut Rect) -> i32;
}
#[link(name = "comctl32")]
extern "system" {
fn SetWindowSubclass(hwnd: Hwnd, cb: SubclassProc, id: usize, data: usize) -> i32;
fn DefSubclassProc(hwnd: Hwnd, msg: u32, wp: usize, lp: isize) -> isize;
}
type SubclassProc = unsafe extern "system" fn(Hwnd, u32, usize, isize, usize, usize) -> isize;
const DWMWA_FORCE_ICONIC_REPRESENTATION: u32 = 7;
const DWMWA_HAS_ICONIC_BITMAP: u32 = 10;
const DWMWA_SYSTEMBACKDROP_TYPE: u32 = 38;
const DWMSBT_TRANSIENTWINDOW: u32 = 3;
const WM_DWMSENDICONICTHUMBNAIL: u32 = 0x0323;
const WM_DWMSENDICONICLIVEPREVIEWBITMAP: u32 = 0x0326;
const SUBCLASS_ID: usize = 0x755A_5457;
pub(super) fn acrylic(hwnd: Hwnd) {
let v: u32 = DWMSBT_TRANSIENTWINDOW;
let m = Margins { left: -1, right: -1, top: -1, bottom: -1 };
unsafe {
DwmExtendFrameIntoClientArea(hwnd, &m);
DwmSetWindowAttribute(hwnd, DWMWA_SYSTEMBACKDROP_TYPE, &v as *const u32 as *const _, 4);
}
}
pub(super) struct Thumb {
hwnd: Hwnd,
}
impl Thumb {
pub(super) fn install(hwnd: Hwnd, handle: TrayWidgetHandle) -> Thumb {
let data = Box::into_raw(Box::new(handle)) as usize;
unsafe {
SetWindowSubclass(hwnd, proc, SUBCLASS_ID, data);
}
Thumb { hwnd }
}
pub(super) fn set_enabled(&self, on: bool) {
let v: i32 = on as i32;
unsafe {
DwmSetWindowAttribute(self.hwnd, DWMWA_FORCE_ICONIC_REPRESENTATION, &v as *const i32 as *const _, 4);
DwmSetWindowAttribute(self.hwnd, DWMWA_HAS_ICONIC_BITMAP, &v as *const i32 as *const _, 4);
}
}
pub(super) fn invalidate(&self) {
unsafe {
DwmInvalidateIconicBitmaps(self.hwnd);
}
}
}
fn dib(img: &uzor::RgbaIcon, w: u32, h: u32, ox: u32, oy: u32) -> Hbitmap {
let bmi = BitmapInfo {
header: BitmapInfoHeader {
size: std::mem::size_of::<BitmapInfoHeader>() as u32,
width: w as i32,
height: -(h as i32),
planes: 1,
bit_count: 32,
compression: 0,
size_image: 0,
x_ppm: 0,
y_ppm: 0,
clr_used: 0,
clr_important: 0,
},
colors: [0],
};
let mut bits: *mut core::ffi::c_void = std::ptr::null_mut();
let hbmp = unsafe { CreateDIBSection(0, &bmi, 0, &mut bits, 0, 0) };
if hbmp == 0 || bits.is_null() {
return 0;
}
let out = unsafe { std::slice::from_raw_parts_mut(bits as *mut u8, (w * h * 4) as usize) };
out.fill(0);
for y in 0..img.height.min(h.saturating_sub(oy)) {
for x in 0..img.width.min(w.saturating_sub(ox)) {
let s = ((y * img.width + x) * 4) as usize;
let d = (((y + oy) * w + x + ox) * 4) as usize;
let a = img.pixels[s + 3] as u32;
let pm = |c: u8| ((c as u32 * a + 127) / 255) as u8;
out[d] = pm(img.pixels[s + 2]);
out[d + 1] = pm(img.pixels[s + 1]);
out[d + 2] = pm(img.pixels[s]);
out[d + 3] = a as u8;
}
}
hbmp
}
unsafe extern "system" fn proc(hwnd: Hwnd, msg: u32, wp: usize, lp: isize, _id: usize, data: usize) -> isize {
if msg == WM_DWMSENDICONICTHUMBNAIL || msg == WM_DWMSENDICONICLIVEPREVIEWBITMAP {
if let Some(Some(r)) = super::guarded("taskbar thumbnail", || unsafe { iconic(hwnd, msg, lp, data) }) {
return r;
}
}
DefSubclassProc(hwnd, msg, wp, lp)
}
unsafe fn iconic(hwnd: Hwnd, msg: u32, lp: isize, data: usize) -> Option<isize> {
{
let handle = &*(data as *const TrayWidgetHandle);
let cfg = match handle.inner.try_lock() {
Ok(g) => g.cfg.clone(),
Err(_) => return None,
};
let Some(w) = cfg.widget.filter(|_| cfg.thumbnail) else { return None };
let (wd, ht) = w.size();
let look = uzor::ui::tray::TrayLook::solid(cfg.dark);
if msg == WM_DWMSENDICONICTHUMBNAIL {
let (mw, mh) = (((lp >> 16) & 0xffff) as f64, (lp & 0xffff) as f64);
let scale = (mw / wd).min(mh / ht).max(0.1);
let img = super::render_rgba(w, &cfg, &look, scale, 0.0);
let h = dib(&img, img.width, img.height, 0, 0);
if h != 0 {
DwmSetIconicThumbnail(hwnd, h, 0);
DeleteObject(h);
}
} else {
let mut r = Rect::default();
GetClientRect(hwnd, &mut r);
let (cw, ch) = ((r.right - r.left).max(1) as u32, (r.bottom - r.top).max(1) as u32);
let scale = (cw as f64 * 0.5 / wd).min(ch as f64 * 0.5 / ht).max(1.0);
let img = super::render_rgba(w, &cfg, &look, scale, 0.0);
let (ox, oy) = (cw.saturating_sub(img.width) / 2, ch.saturating_sub(img.height) / 2);
let h = dib(&img, cw, ch, ox, oy);
if h != 0 {
DwmSetIconicLivePreviewBitmap(hwnd, h, std::ptr::null(), 0);
DeleteObject(h);
}
}
Some(0)
}
}
}
#[cfg(target_os = "macos")]
mod mac {
use objc2::rc::Retained;
use objc2::ClassType;
use objc2_app_kit::{NSApplication, NSImage};
use objc2_foundation::{MainThreadMarker, NSData};
pub(super) fn set_dock_icon(icon: &uzor::RgbaIcon) {
let Some(mtm) = MainThreadMarker::new() else { return };
let Some(png) = super::super::screenshot::encode_png(&icon.pixels, icon.width, icon.height) else { return };
let data = NSData::with_bytes(&png);
let img: Option<Retained<NSImage>> = NSImage::initWithData(NSImage::alloc(), &data);
let app = NSApplication::sharedApplication(mtm);
unsafe { app.setApplicationIconImage(img.as_deref()) };
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn flyout_sits_by_the_icon_and_inside_the_monitor() {
let mon = (0.0, 0.0, 1920.0, 1080.0);
let r = IconRect { x: 1700.0, y: 1050.0, width: 24.0, height: 24.0 };
let (x, y) = flyout_position(Some(r), mon, (320.0, 200.0), 1.0);
assert_eq!((x, y), (1552, 842));
let r = IconRect { x: 1900.0, y: 1050.0, width: 20.0, height: 24.0 };
assert_eq!(flyout_position(Some(r), mon, (320.0, 200.0), 1.0).0, 1600);
let r = IconRect { x: 1000.0, y: 0.0, width: 24.0, height: 24.0 };
assert_eq!(flyout_position(Some(r), mon, (300.0, 164.0), 1.0), (862, 32));
let r = IconRect { x: 4.0, y: 600.0, width: 24.0, height: 24.0 };
assert_eq!(flyout_position(Some(r), mon, (300.0, 164.0), 1.0), (36, 530));
let (x, y) = flyout_position(None, mon, (300.0, 164.0), 2.0);
assert!(x >= 0 && y >= 0 && x + 300 <= 1920 && y + 164 <= 1080);
}
#[test]
fn handle_bumps_only_on_change_and_snapshots() {
let h = TrayWidgetHandle::new(TrayWidgetConfig::default());
let (r0, _) = h.take(0).unwrap();
assert!(h.take(r0).is_none());
h.set_dark(true);
assert!(h.take(r0).is_none(), "same value, no new revision");
h.set_widget(Some(TrayWidget::Tasks));
let (r1, c) = h.take(r0).unwrap();
assert!(r1 > r0 && c.widget == Some(TrayWidget::Tasks));
h.show();
assert_eq!(h.take_show(), Some(true));
assert_eq!(h.take_show(), None);
let img = h.snapshot(2.0, None, 0.0).unwrap();
assert_eq!((img.width, img.height), (600, 328));
let mid = ((164 * 600 + 300) * 4) as usize;
assert_eq!(img.pixels[mid + 3], 255);
assert!(img.pixels[3] < 255);
h.set_widget(None);
assert!(h.snapshot(1.0, None, 0.0).is_none());
}
#[test]
fn rasterized_icons_are_straight_alpha() {
let i = rasterize_icon(16, |ctx, r| {
ctx.set_fill_color("rgba(255,0,0,0.5)");
ctx.fill_rect(r.x, r.y, r.width, r.height);
});
assert_eq!(i.pixels.len(), 16 * 16 * 4);
assert!(i.pixels[0] > 250 && (120..=135).contains(&i.pixels[3]));
}
#[test]
fn a_panic_while_drawing_never_leaves_the_render() {
use std::rc::Rc;
use uzor::ui::tray::{TrayAgent, TrayModel};
let cfg = TrayWidgetConfig { widget: Some(TrayWidget::Agents), model: TrayModel { agents: vec![TrayAgent::new("k1", "one")], ..Default::default() }, ..Default::default() };
let w = cfg.widget.unwrap();
uzor::ui::tray::set_critter_painter(Some(Rc::new(|_: &mut dyn RenderContext, _: Rect, _: &TrayAgent, _: f64, _: bool| panic!("app painter bug"))));
let img = render_rgba(w, &cfg, &TrayLook::solid(true), 1.0, 0.0);
let (wd, ht) = w.size();
assert_eq!((img.width, img.height), (wd.round() as u32, ht.round() as u32));
uzor::ui::tray::set_critter_painter(None);
assert_eq!(guarded("test site", || panic!("x")), None::<()>);
assert_eq!(guarded("test site", || 7), Some(7));
let card = static_card(30, 20, &cfg);
assert_eq!((card.width, card.height, card.pixels.len()), (30, 20, 30 * 20 * 4));
}
}