Skip to content

Commit ccfbdae

Browse files
committed
Remove nswindow_example and add AppRunner for app lifecycle
1 parent 913e1ed commit ccfbdae

File tree

13 files changed

+206
-178
lines changed

13 files changed

+206
-178
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ add_subdirectory(src)
77

88
# Add example programs subdirectory
99
add_subdirectory(examples/display_example)
10-
add_subdirectory(examples/nswindow_example)
1110
add_subdirectory(examples/window_example)

examples/nswindow_example/CMakeLists.txt

Lines changed: 0 additions & 33 deletions
This file was deleted.

examples/nswindow_example/main.mm

Lines changed: 0 additions & 119 deletions
This file was deleted.

examples/window_example/main.cpp

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,45 @@
11
#include <iostream>
22
#include "nativeapi.h"
33

4+
using nativeapi::AppRunner;
5+
using nativeapi::Tray;
6+
using nativeapi::TrayManager;
47
using nativeapi::Window;
58
using nativeapi::WindowManager;
9+
using nativeapi::WindowOptions;
610

711
int main() {
8-
WindowManager windowManager = WindowManager();
12+
WindowManager window_manager = WindowManager();
913

10-
// Get primary display information
11-
std::shared_ptr<Window> currentWindowPtr = windowManager.GetCurrent();
12-
if (currentWindowPtr != nullptr) {
13-
Window& currentWindow = *currentWindowPtr;
14-
std::cout << "Current Window Information:" << std::endl;
15-
std::cout << "ID: " << currentWindow.id << std::endl;
14+
// Create a new window with options
15+
WindowOptions options;
16+
options.title = "My Window";
17+
options.size.width = 800;
18+
options.size.height = 600;
19+
std::shared_ptr<Window> window_ptr = window_manager.Create(options);
20+
if (window_ptr != nullptr) {
21+
Window& window = *window_ptr;
22+
std::cout << "New Window Information:" << std::endl;
23+
std::cout << "ID: " << window.id << std::endl;
1624
std::cout << std::endl;
25+
window.Show();
26+
window.Focus();
1727
}
1828

19-
// Get all windows
20-
std::vector<std::shared_ptr<Window>> windowList = (windowManager.GetAll());
21-
std::cout << "\nAll Windows Information:" << std::endl;
22-
for (size_t i = 0; i < windowList.size(); i++) {
23-
const Window& window = *windowList[i];
24-
std::cout << "Window " << (i + 1) << ":" << std::endl;
25-
std::cout << "ID: " << window.id << std::endl;
26-
auto windowSize = window.GetSize();
27-
std::cout << "Size: " << windowSize.width << "x" << windowSize.height
28-
<< std::endl;
29+
TrayManager trayManager = TrayManager();
30+
31+
std::shared_ptr<Tray> newTrayPtr = trayManager.Create();
32+
if (newTrayPtr != nullptr) {
33+
Tray& newTray = *newTrayPtr;
34+
newTray.SetTitle("Hello, World!");
35+
std::cout << "Tray ID: " << newTray.id << std::endl;
36+
std::cout << "Tray Title: " << newTray.GetTitle() << std::endl;
37+
} else {
38+
std::cerr << "Failed to create tray." << std::endl;
2939
}
40+
41+
AppRunner runner;
42+
runner.Run(window_ptr);
43+
3044
return 0;
31-
}
45+
}

include/nativeapi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "../src/accessibility_manager.h"
4+
#include "../src/app_runner.h"
45
#include "../src/display.h"
56
#include "../src/display_manager.h"
67
#include "../src/geometry.h"

src/app_runner.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "app_runner.h"
2+
#include <iostream>
3+
4+
namespace nativeapi {
5+
6+
7+
} // namespace nativeapi

src/app_runner.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include <functional>
4+
#include <memory>
5+
6+
#include "window.h"
7+
8+
namespace nativeapi {
9+
10+
/**
11+
* AppRunner is a class that manages the application lifecycle and runs the main
12+
* event loop. It provides a way to run an application with a given window and
13+
* handle application events.
14+
*/
15+
class AppRunner {
16+
public:
17+
AppRunner();
18+
virtual ~AppRunner();
19+
20+
/**
21+
* Runs the application with the specified window.
22+
* This method starts the main event loop and blocks until the application
23+
* exits.
24+
*
25+
* @param window The window to run the application with
26+
* @return Exit code of the application (0 for success)
27+
*/
28+
int Run(std::shared_ptr<Window> window);
29+
30+
private:
31+
class Impl;
32+
std::unique_ptr<Impl> pimpl_;
33+
};
34+
35+
} // namespace nativeapi

src/display_manager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "display_manager.h"
2+
#include <algorithm>
23

34
namespace nativeapi {
45

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#import <Cocoa/Cocoa.h>
2+
#include <iostream>
3+
4+
#include "../../app_runner.h"
5+
#include "../../window.h"
6+
7+
@interface AppRunnerDelegate : NSObject <NSApplicationDelegate>
8+
@end
9+
10+
@implementation AppRunnerDelegate
11+
12+
- (void)applicationDidFinishLaunching:(NSNotification*)notification {
13+
NSLog(@"Application finished launching");
14+
}
15+
16+
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender {
17+
return YES;
18+
}
19+
20+
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender {
21+
return NSTerminateNow;
22+
}
23+
24+
@end
25+
26+
namespace nativeapi {
27+
28+
// Private implementation class
29+
class AppRunner::Impl {
30+
public:
31+
AppRunnerDelegate* delegate;
32+
33+
Impl() { delegate = [[AppRunnerDelegate alloc] init]; }
34+
35+
~Impl() { delegate = nil; }
36+
};
37+
38+
AppRunner::AppRunner() : pimpl_(new Impl()) {}
39+
40+
AppRunner::~AppRunner() {}
41+
42+
int AppRunner::Run(std::shared_ptr<Window> window) {
43+
std::cout << "Starting macOS application..." << std::endl;
44+
45+
// Initialize NSApplication if not already done
46+
NSApplication* app = [NSApplication sharedApplication];
47+
48+
// Set up the application delegate
49+
[app setDelegate:pimpl_->delegate];
50+
51+
// If a window is provided, show it and make it the main window
52+
if (window) {
53+
// Access the NSWindow from the Window object
54+
// We need to get the native window handle
55+
window->Show();
56+
window->Focus();
57+
58+
// Get the NSWindow from the Window object
59+
// This assumes the Window class has access to the native NSWindow
60+
std::cout << "Window shown and focused" << std::endl;
61+
}
62+
63+
// Activate the application
64+
[app activateIgnoringOtherApps:YES];
65+
66+
// Set activation policy to regular app (appears in dock)
67+
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
68+
69+
// Finish launching if not already done
70+
[app finishLaunching];
71+
72+
std::cout << "Starting main run loop..." << std::endl;
73+
74+
// Start the main event loop
75+
[app run];
76+
77+
std::cout << "Application event loop ended" << std::endl;
78+
79+
return 0;
80+
}
81+
82+
} // namespace nativeapi

0 commit comments

Comments
 (0)