-
Notifications
You must be signed in to change notification settings - Fork 18
/
MASShortcutView+UserDefaults.m
executable file
·221 lines (189 loc) · 10.3 KB
/
MASShortcutView+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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
///:
/*****************************************************************************
** **
** .======. **
** | 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 "MASShortcutView+UserDefaults.h"
#import "MASShortcut.h"
#import <objc/runtime.h>
NSString static* kKeyPathForShortcutValueInShortcutViewObject = @"shortcutValue";
#pragma mark MASShortcutDefaultsObserver interface
@interface MASShortcutDefaultsObserver : NSObject
{
MASShortcut* _originalShortcut;
BOOL _internalPreferenceChange;
BOOL _internalShortcutChange;
NSString* _userDefaultsKey;
MASShortcutView* _shortcutView;
}
@property ( nonatomic, readonly ) NSString* userDefaultsKey;
@property ( nonatomic, readonly, unsafe_unretained ) MASShortcutView* shortcutView;
- ( id ) initWithShortcutView: ( MASShortcutView* )_ShortcutView
userDefaultsKey: ( NSString* )_UserDefaultsKey;
@end
#pragma mark MASShortcutView + MASShortcutViewUserDefaults
@implementation MASShortcutView ( MASShortcutViewUserDefaults )
void* kDefaultsObserver = &kDefaultsObserver;
- ( NSString* ) associatedUserDefaultsKey
{
MASShortcutDefaultsObserver* defaultsObserver = objc_getAssociatedObject( self, kDefaultsObserver );
return defaultsObserver.userDefaultsKey;
}
- ( void ) setAssociatedUserDefaultsKey: ( NSString* )_AssociatedUserDefaultsKey
{
/* First, stop observing previous shortcut view
* passed nil to clear the association */
objc_setAssociatedObject( self, kDefaultsObserver, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
/* Next, start observing current shortcut view */
MASShortcutDefaultsObserver* defaultsObserver =
[ [ MASShortcutDefaultsObserver alloc ] initWithShortcutView: self
userDefaultsKey: _AssociatedUserDefaultsKey ];
/* Tie the new defaults observer to self... */
objc_setAssociatedObject( self, kDefaultsObserver, defaultsObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
/* ...we have specified a strong reference to the associated object (OBJC_ASSOCIATION_RETAIN_NONATOMIC)
* so we should release it once */
[ defaultsObserver release ];
}
@end
#pragma mark MASShortcutDefaultsObserver implementation
@implementation MASShortcutDefaultsObserver
@synthesize userDefaultsKey = _userDefaultsKey;
@synthesize shortcutView = _shortcutView;
#pragma mark Initializers & Deallocator
- ( id ) initWithShortcutView: ( MASShortcutView* )_ShortcutView
userDefaultsKey: ( NSString* )_UserDefaultsKey
{
if ( self = [ super init ] )
{
_originalShortcut = _ShortcutView.shortcutValue;
_shortcutView = _ShortcutView;
_userDefaultsKey = _UserDefaultsKey.copy;
[ self startObservingShortcutView ];
}
return self;
}
- ( void ) dealloc
{
// __unsafe_unretained _shortcutView is not yet deallocated because it refers MASShortcutDefaultsObserver
[ self stopObservingShortcutView ];
[ super dealloc ];
}
#pragma mark -
void* kShortcutValueObserver = &kShortcutValueObserver;
- ( void ) startObservingShortcutView
{
// Read initial shortcut value from user preferences
NSUserDefaults* defaults = [ NSUserDefaults standardUserDefaults ];
NSData* data = [ defaults dataForKey: _userDefaultsKey ];
_shortcutView.shortcutValue = [ MASShortcut shortcutWithData: data ];
// Observe user preferences to update shortcut value when it changed
[ [ NSNotificationCenter defaultCenter ] addObserver: self
selector: @selector( userDefaultsDidChange: )
name: NSUserDefaultsDidChangeNotification
object: defaults ];
// Observe the keyboard shortcut that user inputs by hand
[_shortcutView addObserver: self
forKeyPath: kKeyPathForShortcutValueInShortcutViewObject
options: 0
context: kShortcutValueObserver ];
}
- ( void ) userDefaultsDidChange: ( NSNotification* )_Note
{
// Ignore notifications posted from -[self observeValueForKeyPath:]
if ( _internalPreferenceChange )
return;
_internalShortcutChange = YES;
NSData* data = [ _Note.object dataForKey: _userDefaultsKey ];
_shortcutView.shortcutValue = [ MASShortcut shortcutWithData: data ];
_internalShortcutChange = NO;
}
- ( void ) stopObservingShortcutView
{
// Stop observing keyboard hotkeys entered by user in the shortcut view
[ _shortcutView removeObserver: self
forKeyPath: kKeyPathForShortcutValueInShortcutViewObject
context: kShortcutValueObserver ];
// Stop observing user preferences
[ [ NSNotificationCenter defaultCenter ] removeObserver: self
name: NSUserDefaultsDidChangeNotification
object: [ NSUserDefaults standardUserDefaults ] ];
// Restore original hotkey in the shortcut view
_shortcutView.shortcutValue = _originalShortcut;
}
- ( void ) observeValueForKeyPath: ( NSString* )_KeyPath
ofObject: ( id )_Object
change: ( NSDictionary* )_Change
context: ( void* )_Context
{
if ( _Context == kShortcutValueObserver )
{
if ( _internalShortcutChange )
return;
MASShortcut* shortcut = [ _Object valueForKey: _KeyPath ];
_internalPreferenceChange = YES;
NSUserDefaults *defaults = [ NSUserDefaults standardUserDefaults ];
[ defaults setObject: ( shortcut.data ? : [ NSKeyedArchiver archivedDataWithRootObject: nil ] )
forKey: _userDefaultsKey ];
[ defaults synchronize ];
_internalPreferenceChange = NO;
}
else
[ super observeValueForKeyPath: _KeyPath
ofObject: _Object
change: _Change
context: _Context ];
}
@end
//////////////////////////////////////////////////////////////////////////////
/*****************************************************************************
** **
** _________ _______ **
** |___ ___| / ______ \ **
** | | _______ _______ _______ | / |_| **
** | | || || || || || || | | _ __ **
** | | || || || || || || | | |__ \ **
** | | || || || || || || | \_ _ __| | _ **
** |_| ||_____|| || || ||_____|| \________/ |_| **
** || **
** ||_____|| **
** **
****************************************************************************/
///: