fix(app): what is left of the virtual monitor's driver stays ours and can be removed from the screen

- a failed install forgets the driver (registry value, settings folder) only when its clean-up
  left no device and no package, and never when it was ours before; otherwise it stays ours and
  the error gives both the install's reason and that something is left to remove
- the driver status separates a usable monitor (installed) from what this app left without one
  (leftover) and names the button (action): uninstall for both, so a removal whose package step
  failed can be retried from the features screen
- pure rules after_failed_install and driver_status with unit tests; checked again on the win-ci
  VM: a declined install leaves nothing, then install and uninstall

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 03:00:54 +09:00
parent cfab18a47e
commit 9b53af169d
4 changed files with 93 additions and 29 deletions

View File

@@ -67,15 +67,43 @@ pub fn driver_dir() -> Option<PathBuf> {
pub struct DriverStatus {
/// Windows only.
pub supported: bool,
/// Installed by this app (its settings folder is the driver's).
/// Installed by this app with its monitor there: usable.
pub installed: bool,
/// Something this app installed is left without a usable monitor (a removal or a failed
/// install's clean-up that did not finish): to be removed.
pub leftover: bool,
/// Another program's copy of the same driver is installed; the app leaves it alone.
pub other: bool,
/// The button the screen offers: `install`, `uninstall` (also for a leftover), or none.
pub action: Option<&'static str>,
}
/// The status from whether the driver is ours (its registry value points at our settings), its
/// monitor is there, and any of its devices is.
#[cfg_attr(not(windows), allow(dead_code))]
fn driver_status(ours: bool, monitor: bool, devices: bool) -> DriverStatus {
let installed = ours && monitor;
let leftover = ours && !installed;
let other = (monitor || devices) && !ours;
let action = if ours { Some("uninstall") } else if other { None } else { Some("install") };
DriverStatus { supported: true, installed, leftover, other, action }
}
/// After a failed install: whether to forget the driver (its registry value and settings folder)
/// and what to report. Only an attempt that was not ours before and left no device and no package
/// (1168: not in the driver store) is forgotten; otherwise it stays ours so the screen can remove
/// what is left.
#[cfg_attr(not(windows), allow(dead_code))]
fn after_failed_install(failure: String, was_ours: bool, devices_left: usize, package: Result<(), u32>) -> (bool, String) {
match removal_done(devices_left, package) {
Ok(()) => (!was_ours, failure),
Err(e) => (false, format!("{failure}. 정리하지 못한 것이 남아 있으니 제거를 눌러 주세요 ({e})")),
}
}
#[cfg(not(windows))]
pub fn status() -> DriverStatus {
DriverStatus { supported: false, installed: false, other: false }
DriverStatus { supported: false, installed: false, leftover: false, other: false, action: None }
}
#[cfg(not(windows))]
@@ -232,12 +260,10 @@ mod win {
devices().into_iter().find(|d| d.string == ADAPTER_NAME)
}
/// Installed means ours with its monitor there (the driver loaded), so a device left without
/// its driver shows as not installed and can be installed again.
/// Installed means ours with its monitor there (the driver loaded); ours without it is a
/// leftover the screen offers to remove.
pub fn status() -> DriverStatus {
let present = adapter().is_some() || !hardware_devices().is_empty();
let ours = ours();
DriverStatus { supported: true, installed: ours && adapter().is_some(), other: present && !ours }
driver_status(ours(), adapter().is_some(), !hardware_devices().is_empty())
}
/// Put the virtual monitor on the desktop at `corner` and return where it is.
@@ -378,18 +404,15 @@ mod win {
if (had_device || adapter().is_some()) && !was_ours {
return Err("다른 프로그램이 설치한 가상 디스플레이 드라이버가 이미 있습니다".into());
}
let result = install_steps(&inf, had_device);
if result.is_err() {
if !had_device {
remove_devices();
}
if !was_ours {
let _ = uninstall_package(&inf);
let _ = run("reg", &["delete", &format!("HKLM\\{REG_KEY}"), "/f"]);
remove_config_dir();
}
let Err(failure) = install_steps(&inf, had_device) else { return Ok(()) };
let left = if had_device { 0 } else { remove_devices() };
let package = if was_ours || left > 0 { Ok(()) } else { uninstall_package(&inf) };
let (forget, why) = after_failed_install(failure, was_ours, left, package);
if forget {
let _ = run("reg", &["delete", &format!("HKLM\\{REG_KEY}"), "/f"]);
remove_config_dir();
}
result
Err(why)
}
fn install_steps(inf: &Path, had_device: bool) -> Result<(), String> {
@@ -558,6 +581,40 @@ fn removal_done(devices_left: usize, package: Result<(), u32>) -> Result<(), Str
mod tests {
use super::*;
#[test]
fn a_failed_install_forgets_the_driver_only_when_its_leftovers_are_gone() {
let declined = || "드라이버 설치를 취소했습니다".to_owned();
// Cleaned up: forgotten (when it was not ours before), the failure reported as it was.
assert_eq!(after_failed_install(declined(), false, 0, Err(1168)), (true, declined()));
// A device left: kept ours, so the leftover can be removed; both reasons reported.
let (forget, why) = after_failed_install(declined(), false, 1, Ok(()));
assert!(!forget, "a device left keeps the registry value");
assert!(why.starts_with(&declined()) && why.contains("제거"), "{why}");
// A package left: the same.
let (forget, why) = after_failed_install(declined(), false, 0, Err(5));
assert!(!forget);
assert!(why.contains("제거"), "{why}");
// Ours before the attempt: never forgotten by a failed reinstall.
assert!(!after_failed_install(declined(), true, 0, Ok(())).0);
}
#[test]
fn what_is_left_of_the_driver_can_always_be_removed_from_the_screen() {
// Installed and working: remove.
let s = driver_status(true, true, true);
assert!(s.installed && !s.leftover && !s.other);
assert_eq!(s.action, Some("uninstall"));
// Device gone but the package (and the registry value) left after a failed removal.
let s = driver_status(true, false, false);
assert!(!s.installed && s.leftover);
assert_eq!(s.action, Some("uninstall"), "the leftover can be removed again");
// Nothing of ours: install. Another program's copy: nothing to press.
assert_eq!(driver_status(false, false, false).action, Some("install"));
let s = driver_status(false, true, true);
assert!(s.other && !s.installed && !s.leftover);
assert_eq!(s.action, None);
}
#[test]
fn a_removal_forgets_the_driver_only_when_device_and_package_are_gone() {
assert_eq!(removal_done(0, Ok(())), Ok(()));