From 8ce97640ddb08dd0915aeeebd40bab86aadd8f34 Mon Sep 17 00:00:00 2001 From: spx Date: Tue, 29 Jan 2013 16:02:16 +0900 Subject: [PATCH] OnChangeAction: Tweaks --- Library/Measure.cpp | 47 ++++++++++++++++++++++++++++----------------- Library/Measure.h | 18 +++++++++++++---- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/Library/Measure.cpp b/Library/Measure.cpp index 2b40783e..208d566b 100644 --- a/Library/Measure.cpp +++ b/Library/Measure.cpp @@ -88,7 +88,7 @@ CMeasure::CMeasure(CMeterWindow* meterWindow, const WCHAR* name) : CSection(mete m_Disabled(false), m_Initialized(false), m_OldValue(), - m_OldValueInitialized(false) + m_ValueAssigned(false) { } @@ -98,6 +98,7 @@ CMeasure::CMeasure(CMeterWindow* meterWindow, const WCHAR* name) : CSection(mete */ CMeasure::~CMeasure() { + delete m_OldValue; } /* @@ -116,6 +117,8 @@ void CMeasure::Initialize() */ void CMeasure::ReadOptions(CConfigParser& parser, const WCHAR* section) { + bool oldOnChangeActionEmpty = m_OnChangeAction.empty(); + // Clear substitutes to prevent from being added more than once. if (!m_Substitute.empty()) { @@ -172,6 +175,12 @@ void CMeasure::ReadOptions(CConfigParser& parser, const WCHAR* section) const std::wstring& group = parser.ReadString(section, L"Group", L""); InitializeGroup(group); + + if (m_Initialized && + oldOnChangeActionEmpty && !m_OnChangeAction.empty()) + { + DoChangeAction(false); + } } void CMeasure::Disable() @@ -493,6 +502,8 @@ bool CMeasure::Update() m_MinValue = min(m_MinValue, medianValue); } + m_ValueAssigned = true; + if (m_MeterWindow) { if (!m_IfEqualAction.empty()) @@ -729,31 +740,31 @@ void CMeasure::RemoveTrailingZero(WCHAR* str, int strLen) } /* -** Execute OnChangeAction if action is set +** Executes OnChangeAction if action is set. +** If execute parameter is set to false, only updates old value with current value. ** */ -void CMeasure::DoChangeAction() +void CMeasure::DoChangeAction(bool execute) { - if (!m_OldValueInitialized) + if (!m_OnChangeAction.empty() && m_ValueAssigned) { double newValue = GetValue(); const WCHAR* newStringValue = GetStringValue(AUTOSCALE_OFF, 1, -1, false); - m_OldValue = newValue; - m_OldStringValue = newStringValue; - m_OldValueInitialized = true; - } - else if (!m_OnChangeAction.empty()) - { - double newValue = GetValue(); - const WCHAR* newStringValue = GetStringValue(AUTOSCALE_OFF, 1, -1, false); - - if (m_OldValue != newValue || wcscmp(m_OldStringValue.c_str(), newStringValue) != 0) + if (!m_OldValue) { - m_OldValue = newValue; - m_OldStringValue = newStringValue; - - Rainmeter->ExecuteCommand(m_OnChangeAction.c_str(), m_MeterWindow); + m_OldValue = new CMeasureValueSet(newValue, newStringValue); + } + else if (execute) + { + if (m_OldValue->IsChanged(newValue, newStringValue)) + { + Rainmeter->ExecuteCommand(m_OnChangeAction.c_str(), m_MeterWindow); + } + } + else + { + m_OldValue->Set(newValue, newStringValue); } } } diff --git a/Library/Measure.h b/Library/Measure.h index 7c7d5a02..5387b2c4 100644 --- a/Library/Measure.h +++ b/Library/Measure.h @@ -37,6 +37,17 @@ enum AUTOSCALE AUTOSCALE_ON = AUTOSCALE_1024 }; +class CMeasureValueSet +{ +public: + CMeasureValueSet(double val, const WCHAR* str) : m_Value(val), m_StringValue(str) {} + void Set(double val, const WCHAR* str) { m_Value = val; m_StringValue = str; } + bool IsChanged(double val, const WCHAR* str) { if (m_Value != val || wcscmp(m_StringValue.c_str(), str) != 0) { Set(val, str); return true; } return false; } +private: + double m_Value; + std::wstring m_StringValue; +}; + class CMeter; class CMeterWindow; class CConfigParser; @@ -68,7 +79,7 @@ public: static void RemoveTrailingZero(WCHAR* str, int strLen); const std::wstring& GetOnChangeAction() { return m_OnChangeAction; } - void DoChangeAction(); + void DoChangeAction(bool execute = true); CMeterWindow* GetMeterWindow() { return m_MeterWindow; } @@ -114,9 +125,8 @@ protected: bool m_Initialized; std::wstring m_OnChangeAction; - double m_OldValue; - std::wstring m_OldStringValue; - bool m_OldValueInitialized; + CMeasureValueSet* m_OldValue; + bool m_ValueAssigned; }; #endif