891 lines
34 KiB
Rust
891 lines
34 KiB
Rust
use crate::{
|
|
engine::{self, Event, Outcome},
|
|
model::{FORMATS, Options},
|
|
};
|
|
use eframe::egui::{self, Color32, RichText};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::{
|
|
path::PathBuf,
|
|
sync::{
|
|
Arc,
|
|
atomic::{AtomicBool, Ordering},
|
|
mpsc::{self, Receiver},
|
|
},
|
|
time::{Duration, Instant},
|
|
};
|
|
|
|
#[derive(Default, Clone, Serialize, Deserialize)]
|
|
#[serde(default)]
|
|
pub struct Preferences {
|
|
pub options: Options,
|
|
pub recent: Vec<PathBuf>,
|
|
}
|
|
impl Preferences {
|
|
pub fn remember(&mut self, path: PathBuf) {
|
|
self.recent.retain(|p| p != &path);
|
|
self.recent.insert(0, path);
|
|
self.recent.truncate(8);
|
|
}
|
|
}
|
|
fn config_path() -> Option<PathBuf> {
|
|
directories::ProjectDirs::from("com", "beffroi", "pptx-reporter")
|
|
.map(|d| d.config_dir().join("settings.json"))
|
|
}
|
|
pub fn accept_drop(paths: impl IntoIterator<Item = PathBuf>) -> Result<Vec<PathBuf>, String> {
|
|
let paths: Vec<_> = paths.into_iter().collect();
|
|
if paths.is_empty() {
|
|
return Err("Choose PPTX files or a folder.".into());
|
|
}
|
|
for path in &paths {
|
|
if !path.is_dir()
|
|
&& !(path.is_file()
|
|
&& path
|
|
.extension()
|
|
.is_some_and(|e| e.eq_ignore_ascii_case("pptx")))
|
|
{
|
|
return Err("Choose PPTX files or a folder.".into());
|
|
}
|
|
}
|
|
Ok(paths)
|
|
}
|
|
|
|
fn format_label(format: &str) -> &str {
|
|
match format {
|
|
"md" => "Markdown",
|
|
"txt" => "Plain text",
|
|
"json" => "JSON",
|
|
"html" => "Web page",
|
|
"typst" => "Typst source",
|
|
"pdf" => "PDF",
|
|
"pdf-dense" => "Compact PDF",
|
|
"ontime" => "Ontime",
|
|
other => other,
|
|
}
|
|
}
|
|
|
|
enum Update {
|
|
Progress(Event),
|
|
Finished(Result<Outcome, String>),
|
|
}
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
enum Tab {
|
|
Setup,
|
|
Results,
|
|
}
|
|
|
|
struct App {
|
|
tab: Tab,
|
|
settings_open: bool,
|
|
formats_open: bool,
|
|
preferences: Preferences,
|
|
source: String,
|
|
output: String,
|
|
ffprobe: String,
|
|
rx: Option<Receiver<Update>>,
|
|
cancel: Arc<AtomicBool>,
|
|
status: Event,
|
|
started: Instant,
|
|
phase: Instant,
|
|
log: Vec<(String, bool)>,
|
|
paths: Vec<PathBuf>,
|
|
error: Option<String>,
|
|
completed_output: Option<PathBuf>,
|
|
}
|
|
impl App {
|
|
fn new() -> Self {
|
|
let preferences: Preferences = config_path()
|
|
.and_then(|p| std::fs::read(p).ok())
|
|
.and_then(|b| serde_json::from_slice(&b).ok())
|
|
.unwrap_or_default();
|
|
Self::from_preferences(preferences)
|
|
}
|
|
fn from_preferences(preferences: Preferences) -> Self {
|
|
Self {
|
|
tab: Tab::Setup,
|
|
settings_open: false,
|
|
formats_open: false,
|
|
source: preferences.options.source.display().to_string(),
|
|
output: preferences
|
|
.options
|
|
.output
|
|
.as_ref()
|
|
.map(|p| p.display().to_string())
|
|
.unwrap_or_default(),
|
|
ffprobe: preferences
|
|
.options
|
|
.ffprobe
|
|
.as_ref()
|
|
.map(|p| p.display().to_string())
|
|
.unwrap_or_default(),
|
|
preferences,
|
|
rx: None,
|
|
cancel: Arc::new(AtomicBool::new(false)),
|
|
status: Event::step("Ready", 0, 0),
|
|
started: Instant::now(),
|
|
phase: Instant::now(),
|
|
log: vec![],
|
|
paths: vec![],
|
|
error: None,
|
|
completed_output: None,
|
|
}
|
|
}
|
|
fn save(&mut self) {
|
|
if let Some(path) = config_path() {
|
|
let result = (|| -> anyhow::Result<()> {
|
|
let parent = path.parent().unwrap();
|
|
std::fs::create_dir_all(parent)?;
|
|
let temp = tempfile::NamedTempFile::new_in(parent)?;
|
|
std::fs::write(temp.path(), serde_json::to_vec_pretty(&self.preferences)?)?;
|
|
temp.persist(path).map_err(|e| e.error)?;
|
|
Ok(())
|
|
})();
|
|
if let Err(e) = result {
|
|
self.log
|
|
.push((format!("Could not save settings: {e}"), true));
|
|
}
|
|
}
|
|
}
|
|
fn start(&mut self) {
|
|
let options = &mut self.preferences.options;
|
|
options.source = PathBuf::from(&self.source);
|
|
options.output = (!self.output.trim().is_empty()).then(|| PathBuf::from(&self.output));
|
|
options.ffprobe = (!self.ffprobe.trim().is_empty()).then(|| PathBuf::from(&self.ffprobe));
|
|
if let Err(error) = options.validate() {
|
|
self.error = Some(error.to_string());
|
|
return;
|
|
}
|
|
let options = options.clone();
|
|
self.preferences.remember(options.source.clone());
|
|
self.log.clear();
|
|
self.error = None;
|
|
self.paths.clear();
|
|
self.completed_output = None;
|
|
self.save();
|
|
self.started = Instant::now();
|
|
self.phase = self.started;
|
|
self.status = Event::step("Starting", 0, 0);
|
|
self.cancel = Arc::new(AtomicBool::new(false));
|
|
let cancel = self.cancel.clone();
|
|
let (tx, rx) = mpsc::channel();
|
|
self.rx = Some(rx);
|
|
std::thread::spawn(move || {
|
|
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
|
engine::run(&options, &cancel, |event| {
|
|
let _ = tx.send(Update::Progress(event));
|
|
})
|
|
}))
|
|
.map_err(|_| "Reporter worker stopped unexpectedly.".to_string())
|
|
.and_then(|r| r.map_err(|e| format!("{e:#}")));
|
|
let _ = tx.send(Update::Finished(result));
|
|
});
|
|
}
|
|
fn receive(&mut self) {
|
|
let updates: Vec<_> = self
|
|
.rx
|
|
.as_ref()
|
|
.map(|r| r.try_iter().collect())
|
|
.unwrap_or_default();
|
|
for update in updates {
|
|
match update {
|
|
Update::Progress(event) => {
|
|
if event.total != self.status.total || event.done < self.status.done {
|
|
self.phase = Instant::now();
|
|
}
|
|
self.log.push((event.message.clone(), event.warning));
|
|
if self.log.len() > 500 {
|
|
self.log.remove(0);
|
|
}
|
|
self.status = event;
|
|
}
|
|
Update::Finished(result) => {
|
|
self.rx = None;
|
|
match result {
|
|
Ok(outcome) => {
|
|
self.tab = Tab::Results;
|
|
self.status = Event::step(
|
|
if outcome.paths.is_empty() {
|
|
"No videos found."
|
|
} else {
|
|
"Files ready"
|
|
},
|
|
0,
|
|
0,
|
|
);
|
|
if !outcome.paths.is_empty() {
|
|
self.completed_output = Some(outcome.output);
|
|
}
|
|
self.paths = outcome.paths;
|
|
}
|
|
Err(error) => {
|
|
self.status = Event::step("Stopped", 0, 0);
|
|
self.error = Some(error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
fn open(&mut self, path: &std::path::Path) {
|
|
if let Err(e) = open::that(path) {
|
|
self.error = Some(format!("Could not open {}: {e}", path.display()));
|
|
}
|
|
}
|
|
}
|
|
impl Drop for App {
|
|
fn drop(&mut self) {
|
|
self.cancel.store(true, Ordering::Relaxed);
|
|
}
|
|
}
|
|
impl eframe::App for App {
|
|
fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) {
|
|
self.draw(ctx);
|
|
}
|
|
}
|
|
impl App {
|
|
fn select_inputs(&mut self, mut paths: Vec<PathBuf>) {
|
|
paths.sort_by(|a, b| crate::ordering::paths(a, b));
|
|
if let Some(first) = paths.first() {
|
|
self.source = first.display().to_string();
|
|
self.preferences.options.additional_sources = paths[1..].to_vec();
|
|
self.tab = Tab::Setup;
|
|
self.error = None;
|
|
}
|
|
}
|
|
|
|
fn setup(&mut self, ui: &mut egui::Ui) {
|
|
ui.add_enabled_ui(self.rx.is_none(), |ui| {
|
|
ui.horizontal_wrapped(|ui| {
|
|
ui.label(RichText::new("Inputs").strong());
|
|
if ui.button("PPTX files…").clicked()
|
|
&& let Some(files) = rfd::FileDialog::new().add_filter("PowerPoint", &["pptx"]).pick_files() {
|
|
self.select_inputs(files);
|
|
}
|
|
if ui.button("Folder…").clicked()
|
|
&& let Some(folder) = rfd::FileDialog::new().pick_folder() {
|
|
self.select_inputs(vec![folder]);
|
|
}
|
|
if !self.preferences.recent.is_empty() {
|
|
let mut selected = None;
|
|
egui::ComboBox::from_id_salt("recent").selected_text("Recent…").width(90.0).show_ui(ui, |ui| {
|
|
for path in &self.preferences.recent {
|
|
if ui.selectable_label(false, path.display().to_string()).clicked() { selected = Some(path.clone()); }
|
|
}
|
|
});
|
|
if let Some(path) = selected { self.select_inputs(vec![path]); }
|
|
}
|
|
});
|
|
egui::Frame::group(ui.style()).inner_margin(10.0).show(ui, |ui| {
|
|
ui.set_min_width(ui.available_width());
|
|
let height = (ui.ctx().screen_rect().height() * 0.20).clamp(60.0, 120.0);
|
|
egui::ScrollArea::vertical().id_salt("input_list").max_height(height).show(ui, |ui| {
|
|
if self.source.is_empty() {
|
|
ui.set_min_height(54.0);
|
|
ui.label(RichText::new("Drop PPTX files or a folder here").size(18.0));
|
|
ui.weak("A folder can contain videos, presentations, or video subfolders.");
|
|
} else {
|
|
let first = PathBuf::from(&self.source);
|
|
let mut paths: Vec<_> = std::iter::once(&first).chain(&self.preferences.options.additional_sources).collect();
|
|
paths.sort_by(|a, b| crate::ordering::paths(a, b));
|
|
for path in paths {
|
|
ui.add(egui::Label::new(path.file_name().unwrap_or_default().to_string_lossy()).truncate())
|
|
.on_hover_text(path.display().to_string());
|
|
}
|
|
}
|
|
});
|
|
});
|
|
ui.horizontal_wrapped(|ui| {
|
|
let count = if self.source.is_empty() { 0 } else { 1 + self.preferences.options.additional_sources.len() };
|
|
ui.weak(format!("{count} selected · Natural filename order"));
|
|
ui.collapsing("Enter a path", |ui| {
|
|
if ui.add(egui::TextEdit::singleline(&mut self.source).desired_width(260.0).hint_text("PPTX or folder path")).changed() {
|
|
self.preferences.options.additional_sources.clear();
|
|
}
|
|
});
|
|
});
|
|
ui.separator();
|
|
let options = &mut self.preferences.options;
|
|
let mut mode = if options.extract_only { 2 } else if options.timings_only { 1 } else { 0 };
|
|
ui.horizontal(|ui| {
|
|
ui.label("Create");
|
|
egui::ComboBox::from_id_salt("mode").selected_text(match mode { 1 => "Reports only", 2 => "Extract only", _ => "Extract + reports" }).show_ui(ui, |ui| {
|
|
ui.selectable_value(&mut mode, 0, "Extract + reports");
|
|
ui.selectable_value(&mut mode, 1, "Reports only");
|
|
ui.selectable_value(&mut mode, 2, "Extract only");
|
|
});
|
|
});
|
|
options.extract_only = mode == 2;
|
|
options.timings_only = mode == 1;
|
|
ui.add_enabled_ui(!options.extract_only, |ui| {
|
|
ui.horizontal_wrapped(|ui| {
|
|
ui.label("Formats");
|
|
for format in ["ontime", "pdf", "md"] { format_checkbox(ui, &mut options.formats, format); }
|
|
if ui.button("More formats…").clicked() { self.formats_open = true; }
|
|
});
|
|
let selected = options.formats.iter().map(|f| format_label(f)).collect::<Vec<_>>().join(", ");
|
|
ui.add(egui::Label::new(RichText::new(if selected.is_empty() { "Select at least one report format." } else { &selected }).weak()).truncate()).on_hover_text(&selected);
|
|
});
|
|
ui.separator();
|
|
ui.horizontal(|ui| {
|
|
ui.label("Save to");
|
|
if ui.button("Change…").clicked()
|
|
&& let Some(path) = rfd::FileDialog::new().pick_folder() { self.output = path.display().to_string(); }
|
|
if ui.button("Use default").clicked() { self.output.clear(); }
|
|
ui.add(egui::TextEdit::singleline(&mut self.output).desired_width(ui.available_width()).hint_text("Beside the first input"));
|
|
});
|
|
if !self.source.is_empty() {
|
|
let output = Options { source: PathBuf::from(&self.source), output: (!self.output.trim().is_empty()).then(|| PathBuf::from(&self.output)), ..Default::default() }.output_dir();
|
|
let path = output.display().to_string();
|
|
ui.add(egui::Label::new(RichText::new(&path).weak()).truncate()).on_hover_text(path);
|
|
}
|
|
});
|
|
}
|
|
|
|
fn results(&mut self, ui: &mut egui::Ui, ctx: &egui::Context) {
|
|
if let Some(dir) = self.completed_output.clone() {
|
|
ui.horizontal(|ui| {
|
|
ui.label(
|
|
RichText::new("Your files are ready")
|
|
.size(20.0)
|
|
.color(Color32::from_rgb(133, 214, 170))
|
|
.strong(),
|
|
);
|
|
if ui.button("Open output folder").clicked() {
|
|
self.open(&dir);
|
|
}
|
|
});
|
|
} else {
|
|
ui.label(if self.rx.is_some() {
|
|
"Processing your inputs…"
|
|
} else {
|
|
"Generated files will appear here."
|
|
});
|
|
}
|
|
for path in self.paths.clone() {
|
|
ui.horizontal(|ui| {
|
|
if ui.button("Open").clicked() {
|
|
self.open(&path);
|
|
}
|
|
if ui.button("Copy path").clicked() {
|
|
ctx.copy_text(path.display().to_string());
|
|
}
|
|
ui.add(
|
|
egui::Label::new(
|
|
RichText::new(path.file_name().unwrap_or_default().to_string_lossy())
|
|
.strong(),
|
|
)
|
|
.truncate(),
|
|
)
|
|
.on_hover_text(path.display().to_string());
|
|
});
|
|
}
|
|
ui.collapsing("Activity & diagnostics", |ui| {
|
|
egui::ScrollArea::vertical()
|
|
.id_salt("diagnostics")
|
|
.max_height(180.0)
|
|
.stick_to_bottom(true)
|
|
.show(ui, |ui| {
|
|
for (line, warning) in &self.log {
|
|
ui.colored_label(
|
|
if *warning {
|
|
Color32::from_rgb(236, 185, 86)
|
|
} else {
|
|
Color32::from_rgb(160, 175, 195)
|
|
},
|
|
line,
|
|
);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
fn action_bar(&mut self, ui: &mut egui::Ui) {
|
|
ui.horizontal(|ui| {
|
|
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
|
if self.rx.is_some() {
|
|
let cancelling = self.cancel.load(Ordering::Relaxed);
|
|
if ui
|
|
.add_enabled(
|
|
!cancelling,
|
|
egui::Button::new(if cancelling {
|
|
"Cancelling…"
|
|
} else {
|
|
"Cancel"
|
|
}),
|
|
)
|
|
.clicked()
|
|
{
|
|
self.cancel.store(true, Ordering::Relaxed);
|
|
}
|
|
ui.spinner();
|
|
} else {
|
|
let options = &self.preferences.options;
|
|
let ready = !self.source.is_empty()
|
|
&& (options.extract_only || !options.formats.is_empty());
|
|
let action = if options.extract_only {
|
|
"Extract videos"
|
|
} else if options.timings_only {
|
|
"Generate reports"
|
|
} else {
|
|
"Extract & generate reports"
|
|
};
|
|
if ui
|
|
.add_enabled(
|
|
ready,
|
|
egui::Button::new(RichText::new(action).color(Color32::WHITE))
|
|
.min_size(egui::vec2(190.0, 34.0))
|
|
.fill(Color32::from_rgb(87, 68, 181)),
|
|
)
|
|
.clicked()
|
|
{
|
|
self.start();
|
|
}
|
|
}
|
|
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
|
|
ui.add(egui::Label::new(&self.status.message).truncate())
|
|
.on_hover_text(&self.status.message);
|
|
});
|
|
});
|
|
});
|
|
if self.rx.is_some() {
|
|
let e = &self.status;
|
|
if e.total > 0 {
|
|
ui.add(egui::ProgressBar::new(e.done as f32 / e.total as f32).show_percentage());
|
|
}
|
|
let eta = if e.done > 0 && e.done < e.total {
|
|
format!(
|
|
" · about {:.0}s remaining in this phase",
|
|
self.phase.elapsed().as_secs_f64() / e.done as f64 * (e.total - e.done) as f64
|
|
)
|
|
} else {
|
|
String::new()
|
|
};
|
|
ui.weak(format!(
|
|
"{:.0}s elapsed{eta}",
|
|
self.started.elapsed().as_secs_f64()
|
|
));
|
|
}
|
|
if let Some(error) = &self.error {
|
|
egui::ScrollArea::vertical()
|
|
.id_salt("error")
|
|
.max_height(54.0)
|
|
.show(ui, |ui| {
|
|
ui.colored_label(Color32::from_rgb(255, 150, 120), error);
|
|
});
|
|
}
|
|
}
|
|
|
|
fn dialogs(&mut self, ctx: &egui::Context) {
|
|
egui::Window::new("Settings")
|
|
.open(&mut self.settings_open)
|
|
.collapsible(false)
|
|
.resizable(true)
|
|
.default_width(420.0)
|
|
.show(ctx, |ui| {
|
|
ui.add_enabled_ui(self.rx.is_none(), |ui| {
|
|
ui.checkbox(
|
|
&mut self.preferences.options.overwrite,
|
|
"Replace existing extracted or copied videos",
|
|
);
|
|
ui.label("Existing reports are regenerated. Source files are preserved.");
|
|
ui.separator();
|
|
ui.horizontal(|ui| {
|
|
ui.label("FFprobe");
|
|
if ui.button("Locate…").clicked()
|
|
&& let Some(path) = rfd::FileDialog::new().pick_file()
|
|
{
|
|
self.ffprobe = path.display().to_string();
|
|
}
|
|
});
|
|
ui.add(
|
|
egui::TextEdit::singleline(&mut self.ffprobe)
|
|
.desired_width(f32::INFINITY)
|
|
.hint_text("Automatic: beside app, bin/, or PATH"),
|
|
);
|
|
ui.weak("PDF generation is built in. FFprobe comes from FFmpeg.");
|
|
});
|
|
});
|
|
egui::Window::new("Report formats")
|
|
.open(&mut self.formats_open)
|
|
.collapsible(false)
|
|
.resizable(false)
|
|
.show(ctx, |ui| {
|
|
ui.add_enabled_ui(
|
|
self.rx.is_none() && !self.preferences.options.extract_only,
|
|
|ui| {
|
|
let formats = &mut self.preferences.options.formats;
|
|
ui.horizontal_wrapped(|ui| {
|
|
if ui.button("Reading & printing").clicked() {
|
|
*formats = ["html", "pdf", "pdf-dense"].map(String::from).to_vec();
|
|
}
|
|
if ui.button("Defaults").clicked() {
|
|
*formats = Options::default().formats;
|
|
}
|
|
if ui.button("All").clicked() {
|
|
*formats = FORMATS.iter().map(|s| (*s).into()).collect();
|
|
}
|
|
});
|
|
egui::Grid::new("formats_grid")
|
|
.num_columns(2)
|
|
.show(ui, |ui| {
|
|
for (i, format) in FORMATS.iter().enumerate() {
|
|
format_checkbox(ui, formats, format);
|
|
if i % 2 == 1 {
|
|
ui.end_row();
|
|
}
|
|
}
|
|
});
|
|
},
|
|
);
|
|
});
|
|
}
|
|
|
|
fn draw(&mut self, ctx: &egui::Context) {
|
|
self.receive();
|
|
if self.rx.is_some() {
|
|
ctx.request_repaint_after(Duration::from_millis(100));
|
|
}
|
|
let drops = ctx.input(|i| {
|
|
i.raw
|
|
.dropped_files
|
|
.iter()
|
|
.filter_map(|f| f.path.clone())
|
|
.collect::<Vec<_>>()
|
|
});
|
|
if !drops.is_empty() {
|
|
if self.rx.is_some() {
|
|
self.error = Some(
|
|
"Wait for the current report or cancel it before dropping another file.".into(),
|
|
);
|
|
} else {
|
|
match accept_drop(drops) {
|
|
Ok(paths) => self.select_inputs(paths),
|
|
Err(e) => self.error = Some(e),
|
|
}
|
|
}
|
|
}
|
|
egui::TopBottomPanel::top("header").show(ctx, |ui| {
|
|
ui.horizontal(|ui| {
|
|
ui.label(
|
|
RichText::new("PPTX Reporter")
|
|
.size(24.0)
|
|
.strong()
|
|
.color(Color32::from_rgb(153, 137, 245)),
|
|
);
|
|
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
|
if ui.button("Settings").clicked() {
|
|
self.settings_open = true;
|
|
}
|
|
});
|
|
});
|
|
ui.horizontal(|ui| {
|
|
ui.selectable_value(&mut self.tab, Tab::Setup, "Setup");
|
|
ui.selectable_value(&mut self.tab, Tab::Results, "Results");
|
|
});
|
|
});
|
|
egui::TopBottomPanel::bottom("actions").show(ctx, |ui| self.action_bar(ui));
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
|
// Only the active pane scrolls; actions and navigation stay visible.
|
|
egui::ScrollArea::vertical()
|
|
.id_salt(match self.tab {
|
|
Tab::Setup => "setup",
|
|
Tab::Results => "results",
|
|
})
|
|
.auto_shrink([false, false])
|
|
.show(ui, |ui| match self.tab {
|
|
Tab::Setup => self.setup(ui),
|
|
Tab::Results => self.results(ui, ctx),
|
|
});
|
|
});
|
|
self.dialogs(ctx);
|
|
if ctx.input(|i| !i.raw.hovered_files.is_empty()) {
|
|
let painter = ctx.layer_painter(egui::LayerId::new(
|
|
egui::Order::Foreground,
|
|
egui::Id::new("drop"),
|
|
));
|
|
painter.rect_filled(ctx.screen_rect(), 0.0, Color32::from_black_alpha(210));
|
|
painter.text(
|
|
ctx.screen_rect().center(),
|
|
egui::Align2::CENTER_CENTER,
|
|
"Drop PPTX files or folders",
|
|
egui::FontId::proportional(24.0),
|
|
Color32::WHITE,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn format_checkbox(ui: &mut egui::Ui, formats: &mut Vec<String>, format: &str) {
|
|
let mut selected = formats.iter().any(|f| f == format);
|
|
if ui.checkbox(&mut selected, format_label(format)).changed() {
|
|
formats.retain(|f| f != format);
|
|
if selected {
|
|
formats.push(format.into());
|
|
}
|
|
}
|
|
}
|
|
|
|
fn configure_style(ctx: &egui::Context) {
|
|
ctx.set_visuals(egui::Visuals::dark());
|
|
let mut style = (*ctx.style()).clone();
|
|
style.spacing.item_spacing = egui::vec2(10.0, 10.0);
|
|
style.visuals.override_text_color = Some(Color32::from_rgb(218, 224, 235));
|
|
for role in [egui::TextStyle::Body, egui::TextStyle::Button] {
|
|
style
|
|
.text_styles
|
|
.insert(role, egui::FontId::proportional(14.0));
|
|
}
|
|
style.visuals.selection.bg_fill = Color32::from_rgb(87, 68, 181);
|
|
ctx.set_style(style);
|
|
}
|
|
|
|
pub fn launch() -> anyhow::Result<()> {
|
|
let icon = eframe::icon_data::from_png_bytes(include_bytes!("../assets/rust-report-icon.png"))?;
|
|
let options = eframe::NativeOptions {
|
|
viewport: egui::ViewportBuilder::default()
|
|
.with_icon(icon)
|
|
.with_inner_size([760.0, 560.0])
|
|
.with_min_inner_size([560.0, 420.0])
|
|
.with_drag_and_drop(true),
|
|
..Default::default()
|
|
};
|
|
eframe::run_native(
|
|
"PPTX Reporter",
|
|
options,
|
|
Box::new(|cc| {
|
|
configure_style(&cc.egui_ctx);
|
|
Ok(Box::new(App::new()))
|
|
}),
|
|
)
|
|
.map_err(|e| anyhow::anyhow!("{e}"))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn frame(app: &mut App, input: egui::RawInput) {
|
|
let ctx = egui::Context::default();
|
|
let output = ctx.run(input, |ctx| app.draw(ctx));
|
|
assert!(!output.shapes.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn renders_drop_validation_and_running_state_without_display() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("Présentation.pptx");
|
|
std::fs::write(&path, b"fixture").unwrap();
|
|
let mut app = App::from_preferences(Preferences::default());
|
|
let input = egui::RawInput {
|
|
dropped_files: vec![egui::DroppedFile {
|
|
path: Some(path.clone()),
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
};
|
|
frame(&mut app, input.clone());
|
|
assert_eq!(app.source, path.display().to_string());
|
|
assert!(app.error.is_none());
|
|
let (tx, rx) = mpsc::channel();
|
|
app.rx = Some(rx);
|
|
tx.send(Update::Progress(Event::step("Probing", 1, 3)))
|
|
.unwrap();
|
|
frame(&mut app, input);
|
|
assert!(app.error.as_ref().unwrap().contains("current report"));
|
|
assert_eq!(app.status.done, 1);
|
|
assert_eq!(app.log.len(), 1);
|
|
tx.send(Update::Finished(Err("Cancelled".into()))).unwrap();
|
|
frame(&mut app, egui::RawInput::default());
|
|
assert!(app.rx.is_none());
|
|
assert_eq!(app.error.as_deref(), Some("Cancelled"));
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_start_and_successful_completion_show_actionable_state() {
|
|
let mut app = App::from_preferences(Preferences::default());
|
|
app.start();
|
|
assert!(app.error.is_some());
|
|
assert!(app.rx.is_none());
|
|
let (tx, rx) = mpsc::channel();
|
|
app.rx = Some(rx);
|
|
for i in 0..505 {
|
|
tx.send(Update::Progress(Event::step("Working", i, 505)))
|
|
.unwrap();
|
|
}
|
|
tx.send(Update::Finished(Ok(Outcome {
|
|
report: None,
|
|
paths: vec!["reports/timings.html".into()],
|
|
output: "reports".into(),
|
|
})))
|
|
.unwrap();
|
|
frame(&mut app, egui::RawInput::default());
|
|
assert_eq!(app.log.len(), 500);
|
|
assert_eq!(app.paths.len(), 1);
|
|
assert_eq!(app.tab, Tab::Results);
|
|
assert_eq!(app.completed_output, Some("reports".into()));
|
|
let cancellation = app.cancel.clone();
|
|
drop(app);
|
|
assert!(cancellation.load(Ordering::Relaxed));
|
|
}
|
|
fn layout_frame(
|
|
app: &mut App,
|
|
ctx: &egui::Context,
|
|
size: egui::Vec2,
|
|
events: Vec<egui::Event>,
|
|
) -> egui::FullOutput {
|
|
ctx.run(
|
|
egui::RawInput {
|
|
screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, size)),
|
|
events,
|
|
..Default::default()
|
|
},
|
|
|ctx| app.draw(ctx),
|
|
)
|
|
}
|
|
|
|
fn text_rect(output: &egui::FullOutput, text: &str) -> (egui::Rect, egui::Rect) {
|
|
output
|
|
.shapes
|
|
.iter()
|
|
.find_map(|clipped| {
|
|
if let egui::epaint::Shape::Text(shape) = &clipped.shape
|
|
&& shape.galley.text() == text
|
|
{
|
|
return Some((
|
|
shape.galley.rect.translate(shape.pos.to_vec2()),
|
|
clipped.clip_rect,
|
|
));
|
|
}
|
|
None
|
|
})
|
|
.unwrap_or_else(|| panic!("Missing text: {text}"))
|
|
}
|
|
|
|
#[test]
|
|
fn compact_setup_and_actions_remain_visible_with_many_inputs() {
|
|
for size in [egui::vec2(760.0, 560.0), egui::vec2(560.0, 420.0)] {
|
|
let ctx = egui::Context::default();
|
|
configure_style(&ctx);
|
|
let mut app = App::from_preferences(Preferences::default());
|
|
app.select_inputs(
|
|
(1..=60)
|
|
.rev()
|
|
.map(|n| PathBuf::from(format!("/presentations/Presentation {n}.pptx")))
|
|
.collect(),
|
|
);
|
|
assert!(app.source.ends_with("Presentation 1.pptx"));
|
|
layout_frame(&mut app, &ctx, size, vec![]);
|
|
let output = layout_frame(&mut app, &ctx, size, vec![]);
|
|
for text in [
|
|
"Setup",
|
|
"Results",
|
|
"Formats",
|
|
"Save to",
|
|
"Extract & generate reports",
|
|
] {
|
|
let (rect, clip) = text_rect(&output, text);
|
|
assert!(
|
|
clip.contains_rect(rect),
|
|
"{text} clipped at {size:?}: {rect:?}, {clip:?}"
|
|
);
|
|
assert!(egui::Rect::from_min_size(egui::Pos2::ZERO, size).contains_rect(rect));
|
|
}
|
|
let (button, _) = text_rect(&output, "Extract & generate reports");
|
|
assert!(button.top() > size.y - 55.0);
|
|
let (_tx, rx) = mpsc::channel();
|
|
app.rx = Some(rx);
|
|
app.status = Event::step(
|
|
"Processing a very long filename that must not push Cancel off the edge of the window",
|
|
1,
|
|
3,
|
|
);
|
|
layout_frame(&mut app, &ctx, size, vec![]);
|
|
let output = layout_frame(&mut app, &ctx, size, vec![]);
|
|
let (cancel, clip) = text_rect(&output, "Cancel");
|
|
assert!(clip.contains_rect(cancel));
|
|
assert!(cancel.right() <= size.x);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn tabs_dialogs_and_cancel_work_in_compact_window() {
|
|
let ctx = egui::Context::default();
|
|
configure_style(&ctx);
|
|
let mut app = App::from_preferences(Preferences::default());
|
|
let size = egui::vec2(760.0, 560.0);
|
|
layout_frame(&mut app, &ctx, size, vec![]);
|
|
let mut output = layout_frame(&mut app, &ctx, size, vec![]);
|
|
for label in ["Results", "Setup", "Settings"] {
|
|
let pos = text_rect(&output, label).0.center();
|
|
layout_frame(
|
|
&mut app,
|
|
&ctx,
|
|
size,
|
|
vec![
|
|
egui::Event::PointerMoved(pos),
|
|
egui::Event::PointerButton {
|
|
pos,
|
|
button: egui::PointerButton::Primary,
|
|
pressed: true,
|
|
modifiers: egui::Modifiers::NONE,
|
|
},
|
|
],
|
|
);
|
|
output = layout_frame(
|
|
&mut app,
|
|
&ctx,
|
|
size,
|
|
vec![egui::Event::PointerButton {
|
|
pos,
|
|
button: egui::PointerButton::Primary,
|
|
pressed: false,
|
|
modifiers: egui::Modifiers::NONE,
|
|
}],
|
|
);
|
|
if label == "Results" {
|
|
assert_eq!(app.tab, Tab::Results);
|
|
}
|
|
if label == "Setup" {
|
|
assert_eq!(app.tab, Tab::Setup);
|
|
}
|
|
}
|
|
assert!(app.settings_open);
|
|
app.settings_open = false;
|
|
app.formats_open = true;
|
|
layout_frame(&mut app, &ctx, size, vec![]);
|
|
output = layout_frame(&mut app, &ctx, size, vec![]);
|
|
text_rect(&output, "Reading & printing");
|
|
app.formats_open = false;
|
|
app.tab = Tab::Results;
|
|
let (_tx, rx) = mpsc::channel();
|
|
app.rx = Some(rx);
|
|
output = layout_frame(&mut app, &ctx, size, vec![]);
|
|
let pos = text_rect(&output, "Cancel").0.center();
|
|
layout_frame(
|
|
&mut app,
|
|
&ctx,
|
|
size,
|
|
vec![
|
|
egui::Event::PointerMoved(pos),
|
|
egui::Event::PointerButton {
|
|
pos,
|
|
button: egui::PointerButton::Primary,
|
|
pressed: true,
|
|
modifiers: egui::Modifiers::NONE,
|
|
},
|
|
],
|
|
);
|
|
layout_frame(
|
|
&mut app,
|
|
&ctx,
|
|
size,
|
|
vec![egui::Event::PointerButton {
|
|
pos,
|
|
button: egui::PointerButton::Primary,
|
|
pressed: false,
|
|
modifiers: egui::Modifiers::NONE,
|
|
}],
|
|
);
|
|
assert!(app.cancel.load(Ordering::Relaxed));
|
|
}
|
|
}
|