Feat: always_on_top CLI Setting (#2715)

* feat: always_on_top cli setting

* revision: add user message for configuring settings
This commit is contained in:
Miles Murgaw authored and GitHub committed 2024-07-26 16:27:56 -07:00
1 parent e42b71b1ba
commit 810f8bbfb8
5 files changed
+74 -13

No files matched your search

+20
View File
@@ -89,6 +89,9 @@ pub struct DioxusConfig {
#[serde(default)] #[serde(default)]
pub web: WebConfig, pub web: WebConfig,
#[serde(default)]
pub desktop: DesktopConfig,
#[serde(default)] #[serde(default)]
pub bundle: BundleConfig, pub bundle: BundleConfig,
} }
@@ -129,6 +132,7 @@ impl Default for DioxusConfig {
pre_compress: true, pre_compress: true,
wasm_opt: Default::default(), wasm_opt: Default::default(),
}, },
desktop: DesktopConfig::default(),
bundle: BundleConfig { bundle: BundleConfig {
identifier: Some(format!("io.github.{name}")), identifier: Some(format!("io.github.{name}")),
publisher: Some(name), publisher: Some(name),
@@ -206,6 +210,22 @@ impl Default for WebConfig {
} }
} }
/// Represents configuration items for the desktop platform.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DesktopConfig {
/// Describes whether a debug-mode desktop app should be always-on-top.
#[serde(default)]
pub always_on_top: bool,
}
impl Default for DesktopConfig {
fn default() -> Self {
Self {
always_on_top: true,
}
}
}
/// The wasm-opt configuration /// The wasm-opt configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)] #[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct WasmOptConfig { pub struct WasmOptConfig {
+26 -2
View File
@@ -30,21 +30,42 @@ pub enum Config {
SetGlobal { setting: Setting, value: Value }, SetGlobal { setting: Setting, value: Value },
} }
#[derive(Debug, Clone, Deserialize, clap::ValueEnum)] #[derive(Debug, Clone, Copy, Deserialize, clap::ValueEnum)]
pub enum Setting { pub enum Setting {
/// Set the value of the always-hot-reload setting. /// Set the value of the always-hot-reload setting.
AlwaysHotReload, AlwaysHotReload,
/// Set the value of the always-open-browser setting. /// Set the value of the always-open-browser setting.
AlwaysOpenBrowser, AlwaysOpenBrowser,
/// Set the value of the always-on-top desktop setting.
AlwaysOnTop,
}
impl Display for Setting {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::AlwaysHotReload => write!(f, "always_hot_reload"),
Self::AlwaysOpenBrowser => write!(f, "always_open_browser"),
Self::AlwaysOnTop => write!(f, "always_on_top"),
}
}
} }
// NOTE: Unsure of an alternative to get the desired behavior with clap, if it exists. // NOTE: Unsure of an alternative to get the desired behavior with clap, if it exists.
#[derive(Debug, Clone, Deserialize, clap::ValueEnum)] #[derive(Debug, Clone, Copy, Deserialize, clap::ValueEnum)]
pub enum Value { pub enum Value {
True, True,
False, False,
} }
impl Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::True => write!(f, "true"),
Self::False => write!(f, "false"),
}
}
}
impl From<Value> for bool { impl From<Value> for bool {
fn from(value: Value) -> Self { fn from(value: Value) -> Self {
match value { match value {
@@ -90,11 +111,14 @@ impl Config {
file.write_all(content.as_bytes())?; file.write_all(content.as_bytes())?;
tracing::info!("🚩 Create custom html file done."); tracing::info!("🚩 Create custom html file done.");
} }
// Handle configuration of global CLI settings.
Config::SetGlobal { setting, value } => { Config::SetGlobal { setting, value } => {
CliSettings::modify_settings(|settings| match setting { CliSettings::modify_settings(|settings| match setting {
Setting::AlwaysHotReload => settings.always_hot_reload = Some(value.into()), Setting::AlwaysHotReload => settings.always_hot_reload = Some(value.into()),
Setting::AlwaysOpenBrowser => settings.always_open_browser = Some(value.into()), Setting::AlwaysOpenBrowser => settings.always_open_browser = Some(value.into()),
Setting::AlwaysOnTop => settings.always_on_top = Some(value.into()),
})?; })?;
tracing::info!("🚩 CLI setting `{setting}` has been set to `{value}`")
} }
} }
Ok(()) Ok(())
+11 -2
View File
@@ -16,14 +16,18 @@ pub struct ServeArguments {
#[clap(flatten)] #[clap(flatten)]
pub address: AddressArguments, pub address: AddressArguments,
/// Open the app in the default browser [default: false - unless project or global settings are set] /// Open the app in the default browser [default: true - unless cli settings are set]
#[arg(long, default_missing_value="true", num_args=0..=1)] #[arg(long, default_missing_value="true", num_args=0..=1)]
pub open: Option<bool>, pub open: Option<bool>,
/// Enable full hot reloading for the app [default: true - unless project or global settings are set] /// Enable full hot reloading for the app [default: true - unless cli settings are set]
#[clap(long, group = "release-incompatible")] #[clap(long, group = "release-incompatible")]
pub hot_reload: Option<bool>, pub hot_reload: Option<bool>,
/// Configure always-on-top for desktop apps [default: true - unless cli settings are set]
#[clap(long, default_missing_value = "true")]
pub always_on_top: Option<bool>,
/// Set cross-origin-policy to same-origin [default: false] /// Set cross-origin-policy to same-origin [default: false]
#[clap(name = "cross-origin-policy")] #[clap(name = "cross-origin-policy")]
#[clap(long)] #[clap(long)]
@@ -64,6 +68,11 @@ impl Serve {
if self.server_arguments.open.is_none() { if self.server_arguments.open.is_none() {
self.server_arguments.open = Some(settings.always_open_browser.unwrap_or_default()); self.server_arguments.open = Some(settings.always_open_browser.unwrap_or_default());
} }
if self.server_arguments.always_on_top.is_none() {
self.server_arguments.always_on_top = Some(settings.always_on_top.unwrap_or(true))
}
crate_config.dioxus_config.desktop.always_on_top =
self.server_arguments.always_on_top.unwrap_or(true);
// Resolve the build arguments // Resolve the build arguments
self.build_arguments.resolve(crate_config)?; self.build_arguments.resolve(crate_config)?;
+2
View File
@@ -23,6 +23,8 @@ pub struct CliSettings {
pub always_hot_reload: Option<bool>, pub always_hot_reload: Option<bool>,
/// Describes whether the CLI should always open the browser for Web targets. /// Describes whether the CLI should always open the browser for Web targets.
pub always_open_browser: Option<bool>, pub always_open_browser: Option<bool>,
/// Describes whether desktop apps in development will be pinned always-on-top.
pub always_on_top: Option<bool>,
} }
impl CliSettings { impl CliSettings {
+15 -9
View File
@@ -49,15 +49,21 @@ impl Config {
/// Initializes a new `WindowBuilder` with default values. /// Initializes a new `WindowBuilder` with default values.
#[inline] #[inline]
pub fn new() -> Self { pub fn new() -> Self {
let window: WindowBuilder = WindowBuilder::new() let dioxus_config = dioxus_cli_config::CURRENT_CONFIG.as_ref();
.with_title(
dioxus_cli_config::CURRENT_CONFIG let mut window: WindowBuilder = WindowBuilder::new().with_title(
.as_ref() dioxus_config
.map(|c| c.application.name.clone()) .map(|c| c.application.name.clone())
.unwrap_or("Dioxus App".to_string()), .unwrap_or("Dioxus App".to_string()),
) );
// During development we want the window to be on top so we can see it while we work
.with_always_on_top(cfg!(debug_assertions)); // During development we want the window to be on top so we can see it while we work
let always_on_top = dioxus_config
.map(|c| c.desktop.always_on_top)
.unwrap_or(true);
if cfg!(debug_assertions) {
window = window.with_always_on_top(always_on_top);
}
Self { Self {
window, window,