Skip to content

Commit

Permalink
CI fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexhuszagh committed Sep 7, 2024
1 parent e3b3fe7 commit 341a191
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
87 changes: 84 additions & 3 deletions example/breeze_theme.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,17 @@

#include <algorithm>
#include <array>
#include <cctype>
#include <cstdio>
#include <cstdint>
#include <filesystem>
#include <iostream>
#include <locale>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>

Expand All @@ -116,6 +119,11 @@
# include <unistd.h>
#endif

#if __APPLE__
# include <objc/runtime.h>
# include <objc/objc-runtime.h>
#endif

namespace breeze_stylesheets
{
// we need a lot of optional strings
Expand Down Expand Up @@ -169,6 +177,31 @@ namespace breeze_stylesheets
return (c <= 'Z' && c >= 'A') ? c - ('Z' - 'z') : c;
}

/// @brief Trim from the start (in place).
/// @param s The string to trim.
inline void
_ltrim(::std::string &s)
{
s.erase(s.begin(), ::std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !::std::isspace(ch); }));
}

/// @brief Trim from the end (in place).
/// @param s The string to trim.
inline void
_rtrim(::std::string &s)
{
s.erase(::std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !::std::isspace(ch); }).base(), s.end());
}

/// @brief Trim from both ends (in place).
/// @param s The string to trim.
inline void
_trim(::std::string &s)
{
_rtrim(s);
_ltrim(s);
}

#if __APPLE__ || __linux__

/// @brief Determine if the file is an executable.
Expand Down Expand Up @@ -362,7 +395,7 @@ namespace breeze_stylesheets
return ::std::make_tuple(
int32_t(major),
int32_t(minor),
std::stoi(build),
::std::stoi(build),
int32_t(platformId),
int32_t(0),
int32_t(0)
Expand Down Expand Up @@ -408,10 +441,58 @@ namespace breeze_stylesheets
}

#elif __APPLE__
# error "macOS not yet supported."

/// @brief Get if the macOS version supports theme detection.
/// @return If the macOS version supports theme detection.
inline bool
_macos_supported_version()
{
// This gives an output similar to:
// ProductName: Mac OS X
// ProductVersion: 10.12.5
// BuildVersion: 16F73
auto [stdout, code] = _run_command('sw_vers r');
if (code != EXIT_SUCCESS)
return false;
auto lines = _split(stdout, '\n');
const ::std::string version_key = "ProductVersion:";
for (auto &line : lines)
{
// find if we have a matching line and get the right value.
if (line.rfind(version_key, 0) != 0)
continue;
auto version = line.substr(version_key.length());
_trim(version);
auto parts = _split(version, '.');
auto major = ::std::stoi(parts.at(0));
auto minor = ::std::stoi(parts.at(1));
auto patch = ::std::stoi(parts.at(2));
if (major < 10)
return false;
if (major >= 11)
return true;
return minor >= 14;
}
return false;
}

/// @brief Get the current system theme.
/// @return The type of the system theme.
inline ::breeze_stylesheets::theme
get_theme()
{
// old macOS versions were always light
if (!_macos_supported_version())
return ::breeze_stylesheets::theme::light;

// TODO: Implement here
::std::cout << "TODO" << std::endl;
return ::breeze_stylesheets::theme::unknown;
}

#elif __linux__

/// @brief Get the current system theme. This requires Windows 10+.
/// @brief Get the current system theme.
/// @return The type of the system theme.
inline ::breeze_stylesheets::theme
get_theme()
Expand Down
8 changes: 6 additions & 2 deletions example/breeze_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def _initialize_advapi32() -> ctypes.CDLL:
# region macos


def macos_supported_version() -> bool:
def _macos_supported_version() -> bool:
'''Determine if we use a support macOS version.'''

# NOTE: This is typically 10.14.2 or 12.3
Expand All @@ -292,6 +292,10 @@ def macos_supported_version() -> bool:
def _get_theme_macos() -> Theme:
'''Get the current theme, as light or dark, for the system on macOS.'''

# old macOS versions were always light
if not _macos_supported_version():
return Theme.LIGHT

# NOTE: This can segfault on M1 and M2 Macs on Big Sur 11.4+. So, we also
# try reading directly using subprocess.
try:
Expand Down Expand Up @@ -538,7 +542,7 @@ def listener(callback: CallbackFn) -> None:
def register_functions() -> tuple[ThemeFn, ListenerFn]:
'''Register our global functions for our themes and listeners.'''

if sys.platform == 'darwin' and macos_supported_version():
if sys.platform == 'darwin':
return (_get_theme_macos, _listener_macos)
if sys.platform == 'win32' and platform.release().isdigit() and int(platform.release()) >= 10:
# Checks if running Windows 10 version 10.0.14393 (Anniversary Update) OR HIGHER.
Expand Down

0 comments on commit 341a191

Please sign in to comment.