Skip to content

Commit 7ac846c

Browse files
committed
Add window management and AppRunner API
- Implement Window, WindowOptions, and WindowList types - Add WindowManager with event handling and window lifecycle methods - Introduce AppRunner for running applications with windows - Update example to demonstrate simple AppRunner usage - Update libnativeapi submodule to latest commit
1 parent a9a6a6f commit 7ac846c

File tree

5 files changed

+639
-22
lines changed

5 files changed

+639
-22
lines changed

Sources/Example/main.swift

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,49 @@
11
import Foundation
22
import NativeAPI
33

4-
// MARK: - Main Program
4+
// MARK: - Simple AppRunner Example
55

6-
print("=== NativeAPI Swift Example ===")
6+
print("=== NativeAPI AppRunner Simple Example ===")
7+
print("🚀 This demonstrates the simplest way to use AppRunner")
78
print()
89

9-
// Test basic Display functionality
10-
print("🚀 Testing NativeAPI Display...")
11-
12-
AccessibilityManager.shared.enable()
13-
14-
let isAccessibilityEnabled = AccessibilityManager.shared.isEnabled()
10+
// Method 1: Using the convenience function with default window
11+
print("📱 Method 1: Running app with default window")
12+
print("💡 This creates a default window and runs the application")
13+
print()
1514

16-
print("✅ Accessibility is enabled:", isAccessibilityEnabled)
15+
// Uncomment to try the simplest approach:
16+
// let exitCode = runApp()
17+
// print("✅ App exited with code: \(exitCode)")
1718

18-
let display = Display()
19-
print("✅ Display instance created successfully!")
19+
// Method 2: Using custom window options
20+
print("📱 Method 2: Running app with custom window options")
21+
print("💡 This creates a window with specific options")
2022
print()
2123

22-
print("📱 NativeAPI is working correctly!")
23-
print("💡 This example demonstrates that the NativeAPI library can be imported and used.")
24+
// Create custom window options
25+
let options = WindowOptions()
26+
_ = options.setTitle("My Swift App")
27+
options.setSize(Size(width: 1000, height: 700))
28+
options.setMinimumSize(Size(width: 500, height: 350))
29+
options.setCentered(true)
30+
31+
print("⚙️ Window options configured:")
32+
print(" - Title: 'My Swift App'")
33+
print(" - Size: 1000x700")
34+
print(" - Minimum size: 500x350")
35+
print(" - Centered on screen")
2436
print()
2537

26-
print("🎯 Next steps:")
27-
print(" - Implement more functionality in the NativeAPI module")
28-
print(" - Add real display management features")
29-
print(" - Test with actual display operations")
30-
print()
38+
// Run with custom options
39+
let exitCodeWithOptions = runApp(with: options)
40+
print("✅ App exited with code: \(exitCodeWithOptions)")
3141

32-
print("✅ Example completed successfully!")
42+
print()
43+
print("🎉 Simple AppRunner example completed!")
44+
print("📝 This example showed:")
45+
print(" ✅ Simple one-line app launching")
46+
print(" ✅ Custom window configuration")
47+
print(" ✅ Exit code handling")
48+
print()
49+
print("💡 For more advanced features, see AppRunnerExample.swift")

Sources/NativeAPI/AppRunner.swift

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,61 @@
1-
public class AppRunner {
2-
public func run() {
1+
import CNativeAPI
2+
import Foundation
33

4+
/// AppRunner exit codes
5+
public enum AppExitCode: Int32 {
6+
case success = 0
7+
case failure = 1
8+
case invalidWindow = 2
9+
}
10+
11+
/// AppRunner manages the application lifecycle and event loop
12+
public class AppRunner: @unchecked Sendable {
13+
/// Shared singleton instance
14+
public static let shared = AppRunner()
15+
16+
private init() {}
17+
18+
/// Run the application with the specified window
19+
/// This method starts the main event loop and blocks until the application exits
20+
/// - Parameter window: The window to run the application with
21+
/// - Returns: Exit code of the application (0 for success)
22+
public func run(with window: Window) -> AppExitCode {
23+
let exitCode = native_app_runner_run(window.handle)
24+
return AppExitCode(rawValue: exitCode) ?? .failure
25+
}
26+
27+
/// Check if the application is currently running
28+
/// - Returns: true if the app is running, false otherwise
29+
public var isRunning: Bool {
30+
return native_app_runner_is_running()
31+
}
32+
}
33+
34+
/// Convenience function to run the application with the specified window
35+
/// This is equivalent to calling AppRunner.shared.run(with: window)
36+
/// - Parameter window: The window to run the application with
37+
/// - Returns: Exit code of the application (0 for success)
38+
public func runApp(with window: Window) -> AppExitCode {
39+
return AppRunner.shared.run(with: window)
40+
}
41+
42+
/// Convenience function to run the application with window options
43+
/// Creates a window with the specified options and runs the application
44+
/// - Parameter options: Window options for creating the window
45+
/// - Returns: Exit code of the application (0 for success)
46+
public func runApp(with options: WindowOptions) -> AppExitCode {
47+
guard let window = WindowManager.shared.createWindow(with: options) else {
48+
return .invalidWindow
49+
}
50+
return AppRunner.shared.run(with: window)
51+
}
52+
53+
/// Convenience function to run the application with default window
54+
/// Creates a default window and runs the application
55+
/// - Returns: Exit code of the application (0 for success)
56+
public func runApp() -> AppExitCode {
57+
guard let window = WindowManager.shared.createWindow() else {
58+
return .invalidWindow
459
}
60+
return AppRunner.shared.run(with: window)
561
}

0 commit comments

Comments
 (0)