diff options
author | crupest <crupest@outlook.com> | 2022-01-25 18:39:14 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-01-25 18:39:14 +0800 |
commit | d3aa43d9ea0dfc32935767cf60a89af2736dc339 (patch) | |
tree | e8cf043ac6d9a2e6ce038ad6b1f57079834ef129 | |
parent | 71c01a175a939d1a519ab235fdfdeec1101f8b84 (diff) | |
download | cru-d3aa43d9ea0dfc32935767cf60a89af2736dc339.tar.gz cru-d3aa43d9ea0dfc32935767cf60a89af2736dc339.tar.bz2 cru-d3aa43d9ea0dfc32935767cf60a89af2736dc339.zip |
...
-rw-r--r-- | CMakeLists.txt | 11 | ||||
-rw-r--r-- | demos/main/CMakeLists.txt | 1 | ||||
-rw-r--r-- | demos/scroll_view/CMakeLists.txt | 1 | ||||
-rw-r--r-- | include/cru/common/String.hpp | 3 | ||||
-rw-r--r-- | include/cru/common/io/Resource.hpp | 6 | ||||
-rw-r--r-- | src/common/String.cpp | 4 | ||||
-rw-r--r-- | src/common/io/Resource.cpp | 15 | ||||
-rw-r--r-- | src/ui/CMakeLists.txt | 7 | ||||
-rw-r--r-- | src/ui/UiManager.cpp | 12 |
9 files changed, 43 insertions, 17 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 055a8302..4040df29 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,17 @@ endif() set(CRU_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include) set(CRU_ASSETS_DIR ${PROJECT_SOURCE_DIR}/assets) +function(target_add_resources target res_dir) + message("Add resources for target ${target} with files ${res_dir}.") + + file(GLOB_RECURSE RES_SOURCES "${CRU_ASSETS_DIR}/${res_dir}/*") + target_sources(${target} PUBLIC ${RES_SOURCES}) + foreach (RES_FILE ${RES_SOURCES}) + file(RELATIVE_PATH RES_PATH ${CRU_ASSETS_DIR} ${RES_FILE}) + set_property(SOURCE ${RES_FILE} PROPERTY MACOSX_PACKAGE_LOCATION "Resources/${RES_PATH}/..") + endforeach(RES_FILE) +endfunction() + add_subdirectory(src) add_subdirectory(test) add_subdirectory(demos) diff --git a/demos/main/CMakeLists.txt b/demos/main/CMakeLists.txt index 6f995ee0..37801dad 100644 --- a/demos/main/CMakeLists.txt +++ b/demos/main/CMakeLists.txt @@ -8,4 +8,5 @@ if(APPLE) ) endif() +target_add_resources(demo_main cru/ui) target_link_libraries(demo_main PRIVATE cru_demo_base cru_ui) diff --git a/demos/scroll_view/CMakeLists.txt b/demos/scroll_view/CMakeLists.txt index 2c6be535..1871dd9f 100644 --- a/demos/scroll_view/CMakeLists.txt +++ b/demos/scroll_view/CMakeLists.txt @@ -8,4 +8,5 @@ if(APPLE) ) endif() +target_add_resources(demo_scroll_view cru/ui) target_link_libraries(demo_scroll_view PRIVATE cru_demo_base cru_ui) diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index 22d68980..c1f957c0 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -5,6 +5,7 @@ #include "StringUtil.hpp" #include <double-conversion/double-conversion.h> +#include <filesystem> #include <initializer_list> #include <iterator> #include <string> @@ -50,6 +51,8 @@ class CRU_BASE_API String { return String{from_buffer_tag{}, buffer, size, capacity}; } + static String FromStdPath(const std::filesystem::path& path); + #ifdef CRU_PLATFORM_WINDOWS static String FromUtf16(wchar_t* str) { return String(str); } static String FromUtf16(wchar_t* str, Index size) { diff --git a/include/cru/common/io/Resource.hpp b/include/cru/common/io/Resource.hpp index d1343d8e..4fd00bdd 100644 --- a/include/cru/common/io/Resource.hpp +++ b/include/cru/common/io/Resource.hpp @@ -1,10 +1,8 @@ #pragma once - #include "../Base.hpp" -#include "Stream.hpp" -#include <memory> +#include <filesystem> namespace cru::io { -std::unique_ptr<Stream> CreateStreamFromResourcePath(const String& path); +std::filesystem::path CRU_BASE_API GetResourceDir(); } diff --git a/src/common/String.cpp b/src/common/String.cpp index 62943059..e791d119 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -33,6 +33,10 @@ String String::FromUtf8(const char* str, Index size) { return result; } +String String::FromStdPath(const std::filesystem::path& path) { + return String::FromUtf8(path.string()); +} + char16_t String::kEmptyBuffer[1] = {0}; String::String(const_pointer str) : String(str, GetStrSize(str)) {} diff --git a/src/common/io/Resource.cpp b/src/common/io/Resource.cpp index e2ff8004..da16b578 100644 --- a/src/common/io/Resource.cpp +++ b/src/common/io/Resource.cpp @@ -7,10 +7,19 @@ #endif namespace cru::io { -std::unique_ptr<Stream> CreateStreamFromResourcePath(const String& path) { +std::filesystem::path GetResourceDir() { #if defined(CRU_PLATFORM_OSX) - // CFBundleRef main_bundle = CFBundleGetMainBundle(); - throw Exception(u"Not implemented."); + CFBundleRef main_bundle = CFBundleGetMainBundle(); + CFURLRef bundle_url = CFBundleCopyBundleURL(main_bundle); + CFStringRef cf_string_ref = + CFURLCopyFileSystemPath(bundle_url, kCFURLPOSIXPathStyle); + std::filesystem::path bundle_path( + CFStringGetCStringPtr(cf_string_ref, kCFStringEncodingUTF8)); + + CFRelease(bundle_url); + CFRelease(cf_string_ref); + + return bundle_path / "Contents/Resources"; #elif defined(CRU_PLATFORM_WINDOWS) throw Exception(u"Not implemented."); #else diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index 0c81b6c7..a86e24c0 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -59,10 +59,3 @@ add_library(cru_ui SHARED ) target_compile_definitions(cru_ui PRIVATE CRU_UI_EXPORT_API) target_link_libraries(cru_ui PUBLIC cru_platform_gui cru_xml) - -file(GLOB_RECURSE CRU_UI_RES_SOURCES "${CRU_ASSETS_DIR}/cru/ui/*") -target_sources(cru_ui PUBLIC ${CRU_UI_RES_SOURCES}) -foreach (CRU_UI_RES_FILE ${CRU_UI_RES_SOURCES}) - file(RELATIVE_PATH CRU_UI_RES_PATH ${CRU_ASSETS_DIR} ${CRU_UI_RES_FILE}) - set_property(SOURCE ${CRU_UI_RES_FILE} PROPERTY MACOSX_PACKAGE_LOCATION "Resources/${CRU_UI_RES_PATH}") -endforeach(CRU_UI_RES_FILE) diff --git a/src/ui/UiManager.cpp b/src/ui/UiManager.cpp index e374cc2e..62dce6a7 100644 --- a/src/ui/UiManager.cpp +++ b/src/ui/UiManager.cpp @@ -3,6 +3,7 @@ #include "Helper.hpp" #include "cru/common/io/FileStream.hpp" #include "cru/common/io/OpenFileFlag.hpp" +#include "cru/common/io/Resource.hpp" #include "cru/platform/graphics/Brush.hpp" #include "cru/platform/graphics/Factory.hpp" #include "cru/platform/graphics/Font.hpp" @@ -37,9 +38,14 @@ UiManager* UiManager::GetInstance() { UiManager::UiManager() { const auto factory = GetGraphicsFactory(); - // TODO: Resource file path!!! - ReadResourcesFile( - u"/Users/crupest/codes/cru/assets/cru/ui/DefaultResources.xml"); + std::filesystem::path resourses_file = + cru::io::GetResourceDir() / "cru/ui/DefaultResources.xml"; + + if (!std::filesystem::exists(resourses_file)) { + throw Exception(u"Default resources file not found."); + } + + ReadResourcesFile(String::FromStdPath(resourses_file)); theme_resource_.default_font_family = u""; |