Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/crates/lbm-agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ features = [
"Win32_System_RemoteDesktop",
"Win32_System_TaskScheduler",
"Win32_System_Threading",
"Win32_UI_HiDpi",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging",
]
Expand Down
8 changes: 8 additions & 0 deletions rust/crates/lbm-agent/icons/menu/Exit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/exit-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/exit-20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/exit-24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/exit-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/exit-40.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/exit-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/open-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/open-20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/open-24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/open-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/open-40.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/open-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/refresh-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/refresh-20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/refresh-24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/refresh-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/refresh-40.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/refresh-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/start-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/start-20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/start-24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/start-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/start-40.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/start-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/crates/lbm-agent/icons/menu/stop-16.png
Binary file added rust/crates/lbm-agent/icons/menu/stop-20.png
Binary file added rust/crates/lbm-agent/icons/menu/stop-24.png
Binary file added rust/crates/lbm-agent/icons/menu/stop-32.png
Binary file added rust/crates/lbm-agent/icons/menu/stop-40.png
Binary file added rust/crates/lbm-agent/icons/menu/stop-48.png
12 changes: 12 additions & 0 deletions rust/crates/lbm-agent/icons/render.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ for state in on off dead paused; do
rsvg-convert -w "$size" -h "$size" "$svg/lbm_$state.svg" -o "$here/lbm_$state-$size.png"
done
done

# The menu's entries: the UI's own pictograms (Start, Stop, Refresh, and the logo for
# Open), and Exit drawn here after lbm-ui's Icon::Power, the UI having none. Sizes: the
# small icon at 100, 125, 150, 200, 250 and 300 % (SM_CXSMICON).
for pair in open:lbm_logo start:Start stop:Stop refresh:Refresh exit:; do
name=${pair%%:*} source=${pair#*:}
file="$svg/$source.svg"
[ -n "$source" ] || file="$here/menu/Exit.svg"
for size in 16 20 24 32 40 48; do
rsvg-convert -w "$size" -h "$size" "$file" -o "$here/menu/$name-$size.png"
done
done
219 changes: 219 additions & 0 deletions rust/crates/lbm-agent/src/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,108 @@ pub fn pixmaps(icon: TrayIcon) -> &'static [Pixmap] {
&decoded[icon.index()]
}

/// The pictogram of an entry of the tray's menu.
///
/// Under Linux the menu host draws a themed icon by its name when the Icon Naming Spec
/// has one, so that it follows KDE's light or dark theme as the rest of the menu does;
/// the UI's pictograms are black where they mean "the foreground", which a dark menu
/// would swallow. Only Open, the logo, has no such name and goes as a PNG: its colours
/// read on either background. Windows has no theme to ask: every entry is a bitmap of
/// the UI's own SVGs (`icons/render.sh`), drawn on the light menu Win32 shows to an
/// application that did not opt into the dark one — the agent does not.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MenuIcon {
/// LittleBigMouse's logo: Open shows the application.
Logo,
Start,
Stop,
Refresh,
Exit,
}

impl MenuIcon {
pub const ALL: [MenuIcon; 5] = [
MenuIcon::Logo,
MenuIcon::Start,
MenuIcon::Stop,
MenuIcon::Refresh,
MenuIcon::Exit,
];

/// Its name in the freedesktop Icon Naming Spec, which every conforming theme (Breeze,
/// Adwaita, hicolor's fallbacks) draws; `None` for the logo, which is ours.
pub fn theme_name(self) -> Option<&'static str> {
match self {
MenuIcon::Logo => None,
MenuIcon::Start => Some("media-playback-start"),
MenuIcon::Stop => Some("media-playback-stop"),
MenuIcon::Refresh => Some("view-refresh"),
MenuIcon::Exit => Some("application-exit"),
}
}

/// The embedded PNGs, smallest first.
pub fn pngs(self) -> &'static [(u32, &'static [u8])] {
&MENU_PNGS[self as usize]
}

/// Every size, decoded, smallest first.
pub fn pixmaps(self) -> &'static [Pixmap] {
static DECODED: OnceLock<Vec<Vec<Pixmap>>> = OnceLock::new();
let decoded = DECODED.get_or_init(|| {
MENU_PNGS
.iter()
.map(|sizes| sizes.iter().map(|(_, png)| decode(png)).collect())
.collect()
});
&decoded[self as usize]
}
}

macro_rules! menu_pngs {
($name:literal) => {
menu_pngs!(@ $name, 16, 20, 24, 32, 40, 48)
};
(@ $name:literal, $($size:literal),*) => {
[$((
$size,
include_bytes!(concat!("../icons/menu/", $name, "-", $size, ".png")).as_slice(),
)),*]
};
}

