diff --git a/AdaptiveBrightness/main_window.cpp b/AdaptiveBrightness/main_window.cpp index 8a88d37..49aec55 100644 --- a/AdaptiveBrightness/main_window.cpp +++ b/AdaptiveBrightness/main_window.cpp @@ -291,23 +291,6 @@ void MainWindow::updateState() return true; }; - const auto setAllMonitorsBrightness = [this](float& brightness) { - if(m_monitors.empty()) { - return false; - } - - bool errorOccurred = false; - for(auto& monitor: m_monitors) { - if(!std::isnan(brightness) && !monitor.manualOverride) { - monitor.brightness = brightness; - } - if(!monitor.driver.setBrightness(monitor.brightness)) { - errorOccurred = true; - } - } - return !errorOccurred; - }; - auto brightness = NAN; if(!getAverageSensorValue(brightness)) { @@ -318,7 +301,7 @@ void MainWindow::updateState() const auto iconBrightness = utils::map(brightness, 0.f, 1.f, size_t{0}, size_t{8}); - if(!setAllMonitorsBrightness(brightness)) { + 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; @@ -339,6 +322,34 @@ void MainWindow::updateState() } } +bool MainWindow::setMonitorBrightness(float brightness) +{ + if(m_monitors.empty()) { + return false; + } + + bool errorOccurred = false; + + for(size_t i = 0; i < m_monitors.size(); ++i) { + if(!std::isnan(brightness) && !m_monitors[i].manualOverride) { + m_monitors[i].brightness = brightness; + } + + m_monitors[i].brightness = std::clamp(m_monitors[i].brightness, m_monitors[i].minBrightness, m_monitors[i].maxBrightness); + + if(!m_monitors[i].driver.setBrightness(m_monitors[i].brightness)) { + errorOccurred = true; + } + else { + if(m_ui.monitorDropdown->currentIndex() == i) { + updateCurrentMonitorGUI(i); + } + } + } + + return !errorOccurred; +} + void MainWindow::trayIconClicked(QSystemTrayIcon::ActivationReason reason) { if(reason == QSystemTrayIcon::ActivationReason::Trigger) { diff --git a/AdaptiveBrightness/main_window.hpp b/AdaptiveBrightness/main_window.hpp index 1048d70..7e68f22 100644 --- a/AdaptiveBrightness/main_window.hpp +++ b/AdaptiveBrightness/main_window.hpp @@ -46,6 +46,8 @@ class MainWindow : public QMainWindow { void updateState(); + bool setMonitorBrightness(float brightness); + void trayIconClicked(QSystemTrayIcon::ActivationReason reason); Ui::MainWindow m_ui;