The count on the icon
Os::badge puts a count, such as the number of unread messages, on the application's icon.
Setting the count
impl Os {
pub fn badge(&self, count: u32) -> Result<(), Error>;
}Os::badge shows count on the application's icon, and 0 clears it. The count stays until the next call, whichever window is open or focused, and goes when the application exits. It fails only while the application is closing. A handler that changes the number of unread messages sets the count as well as the signal the page shows:
use hypersig::Action;
use hypersig_desktop::Os;
#[derive(Signals, Serialize, Deserialize)]
struct Inbox {
unread: u32,
}
#[derive(Clone)]
struct AppState {
os: Os,
}
hypersig::routes! {
fn pages(Router<AppState>);
const INBOX: Route = "/" => inbox;
action READ_ALL: Action<Inbox> = post "/read-all" => read_all;
}
/// Marks every message read, and clears the count on the icon.
async fn read_all(state: AppState, (): ()) -> Patches<Inbox> {
if let Err(error) = state.os.badge(0) {
eprintln!("the badge was not cleared: {error}");
}
Inbox::unread().patch(0).into()
}In a test, FakeOs::badge returns the count the handlers last set, 0 when none is shown.
How each system shows it
| System | Where | What |
|---|---|---|
| macOS | The Dock icon | The number |
| Linux | The launcher entry of <program>.desktop | The number, where the dock reads Unity's launcher API, as Ubuntu's does |
| Windows | The taskbar button of each window | A red dot, since a button has no room for a number |
On Linux, <program> is the name of the running executable, which is the name cargo-packager gives the desktop file of a Debian package and an AppImage. GNOME without an extension shows no count. On macOS the Dock's label was read back as the count set; on Windows and Linux the badge has not been looked at on screen yet.
Next
- Testing without a window:
FakeOsand what it records.