changeset 5:51ae25154b70

Fix OS X support code
author Paper <mrpapersonic@gmail.com>
date Sat, 12 Aug 2023 13:10:34 -0400
parents 5af270662505
children 1d82f6e04d7d
files CMakeLists.txt src/filesystem.cpp src/include/sys/osx/filesystem.h src/main.cpp src/sys/osx/dark_theme.mm src/sys/osx/filesystem.mm
diffstat 6 files changed, 55 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Sat Aug 12 12:08:16 2023 -0400
+++ b/CMakeLists.txt	Sat Aug 12 13:10:34 2023 -0400
@@ -1,5 +1,5 @@
-cmake_minimum_required(VERSION 3.5)
-project(weeaboo)
+cmake_minimum_required(VERSION 3.16)
+project(weeaboo LANGUAGES CXX OBJCXX)
 
 set(SRC_FILES
 	src/main.cpp
@@ -20,7 +20,10 @@
 )
 
 if(APPLE)
-	list(APPEND SRC_FILES src/sys/osx/dark_theme.mm)
+	list(APPEND SRC_FILES
+		src/sys/osx/dark_theme.mm
+		src/sys/osx/filesystem.mm
+	)
 elseif(WIN32)
 	list(APPEND SRC_FILES src/sys/win32/dark_theme.cpp)
 endif()
@@ -32,6 +35,20 @@
 
 find_package(Qt5 COMPONENTS Widgets REQUIRED)
 find_package(CURL REQUIRED)
+
+set(LIBRARIES
+	${Qt5Widgets_LIBRARIES}
+	${CURL_LIBRARIES}
+)
+
+if(APPLE)
+	find_library(COCOA_LIBRARY Cocoa)
+	list(APPEND LIBRARIES ${COCOA_LIBRARY})
+endif()
+
 target_include_directories(weeaboo PUBLIC ${Qt5Widgets_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS} PRIVATE src/include src/icons)
 target_compile_options(weeaboo PRIVATE -Wall -Wextra -Wsuggest-override)
-target_link_libraries(weeaboo ${Qt5Widgets_LIBRARIES} ${CURL_LIBRARIES})
+if(APPLE)
+	target_compile_definitions(weeaboo PUBLIC MACOSX)
+endif()
+target_link_libraries(weeaboo ${LIBRARIES})
--- a/src/filesystem.cpp	Sat Aug 12 12:08:16 2023 -0400
+++ b/src/filesystem.cpp	Sat Aug 12 13:10:34 2023 -0400
@@ -1,12 +1,14 @@
 #ifdef _WIN32
 #include <shlobj.h>
-#elif defined(APPLE)
-#include <NSSearchPathForDirectoriesInDomains.h>
+#elif defined(MACOSX)
+#include "sys/osx/filesystem.h"
 #endif
 #include <filesystem>
 #include <limits.h>
+#include <QMessageBox>
 #include "config.h"
 #include "filesystem.h"
+#include "string_utils.h"
 
 std::filesystem::path get_config_path(void) {
 	std::filesystem::path cfg_path;
@@ -16,8 +18,7 @@
 		cfg_path = std::filesystem::path(buf) / CONFIG_NAME;
 #elif defined(MACOSX)
 	/* hope and pray that std::filesystem can handle tildes... */
-	CFString string = (CFString)NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
-	cfg_path = std::filesystem::path(StringUtils::Utf8ToWstr(std::string(CFStringGetCStringPtr(string, UTF8))));
+ 	cfg_path = std::filesystem::path(StringUtils::Utf8ToWstr(osx::GetApplicationSupportDirectory())) / CONFIG_DIR / CONFIG_NAME;
 #else // just assume POSIX
 	cfg_path = std::filesystem::path(getenv("HOME")) / ".config" / CONFIG_DIR / CONFIG_NAME;
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/include/sys/osx/filesystem.h	Sat Aug 12 13:10:34 2023 -0400
@@ -0,0 +1,7 @@
+#ifndef __sys__osx__filesystem_h
+#define __sys__osx__filesystem_h
+#include <string>
+namespace osx {
+	std::string GetApplicationSupportDirectory();
+}
+#endif // __sys__osx__filesystem_h
\ No newline at end of file
--- a/src/main.cpp	Sat Aug 12 12:08:16 2023 -0400
+++ b/src/main.cpp	Sat Aug 12 13:10:34 2023 -0400
@@ -1,7 +1,7 @@
 #include "window.h"
 #include "config.h"
 #include "anime.h"
-#if APPLE
+#if MACOSX
 #include "sys/osx/dark_theme.h"
 #elif WIN32
 #include "sys/win32/dark_theme.h"
@@ -138,7 +138,7 @@
 void MainWindow::ThemeChanged() {
 	switch (session.config.theme) {
 		case LIGHT: {
-#if APPLE
+#if MACOSX
 			if (osx::DarkThemeAvailable())
 				osx::SetToLightTheme();
 			else
@@ -149,7 +149,7 @@
 			break;
 		}
 		case DARK: {
-#if APPLE
+#if MACOSX
 			if (osx::DarkThemeAvailable())
 				osx::SetToDarkTheme();
 			else
@@ -160,7 +160,7 @@
 			break;
 		}
 		case OS: {
-#if APPLE
+#if MACOSX
 			if (osx::DarkThemeAvailable())
 				osx::SetToAutoTheme();
 			else
--- a/src/sys/osx/dark_theme.mm	Sat Aug 12 12:08:16 2023 -0400
+++ b/src/sys/osx/dark_theme.mm	Sat Aug 12 13:10:34 2023 -0400
@@ -3,12 +3,15 @@
 
 bool osx::DarkThemeAvailable()
 {
-	return (__builtin_available(macOS 10.14, *)) ? true : false;
+	if (@available(macOS 10.14, *))
+		return true;
+	else
+		return false;
 }
 
 bool osx::IsInDarkTheme()
 {
-    if (__builtin_available(macOS 10.14, *))
+    if (@available(macOS 10.14, *))
     {
         auto appearance = [NSApp.effectiveAppearance bestMatchFromAppearancesWithNames:
                 @[ NSAppearanceNameAqua, NSAppearanceNameDarkAqua ]];
@@ -20,7 +23,7 @@
 void osx::SetToDarkTheme()
 {
    // https://stackoverflow.com/questions/55925862/how-can-i-set-my-os-x-application-theme-in-code
-   if (__builtin_available(macOS 10.14, *))
+   if (@available(macOS 10.14, *))
    {
         [NSApp setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]];
    }
@@ -37,7 +40,7 @@
 
 void osx::SetToAutoTheme()
 {
-    if (__builtin_available(macOS 10.14, *))
+    if (@available(macOS 10.14, *))
     {
         [NSApp setAppearance:nil];
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sys/osx/filesystem.mm	Sat Aug 12 13:10:34 2023 -0400
@@ -0,0 +1,11 @@
+#include <Cocoa/Cocoa.h>
+#include <string>
+
+namespace osx {
+
+std::string GetApplicationSupportDirectory() {
+	NSArray* strings = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
+	return std::string([[strings objectAtIndex:0] UTF8String]);
+}
+
+}
\ No newline at end of file