Skip to content

Commit 850111c

Browse files
committed
Refactor application lifecycle and remove AppRunner
Replaces the AppRunner class with direct Application management, consolidating initialization and event loop handling into the Application singleton. Updates all platform-specific implementations and C/C++ APIs to use Application::Run and Application::Run(window), removes app_runner files, and simplifies example usage. This refactor streamlines the application lifecycle and eliminates redundant abstractions.
1 parent 0859caf commit 850111c

File tree

21 files changed

+335
-622
lines changed

21 files changed

+335
-622
lines changed

examples/application_c_example/main.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ void on_application_event(const native_application_event_t* event) {
1010
printf("Application started event received\n");
1111
break;
1212
case NATIVE_APPLICATION_EVENT_EXITING:
13-
printf("Application exiting event received with exit code: %d\n", event->exit_code);
13+
printf("Application exiting event received with exit code: %d\n",
14+
event->exit_code);
1415
break;
1516
case NATIVE_APPLICATION_EVENT_ACTIVATED:
1617
printf("Application activated event received\n");
@@ -44,10 +45,12 @@ int main() {
4445
}
4546

4647
printf("Application initialized successfully\n");
47-
printf("Single instance: %s\n", native_application_is_single_instance(app) ? "Yes" : "No");
48+
printf("Single instance: %s\n",
49+
native_application_is_single_instance(app) ? "Yes" : "No");
4850

4951
// Add event listener
50-
size_t listener_id = native_application_add_event_listener(app, on_application_event);
52+
size_t listener_id =
53+
native_application_add_event_listener(app, on_application_event);
5154
if (listener_id == 0) {
5255
fprintf(stderr, "Failed to add event listener\n");
5356
return 1;
@@ -60,7 +63,8 @@ int main() {
6063
return 1;
6164
}
6265

63-
native_window_options_set_title(window_options, "Application C Example Window");
66+
native_window_options_set_title(window_options,
67+
"Application C Example Window");
6468
native_window_options_set_size(window_options, 400.0, 300.0);
6569

6670
native_window_t window = native_window_manager_create(window_options);

examples/application_example/main.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@ int main() {
1111
// Get the Application singleton
1212
auto& app = Application::GetInstance();
1313

14-
// Initialize the application
15-
if (!app.Initialize()) {
16-
std::cerr << "Failed to initialize application" << std::endl;
17-
return 1;
18-
}
19-
20-
std::cout << "Application initialized successfully" << std::endl;
21-
std::cout << "Single instance: " << (app.IsSingleInstance() ? "Yes" : "No") << std::endl;
14+
std::cout << "Application initialized automatically" << std::endl;
15+
std::cout << "Single instance: " << (app.IsSingleInstance() ? "Yes" : "No")
16+
<< std::endl;
2217

2318
// Add event listeners
2419
auto started_listener = app.AddListener<ApplicationStartedEvent>(
@@ -41,10 +36,9 @@ int main() {
4136
std::cout << "Application deactivated event received" << std::endl;
4237
});
4338

44-
4539
// Create a simple window
4640
auto& window_manager = WindowManager::GetInstance();
47-
41+
4842
WindowOptions window_options;
4943
window_options.title = "Application Example Window";
5044

examples/menu_c_example/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <string.h>
4-
#include "../../src/capi/app_runner_c.h"
4+
#include "../../src/capi/application_c.h"
55
#include "../../src/capi/menu_c.h"
66

77
// Event callback functions

examples/window_example/main.cpp

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

4-
using nativeapi::AppRunner;
4+
using nativeapi::Application;
55
using nativeapi::Display;
66
using nativeapi::DisplayAddedEvent;
77
using nativeapi::DisplayManager;
@@ -371,7 +371,8 @@ int main() {
371371
<< std::endl;
372372
});
373373

374-
RunApp(window_ptr);
374+
auto& app = Application::GetInstance();
375+
app.Run(window_ptr);
375376

376377
return 0;
377378
}

include/nativeapi.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "../src/accessibility_manager.h"
66
#include "../src/application.h"
77
#include "../src/application_event.h"
8-
#include "../src/app_runner.h"
98
#include "../src/display.h"
109
#include "../src/display_event.h"
1110
#include "../src/display_manager.h"
@@ -28,7 +27,6 @@
2827
// C API headers (available for both C and C++)
2928
#include "../src/capi/accessibility_manager_c.h"
3029
#include "../src/capi/application_c.h"
31-
#include "../src/capi/app_runner_c.h"
3230
#include "../src/capi/display_c.h"
3331
#include "../src/capi/display_manager_c.h"
3432
#include "../src/capi/geometry_c.h"

src/app_runner.cpp

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

src/app_runner.h

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

src/application.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ Application& Application::GetInstance() {
77
return instance;
88
}
99

10+
int RunApp(std::shared_ptr<Window> window) {
11+
return Application::GetInstance().Run(window);
12+
}
13+
1014
} // namespace nativeapi

