use crate::deps::PathBuf;
#[derive(Debug, Clone)]
pub struct TrayConfig {
pub app_id: String,
pub tooltip: String,
pub icon_path: Option<PathBuf>,
pub event_channel_capacity: usize,
}
impl Default for TrayConfig {
fn default() -> Self {
Self {
app_id: "unistore".into(),
tooltip: "UniStore".into(),
icon_path: None,
event_channel_capacity: 64,
}
}
}
impl TrayConfig {
pub fn builder() -> TrayConfigBuilder {
TrayConfigBuilder::default()
}
}
#[derive(Debug, Default)]
pub struct TrayConfigBuilder {
app_id: Option<String>,
tooltip: Option<String>,
icon_path: Option<PathBuf>,
event_channel_capacity: Option<usize>,
}
impl TrayConfigBuilder {
pub fn app_id(mut self, app_id: impl Into<String>) -> Self {
self.app_id = Some(app_id.into());
self
}
pub fn tooltip(mut self, tooltip: impl Into<String>) -> Self {
self.tooltip = Some(tooltip.into());
self
}
pub fn icon_path(mut self, path: impl Into<PathBuf>) -> Self {
self.icon_path = Some(path.into());
self
}
pub fn event_channel_capacity(mut self, capacity: usize) -> Self {
self.event_channel_capacity = Some(capacity);
self
}
pub fn build(self) -> TrayConfig {
let default = TrayConfig::default();
TrayConfig {
app_id: self.app_id.unwrap_or(default.app_id),
tooltip: self.tooltip.unwrap_or(default.tooltip),
icon_path: self.icon_path.or(default.icon_path),
event_channel_capacity: self
.event_channel_capacity
.unwrap_or(default.event_channel_capacity),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = TrayConfig::default();
assert_eq!(config.app_id, "unistore");
assert_eq!(config.tooltip, "UniStore");
assert!(config.icon_path.is_none());
assert_eq!(config.event_channel_capacity, 64);
}
#[test]
fn test_builder() {
let config = TrayConfig::builder()
.app_id("myapp")
.tooltip("My Application")
.icon_path("/path/to/icon.png")
.event_channel_capacity(128)
.build();
assert_eq!(config.app_id, "myapp");
assert_eq!(config.tooltip, "My Application");
assert_eq!(
config.icon_path,
Some(PathBuf::from("/path/to/icon.png"))
);
assert_eq!(config.event_channel_capacity, 128);
}
#[test]
fn test_partial_builder() {
let config = TrayConfig::builder().tooltip("Custom Tooltip").build();
assert_eq!(config.app_id, "unistore"); assert_eq!(config.tooltip, "Custom Tooltip");
}
}