AppDelegate.cc
1 #include "AppDelegate.hh" 2 3 AppDelegate::AppDelegate() 4 : window_title(nsStringUtf8("App")) { }; 5 6 AppDelegate::AppDelegate(const char* title) 7 : window_title(nsStringUtf8(title)) { }; 8 9 AppDelegate::~AppDelegate() 10 { 11 this->mtk_view->release(); 12 this->window->release(); 13 this->device->release(); 14 } 15 16 void AppDelegate::applicationWillFinishLaunching( 17 NS::Notification* notification) 18 { 19 NS::Application* app = static_cast<NS::Application*>(notification->object()); 20 app->setActivationPolicy(NS::ActivationPolicy::ActivationPolicyRegular); 21 } 22 23 void AppDelegate::applicationDidFinishLaunching( 24 NS::Notification* notification) 25 { 26 CGRect frame = { 27 .origin = { .x = 100.0, .y = 100.0 }, 28 .size = { .width = 900.0, .height = 1024.0 }, 29 }; 30 31 this->window = NS::Window::alloc()->init( 32 frame, NS::WindowStyleMaskClosable | NS::WindowStyleMaskTitled, 33 NS::BackingStoreBuffered, false); 34 35 this->device = MTL::CreateSystemDefaultDevice(); 36 37 this->mtk_view = MTK::View::alloc()->init(frame, this->device); 38 this->mtk_view->setColorPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm_sRGB); 39 this->mtk_view->setClearColor(MTL::ClearColor::Make(0.1, 0.1, 0.1, 1.0)); 40 this->mtk_view->setDepthStencilPixelFormat(MTL::PixelFormat::PixelFormatDepth16Unorm); 41 this->mtk_view->setClearDepth(1.0f); 42 43 this->mtk_view->setSampleCount(4); 44 45 auto view_delegate = MTKViewDelegate::init(device); 46 47 if (!view_delegate) { 48 std::cerr << std::format("Failed to create MTKViewDelegate: {}", view_delegate.error().message()); 49 return; 50 } 51 52 this->view_delegate = std::move(view_delegate.value()); 53 54 this->mtk_view->setDelegate(this->view_delegate.get()); 55 56 this->window->setContentView(this->mtk_view); 57 this->window->setTitle(this->window_title); 58 59 // Key, as in, show the window. 60 this->window->makeKeyAndOrderFront(nullptr); 61 62 NS::Application* app = static_cast<NS::Application*>(notification->object()); 63 64 app->activateIgnoringOtherApps(true); 65 } 66 67 bool AppDelegate::applicationShouldTerminateAfterLastWindowClosed( 68 NS::Application* _ /*pSender*/) 69 { 70 return true; 71 }