Move useful helper functions to utils

This commit is contained in:
BlackMark 2020-07-06 20:12:37 +02:00
parent 07e8222c6b
commit cf7f4e9a2d
4 changed files with 32 additions and 25 deletions

View File

@ -4,6 +4,7 @@
#include <QtGlobal>
#include "log_tr.hpp"
#include "utils.hpp"
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), m_currentIcon(0), m_visibility(true)
{
@ -56,21 +57,6 @@ MainWindow::~MainWindow()
qDebug(ltr("Destroying main window"));
}
namespace {
template<typename>
struct array_size;
template<typename T, size_t N>
struct array_size<std::array<T, N>> {
static constexpr auto value = N;
};
template<typename T>
inline constexpr auto array_size_v = array_size<T>::value;
} // namespace
void MainWindow::loadIcons()
{
qDebug(ltr("Loading icons"));
@ -81,7 +67,7 @@ void MainWindow::loadIcons()
":/AdaptiveBrightness/resources/nosense_6.ico", ":/AdaptiveBrightness/resources/nosense_7.ico", ":/AdaptiveBrightness/resources/nosense_8.ico",
};
static_assert(NO_SENSOR_ICON_PATHS.size() == array_size_v<decltype(m_noSensorStateIcons)>, "Number of paths does not match number of icons");
static_assert(NO_SENSOR_ICON_PATHS.size() == utils::array_size_v<decltype(m_noSensorStateIcons)>, "Number of paths does not match number of icons");
constexpr auto SENSOR_ICON_PATHS = std::array{
":/AdaptiveBrightness/resources/sense_0.ico", ":/AdaptiveBrightness/resources/sense_1.ico", ":/AdaptiveBrightness/resources/sense_2.ico",
@ -89,7 +75,7 @@ void MainWindow::loadIcons()
":/AdaptiveBrightness/resources/sense_6.ico", ":/AdaptiveBrightness/resources/sense_7.ico", ":/AdaptiveBrightness/resources/sense_8.ico",
};
static_assert(SENSOR_ICON_PATHS.size() == array_size_v<decltype(m_sensorStateIcons)>, "Number of paths does not match number of icons");
static_assert(SENSOR_ICON_PATHS.size() == utils::array_size_v<decltype(m_sensorStateIcons)>, "Number of paths does not match number of icons");
constexpr auto ERROR_ICON_PATH = ":/AdaptiveBrightness/resources/error.ico";

View File

@ -45,12 +45,6 @@ class Monitor {
Monitor(HMONITOR hMonitor, PHYSICAL_MONITOR physicalMonitor);
bool hasBrightnessCapability() const;
template<typename InType, typename OutType>
auto map(InType value, InType inMin, InType inMax, OutType outMin, OutType outMax)
{
return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
};
#endif

View File

@ -13,6 +13,7 @@
#include "get_last_error.hpp"
#include "log_tr.hpp"
#include "utils.hpp"
Monitor::Monitor(HMONITOR hMonitor, PHYSICAL_MONITOR physicalMonitor)
: m_hMonitor(hMonitor)
@ -52,7 +53,7 @@ bool Monitor::setBrightness(float percentage)
return false;
}
const auto mappedBrightness = map(percentage, 0.f, 1.f, m_brightnessRange->first, m_brightnessRange->second);
const auto mappedBrightness = utils::map(percentage, 0.f, 1.f, m_brightnessRange->first, m_brightnessRange->second);
const auto newBrightness = std::clamp<DWORD>(std::round(mappedBrightness), m_brightnessRange->first, m_brightnessRange->second);
if(!SetMonitorBrightness(m_physicalMonitor.handle, newBrightness)) {
@ -98,7 +99,7 @@ float Monitor::getBrightness()
m_brightnessRange = std::pair{minBrightness, maxBrightness};
m_errorOccurred = false;
return std::clamp(map(brightness, minBrightness, maxBrightness, 0.f, 1.f), 0.f, 1.f);
return std::clamp(utils::map(brightness, minBrightness, maxBrightness, 0.f, 1.f), 0.f, 1.f);
}
Monitor::operator bool() const

View File

@ -0,0 +1,26 @@
#pragma once
#include <array>
#include <cstddef>
namespace utils {
template<typename InType, typename OutType>
auto map(InType value, InType inMin, InType inMax, OutType outMin, OutType outMax)
{
return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
template<typename>
struct array_size;
template<typename T, size_t N>
struct array_size<std::array<T, N>> {
static constexpr auto value = N;
};
template<typename T>
inline constexpr auto array_size_v = array_size<T>::value;
} // namespace utils