//! The DPI stages stored in a Logitech mouse's running profile: change the //! values, how many there are and which one it starts on, then save. use super::*; use super::widgets::panel; use crate::hid::OnboardProfile; use serde_json::{json, Value}; const MOST: usize = 5; const STEP: i64 = 50; const PRESETS: [i64; 4] = [400, 800, 1600, 3200]; /// Edits to one profile's stages that are not saved yet. #[derive(Clone, PartialEq)] pub(super) struct StageDraft { sector: u32, /// The library's `DpiStage`s: `x`, `y` and `lod`. stages: Vec, default: usize, } impl StageDraft { /// The profile's used stages, if it has any. fn from_profile(p: &OnboardProfile) -> Option { let stages: Vec = p.dpi_stages.iter().filter(|s| s["x"].as_i64().is_some_and(|x| x > 0)).cloned().collect(); let default = (p.default_dpi_index.unwrap_or(0) as usize).min(stages.len().saturating_sub(1)); (!stages.is_empty()).then_some(Self { sector: p.sector, stages, default }) } /// Sets one stage's DPI. A Y that differs from X is the mouse's own /// setting and stays. fn set_dpi(&mut self, index: usize, value: i64) { let Some(stage) = self.stages.get_mut(index) else { return }; let (Some(x), Some(y)) = (stage["x"].as_i64(), stage["y"].as_i64()) else { return }; let next = value.clamp(100, 32_000); stage["x"] = json!(next); if y == x { stage["y"] = json!(next); } } /// Moves one stage's DPI by `by`. fn nudge(&mut self, index: usize, by: i64) { if let Some(x) = self.stages.get(index).and_then(|s| s["x"].as_i64()) { self.set_dpi(index, x + by); } } fn add(&mut self) { if let Some(mut last) = self.stages.last().cloned().filter(|_| self.stages.len() < MOST) { if let Some(x) = last["x"].as_i64() { let next = (x + 400).clamp(100, 32_000); if last["y"].as_i64() == Some(x) { last["y"] = json!(next); } last["x"] = json!(next); } self.stages.push(last); } } fn remove(&mut self, index: usize) { if self.stages.len() > 1 && index < self.stages.len() { self.stages.remove(index); if self.default >= self.stages.len() { self.default = self.stages.len() - 1; } else if index < self.default { self.default = self.default.saturating_sub(1); } } } #[cfg(test)] fn remove_last(&mut self) { let last = self.stages.len().saturating_sub(1); self.remove(last); } } impl AppView { /// The editor for the profile the mouse is running. pub(super) fn render_logitech_stages(&self, profile: &OnboardProfile, cx: &mut Context) -> Option
{ let saved = StageDraft::from_profile(profile)?; let draft = self.devices.stage_draft.clone().filter(|d| d.sector == profile.sector).unwrap_or_else(|| saved.clone()); let fg = cx.theme().foreground; let muted = cx.theme().muted_foreground; let can_add = draft.stages.len() < MOST; let multiple = draft.stages.len() > 1; let edit = |id: String, label: String, chosen: bool, change: Box, cx: &mut Context| { let from = draft.clone(); self.chip(id, label, chosen, cx, move |this, cx| { let mut next = from.clone(); change(&mut next); this.devices.stage_draft = Some(next); cx.notify(); }) }; let header = h_flex() .justify_between() .items_center() .gap_3() .child( div() .flex() .flex_col() .gap_0p5() .child( div() .text_sm() .font_weight(FontWeight::SEMIBOLD) .text_color(fg) .child(format!("Profile {} — DPI stages", profile.sector)), ) .child(div().text_xs().text_color(muted).child( "DPI steps stored in this onboard profile.", )), ) .when(can_add, |row| { let from = draft.clone(); row.child( Button::new("lg-stage-add") .compact() .label("+ Add stage") .on_click(cx.listener(move |this, _, _, cx| { let mut next = from.clone(); next.add(); this.devices.stage_draft = Some(next); cx.notify(); })), ) }); let mut card = panel(cx).child(header); for (i, stage) in draft.stages.iter().enumerate() { let x = stage["x"].as_i64().unwrap_or(0); let mut presets = segmented(cx); for preset in PRESETS { presets = presets.child(edit( format!("lg-stage-p-{i}-{preset}"), preset.to_string(), x == preset, Box::new(move |d| d.set_dpi(i, preset)), cx, )); } let stepper = segmented(cx) .items_center() .child(edit(format!("lg-stage-down-{i}"), "−".into(), false, Box::new(move |d| d.nudge(i, -STEP)), cx)) .child( div() .min_w(px(68.)) .text_center() .text_sm() .font_weight(FontWeight::MEDIUM) .text_color(fg) .child(format!("{x} DPI")), ) .child(edit(format!("lg-stage-up-{i}"), "+".into(), false, Box::new(move |d| d.nudge(i, STEP)), cx)); let mut left = h_flex() .gap_2() .items_center() .child(div().text_sm().font_weight(FontWeight::MEDIUM).text_color(fg).child(format!("Stage {}", i + 1))); if multiple { let default_chip = edit( format!("lg-stage-start-{i}"), "Default".into(), draft.default == i, Box::new(move |d| d.default = i), cx, ); left = left.child(segmented(cx).child(default_chip)); } let mut right = h_flex().gap_2().items_center().child(presets).child(stepper); if multiple { let from = draft.clone(); right = right.child( Button::new(SharedString::from(format!("lg-stage-rm-{i}"))) .ghost() .compact() .label("×") .tooltip("Remove stage") .on_click(cx.listener(move |this, _, _, cx| { let mut next = from.clone(); next.remove(i); this.devices.stage_draft = Some(next); cx.notify(); })), ); } card = card .child(super::device_page::divider(cx)) .child(h_flex().justify_between().items_center().gap_3().child(left).child(right)); } let changed = draft != saved; let sent = json!({ "stages": draft.stages, "defaultIndex": draft.default }); let footer = h_flex() .justify_end() .gap_2() .when(changed, |row| { row.child( Button::new("lg-stages-discard") .ghost() .compact() .label("Discard") .on_click(cx.listener(|this, _, _, cx| { this.devices.stage_draft = None; cx.notify(); })), ) }) .child( Button::new("lg-stages-save") .primary() .compact() .label("Save stages") .disabled(!changed || self.devices.writing.is_some()) .on_click(cx.listener(move |this, _, _, cx| { this.devices.stage_draft = None; this.onboard_action("stages", sent.clone(), cx); })), ); Some(card.child(super::device_page::divider(cx)).child(footer)) } } #[cfg(test)] mod tests { use super::StageDraft; use crate::hid::OnboardProfile; use serde_json::json; #[test] fn edits_keep_what_a_stage_already_holds() { let profile: OnboardProfile = serde_json::from_value(json!({ "sector": 1, "enabled": true, "isCurrent": true, "defaultDpiIndex": 1, "dpiStages": [{ "x": 400, "y": 400, "lod": 2 }, { "x": 800, "y": 900, "lod": 1 }, { "x": 0, "y": 0, "lod": 0 }], })) .unwrap(); let mut draft = StageDraft::from_profile(&profile).unwrap(); assert_eq!((draft.stages.len(), draft.default), (2, 1), "unused slots are left out"); draft.nudge(0, 50); draft.nudge(1, 50); assert_eq!(draft.stages[0], json!({ "x": 450, "y": 450, "lod": 2 })); assert_eq!(draft.stages[1], json!({ "x": 850, "y": 900, "lod": 1 })); draft.remove_last(); assert_eq!((draft.stages.len(), draft.default), (1, 0)); draft.remove_last(); assert_eq!(draft.stages.len(), 1, "a mouse keeps one stage"); } }