use super::*;
impl App {
pub(crate) fn show_toast(&mut self, text: String, ctx: &egui::Context) {
self.toast = Some(Toast {
text,
until: ctx.input(|i| i.time) + TOAST_SECONDS,
});
}
pub(crate) fn copied_ui(&mut self, copied: Copied, ctx: &egui::Context) {
self.too_big = copied.too_big.map(|too_big| (too_big, false));
self.show_toast(copied.message, ctx);
}
pub(crate) fn too_big_ui(&mut self, ctx: &egui::Context) {
let Some((too_big, working)) = &self.too_big else {
return;
};
let mut make_smaller = false;
let mut close = false;
egui::Area::new(egui::Id::unique("too big"))
.order(egui::Order::Foreground)
.anchor(egui::Align2::CENTER_TOP, Vec2::new(0.0, 56.0))
.show(ctx, |ui| {
let visuals = ui.visuals();
egui::Frame::new()
.fill(visuals.extreme_bg_color)
.stroke(visuals.window_stroke)
.corner_radius(8.0)
.shadow(visuals.popup_shadow)
.inner_margin(egui::Margin::symmetric(14, 10))
.show(ui, |ui| {
ui.set_max_width(card_width(ctx));
ui.label(
RichText::new(format!(
"Copied {}. At {} it is over Discord's 10 MB limit.",
too_big.name,
hdrshot_core::share::megabytes(too_big.size)
))
.size(14.0),
);
ui.add_space(4.0);
ui.horizontal(|ui| {
if *working {
ui.spinner();
ui.label("Making a smaller copy…");
} else {
make_smaller = ui
.button("Copy a smaller version")
.on_hover_text(
"Copy it at 1440p, or lower if needed, so it fits \
in 10 MB. The saved PNGs stay as they are.",
)
.clicked();
close = ui
.button("Keep this one")
.on_hover_text("Keep the full-size copy on the clipboard")
.clicked();
}
});
});
});
if close {
self.too_big = None;
self.toast = None;
} else if make_smaller {
let clipboard = self.clipboard.clone();
let options = self.convert_options(Existing::Replace);
let too_big = too_big.clone();
self.too_big = Some((too_big.clone(), true));
convert::in_background(self.copied.0.clone(), ctx.clone(), move || {
Copied::from(convert::copy_smaller(&clipboard, &too_big, &options))
});
}
}
pub(crate) fn toast_ui(&mut self, ctx: &egui::Context) {
while let Ok(copied) = self.copied.1.try_recv() {
self.copied_ui(copied, ctx);
}
if self.too_big.is_some() {
self.too_big_ui(ctx);
return;
}
let Some(toast) = &self.toast else { return };
let left = toast.until - ctx.input(|i| i.time);
if left <= 0.0 {
self.toast = None;
return;
}
let opacity = (left / 0.5).min(1.0) as f32;
egui::Area::new(egui::Id::unique("toast"))
.order(egui::Order::Foreground)
.anchor(egui::Align2::CENTER_TOP, Vec2::new(0.0, 56.0))
.interactable(false)
.show(ctx, |ui| {
ui.set_opacity(opacity);
let visuals = ui.visuals();
egui::Frame::new()
.fill(visuals.extreme_bg_color)
.stroke(visuals.window_stroke)
.corner_radius(8.0)
.shadow(visuals.popup_shadow)
.inner_margin(egui::Margin::symmetric(14, 10))
.show(ui, |ui| {
ui.set_max_width(card_width(ctx));
ui.label(RichText::new(&toast.text).size(14.0));
});
});
ctx.request_repaint();
}
}
pub(crate) struct Toast {
pub(crate) text: String,
pub(crate) until: f64,
}
pub(crate) const TOAST_SECONDS: f64 = 3.0;
pub(crate) fn card_width(ctx: &egui::Context) -> f32 {
(ctx.content_rect().width() - 2.0 * 16.0 - 2.0 * 14.0).max(120.0)
}