Notification - Part 4

Notification

An existing Rust Crate called notify_rust helped with building and displaying a desktop notification.

In the example below.

Track = a struct to store Track information.

pub struct Track {
    pub artist: String,
    pub album: String,
    pub album_art: String,
    pub title: String,
}

A new notification is created and then later shown on screen.

Note. The use of handle.update(); since you don’t want to display dozens of notification pop-ups on screen.

update() allows re-use of an existing notification.

This comes in handy especially if tracks are changed frequently.

.icon = Allows us to display the album art of a specific track. This is a local image stored on disk.

use std::time::Duration;
use crate::track::Track;

use notify_rust;

const TIMEOUT: i32 = 3000;

pub struct Notification {
    pub app_name: String,
    pub summary: String,
    pub body: String,
    pub icon: String,
    pub timeout: i32,
}

impl Notification {
    pub fn display(track: &mut Track) {
        let notify_body = format! {"{} - {}",&track.artist,&track.album};
        let mut handle = notify_rust::Notification::new()
            .appname("Spotify")
            .summary(&track.title)
            .body(&notify_body)
            .icon(&track.album_art)
            .timeout(TIMEOUT)
            .show()
            .unwrap();

        std::thread::sleep(Duration::from_millis(2_000));
        handle.update();

        handle.close();
    }
}

Here is the final result.

Each notification property includes:

appname = Spotify

summary = Track Title

body = Track Artist and Album title

icon = Album Art

Last updated on 5 Sep 2022
Published on 5 Sep 2022