-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMASShortcut+UserDefaults.m
executable file
·167 lines (141 loc) · 8.07 KB
/
MASShortcut+UserDefaults.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
///:
/*****************************************************************************
** **
** .======. **
** | INRI | **
** | | **
** | | **
** .========' '========. **
** | _ xxxx _ | **
** | /_;-.__ / _\ _.-;_\ | **
** | `-._`'`_/'`.-' | **
** '========.`\ /`========' **
** | | / | **
** |/-.( | **
** |\_._\ | **
** | \ \`;| **
** | > |/| **
** | / // | **
** | |// | **
** | \(\ | **
** | `` | **
** | | **
** | | **
** | | **
** | | **
** \\ _ _\\| \// |//_ _ \// _ **
** ^ `^`^ ^`` `^ ^` ``^^` `^^` `^ `^ **
** **
** Created by Vadim Shpakovski Originally **
** GitHub: https://github.com/shpakovski/MASShortcut **
** **
** **
** Forked and Changed by Richard Heard **
** GitHub: https://github.com/heardrwt/MASShortcut **
** **
** **
** Forked, Changed and Republished by Tong Guo **
** GitHub: https://github.com/TongG/MASShortcut **
** **
** Copyright (c) 2014 Tong G. **
** ALL RIGHTS RESERVED. **
** **
****************************************************************************/
#import "MASShortcut+UserDefaults.h"
#import "MASShortcut+Monitoring.h"
#pragma mark MASShortcutUserDefaultsHotKey class interface
@interface MASShortcutUserDefaultsHotKey : NSObject
{
NSString* _userDefaultsKey;
void ( ^_handler )();
id _monitor;
}
@property ( nonatomic, readonly ) NSString* userDefaultsKey;
@property ( nonatomic, copy ) void ( ^handler )();
@property ( nonatomic, retain ) id monitor;
- ( id ) initWithUserDefaultsKey: ( NSString* )_UserDefaultsKey
handler: ( void (^)() )_Handler;
@end // MASShortcutUserDefaultsHotKey class interface
#pragma mark MASShortcut + MASShortcutUserDefaults
@implementation MASShortcut ( MASShortcutUserDefaults )
+ ( NSMutableDictionary* ) registeredUserDefaultsHotKeys
{
NSMutableDictionary static* shared = nil;
dispatch_once_t static onceToken;
dispatch_once( &onceToken
, ^{ shared = [ [ NSMutableDictionary dictionary ] retain ]; } );
return shared;
}
+ ( void ) registerGlobalShortcutWithUserDefaultsKey: ( NSString* )_UserDefaultsKey
handler: ( void (^)() )_Handler
{
MASShortcutUserDefaultsHotKey* hotKey = [ [ MASShortcutUserDefaultsHotKey alloc ] initWithUserDefaultsKey: _UserDefaultsKey
handler: _Handler ];
[ self registeredUserDefaultsHotKeys ][ _UserDefaultsKey ] = hotKey;
}
+ ( void ) unregisterGlobalShortcutWithUserDefaultsKey: ( NSString* )_UserDefaultsKey
{
NSMutableDictionary* registeredHotKeys = [ self registeredUserDefaultsHotKeys ];
[ registeredHotKeys removeObjectForKey: _UserDefaultsKey ];
}
@end // MASShortcut + MASShortcutUserDefaults
#pragma mark MASShortcutUserDefaultsHotKey class implementation
@implementation MASShortcutUserDefaultsHotKey
@synthesize monitor = _monitor;
@synthesize handler = _handler;
@synthesize userDefaultsKey = _userDefaultsKey;
#pragma mark Initializers & Deallocator
- ( id ) initWithUserDefaultsKey: ( NSString* )_UserDefaultsKey
handler: ( void (^)() )_Handler
{
if ( self = [ super init ] )
{
_userDefaultsKey = [ _UserDefaultsKey copy ];
_handler = [ _Handler copy ];
[ [ NSNotificationCenter defaultCenter] addObserver: self
selector: @selector( userDefaultsDidChange: )
name: NSUserDefaultsDidChangeNotification
object: [ NSUserDefaults standardUserDefaults ] ];
[ self installHotKeyFromUserDefaults ];
}
return self;
}
- ( void ) dealloc
{
[ [ NSNotificationCenter defaultCenter ] removeObserver: self
name: NSUserDefaultsDidChangeNotification
object: [ NSUserDefaults standardUserDefaults ] ];
[ MASShortcut removeGlobalHotkeyMonitor: self.monitor ];
[ super dealloc ];
}
#pragma mark -
- ( void ) userDefaultsDidChange: ( NSNotification* )_Notif
{
[ MASShortcut removeGlobalHotkeyMonitor: self.monitor ];
[ self installHotKeyFromUserDefaults ];
}
- ( void ) installHotKeyFromUserDefaults
{
NSData* data = [ [ NSUserDefaults standardUserDefaults ] dataForKey: _userDefaultsKey ];
MASShortcut* shortcut = [ MASShortcut shortcutWithData: data ];
if ( !shortcut )
return;
self.monitor = [ MASShortcut addGlobalHotkeyMonitorWithShortcut: shortcut
handler: self.handler ];
}
@end // MASShortcutUserDefaultsHotKey class implementation
//////////////////////////////////////////////////////////////////////////////
/*****************************************************************************
** **
** _________ _______ **
** |___ ___| / ______ \ **
** | | _______ _______ _______ | / |_| **
** | | || || || || || || | | _ __ **
** | | || || || || || || | | |__ \ **
** | | || || || || || || | \_ _ __| | _ **
** |_| ||_____|| || || ||_____|| \________/ |_| **
** || **
** ||_____|| **
** **
****************************************************************************/
///: