feat: support system and explicit themes in GUI download helper

This commit is contained in:
Kevin Yue committed 2026-11-05 14:24:02 +08:00
1 parent 0212b7ff91
commit b26a416f45
18 files changed
+338 -51

No files matched your search

+20
View File
@@ -11,6 +11,26 @@ pub enum GuiVariant {
Nsh,
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[serde(rename_all = "kebab-case")]
pub enum GuiTheme {
#[default]
System,
Light,
Dark,
}
impl GuiTheme {
pub fn as_str(self) -> &'static str {
match self {
Self::System => "system",
Self::Light => "light",
Self::Dark => "dark",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct GuiSwitchRequest {
+3 -1
View File
@@ -128,7 +128,9 @@ impl<'a> DesktopLauncher<'a> {
// the same cancellation, service ownership and recovery budget as gpgui.
let outcome = {
let attempt = async {
let program = launcher.resolve_program(&cancellation).await?;
let program = launcher
.resolve_program(&cancellation, crate::gui::GuiTheme::System)
.await?;
#[cfg(target_os = "linux")]
return launcher
.launch_with_switches(
@@ -13,6 +13,7 @@ pub struct GuiHelperLauncher<'a> {
credential: &'a SessionCredential,
gui_version: Option<&'a str>,
variant: crate::gui::GuiVariant,
theme: crate::gui::GuiTheme,
}
impl<'a> GuiHelperLauncher<'a> {
@@ -23,6 +24,7 @@ impl<'a> GuiHelperLauncher<'a> {
credential,
gui_version: None,
variant: crate::gui::GuiVariant::Tauri,
theme: crate::gui::GuiTheme::System,
}
}
@@ -41,6 +43,11 @@ impl<'a> GuiHelperLauncher<'a> {
self
}
pub fn theme(mut self, theme: crate::gui::GuiTheme) -> Self {
self.theme = theme;
self
}
pub async fn launch(&self, cancellation: &CancellationToken) -> anyhow::Result<()> {
let mut cmd = Command::new(&self.program);
@@ -57,6 +64,9 @@ impl<'a> GuiHelperLauncher<'a> {
if let Some(gui_version) = self.gui_version {
cmd.arg("--gui-version").arg(gui_version);
}
if self.theme != crate::gui::GuiTheme::System {
cmd.arg("--theme").arg(self.theme.as_str());
}
info!("Launching gpgui-helper");
anyhow::ensure!(
@@ -93,6 +103,40 @@ mod tests {
path
}
#[tokio::test]
async fn forwards_explicit_themes_and_leaves_initial_downloads_on_system() {
use crate::gui::{GuiTheme, GuiVariant};
let dir = tempfile::TempDir::new().unwrap();
let credential = SessionCredential::generate(uuid::Uuid::new_v4(), "test").unwrap();
let captured = dir.path().join("arguments");
for variant in [GuiVariant::Tauri, GuiVariant::Nsh] {
for theme in [GuiTheme::System, GuiTheme::Light, GuiTheme::Dark] {
let mut launcher = GuiHelperLauncher::new(&credential)
.variant(variant)
.gui_version(Some("test"))
.theme(theme);
launcher.program = fixture(
&dir,
&credential,
&format!("printf '%s\\n' \"$@\" > '{}'", captured.display()),
);
launcher.launch(&CancellationToken::new()).await.unwrap();
let mut expected = vec!["--service-credential-on-stdin"];
if variant == GuiVariant::Nsh {
expected.extend(["--gui-variant", "nsh"]);
}
expected.extend(["--gui-version", "test"]);
if theme != GuiTheme::System {
expected.extend(["--theme", theme.as_str()]);
}
assert_eq!(
std::fs::read_to_string(&captured).unwrap().lines().collect::<Vec<_>>(),
expected
);
}
}
}
#[tokio::test]
async fn propagates_helper_failure_and_preserves_stderr_in_desktop_log() {
let dir = tempfile::TempDir::new().unwrap();
@@ -107,6 +151,7 @@ mod tests {
credential: &credential,
gui_version: None,
variant: crate::gui::GuiVariant::Tauri,
theme: crate::gui::GuiTheme::System,
};
let error = launcher.launch(&CancellationToken::new()).await.unwrap_err();
assert!(error.to_string().contains("gpgui-helper exited with"), "{error:#}");
@@ -134,6 +179,7 @@ mod tests {
credential: &credential,
gui_version: None,
variant: crate::gui::GuiVariant::Tauri,
theme: crate::gui::GuiTheme::System,
};
let error = launcher.launch(&CancellationToken::new()).await.unwrap_err();
let eligible =
@@ -156,6 +202,7 @@ mod tests {
credential: &credential,
gui_version: None,
variant: crate::gui::GuiVariant::Tauri,
theme: crate::gui::GuiTheme::System,
};
let cancellation = CancellationToken::new();
let attempt = launcher.launch(&cancellation);
+21 -7
View File
@@ -51,7 +51,11 @@ impl<'a> GuiLauncher<'a> {
self
}
pub(crate) async fn resolve_program(&self, cancellation: &CancellationToken) -> anyhow::Result<PathBuf> {
pub(crate) async fn resolve_program(
&self,
cancellation: &CancellationToken,
helper_theme: crate::gui::GuiTheme,
) -> anyhow::Result<PathBuf> {
anyhow::ensure!(
uzers::get_effective_uid() != 0,
"The desktop GUI must not be launched by root"
@@ -59,7 +63,7 @@ impl<'a> GuiLauncher<'a> {
if let Some(program) = self.find_compatible_program(cancellation).await? {
return Ok(program);
}
self.download_program(cancellation).await?;
self.download_program(cancellation, helper_theme).await?;
let program = self.variant.update_target();
tokio::select! {
biased;
@@ -139,13 +143,18 @@ impl<'a> GuiLauncher<'a> {
Ok(())
}
async fn download_program(&self, cancellation: &CancellationToken) -> anyhow::Result<()> {
async fn download_program(
&self,
cancellation: &CancellationToken,
theme: crate::gui::GuiTheme,
) -> anyhow::Result<()> {
let gui_helper = GuiHelperLauncher::new(&self.credential);
gui_helper
.envs(self.envs.as_ref())
.gui_version(Some(self.version))
.variant(self.variant)
.theme(theme)
.launch(cancellation)
.await
}
@@ -177,7 +186,8 @@ impl<'a> GuiLauncher<'a> {
let (mut socket, _) = accepted?;
let mut pending = false;
let replacement = async {
let selection = gui_switch::receive(&mut socket).await?;
let command = gui_switch::receive(&mut socket).await?;
let selection = command.selection;
let variant = selection.variant;
let candidate = Self::new(self.version, self.credential.clone(), variant).envs(self.envs.clone());
let program = candidate.find_compatible_program(cancellation).await?;
@@ -210,7 +220,7 @@ impl<'a> GuiLauncher<'a> {
.minimized(false);
let program = match program {
Some(program) => program,
None => launcher.resolve_program(cancellation).await?,
None => launcher.resolve_program(cancellation, command.theme).await?,
};
let mut child = launcher.start_owned(program, cancellation.child_token());
let start_result = tokio::select! {
@@ -401,7 +411,7 @@ mod tests {
}
let launcher = launcher(VERSION, vec![first, second]);
let cancellation = CancellationToken::new();
let attempt = launcher.resolve_program(&cancellation);
let attempt = launcher.resolve_program(&cancellation, crate::gui::GuiTheme::System);
tokio::pin!(attempt);
tokio::select! {
result = &mut attempt => panic!("version probe exited early: {result:?}"),
@@ -602,7 +612,10 @@ mod tests {
crate::gui::save_preference(crate::gui::GuiVariant::Nsh).unwrap();
let credential = Arc::new(SessionCredential::generate(Uuid::new_v4(), VERSION).unwrap());
let selected = GuiLauncher::new(VERSION, credential, crate::gui::preferred().unwrap());
let program = selected.resolve_program(&CancellationToken::new()).await.unwrap();
let program = selected
.resolve_program(&CancellationToken::new(), crate::gui::GuiTheme::System)
.await
.unwrap();
assert_eq!(program.file_name().unwrap(), "gpgui-nsh");
}
@@ -707,6 +720,7 @@ mod tests {
variant: crate::gui::GuiVariant::Nsh,
allow_download: scenario == "runtime-too-old-confirmed",
},
crate::gui::GuiTheme::System,
)
.await;
if runtime_too_old {
+28 -18
View File
@@ -6,7 +6,15 @@ use tokio::{
net::{UnixListener, UnixStream},
};
use crate::gui::{GuiSwitchOutcome, GuiSwitchRequest};
use crate::gui::{GuiSwitchOutcome, GuiSwitchRequest, GuiTheme};
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub(crate) struct SwitchCommand {
#[serde(flatten)]
pub selection: GuiSwitchRequest,
#[serde(default)]
pub theme: GuiTheme,
}
pub(crate) const SOCKET_ENV: &str = "GP_DESKTOP_CONTROL_SOCKET";
pub(crate) const READY_ENV: &str = "GP_DESKTOP_READY_SOCKET";
@@ -22,19 +30,22 @@ pub fn notify_ready() -> anyhow::Result<()> {
Ok(())
}
pub async fn request(selection: GuiSwitchRequest) -> anyhow::Result<GuiSwitchOutcome> {
pub async fn request(selection: GuiSwitchRequest, theme: GuiTheme) -> anyhow::Result<GuiSwitchOutcome> {
let path = std::env::var_os(SOCKET_ENV).context("GUI switching requires a desktop supervisor")?;
request_at(std::path::Path::new(&path), selection).await
request_at(std::path::Path::new(&path), selection, theme).await
}
pub(super) async fn request_at(
path: &std::path::Path,
selection: GuiSwitchRequest,
theme: GuiTheme,
) -> anyhow::Result<GuiSwitchOutcome> {
let mut socket = UnixStream::connect(path)
.await
.context("Could not contact the desktop supervisor")?;
socket.write_all(&serde_json::to_vec(&selection)?).await?;
socket
.write_all(&serde_json::to_vec(&SwitchCommand { selection, theme })?)
.await?;
socket.shutdown().await?;
let mut reply = Vec::new();
socket.take(4096).read_to_end(&mut reply).await?;
@@ -74,7 +85,7 @@ impl SwitchListener {
}
}
pub(crate) async fn receive(socket: &mut UnixStream) -> anyhow::Result<GuiSwitchRequest> {
pub(crate) async fn receive(socket: &mut UnixStream) -> anyhow::Result<SwitchCommand> {
anyhow::ensure!(
socket.peer_cred()?.uid() == uzers::get_effective_uid(),
"Desktop supervisor user mismatch"
@@ -107,22 +118,21 @@ mod tests {
let control = SwitchListener::new().unwrap();
let path = control.path();
assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600);
let client = tokio::spawn(async move {
let mut socket = UnixStream::connect(path).await.unwrap();
socket
.write_all(&serde_json::to_vec(&selection).unwrap())
.await
.unwrap();
socket.shutdown().await.unwrap();
let mut reply = Vec::new();
socket.read_to_end(&mut reply).await.unwrap();
serde_json::from_slice::<Result<GuiSwitchOutcome, String>>(&reply).unwrap()
});
let client = tokio::spawn(async move { request_at(&path, selection, GuiTheme::Dark).await });
let (mut socket, _) = control.listener.accept().await.unwrap();
assert_eq!(receive(&mut socket).await.unwrap(), selection);
assert_eq!(
receive(&mut socket).await.unwrap(),
SwitchCommand {
selection,
theme: GuiTheme::Dark
}
);
reply(&mut socket, Err("Selected GUI could not start".into()))
.await
.unwrap();
assert_eq!(client.await.unwrap().unwrap_err(), "Selected GUI could not start");
assert_eq!(
client.await.unwrap().unwrap_err().to_string(),
"Selected GUI could not start"
);
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ pub use command_traits::CommandExt;
pub mod auth_launcher;
pub mod desktop_launcher;
pub mod gui_launcher;
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", test))]
pub mod gui_switch;
#[cfg(any(target_os = "linux", test))]
mod nsh_runtime;