diff options
author | crupest <crupest@outlook.com> | 2022-02-06 23:19:52 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-02-06 23:19:52 +0800 |
commit | d75f107d845c249a0f832093609614afb2fd0967 (patch) | |
tree | 1bbcbe2163a9d2764579671c55db5990bcad5669 /src | |
parent | 57edb13bbaa4a739c2a1426d206ec17fda244f34 (diff) | |
download | cru-d75f107d845c249a0f832093609614afb2fd0967.tar.gz cru-d75f107d845c249a0f832093609614afb2fd0967.tar.bz2 cru-d75f107d845c249a0f832093609614afb2fd0967.zip |
...
Diffstat (limited to 'src')
-rw-r--r-- | src/osx/gui/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/osx/gui/UiApplication.mm | 60 |
2 files changed, 62 insertions, 1 deletions
diff --git a/src/osx/gui/CMakeLists.txt b/src/osx/gui/CMakeLists.txt index 5446c9df..5da507e1 100644 --- a/src/osx/gui/CMakeLists.txt +++ b/src/osx/gui/CMakeLists.txt @@ -10,5 +10,6 @@ add_library(cru_osx_gui SHARED ) find_library(APPKIT AppKit REQUIRED) +find_library(UNIFORMTYPEIDENTIFIERS UniformTypeIdentifiers REQUIRED) -target_link_libraries(cru_osx_gui PUBLIC cru_platform_gui cru_osx_graphics_quartz ${APPKIT}) +target_link_libraries(cru_osx_gui PUBLIC cru_platform_gui cru_osx_graphics_quartz ${APPKIT} ${UNIFORMTYPEIDENTIFIERS}) diff --git a/src/osx/gui/UiApplication.mm b/src/osx/gui/UiApplication.mm index b923742e..b80df598 100644 --- a/src/osx/gui/UiApplication.mm +++ b/src/osx/gui/UiApplication.mm @@ -2,6 +2,7 @@ #include "ClipboardPrivate.h" #include "cru/common/Logger.hpp" +#include "cru/common/platform/osx/Convert.hpp" #include "cru/osx/graphics/quartz/Factory.hpp" #include "cru/osx/gui/Clipboard.hpp" #include "cru/osx/gui/Cursor.hpp" @@ -14,6 +15,7 @@ #include <AppKit/NSApplication.h> #include <Foundation/NSRunLoop.h> +#include <UniformTypeIdentifiers/UTType.h> #include <algorithm> #include <iterator> @@ -29,6 +31,8 @@ namespace cru::platform::gui::osx { +using cru::platform::osx::Convert; + namespace details { class OsxUiApplicationPrivate { friend OsxUiApplication; @@ -179,6 +183,62 @@ graphics::IGraphicsFactory* OsxUiApplication::GetGraphicsFactory() { return p_->quartz_graphics_factory_.get(); } +std::optional<String> OsxUiApplication::ShowSaveDialog(SaveDialogOptions options) { + NSSavePanel* panel = [NSSavePanel savePanel]; + [panel setTitle:(NSString*)Convert(options.title)]; + [panel setPrompt:(NSString*)Convert(options.prompt)]; + [panel setMessage:(NSString*)Convert(options.message)]; + + NSMutableArray* allowed_content_types = [NSMutableArray array]; + + for (const auto& file_type : options.allowed_file_types) { + [allowed_content_types + addObject:[UTType typeWithFilenameExtension:(NSString*)Convert(file_type)]]; + } + + [panel setAllowedContentTypes:allowed_content_types]; + [panel setAllowsOtherFileTypes:options.allow_all_file_types]; + + auto model_result = [panel runModal]; + if (model_result == NSModalResponseOK) { + return Convert((CFStringRef)[[panel URL] path]); + } else { + return std::nullopt; + } +} + +std::optional<std::vector<String>> OsxUiApplication::ShowOpenDialog(OpenDialogOptions options) { + NSOpenPanel* panel = [NSOpenPanel openPanel]; + [panel setTitle:(NSString*)Convert(options.title)]; + [panel setPrompt:(NSString*)Convert(options.prompt)]; + [panel setMessage:(NSString*)Convert(options.message)]; + + NSMutableArray* allowed_content_types = [NSMutableArray array]; + + for (const auto& file_type : options.allowed_file_types) { + [allowed_content_types + addObject:[UTType typeWithFilenameExtension:(NSString*)Convert(file_type)]]; + } + + [panel setAllowedContentTypes:allowed_content_types]; + [panel setAllowsOtherFileTypes:options.allow_all_file_types]; + + [panel setCanChooseFiles:options.can_choose_files]; + [panel setCanChooseDirectories:options.can_choose_directories]; + [panel setAllowsMultipleSelection:options.allow_mulitple_selection]; + + auto model_result = [panel runModal]; + if (model_result == NSModalResponseOK) { + std::vector<String> result; + for (NSURL* url in [panel URLs]) { + result.push_back(Convert((CFStringRef)[url path])); + } + return result; + } else { + return std::nullopt; + } +} + void OsxUiApplication::UnregisterWindow(OsxWindow* window) { p_->windows_.erase( std::remove(p_->windows_.begin(), p_->windows_.end(), static_cast<INativeWindow*>(window)), |