A lightweight, macOS-style dock for Windows 11, with glass visuals, smooth magnification, and Genie effect.
feat(dock): show app jump lists with icons in the right-click menu
lazerLipa5dd5ee64d3cd
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0662e61..65a3f9f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,19 @@ copia la sección de la versión que se publica a la release de GitHub.## [Unreleased]+### English++- Right-clicking an app in the dock shows its jump list, as on the Windows+ taskbar: pinned, recent and frequent items, the app's own categories and its+ tasks, each with its icon. Files open in that app.++### Español++- El clic derecho en una app del dock muestra su lista de saltos, como en la+ barra de Windows: elementos anclados, recientes y frecuentes, las categorías+ propias de la app y sus tareas, cada uno con su icono. Los archivos se abren+ en esa app.+## [0.1.19]### Englishdiff --git a/README.es.md b/README.es.mdindex c57f8dc..98a0284 100644--- a/README.es.md+++ b/README.es.md@@ -28,6 +28,8 @@ en desarrollo activo.activa Ajustes → Ventanas → *Mostrar ventanas minimizadas* para verlas comominiaturas en vivo a la derecha del dock.- Clic en la app que ya está delante para minimizarla, como en la barra de Windows.+- Clic derecho en una app para ver su lista de saltos, con iconos, como en la+ barra de Windows: elementos anclados y recientes, sus categorías y sus tareas.- Arrastrar y soltar archivos sobre apps, Descargas o la Papelera.- Una carpeta de accesos directos a la derecha del dock: su contenido se abrecomo un menú, con un submenú para cada carpeta, como una barra dediff --git a/README.fr.md b/README.fr.mdindex f8c51de..9bc042f 100644--- a/README.fr.md+++ b/README.fr.md@@ -42,6 +42,9 @@ Agrandissement fluide, aperçus des fenêtres en direct et effet génie facultatles garder plutôt sous forme de miniatures en direct à droite du dock.- Cliquer sur l'app déjà au premier plan la réduit, comme dans la barre destâches de Windows.+- Clic droit sur une app pour voir sa liste de raccourcis, avec icônes, comme+ dans la barre des tâches de Windows : éléments épinglés et récents, ses+ catégories et ses tâches.- Glisser-déposer des fichiers sur les apps, les Téléchargements ou laCorbeille.- Un dossier de raccourcis à droite du dock : son contenu s'ouvre en menu, avecdiff --git a/README.md b/README.mdindex b844f7e..699920d 100644--- a/README.md+++ b/README.md@@ -34,6 +34,8 @@Settings → Windows → *Show minimized windows* to keep them as live thumbnailson the right side of the dock instead.- Click the app already in front to minimize it, as on the Windows taskbar.+- Right-click an app to see its jump list, with icons, as on the Windows+ taskbar: pinned and recent items, the app's own categories and its tasks.- Drag and drop files onto apps, Downloads, or the Recycle Bin.- A shortcuts folder at the right of the dock: its contents open as a menu,with a submenu for each folder inside, like a Windows 10 taskbar toolbar.diff --git a/apps/dock/src/app.rs b/apps/dock/src/app.rsindex b4593d0..5dc881b 100644--- a/apps/dock/src/app.rs+++ b/apps/dock/src/app.rs@@ -37,7 +37,7 @@ use muelle_shell::{end_task::PreparedEndTask,files,icons::IconImage,- launcher,+ jumplist, launcher,pins::{self, app_exists, taskbar_pins, taskbar_pins_if_readable},power,recycle_bin::{self, BinWatch},@@ -282,6 +282,8 @@ const CMD_FORGET_RECENT: u32 = 17;const CMD_END_TASK: u32 = 18;const CMD_HIDE_FIXED: u32 = 19;const CMD_WINDOW: u32 = 100;+/// Jump list entries start here, after any window command.+const CMD_JUMP: u32 = 1000;const MENU_WINDOWS: usize = 12;const MENU_TITLE_CHARS: usize = 60;@@ -444,6 +446,11 @@ pub(crate) enum Outside {OpenAsAdmin(String),/// Unpin from the Windows taskbar too (see `pins::unpin_from_taskbar`).UnpinFromTaskbar(String),+ /// An entry of the jump list of the app `program`.+ OpenJump {+ target: jumplist::Target,+ program: String,+ },ShowDesktop,}@@ -493,6 +500,8 @@ enum MenuTarget {role: Role,windows: Vec<isize>,end_task: Option<PreparedEndTask>,+ /// What each jump list command opens, from `CMD_JUMP` on.+ jump: Vec<jumplist::Target>,},Dock,}@@ -3528,7 +3537,7 @@ impl App {return Ok(None);};let windows = self.windows_of(&id);- let (items, end_task) = self.entry_menu(&id, Role::App, &windows);+ let (items, end_task, jump) = self.entry_menu(&id, Role::App, &windows);let gap = self.stage.as_ref()@@ -3545,6 +3554,7 @@ impl App {role: Role::App,windows,end_task,+ jump,},items,anchor,@@ -3610,13 +3620,14 @@ impl App {Some((id, role, rect)) => {let windows = self.windows_of(&id);let anchor = stage.beside(rect, gap);- let (items, end_task) = self.entry_menu(&id, role, &windows);+ let (items, end_task, jump) = self.entry_menu(&id, role, &windows);(MenuTarget::Entry {id,role,windows,end_task,+ jump,},items,anchor,@@ -3662,15 +3673,22 @@ impl App {})))}+ /// The menu of a dock item, the app's prepared End Task and what its jump+ /// list entries open (see `CMD_JUMP`).fn entry_menu(&self,id: &str,role: Role,windows: &[isize],- ) -> (Vec<MenuItem>, Option<PreparedEndTask>) {+ ) -> (+ Vec<MenuItem>,+ Option<PreparedEndTask>,+ Vec<jumplist::Target>,+ ) {let is_file = !id.starts_with("shell:") && !id.starts_with("::");let t = text();let mut items = Vec::new();+ let mut jump = Vec::new();let end_task = if role == Role::App && id != explorer_id() {PreparedEndTask::capture(id, windows, &self.windows)} else {@@ -3678,6 +3696,39 @@ impl App {};match role {Role::App => {+ // The app's jump list first, as on the taskbar.+ for group in jumplist::for_dock_item(id, windows) {+ items.push(MenuItem::Header(match group.section {+ jumplist::Section::Pinned => t.jump_pinned.to_owned(),+ jumplist::Section::Frequent => t.jump_frequent.to_owned(),+ jumplist::Section::Recent => t.jump_recent.to_owned(),+ jumplist::Section::Tasks => t.jump_tasks.to_owned(),+ jumplist::Section::Custom(name) => shorten(&name, MENU_TITLE_CHARS),+ }));+ for entry in group.entries {+ match entry {+ jumplist::Entry::Item {+ label,+ target,+ icon,+ } => {+ items.push(MenuItem::Action {+ command: CMD_JUMP + jump.len() as u32,+ label: shorten(&label, MENU_TITLE_CHARS).replace('&', "&&"),+ checked: false,+ enabled: true,+ icon,+ });+ jump.push(target);+ }+ jumplist::Entry::Separator => items.push(MenuItem::Separator),+ }+ }+ }+ if !items.is_empty() {+ items.push(MenuItem::Separator);+ }+ let jump_rows = items.len();for (index, &hwnd) in windows.iter().take(MENU_WINDOWS).enumerate() {let title = tracker::title(hwnd);if !title.is_empty() {@@ -3687,7 +3738,7 @@ impl App {));}}- if !items.is_empty() {+ if items.len() > jump_rows {items.push(MenuItem::Separator);}let open = if windows.is_empty() {@@ -3702,6 +3753,7 @@ impl App {label: t.keep_in_dock.into(),checked: self.roster.is_pinned(id),enabled: true,+ icon: None,});}// A recent app can be dismissed while recent apps stay on.@@ -3721,6 +3773,7 @@ impl App {label: t.end_task.into(),checked: false,enabled: end_task.is_some(),+ icon: None,});}}@@ -3755,6 +3808,7 @@ impl App {label: t.empty_bin.into(),checked: false,enabled: self.bin_full,+ icon: None,});}}@@ -3762,7 +3816,7 @@ impl App {items.push(MenuItem::Separator);items.push(MenuItem::action(CMD_HIDE_FIXED, t.remove_from_dock));}- (items, end_task)+ (items, end_task, jump)}fn on_command(&mut self, target: MenuTarget, command: u32) -> Result<Option<Outside>> {@@ -3771,6 +3825,7 @@ impl App {role,windows,end_task,+ jump,} = targetelse {return match command {@@ -3861,6 +3916,16 @@ impl App {}CMD_END_TASK => end_task.map(Outside::EndTask),CMD_EMPTY_BIN => Some(Outside::EmptyBin),+ command if command >= CMD_JUMP => {+ let target = jump.into_iter().nth((command - CMD_JUMP) as usize);+ target.map(|target| {+ self.start_bounce(id.clone(), None);+ Outside::OpenJump {+ target,+ program: id,+ }+ })+ }window => {let index = window.checked_sub(CMD_WINDOW).map(|index| index as usize);index@@ -5239,6 +5304,7 @@ fn run_outside(outside: Outside) {Outside::OpenAsAdmin(id) => submit(ShellAction::OpenAsAdmin { id }),Outside::ShowDesktop => launcher::show_desktop(),Outside::UnpinFromTaskbar(id) => submit(ShellAction::Unpin { id }),+ Outside::OpenJump { target, program } => submit(ShellAction::OpenJump { target, program }),Outside::OpenSettings => {// The settings app is installed next to the dock.let settings = env::current_exe()@@ -5264,6 +5330,7 @@ fn run_outside(outside: Outside) {label: layout.name.replace('&', "&&"),checked: context.is_some_and(|context| context.layout == layout.handle),enabled: context.is_some(),+ icon: None,}).collect();if !items.is_empty() {diff --git a/apps/dock/src/menu.rs b/apps/dock/src/menu.rsindex 054a59d..02b5873 100644--- a/apps/dock/src/menu.rs+++ b/apps/dock/src/menu.rs@@ -16,6 +16,7 @@ use std::{};use anyhow::Result;+use muelle_shell::icons::IconImage;use windows::{UI::{Color,@@ -74,7 +75,11 @@ pub enum MenuItem {label: String,checked: bool,enabled: bool,+ /// Shown before the label in the drawn menu, as jump list entries.+ icon: Option<IconImage>,},+ /// The title of a section, such as a jump list's "Recent". Not selectable.+ Header(String),Separator,}@@ -85,6 +90,7 @@ impl MenuItem {label: label.into(),checked: false,enabled: true,+ icon: None,}}}@@ -200,12 +206,21 @@ fn needs_native_menu() -> bool {/// Sizes in logical pixels, after the macOS menu.const PAD_Y: f32 = 5.0;const ROW: f32 = 24.0;+const HEADER: f32 = 22.0;+const HEADER_FONT: f32 = 11.5;+const HEADER_OPACITY: f32 = 0.55;const SEPARATOR: f32 = 11.0;/// The selection is inset from the menu's sides and rounded.const INSET: f32 = 5.0;const SELECTION_RADIUS: f32 = 5.0;/// Text starts after room for a check mark.const TEXT_LEFT: f32 = 24.0;+/// With icons: the icon where the check mark goes, then the text.+const ICON: f32 = 16.0;+const ICON_LEFT: f32 = 4.0;+const ICON_TEXT_LEFT: f32 = 32.0;+/// The gray white glyphs take on a light menu, near the text's.+const DARK_GLYPH: u8 = 0x1F;const TEXT_RIGHT: f32 = 24.0;const FONT: f32 = 13.5;const MIN_WIDTH: f32 = 180.0;@@ -269,10 +284,19 @@ impl Popup {selected: LabelBrush,}let mut labels: Vec<Option<Label>> = Vec::with_capacity(items.len());+ let mut headers: Vec<Option<LabelBrush>> = Vec::with_capacity(items.len());let mut text_width: f32 = 0.0;for item in items {+ headers.push(match item {+ MenuItem::Header(title) => {+ let brush = gfx.text_brush_sized(title, scale, HEADER_FONT, true, ink)?;+ text_width = text_width.max(brush.width);+ Some(brush)+ }+ _ => None,+ });labels.push(match item {- MenuItem::Separator => None,+ MenuItem::Separator | MenuItem::Header(_) => None,MenuItem::Action { label, .. } => {let (text, _) = strip_mnemonic(label);let normal = gfx.text_brush_sized(&text, scale, FONT, false, ink)?;@@ -282,11 +306,17 @@ impl Popup {}});}- let width = (text_width + px(TEXT_LEFT) + px(TEXT_RIGHT)).max(px(MIN_WIDTH));+ // Room for icons only when some row has one.+ let has_icons = items+ .iter()+ .any(|item| matches!(item, MenuItem::Action { icon: Some(_), .. }));+ let text_left = px(if has_icons { ICON_TEXT_LEFT } else { TEXT_LEFT });+ let width = (text_width + text_left + px(TEXT_RIGHT)).max(px(MIN_WIDTH));let mut height = px(PAD_Y) * 2.0;for item in items {height += px(match item {MenuItem::Separator => SEPARATOR,+ MenuItem::Header(_) => HEADER,MenuItem::Action { .. } => ROW,});}@@ -317,7 +347,7 @@ impl Popup {};let mut rows = Vec::with_capacity(items.len());let mut top = px(PAD_Y);- for (item, label) in items.iter().zip(labels) {+ for ((item, label), header) in items.iter().zip(labels).zip(headers) {match (item, label) {(MenuItem::Action {@@ -325,6 +355,7 @@ impl Popup {label,checked,enabled,+ icon,},Some(brushes),) => {@@ -335,7 +366,7 @@ impl Popup {add_sprite(&group.Children()?,brush.brush.cast()?,- px(TEXT_LEFT),+ text_left,text_top,brush.width,brush.height,@@ -347,6 +378,33 @@ impl Popup {let normal = place(&brushes.normal)?;let selected = place(&brushes.selected)?;selected.SetIsVisible(false)?;+ if let Some(icon) = icon {+ let side = px(ICON);+ // A white glyph made for a dark taskbar (Edge's tasks)+ // takes the text's ink on a light menu; over the+ // selection it stays white, as the text does.+ let normal_icon =+ (!dark && is_white_glyph(icon)).then(|| tinted(icon, DARK_GLYPH));+ let pairs = [+ (&normal, normal_icon.as_ref().unwrap_or(icon)),+ (&selected, icon),+ ];+ for (visual, image) in pairs {+ // A bad icon costs only the icon, not the menu.+ let Ok(brush) = gfx.icon_brush(image, side as u32) else {+ continue;+ };+ let group: ContainerVisual = visual.cast()?;+ add_sprite(+ &group.Children()?,+ brush.cast()?,+ px(INSET) + px(ICON_LEFT),+ top + ((row_height - side) / 2.0).round(),+ side,+ side,+ )?;+ }+ }if *checked {for (visual, mark_ink) in [(&normal, ink), (&selected, Ink::Light)] {let mark =@@ -373,6 +431,33 @@ impl Popup {});top += row_height;}+ (MenuItem::Header(_), _) => {+ let row_height = px(HEADER);+ if let Some(brush) = header {+ let group = c.CreateContainerVisual()?;+ // Sits low in its row, near the items it names.+ add_sprite(+ &group.Children()?,+ brush.brush.cast()?,+ px(INSET) + px(7.0),+ top + (row_height - brush.height - px(2.0)).round(),+ brush.width,+ brush.height,+ )?;+ group.SetOpacity(HEADER_OPACITY)?;+ layers.InsertAtTop(&group)?;+ }+ rows.push(Row {+ command: None,+ enabled: false,+ mnemonic: None,+ top,+ height: row_height,+ normal: None,+ selected: None,+ });+ top += row_height;+ }_ => {let row_height = px(SEPARATOR);add_sprite(@@ -815,6 +900,39 @@ unsafe extern "system" fn menu_proc(hwnd: HWND, msg: u32, w: WPARAM, l: LPARAM)unsafe { DefWindowProcW(hwnd, msg, w, l) }}+/// `true` for an icon drawn in white only: every visible pixel near white.+/// Pixels are premultiplied, so white is each channel close to its alpha.+fn is_white_glyph(icon: &IconImage) -> bool {+ let visible: Vec<&[u8; 4]> = icon+ .bgra+ .as_chunks::<4>()+ .0+ .iter()+ .filter(|pixel| pixel[3] > 32)+ .collect();+ !visible.is_empty()+ && visible.iter().all(|pixel| {+ let alpha = u16::from(pixel[3]);+ pixel[..3]+ .iter()+ .all(|&channel| u16::from(channel) * 255 >= alpha * 215)+ })+}++/// `icon` in one gray `value`, keeping its shape (alpha).+fn tinted(icon: &IconImage, value: u8) -> IconImage {+ let mut bgra = icon.bgra.clone();+ for pixel in bgra.as_chunks_mut::<4>().0 {+ let channel = (u16::from(value) * u16::from(pixel[3]) / 255) as u8;+ pixel[..3].fill(channel);+ }+ IconImage {+ width: icon.width,+ height: icon.height,+ bgra,+ }+}+/// The label without its `&` marker, and the letter it marked.fn strip_mnemonic(label: &str) -> (String, Option<char>) {let mut text = String::with_capacity(label.len());@@ -885,11 +1003,18 @@ fn show_native(for item in items {let _ = match item {MenuItem::Separator => AppendMenuW(menu, MF_SEPARATOR, 0, PCWSTR::null()),+ MenuItem::Header(title) => AppendMenuW(+ menu,+ MF_STRING | MF_GRAYED,+ 0,+ &HSTRING::from(title.replace('&', "&&")),+ ),MenuItem::Action {command,label,checked,enabled,+ ..} => {let mut flags = MF_STRING;if *checked {@@ -969,6 +1094,30 @@ pub fn apply_theme(dark: bool) {mod tests {use super::*;+ fn icon(pixels: &[[u8; 4]]) -> IconImage {+ IconImage {+ width: pixels.len() as u32,+ height: 1,+ bgra: pixels.concat(),+ }+ }++ #[test]+ fn only_white_glyphs_are_tinted_and_keep_their_shape() {+ // Premultiplied: half-transparent white is 128 in every channel.+ let glyph = icon(&[[255, 255, 255, 255], [128, 128, 128, 128], [0, 0, 0, 0]]);+ assert!(is_white_glyph(&glyph));+ assert!(!is_white_glyph(&icon(&[+ [255, 255, 255, 255],+ [200, 40, 0, 255]+ ])));+ assert!(!is_white_glyph(&icon(&[[0, 0, 0, 0]])));+ let dark = tinted(&glyph, 0x1F);+ assert_eq!(dark.bgra[..4], [0x1F, 0x1F, 0x1F, 255]);+ assert_eq!(dark.bgra[7], 128);+ assert_eq!(dark.bgra[8..], [0, 0, 0, 0]);+ }+#[test]#[ignore = "Requires an interactive Windows desktop; briefly opens a menu"]fn keyboard_menu_opens_below_its_anchor_and_returns_the_selected_layout() -> Result<()> {diff --git a/apps/dock/src/shell_work.rs b/apps/dock/src/shell_work.rsindex 98d7f25..593e5ea 100644--- a/apps/dock/src/shell_work.rs+++ b/apps/dock/src/shell_work.rs@@ -5,6 +5,7 @@ use std::{env,io::{Read, Write},os::windows::{io::AsRawHandle, process::CommandExt},+ path::Path,process::{Command, Stdio},sync::{Arc, Mutex, OnceLock,@@ -14,7 +15,11 @@ use std::{};use anyhow::{Context, Result, bail};-use muelle_shell::{files, launcher, pins::unpin_from_taskbar, recycle_bin};+use muelle_shell::{+ files, jumplist, launcher,+ pins::{self, unpin_from_taskbar},+ recycle_bin, tracker,+};use serde::{Deserialize, Serialize};use windows::Win32::{Foundation::{HANDLE, HWND, WAIT_TIMEOUT},@@ -58,12 +63,20 @@ pub enum Action {Unpin {id: String,},+ /// A jump list entry of the dock item `program` (see `jumplist::open`).+ OpenJump {+ target: jumplist::Target,+ program: String,+ },}impl Action {fn launch_timeout_ms(&self) -> Option<u32> {match self {- Self::Open { .. } | Self::OpenWith { .. } | Self::Reveal { .. } => Some(30_000),+ Self::Open { .. }+ | Self::OpenWith { .. }+ | Self::Reveal { .. }+ | Self::OpenJump { .. } => Some(30_000),// File progress and UAC can legitimately wait for a user decision._ => None,}@@ -104,6 +117,15 @@ impl Action {Self::Unpin { id } => {let _ = unpin_from_taskbar(&id);}+ Self::OpenJump { target, program } => {+ // A pinned shortcut stands for the program it opens.+ let program = if tracker::is_shortcut(&program) {+ pins::link_target(Path::new(&program))+ } else {+ Some(program)+ };+ jumplist::open(&target, program.as_deref())?;+ }}Ok(())}@@ -370,6 +392,28 @@ mod tests {);}+ #[test]+ fn jump_list_entries_survive_the_request_pipe() -> Result<()> {+ let action = Action::OpenJump {+ target: jumplist::Target::Link {+ stream: vec![0x4c, 0, 0, 0, 255],+ },+ program: r"C:\Program Files\Código\app.exe".into(),+ };+ let parsed = read_action(serde_json::to_vec(&action)?.as_slice())?;+ let Action::OpenJump { target, program } = parsed else {+ bail!("la entrada de la lista de saltos cambió durante el transporte");+ };+ assert_eq!(+ target,+ jumplist::Target::Link {+ stream: vec![0x4c, 0, 0, 0, 255]+ }+ );+ assert_eq!(program, r"C:\Program Files\Código\app.exe");+ Ok(())+ }+#[test]fn oversized_requests_are_rejected_before_any_shell_call() {assert!(read_action(std::io::repeat(0).take(MAX_REQUEST_BYTES + 2)).is_err());diff --git a/apps/dock/src/strings.rs b/apps/dock/src/strings.rsindex 19a5610..75884f2 100644--- a/apps/dock/src/strings.rs+++ b/apps/dock/src/strings.rs@@ -47,9 +47,17 @@ pub struct Strings {pub shell_operation_failed: &'static str,pub keyboard_settings: &'static str,pub keyboard: &'static str,+ pub jump_pinned: &'static str,+ pub jump_frequent: &'static str,+ pub jump_recent: &'static str,+ pub jump_tasks: &'static str,}const ES: Strings = Strings {+ jump_pinned: "Anclado",+ jump_frequent: "Frecuentes",+ jump_recent: "Recientes",+ jump_tasks: "Tareas",keyboard_settings: "Ajustes de idioma y teclado…",keyboard: "Teclado",shell_operation_failed: "No se pudo iniciar la operación. Inténtalo de nuevo en un momento.",@@ -92,6 +100,10 @@ const ES: Strings = Strings {};const EN: Strings = Strings {+ jump_pinned: "Pinned",+ jump_frequent: "Frequent",+ jump_recent: "Recent",+ jump_tasks: "Tasks",keyboard_settings: "Language and keyboard settings…",keyboard: "Keyboard",shell_operation_failed: "The operation could not start. Please try again in a moment.",@@ -134,6 +146,10 @@ const EN: Strings = Strings {};const FR: Strings = Strings {+ jump_pinned: "Épinglé",+ jump_frequent: "Fréquents",+ jump_recent: "Récents",+ jump_tasks: "Tâches",keyboard_settings: "Paramètres de langue et de clavier…",keyboard: "Clavier",shell_operation_failed: "L'opération n'a pas pu démarrer. Réessayez dans un instant.",diff --git a/crates/muelle-shell/src/icons.rs b/crates/muelle-shell/src/icons.rsindex 2900f1c..afe9be4 100644--- a/crates/muelle-shell/src/icons.rs+++ b/crates/muelle-shell/src/icons.rs@@ -10,13 +10,13 @@ use windows::{},UI::{Shell::{- IShellItemImageFactory, SHCreateItemFromParsingName, SHDefExtractIconW,+ IShellItem, IShellItemImageFactory, SHCreateItemFromParsingName, SHDefExtractIconW,SIIGBF_ICONONLY,},WindowsAndMessaging::{DestroyIcon, GetIconInfo, HICON, ICONINFO},},},- core::HSTRING,+ core::{HSTRING, Interface},};#[derive(Debug, Error)]@@ -92,6 +92,23 @@ pub fn load_icon(parsing_name: &str, size: u32) -> Result<IconImage, IconError>read_pixels(&bitmap).ok_or_else(|| IconError::Pixels(parsing_name.to_owned()))}+/// The icon of a shell item (a file, a folder, a known folder) at `size`+/// pixels, as Explorer shows it.+#[must_use]+pub fn item_icon(item: &IShellItem, size: u32) -> Option<IconImage> {+ let side = size as i32;+ // SAFETY: The factory is the item's own; the bitmap is owned from here.+ let bitmap = unsafe {+ let factory: IShellItemImageFactory = item.cast().ok()?;+ OwnedBitmap(+ factory+ .GetImage(SIZE { cx: side, cy: side }, SIIGBF_ICONONLY)+ .ok()?,+ )+ };+ read_pixels(&bitmap)+}+/// 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> {diff --git a/crates/muelle-shell/src/jumplist.rs b/crates/muelle-shell/src/jumplist.rsnew file mode 100644index 0000000..7efa690--- /dev/null+++ b/crates/muelle-shell/src/jumplist.rs@@ -0,0 +1,1067 @@+//! An app's jump list, as the Windows taskbar shows it on right-click: the+//! items pinned to it, the app's own categories, its recent or frequent+//! items and its tasks.+//!+//! Only Explorer reads another app's jump list. It does so through shell+//! interfaces Windows does not document (`IApplicationResolver`,+//! `IAutomaticDestinationList` and the reading side of the destination+//! list behind `ICustomDestinationList`). The layouts below are the ones+//! Open-Shell and taskbar tools have relied on since Windows 10 (10547). If a+//! build changes them, the interfaces are not found and the list is empty:+//! the dock's menu then has its own items only.+//!+//! Requires COM to be initialized on the calling thread.++// The interfaces keep the method names Windows gives them.+#![allow(non_snake_case)]++use std::{env, ffi::c_void, iter, path::Path};++use serde::{Deserialize, Serialize};+use windows::{+ Win32::{+ Foundation::{E_FAIL, E_INVALIDARG, E_OUTOFMEMORY, HWND, PROPERTYKEY},+ Storage::EnhancedStorage::{+ PKEY_AppUserModel_ID, PKEY_AppUserModel_IsDestListSeparator, PKEY_Link_Arguments,+ PKEY_Title,+ },+ System::Com::{+ CLSCTX_INPROC_SERVER, CoCreateInstance, CoTaskMemFree, IPersistStream, IStream,+ STATFLAG_NONAME, STATSTG, STREAM_SEEK_SET,+ },+ UI::{+ Shell::{+ BHID_SFUIObject, CMF_DEFAULTONLY, CMINVOKECOMMANDINFO, CMINVOKECOMMANDINFOEX,+ Common::{IObjectArray, ITEMIDLIST},+ DestinationList, FOLDERID_AppsFolder, IContextMenu, ILGetSize, IShellItem,+ IShellItem2, IShellLinkW, KF_FLAG_DEFAULT,+ PropertiesSystem::IPropertyStore,+ SEE_MASK_FLAG_LOG_USAGE, SEE_MASK_IDLIST, SEE_MASK_NOASYNC, SHCreateItemFromIDList,+ SHCreateItemFromParsingName, SHCreateItemInKnownFolder, SHCreateMemStream,+ SHELLEXECUTEINFOW, SHGetIDListFromObject, SHGetNameFromIDList,+ SHGetPathFromIDListW, SHLoadIndirectString, SIGDN, SIGDN_DESKTOPABSOLUTEPARSING,+ SIGDN_NORMALDISPLAY, ShellExecuteExW, ShellLink,+ },+ WindowsAndMessaging::{+ CreatePopupMenu, DestroyMenu, GET_MENU_DEFAULT_ITEM_FLAGS, GetMenuDefaultItem,+ SW_SHOWNORMAL,+ },+ },+ },+ core::{+ BOOL, GUID, HRESULT, HSTRING, IUnknown, IUnknown_Vtbl, Interface, PCSTR, PCWSTR, PWSTR, w,+ },+};++use crate::{+ icons::{self, IconImage},+ launcher::allow_foreground,+ pins, tracker,+};++/// Jump list icons, in pixels: a 16-pixel menu icon at up to 200% scale.+pub const ICON_SIZE: u32 = 32;++/// Recent, frequent and the app's own items together, as the taskbar shows+/// by default (`JumpListItems_Maximum`). Pinned items and tasks are always+/// all shown.+const MAX_ITEMS: usize = 10;+/// The longest label kept, as one menu row.+const MAX_LABEL: usize = 260;+const EXPLORER_APP_ID: &str = "Microsoft.Windows.Explorer";++const CLSID_APPLICATION_RESOLVER: GUID = GUID::from_u128(0x660b90c8_73a9_4b58_8cae_355b7f55341b);+const CLSID_AUTOMATIC_DESTINATION_LIST: GUID =+ GUID::from_u128(0xf0ae1542_f497_484b_a175_a20db09144ba);+/// The destination list's reading interface changed IID within Windows 10+/// (10240, then 10547) but kept its layout.+const IID_DESTINATION_LISTS: [GUID; 2] = [+ GUID::from_u128(0x507101cd_f6ad_46c8_8e20_eeb9e6bac47f),+ GUID::from_u128(0xfebd543d_1f7b_4b38_940b_5933bd2cb21b),+];++const PKEY_APP_USER_MODEL_HOST_ENVIRONMENT: PROPERTYKEY = PROPERTYKEY {+ fmtid: GUID::from_u128(0x9f4c2855_9f79_4b39_a8d0_e1d42de1d5f3),+ pid: 14,+};+const PKEY_APP_USER_MODEL_ACTIVATION_CONTEXT: PROPERTYKEY = PROPERTYKEY {+ fmtid: GUID::from_u128(0x9f4c2855_9f79_4b39_a8d0_e1d42de1d5f3),+ pid: 20,+};+const CMIC_MASK_NOASYNC: u32 = 0x0000_0100;+const CMIC_MASK_UNICODE: u32 = 0x0000_4000;+const CMIC_MASK_FLAG_LOG_USAGE: u32 = 0x0400_0000;++#[windows_core::interface("de25675a-72de-44b4-9373-05170450c140")]+unsafe trait IApplicationResolver: IUnknown {+ fn GetAppIDForShortcut(&self, item: *mut c_void, app_id: *mut PWSTR) -> HRESULT;+ fn GetAppIDForShortcutObject(+ &self,+ link: *mut c_void,+ item: *mut c_void,+ app_id: *mut PWSTR,+ ) -> HRESULT;+ fn GetAppIDForWindow(+ &self,+ hwnd: HWND,+ app_id: *mut PWSTR,+ pinning_prevented: *mut BOOL,+ explicit: *mut BOOL,+ embedded_shortcut_valid: *mut BOOL,+ ) -> HRESULT;+}++#[windows_core::interface("e9c5ef8d-fd41-4f72-ba87-eb03bad5817c")]+unsafe trait IAutomaticDestinationList: IUnknown {+ fn Initialize(&self, app_id: PCWSTR, link: PCWSTR, reserved: PCWSTR) -> HRESULT;+ fn HasList(&self, has: *mut BOOL) -> HRESULT;+ fn GetList(+ &self,+ kind: i32,+ max: u32,+ flags: u32,+ iid: *const GUID,+ object: *mut *mut c_void,+ ) -> HRESULT;+}++/// The IID is the first of [`IID_DESTINATION_LISTS`]; the object+/// is queried for each.+#[windows_core::interface("507101cd-f6ad-46c8-8e20-eeb9e6bac47f")]+unsafe trait IDestinationList: IUnknown {+ fn SetMinItems(&self) -> HRESULT;+ fn SetApplicationID(&self, app_id: PCWSTR) -> HRESULT;+ fn GetSlotCount(&self) -> HRESULT;+ fn GetCategoryCount(&self, count: *mut u32) -> HRESULT;+ fn GetCategory(&self, index: u32, flags: i32, category: *mut Category) -> HRESULT;+ fn DeleteCategory(&self) -> HRESULT;+ fn EnumerateCategoryDestinations(+ &self,+ index: u32,+ iid: *const GUID,+ object: *mut *mut c_void,+ ) -> HRESULT;+}++#[repr(C)]+#[derive(Default)]+struct Category {+ kind: i32,+ /// A union: the name of an app's own category (`CATEGORY_CUSTOM`), or+ /// which known category it is (`CATEGORY_KNOWN`).+ name_or_known: usize,+ count: i32,+ reserved: [i32; 10],+}++const CATEGORY_CUSTOM: i32 = 0;+const CATEGORY_KNOWN: i32 = 1;+const CATEGORY_TASKS: i32 = 2;+const KNOWN_FREQUENT: i32 = 1;+const KNOWN_RECENT: i32 = 2;+const LIST_PINNED: i32 = 0;+const LIST_RECENT: i32 = 1;+const LIST_FREQUENT: i32 = 2;++#[derive(Debug, Clone, PartialEq, Eq)]+pub enum Section {+ Pinned,+ Frequent,+ Recent,+ Tasks,+ /// A category the app names itself, such as a browser's "Most visited".+ Custom(String),+}++#[derive(Debug, Clone)]+pub struct Group {+ pub section: Section,+ pub entries: Vec<Entry>,+}++#[derive(Debug, Clone)]+pub enum Entry {+ Item {+ label: String,+ target: Target,+ /// [`ICON_SIZE`] pixels square; `None` when Windows has none for it.+ icon: Option<IconImage>,+ },+ /// Between an app's tasks.+ Separator,+}++/// What a jump list entry opens, in a form that can be sent to the+/// process that opens it.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]+pub enum Target {+ /// A file or folder, by its shell ID list.+ Item { id_list: Vec<u8> },+ /// A shortcut as it saves itself to a stream: the app's tasks and most of+ /// its own categories.+ Link { stream: Vec<u8> },+}++/// The jump list of dock item `id`, whose open windows are `windows`: the+/// program's own, or, when it has none, that of the first window that names+/// one at run time. A program can also host other apps' windows (Edge, its+/// web apps), whose lists are theirs, not its.+#[must_use]+pub fn for_dock_item(id: &str, windows: &[isize]) -> Vec<Group> {+ let mut tried = Vec::new();+ let candidates = iter::once(None).chain(windows.iter().take(8).copied().map(Some));+ for window in candidates {+ let Some(app) = app_id(id, window) else {+ continue;+ };+ if tried.contains(&app) {+ continue;+ }+ let groups = read(&app);+ if !groups.is_empty() {+ return groups;+ }+ tried.push(app);+ }+ Vec::new()+}++/// The identity Windows files the jump list of dock item `id` under, as the+/// taskbar works it out: from the app's open window when it has one, or+/// else from its shortcut or program.+#[must_use]+pub fn app_id(id: &str, window: Option<isize>) -> Option<String> {+ // SAFETY: The resolver and the shell item live through each call, and+ // each returned string is freed once read.+ unsafe {+ let resolver: IApplicationResolver = CoCreateInstance::<_, IUnknown>(+ &CLSID_APPLICATION_RESOLVER,+ None,+ CLSCTX_INPROC_SERVER,+ )+ .ok()?+ .cast()+ .ok()?;+ if let Some(hwnd) = window {+ let mut app_id = PWSTR::null();+ let (mut prevented, mut explicit, mut embedded) =+ (BOOL::default(), BOOL::default(), BOOL::default());+ if resolver+ .GetAppIDForWindow(+ HWND(hwnd as *mut _),+ &mut app_id,+ &mut prevented,+ &mut explicit,+ &mut embedded,+ )+ .is_ok()+ && let Some(app_id) = take_string(app_id)+ {+ return Some(app_id);+ }+ }+ if let Some(aumid) = id.strip_prefix("shell:AppsFolder\\") {+ return Some(aumid.to_owned());+ }+ if id.starts_with("shell:") || id.starts_with("::") {+ return None;+ }+ // File Explorer files its list under its own identity, not its program's.+ let explorer = env::var("WINDIR")+ .map(|windir| format!("{windir}\\explorer.exe"))+ .is_ok_and(|explorer| explorer.eq_ignore_ascii_case(id));+ if explorer {+ return Some(EXPLORER_APP_ID.to_owned());+ }+ // A program's shortcut can give it its own identity (a browser's).+ if let Some(link) = pins::shortcut_for_program(id)+ .or_else(|| tracker::is_shortcut(id).then(|| id.to_owned()))+ {+ let item: IShellItem = SHCreateItemFromParsingName(&HSTRING::from(link), None).ok()?;+ let mut app_id = PWSTR::null();+ resolver+ .GetAppIDForShortcut(item.as_raw(), &mut app_id)+ .ok()+ .ok()?;+ return take_string(app_id);+ }+ if !tracker::is_program(id) {+ return None;+ }+ // A program without a shortcut, through one made for it in memory.+ let path = HSTRING::from(id);+ let item: IShellItem = SHCreateItemFromParsingName(&path, None).ok()?;+ let link: IShellLinkW = CoCreateInstance(&ShellLink, None, CLSCTX_INPROC_SERVER).ok()?;+ link.SetPath(&path).ok()?;+ let mut app_id = PWSTR::null();+ resolver+ .GetAppIDForShortcutObject(link.as_raw(), item.as_raw(), &mut app_id)+ .ok()+ .ok()?;+ take_string(app_id)+ }+}++/// The jump list of the app filed as `app_id`, in the taskbar's order:+/// pinned items, the app's categories (its own, recent, frequent), then its+/// tasks. Empty groups are left out.+#[must_use]+pub fn read(app_id: &str) -> Vec<Group> {+ let app_id = HSTRING::from(app_id);+ let automatic = automatic_list(&app_id);+ let custom = custom_list(&app_id);+ let mut groups = Vec::new();+ let mut seen = Vec::new();++ let pinned = automatic+ .as_ref()+ .and_then(|list| automatic_entries(list, LIST_PINNED))+ .unwrap_or_default();+ for (key, _) in &pinned {+ seen.push(key.clone());+ }+ push_group(+ &mut groups,+ Section::Pinned,+ pinned,+ usize::MAX,+ &mut Vec::new(),+ );++ let mut room = MAX_ITEMS;+ let mut tasks = None;+ let count = custom.as_ref().map_or(0, |list| {+ let mut count = 0;+ // SAFETY: `count` outlives the call.+ unsafe { list.GetCategoryCount(&mut count) }+ .ok()+ .map_or(0, |()| count)+ });+ for index in 0..count {+ let Some(list) = &custom else { break };+ let mut category = Category::default();+ // SAFETY: `category` has the layout the list fills in.+ if unsafe { list.GetCategory(index, 1, &mut category) }.is_err() {+ continue;+ }+ match category.kind {+ CATEGORY_CUSTOM => {+ // SAFETY: For an app's own category the union holds its name,+ // allocated for the caller.+ let name = unsafe { take_string(PWSTR(category.name_or_known as *mut u16)) };+ let Some(name) = name.map(|name| indirect(&name)) else {+ continue;+ };+ let entries = destinations(list, index).unwrap_or_default();+ push_group(&mut groups, Section::Custom(name), entries, room, &mut seen);+ }+ CATEGORY_KNOWN => {+ let (section, kind) = match category.name_or_known as u32 as i32 {+ KNOWN_FREQUENT => (Section::Frequent, LIST_FREQUENT),+ KNOWN_RECENT => (Section::Recent, LIST_RECENT),+ _ => continue,+ };+ let entries = automatic+ .as_ref()+ .and_then(|list| automatic_entries(list, kind))+ .unwrap_or_default();+ push_group(&mut groups, section, entries, room, &mut seen);+ }+ CATEGORY_TASKS if tasks.is_none() => tasks = Some(index),+ _ => {}+ }+ room = MAX_ITEMS.saturating_sub(shown(&groups));+ }+ // Without a list of its own, an app gets its recent items.+ if count == 0 {+ let entries = automatic+ .as_ref()+ .and_then(|list| automatic_entries(list, LIST_RECENT))+ .unwrap_or_default();+ push_group(&mut groups, Section::Recent, entries, room, &mut seen);+ }+ if let (Some(list), Some(index)) = (&custom, tasks) {+ let entries = destinations(list, index).unwrap_or_default();+ push_group(+ &mut groups,+ Section::Tasks,+ entries,+ usize::MAX,+ &mut Vec::new(),+ );+ }+ groups+}++/// Opens a jump list entry. A file goes to `program`, the app whose list it+/// is, as on the taskbar, rather than to the file type's default app.+pub fn open(target: &Target, program: Option<&str>) -> windows::core::Result<()> {+ allow_foreground();+ match target {+ Target::Item { id_list } => open_item(id_list, program),+ Target::Link { stream } => open_link(stream),+ }+}++fn shown(groups: &[Group]) -> usize {+ groups+ .iter()+ .filter(|group| !matches!(group.section, Section::Pinned | Section::Tasks))+ .map(|group| group.entries.len())+ .sum()+}++/// Adds the entries not already shown (an item pinned also stays in the+/// recent list), up to `room`, and the group only if any remain.+fn push_group(+ groups: &mut Vec<Group>,+ section: Section,+ entries: Vec<(String, Entry)>,+ room: usize,+ seen: &mut Vec<String>,+) {+ let mut kept = Vec::new();+ for (key, entry) in entries {+ if kept.len() >= room {+ break;+ }+ if !key.is_empty() && seen.contains(&key) {+ continue;+ }+ if !key.is_empty() {+ seen.push(key);+ }+ kept.push(entry);+ }+ // A separator never starts, ends or doubles in a list.+ let mut entries: Vec<Entry> = Vec::with_capacity(kept.len());+ for entry in kept {+ let after_separator = entries+ .last()+ .is_none_or(|last| matches!(last, Entry::Separator));+ if matches!(entry, Entry::Separator) && after_separator {+ continue;+ }+ entries.push(entry);+ }+ if matches!(entries.last(), Some(Entry::Separator)) {+ entries.pop();+ }+ if !entries.is_empty() {+ groups.push(Group { section, entries });+ }+}++fn automatic_list(app_id: &HSTRING) -> Option<IAutomaticDestinationList> {+ // SAFETY: `app_id` lives through the call.+ unsafe {+ let list: IAutomaticDestinationList = CoCreateInstance::<_, IUnknown>(+ &CLSID_AUTOMATIC_DESTINATION_LIST,+ None,+ CLSCTX_INPROC_SERVER,+ )+ .ok()?+ .cast()+ .ok()?;+ list.Initialize(PCWSTR(app_id.as_ptr()), PCWSTR::null(), PCWSTR::null())+ .ok()+ .ok()?;+ Some(list)+ }+}++fn custom_list(app_id: &HSTRING) -> Option<IDestinationList> {+ // SAFETY: Each interface pointer queried is owned by the wrapper it is+ // turned into; `app_id` lives through the call.+ unsafe {+ let object: IUnknown =+ CoCreateInstance(&DestinationList, None, CLSCTX_INPROC_SERVER).ok()?;+ let list = IID_DESTINATION_LISTS.iter().find_map(|iid| {+ let mut raw = std::ptr::null_mut();+ object+ .query(iid, &mut raw)+ .is_ok()+ .then(|| IDestinationList::from_raw(raw))+ })?;+ list.SetApplicationID(PCWSTR(app_id.as_ptr())).ok().ok()?;+ Some(list)+ }+}++fn automatic_entries(list: &IAutomaticDestinationList, kind: i32) -> Option<Vec<(String, Entry)>> {+ let mut raw = std::ptr::null_mut();+ // SAFETY: The returned pointer is an `IObjectArray`, owned from here.+ unsafe {+ list.GetList(kind, MAX_ITEMS as u32 * 2, 1, &IObjectArray::IID, &mut raw)+ .ok()+ .ok()?;+ Some(entries(&IObjectArray::from_raw(raw), false))+ }+}++fn destinations(list: &IDestinationList, index: u32) -> Option<Vec<(String, Entry)>> {+ let mut raw = std::ptr::null_mut();+ // SAFETY: The returned pointer is an `IObjectArray`, owned from here.+ unsafe {+ list.EnumerateCategoryDestinations(index, &IObjectArray::IID, &mut raw)+ .ok()+ .ok()?;+ let array = IObjectArray::from_raw(raw);+ let category_is_tasks = {+ let mut category = Category::default();+ list.GetCategory(index, 1, &mut category).is_ok() && category.kind == CATEGORY_TASKS+ };+ Some(entries(&array, category_is_tasks))+ }+}++/// Each entry with the key that tells whether it is already shown.+fn entries(array: &IObjectArray, tasks: bool) -> Vec<(String, Entry)> {+ // SAFETY: Reads the array's own count.+ let count = unsafe { array.GetCount() }.unwrap_or(0);+ (0..count)+ // SAFETY: Each index is below the count the array reports.+ .filter_map(|index| unsafe { array.GetAt::<IUnknown>(index) }.ok())+ .filter_map(|object| entry(&object, tasks))+ .collect()+}++fn entry(object: &IUnknown, tasks: bool) -> Option<(String, Entry)> {+ if let Ok(item) = object.cast::<IShellItem>() {+ // SAFETY: Each returned string and ID list is freed once copied.+ unsafe {+ let label = take_string(item.GetDisplayName(SIGDN_NORMALDISPLAY).ok()?)?;+ let key = item+ .GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING)+ .ok()+ .and_then(|name| take_string(name))+ .unwrap_or_default()+ .to_uppercase();+ let id_list = copy_id_list(SHGetIDListFromObject(&item).ok()?)?;+ return Some((+ key,+ Entry::Item {+ label: shorten(&label),+ target: Target::Item { id_list },+ icon: icons::item_icon(&item, ICON_SIZE),+ },+ ));+ }+ }+ let link = object.cast::<IShellLinkW>().ok()?;+ let store = link.cast::<IPropertyStore>().ok();+ if tasks+ && store+ .as_ref()+ .is_some_and(|store| property(store, &PKEY_AppUserModel_IsDestListSeparator) == "true")+ {+ return Some((String::new(), Entry::Separator));+ }+ let title = store+ .as_ref()+ .map(|store| property(store, &PKEY_Title))+ .filter(|title| !title.is_empty())+ .map(|title| indirect(&title));+ // SAFETY: The ID list is freed after its names and icon are read.+ let (name, path, target_icon) = unsafe {+ match link.GetIDList() {+ Ok(id_list) if !id_list.is_null() => {+ let name = |kind: SIGDN| {+ SHGetNameFromIDList(id_list, kind)+ .ok()+ .and_then(|name| take_string(name))+ };+ let names = (+ name(SIGDN_NORMALDISPLAY),+ name(SIGDN_DESKTOPABSOLUTEPARSING),+ SHCreateItemFromIDList::<IShellItem>(id_list)+ .ok()+ .and_then(|item| icons::item_icon(&item, ICON_SIZE)),+ );+ CoTaskMemFree(Some(id_list as *const c_void));+ names+ }+ _ => (None, None, None),+ }+ };+ let label = title.or(name).filter(|label| !label.is_empty())?;+ let arguments = store+ .as_ref()+ .map(|store| property(store, &PKEY_Link_Arguments))+ .unwrap_or_default();+ let key = format!("{}\u{0}{arguments}", path.as_deref().unwrap_or_default()).to_uppercase();+ let stream = save_link(&link)?;+ // The icon the app chose for the entry; for a Store app's task, which+ // opens the app rather than a file, the app's; else that of what it opens.+ let opens_nothing = path.as_deref().is_none_or(|path| !Path::new(path).exists());+ let icon = link_icon(&link)+ .or_else(|| {+ opens_nothing+ .then(|| {+ store+ .as_ref()+ .map(|store| property(store, &PKEY_AppUserModel_ID))+ })+ .flatten()+ .and_then(|app_id| app_icon(&app_id))+ })+ .or(target_icon);+ Some((+ key,+ Entry::Item {+ label: shorten(&label),+ target: Target::Link { stream },+ icon,+ },+ ))+}++fn link_icon(link: &IShellLinkW) -> Option<IconImage> {+ let mut path = [0u16; 1024];+ let mut index = 0;+ // SAFETY: The buffer and index outlive the call.+ unsafe { link.GetIconLocation(&mut path, &mut index) }.ok()?;+ let len = path.iter().position(|&unit| unit == 0).unwrap_or(0);+ let path = expand_variables(&String::from_utf16_lossy(&path[..len]));+ if path.is_empty() {+ return None;+ }+ icons::load_icon_at(&path, index, ICON_SIZE).ok()+}++/// The icon of the app filed as `app_id` in the Apps folder.+fn app_icon(app_id: &str) -> Option<IconImage> {+ if app_id.is_empty() {+ return None;+ }+ // SAFETY: Only resolves the app's shell item.+ let item = unsafe {+ SHCreateItemInKnownFolder::<_, IShellItem>(+ &FOLDERID_AppsFolder,+ KF_FLAG_DEFAULT,+ &HSTRING::from(app_id),+ )+ }+ .ok()?;+ icons::item_icon(&item, ICON_SIZE)+}++/// Replaces `%NAME%` with the environment variable, as icon locations write+/// `%ProgramFiles%`. Unknown names are kept as they are.+fn expand_variables(text: &str) -> String {+ let mut out = String::with_capacity(text.len());+ let mut rest = text;+ while let Some(start) = rest.find('%') {+ out.push_str(&rest[..start]);+ let after = &rest[start + 1..];+ match after.find('%') {+ Some(end) => {+ let name = &after[..end];+ match env::var(name) {+ Ok(value) if !name.is_empty() => out.push_str(&value),+ _ => {+ out.push('%');+ out.push_str(name);+ out.push('%');+ }+ }+ rest = &after[end + 1..];+ }+ None => {+ out.push_str(&rest[start..]);+ rest = "";+ }+ }+ }+ out.push_str(rest);+ out+}++fn save_link(link: &IShellLinkW) -> Option<Vec<u8>> {+ // SAFETY: The buffer has the length the stream reports and is read into+ // only up to it.+ unsafe {+ let persist: IPersistStream = link.cast().ok()?;+ let stream: IStream = SHCreateMemStream(None)?;+ persist.Save(&stream, true).ok()?;+ let mut stat = STATSTG::default();+ stream.Stat(&mut stat, STATFLAG_NONAME).ok()?;+ let length = u32::try_from(stat.cbSize).ok()?;+ stream.Seek(0, STREAM_SEEK_SET, None).ok()?;+ let mut bytes = vec![0u8; length as usize];+ let mut read = 0;+ stream+ .Read(bytes.as_mut_ptr().cast(), length, Some(&mut read))+ .ok()+ .ok()?;+ bytes.truncate(read as usize);+ Some(bytes)+ }+}++fn load_link(stream: &[u8]) -> windows::core::Result<IShellLinkW> {+ // SAFETY: The stream is a copy of `stream`, alive through the load.+ unsafe {+ let link: IShellLinkW = CoCreateInstance(&ShellLink, None, CLSCTX_INPROC_SERVER)?;+ let memory = SHCreateMemStream(Some(stream))+ .ok_or_else(|| windows::core::Error::from_hresult(E_OUTOFMEMORY))?;+ link.cast::<IPersistStream>()?.Load(&memory)?;+ Ok(link)+ }+}++/// Copies and frees an ID list the shell allocated.+///+/// # Safety+/// `id_list` must be null or a valid ID list allocated with `CoTaskMemAlloc`.+unsafe fn copy_id_list(id_list: *mut ITEMIDLIST) -> Option<Vec<u8>> {+ if id_list.is_null() {+ return None;+ }+ // SAFETY: `ILGetSize` gives the size of the whole list, terminator included.+ unsafe {+ let size = ILGetSize(Some(id_list)) as usize;+ let bytes = std::slice::from_raw_parts(id_list.cast::<u8>(), size).to_vec();+ CoTaskMemFree(Some(id_list as *const c_void));+ is_id_list(&bytes).then_some(bytes)+ }+}++/// `true` when `bytes` is a whole ID list: items whose sizes stay inside it,+/// then the two-byte zero that ends it. Checked before the shell reads an ID+/// list received from another process.+fn is_id_list(bytes: &[u8]) -> bool {+ let mut offset = 0usize;+ loop {+ let Some(size) = bytes+ .get(offset..offset + 2)+ .map(|size| usize::from(u16::from_le_bytes([size[0], size[1]])))+ else {+ return false;+ };+ if size == 0 {+ return offset + 2 == bytes.len();+ }+ if size < 2 {+ return false;+ }+ offset += size;+ }+}++fn open_item(id_list: &[u8], program: Option<&str>) -> windows::core::Result<()> {+ if !is_id_list(id_list) {+ return Err(windows::core::Error::from_hresult(E_INVALIDARG));+ }+ let pointer = id_list.as_ptr().cast::<ITEMIDLIST>();+ let mut path = [0u16; 260];+ // SAFETY: `id_list` is a whole ID list (checked above).+ let file = unsafe { SHGetPathFromIDListW(pointer, &mut path) }+ .as_bool()+ .then(|| {+ let len = path+ .iter()+ .position(|&unit| unit == 0)+ .unwrap_or(path.len());+ String::from_utf16_lossy(&path[..len])+ })+ .filter(|file| Path::new(file).is_file());+ let program = program.filter(|program| tracker::is_program(program));+ let (file_name, parameters) = match (program, file) {+ (Some(program), Some(file)) => (+ Some(HSTRING::from(program)),+ Some(HSTRING::from(format!("\"{file}\""))),+ ),+ _ => (None, None),+ };+ let mut request = SHELLEXECUTEINFOW {+ cbSize: size_of::<SHELLEXECUTEINFOW>() as u32,+ fMask: SEE_MASK_NOASYNC | SEE_MASK_FLAG_LOG_USAGE,+ lpVerb: w!("open"),+ nShow: SW_SHOWNORMAL.0,+ ..Default::default()+ };+ if let (Some(file_name), Some(parameters)) = (&file_name, ¶meters) {+ request.lpFile = PCWSTR(file_name.as_ptr());+ request.lpParameters = PCWSTR(parameters.as_ptr());+ } else {+ request.fMask |= SEE_MASK_IDLIST;+ request.lpVerb = PCWSTR::null();+ request.lpIDList = pointer.cast_mut().cast();+ }+ // SAFETY: The strings and the ID list live through the call.+ unsafe { ShellExecuteExW(&mut request) }+}++/// A shortcut opens through its default command, as the taskbar does; a+/// Store app's task, through the app with the task's activation context.+fn open_link(stream: &[u8]) -> windows::core::Result<()> {+ let link = load_link(stream)?;+ let mut menu: IContextMenu = link.cast()?;+ let mut parameters = String::new();+ if let Ok(store) = link.cast::<IPropertyStore>() {+ let app_id = property(&store, &PKEY_AppUserModel_ID);+ if !app_id.is_empty() {+ // SAFETY: Only resolves and queries the app's own shell item.+ let packaged = unsafe {+ SHCreateItemInKnownFolder::<_, IShellItem2>(+ &FOLDERID_AppsFolder,+ KF_FLAG_DEFAULT,+ &HSTRING::from(app_id),+ )+ }+ .ok()+ .filter(|app| {+ // SAFETY: Reads a number property of the item.+ unsafe { app.GetUInt32(&PKEY_APP_USER_MODEL_HOST_ENVIRONMENT) }+ .is_ok_and(|host| host != 0)+ });+ if let Some(app) = packaged {+ // SAFETY: Binds to the item's own context menu.+ let app_menu =+ unsafe { app.BindToHandler::<_, IContextMenu>(None, &BHID_SFUIObject) };+ if let Ok(app_menu) = app_menu {+ menu = app_menu;+ parameters = property(&store, &PKEY_APP_USER_MODEL_ACTIVATION_CONTEXT);+ }+ }+ }+ }+ invoke_default(&menu, ¶meters)+}++fn invoke_default(menu: &IContextMenu, parameters: &str) -> windows::core::Result<()> {+ // SAFETY: The popup menu is destroyed before returning; the strings live+ // through the call.+ unsafe {+ let popup = CreatePopupMenu()?;+ let result = (|| {+ menu.QueryContextMenu(popup, 0, 1, 0x7FFF, CMF_DEFAULTONLY)+ .ok()?;+ let id = GetMenuDefaultItem(popup, 0, GET_MENU_DEFAULT_ITEM_FLAGS(0));+ if id == u32::MAX || id == 0 {+ return Err(windows::core::Error::from_hresult(E_FAIL));+ }+ let verb = (id - 1) as usize;+ let parameters = HSTRING::from(parameters);+ let request = CMINVOKECOMMANDINFOEX {+ cbSize: size_of::<CMINVOKECOMMANDINFOEX>() as u32,+ fMask: CMIC_MASK_UNICODE | CMIC_MASK_NOASYNC | CMIC_MASK_FLAG_LOG_USAGE,+ lpVerb: PCSTR(verb as *const u8),+ lpVerbW: PCWSTR(verb as *const u16),+ lpParametersW: if parameters.is_empty() {+ PCWSTR::null()+ } else {+ PCWSTR(parameters.as_ptr())+ },+ nShow: SW_SHOWNORMAL.0,+ ..Default::default()+ };+ menu.InvokeCommand((&raw const request).cast::<CMINVOKECOMMANDINFO>())+ })();+ let _ = DestroyMenu(popup);+ result+ }+}++fn property(store: &IPropertyStore, key: &PROPERTYKEY) -> String {+ // SAFETY: Reads a property into an owned value.+ unsafe { store.GetValue(key) }.map_or_else(|_| String::new(), |value| value.to_string())+}++/// Resolves `@dll,-id` and `ms-resource:` labels.+fn indirect(text: &str) -> String {+ if !text.starts_with('@') {+ return text.to_owned();+ }+ let mut buffer = [0u16; 512];+ // SAFETY: Writes into a bounded buffer.+ if unsafe { SHLoadIndirectString(&HSTRING::from(text), &mut buffer, None) }.is_ok() {+ let len = buffer+ .iter()+ .position(|&unit| unit == 0)+ .unwrap_or(buffer.len());+ if len > 0 {+ return String::from_utf16_lossy(&buffer[..len]);+ }+ }+ text.to_owned()+}++fn shorten(label: &str) -> String {+ label.chars().take(MAX_LABEL).collect()+}++/// Reads and frees a string the shell allocated.+///+/// # Safety+/// `text` must be null or a string allocated with `CoTaskMemAlloc`.+unsafe fn take_string(text: PWSTR) -> Option<String> {+ if text.is_null() {+ return None;+ }+ // SAFETY: Guaranteed by the caller.+ unsafe {+ let value = text.to_string().ok();+ CoTaskMemFree(Some(text.0 as *const c_void));+ value.filter(|value| !value.is_empty())+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn id_lists_must_end_inside_their_bytes() {+ assert!(is_id_list(&[0, 0]));+ assert!(is_id_list(&[4, 0, 7, 7, 0, 0]));+ assert!(!is_id_list(&[]));+ assert!(!is_id_list(&[4, 0, 7, 7]));+ assert!(!is_id_list(&[9, 0, 7, 7, 0, 0]));+ assert!(!is_id_list(&[1, 0, 0, 0]));+ assert!(!is_id_list(&[0, 0, 0]));+ }++ #[test]+ fn separators_never_start_end_or_double_a_group() {+ let item = |label: &str| {+ (+ label.to_owned(),+ Entry::Item {+ label: label.to_owned(),+ target: Target::Item {+ id_list: vec![0, 0],+ },+ icon: None,+ },+ )+ };+ let separator = || (String::new(), Entry::Separator);+ let mut groups = Vec::new();+ push_group(+ &mut groups,+ Section::Tasks,+ vec![+ separator(),+ item("A"),+ separator(),+ separator(),+ item("B"),+ separator(),+ ],+ usize::MAX,+ &mut Vec::new(),+ );+ let shape: Vec<bool> = groups[0]+ .entries+ .iter()+ .map(|entry| matches!(entry, Entry::Separator))+ .collect();+ assert_eq!(shape, [false, true, false]);+ }++ #[test]+ fn items_already_shown_and_beyond_the_room_are_left_out() {+ let item = |key: &str| {+ (+ key.to_owned(),+ Entry::Item {+ label: key.to_owned(),+ target: Target::Item {+ id_list: vec![0, 0],+ },+ icon: None,+ },+ )+ };+ let mut groups = Vec::new();+ let mut seen = vec!["PINNED".to_owned()];+ push_group(+ &mut groups,+ Section::Recent,+ vec![item("PINNED"), item("A"), item("B"), item("C")],+ 2,+ &mut seen,+ );+ push_group(+ &mut groups,+ Section::Frequent,+ vec![item("PINNED")],+ 5,+ &mut seen,+ );+ assert_eq!(groups.len(), 1);+ let labels: Vec<&str> = groups[0]+ .entries+ .iter()+ .filter_map(|entry| match entry {+ Entry::Item { label, .. } => Some(label.as_str()),+ Entry::Separator => None,+ })+ .collect();+ assert_eq!(labels, ["A", "B"]);+ }++ #[test]+ fn icon_locations_expand_known_variables_and_keep_the_rest() {+ // SAFETY: Only this test reads the variable.+ unsafe { std::env::set_var("MUELLE_JUMP_TEST", r"C:\Apps") };+ assert_eq!(+ expand_variables(r"%MUELLE_JUMP_TEST%\app.exe"),+ r"C:\Apps\app.exe"+ );+ assert_eq!(expand_variables("%NO_SUCH_VAR_X%\\a"), "%NO_SUCH_VAR_X%\\a");+ assert_eq!(expand_variables("50% off"), "50% off");+ assert_eq!(expand_variables(r"C:\plain.exe"), r"C:\plain.exe");+ }++ /// Prints the jump lists of the dock's usual apps, to check the+ /// undocumented interfaces against a new Windows build.+ #[test]+ #[ignore = "reads this user's jump lists; prints them"]+ fn prints_jump_lists_of_common_apps() {+ use windows::Win32::System::Com::{COINIT_APARTMENTTHREADED, CoInitializeEx};+ // SAFETY: COM for this test thread.+ let _ = unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) };+ let windir = std::env::var("WINDIR").unwrap_or_default();+ let mut ids = vec![+ format!("{windir}\\explorer.exe"),+ format!("{windir}\\notepad.exe"),+ "shell:AppsFolder\\Microsoft.Windows.Explorer".to_owned(),+ ];+ ids.extend(pins::taskbar_pins());+ for id in ids {+ let app = app_id(&id, None);+ println!("{id} -> {app:?}");+ for group in app.as_deref().map(read).unwrap_or_default() {+ println!(" [{:?}]", group.section);+ for entry in &group.entries {+ match entry {+ Entry::Item {+ label,+ target,+ icon,+ } => println!(+ " {label} ({}, icon {})",+ match target {+ Target::Item { id_list } =>+ format!("item, {} bytes", id_list.len()),+ Target::Link { stream } => {+ assert!(load_link(stream).is_ok(), "{label} does not reload");+ format!("link, {} bytes", stream.len())+ }+ },+ icon.as_ref().map_or_else(+ || "none".to_owned(),+ |icon| format!("{}x{}", icon.width, icon.height)+ )+ ),+ Entry::Separator => println!(" ----"),+ }+ }+ }+ }+ }+}diff --git a/crates/muelle-shell/src/launcher.rs b/crates/muelle-shell/src/launcher.rsindex 43f16ee..494c9a2 100644--- a/crates/muelle-shell/src/launcher.rs+++ b/crates/muelle-shell/src/launcher.rs@@ -42,7 +42,7 @@ pub struct LaunchError {/// The dock never takes the focus, so Windows may refuse the foreground to the/// app it starts and open it behind other windows. The click that asked for it/// was the dock's, which lets the dock hand that right on.-fn allow_foreground() {+pub(crate) fn allow_foreground() {// SAFETY: No pointers; failure only means Windows keeps its default.unsafe {let _ = AllowSetForegroundWindow(ASFW_ANY);diff --git a/crates/muelle-shell/src/lib.rs b/crates/muelle-shell/src/lib.rsindex 01fb400..419dbd0 100644--- a/crates/muelle-shell/src/lib.rs+++ b/crates/muelle-shell/src/lib.rs@@ -8,6 +8,7 @@ pub mod folder;pub mod icon_cache;pub mod icons;pub mod imagery;+pub mod jumplist;pub mod keyboard;pub mod launcher;pub mod log;diff --git a/crates/muelle-shell/src/pins.rs b/crates/muelle-shell/src/pins.rsindex 08e8c66..8c9db4a 100644--- a/crates/muelle-shell/src/pins.rs+++ b/crates/muelle-shell/src/pins.rs@@ -373,6 +373,31 @@ pub fn launch_target(id: &str) -> Option<String> {.map(|link| link.to_string_lossy().into_owned())}+/// Any shortcut, kept by Muelle or pinned to the Windows taskbar, that opens+/// the program `id`, with or without arguments. It carries the identity+/// Windows gives the program's jump list (`AppUserModelID`).+///+/// Requires COM to be initialized on the calling thread.+#[must_use]+pub fn shortcut_for_program(id: &str) -> Option<String> {+ if crate::tracker::is_shortcut(id) || id.starts_with("shell:") {+ return None;+ }+ [kept_folder(), taskbar_folder()]+ .into_iter()+ .flatten()+ .filter_map(|folder| fs::read_dir(folder).ok())+ .flatten()+ .filter_map(|entry| Some(entry.ok()?.path()))+ .filter(|link| crate::tracker::is_shortcut(&link.to_string_lossy()))+ .find(|link| {+ read_link(link)+ .and_then(|(target, _)| target)+ .is_some_and(|target| normalize_id(&target) == id)+ })+ .map(|link| link.to_string_lossy().into_owned())+}+/// The shortcut Muelle keeps for the program `id` (see [`keep_shortcut`]),/// which gives its pin its name and icon. Only Muelle's own folder, which/// is usually empty: cheap enough for every icon lookup.diff --git a/spikes/src/bin/spike_menu.rs b/spikes/src/bin/spike_menu.rsindex 97f14ba..7179c46 100644--- a/spikes/src/bin/spike_menu.rs+++ b/spikes/src/bin/spike_menu.rs@@ -85,12 +85,14 @@ fn main() -> anyhow::Result<()> {label: "&Mantener en el Dock".into(),checked: true,enabled: true,+ icon: None,},menu::MenuItem::Action {command: 4,label: "Abrir al iniciar sesión".into(),checked: false,enabled: false,+ icon: None,},menu::MenuItem::Separator,menu::MenuItem::action(5, "&Salir de Muelle"),