/// In [`MenuIcon`] order.
const MENU_PNGS: [[(u32, &[u8]); 6]; 5] = [
menu_pngs!("open"),
menu_pngs!("start"),
menu_pngs!("stop"),
menu_pngs!("refresh"),
menu_pngs!("exit"),
];

/// The first of `pixmaps` (smallest first) at least `wanted` wide, or the largest: a
/// smaller one would be stretched into a blur, a larger one is drawn shrunk.
pub fn at_least(pixmaps: &[Pixmap], wanted: u32) -> &Pixmap {
pixmaps
.iter()
.find(|p| p.width >= wanted)
.unwrap_or_else(|| pixmaps.last().expect("icons of every size"))
}

/// `pixmap` as the rows of a 32-bit DIB: BGRA, its colours premultiplied by their alpha,
/// as `AlphaBlend` — which draws a menu item's bitmap — reads them. Straight colours there
/// come out brighter than they are on every half-transparent edge.
pub fn premultiplied_bgra(pixmap: &Pixmap) -> Vec<u8> {
let times = |c: u8, a: u8| ((u16::from(c) * u16::from(a) + 127) / 255) as u8;
pixmap
.argb
.as_chunks::<4>()
.0
.iter()
.flat_map(|&[a, r, g, b]| [times(b, a), times(g, a), times(r, a), a])
.collect()
}

/// An embedded PNG (8-bit RGBA, as `rsvg-convert` writes them) as ARGB32.
fn decode(png: &[u8]) -> Pixmap {
let mut decoder = png::Decoder::new(png);
Expand Down Expand Up @@ -298,6 +400,123 @@ mod tests {
);
}

#[test]
fn every_menu_icon_has_every_size_decoded() {
for icon in MenuIcon::ALL {
let sizes: Vec<u32> = icon.pixmaps().iter().map(|p| p.width).collect();
assert_eq!(sizes, [16, 20, 24, 32, 40, 48], "{icon:?}");
let named: Vec<u32> = icon.pngs().iter().map(|&(size, _)| size).collect();
assert_eq!(named, sizes, "{icon:?}: the table says the sizes it embeds");
for p in icon.pixmaps() {
assert_eq!(
(p.height as usize, p.argb.len()),
(p.width as usize, (p.width * p.width * 4) as usize)
);
assert!(
p.argb.chunks(4).any(|px| px[0] == 255),
"{icon:?} draws something"
);
assert!(
p.argb.chunks(4).any(|px| px[0] == 0),
"{icon:?} is transparent around"
);
}
}
}

#[test]
fn the_menu_icons_are_the_uis_pictograms() {
// The 48 px renders of the UI's SVGs: Start's green outline, Stop's red one, the
// logo's blue screens; Refresh and Exit in the foreground's black.
let colours = |icon: MenuIcon| -> Vec<[u8; 3]> {
let p = &icon.pixmaps()[5];
p.argb
.as_chunks::<4>()
.0
.iter()
.filter(|px| px[0] == 255)
.map(|px| [px[1], px[2], px[3]])
.collect()
};
let has = |icon, test: fn(&[u8; 3]) -> bool| colours(icon).iter().any(test);
assert!(has(MenuIcon::Start, |&[r, g, b]| g > 200
&& r < 100
&& b < 100));
assert!(has(MenuIcon::Stop, |&[r, g, b]| r > 150
&& g < 60
&& b < 60));
assert!(has(MenuIcon::Logo, |&[r, g, b]| b > 150
&& r < 60
&& g < 160));
for icon in [MenuIcon::Refresh, MenuIcon::Exit] {
assert!(
colours(icon)
.iter()
.all(|&[r, g, b]| r < 40 && g < 40 && b < 40),
"{icon:?}"
);
}
}

#[test]
fn the_menu_names_are_the_icon_naming_specs() {
let names: Vec<Option<&str>> = MenuIcon::ALL.iter().map(|i| i.theme_name()).collect();
assert_eq!(
names,
[
None,
Some("media-playback-start"),
Some("media-playback-stop"),
Some("view-refresh"),
Some("application-exit"),
]
);
}

#[test]
fn at_least_takes_the_next_size_up() {
let sizes = MenuIcon::Stop.pixmaps();
assert_eq!(at_least(sizes, 16).width, 16);
assert_eq!(
at_least(sizes, 18).width,
20,
"125 % of 15 rounds to 18 on some setups"
);
assert_eq!(at_least(sizes, 32).width, 32);
assert_eq!(
at_least(sizes, 64).width,
48,
"past the largest: the largest"
);
assert_eq!(at_least(sizes, 0).width, 16);
}

