Skip to content

Commit

Permalink
refactor(application): remove internal duplicate (#44)
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Gorez <[email protected]>
  • Loading branch information
tony-go authored Nov 20, 2024
1 parent 0425947 commit b0da291
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/application/appkit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ noa_library(
VARIANT appkit
PRIVATE_HEADERS entry.h
FOLDER "Native/Application"
SOURCES app_appkit.mm delegate.mm delegate.h)
SOURCES app_appkit.mm)

noa_library_install(
NAMESPACE sourcemeta
Expand Down
67 changes: 56 additions & 11 deletions src/application/appkit/app_appkit.mm
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@
#include <sourcemeta/native/application.h>

#include "delegate.h"

#import <AppKit/AppKit.h>
#import <Cocoa/Cocoa.h>

#include <exception>
#include <functional>
#include <iostream>

@interface AppDelegate : NSObject <NSApplicationDelegate>
@property std::function<void()> on_ready;
@property std::function<void(std::exception_ptr)> on_error;
@end

@implementation AppDelegate
- (AppDelegate *)initWithCallbacks:(std::function<void()>)on_ready
and:(std::function<void(std::exception_ptr)>)
on_error {
self.on_ready = on_ready;
self.on_error = on_error;

return self;
}

- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender
hasVisibleWindows:(BOOL)visibleWindows {
assert(self.on_ready);
assert(self.on_error);

if (!visibleWindows) {
try {
(self.on_ready)();
} catch (const std::exception &) {
std::exception_ptr error = std::current_exception();
(self.on_error)(error);
}
}

return YES;
}

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
assert(self.on_ready);
assert(self.on_error);

try {
(self.on_ready)();
} catch (const std::exception &) {
std::exception_ptr error = std::current_exception();
(self.on_error)(error);
}
}
@end

namespace {
sourcemeta::native::Application *instance_{nullptr};
}
Expand All @@ -19,9 +63,13 @@
Internal() = default;
~Internal() = default;

auto run_application(std::function<void()> on_start) -> void {
auto run_application(std::function<void()> on_start,
std::function<void()> on_ready,
std::function<void(std::exception_ptr)> on_error)
-> void {
NSApplication *application = [NSApplication sharedApplication];
AppDelegate *delegate = [[AppDelegate alloc] init];
AppDelegate *delegate = [[AppDelegate alloc] initWithCallbacks:on_ready
and:on_error];
[application setDelegate:delegate];
[application run];

Expand All @@ -47,13 +95,10 @@ auto run_application(std::function<void()> on_start) -> void {
assert(!running_);
running_ = true;

internal_->run_application([this]() {
try {
on_start();
} catch (...) {
on_error(std::current_exception());
}
});
internal_->run_application(
std::bind(&Application::on_start, this),
std::bind(&Application::on_ready, this),
std::bind(&Application::on_error, this, std::placeholders::_1));

return EXIT_SUCCESS;
}
Expand Down
19 changes: 0 additions & 19 deletions src/application/appkit/delegate.h

This file was deleted.

42 changes: 0 additions & 42 deletions src/application/appkit/delegate.mm

This file was deleted.

0 comments on commit b0da291

Please sign in to comment.