Skip to content

A lightweight, macOS-style dock for Windows 11, with glass visuals, smooth magnification, and Genie effect.

fix(shell): refresh recycle bin icons from Windows

CristianOlivera12833ba42bab3
diff --git a/crates/muelle-shell/src/icon_cache.rs b/crates/muelle-shell/src/icon_cache.rs
index c5d3b9e..64ff701 100644
--- a/crates/muelle-shell/src/icon_cache.rs
+++ b/crates/muelle-shell/src/icon_cache.rs
@@ -16,8 +16,9 @@ use std::{
};
use crate::{
- icons::{IconError, IconImage, load_icon},
+ icons::{IconError, IconImage, load_icon, load_icon_at},
names::display_name,
+ recycle_bin,
};
/// Changed when icons are read differently, so entries from before are read
@@ -71,14 +72,27 @@ impl IconCache {
let kept = crate::pins::kept_shortcut_for(id);
let source = kept.as_deref().unwrap_or(id);
let path = self.entry_path(&format!("{source}|{variant}"), size);
- let stamp = stamp_of(source);
+ // The recycle bin's two icons come from where Windows names them, not
+ // from the shell item, which shows whichever state Explorer has drawn
+ // last (issue #15).
+ let bin_icon = (source == recycle_bin::PARSING_NAME)
+ .then(|| recycle_bin::icon_location(variant == recycle_bin::FULL))
+ .flatten();
+ let stamp = bin_icon.as_ref().map_or_else(
+ || stamp_of(source),
+ |(file, index)| location_stamp(file, *index),
+ );
if let Some(cached) = fs::read(&path).ok().and_then(|bytes| decode(&bytes, stamp)) {
return Ok(cached);
}
+ let icon = match &bin_icon {
+ Some((file, index)) => load_icon_at(file, *index, size)?,
+ None => load_icon(source, size)?,
+ };
let visual = AppVisual {
name: display_name(source),
- icon: load_icon(source, size)?,
+ icon,
};
// An unwritable cache only makes the next startup slower.
let _ = fs::create_dir_all(&self.folder);
@@ -105,6 +119,15 @@ fn stamp_of(id: &str) -> u64 {
}
}
+/// Validity of an icon read from `file,index`: changes when another icon is
+/// chosen or the file is updated. It never equals a day number, so entries
+/// cached the old way (issue #15) are read again.
+fn location_stamp(file: &str, index: i32) -> u64 {
+ let mut hasher = DefaultHasher::new();
+ (file.to_lowercase(), index, stamp_of(file)).hash(&mut hasher);
+ hasher.finish() | (1 << 63)
+}
+
fn encode(visual: &AppVisual, stamp: u64) -> Vec<u8> {
let name = visual.name.as_bytes();
let mut bytes = Vec::with_capacity(HEADER_LEN + name.len() + visual.icon.bgra.len());
diff --git a/crates/muelle-shell/src/icons.rs b/crates/muelle-shell/src/icons.rs
index bf80a95..2900f1c 100644
--- a/crates/muelle-shell/src/icons.rs
+++ b/crates/muelle-shell/src/icons.rs
@@ -95,10 +95,20 @@ pub fn load_icon(parsing_name: &str, size: u32) -> Result<IconImage, IconError>
/// The first icon in a program's resources at `size` pixels; `None` when the
/// program has none, and the shell gives Windows' generic program icon.
fn program_icon(path: &str, size: u32) -> Option<IconImage> {
+ icon_at(path, 0, size)
+}
+
+/// The icon at an icon location (`file,index`, as the registry writes them)
+/// at `size` pixels. A negative `index` is a resource id, as in `DefaultIcon`.
+pub fn load_icon_at(path: &str, index: i32, size: u32) -> Result<IconImage, IconError> {
+ icon_at(path, index, size).ok_or_else(|| IconError::Pixels(format!("{path},{index}")))
+}
+
+fn icon_at(path: &str, index: i32, size: u32) -> Option<IconImage> {
let mut icon = HICON::default();
// SAFETY: The path lives for the call, which writes one icon handle we own.
let result =
- unsafe { SHDefExtractIconW(&HSTRING::from(path), 0, 0, Some(&mut icon), None, size) };
+ unsafe { SHDefExtractIconW(&HSTRING::from(path), index, 0, Some(&mut icon), None, size) };
// S_FALSE: the file has no icon.
if result != S_OK || icon.is_invalid() {
return None;
diff --git a/crates/muelle-shell/src/recycle_bin.rs b/crates/muelle-shell/src/recycle_bin.rs
index 23404a2..eee78de 100644
--- a/crates/muelle-shell/src/recycle_bin.rs
+++ b/crates/muelle-shell/src/recycle_bin.rs
@@ -13,6 +13,102 @@ use windows::Win32::{
pub const PARSING_NAME: &str = "::{645FF040-5081-101B-9F08-00AA002F954E}";
+/// The two icon variants the dock asks the icon cache for. Their names are
+/// part of the cache keys, so they stay as they were.
+pub const FULL: &str = "llena";
+pub const EMPTY: &str = "vacia";
+
+/// Where Windows keeps the bin's icon for one state, as `(file, index)`.
+///
+/// The icon of the bin as a shell item is whatever Explorer shows *now*, and
+/// right after the bin is emptied or filled Explorer can still show the
+/// previous state. Asked for the "empty" variant at that moment, the dock
+/// got the full icon and cached it (issue #15). Windows names both icons in
+/// `DefaultIcon` (`Empty` and `Full`), where Explorer itself reads them: the
+/// per-user copy first, which holds an icon the user picked in Desktop icon
+/// settings, then the system default, then the stock icons.
+#[must_use]
+pub fn icon_location(full: bool) -> Option<(String, i32)> {
+ use windows::{
+ Win32::{
+ System::Registry::{HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, RRF_RT_REG_SZ, RegGetValueW},
+ UI::Shell::{
+ SHGSI_ICONLOCATION, SHGetStockIconInfo, SHSTOCKICONINFO, SIID_RECYCLER,
+ SIID_RECYCLERFULL,
+ },
+ },
+ core::{PCWSTR, w},
+ };
+
+ let value = if full { w!("Full") } else { w!("Empty") };
+ let keys: [(_, PCWSTR); 2] = [
+ (
+ HKEY_CURRENT_USER,
+ w!(
+ r"Software\Microsoft\Windows\CurrentVersion\Explorer\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}\DefaultIcon"
+ ),
+ ),
+ (
+ HKEY_CLASSES_ROOT,
+ w!(r"CLSID\{645FF040-5081-101B-9F08-00AA002F954E}\DefaultIcon"),
+ ),
+ ];
+ for (root, key) in keys {
+ let mut buffer = [0u16; 520];
+ let mut bytes = size_of_val(&buffer) as u32;
+ // SAFETY: `buffer` and `bytes` describe a valid buffer. `RRF_RT_REG_SZ`
+ // also accepts `REG_EXPAND_SZ` and expands `%SystemRoot%`.
+ let read = unsafe {
+ RegGetValueW(
+ root,
+ key,
+ value,
+ RRF_RT_REG_SZ,
+ None,
+ Some(buffer.as_mut_ptr().cast()),
+ Some(&mut bytes),
+ )
+ };
+ if read.is_ok() {
+ let len = (bytes as usize / 2).saturating_sub(1);
+ if let Some(location) = parse_icon_location(&String::from_utf16_lossy(&buffer[..len])) {
+ return Some(location);
+ }
+ }
+ }
+
+ let mut stock = SHSTOCKICONINFO {
+ cbSize: size_of::<SHSTOCKICONINFO>() as u32,
+ ..Default::default()
+ };
+ let id = if full {
+ SIID_RECYCLERFULL
+ } else {
+ SIID_RECYCLER
+ };
+ // SAFETY: `stock` has its size set; only the location is asked for.
+ unsafe { SHGetStockIconInfo(id, SHGSI_ICONLOCATION, &mut stock) }.ok()?;
+ let len = stock
+ .szPath
+ .iter()
+ .position(|&c| c == 0)
+ .unwrap_or(stock.szPath.len());
+ let path = String::from_utf16_lossy(&stock.szPath[..len]);
+ (!path.is_empty()).then_some((path, stock.iIcon))
+}
+
+/// `C:\Windows\System32\imageres.dll,-55` → (`C:\...\imageres.dll`, -55). A
+/// location without an index is the file's first icon.
+fn parse_icon_location(text: &str) -> Option<(String, i32)> {
+ let text = text.trim();
+ let (path, index) = match text.rsplit_once(',') {
+ Some((path, index)) => (path, index.trim().parse().ok()?),
+ None => (text, 0),
+ };
+ let path = path.trim().trim_matches('"');
+ (!path.is_empty()).then(|| (path.to_owned(), index))
+}
+
#[must_use]
pub fn has_items() -> bool {
let mut info = SHQUERYRBINFO {
@@ -75,3 +171,39 @@ impl Drop for BinWatch {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn parse_icon_location_should_split_file_and_resource_id() {
+ assert_eq!(
+ parse_icon_location(r"C:\Windows\System32\imageres.dll,-55"),
+ Some((r"C:\Windows\System32\imageres.dll".to_owned(), -55))
+ );
+ assert_eq!(
+ parse_icon_location(r#""C:\Icons\bin, full.ico" , 2"#),
+ Some((r"C:\Icons\bin, full.ico".to_owned(), 2))
+ );
+ assert_eq!(
+ parse_icon_location(r"C:\Icons\bin.ico"),
+ Some((r"C:\Icons\bin.ico".to_owned(), 0))
+ );
+ assert_eq!(parse_icon_location(" , 3"), None);
+ }
+
+ /// Reads this PC's two bin icons: they must exist and differ.
+ #[test]
+ fn empty_and_full_icons_should_be_different_images() -> Result<(), crate::icons::IconError> {
+ let (Some(empty), Some(full)) = (icon_location(false), icon_location(true)) else {
+ panic!("Windows names no recycle bin icons");
+ };
+ assert_ne!(empty, full);
+ let empty = crate::icons::load_icon_at(&empty.0, empty.1, 64)?;
+ let full = crate::icons::load_icon_at(&full.0, full.1, 64)?;
+ assert_eq!((empty.width, full.width), (64, 64));
+ assert_ne!(empty.bgra, full.bgra);
+ Ok(())
+ }
+}