Skip to content

Commit dd295c5

Browse files
committed
Refactor AccessibilityManager to use singleton pattern
Add GetInstance() method and update usage to enforce singleton. Improve documentation in headers and update C API to use singleton.
1 parent a440f34 commit dd295c5

File tree

6 files changed

+116
-20
lines changed

6 files changed

+116
-20
lines changed

examples/window_example/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ using nativeapi::WindowManager;
1313
using nativeapi::WindowOptions;
1414

1515
int main() {
16+
17+
native_accessibility_manager_enable();
18+
bool is_enabled= native_accessibility_manager_is_enabled();
19+
std::cout << "is_enabled: " << is_enabled << std::endl;
20+
1621
DisplayManager& display_manager = DisplayManager::GetInstance();
1722
TrayManager& tray_manager = TrayManager::GetInstance();
1823
WindowManager& window_manager = WindowManager::GetInstance();

src/accessibility_manager.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
#include <iostream>
2-
31
#include "accessibility_manager.h"
42

53
namespace nativeapi {
64

5+
AccessibilityManager& AccessibilityManager::GetInstance() {
6+
static AccessibilityManager instance;
7+
return instance;
8+
}
9+
710
AccessibilityManager::AccessibilityManager() {}
811

912
AccessibilityManager::~AccessibilityManager() {}

src/accessibility_manager.h

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,90 @@
22

33
namespace nativeapi {
44

5-
// AccessibilityManager is a singleton that manages all accessibility on the
6-
// system.
5+
/**
6+
* @class AccessibilityManager
7+
* @brief A singleton class that manages system accessibility features
8+
*
9+
* The AccessibilityManager provides a centralized interface for managing
10+
* accessibility functionality across the system. It follows the singleton
11+
* pattern to ensure only one instance exists throughout the application
12+
* lifecycle, providing consistent state management for accessibility features.
13+
*
14+
* Key responsibilities:
15+
* - Enable/disable system accessibility features
16+
* - Query accessibility state
17+
* - Provide thread-safe access to accessibility functionality
18+
*
19+
* Usage example:
20+
* @code
21+
* AccessibilityManager& manager = AccessibilityManager::GetInstance();
22+
* manager.Enable();
23+
* bool enabled = manager.IsEnabled();
24+
* @endcode
25+
*/
726
class AccessibilityManager {
827
public:
9-
AccessibilityManager();
28+
/**
29+
* @brief Gets the singleton instance of AccessibilityManager
30+
* @return Reference to the singleton instance
31+
*
32+
* This method provides thread-safe access to the singleton instance.
33+
* The instance is created on first access and remains alive for the
34+
* duration of the application.
35+
*/
36+
static AccessibilityManager& GetInstance();
37+
38+
/**
39+
* @brief Virtual destructor
40+
*
41+
* Ensures proper cleanup of resources when the manager is destroyed.
42+
* Note: In singleton pattern, this is typically called only at application
43+
* shutdown.
44+
*/
1045
virtual ~AccessibilityManager();
1146

12-
// Enable the accessibility
47+
/**
48+
* @brief Enables system accessibility features
49+
*
50+
* Activates accessibility functionality across the system. This method
51+
* should be called to make accessibility features available to users.
52+
* The operation is idempotent - calling it multiple times has the same
53+
* effect as calling it once.
54+
*
55+
* @note This operation may require system permissions depending on the
56+
* platform implementation.
57+
*/
1358
void Enable();
1459

15-
// Whether the accessibility is enabled
60+
/**
61+
* @brief Checks if accessibility features are currently enabled
62+
* @return true if accessibility is enabled, false otherwise
63+
*
64+
* This method provides a quick way to query the current state of
65+
* accessibility features without modifying the system state.
66+
*/
1667
bool IsEnabled();
68+
69+
// Delete copy constructor and assignment operator to prevent copies
70+
AccessibilityManager(const AccessibilityManager&) = delete;
71+
AccessibilityManager& operator=(const AccessibilityManager&) = delete;
72+
73+
private:
74+
/**
75+
* @brief Private constructor for singleton pattern
76+
*
77+
* Initializes the AccessibilityManager instance. This constructor is
78+
* private to prevent direct instantiation - use GetInstance() instead.
79+
*/
80+
AccessibilityManager();
81+
82+
/**
83+
* @brief Internal flag tracking accessibility state
84+
*
85+
* Maintains the current enabled/disabled state of accessibility features.
86+
* This member is used internally by Enable() and IsEnabled() methods.
87+
*/
88+
bool enabled_;
1789
};
1890

19-
} // namespace nativeapi
91+
} // namespace nativeapi
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
#include <string.h>
2-
#include <iostream>
3-
4-
#include "../accessibility_manager.h"
51
#include "accessibility_manager_c.h"
2+
#include "../accessibility_manager.h"
63

7-
using namespace nativeapi;
8-
9-
static AccessibilityManager g_accessibility_manager = AccessibilityManager();
4+
static nativeapi::AccessibilityManager& AccessibilityManagerInstance() {
5+
return nativeapi::AccessibilityManager::GetInstance();
6+
}
107

118
FFI_PLUGIN_EXPORT
129
void native_accessibility_manager_enable() {
13-
g_accessibility_manager.Enable();
10+
AccessibilityManagerInstance().Enable();
1411
}
1512

1613
FFI_PLUGIN_EXPORT
1714
bool native_accessibility_manager_is_enabled() {
18-
return g_accessibility_manager.IsEnabled();
15+
return AccessibilityManagerInstance().IsEnabled();
1916
}

src/capi/accessibility_manager_c.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include <stdbool.h>
24

35
#if _WIN32
@@ -10,9 +12,27 @@
1012
extern "C" {
1113
#endif
1214

15+
/**
16+
* @brief Enables system accessibility features
17+
*
18+
* This function activates accessibility functionality across the system.
19+
* The operation is idempotent - calling it multiple times has the same
20+
* effect as calling it once.
21+
*
22+
* @note This operation may require system permissions depending on the
23+
* platform implementation.
24+
*/
1325
FFI_PLUGIN_EXPORT
1426
void native_accessibility_manager_enable();
1527

28+
/**
29+
* @brief Checks if accessibility features are currently enabled
30+
*
31+
* @return true if accessibility is enabled, false otherwise
32+
*
33+
* This function provides a quick way to query the current state of
34+
* accessibility features without modifying the system state.
35+
*/
1636
FFI_PLUGIN_EXPORT
1737
bool native_accessibility_manager_is_enabled();
1838

src/platform/macos/accessibility_manager_macos.mm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#include "../../accessibility_manager.h"
2-
3-
// Import Cocoa headers
41
#import <Cocoa/Cocoa.h>
52

3+
#include "../../accessibility_manager.h"
4+
65
namespace nativeapi {
76

87
void AccessibilityManager::Enable() {

0 commit comments

Comments
 (0)