From 17da2cb9ed9a6621c66b0c009c5cb5c7c0382b7b Mon Sep 17 00:00:00 2001 From: Cristi Paraschiv Date: Sun, 29 Mar 2020 19:17:10 +0300 Subject: [PATCH] Persist collapsed nodes (#222) Signed-off-by: chitza --- .../LibreHardwareMonitor.csproj | 1 + LibreHardwareMonitor/UI/HardwareNode.cs | 24 ++++++++++--- LibreHardwareMonitor/UI/IExpandPersistNode.cs | 7 ++++ LibreHardwareMonitor/UI/MainForm.cs | 36 +++++++++++++++++-- LibreHardwareMonitor/UI/TypeNode.cs | 22 ++++++++++-- 5 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 LibreHardwareMonitor/UI/IExpandPersistNode.cs diff --git a/LibreHardwareMonitor/LibreHardwareMonitor.csproj b/LibreHardwareMonitor/LibreHardwareMonitor.csproj index c46a47b..2328dd8 100644 --- a/LibreHardwareMonitor/LibreHardwareMonitor.csproj +++ b/LibreHardwareMonitor/LibreHardwareMonitor.csproj @@ -87,6 +87,7 @@ Properties\AssemblyVersion.cs + diff --git a/LibreHardwareMonitor/UI/HardwareNode.cs b/LibreHardwareMonitor/UI/HardwareNode.cs index 41374b5..9abed78 100644 --- a/LibreHardwareMonitor/UI/HardwareNode.cs +++ b/LibreHardwareMonitor/UI/HardwareNode.cs @@ -4,18 +4,20 @@ // Partial Copyright (C) Michael Möller and Contributors. // All Rights Reserved. -using System; -using System.Collections.Generic; using LibreHardwareMonitor.Hardware; using LibreHardwareMonitor.Utilities; +using System; +using System.Collections.Generic; namespace LibreHardwareMonitor.UI { - public class HardwareNode : Node + public class HardwareNode : Node, IExpandPersistNode { private readonly PersistentSettings _settings; private readonly UnitManager _unitManager; private readonly List _typeNodes = new List(); + private readonly string _expandedIdentifier; + private bool _expanded; public event EventHandler PlotSelectionChanged; @@ -23,19 +25,23 @@ namespace LibreHardwareMonitor.UI { _settings = settings; _unitManager = unitManager; + _expandedIdentifier = new Identifier(hardware.Identifier, "expanded").ToString(); Hardware = hardware; Image = HardwareTypeImage.Instance.GetImage(hardware.HardwareType); foreach (SensorType sensorType in Enum.GetValues(typeof(SensorType))) - _typeNodes.Add(new TypeNode(sensorType)); + _typeNodes.Add(new TypeNode(sensorType, hardware.Identifier, _settings)); foreach (ISensor sensor in hardware.Sensors) SensorAdded(sensor); hardware.SensorAdded += SensorAdded; hardware.SensorRemoved += SensorRemoved; + + _expanded = settings.GetValue(_expandedIdentifier, true); } + public override string Text { get { return Hardware.Name; } @@ -44,6 +50,16 @@ namespace LibreHardwareMonitor.UI public IHardware Hardware { get; } + public bool Expanded + { + get => _expanded; + set + { + _expanded = value; + _settings.SetValue(_expandedIdentifier, _expanded); + } + } + private void UpdateNode(TypeNode node) { if (node.Nodes.Count > 0) diff --git a/LibreHardwareMonitor/UI/IExpandPersistNode.cs b/LibreHardwareMonitor/UI/IExpandPersistNode.cs new file mode 100644 index 0000000..63c0441 --- /dev/null +++ b/LibreHardwareMonitor/UI/IExpandPersistNode.cs @@ -0,0 +1,7 @@ +namespace LibreHardwareMonitor.UI +{ + public interface IExpandPersistNode + { + bool Expanded { get; set; } + } +} diff --git a/LibreHardwareMonitor/UI/MainForm.cs b/LibreHardwareMonitor/UI/MainForm.cs index c80fbe8..474ba4d 100644 --- a/LibreHardwareMonitor/UI/MainForm.cs +++ b/LibreHardwareMonitor/UI/MainForm.cs @@ -1,7 +1,7 @@ // 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. -// Partial Copyright (C) Michael Möller and Contributors. +// Partial Copyright (C) Michael Möller and Contributors. // All Rights Reserved. using System; @@ -9,13 +9,15 @@ using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.IO; +using System.Linq; using System.Reflection; using System.Windows.Forms; using Aga.Controls.Tree; using Aga.Controls.Tree.NodeControls; using LibreHardwareMonitor.Hardware; -using LibreHardwareMonitor.Wmi; using LibreHardwareMonitor.Utilities; +using LibreHardwareMonitor.Wmi; + namespace LibreHardwareMonitor.UI { @@ -630,6 +632,8 @@ namespace LibreHardwareMonitor.UI if (_delayCount < 4) _delayCount++; + + RestoreCollapsedNodeState(treeView); } private void SaveConfiguration() @@ -687,9 +691,25 @@ namespace LibreHardwareMonitor.UI newBounds.Y = (Screen.PrimaryScreen.WorkingArea.Height / 2) - (newBounds.Height / 2); } Bounds = newBounds; + + RestoreCollapsedNodeState(treeView); + FormClosed += MainForm_FormClosed; } + private void RestoreCollapsedNodeState(TreeViewAdv treeViewAdv) + { + var collapsedHwNodes = treeViewAdv.AllNodes + .Where(n => n.IsExpanded && n.Tag is IExpandPersistNode expandPersistNode && !expandPersistNode.Expanded) + .OrderByDescending(n => n.Level) + .ToList(); + + foreach (TreeNodeAdv node in collapsedHwNodes) + { + node.IsExpanded = false; + } + } + private void CloseApplication() { FormClosed -= MainForm_FormClosed; @@ -718,10 +738,20 @@ namespace LibreHardwareMonitor.UI private void TreeView_Click(object sender, EventArgs e) { - if (!(e is MouseEventArgs m) || m.Button != MouseButtons.Right) + if (!(e is MouseEventArgs m) || (m.Button != MouseButtons.Left && m.Button != MouseButtons.Right)) return; NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y)); + if (m.Button == MouseButtons.Left && info.Node != null) + { + if (info.Node.Tag is IExpandPersistNode expandPersistNode) + { + expandPersistNode.Expanded = info.Node.IsExpanded; + } + + return; + } + treeView.SelectedNode = info.Node; if (info.Node != null) { diff --git a/LibreHardwareMonitor/UI/TypeNode.cs b/LibreHardwareMonitor/UI/TypeNode.cs index 6a06768..b23f080 100644 --- a/LibreHardwareMonitor/UI/TypeNode.cs +++ b/LibreHardwareMonitor/UI/TypeNode.cs @@ -5,14 +5,21 @@ // All Rights Reserved. using LibreHardwareMonitor.Hardware; +using LibreHardwareMonitor.Utilities; namespace LibreHardwareMonitor.UI { - public sealed class TypeNode : Node + public sealed class TypeNode : Node, IExpandPersistNode { - public TypeNode(SensorType sensorType) + private readonly PersistentSettings _settings; + private readonly string _expandedIdentifier; + private bool _expanded; + + public TypeNode(SensorType sensorType, Identifier parentId, PersistentSettings settings) { SensorType = sensorType; + _expandedIdentifier = new Identifier(parentId, SensorType.ToString(), ".expanded").ToString(); + _settings = settings; switch (sensorType) { @@ -76,6 +83,7 @@ namespace LibreHardwareMonitor.UI NodeAdded += TypeNode_NodeAdded; NodeRemoved += TypeNode_NodeRemoved; + _expanded = settings.GetValue(_expandedIdentifier, true); } private void TypeNode_NodeRemoved(Node node) @@ -104,5 +112,15 @@ namespace LibreHardwareMonitor.UI } public SensorType SensorType { get; } + + public bool Expanded + { + get => _expanded; + set + { + _expanded = value; + _settings.SetValue(_expandedIdentifier, _expanded); + } + } } }