1use std::collections::BTreeMap;
10
11use super::{
12 CameraControls, Config, DeviceIdentity, LightSettings, Lighting, ScrollResolution, SmartShift,
13 ThumbwheelSensitivity,
14};
15use crate::binding::{
16 ActionRingConfig, ActionRingIcon, ActionRingSlot, Binding, ButtonId, RingAction,
17};
18use crate::hid::Dpi;
19
20impl Config {
21 #[must_use]
26 pub fn stored_bindings(&self, device_key: &str) -> BTreeMap<ButtonId, Binding> {
27 self.devices
28 .get(device_key)
29 .map(|d| d.bindings.clone())
30 .unwrap_or_default()
31 }
32
33 pub fn set_binding(&mut self, device_key: &str, button: ButtonId, binding: Binding) {
38 self.devices
39 .entry(device_key.to_string())
40 .or_default()
41 .bindings
42 .insert(button, binding);
43 }
44
45 #[must_use]
48 pub fn action_ring(&self, device_key: &str) -> ActionRingConfig {
49 self.devices
50 .get(device_key)
51 .map(|device| device.action_ring.clone())
52 .unwrap_or_default()
53 }
54
55 pub fn set_action_ring_enabled(&mut self, device_key: &str, enabled: bool) {
57 self.devices
58 .entry(device_key.to_string())
59 .or_default()
60 .action_ring
61 .enabled = enabled;
62 }
63
64 pub fn set_action_ring_haptics(&mut self, device_key: &str, enabled: bool) {
66 self.devices
67 .entry(device_key.to_string())
68 .or_default()
69 .action_ring
70 .haptics = enabled;
71 }
72
73 pub fn set_action_ring_slot(
75 &mut self,
76 device_key: &str,
77 slot: ActionRingSlot,
78 action: Option<RingAction>,
79 ) {
80 self.devices
81 .entry(device_key.to_string())
82 .or_default()
83 .action_ring
84 .default
85 .set_action(slot, action);
86 }
87
88 pub fn set_action_ring_icon(
90 &mut self,
91 device_key: &str,
92 slot: ActionRingSlot,
93 icon: Option<ActionRingIcon>,
94 ) {
95 self.devices
96 .entry(device_key.to_string())
97 .or_default()
98 .action_ring
99 .default
100 .set_icon(slot, icon);
101 }
102
103 #[must_use]
106 pub fn dpi_presets(&self, device_key: &str) -> Vec<Dpi> {
107 self.devices
108 .get(device_key)
109 .map(|d| d.dpi_presets.clone())
110 .unwrap_or_default()
111 }
112
113 pub fn set_dpi_presets(&mut self, device_key: &str, presets: Vec<Dpi>) {
117 self.devices
118 .entry(device_key.to_string())
119 .or_default()
120 .dpi_presets = presets;
121 }
122
123 #[must_use]
127 pub fn device_identity(&self, device_key: &str) -> Option<&DeviceIdentity> {
128 self.devices
129 .get(device_key)
130 .and_then(|d| d.identity.as_ref())
131 }
132
133 pub fn set_device_identity(&mut self, device_key: &str, identity: DeviceIdentity) {
136 self.devices
137 .entry(device_key.to_string())
138 .or_default()
139 .identity = Some(identity.without_unit_identifiers());
140 }
141
142 pub fn remove_device(&mut self, device_key: &str) -> bool {
145 self.devices.remove(device_key).is_some()
146 }
147
148 #[must_use]
150 pub fn device_custom_name(&self, device_key: &str) -> Option<&str> {
151 self.devices
152 .get(device_key)
153 .and_then(|device| device.custom_name.as_deref())
154 }
155
156 pub fn set_device_custom_name(&mut self, device_key: &str, custom_name: Option<String>) {
159 self.devices
160 .entry(device_key.to_string())
161 .or_default()
162 .custom_name = custom_name;
163 }
164
165 pub fn known_identities(&self) -> impl Iterator<Item = (&str, &DeviceIdentity)> {
169 self.devices
170 .iter()
171 .filter_map(|(k, d)| d.identity.as_ref().map(|i| (k.as_str(), i)))
172 }
173
174 #[must_use]
176 pub fn lighting(&self, device_key: &str) -> Option<Lighting> {
177 self.devices
178 .get(device_key)
179 .and_then(|d| d.lighting.clone())
180 }
181
182 pub fn set_lighting(&mut self, device_key: &str, lighting: Lighting) {
184 self.devices
185 .entry(device_key.to_string())
186 .or_default()
187 .lighting = Some(lighting);
188 }
189
190 #[must_use]
192 pub fn camera_controls(&self, device_key: &str) -> Option<CameraControls> {
193 self.devices
194 .get(device_key)
195 .and_then(|d| d.camera_controls.clone())
196 }
197
198 pub fn set_camera_controls(&mut self, device_key: &str, controls: CameraControls) {
200 self.devices
201 .entry(device_key.to_string())
202 .or_default()
203 .camera_controls = Some(controls);
204 }
205
206 #[must_use]
208 pub fn camera_profiles(&self, device_key: &str) -> BTreeMap<String, CameraControls> {
209 self.devices
210 .get(device_key)
211 .map(|d| d.camera_profiles.clone())
212 .unwrap_or_default()
213 }
214
215 pub fn save_camera_profile(&mut self, device_key: &str, name: &str, snap: CameraControls) {
217 self.devices
218 .entry(device_key.to_string())
219 .or_default()
220 .camera_profiles
221 .insert(name.to_string(), snap);
222 }
223
224 pub fn delete_camera_profile(&mut self, device_key: &str, name: &str) {
227 if let Some(device) = self.devices.get_mut(device_key) {
228 device.camera_profiles.remove(name);
229 if device.camera_profile.as_deref() == Some(name) {
230 device.camera_profile = None;
231 }
232 }
233 }
234
235 #[must_use]
237 pub fn camera_active_profile(&self, device_key: &str) -> Option<String> {
238 self.devices
239 .get(device_key)
240 .and_then(|d| d.camera_profile.clone())
241 }
242
243 pub fn set_camera_active_profile(&mut self, device_key: &str, name: Option<String>) {
245 self.devices
246 .entry(device_key.to_string())
247 .or_default()
248 .camera_profile = name;
249 }
250
251 #[must_use]
253 pub fn light(&self, device_key: &str) -> Option<LightSettings> {
254 self.devices.get(device_key).and_then(|d| d.light)
255 }
256
257 pub fn set_light(&mut self, device_key: &str, light: LightSettings) {
259 self.devices
260 .entry(device_key.to_string())
261 .or_default()
262 .light = Some(light);
263 }
264
265 #[must_use]
267 pub fn dpi(&self, device_key: &str) -> Option<Dpi> {
268 self.devices.get(device_key).and_then(|d| d.dpi)
269 }
270
271 pub fn set_dpi(&mut self, device_key: &str, dpi: Dpi) {
274 self.devices.entry(device_key.to_string()).or_default().dpi = Some(dpi);
275 }
276
277 #[must_use]
279 pub fn smartshift(&self, device_key: &str) -> Option<SmartShift> {
280 self.devices.get(device_key).and_then(|d| d.smartshift)
281 }
282
283 #[must_use]
286 pub fn fn_lock(&self, device_key: &str) -> Option<bool> {
287 self.devices.get(device_key).and_then(|d| d.fn_lock)
288 }
289
290 pub fn set_fn_lock(&mut self, device_key: &str, fn_lock: bool) {
293 self.devices
294 .entry(device_key.to_string())
295 .or_default()
296 .fn_lock = Some(fn_lock);
297 }
298
299 pub fn set_smartshift(&mut self, device_key: &str, smartshift: SmartShift) {
302 self.devices
303 .entry(device_key.to_string())
304 .or_default()
305 .smartshift = Some(smartshift);
306 }
307
308 #[must_use]
311 pub fn invert_scroll(&self, device_key: &str) -> bool {
312 self.devices
313 .get(device_key)
314 .is_some_and(|d| d.invert_scroll)
315 }
316
317 pub fn set_invert_scroll(&mut self, device_key: &str, invert: bool) {
320 self.devices
321 .entry(device_key.to_string())
322 .or_default()
323 .invert_scroll = invert;
324 }
325
326 #[must_use]
329 pub fn scroll_resolution(&self, device_key: &str) -> Option<ScrollResolution> {
330 self.devices
331 .get(device_key)
332 .and_then(|device| device.scroll_resolution)
333 }
334
335 pub fn set_scroll_resolution(
338 &mut self,
339 device_key: &str,
340 resolution: Option<ScrollResolution>,
341 ) {
342 self.devices
343 .entry(device_key.to_string())
344 .or_default()
345 .scroll_resolution = resolution;
346 }
347
348 #[must_use]
351 pub fn device_enabled(&self, device_key: &str) -> bool {
352 self.devices.get(device_key).is_none_or(|d| d.enabled)
353 }
354
355 pub fn set_device_enabled(&mut self, device_key: &str, enabled: bool) {
357 self.devices
358 .entry(device_key.to_string())
359 .or_default()
360 .enabled = enabled;
361 }
362
363 #[must_use]
366 pub fn thumbwheel_sensitivity(&self, device_key: &str) -> ThumbwheelSensitivity {
367 self.devices
368 .get(device_key)
369 .and_then(|d| d.thumbwheel_sensitivity)
370 .unwrap_or(self.app_settings.thumbwheel_sensitivity)
371 }
372
373 pub fn set_device_thumbwheel_sensitivity(
376 &mut self,
377 device_key: &str,
378 sensitivity: Option<ThumbwheelSensitivity>,
379 ) {
380 self.devices
381 .entry(device_key.to_string())
382 .or_default()
383 .thumbwheel_sensitivity = sensitivity;
384 }
385}