#[test]
fn premultiplied_bgra_swaps_and_scales_the_colours() {
let pixmap = Pixmap {
width: 4,
height: 1,
argb: vec![
255, 10, 20, 30, // opaque: unchanged, reordered
0, 200, 100, 50, // transparent: black
128, 255, 0, 64, // half: halved, rounded
51, 255, 255, 255, // a fifth of white
],
};
assert_eq!(
premultiplied_bgra(&pixmap),
[30, 20, 10, 255, 0, 0, 0, 0, 32, 0, 128, 128, 51, 51, 51, 51]
);
// Never a colour over its alpha, which AlphaBlend would overflow.
for icon in MenuIcon::ALL {
for p in icon.pixmaps() {
for px in premultiplied_bgra(p).as_chunks::<4>().0 {
assert!(px[..3].iter().all(|&c| c <= px[3]), "{icon:?}");
}
}
}
}

#[test]
fn the_icon_follows_the_csharp_mapping() {
assert_eq!(TrayIcon::for_state("Running", false), On);
Expand Down
42 changes: 42 additions & 0 deletions rust/crates/lbm-agent/src/tray/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl ksni::Tray for AgentTray {
.map(|entry| match *entry {
Some(action) => StandardItem {
label: action.label().to_owned(),
icon_name: action.icon().theme_name().unwrap_or_default().to_owned(),
icon_data: menu_png(action.icon()),
activate: Box::new(move |t: &mut AgentTray| t.0.act(action)),
..Default::default()
}
Expand All @@ -66,6 +68,20 @@ impl ksni::Tray for AgentTray {
}
}

/// The PNG a menu entry carries when the theme has no icon of that name (the logo): the
/// 32 px one, which the host shrinks for a menu at 100 % and keeps sharp at 200 %. An
/// entry with a name carries none, so that no host prefers our black over its theme.
fn menu_png(icon: crate::icons::MenuIcon) -> Vec<u8> {
if icon.theme_name().is_some() {
return Vec::new();
}
icon.pngs()
.iter()
.find(|&&(size, _)| size == 32)
.map(|&(_, png)| png.to_vec())
.unwrap_or_default()
}

/// Puts the tray up and keeps it on the agent's state until the agent goes. Without a
/// tray host (no StatusNotifierWatcher: a bare session, a test runner) it says so and
/// returns: the agent runs on without one.
Expand Down Expand Up @@ -141,6 +157,32 @@ mod tests {
})
.collect();
assert_eq!(labels, ["Open", "Start", "Stop", "Refresh", "-", "Exit"]);
let icons: Vec<(String, bool)> = tray
.menu()
.iter()
.filter_map(|item| match item {
MenuItem::Standard(s) => Some((s.icon_name.clone(), !s.icon_data.is_empty())),
_ => None,
})
.collect();
assert_eq!(
icons,
[
(String::new(), true),
("media-playback-start".to_owned(), false),
("media-playback-stop".to_owned(), false),
("view-refresh".to_owned(), false),
("application-exit".to_owned(), false),
],
"a theme's icon where it has one, else ours"
);
let MenuItem::Standard(open) = &tray.menu()[0] else {
panic!("an entry");
};
assert!(
open.icon_data.starts_with(b"\x89PNG"),
"a PNG, as DBusMenu wants"
);

let MenuItem::Standard(stop) = tray.menu().remove(2) else {
panic!("an entry");
Expand Down
29 changes: 28 additions & 1 deletion rust/crates/lbm-agent/src/tray/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde_json::{json, Value};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};

use crate::api::{self, Call, Request};
use crate::icons::{Badge, TrayIcon};
use crate::icons::{Badge, MenuIcon, TrayIcon};

#[cfg(target_os = "linux")]
mod linux;
Expand Down Expand Up @@ -68,6 +68,18 @@ impl Action {
}
}

/// Its pictogram, the UI's for the same thing: Start and Stop are the toolbar's
/// buttons, Refresh its reload, Open the application's logo.
pub fn icon(self) -> MenuIcon {
match self {
Action::Open => MenuIcon::Logo,
Action::Start => MenuIcon::Start,
Action::Stop => MenuIcon::Stop,
Action::Refresh => MenuIcon::Refresh,
Action::Exit => MenuIcon::Exit,
}
}

/// The request it sends; `None` for Open, which launches the frontend instead.
fn method(self) -> Option<&'static str> {
match self {
Expand Down Expand Up @@ -303,6 +315,21 @@ mod tests {
assert!(calls.try_recv().is_err(), "Open asks the agent nothing");
}

#[test]
fn every_entry_has_an_icon_of_its_own() {
let icons: Vec<MenuIcon> = Action::MENU.iter().flatten().map(|a| a.icon()).collect();
assert_eq!(
icons,
[
MenuIcon::Logo,
MenuIcon::Start,
MenuIcon::Stop,
MenuIcon::Refresh,
MenuIcon::Exit
]
);
}

#[test]
fn the_hide_option_is_followed_both_ways() {
// It is a state like any other: the agent publishes it, so a change made from any
Expand Down
Loading
Loading