From c930e316c48401fb7c1cac4826292810e2d6b863 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Fri, 17 Jul 2020 17:52:18 +0200 Subject: [PATCH] Set tray icon to average brightness value --- AdaptiveBrightness/main_window.cpp | 15 +++++++++++---- AdaptiveBrightness/main_window.hpp | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/AdaptiveBrightness/main_window.cpp b/AdaptiveBrightness/main_window.cpp index c1b9b32..fe31789 100644 --- a/AdaptiveBrightness/main_window.cpp +++ b/AdaptiveBrightness/main_window.cpp @@ -301,14 +301,14 @@ void MainWindow::updateState() sensorError = true; } - const auto iconBrightness = utils::map(brightness, 0.f, 1.f, size_t{0}, size_t{8}); - if(!setMonitorBrightness(brightness)) { enumMonitors(); qInfo(ltr("At least one monitor became invalid, re-enumerating monitors found %1 valid monitor(s)").arg(m_monitors.size())); monitorError = true; } + const auto iconBrightness = utils::map(brightness, 0.f, 1.f, size_t{0}, size_t{8}); + if(!sensorError && !monitorError && m_trayIcon) { m_trayIcon->setIcon(*m_sensorStateIcons[iconBrightness]); } @@ -324,7 +324,7 @@ void MainWindow::updateState() } } -bool MainWindow::setMonitorBrightness(float brightness) +bool MainWindow::setMonitorBrightness(float& brightness) { if(m_monitors.empty()) { return false; @@ -352,8 +352,15 @@ bool MainWindow::setMonitorBrightness(float brightness) } } + if(!errorOccurred) { + auto avgBrightness = 0.f; + std::for_each(m_monitors.begin(), m_monitors.end(), [&avgBrightness](const auto& mon) { avgBrightness += mon.brightness; }); + avgBrightness /= m_monitors.size(); + brightness = avgBrightness; + } + if(allMonitorsSameBrightness) { - m_ui.monitorOverallBrightnessSlider->setValue(utils::map(m_monitors.front().brightness, 0.f, 1.f, 0, 100)); + m_ui.monitorOverallBrightnessSlider->setValue(utils::map(brightness, 0.f, 1.f, 0, 100)); } return !errorOccurred; diff --git a/AdaptiveBrightness/main_window.hpp b/AdaptiveBrightness/main_window.hpp index 7e68f22..dd7608e 100644 --- a/AdaptiveBrightness/main_window.hpp +++ b/AdaptiveBrightness/main_window.hpp @@ -46,7 +46,7 @@ class MainWindow : public QMainWindow { void updateState(); - bool setMonitorBrightness(float brightness); + bool setMonitorBrightness(float& brightness); void trayIconClicked(QSystemTrayIcon::ActivationReason reason);