Implement clamping to min and max brightness

This commit is contained in:
BlackMark 2020-07-17 17:21:08 +02:00
parent 348631fedc
commit 4956dde368
2 changed files with 31 additions and 18 deletions

View File

@ -291,23 +291,6 @@ void MainWindow::updateState()
return true; 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; auto brightness = NAN;
if(!getAverageSensorValue(brightness)) { 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}); const auto iconBrightness = utils::map(brightness, 0.f, 1.f, size_t{0}, size_t{8});
if(!setAllMonitorsBrightness(brightness)) { if(!setMonitorBrightness(brightness)) {
enumMonitors(); enumMonitors();
qInfo(ltr("At least one monitor became invalid, re-enumerating monitors found %1 valid monitor(s)").arg(m_monitors.size())); qInfo(ltr("At least one monitor became invalid, re-enumerating monitors found %1 valid monitor(s)").arg(m_monitors.size()));
monitorError = true; 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) void MainWindow::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
{ {
if(reason == QSystemTrayIcon::ActivationReason::Trigger) { if(reason == QSystemTrayIcon::ActivationReason::Trigger) {

View File

@ -46,6 +46,8 @@ class MainWindow : public QMainWindow {
void updateState(); void updateState();
bool setMonitorBrightness(float brightness);
void trayIconClicked(QSystemTrayIcon::ActivationReason reason); void trayIconClicked(QSystemTrayIcon::ActivationReason reason);
Ui::MainWindow m_ui; Ui::MainWindow m_ui;