|
| 1 | +use super::{ |
| 2 | + edit_typed_settings, refresh_interval_setting, TimedCache, Widget, WidgetAction, |
| 3 | + WidgetSettingsContext, WidgetSettingsUiResult, |
| 4 | +}; |
| 5 | +use crate::actions::Action; |
| 6 | +use crate::dashboard::dashboard::{DashboardContext, WidgetActivation}; |
| 7 | +use crate::gui::volume_data::{get_process_volumes, get_system_volume, ProcessVolume}; |
| 8 | +use eframe::egui; |
| 9 | +use serde::{Deserialize, Serialize}; |
| 10 | +use std::time::Duration; |
| 11 | + |
| 12 | +fn default_refresh_interval() -> f32 { |
| 13 | + 5.0 |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 17 | +pub struct VolumeConfig { |
| 18 | + #[serde(default = "default_refresh_interval")] |
| 19 | + pub refresh_interval_secs: f32, |
| 20 | + #[serde(default)] |
| 21 | + pub manual_refresh_only: bool, |
| 22 | +} |
| 23 | + |
| 24 | +impl Default for VolumeConfig { |
| 25 | + fn default() -> Self { |
| 26 | + Self { |
| 27 | + refresh_interval_secs: default_refresh_interval(), |
| 28 | + manual_refresh_only: false, |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Clone, Default)] |
| 34 | +struct VolumeSnapshot { |
| 35 | + system_volume: u8, |
| 36 | + processes: Vec<ProcessVolume>, |
| 37 | +} |
| 38 | + |
| 39 | +pub struct VolumeWidget { |
| 40 | + cfg: VolumeConfig, |
| 41 | + cache: TimedCache<VolumeSnapshot>, |
| 42 | + refresh_pending: bool, |
| 43 | +} |
| 44 | + |
| 45 | +impl VolumeWidget { |
| 46 | + pub fn new(cfg: VolumeConfig) -> Self { |
| 47 | + let interval = Duration::from_secs_f32(cfg.refresh_interval_secs.max(1.0)); |
| 48 | + Self { |
| 49 | + cfg, |
| 50 | + cache: TimedCache::new(VolumeSnapshot::default(), interval), |
| 51 | + refresh_pending: true, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + pub fn settings_ui( |
| 56 | + ui: &mut egui::Ui, |
| 57 | + value: &mut serde_json::Value, |
| 58 | + ctx: &WidgetSettingsContext<'_>, |
| 59 | + ) -> WidgetSettingsUiResult { |
| 60 | + edit_typed_settings(ui, value, ctx, |ui, cfg: &mut VolumeConfig, _ctx| { |
| 61 | + refresh_interval_setting( |
| 62 | + ui, |
| 63 | + &mut cfg.refresh_interval_secs, |
| 64 | + &mut cfg.manual_refresh_only, |
| 65 | + "Volume data is cached. The widget will skip refreshing until this many seconds have passed. Use Refresh to update immediately.", |
| 66 | + ) |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + fn refresh_interval(&self) -> Duration { |
| 71 | + Duration::from_secs_f32(self.cfg.refresh_interval_secs.max(1.0)) |
| 72 | + } |
| 73 | + |
| 74 | + fn update_interval(&mut self) { |
| 75 | + self.cache.set_interval(self.refresh_interval()); |
| 76 | + } |
| 77 | + |
| 78 | + fn refresh(&mut self) { |
| 79 | + self.update_interval(); |
| 80 | + let system_volume = get_system_volume().unwrap_or(50); |
| 81 | + let mut processes = get_process_volumes(); |
| 82 | + processes.sort_by(|a, b| a.name.cmp(&b.name).then(a.pid.cmp(&b.pid))); |
| 83 | + self.cache.refresh(|data| { |
| 84 | + data.system_volume = system_volume; |
| 85 | + data.processes = processes; |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + fn maybe_refresh(&mut self) { |
| 90 | + self.update_interval(); |
| 91 | + if self.refresh_pending { |
| 92 | + self.refresh_pending = false; |
| 93 | + self.refresh(); |
| 94 | + } else if !self.cfg.manual_refresh_only && self.cache.should_refresh() { |
| 95 | + self.refresh(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fn action(label: String, action: String) -> WidgetAction { |
| 100 | + WidgetAction { |
| 101 | + action: Action { |
| 102 | + label, |
| 103 | + desc: "Volume".into(), |
| 104 | + action, |
| 105 | + args: None, |
| 106 | + }, |
| 107 | + query_override: None, |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl Default for VolumeWidget { |
| 113 | + fn default() -> Self { |
| 114 | + Self::new(VolumeConfig::default()) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl Widget for VolumeWidget { |
| 119 | + fn render( |
| 120 | + &mut self, |
| 121 | + ui: &mut egui::Ui, |
| 122 | + _ctx: &DashboardContext<'_>, |
| 123 | + _activation: WidgetActivation, |
| 124 | + ) -> Option<WidgetAction> { |
| 125 | + self.maybe_refresh(); |
| 126 | + |
| 127 | + let mut clicked = None; |
| 128 | + |
| 129 | + ui.vertical(|ui| { |
| 130 | + ui.label("System volume"); |
| 131 | + ui.horizontal(|ui| { |
| 132 | + let resp = ui.add( |
| 133 | + egui::Slider::new(&mut self.cache.data.system_volume, 0..=100) |
| 134 | + .text("Level"), |
| 135 | + ); |
| 136 | + if resp.changed() { |
| 137 | + let label = format!("Set system volume to {}%", self.cache.data.system_volume); |
| 138 | + let action = format!("volume:set:{}", self.cache.data.system_volume); |
| 139 | + clicked.get_or_insert_with(|| Self::action(label, action)); |
| 140 | + } |
| 141 | + ui.label(format!("{}%", self.cache.data.system_volume)); |
| 142 | + if ui.button("Mute active").clicked() { |
| 143 | + clicked.get_or_insert_with(|| { |
| 144 | + Self::action( |
| 145 | + "Toggle mute for active window".into(), |
| 146 | + "volume:mute_active".into(), |
| 147 | + ) |
| 148 | + }); |
| 149 | + } |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + ui.separator(); |
| 154 | + if self.cache.data.processes.is_empty() { |
| 155 | + ui.label("No audio sessions found."); |
| 156 | + return clicked; |
| 157 | + } |
| 158 | + |
| 159 | + for proc in &mut self.cache.data.processes { |
| 160 | + ui.horizontal(|ui| { |
| 161 | + ui.label(format!("{} (PID {})", proc.name, proc.pid)); |
| 162 | + let resp = ui.add(egui::Slider::new(&mut proc.value, 0..=100).text("Level")); |
| 163 | + if resp.changed() { |
| 164 | + let label = format!("Set PID {} volume to {}%", proc.pid, proc.value); |
| 165 | + let action = format!("volume:pid:{}:{}", proc.pid, proc.value); |
| 166 | + clicked.get_or_insert_with(|| Self::action(label, action)); |
| 167 | + } |
| 168 | + ui.label(format!("{}%", proc.value)); |
| 169 | + let mute_label = if proc.muted { "Unmute" } else { "Mute" }; |
| 170 | + if ui.button(mute_label).clicked() { |
| 171 | + let label = format!("Toggle mute for PID {}", proc.pid); |
| 172 | + let action = format!("volume:pid_toggle_mute:{}", proc.pid); |
| 173 | + clicked.get_or_insert_with(|| Self::action(label, action)); |
| 174 | + proc.muted = !proc.muted; |
| 175 | + } |
| 176 | + if proc.muted { |
| 177 | + ui.colored_label(egui::Color32::RED, "muted"); |
| 178 | + } |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + clicked |
| 183 | + } |
| 184 | + |
| 185 | + fn on_config_updated(&mut self, settings: &serde_json::Value) { |
| 186 | + if let Ok(cfg) = serde_json::from_value::<VolumeConfig>(settings.clone()) { |
| 187 | + self.cfg = cfg; |
| 188 | + self.update_interval(); |
| 189 | + self.cache.invalidate(); |
| 190 | + self.refresh_pending = true; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + fn header_ui(&mut self, ui: &mut egui::Ui, _ctx: &DashboardContext<'_>) -> Option<WidgetAction> { |
| 195 | + let tooltip = format!( |
| 196 | + "Cached for {:.0}s. Refresh to update volume data immediately.", |
| 197 | + self.cfg.refresh_interval_secs |
| 198 | + ); |
| 199 | + if ui.small_button("Refresh").on_hover_text(tooltip).clicked() { |
| 200 | + self.refresh(); |
| 201 | + } |
| 202 | + None |
| 203 | + } |
| 204 | +} |
0 commit comments