From 1a1747a472e53633b6f5df4eee4691a158b0705a Mon Sep 17 00:00:00 2001 From: Daniel Dada Date: Thu, 18 Dec 2025 01:29:32 +0300 Subject: [PATCH] chore: got build working on mac (kind of) --- .vscode/launch.json | 18 +++++++- cmake/Dependencies.cmake | 49 ++++++++++++++++----- src/core/version.hpp | 2 +- src/gui/CMakeLists.txt | 3 ++ src/gui/file_browser.cpp | 82 +---------------------------------- src/gui/file_browser_macos.mm | 80 ++++++++++++++++++++++++++++++++++ src/gui/font_loader.cpp | 20 +++++---- src/gui/imgui_helpers.cpp | 10 ++++- 8 files changed, 160 insertions(+), 104 deletions(-) create mode 100644 src/gui/file_browser_macos.mm diff --git a/.vscode/launch.json b/.vscode/launch.json index 30b17db..b415052 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "version": "0.2.0", "configurations": [ { - "name": "Debug current target", + "name": "Debug current target (GDB)", "type": "cppdbg", "request": "launch", "program": "${command:cmake.launchTargetPath}", @@ -31,6 +31,22 @@ "ignoreFailures": true } ] + }, + { + "name": "Debug current target (LLDB)", + "type": "cppdbg", + "request": "launch", + "program": "${command:cmake.launchTargetPath}", + "args": [ + "--apply", + "--theme", + "dark" + ], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "lldb", }, ] } \ No newline at end of file diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index 934e21d..100dccd 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -8,6 +8,21 @@ if(WIN32) URL https://download.savannah.gnu.org/releases/freetype/freetype-2.14.1.tar.gz ) FetchContent_MakeAvailable(freetype) +elseif(APPLE) + option(USE_SYSTEM_GLFW ON) + find_package(Freetype REQUIRED) + find_package(ZLIB REQUIRED) + find_package(BZip2 REQUIRED) + find_package(PNG REQUIRED) + + + find_library(BROTLIDEC_LIBRARY NAMES brotlidec) + find_library(BROTLICOMMON_LIBRARY NAMES brotlicommon) + + find_package(PkgConfig QUIET) + if(PkgConfig_FOUND) + pkg_check_modules(HARFBUZZ harfbuzz) + endif() else() find_package(Freetype REQUIRED) find_package(PkgConfig REQUIRED) @@ -24,13 +39,24 @@ else() pkg_check_modules(WAYLAND_EGL wayland-egl) endif() -if (LINUX) +if(LINUX) find_package(PkgConfig REQUIRED) pkg_check_modules(GTK3 REQUIRED gtk+-3.0) endif() if(USE_SYSTEM_GLFW) - pkg_check_modules(GLFW REQUIRED glfw3) + if(APPLE) + find_package(glfw3 QUIET) + if(glfw3_FOUND) + set(GLFW_FOUND TRUE) + set(GLFW_LIBRARIES glfw) + else() + find_package(PkgConfig REQUIRED) + pkg_check_modules(GLFW REQUIRED glfw3) + endif() + else() + pkg_check_modules(GLFW REQUIRED glfw3) + endif() else() include(FetchContent) FetchContent_Declare( @@ -62,12 +88,13 @@ if(HARFBUZZ_FOUND) endif() set(WAYLAND_LIBS "") -if(WAYLAND_CLIENT_FOUND) - list(APPEND WAYLAND_LIBS ${WAYLAND_CLIENT_LIBRARIES}) - message(STATUS "Found Wayland client") -endif() -if(WAYLAND_EGL_FOUND) - list(APPEND WAYLAND_LIBS ${WAYLAND_EGL_LIBRARIES}) - message(STATUS "Found Wayland EGL") -endif() - +if(NOT APPLE) + if(WAYLAND_CLIENT_FOUND) + list(APPEND WAYLAND_LIBS ${WAYLAND_CLIENT_LIBRARIES}) + message(STATUS "Found Wayland client") + endif() + if(WAYLAND_EGL_FOUND) + list(APPEND WAYLAND_LIBS ${WAYLAND_EGL_LIBRARIES}) + message(STATUS "Found Wayland EGL") + endif() +endif() \ No newline at end of file diff --git a/src/core/version.hpp b/src/core/version.hpp index 3bc6207..88e106a 100644 --- a/src/core/version.hpp +++ b/src/core/version.hpp @@ -6,7 +6,7 @@ namespace clrsync::core { -const std::string GIT_SEMVER = "0.1.5+git.gd4ff415"; +const std::string GIT_SEMVER = "0.1.6+git.g57c3c55"; const std::string version_string(); } // namespace clrsync::core diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 7ee2bec..66ef295 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -15,6 +15,9 @@ set(GUI_SOURCES file_browser.cpp ${CMAKE_SOURCE_DIR}/lib/color_text_edit/TextEditor.cpp ) +if(APPLE) + list(APPEND GUI_SOURCES file_browser_macos.mm) +endif() add_executable(clrsync_gui ${GUI_SOURCES}) diff --git a/src/gui/file_browser.cpp b/src/gui/file_browser.cpp index f603554..db32ac0 100644 --- a/src/gui/file_browser.cpp +++ b/src/gui/file_browser.cpp @@ -138,85 +138,7 @@ std::string select_folder_dialog(const std::string& title, } -#else - -#ifdef __APPLE__ -#include - -namespace file_dialogs { - -std::string open_file_dialog(const std::string& title, - const std::string& initial_path, - const std::vector& filters) { - @autoreleasepool { - NSOpenPanel* panel = [NSOpenPanel openPanel]; - [panel setTitle:[NSString stringWithUTF8String:title.c_str()]]; - [panel setCanChooseFiles:YES]; - [panel setCanChooseDirectories:NO]; - [panel setAllowsMultipleSelection:NO]; - - if (!initial_path.empty()) { - NSURL* url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:initial_path.c_str()]]; - [panel setDirectoryURL:url]; - } - - if ([panel runModal] == NSModalResponseOK) { - NSURL* url = [[panel URLs] objectAtIndex:0]; - return std::string([[url path] UTF8String]); - } - } - return ""; -} - -std::string save_file_dialog(const std::string& title, - const std::string& initial_path, - const std::vector& filters) { - @autoreleasepool { - NSSavePanel* panel = [NSSavePanel savePanel]; - [panel setTitle:[NSString stringWithUTF8String:title.c_str()]]; - - if (!initial_path.empty()) { - std::filesystem::path p(initial_path); - if (std::filesystem::exists(p.parent_path())) { - NSURL* url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:p.parent_path().c_str()]]; - [panel setDirectoryURL:url]; - [panel setNameFieldStringValue:[NSString stringWithUTF8String:p.filename().c_str()]]; - } - } - - if ([panel runModal] == NSModalResponseOK) { - NSURL* url = [panel URL]; - return std::string([[url path] UTF8String]); - } - } - return ""; -} - -std::string select_folder_dialog(const std::string& title, - const std::string& initial_path) { - @autoreleasepool { - NSOpenPanel* panel = [NSOpenPanel openPanel]; - [panel setTitle:[NSString stringWithUTF8String:title.c_str()]]; - [panel setCanChooseFiles:NO]; - [panel setCanChooseDirectories:YES]; - [panel setAllowsMultipleSelection:NO]; - - if (!initial_path.empty()) { - NSURL* url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:initial_path.c_str()]]; - [panel setDirectoryURL:url]; - } - - if ([panel runModal] == NSModalResponseOK) { - NSURL* url = [[panel URLs] objectAtIndex:0]; - return std::string([[url path] UTF8String]); - } - } - return ""; -} - -} - -#else +#elif !defined(__APPLE__) #include @@ -338,6 +260,4 @@ std::string select_folder_dialog(const std::string& title, } -#endif - #endif \ No newline at end of file diff --git a/src/gui/file_browser_macos.mm b/src/gui/file_browser_macos.mm new file mode 100644 index 0000000..9d737bb --- /dev/null +++ b/src/gui/file_browser_macos.mm @@ -0,0 +1,80 @@ +#include "file_browser.hpp" +#include + +#ifdef __APPLE__ +#include + +namespace file_dialogs { + +std::string open_file_dialog(const std::string& title, + const std::string& initial_path, + const std::vector& filters) { + @autoreleasepool { + NSOpenPanel* panel = [NSOpenPanel openPanel]; + [panel setTitle:[NSString stringWithUTF8String:title.c_str()]]; + [panel setCanChooseFiles:YES]; + [panel setCanChooseDirectories:NO]; + [panel setAllowsMultipleSelection:NO]; + + if (!initial_path.empty()) { + NSURL* url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:initial_path.c_str()]]; + [panel setDirectoryURL:url]; + } + + if ([panel runModal] == NSModalResponseOK) { + NSURL* url = [[panel URLs] objectAtIndex:0]; + return std::string([[url path] UTF8String]); + } + } + return ""; +} + +std::string save_file_dialog(const std::string& title, + const std::string& initial_path, + const std::vector& filters) { + @autoreleasepool { + NSSavePanel* panel = [NSSavePanel savePanel]; + [panel setTitle:[NSString stringWithUTF8String:title.c_str()]]; + + if (!initial_path.empty()) { + std::filesystem::path p(initial_path); + if (std::filesystem::exists(p.parent_path())) { + NSURL* url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:p.parent_path().c_str()]]; + [panel setDirectoryURL:url]; + [panel setNameFieldStringValue:[NSString stringWithUTF8String:p.filename().c_str()]]; + } + } + + if ([panel runModal] == NSModalResponseOK) { + NSURL* url = [panel URL]; + return std::string([[url path] UTF8String]); + } + } + return ""; +} + +std::string select_folder_dialog(const std::string& title, + const std::string& initial_path) { + @autoreleasepool { + NSOpenPanel* panel = [NSOpenPanel openPanel]; + [panel setTitle:[NSString stringWithUTF8String:title.c_str()]]; + [panel setCanChooseFiles:NO]; + [panel setCanChooseDirectories:YES]; + [panel setAllowsMultipleSelection:NO]; + + if (!initial_path.empty()) { + NSURL* url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:initial_path.c_str()]]; + [panel setDirectoryURL:url]; + } + + if ([panel runModal] == NSModalResponseOK) { + NSURL* url = [[panel URLs] objectAtIndex:0]; + return std::string([[url path] UTF8String]); + } + } + return ""; +} + +} + +#endif \ No newline at end of file diff --git a/src/gui/font_loader.cpp b/src/gui/font_loader.cpp index 9840bd2..9ca1b8a 100644 --- a/src/gui/font_loader.cpp +++ b/src/gui/font_loader.cpp @@ -123,25 +123,27 @@ std::vector font_loader::load_font_macos(const char* font_name) if (!desc) return out; - CTFontRef font = CTFontCreateWithFontDescriptor(desc, 0, nullptr); + CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(desc, kCTFontURLAttribute); CFRelease(desc); - if (!font) + if (!url) return out; - CFDataRef data = CTFontCopyTable(font, kCTFontTableCFF, 0); - if (!data) - data = CTFontCopyTable(font, kCTFontTableHead, 0); + CFDataRef data = nullptr; + Boolean success = CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, url, &data, nullptr, nullptr, nullptr); + CFRelease(url); - if (data) + if (success && data) { CFIndex size = CFDataGetLength(data); - out.resize(size); - CFDataGetBytes(data, CFRangeMake(0, size), out.data()); + if (size > 100) + { + out.resize(size); + CFDataGetBytes(data, CFRangeMake(0, size), out.data()); + } CFRelease(data); } - CFRelease(font); return out; } diff --git a/src/gui/imgui_helpers.cpp b/src/gui/imgui_helpers.cpp index 1fe6123..8f9e7e6 100644 --- a/src/gui/imgui_helpers.cpp +++ b/src/gui/imgui_helpers.cpp @@ -23,7 +23,11 @@ GLFWwindow * init_glfw() } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + #ifdef __APPLE__ + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); + #else glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + #endif glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); GLFWwindow* w = glfwCreateWindow(1280, 720, "clrsync", nullptr, nullptr); @@ -49,7 +53,11 @@ void init_imgui(GLFWwindow* window, const std::string& ini_path) ImGui::StyleColorsDark(); ImGui_ImplGlfw_InitForOpenGL(window, true); - ImGui_ImplOpenGL3_Init("#version 130"); + #ifdef __APPLE__ + ImGui_ImplOpenGL3_Init("#version 150"); + #else + ImGui_ImplOpenGL3_Init("#version 120"); + #endif } void begin_frame()