fractal/utils/macros.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
//! Collection of macros.
/// Spawn a future on the default `MainContext`
///
/// This was taken from `gtk-macros`
/// but allows setting optionally the priority
///
/// FIXME: this should maybe be upstreamed
#[macro_export]
macro_rules! spawn {
($future:expr) => {
let ctx = glib::MainContext::default();
ctx.spawn_local($future);
};
($priority:expr, $future:expr) => {
let ctx = glib::MainContext::default();
ctx.spawn_local_with_priority($priority, $future);
};
}
/// Spawn a future on the tokio runtime
#[macro_export]
macro_rules! spawn_tokio {
($future:expr) => {
$crate::RUNTIME.spawn($future)
};
}
/// Show a toast with the given message on the ancestor window of `widget`.
///
/// The simplest way to use this macros is for displaying a simple message. It
/// can be anything that implements `AsRef<str>`.
///
/// ```no_run
/// use gettextts::gettext;
///
/// use crate::toast;
///
/// # let widget = unimplemented!();
/// toast!(widget, gettext("Something happened"));
/// ```
///
/// This macro also supports replacing named variables with their value. It
/// supports both the `var` and the `var = expr` syntax. In this case the
/// message and the variables must be `String`s.
///
/// ```no_run
/// use gettextts::gettext;
///
/// use crate::toast;
///
/// # let widget = unimplemented!();
/// # let error_nb = 0;
/// toast!(
/// widget,
/// gettext("Error number {n}: {msg}"),
/// n = error_nb.to_string(),
/// msg,
/// );
/// ```
///
/// To add `Pill`s to the toast, you can precede a [`Room`] or [`User`] with
/// `@`.
///
/// ```no_run
/// use gettextts::gettext;
/// use crate::toast;
/// use crate::session::model::{Room, User};
///
/// # let session = unimplemented!();
/// # let room_id = unimplemented!();
/// # let user_id = unimplemented!();
/// let room = Room::new(session, room_id);
/// let member = Member::new(room, user_id);
///
/// toast!(
/// widget,
/// gettext("Could not contact {user} in {room}"),
/// @user = member,
/// @room,
/// );
/// ```
///
/// For this macro to work, the ancestor window be a [`Window`](crate::Window)
/// or an [`adw::PreferencesWindow`].
///
/// [`Room`]: crate::session::model::Room
/// [`User`]: crate::session::model::User
#[macro_export]
macro_rules! toast {
($widget:expr, $message:expr) => {
{
$crate::_add_toast!($widget, adw::Toast::new($message.as_ref()));
}
};
($widget:expr, $message:expr, $($tail:tt)+) => {
{
let (string_vars, pill_vars) = $crate::_toast_accum!([], [], $($tail)+);
let string_dict: Vec<_> = string_vars
.iter()
.map(|(key, val): &(&str, String)| (key.as_ref(), val.as_ref()))
.collect();
let message = $crate::utils::freplace($message.into(), &*string_dict);
let toast = if pill_vars.is_empty() {
adw::Toast::new($message.as_ref())
} else {
let pill_vars = std::collections::HashMap::<&str, $crate::components::Pill>::from(pill_vars);
let mut swapped_label = String::new();
let mut widgets = Vec::with_capacity(pill_vars.len());
let mut last_end = 0;
let mut matches = pill_vars
.keys()
.map(|key: &&str| {
message
.match_indices(&format!("{{{key}}}"))
.map(|(start, _)| (start, key))
.collect::<Vec<_>>()
})
.flatten()
.collect::<Vec<_>>();
matches.sort_unstable();
for (start, key) in matches {
swapped_label.push_str(&message[last_end..start]);
swapped_label.push_str($crate::components::LabelWithWidgets::PLACEHOLDER);
last_end = start + key.len() + 2;
widgets.push(pill_vars.get(key).unwrap().clone())
}
swapped_label.push_str(&message[last_end..message.len()]);
let widget = $crate::components::LabelWithWidgets::new();
widget.set_label_and_widgets(
swapped_label,
widgets,
);
adw::Toast::builder()
.custom_title(&widget)
.build()
};
$crate::_add_toast!($widget, toast);
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! _toast_accum {
([$($string_vars:tt)*], [$($pill_vars:tt)*], $var:ident, $($tail:tt)*) => {
$crate::_toast_accum!([$($string_vars)* (stringify!($var), $var),], [$($pill_vars)*], $($tail)*)
};
([$($string_vars:tt)*], [$($pill_vars:tt)*], $var:ident = $val:expr, $($tail:tt)*) => {
$crate::_toast_accum!([$($string_vars)* (stringify!($var), $val),], [$($pill_vars)*], $($tail)*)
};
([$($string_vars:tt)*], [$($pill_vars:tt)*], @$var:ident, $($tail:tt)*) => {
{
use $crate::components::PillSourceExt;
let pill: $crate::components::Pill = $var.to_pill();
$crate::_toast_accum!([$($string_vars)*], [$($pill_vars)* (stringify!($var), pill),], $($tail)*)
}
};
([$($string_vars:tt)*], [$($pill_vars:tt)*], @$var:ident = $val:expr, $($tail:tt)*) => {
{
use $crate::components::PillSourceExt;
let pill: $crate::components::Pill = $val.to_pill();
$crate::_toast_accum!([$($string_vars)*], [$($pill_vars)* (stringify!($var), pill),], $($tail)*)
}
};
([$($string_vars:tt)*], [$($pill_vars:tt)*],) => { ([$($string_vars)*], [$($pill_vars)*]) };
}
#[doc(hidden)]
#[macro_export]
macro_rules! _add_toast {
($widget:expr, $toast:expr) => {{
use gtk::prelude::WidgetExt;
if let Some(dialog) = $widget
.ancestor($crate::components::ToastableDialog::static_type())
.and_downcast::<$crate::components::ToastableDialog>()
{
use $crate::prelude::ToastableDialogExt;
dialog.add_toast($toast);
} else if let Some(dialog) = $widget
.ancestor(adw::PreferencesDialog::static_type())
.and_downcast::<adw::PreferencesDialog>()
{
use adw::prelude::PreferencesDialogExt;
dialog.add_toast($toast);
} else if let Some(root) = $widget.root() {
// FIXME: AdwPreferencesWindow is deprecated but RoomDetails uses it.
#[allow(deprecated)]
if let Some(window) = root.downcast_ref::<adw::PreferencesWindow>() {
use adw::prelude::PreferencesWindowExt;
window.add_toast($toast);
} else if let Some(window) = root.downcast_ref::<$crate::Window>() {
window.add_toast($toast);
} else {
panic!("Trying to display a toast when the parent doesn't support it");
}
}
}};
}