src/application.h

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace nativeapi {
1515

16-
1716
/**
1817
* @brief Application is a singleton class that manages the application lifecycle
1918
*
@@ -50,7 +49,7 @@ class Application : public EventEmitter<ApplicationEvent> {
5049
* @code
5150
* // Usage example:
5251
* auto& app = Application::GetInstance();
53-
* app.Initialize(options);
52+
* int exit_code = app.Run();
5453
* @endcode
5554
*/
5655
static Application& GetInstance();
@@ -64,40 +63,38 @@ class Application : public EventEmitter<ApplicationEvent> {
6463
virtual ~Application();
6564

6665
/**
67-
* @brief Initialize the application
68-
*
69-
* Sets up the application with default configuration. This method
70-
* should be called before any other application operations. It sets up
71-
* platform-specific initialization and emits an ApplicationStartedEvent.
66+
* @brief Run the application main event loop
7267
*
73-
* @return true if initialization succeeded, false otherwise
68+
* Starts the main event loop and blocks until the application exits.
69+
* This method handles platform-specific event processing and coordination
70+
* between different managers.
7471
*
75-
* @throws std::runtime_error if initialization fails due to system limitations
72+
* @return Exit code of the application (0 for success)
7673
*
7774
* @code
7875
* auto& app = Application::GetInstance();
79-
* if (app.Initialize()) {
80-
* // Application initialized successfully
81-
* }
76+
* int exit_code = app.Run();
8277
* @endcode
8378
*/
84-
bool Initialize();
79+
int Run();
8580

8681
/**
87-
* @brief Run the application main event loop
82+
* @brief Run the application with the specified window
8883
*
89-
* Starts the main event loop and blocks until the application exits.
90-
* This method handles platform-specific event processing and coordination
91-
* between different managers.
84+
* Starts the main event loop with the given window and blocks until the
85+
* application exits. This method sets the window as the primary window
86+
* and starts the event loop.
9287
*
88+
* @param window The window to run the application with
9389
* @return Exit code of the application (0 for success)
9490
*
9591
* @code
9692
* auto& app = Application::GetInstance();
97-
* int exit_code = app.Run();
93+
* auto window = WindowManager::GetInstance().Create(options);
94+
* int exit_code = app.Run(window);
9895
* @endcode
9996
*/
100-
int Run();
97+
int Run(std::shared_ptr<Window> window);
10198

10299
/**
103100
* @brief Request the application to quit
@@ -121,7 +118,6 @@ class Application : public EventEmitter<ApplicationEvent> {
121118
*/
122119
bool IsRunning() const;
123120

124-
125121
/**
126122
* @brief Check if this is a single instance application
127123
*
@@ -191,12 +187,11 @@ class Application : public EventEmitter<ApplicationEvent> {
191187
*/
192188
std::vector<std::shared_ptr<Window>> GetAllWindows() const;
193189

194-
195190
private:
196191
/**
197192
* @brief Private constructor to enforce singleton pattern
198193
*
199-
* Initializes the Application instance and sets up platform-specific
194+
* Automatically initializes the Application instance and sets up platform-specific
200195
* event monitoring. This constructor is private to prevent direct instantiation.
201196
*/
202197
Application();
@@ -216,7 +211,6 @@ class Application : public EventEmitter<ApplicationEvent> {
216211
class Impl;
217212
std::unique_ptr<Impl> pimpl_;
218213

219-
220214
/**
221215
* @brief Application state
222216
*/
@@ -229,15 +223,24 @@ class Application : public EventEmitter<ApplicationEvent> {
229223
*/
230224
std::shared_ptr<Window> primary_window_;
231225

232-
/**
233-
* @brief Clean up platform-specific event monitoring
234-
*
235-
* Stops event monitoring and cleans up associated resources.
236-
* This is called during destruction.
237-
*/
238-
void CleanupEventMonitoring();
239-
240226
private:
241227
};
242228

229+
/**
230+
* @brief Convenience function to run the application with the specified window
231+
*
232+
* This is equivalent to calling Application::GetInstance().Run(window).
233+
* This function provides a simple way to run an application without
234+
* explicitly accessing the singleton.
235+
*
236+
* @param window The window to run the application with
237+
* @return Exit code of the application (0 for success)
238+
*
239+
* @code
240+
* auto window = WindowManager::GetInstance().Create(options);
241+
* int exit_code = RunApp(window);
242+
* @endcode
243+
*/
244+
int RunApp(std::shared_ptr<Window> window);
245+
243246
} // namespace nativeapi

src/application_event.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ApplicationStartedEvent : public ApplicationEvent {
3131
class ApplicationExitingEvent : public ApplicationEvent {
3232
public:
3333
ApplicationExitingEvent(int exit_code) : exit_code_(exit_code) {}
34-
34+
3535
int GetExitCode() const { return exit_code_; }
3636
std::string GetTypeName() const override { return "ApplicationExitingEvent"; }
3737

@@ -66,5 +66,4 @@ class ApplicationQuitRequestedEvent : public ApplicationEvent {
6666
std::string GetTypeName() const override { return "ApplicationQuitRequestedEvent"; }
6767
};
6868

69-
7069
} // namespace nativeapi

0 commit comments

Comments
 (0)