use super::*;
const FORMATS: [&str; 8] = [
"JPEG XR",
"HDR JPEG",
"HEIC",
"HDR AVIF",
"OpenEXR",
"Radiance HDR",
"JPEG XL",
"HDR PNG",
];
const WATCH_TIP: &str = "watch-folder";
const SCREENSHOT_TIP: &str = "screenshots";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Tip {
Screenshot { offer_tray: bool },
Watch,
}
fn choose_tip(
hidden: &[String],
shortcut_on: bool,
always_running: bool,
can_run_in_background: bool,
watch_applies: bool,
) -> Option<Tip> {
let hidden = |name: &str| hidden.iter().any(|t| t == name);
if shortcut_on && !hidden(SCREENSHOT_TIP) {
Some(Tip::Screenshot {
offer_tray: can_run_in_background && !always_running,
})
} else if watch_applies && !hidden(WATCH_TIP) {
Some(Tip::Watch)
} else {
None
}
}
const MIN_DROP_HEIGHT: f32 = 180.0;
impl App {
pub(crate) fn add_paths(&mut self, paths: impl IntoIterator<Item = PathBuf>) {
let mut skipped = 0;
for path in paths {
self.settings.view = View::Files;
if path.is_dir() {
for file in hdrshot_core::find_inputs(&path, true) {
self.add_file(file);
}
} else if hdrshot_core::is_supported(&path) {
self.add_file(path);
} else {
skipped += 1;
}
}
self.notice = (skipped > 0).then(|| {
format!(
"Skipped {skipped} file{} that {} not a supported image.",
if skipped == 1 { "" } else { "s" },
if skipped == 1 { "is" } else { "are" }
)
});
}
pub(crate) fn add_file(&mut self, path: PathBuf) {
if !self.files.iter().any(|e| e.path == path) {
let size = hdrshot_core::input::dimensions(&path).ok();
let bytes = std::fs::metadata(&path).ok().map(|m| m.len());
self.previews.request(&path);
self.files.push(Entry {
info: input_info(&path, size, bytes),
path,
status: Status::Waiting,
size,
saved: String::new(),
thumb: Thumb::Loading,
});
}
}
pub(crate) fn choose_files(&mut self) {
if let Some(paths) = rfd::FileDialog::new()
.set_title("Choose HDR images")
.add_filter("HDR images", &INPUT_EXTENSIONS)
.pick_files()
{
self.add_paths(paths);
}
}
pub(crate) fn empty_ui(&mut self, ui: &mut egui::Ui) {
let tip_id = egui::Id::unique("empty window tip height");
let tip_height = ui.data(|d| d.get_temp::<f32>(tip_id)).unwrap_or_default();
let available = ui.available_height();
let tip = self
.current_tip()
.filter(|_| available - tip_height >= MIN_DROP_HEIGHT);
let reserved = if tip.is_some() { tip_height } else { 0.0 };
self.drop_area(ui, (available - reserved).max(MIN_DROP_HEIGHT));
let top = ui.cursor().top();
match tip {
Some(Tip::Screenshot { offer_tray }) => self.screenshot_tip(ui, offer_tray),
Some(Tip::Watch) => self.watch_tip(ui),
None => return,
}
let used = ui.cursor().top() - top;
if (used - tip_height).abs() > 0.5 {
ui.data_mut(|d| d.insert_temp(tip_id, used));
ui.ctx().request_discard("the tip's height changed");
}
}
fn current_tip(&self) -> Option<Tip> {
let s = &self.settings;
choose_tip(
&s.hidden_tips,
self.shortcut_name().is_some(),
s.tray && (s.start_with_windows || !startup::SUPPORTED),
tray::SUPPORTED,
self.watch_tip_applies(),
)
}
fn watch_tip_applies(&self) -> bool {
let folders = watching::capture_folders();
!self.settings.watch
&& !folders.is_empty()
&& self
.settings
.watch_dir
.as_ref()
.is_none_or(|dir| folders.iter().any(|f| &f.dir == dir))
}
fn screenshot_tip(&mut self, ui: &mut egui::Ui, offer_tray: bool) {
let Some(keys) = self.shortcut_name() else {
return;
};
let text = if offer_tray {
format!(
"Press {keys} in a game to save an HDR screenshot. The shortcut \
only works while hdrshot runs, so keep it in the tray to have it \
ready."
)
} else {
format!(
"Press {keys} in a game to save an HDR screenshot. The shortcut \
only works while hdrshot runs, open or in the tray."
)
};
let buttons: &[&str] = if offer_tray {
&["Keep in tray and start with Windows", "Not now"]
} else {
&["Got it"]
};
match stacked_tip(ui, icons::camera, "Take HDR screenshots", &text, buttons) {
Some(0) if offer_tray => {
let ctx = ui.ctx().clone();
self.settings.tray = true;
let started = !startup::SUPPORTED || {
self.settings.start_with_windows = true;
self.apply_start_with_windows(&ctx)
};
if started {
self.show_toast(
"hdrshot now starts with Windows and stays in the tray".into(),
&ctx,
);
}
self.settings.hidden_tips.push(SCREENSHOT_TIP.into());
}
Some(_) => self.settings.hidden_tips.push(SCREENSHOT_TIP.into()),
None => {}
}
}
fn watch_tip(&mut self, ui: &mut egui::Ui) {
let Some(folder) = watching::capture_folders().first() else {
return;
};
match tip(
ui,
icons::eye,
&format!("Watch the {} folder", folder.app),
&format!(
"Convert new {} screenshots as soon as you take them, while \
hdrshot is open.",
folder.app
),
"Turn on",
) {
Some(true) => {
let ctx = ui.ctx().clone();
self.show_toast(format!("Watching {}", folder.dir.display()), &ctx);
self.settings.watch_dir = Some(folder.dir.clone());
self.settings.watch = true;
}
Some(false) => self.settings.hidden_tips.push(WATCH_TIP.into()),
None => {}
}
}
fn drop_area(&mut self, ui: &mut egui::Ui, height: f32) {
let (rect, response) =
ui.allocate_exact_size(Vec2::new(ui.available_width(), height), Sense::click());
let visuals = ui.visuals();
let accent = visuals.selection.bg_fill;
let hovered = response.hovered();
let painter = ui.painter();
if hovered {
painter.rect_filled(rect, 12.0, accent.gamma_multiply(0.08));
painter.rect_stroke(rect, 12.0, Stroke::new(1.5, accent), StrokeKind::Inside);
} else {
painter.rect_filled(rect, 12.0, visuals.faint_bg_color);
let stroke = Stroke::new(1.5, visuals.weak_text_color().gamma_multiply(0.45));
dashed_outline(painter, rect.shrink(1.0), 12.0, stroke);
}
let center = rect.center() - Vec2::new(0.0, 16.0);
let circle = center - Vec2::new(0.0, 46.0);
painter.circle_filled(
circle,
30.0,
accent.gamma_multiply(if hovered { 0.24 } else { 0.14 }),
);
icons::image(
painter,
egui::Rect::from_center_size(circle, Vec2::splat(28.0)),
accent,
);
painter.text(
center,
egui::Align2::CENTER_CENTER,
"Drop HDR images or folders here",
theme::semibold(16.0),
visuals.text_color(),
);
let paste = if cfg!(target_os = "macos") {
"Cmd+V"
} else {
"Ctrl+V"
};
painter.text(
center + Vec2::new(0.0, 24.0),
egui::Align2::CENTER_CENTER,
format!("or click to choose files. {paste} pastes copied files."),
egui::FontId::proportional(13.0),
visuals.weak_text_color(),
);
format_tags(
painter,
center + Vec2::new(0.0, 58.0),
visuals,
rect.width(),
);
if response
.on_hover_cursor(egui::CursorIcon::PointingHand)
.clicked()
{
self.choose_files();
}
}
pub(crate) fn toolbar(&mut self, ui: &mut egui::Ui) {
ui.horizontal(|ui| {
let total = self.files.len();
let converted = self
.files
.iter()
.filter(|e| matches!(e.status, Status::Done(_)))
.count();
let mut count = images(total);
if converted > 0 {
count = format!("{count}, {converted} converted");
}
ui.label(RichText::new(count).weak());
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if theme::icon_text_button(ui, icons::plus, "Add files")
.on_hover_text("Choose more images. You can also drop them on the window.")
.clicked()
{
self.choose_files();
}
ui.add_enabled_ui(!self.running(), |ui| {
ui.menu_button("Clear", |ui| {
let ctx = ui.ctx().clone();
if ui.button("All files").clicked() {
self.clear_all(&ctx);
}
if ui
.add_enabled(converted > 0, egui::Button::new("Converted files"))
.on_hover_text("Keep the files still waiting or failed")
.clicked()
{
self.clear_finished(&ctx);
}
})
.response
.on_hover_text("Remove files from the list");
});
});
});
}
}
pub(crate) fn drop_overlay(ctx: &egui::Context) {
if ctx.input(|i| i.raw.hovered_files.is_empty()) || dragout::active() {
return;
}
let layer = egui::LayerId::new(egui::Order::Foreground, egui::Id::unique("drop overlay"));
let painter = ctx.layer_painter(layer);
let style = ctx.global_style();
let visuals = &style.visuals;
let accent = visuals.selection.bg_fill;
let screen = ctx.content_rect();
painter.rect_filled(screen, 0.0, visuals.panel_fill);
let target = screen.shrink(12.0);
painter.rect_filled(target, 14.0, accent.gamma_multiply(0.10));
dashed_outline(&painter, target, 14.0, Stroke::new(2.0, accent));
let center = target.center();
let circle = center - Vec2::new(0.0, 44.0);
painter.circle_filled(circle, 34.0, accent.gamma_multiply(0.2));
icons::drop(
&painter,
egui::Rect::from_center_size(circle, Vec2::splat(30.0)),
accent,
);
painter.text(
center + Vec2::new(0.0, 8.0),
egui::Align2::CENTER_CENTER,
"Release to add",
theme::semibold(18.0),
visuals.text_color(),
);
painter.text(
center + Vec2::new(0.0, 34.0),
egui::Align2::CENTER_CENTER,
"Nothing is converted until you press Convert.",
egui::FontId::proportional(13.0),
visuals.weak_text_color(),
);
}
fn format_tags(painter: &egui::Painter, center: egui::Pos2, visuals: &egui::Visuals, width: f32) {
let font = egui::FontId::proportional(11.5);
let color = visuals.weak_text_color();
let gap = 6.0;
let mut galleys: Vec<_> = FORMATS
.iter()
.map(|f| painter.layout_no_wrap((*f).to_owned(), font.clone(), color))
.collect();
let row_width = |galleys: &[std::sync::Arc<egui::Galley>]| {
galleys.iter().map(|g| g.size().x + 16.0 + gap).sum::<f32>() - gap
};
while galleys.len() > 1 && row_width(&galleys) > width - 32.0 {
galleys.pop();
}
let mut x = center.x - row_width(&galleys) / 2.0;
for galley in galleys {
let size = galley.size() + Vec2::new(16.0, 6.0);
let rect = egui::Rect::from_min_size(egui::pos2(x, center.y - size.y / 2.0), size);
painter.rect(
rect,
size.y / 2.0,
visuals.widgets.inactive.bg_fill,
Stroke::NONE,
StrokeKind::Inside,
);
painter.galley(rect.center() - galley.size() / 2.0, galley, color);
x = rect.right() + gap;
}
}
fn tip(
ui: &mut egui::Ui,
icon: icons::Icon,
title: &str,
text: &str,
button: &str,
) -> Option<bool> {
let mut clicked = None;
theme::card(ui, |ui| {
ui.horizontal(|ui| {
let accent = theme::accent(ui);
let (rect, _) = ui.allocate_exact_size(Vec2::splat(32.0), Sense::hover());
ui.painter()
.circle_filled(rect.center(), 16.0, accent.gamma_multiply(0.14));
icon(
ui.painter(),
egui::Rect::from_center_size(rect.center(), Vec2::splat(16.0)),
accent,
);
ui.add_space(4.0);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if theme::small_icon_button(ui, icons::cross)
.on_hover_text("Don't show this again")
.clicked()
{
clicked = Some(false);
}
if ui.button(button).clicked() {
clicked = Some(true);
}
ui.add_space(4.0);
ui.with_layout(egui::Layout::top_down(egui::Align::Min), |ui| {
ui.spacing_mut().item_spacing.y = 2.0;
ui.label(RichText::new(title).font(theme::semibold(13.5)));
ui.label(RichText::new(text).size(12.5).weak());
});
});
});
});
clicked
}
fn stacked_tip(
ui: &mut egui::Ui,
icon: icons::Icon,
title: &str,
text: &str,
buttons: &[&str],
) -> Option<usize> {
let mut clicked = None;
theme::card(ui, |ui| {
ui.horizontal_top(|ui| {
let accent = theme::accent(ui);
let (rect, _) = ui.allocate_exact_size(Vec2::splat(32.0), Sense::hover());
ui.painter()
.circle_filled(rect.center(), 16.0, accent.gamma_multiply(0.14));
icon(
ui.painter(),
egui::Rect::from_center_size(rect.center(), Vec2::splat(16.0)),
accent,
);
ui.add_space(4.0);
ui.vertical(|ui| {
ui.spacing_mut().item_spacing.y = 2.0;
ui.label(RichText::new(title).font(theme::semibold(13.5)));
ui.label(RichText::new(text).size(12.5).weak());
ui.add_space(6.0);
ui.horizontal_wrapped(|ui| {
for (i, label) in buttons.iter().enumerate() {
let button = if i == 0 {
theme::primary(
ui,
egui::Button::new(
RichText::new(*label).color(egui::Color32::WHITE),
),
)
} else {
egui::Button::new(*label)
};
if ui.add(button).clicked() {
clicked = Some(i);
}
}
});
});
});
});
clicked
}
#[cfg(test)]
mod tests {
use super::*;
fn hidden(names: &[&str]) -> Vec<String> {
names.iter().map(|n| (*n).to_owned()).collect()
}
#[test]
fn screenshot_tip_comes_before_watch_tip() {
assert_eq!(
choose_tip(&[], true, false, true, true),
Some(Tip::Screenshot { offer_tray: true })
);
}
#[test]
fn screenshot_tip_offers_tray_only_when_useful() {
assert_eq!(
choose_tip(&[], true, true, true, false),
Some(Tip::Screenshot { offer_tray: false })
);
assert_eq!(
choose_tip(&[], true, false, false, false),
Some(Tip::Screenshot { offer_tray: false })
);
}
#[test]
fn hidden_or_unusable_screenshot_tip_leaves_watch_tip() {
let watch = Some(Tip::Watch);
assert_eq!(
choose_tip(&hidden(&[SCREENSHOT_TIP]), true, false, true, true),
watch
);
assert_eq!(choose_tip(&[], false, false, true, true), watch);
}
#[test]
fn no_tip_once_all_are_hidden_or_moot() {
let both = hidden(&[SCREENSHOT_TIP, WATCH_TIP]);
assert_eq!(choose_tip(&both, true, false, true, true), None);
assert_eq!(
choose_tip(&hidden(&[WATCH_TIP]), false, false, true, true),
None
);
assert_eq!(choose_tip(&[], false, false, true, false), None);
}
}