Files
ReLibreHardwareMonitor/LibreHardwareMonitor.Windows.WinUI/ViewModels/SensorSelectionService.cs
T
JMR-devandClaude Opus 4.8 6cd0694cd7 Extract SensorSelectionService from MainWindowViewModel
Moves tray/gadget sensor selection (the per-sensor 'tray'/'gadget' settings,
their key convention, and the GadgetSensorsChanged/TraySensorsChanged events)
into a focused, testable collaborator. The view model keeps thin delegating
methods and forwards the events, so its public surface (used by MainWindow) is
unchanged. Behavior is unchanged.

Adds 3 tests (persistence round-trip, event raising, tray filtering).

196 tests pass (was 193).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-02 10:47:40 -05:00

85 lines
2.8 KiB
C#

// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Copyright (C) LibreHardwareMonitor and Contributors.
using System;
using System.Collections.Generic;
using System.Linq;
using LibreHardwareMonitor.Hardware;
using LibreHardwareMonitor.Windows.WinUI.Services;
namespace LibreHardwareMonitor.Windows.WinUI.ViewModels;
/// <summary>
/// Tracks which sensors the user has chosen to show in the system-tray icons and in the desktop gadget, persisting each
/// choice as a per-sensor setting. Extracted from <see cref="MainWindowViewModel" /> to keep that selection concern (and
/// its settings-key convention) in one testable place.
/// </summary>
internal sealed class SensorSelectionService
{
private readonly AppSettings _settings;
public SensorSelectionService(AppSettings settings)
{
_settings = settings;
}
public event EventHandler? GadgetSensorsChanged;
public event EventHandler? TraySensorsChanged;
public bool IsSensorInGadget(SensorTreeItemViewModel item)
{
return item.Sensor != null && _settings.GetValue(GetSensorSettingName(item.Sensor, "gadget"), false);
}
public bool IsSensorInTray(SensorTreeItemViewModel item)
{
return item.Sensor != null && _settings.GetValue(GetSensorSettingName(item.Sensor, "tray"), false);
}
public IEnumerable<SensorTreeItemViewModel> GetGadgetSensorItems(SensorTreeItemViewModel? root)
{
return root?.EnumerateSensors().Where(IsSensorInGadget) ?? [];
}
public IEnumerable<SensorTreeItemViewModel> GetTraySensorItems(SensorTreeItemViewModel? root)
{
return root?.EnumerateSensors().Where(IsSensorInTray) ?? [];
}
public void SetSensorInGadget(SensorTreeItemViewModel item, bool value)
{
if (item.Sensor == null)
return;
SetSensorBooleanSetting(item.Sensor, "gadget", value);
GadgetSensorsChanged?.Invoke(this, EventArgs.Empty);
}
public void SetSensorInTray(SensorTreeItemViewModel item, bool value)
{
if (item.Sensor == null)
return;
SetSensorBooleanSetting(item.Sensor, "tray", value);
if (!value)
_settings.Remove(GetSensorSettingName(item.Sensor, "traycolor"));
TraySensorsChanged?.Invoke(this, EventArgs.Empty);
}
public static string GetSensorSettingName(ISensor sensor, string suffix)
{
return new Identifier(sensor.Identifier, suffix).ToString();
}
private void SetSensorBooleanSetting(ISensor sensor, string suffix, bool value)
{
string settingName = GetSensorSettingName(sensor, suffix);
if (value)
_settings.SetValue(settingName, true);
else
_settings.Remove(settingName);
}
}