util: Add error toast helper

This commit is contained in:
Elias Projahn 2025-03-23 09:58:45 +01:00
parent 538e559f8f
commit f977cac3fd
3 changed files with 37 additions and 76 deletions

View file

@ -1,16 +1,15 @@
use std::cell::OnceCell; use std::cell::OnceCell;
use adw::subclass::prelude::*; use adw::subclass::prelude::*;
use gettextrs::gettext;
use gtk::{ use gtk::{
gio, gio,
glib::{self, clone, Properties}, glib::{self, Properties},
prelude::*, prelude::*,
}; };
use crate::{ use crate::{
db::models::*, editor::album::AlbumEditor, library::Library, player::Player, db::models::*, editor::album::AlbumEditor, library::Library, player::Player,
playlist_item::PlaylistItem, recording_tile::RecordingTile, util::error_dialog::ErrorDialog, playlist_item::PlaylistItem, recording_tile::RecordingTile, util,
}; };
mod imp { mod imp {
@ -101,20 +100,7 @@ mod imp {
.library() .library()
.delete_album(&obj.imp().album.get().unwrap().album_id) .delete_album(&obj.imp().album.get().unwrap().album_id)
{ {
let toast = adw::Toast::builder() util::error_toast("Failed to delete album", err, &obj.toast_overlay());
.title(&gettext("Failed to delete album"))
.button_label("Details")
.build();
toast.connect_button_clicked(clone!(
#[weak]
obj,
move |_| {
ErrorDialog::present(&err, &obj);
}
));
obj.toast_overlay().add_toast(toast);
} }
}) })
.build(); .build();

View file

@ -5,7 +5,7 @@ use formatx::formatx;
use gettextrs::gettext; use gettextrs::gettext;
use gtk::{ use gtk::{
gio, gio,
glib::{self, clone, Properties}, glib::{self, Properties},
prelude::*, prelude::*,
}; };
@ -24,7 +24,7 @@ use crate::{
recording_tile::RecordingTile, recording_tile::RecordingTile,
search_tag::Tag, search_tag::Tag,
tag_tile::TagTile, tag_tile::TagTile,
util::error_dialog::ErrorDialog, util,
}; };
mod imp { mod imp {
@ -229,74 +229,26 @@ impl SearchPage {
match highlight { match highlight {
Tag::Composer(person) | Tag::Performer(person) => { Tag::Composer(person) | Tag::Performer(person) => {
if let Err(err) = self.library().delete_person(&person.person_id) { if let Err(err) = self.library().delete_person(&person.person_id) {
let toast = adw::Toast::builder() util::error_toast("Failed to delete person", err, &self.toast_overlay());
.title(&gettext("Failed to delete person"))
.button_label("Details")
.build();
toast.connect_button_clicked(clone!(
#[weak(rename_to = obj)]
self,
move |_| {
ErrorDialog::present(&err, &obj);
}
));
self.toast_overlay().add_toast(toast);
} }
} }
Tag::Ensemble(ensemble) => { Tag::Ensemble(ensemble) => {
if let Err(err) = self.library().delete_ensemble(&ensemble.ensemble_id) { if let Err(err) = self.library().delete_ensemble(&ensemble.ensemble_id) {
let toast = adw::Toast::builder() util::error_toast("Failed to delete ensemble", err, &self.toast_overlay());
.title(&gettext("Failed to delete ensemble"))
.button_label("Details")
.build();
toast.connect_button_clicked(clone!(
#[weak(rename_to = obj)]
self,
move |_| {
ErrorDialog::present(&err, &obj);
}
));
self.toast_overlay().add_toast(toast);
} }
} }
Tag::Instrument(instrument) => { Tag::Instrument(instrument) => {
if let Err(err) = self.library().delete_instrument(&instrument.instrument_id) { if let Err(err) = self.library().delete_instrument(&instrument.instrument_id) {
let toast = adw::Toast::builder() util::error_toast(
.title(&gettext("Failed to delete instrument")) "Failed to delete instrument",
.button_label("Details") err,
.build(); &self.toast_overlay(),
);
toast.connect_button_clicked(clone!(
#[weak(rename_to = obj)]
self,
move |_| {
ErrorDialog::present(&err, &obj);
}
));
self.toast_overlay().add_toast(toast);
} }
} }
Tag::Work(work) => { Tag::Work(work) => {
if let Err(err) = self.library().delete_work(&work.work_id) { if let Err(err) = self.library().delete_work(&work.work_id) {
let toast = adw::Toast::builder() util::error_toast("Failed to delete work", err, &self.toast_overlay());
.title(&gettext("Failed to delete work"))
.button_label("Details")
.build();
toast.connect_button_clicked(clone!(
#[weak(rename_to = obj)]
self,
move |_| {
ErrorDialog::present(&err, &obj);
}
));
self.toast_overlay().add_toast(toast);
} }
} }
} }

View file

@ -2,9 +2,12 @@ pub mod activatable_row;
pub mod drag_widget; pub mod drag_widget;
pub mod error_dialog; pub mod error_dialog;
use gtk::glib; use gettextrs::gettext;
use gtk::glib::{self, clone};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use error_dialog::ErrorDialog;
lazy_static! { lazy_static! {
/// The user's language code. /// The user's language code.
pub static ref LANG: String = { pub static ref LANG: String = {
@ -20,3 +23,23 @@ lazy_static! {
lang lang
}; };
} }
/// Create and show an error toast. This will also log the error to the console.
pub fn error_toast(msgid: &str, err: anyhow::Error, toast_overlay: &adw::ToastOverlay) {
log::error!("{msgid}: {err:?}");
let toast = adw::Toast::builder()
.title(&gettext(msgid))
.button_label("Details")
.build();
toast.connect_button_clicked(clone!(
#[weak]
toast_overlay,
move |_| {
ErrorDialog::present(&err, &toast_overlay);
}
));
toast_overlay.add_toast(toast);
}