-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathQSHPreferencesViewController.m
89 lines (73 loc) · 3.18 KB
/
QSHPreferencesViewController.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
//
// QSHPreferencesViewController.m
// quark-shell
//
// Created by Xhacker Liu on 6/11/14.
// Copyright (c) 2014 Xhacker. All rights reserved.
//
#import "QSHPreferencesViewController.h"
#import "QSHWebScriptObjectConverter.h"
#import <MASShortcutView.h>
#import <MASShortcut.h>
@interface QSHPreferencesViewController ()
@property (nonatomic) NSString *identifier;
@property (nonatomic) NSImage *toolbarItemImage;
@property (nonatomic) NSString *toolbarItemLabel;
@property (nonatomic) NSInteger height;
@property (nonatomic, weak) id delegate;
@end
@implementation QSHPreferencesViewController
// a hack for OS X 10.9, don't really know why
@synthesize identifier = _identifier;
- (instancetype)initWithIdentifier:(NSString *)identifier
toolbarImage:(NSImage *)image
toolbarLabel:(NSString *)label
height:(NSInteger)height
delegate:(id)delegate
{
self = [super initWithNibName:@"QSHPreferencesViewController" bundle:nil];
if (self) {
_identifier = identifier;
_toolbarItemImage = image;
_toolbarItemLabel = label;
_height = height;
_delegate = delegate;
}
return self;
}
- (void)loadView
{
[super loadView];
self.view.frame = NSMakeRect(self.view.frame.origin.x, self.view.frame.origin.y,
self.view.frame.size.width, self.height);
NSString *url = [[NSURL URLWithString:kPreferencesDirectory relativeToURL:[[NSBundle mainBundle] resourceURL]] absoluteString];
url = [url stringByAppendingString:[NSString stringWithFormat:@"%@.html", self.identifier]];
self.webView.mainFrameURL = url;
self.webView.frameLoadDelegate = self.delegate;
self.webView.UIDelegate = self.delegate;
self.webView.policyDelegate = self.delegate;
}
- (void)addNativeComponent:(NSDictionary *)component
{
if ([component[@"type"] isEqualToString:@"ShortcutRecorder"]) {
static const CGFloat width = 180;
static const CGFloat height = 19;
CGFloat x = [component[@"options"][@"x"] doubleValue];
CGFloat yFlipped = self.view.frame.size.height - [component[@"options"][@"y"] doubleValue] - height;
MASShortcutView *shortcutView = [[MASShortcutView alloc] initWithFrame:NSMakeRect(x, yFlipped, width, height)];
if (component[@"options"][@"keycode"]) {
NSUInteger keycode = [component[@"options"][@"keycode"] unsignedIntegerValue];
NSUInteger flags = [component[@"options"][@"modifierFlags"] unsignedIntegerValue];
MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:keycode modifierFlags:flags];
shortcutView.shortcutValue = shortcut;
}
shortcutView.shortcutValueChange = ^(MASShortcutView *sender) {
QSHWebScriptObjectConverter *converter = [[QSHWebScriptObjectConverter alloc] initWithWebView:self.webView];
[converter callFunction:component[@"options"][@"onChange"]
withArgs:@[@([sender.shortcutValue keyCode]),
@([sender.shortcutValue modifierFlags])]];
};
[self.view addSubview:shortcutView];
}
}
@end