Tunal/crates/tunal/src/main.rs
astar76 b042dfe29c S034: screenshot matrix and UI review fixes
--matrix renders every screen light/dark, hovered/pressed, empty/full and
at 720x480. Fixes: breadcrumb overflow mark, compact toolbar, names-only
narrow panes, short-window dialogs, scrolling settings pages.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-19 04:49:45 -06:00

232 lines
7.7 KiB
Rust

mod actions;
mod clipboard;
mod config;
mod desktop_apps;
mod devices_bridge;
mod dnd;
mod folder;
mod icons;
mod jobs_bridge;
mod menus;
mod native;
mod net_bridge;
mod open;
mod player_bridge;
mod properties;
mod screenshot;
mod settings;
mod shapes;
mod shell;
mod tags_bridge;
mod theme_bridge;
mod thumbs_bridge;
use std::path::PathBuf;
use std::process::ExitCode;
use tunal_core::{Locations, Uri};
slint::include_modules!();
const USAGE: &str = "usage: tunal [<folder>…] [--split <folder> <folder>] \
[--gallery [--page menu|dialog|connection|host-key|secret]] [--screenshot <png> [--dark] [--size WxH]] \
| --matrix <dir>";
fn main() -> ExitCode {
// Root helper (`pkexec tunal --elevated-helper`): before any UI, theme or env lookups.
if std::env::args().nth(1).as_deref() == Some(tunal_elevate::HELPER_ARG) {
return match tunal_elevate::serve() {
Ok(()) => ExitCode::SUCCESS,
Err(_) => ExitCode::FAILURE,
};
}
let mut argv = std::env::args().skip(1);
if std::env::args().nth(1).as_deref() == Some("--matrix") {
let result = match (argv.nth(1), argv.next()) {
(Some(dir), None) => screenshot::matrix(std::path::Path::new(&dir)),
_ => Err(USAGE.into()),
};
return match result {
Ok(()) => ExitCode::SUCCESS,
Err(message) => {
eprintln!("tunal: {message}");
ExitCode::FAILURE
}
};
}
let result = parse(argv).and_then(|args| match args.screenshot {
None if args.gallery => gallery(args.page).and_then(run).map_err(|e| e.to_string()),
None => window(args.tabs, false)
.and_then(|(ui, shell)| {
run(ui)?;
shell.save();
Ok(())
})
.map_err(|e| e.to_string()),
Some(options) if args.gallery => screenshot::run(options, || gallery(args.page), || {}),
// A screenshot is one frame: list the folder before it is drawn. The shell lives until
// the frame is drawn, so callbacks fired while drawing (grid columns) still reach it.
Some(options) => {
let shell = std::cell::RefCell::new(None);
screenshot::run(
options,
|| {
window(args.tabs, true).map(|(ui, s)| {
*shell.borrow_mut() = Some(s);
ui
})
},
|| {
if let Some(shell) = shell.borrow().as_ref() {
shell.settle_thumbs();
// `TUNAL_ACTION=<id>` runs a menu action on what `TUNAL_POINTER` selected.
if let Some(action) = std::env::var("TUNAL_ACTION")
.ok()
.and_then(|id| actions::Action::from_id(&id))
{
shell.run_action(action);
}
}
},
)
}
});
match result {
Ok(()) => ExitCode::SUCCESS,
Err(message) => {
eprintln!("tunal: {message}");
ExitCode::FAILURE
}
}
}
/// The main window with the shell: one tab per entry of `tabs` (two folders make a split
/// tab), else the last session. `blocking` lists the folders before returning (screenshots).
fn window(
tabs: Vec<Vec<PathBuf>>,
blocking: bool,
) -> Result<(MainWindow, std::rc::Rc<shell::Shell>), slint::PlatformError> {
let ui = MainWindow::new()?;
let tabs = tabs
.iter()
.map(|dirs| dirs.iter().map(|d| Uri::from_path(d)).collect())
.collect();
let mut locations = Locations::with_local();
let mtp = devices_bridge::register(&mut locations);
let net = net_bridge::register(&mut locations, &ui);
let shell = shell::Shell::attach(&ui, locations, tabs, blocking);
devices_bridge::attach(&shell, mtp);
net_bridge::attach(&ui, &shell, net);
// `TUNAL_SETTINGS=<page 0-4>` opens the settings dialog (screenshots).
if let Some(page) = std::env::var("TUNAL_SETTINGS")
.ok()
.and_then(|p| p.parse().ok())
{
ui.invoke_show_settings(page);
}
// `TUNAL_APPEARANCE=1` opens the folder appearance dialog for the folder shown.
if std::env::var_os("TUNAL_APPEARANCE").is_some() {
shell.run_action(actions::Action::Appearance);
}
// `TUNAL_TAGS=editor|lookup|mapping:<file>:<file>…` opens the tag editor or the lookup
// (screenshots; `TUNAL_TAGS_SYNC=1` runs its work in place, `TUNAL_MUSICBRAINZ=<url>` is a mock).
if let Ok(spec) = std::env::var("TUNAL_TAGS") {
shell.debug_tags(&spec);
}
// `TUNAL_PLAY=<audio file>` shows it in the player bar, a third of the way through.
if let Some(path) = std::env::var_os("TUNAL_PLAY") {
shell.debug_play(PathBuf::from(path));
}
Ok((ui, shell))
}
fn gallery(page: GalleryPage) -> Result<Gallery, slint::PlatformError> {
let ui = Gallery::new()?;
ui.set_page(page);
ui.invoke_stage();
Ok(ui)
}
fn run(ui: impl theme_bridge::Themed) -> Result<(), slint::PlatformError> {
theme_bridge::start(&ui, theme_bridge::Overrides::from_env());
ui.run()
}
struct Args {
/// Folders of each tab given on the command line.
tabs: Vec<Vec<PathBuf>>,
gallery: bool,
page: GalleryPage,
screenshot: Option<screenshot::Options>,
}
fn parse(mut args: impl Iterator<Item = String>) -> Result<Args, String> {
let mut tabs = Vec::new();
let mut gallery = false;
let mut page = GalleryPage::Controls;
let mut options = screenshot::Options {
path: String::new(),
dark: false,
size: (960, 640),
reduced_motion: false,
};
let mut screenshot_flags = false;
while let Some(arg) = args.next() {
match arg.as_str() {
"--gallery" => gallery = true,
"--split" => {
let a = args.next().ok_or(USAGE)?;
let b = args.next().ok_or(USAGE)?;
tabs.push(vec![PathBuf::from(a), PathBuf::from(b)]);
}
"--page" => {
page = match args.next().as_deref() {
Some("menu") => GalleryPage::Menu,
Some("dialog") => GalleryPage::Dialog,
Some("connection") => GalleryPage::Connection,
Some("host-key") => GalleryPage::HostKey,
Some("secret") => GalleryPage::Secret,
_ => return Err(USAGE.into()),
}
}
"--screenshot" => {
options.path = args.next().ok_or(USAGE)?;
screenshot_flags = true;
}
"--dark" => {
options.dark = true;
screenshot_flags = true;
}
"--size" => {
let size = args.next().ok_or(USAGE)?;
let (w, h) = size.split_once('x').ok_or(USAGE)?;
let parse = |n: &str| n.parse::<u32>().ok().filter(|&n| n > 0).ok_or(USAGE);
options.size = (parse(w)?, parse(h)?);
screenshot_flags = true;
}
_ if !arg.starts_with('-') => tabs.push(vec![PathBuf::from(arg)]),
_ => return Err(USAGE.into()),
}
}
if page != GalleryPage::Controls && !gallery {
return Err(USAGE.into());
}
if !screenshot_flags {
return Ok(Args {
tabs,
gallery,
page,
screenshot: None,
});
}
if options.path.is_empty() {
return Err(USAGE.into());
}
options.reduced_motion = gallery;
Ok(Args {
tabs,
gallery,
page,
screenshot: Some(options),
})
}