use super::*;
impl App {
pub(crate) fn show_toast(&mut self, text: String, ctx: &egui::Context) {
self.show_toast_for(text, TOAST_SECONDS, false, ctx);
}
pub(crate) fn show_toast_for(
&mut self,
text: String,
seconds: f64,
warning: bool,
ctx: &egui::Context,
) {
self.toast = Some(Toast {
text,
until: ctx.input(|i| i.time) + seconds,
warning,
});
}
pub(crate) fn copied_ui(&mut self, copied: Copied, ctx: &egui::Context) {
let now = ctx.input(|i| i.time);
self.too_big = copied.too_big.map(|too_big| (too_big, false));
match &mut self.toast {
Some(toast) if toast.warning && toast.until > now => {
if self.too_big.is_none() {
toast.text = format!("{}\n{}", toast.text, copied.message);
toast.until = toast.until.max(now + TOAST_SECONDS);
}
}
_ => 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 now = ctx.input(|i| i.time);
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));
if let Some(warning) =
self.toast.as_ref().filter(|t| t.warning && t.until > now)
{
ui.label(RichText::new(&warning.text).size(14.0));
ui.add_space(4.0);
}
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 undo_ui(&mut self, ctx: &egui::Context) -> bool {
let running = self.running();
let Some(removed) = &mut self.removed else {
return false;
};
let now = ctx.input(|i| i.time);
if now >= removed.until || running {
self.removed = None;
return false;
}
let count = removed.rows.len();
let mut undo = false;
let area = egui::Area::new(egui::Id::unique("undo"))
.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, 8))
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(
RichText::new(format!("Removed {}", images(count))).size(14.0),
);
ui.add_space(8.0);
let shortcut = if cfg!(target_os = "macos") {
"Cmd+Z"
} else {
"Ctrl+Z"
};
undo = ui
.button("Undo")
.on_hover_text(format!("Put them back ({shortcut})"))
.clicked();
});
});
});
if area.response.contains_pointer() {
removed.until = removed.until.max(now + 2.0);
}
if undo {
self.undo_remove();
} else {
ctx.request_repaint_after(std::time::Duration::from_secs_f64(removed.until - now));
}
true
}
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 top = if self.undo_ui(ctx) { 104.0 } else { 56.0 };
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, top))
.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) warning: bool,
}
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)
}