Skip to content

Commit

Permalink
feat: add config path to widget window title (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger authored Nov 27, 2024
1 parent 72a9ac2 commit b4aa09f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
14 changes: 14 additions & 0 deletions packages/desktop/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,20 @@ impl Config {
.into()
}

/// Formats a widget's config path for display.
///
/// Returns relative path without the `.zebar.json` suffix (e.g.
/// `starter/vanilla`).
pub fn formatted_widget_path(&self, config_path: &PathBuf) -> String {
let path = self.to_relative_path(config_path).to_unicode_string();

// Ensure path delimiters are forward slashes on Windows.
#[cfg(windows)]
let path = path.replace('\\', "/");

path.strip_suffix(".zebar.json").unwrap_or(&path).into()
}

/// Returns the widget config at the given path.
///
/// Config path can be either absolute or relative.
Expand Down
21 changes: 6 additions & 15 deletions packages/desktop/src/sys_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,14 @@ impl SysTray {
widget_states: &HashMap<PathBuf, Vec<WidgetState>>,
startup_configs: &HashMap<PathBuf, StartupConfig>,
) -> anyhow::Result<Submenu<Wry>> {
let formatted_config_path =
Self::format_config_path(&self.config, config_path);

let label = match widget_states.get(config_path) {
None => formatted_config_path,
None => self.config.formatted_widget_path(config_path),
Some(states) => {
format!("({}) {}", states.len(), formatted_config_path)
format!(
"({}) {}",
states.len(),
self.config.formatted_widget_path(config_path)
)
}
};

Expand Down Expand Up @@ -495,14 +496,4 @@ impl SysTray {

Ok(config_menu.build()?)
}

/// Formats the config path for display in the system tray.
fn format_config_path(
config: &Arc<Config>,
config_path: &PathBuf,
) -> String {
let path = config.to_relative_path(config_path).to_unicode_string();

path.strip_suffix(".zebar.json").unwrap_or(&path).into()
}
}
5 changes: 4 additions & 1 deletion packages/desktop/src/widget_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ impl WidgetFactory {
widget_id.clone(),
webview_url,
)
.title("Zebar")
.title(format!(
"Zebar - {}",
self.config.formatted_widget_path(&config_path)
))
.focused(widget_config.focused)
.skip_taskbar(!widget_config.shown_in_taskbar)
.visible_on_all_workspaces(true)
Expand Down

0 comments on commit b4aa09f

Please sign in to comment.