Handle messages from a relm widget

Let's now see how to handle the SongStarted message. To do so, we use a syntax similar to the one for handling gtk events. The message is on the left side of => while the handler is on the right side of it:

#[widget]
impl Widget for App {
    // …

    view! {
        // …
        #[name="playlist"]
        Playlist {
            SongStarted(ref pixbuf) => Started(pixbuf.clone()),
        }
    }
}

We can see here that when we receive the SongStarted message from the playlist, we emit the Started message on the same widget (App). We needed to use ref and then clone() the value contained in the message here because we do not own the message. Indeed, multiple widgets can listen to the same message, the widget that emitted the message and its parent. Before we handle this new message, we'll add it to our Msg enumeration:

#[derive(Msg)]
pub enum Msg {
    Open,
    PlayPause,
    Quit,
    Save,
    Started(Option<Pixbuf>),
    Stop,
}

This variant takes an optional pixbuf because some MP3 files do not have a cover image inside them. And here's how we handle this message:

fn update(&mut self, event: Msg) {
    match event {
        // …
        Started(pixbuf) => {
            self.set_play_icon(PAUSE_ICON);
            self.model.cover_visible = true;
            self.model.cover_pixbuf = pixbuf;
        },
    }
}

When the song starts playing, we show the pause icon and the cover.