aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-09-28 17:49:09 +0800
committercrupest <crupest@outlook.com>2021-09-28 17:49:09 +0800
commitf626ebce8fcbe8f06bd7d459efbc0c21ba6477c7 (patch)
tree74dfd06b70dc650c5fc756f6efb693cd0a2058cd /src
parent9a42f6fd709feaa9e9e3cc4530c67e2a237d5bee (diff)
downloadcru-f626ebce8fcbe8f06bd7d459efbc0c21ba6477c7.tar.gz
cru-f626ebce8fcbe8f06bd7d459efbc0c21ba6477c7.tar.bz2
cru-f626ebce8fcbe8f06bd7d459efbc0c21ba6477c7.zip
...
Diffstat (limited to 'src')
-rw-r--r--src/osx/gui/CMakeLists.txt4
-rw-r--r--src/osx/gui/UiApplication.mm76
-rw-r--r--src/osx/gui/Window.cpp0
-rw-r--r--src/osx/gui/Window.mm (renamed from src/osx/gui/UiApplication.cpp)0
4 files changed, 78 insertions, 2 deletions
diff --git a/src/osx/gui/CMakeLists.txt b/src/osx/gui/CMakeLists.txt
index 26191655..a9a75d76 100644
--- a/src/osx/gui/CMakeLists.txt
+++ b/src/osx/gui/CMakeLists.txt
@@ -2,8 +2,8 @@ set(CRU_OSX_GUI_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/osx/gui)
add_library(cru_osx_gui SHARED
Resource.cpp
- UiApplication.cpp
- Window.cpp
+ UiApplication.mm
+ Window.mm
)
target_sources(cru_osx_gui PUBLIC
diff --git a/src/osx/gui/UiApplication.mm b/src/osx/gui/UiApplication.mm
new file mode 100644
index 00000000..69f52c1f
--- /dev/null
+++ b/src/osx/gui/UiApplication.mm
@@ -0,0 +1,76 @@
+#include "cru/osx/gui/UiApplication.hpp"
+
+#include <AppKit/NSApplication.h>
+#include <algorithm>
+
+@interface AppDelegate : NSObject <NSApplicationDelegate>
+- (id)init:(cru::platform::gui::osx::details::OsxUiApplicationPrivate*)p;
+- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender;
+- (void)applicationWillTerminate:(NSNotification*)notification;
+@end
+
+namespace cru::platform::gui::osx {
+
+namespace details {
+class OsxUiApplicationPrivate {
+ friend OsxUiApplication;
+
+ public:
+ explicit OsxUiApplicationPrivate(OsxUiApplication* osx_ui_application)
+ : osx_ui_application_(osx_ui_application) {
+ app_delegate_ = [[AppDelegate alloc] init:this];
+ }
+
+ CRU_DELETE_COPY(OsxUiApplicationPrivate)
+ CRU_DELETE_MOVE(OsxUiApplicationPrivate)
+
+ ~OsxUiApplicationPrivate() = default;
+
+ void CallQuitHandlers();
+
+ private:
+ OsxUiApplication* osx_ui_application_;
+ AppDelegate* app_delegate_;
+ std::vector<std::function<void()>> quit_handlers_;
+};
+
+void OsxUiApplicationPrivate::CallQuitHandlers() {
+ for (const auto& handler : quit_handlers_) {
+ handler();
+ }
+}
+}
+
+OsxUiApplication::OsxUiApplication() : OsxGuiResource(this) {}
+
+OsxUiApplication::~OsxUiApplication() {}
+
+int OsxUiApplication::Run() {
+ [NSApp run];
+ return 0;
+}
+
+void OsxUiApplication::RequestQuit(int quit_code) {
+ [NSApp terminate:[NSNumber numberWithInteger:quit_code]];
+}
+
+void OsxUiApplication::AddOnQuitHandler(std::function<void()> handler) {
+ p_->quit_handlers_.push_back(std::move(handler));
+}
+
+}
+
+@implementation AppDelegate
+cru::platform::gui::osx::details::OsxUiApplicationPrivate* _p;
+
+- (id)init:(cru::platform::gui::osx::details::OsxUiApplicationPrivate*)p {
+ _p = p;
+ return self;
+}
+- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender {
+ return NSApplicationTerminateReply::NSTerminateNow;
+}
+- (void)applicationWillTerminate:(NSNotification*)notification {
+ _p->CallQuitHandlers();
+}
+@end
diff --git a/src/osx/gui/Window.cpp b/src/osx/gui/Window.cpp
deleted file mode 100644
index e69de29b..00000000
--- a/src/osx/gui/Window.cpp
+++ /dev/null
diff --git a/src/osx/gui/UiApplication.cpp b/src/osx/gui/Window.mm
index e69de29b..e69de29b 100644
--- a/src/osx/gui/UiApplication.cpp
+++ b/src/osx/gui/Window.mm