mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge PR #7242: A second launch that sets only environment overrides keeps its own process
apps/photocraft/src/single_instance.rs: both sides' appended tests kept whole.
This commit is contained in:
commit
c50708ea08
2 files changed
+111
-1
No files matched your search
@@ -524,6 +524,13 @@ fn main() -> eframe::Result {
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
let single_instance = {
|
||||
let new_instance_env = std::env::var("PHOTOCRAFT_NEW_INSTANCE").ok();
|
||||
// A variable documented as the equivalent of a per-launch flag counts as that flag: a
|
||||
// launch that sets only `PHOTOCRAFT_PRESENT_MODE` (say) has per-process options too, so it
|
||||
// keeps its own process instead of handing its files over and being ignored.
|
||||
// Folded in here, inside the block that only non-macOS builds compile, so the binding
|
||||
// above keeps its `mut` off macOS, where nothing is folded in.
|
||||
let mut launch_options = launch_options;
|
||||
launch_options.extend_from_slice(&single_instance::env_launch_options());
|
||||
let context = single_instance::LaunchContext {
|
||||
per_user_settings: app_dirs::current().mode == app_dirs::Mode::Platform,
|
||||
new_instance_flag: new_instance,
|
||||
|
||||
@@ -129,10 +129,71 @@ pub struct LaunchContext<'a> {
|
||||
pub control: bool,
|
||||
/// Paths that aren't valid Unicode: this launch reports them.
|
||||
pub unreadable_paths: bool,
|
||||
/// Options that only apply to the process they're given to (`--safe-gpu`, …).
|
||||
/// Options that only apply to the process they're given to (`--safe-gpu`, …), including the
|
||||
/// ones this launch's environment sets (see [`env_launch_options`]).
|
||||
pub launch_options: &'a [&'static str],
|
||||
}
|
||||
|
||||
/// Variables documented as the equivalent of a per-launch flag, each with the flag it stands for
|
||||
/// (`PHOTOCRAFT_PRESENT_MODE` is `--present-mode`) and the rule that says whether its value asks for
|
||||
/// that option at all. Setting one asks for the same thing as giving the flag, so it belongs to this
|
||||
/// process alone.
|
||||
const ENV_LAUNCH_OPTIONS: [(&str, &str, ValueRule); 6] = [
|
||||
("PHOTOCRAFT_PRESENT_MODE", "--present-mode", present_mode_is_on),
|
||||
("PHOTOCRAFT_FRAME_LATENCY", "--frame-latency", frame_latency_is_on),
|
||||
("PHOTOCRAFT_STROKE_TRACE", "--stroke-trace", stroke_trace_is_on),
|
||||
("PHOTOCRAFT_IN_WINDOW_MENUS", "--in-window-menus", flag_is_on),
|
||||
("PHOTOCRAFT_AUTOMATION_READ_ROOT", "--automation-read-root", path_is_set),
|
||||
("PHOTOCRAFT_AUTOMATION_WRITE_ROOT", "--automation-write-root", path_is_set),
|
||||
];
|
||||
|
||||
/// Whether a variable's value asks this launch for its own option. Each rule matches how that
|
||||
/// variable is read where it takes effect, so a value ignored there keeps no process here either.
|
||||
type ValueRule = fn(&str) -> bool;
|
||||
|
||||
/// A value `presentation::parse` accepts, handed over as written: it trims, treats empty as unset and
|
||||
/// warns and ignores anything it does not recognise, so an unrecognised `PHOTOCRAFT_PRESENT_MODE`
|
||||
/// changes nothing there and so needs no process of its own here.
|
||||
fn present_mode_is_on(value: &str) -> bool {
|
||||
crate::presentation::parse(Some(value), None).0.mode.is_some()
|
||||
}
|
||||
|
||||
/// [`present_mode_is_on`] for `PHOTOCRAFT_FRAME_LATENCY`, which takes `1` to `3`.
|
||||
fn frame_latency_is_on(value: &str) -> bool {
|
||||
crate::presentation::parse(None, Some(value)).0.latency.is_some()
|
||||
}
|
||||
|
||||
/// Anything except `""`, `0`, `off` and `false`, which is how `StrokeTrace::from_value` reads it: the
|
||||
/// rest is a stderr summary or a CSV path. It trims, so `" 0 "` is off there and off here.
|
||||
fn stroke_trace_is_on(value: &str) -> bool {
|
||||
!matches!(value.trim(), "" | "0" | "off" | "false")
|
||||
}
|
||||
|
||||
/// Any value except empty and exactly `0`, as `main.rs` reads `PHOTOCRAFT_IN_WINDOW_MENUS` - which is
|
||||
/// why `off` means on there, why this rule is not shared with the stroke trace above, and why it does
|
||||
/// not trim: a padded `" 0 "` enables the menus there, so it keeps a process here too.
|
||||
fn flag_is_on(value: &str) -> bool {
|
||||
!value.is_empty() && value != "0"
|
||||
}
|
||||
|
||||
/// A path is an option; an empty value is no path. Not trimmed: a path may end in a space. An empty
|
||||
/// root is read as no root unless `--control` is given, and that flag keeps its own process already.
|
||||
fn path_is_set(value: &str) -> bool {
|
||||
!value.is_empty()
|
||||
}
|
||||
|
||||
/// The per-process options this launch's environment sets, named as the flags they stand for. A
|
||||
/// launch that sets only variables would otherwise hand its files to the running instance, which
|
||||
/// never sees the override, and exit.
|
||||
pub fn env_launch_options() -> Vec<&'static str> {
|
||||
env_launch_options_from(|name| std::env::var_os(name))
|
||||
}
|
||||
|
||||
/// [`env_launch_options`] with the reader given, so the rule is testable without the environment.
|
||||
fn env_launch_options_from(get: impl Fn(&str) -> Option<std::ffi::OsString>) -> Vec<&'static str> {
|
||||
ENV_LAUNCH_OPTIONS.iter().filter(|(name, _, is_on)| get(name).is_some_and(|v| is_on(&v.to_string_lossy()))).map(|(_, flag, _)| *flag).collect()
|
||||
}
|
||||
|
||||
/// Why this launch runs as its own instance, or `None` to take part in single-instance.
|
||||
pub fn skip_reason(c: &LaunchContext) -> Option<String> {
|
||||
if !c.per_user_settings {
|
||||
@@ -915,4 +976,46 @@ mod tests {
|
||||
assert_eq!(sandbox_socket(Some(OsStr::new("")), Some(run)), None);
|
||||
assert_eq!(sandbox_socket(Some(id), Some(OsStr::new(""))), None);
|
||||
}
|
||||
|
||||
/// A variable documented as the equivalent of a per-launch flag counts as that flag: a launch
|
||||
/// that sets only variables has per-process options, so it keeps its own process rather than
|
||||
/// handing its files to the running instance, which never sees the override.
|
||||
#[test]
|
||||
fn an_environment_override_counts_as_a_process_option() {
|
||||
let with = |values: &[(&str, &str)]| env_launch_options_from(|n| values.iter().find(|(k, _)| *k == n).map(|(_, v)| (*v).into()));
|
||||
assert_eq!(with(&[("PHOTOCRAFT_PRESENT_MODE", "novsync")]), vec!["--present-mode"]);
|
||||
assert_eq!(
|
||||
with(&[("PHOTOCRAFT_STROKE_TRACE", "strokes.csv"), ("PHOTOCRAFT_AUTOMATION_WRITE_ROOT", "/tmp/out")]),
|
||||
vec!["--stroke-trace", "--automation-write-root"]
|
||||
);
|
||||
// Nothing set, or a value that means off, is an ordinary launch. Each variable is off the way
|
||||
// it is read where it takes effect, not by one rule shared across all six.
|
||||
assert!(with(&[]).is_empty());
|
||||
assert!(with(&[("PHOTOCRAFT_IN_WINDOW_MENUS", "0"), ("PHOTOCRAFT_FRAME_LATENCY", "")]).is_empty());
|
||||
// The trace is off at `off` and `false` as well, and a value the presentation parser rejects
|
||||
// is ignored there, so neither keeps a process of its own here.
|
||||
assert!(with(&[("PHOTOCRAFT_STROKE_TRACE", "off"), ("PHOTOCRAFT_PRESENT_MODE", "maybe"), ("PHOTOCRAFT_FRAME_LATENCY", "4")]).is_empty());
|
||||
assert!(with(&[("PHOTOCRAFT_AUTOMATION_READ_ROOT", "")]).is_empty());
|
||||
// Spaces are part of the value only where the reader keeps them: the trace and the two
|
||||
// presentation values are trimmed where they take effect, the menu flag and a path are not.
|
||||
assert_eq!(with(&[("PHOTOCRAFT_PRESENT_MODE", " novsync "), ("PHOTOCRAFT_STROKE_TRACE", " 1 ")]), vec!["--present-mode", "--stroke-trace"]);
|
||||
assert_eq!(
|
||||
with(&[("PHOTOCRAFT_IN_WINDOW_MENUS", " 0 "), ("PHOTOCRAFT_AUTOMATION_WRITE_ROOT", " /tmp/out ")]),
|
||||
vec!["--in-window-menus", "--automation-write-root"]
|
||||
);
|
||||
// A whitespace-only menu value is not empty, so it enables the menus there and here too.
|
||||
assert_eq!(with(&[("PHOTOCRAFT_IN_WINDOW_MENUS", " ")]), vec!["--in-window-menus"]);
|
||||
// `PHOTOCRAFT_IN_WINDOW_MENUS=off` means menus in the window there, so it means it here too.
|
||||
assert_eq!(with(&[("PHOTOCRAFT_IN_WINDOW_MENUS", "off")]), vec!["--in-window-menus"]);
|
||||
let base = LaunchContext {
|
||||
per_user_settings: true,
|
||||
new_instance_flag: false,
|
||||
new_instance_env: None,
|
||||
control: false,
|
||||
unreadable_paths: false,
|
||||
launch_options: &[],
|
||||
};
|
||||
let reason = skip_reason(&LaunchContext { launch_options: &with(&[("PHOTOCRAFT_PRESENT_MODE", "novsync")]), ..base });
|
||||
assert!(reason.as_deref().is_some_and(|r| r.contains("--present-mode")), "{reason:?}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user