deskflow/src/lib/gui/StyleUtils.h

67 lines
1.8 KiB
C++

/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2024 Synergy App Ltd
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#pragma once
#include <QDir>
#include <QFileInfoList>
#include <QFontDatabase>
#include <QIcon>
#include <QPalette>
#include <QStyleHints>
#include "common/Constants.h"
namespace deskflow::gui {
/**
* @brief Detects dark mode in a universal manner (all Qt versions).
* Until better platform support is added, it's more reliable to use the old way (compare text and window lightness),
* because the newer versions in Qt 6.5+ are not always correct and some return `UnknownScheme`.
* https://www.qt.io/blog/dark-mode-on-windows-11-with-qt-6.5
*/
inline bool isDarkMode()
{
const QPalette defaultPalette;
const auto text = defaultPalette.color(QPalette::WindowText);
const auto window = defaultPalette.color(QPalette::Window);
return text.lightness() > window.lightness();
}
/**
* @brief get a string for the iconMode
* @returns "dark" or "light"
*/
inline QString iconMode()
{
return isDarkMode() ? QStringLiteral("dark") : QStringLiteral("light");
}
inline void updateIconTheme()
{
// Sets the fallback icon path and fallback theme
const auto themeName = QStringLiteral("%1-%2").arg(kAppId, iconMode());
if (QIcon::themeName().isEmpty() || QIcon::themeName().startsWith(kAppId))
QIcon::setThemeName(themeName);
else
QIcon::setFallbackThemeName(themeName);
QIcon::setFallbackSearchPaths({QStringLiteral(":/icons/%1").arg(themeName)});
}
} // namespace deskflow::gui
inline QFont fixedFont()
{
#if defined(Q_OS_WIN)
QFont f({"Hack", "Liberation Mono", "Monospace", "Andale Mono"});
f.setStyleHint(QFont::Monospace);
#else
QFont f = QFontDatabase::systemFont(QFontDatabase::FixedFont);
#endif
#if defined(Q_OS_MAC)
f.setPointSize(12);
#endif
return f;
}