KMSignatureWindowController.m 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. //
  2. // KMSignatureWindowController.m
  3. // PDF Reader Premium
  4. //
  5. // Created by wangshuai on 14/12/3.
  6. // Copyright (c) 2014年 zhangjie. All rights reserved.
  7. //
  8. #import "KMSignatureWindowController.h"
  9. #import "KMDrawView.h"
  10. #import "KMSignatureManager.h"
  11. #import "NSImage+CustomImage.h"
  12. #import "KMPDFSignatureImageView.h"
  13. #import "KMPDFSignatureTextView.h"
  14. #import "KMPopUpButton.h"
  15. #import "NSMenu+Category.h"
  16. #import "CPDFListView.h"
  17. #import "NSButton+TitleColor.h"
  18. #import <PDF_Master-Swift.h>
  19. #import "KMCoverButton.h"
  20. #import "KMSignatureHelpViewController.h"
  21. static NSMutableArray * recentlyFonts;
  22. #pragma mark KMSignatureColorButton
  23. @interface KMSignatureColorButton : NSButton
  24. @property (nonatomic,retain) NSColor *circleColor;
  25. @property (nonatomic,retain) NSImage *drawImage;
  26. @end
  27. @implementation KMSignatureColorButton
  28. -(void)setCircleColor:(NSColor *)circleColor
  29. {
  30. _circleColor = circleColor;
  31. [self setNeedsDisplay:YES];
  32. }
  33. -(id)initWithCoder:(NSCoder *)coder {
  34. if (self == [super initWithCoder:coder]) {
  35. self.wantsLayer = YES;
  36. self.layer.cornerRadius = 12;
  37. self.layer.masksToBounds = YES;
  38. self.layer.borderWidth = 1.5;
  39. }
  40. return self;
  41. }
  42. -(void)drawRect:(NSRect)rect
  43. {
  44. [super drawRect:rect];
  45. if (_circleColor) {
  46. NSBezierPath *path3 = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(3, 3, rect.size.width - 6, rect.size.height - 6)];
  47. [self.circleColor set];
  48. [path3 fill];
  49. } else if (self.drawImage) {
  50. [self.drawImage drawInRect:CGRectMake(3,3,rect.size.width -6,rect.size.height - 6)
  51. fromRect:NSZeroRect
  52. operation:NSCompositeSourceOver
  53. fraction:1.0];
  54. }
  55. }
  56. @end
  57. #pragma mark KMSignatureButton
  58. @interface KMSignatureButton : NSButton
  59. @end
  60. @implementation KMSignatureButton
  61. - (NSMenu*)menuForEvent:(NSEvent *)theEvent
  62. {
  63. NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
  64. NSMenuItem *item = [menu addItemWithTitle:NSLocalizedString(@"Delete", nil) action:@selector(delete) keyEquivalent:@""];
  65. item.target = self;
  66. item = [menu addItemWithTitle:NSLocalizedString(@"Export", @"Menu item title") action:nil keyEquivalent:@""];
  67. NSMenu * tSubMenu = [[NSMenu alloc] init];
  68. NSMenuItem *tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PNG", @"Menu item title") action:@selector(export:) target:self atIndex:0];
  69. tMenuItem.tag = 0;
  70. tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"JPG", @"Menu item title") action:@selector(export:) target:self atIndex:1];
  71. tMenuItem.tag = 1;
  72. tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PDF", @"Menu item title") action:@selector(export:) target:self atIndex:2];
  73. tMenuItem.tag = 2;
  74. item.submenu = tSubMenu;
  75. return menu;
  76. }
  77. - (void)delete
  78. {
  79. [[NSNotificationCenter defaultCenter] postNotificationName:@"kKMSignatureDeleteNotification" object:[NSNumber numberWithInteger:self.tag]];
  80. }
  81. - (void)export:(NSMenuItem *)sender
  82. {
  83. NSInteger index = self.tag;
  84. NSInteger type = sender.tag;
  85. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  86. [signatureManager loadAllSignatureList];
  87. KMSignature *signature = signatureManager.sigatureList[index];
  88. NSImage *image = signature.pathsImage;
  89. if (type == 0) {
  90. NSData *data = image.TIFFRepresentation;
  91. NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
  92. [imageRep setSize:image.size];
  93. NSData *imageData = [imageRep representationUsingType:NSPNGFileType properties:@{}];
  94. NSSavePanel *savePanel = [NSSavePanel savePanel];
  95. savePanel.allowedFileTypes = @[@"png"];
  96. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  97. if (result) {
  98. if ([imageData writeToURL:savePanel.URL atomically:YES]) {
  99. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  100. inFileViewerRootedAtPath:@""];
  101. }
  102. }
  103. }];
  104. } else if (type == 1) {
  105. NSData *data = image.TIFFRepresentation;
  106. NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
  107. [imageRep setSize:image.size];
  108. NSData *imageData = [imageRep representationUsingType:NSJPEGFileType properties:@{}];
  109. NSSavePanel *savePanel = [NSSavePanel savePanel];
  110. savePanel.allowedFileTypes = @[@"jpg"];
  111. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  112. if (result) {
  113. if ([imageData writeToURL:savePanel.URL atomically:YES]) {
  114. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  115. inFileViewerRootedAtPath:@""];
  116. }
  117. }
  118. }];
  119. } else {
  120. CPDFDocument *pdf = [[CPDFDocument alloc] init];
  121. // CPDFPage *page = [[CPDFPage alloc] initWithImage:image];
  122. NSString *signatureImagePath =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"signatureImage.png"];
  123. NSData *data = image.TIFFRepresentation;
  124. [data writeToURL:[NSURL fileURLWithPath:signatureImagePath] atomically:YES];
  125. [pdf insertPage:image.size withImage:signatureImagePath atIndex:0];
  126. NSSavePanel *savePanel = [NSSavePanel savePanel];
  127. savePanel.allowedFileTypes = @[@"pdf"];
  128. [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  129. if (result) {
  130. if ([pdf writeToURL:savePanel.URL]) {
  131. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  132. inFileViewerRootedAtPath:@""];
  133. }
  134. }
  135. }];
  136. }
  137. }
  138. @end
  139. #pragma mark KMSignatureWindowController
  140. static KMSignatureWindowController *windowController_signature;
  141. @interface KMSignatureWindowController () <KMDrawViewDelegate,KMChangeSignatureTextDelegate, KMSelectPopButtonDelegate>
  142. @property (weak) IBOutlet NSButton *cancelBtton;
  143. @property (weak) IBOutlet NSButton *applyButton;
  144. @property (nonatomic, strong) KMDesignButton *cancelButtonVC;
  145. @property (nonatomic, strong) KMDesignButton *applyButtonVC;
  146. @property (weak) IBOutlet NSButton *clearButton;
  147. @property (strong) IBOutlet NSView *signTypeView;
  148. @property (weak) IBOutlet NSButton *inputButton;
  149. @property (weak) IBOutlet NSButton *drawingButton;
  150. @property (weak) IBOutlet NSButton *pictureButton;
  151. @property (strong) IBOutlet NSView *signTypeBackView;
  152. @property (weak) IBOutlet NSTextField *trackpadLabel;
  153. @property (weak) IBOutlet NSBox *contentBox;
  154. @property (weak) IBOutlet NSTextField *titleLabel;
  155. //keyboard
  156. @property (strong) IBOutlet NSView *inputView;
  157. @property (weak) IBOutlet KMPDFSignatureTextView *keyboardView;
  158. @property (weak) IBOutlet NSPopUpButton *fontBox;
  159. @property (strong) IBOutlet NSButton *keyboardColorBtn1;
  160. @property (strong) IBOutlet NSButton *keyboardColorBtn2;
  161. @property (strong) IBOutlet NSButton *keyboardColorBtn3;
  162. @property (strong) IBOutlet NSButton *keyboardColorBtn4;
  163. @property (strong) IBOutlet NSView *keyboardColorSelView;
  164. @property (weak) IBOutlet KMSignatureColorButton *textColorButton;
  165. @property (nonatomic, strong) KMDesignSelect *fontVC;
  166. @property (nonatomic, strong) NSArray *fontValues;
  167. @property (nonatomic, copy) NSString *fontDefaultValue;
  168. //Draw
  169. @property (strong) IBOutlet NSView *drawingView;
  170. @property (weak) IBOutlet KMDrawView *drawView;
  171. @property (weak) IBOutlet NSButton *trackpadButton;
  172. @property (nonatomic, strong) KMDesignButton *trackpadButtonVC;
  173. @property (strong) IBOutlet NSPopUpButton *drawSizeBox;
  174. @property (strong) IBOutlet NSView *colorBGView;
  175. @property (strong) IBOutlet NSView *drawColorSelView;
  176. @property (strong) IBOutlet NSButton *drawColorBtn1;
  177. @property (strong) IBOutlet NSButton *drawColorBtn2;
  178. @property (strong) IBOutlet NSButton *drawColorBtn3;
  179. @property (strong) IBOutlet NSButton *drawColorBtn4;
  180. @property (nonatomic, strong) KMDesignSelect *drawSizeVC;
  181. @property (nonatomic, strong) NSArray *drawSizeValues;
  182. @property (nonatomic, copy) NSString *drawSizeDefaultValue;
  183. @property (weak) IBOutlet NSView *drawTipView;
  184. @property (weak) IBOutlet NSTextField *drawTipLabel;
  185. //Picture
  186. @property (strong) IBOutlet NSView *pictureView;
  187. @property (weak) IBOutlet KMPDFSignatureImageView *pictureBackView;
  188. @property (strong) IBOutlet NSButton *pictureClearBackBtn;
  189. @property (nonatomic, strong) KMDesignButton *pictureClearBackBtnVC;
  190. @property (weak) IBOutlet KMCoverButton *pictureHelpButton;
  191. @property (nonatomic,strong) KMSignature *selectedSignature;
  192. @property (nonatomic,strong) NSMenuItem *selectItem;
  193. @property (nonatomic,assign) kKMPDFSignatureType type;
  194. @property (nonatomic,copy) void (^handler)(KMSignature*);
  195. @property (nonatomic, strong) NSPopover *popover;
  196. @end
  197. @implementation KMSignatureWindowController
  198. - (id)init
  199. {
  200. if (self = [super initWithWindowNibName:@"KMSignatureWindowController"]) {
  201. NSMutableArray *fonts = [NSMutableArray array];
  202. for (NSDictionary *dic in [CPDFAnnotationModel supportFonts]) {
  203. [fonts addObject:dic.allKeys.firstObject];
  204. }
  205. self.fontValues = fonts.copy;
  206. self.fontDefaultValue = @"Helvetica";
  207. self.drawSizeValues = @[@"1.0",@"2.0",@"4.0",@"6.0",@"8.0"];
  208. self.drawSizeDefaultValue = @"2.0";
  209. }
  210. return self;
  211. }
  212. - (void)dealloc
  213. {
  214. _drawView.delegate = nil;
  215. _keyboardView.delegate = nil;
  216. [[NSNotificationCenter defaultCenter] removeObserver:self];
  217. [[NSColorPanel sharedColorPanel] setTarget:nil];
  218. [[NSColorPanel sharedColorPanel] setAction:nil];
  219. }
  220. #pragma mark - View Methods
  221. - (void)windowDidLoad {
  222. [super windowDidLoad];
  223. __weak typeof (self) weakSelf = self;
  224. self.type = kKMPDFSignatureType_Text;
  225. // self.textColorButton.drawImage = self.mouseColorButton.drawImage = [NSImage imageNamed:@"view_color"];
  226. self.cancelBtton.title = @"";
  227. self.cancelButtonVC = [[KMDesignButton alloc] initWithType:DesignButtonTypeText];
  228. [self.cancelBtton addSubview:self.cancelButtonVC.view];
  229. self.cancelButtonVC.view.frame = self.cancelBtton.bounds;
  230. self.cancelButtonVC.view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
  231. self.cancelButtonVC.stringValue = NSLocalizedString(@"Cancel", nil);
  232. self.cancelButtonVC.target = self;
  233. self.cancelButtonVC.action = @selector(dismissSheet:);
  234. [self.cancelButtonVC buttonWithType:TokenButtonTypeSec_Icon size:TokenButtonSizeM height:nil];
  235. [self.clearButton setTarget:self];
  236. [self.clearButton setAction:@selector(clearButton_Click:)];
  237. self.clearButton.hidden = YES;
  238. self.applyButton.title = @"";
  239. self.applyButtonVC = [[KMDesignButton alloc] initWithType:DesignButtonTypeText];
  240. [self.applyButton addSubview:self.applyButtonVC.view];
  241. self.applyButtonVC.view.frame = self.applyButton.bounds;
  242. self.applyButtonVC.view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
  243. self.applyButtonVC.stringValue = NSLocalizedString(@"Save", nil);
  244. self.applyButtonVC.target = self;
  245. self.applyButtonVC.action = @selector(applyButton_Click:);
  246. [self.applyButtonVC buttonWithType:TokenButtonTypeCta size:TokenButtonSizeM height:nil];
  247. self.applyButtonVC.enabled = NO;
  248. self.contentBox.wantsLayer = YES;
  249. self.contentBox.layer.backgroundColor = [NSColor clearColor].CGColor;
  250. self.contentBox.borderType = NSNoBorder;
  251. self.inputButton.bordered = self.drawingButton.bordered = self.pictureButton.bordered = NO;
  252. //Keyboard
  253. self.inputView.wantsLayer = YES;
  254. self.inputView.layer.backgroundColor = [NSColor whiteColor].CGColor;
  255. self.keyboardView.wantsLayer = YES;
  256. self.keyboardView.layer.backgroundColor = [NSColor colorWithRed:247/255. green:248/255. blue:250/255. alpha:1.].CGColor;
  257. self.keyboardView.changeSignatureTextCallback = ^(BOOL isTure) {
  258. if (isTure) {
  259. weakSelf.clearButton.hidden = NO;
  260. weakSelf.clearButton.title = NSLocalizedString(@"Clear", nil);
  261. } else {
  262. weakSelf.clearButton.hidden = YES;
  263. }
  264. };
  265. self.keyboardColorBtn1.wantsLayer = self.keyboardColorBtn2.wantsLayer = self.keyboardColorBtn3.wantsLayer = self.keyboardColorBtn4.wantsLayer = YES;
  266. self.keyboardColorBtn1.layer.backgroundColor = [NSColor colorWithRed:37/255.0f green:38/255.0f blue:41/255.0f alpha:1.0f].CGColor;
  267. self.keyboardColorBtn2.layer.backgroundColor = [NSColor colorWithRed:252/255.0f green:31/255.0f blue:31/255.0f alpha:1.0f].CGColor;
  268. self.keyboardColorBtn3.layer.backgroundColor = [NSColor colorWithRed:39/255.0f green:60/255.0f blue:98/255.0f alpha:1.0f].CGColor;
  269. self.keyboardColorBtn4.layer.backgroundColor = [NSColor colorWithRed:148/255.0f green:152/255.0f blue:156/255.0f alpha:1.0f].CGColor;
  270. self.keyboardColorBtn1.layer.cornerRadius = self.keyboardColorBtn2.layer.cornerRadius = self.keyboardColorBtn3.layer.cornerRadius = self.keyboardColorBtn4.layer.cornerRadius = 10.;
  271. self.keyboardColorBtn1.layer.masksToBounds = self.keyboardColorBtn2.layer.masksToBounds = self.keyboardColorBtn3.layer.masksToBounds = self.keyboardColorBtn4.layer.masksToBounds = YES;
  272. self.keyboardColorSelView.wantsLayer = YES;
  273. self.keyboardColorSelView.layer.backgroundColor = [NSColor colorWithRed:206/255.0f green:208/255.0f blue:212/255.0f alpha:0.6].CGColor;
  274. self.keyboardColorSelView.layer.borderWidth = 1.;
  275. self.keyboardColorSelView.layer.borderColor = [NSColor colorWithRed:206/255.0f green:208/255.0f blue:212/255.0f alpha:1.0f].CGColor;
  276. self.keyboardColorSelView.layer.cornerRadius = 4.;
  277. self.keyboardColorSelView.layer.masksToBounds = YES;
  278. [self colorTextButtonAction:self.keyboardColorBtn1];
  279. //Draw
  280. self.drawView.delegate = self;
  281. self.drawView.wantsLayer = YES;
  282. self.drawView.strokeRadius = [self.drawSizeDefaultValue floatValue];
  283. self.drawView.layer.cornerRadius = 4.;
  284. self.drawView.layer.masksToBounds = YES;
  285. self.drawView.layer.borderWidth = 1.;
  286. self.drawView.layer.borderColor = [NSColor colorWithRed:223/255. green:225/255. blue:229/255. alpha:1.].CGColor;
  287. self.drawView.layer.backgroundColor = [NSColor colorWithRed:247/255. green:248/255. blue:250/255. alpha:1.].CGColor;
  288. self.drawColorBtn1.wantsLayer = self.drawColorBtn2.wantsLayer = self.drawColorBtn3.wantsLayer = self.drawColorBtn4.wantsLayer = YES;
  289. self.drawColorBtn1.layer.backgroundColor = [NSColor colorWithRed:37/255.0f green:38/255.0f blue:41/255.0f alpha:1.0f].CGColor;
  290. self.drawColorBtn2.layer.backgroundColor = [NSColor colorWithRed:252/255.0f green:31/255.0f blue:31/255.0f alpha:1.0f].CGColor;
  291. self.drawColorBtn3.layer.backgroundColor = [NSColor colorWithRed:39/255.0f green:60/255.0f blue:98/255.0f alpha:1.0f].CGColor;
  292. self.drawColorBtn4.layer.backgroundColor = [NSColor colorWithRed:148/255.0f green:152/255.0f blue:156/255.0f alpha:1.0f].CGColor;
  293. self.drawColorBtn1.layer.cornerRadius = self.drawColorBtn2.layer.cornerRadius = self.drawColorBtn3.layer.cornerRadius = self.drawColorBtn4.layer.cornerRadius = 10.;
  294. self.drawColorBtn1.layer.masksToBounds = self.drawColorBtn2.layer.masksToBounds = self.drawColorBtn3.layer.masksToBounds = self.drawColorBtn4.layer.masksToBounds = YES;
  295. self.drawColorSelView.wantsLayer = YES;
  296. self.drawColorSelView.layer.backgroundColor = [NSColor colorWithRed:206/255.0f green:208/255.0f blue:212/255.0f alpha:0.6].CGColor;
  297. self.drawColorSelView.layer.borderWidth = 1.;
  298. self.drawColorSelView.layer.borderColor = [NSColor colorWithRed:206/255.0f green:208/255.0f blue:212/255.0f alpha:1.0f].CGColor;
  299. self.drawColorSelView.layer.cornerRadius = 4.;
  300. self.drawColorSelView.layer.masksToBounds = YES;
  301. [self drawColorBtnClicked:self.drawColorBtn1];
  302. for (NSString *pxSize in @[@"0.5",@"1.0",@"1.5",@"2.0",@"2.5",@"3.0"]) {
  303. NSDictionary *attrited = @{NSFontAttributeName:[NSFont systemFontOfSize:14]};
  304. NSAttributedString *string = [[NSAttributedString alloc] initWithString:pxSize attributes:attrited];
  305. NSMenuItem *item = [[NSMenuItem alloc] init];
  306. item.attributedTitle = string;
  307. [self.drawSizeBox.menu addItem:item];
  308. }
  309. [self.drawSizeBox selectItem:[self.drawSizeBox itemAtIndex:1]];
  310. [self.drawSizeBox setTitle:@"1.0 pt"];
  311. //Picture
  312. self.pictureView.wantsLayer = YES;
  313. self.pictureView.layer.backgroundColor = [NSColor whiteColor].CGColor;
  314. self.pictureBackView.wantsLayer = YES;
  315. self.pictureBackView.layer.cornerRadius = 4.;
  316. self.pictureBackView.layer.masksToBounds = YES;
  317. self.pictureBackView.layer.borderWidth = 1.;
  318. self.pictureBackView.layer.borderColor = [NSColor colorWithRed:223/255. green:225/255. blue:229/255. alpha:1.].CGColor;
  319. self.pictureBackView.layer.backgroundColor = [NSColor colorWithRed:247/255. green:248/255. blue:250/255. alpha:1.].CGColor;
  320. self.pictureBackView.changeSignatureImageCallback = ^(BOOL isTure) {
  321. if (isTure) {
  322. weakSelf.applyButtonVC.enabled = YES;
  323. } else {
  324. weakSelf.applyButtonVC.enabled = NO;
  325. }
  326. if (weakSelf.pictureBackView.signatureImage) {
  327. weakSelf.clearButton.hidden = NO;
  328. weakSelf.clearButton.title = NSLocalizedString(@"Reselect", nil);
  329. } else {
  330. weakSelf.clearButton.hidden = YES;
  331. }
  332. };
  333. [self.trackpadLabel setHidden:YES];
  334. self.drawTipView.hidden = YES;
  335. self.keyboardView.delegate = self;
  336. self.drawView.changeDrawCallback = ^(BOOL isTure) {
  337. if (isTure) {
  338. weakSelf.applyButtonVC.enabled = YES;
  339. } else {
  340. weakSelf.applyButtonVC.enabled = NO;
  341. }
  342. };
  343. self.drawView.touchEndCallback = ^(BOOL isClear) {
  344. if (isClear) {
  345. weakSelf.clearButton.hidden = YES;
  346. } else {
  347. weakSelf.clearButton.hidden = NO;
  348. weakSelf.clearButton.title = NSLocalizedString(@"Clear", nil);
  349. }
  350. };
  351. [self inputButton_Click:nil];
  352. self.pictureHelpButton.image = [NSImage imageNamed:@"KMImageNameHelpNormal"];
  353. self.pictureHelpButton.toolTip = @"Remove white background from images";
  354. self.pictureHelpButton.action = @selector(showHelpTip:);
  355. self.pictureHelpButton.coverAction = ^(KMCoverButton * _Nonnull button, KMCoverAction action) {
  356. if (action == KMCoverActionEnter) {
  357. button.image = [NSImage imageNamed:@"KMImageNameHelpHover"];
  358. // [weakSelf showHelpTip:button];
  359. } else if (action == KMCoverActionExit) {
  360. button.image = [NSImage imageNamed:@"KMImageNameHelpNormal"];
  361. // [weakSelf dismissHelpTip];
  362. }
  363. };
  364. [self localizedString];
  365. [self setupUI];
  366. }
  367. - (void)setupUI {
  368. NSString *fontName = @"SFProText-Regular";
  369. self.titleLabel.font = [NSFont fontWithName:@"SFProText-Semibold" size:16];
  370. self.titleLabel.textColor = [NSColor colorWithRed:37/255.f green:38/255.f blue:41/255.f alpha:1.f];
  371. self.signTypeView.wantsLayer = YES;
  372. self.signTypeView.layer.backgroundColor = [NSColor colorWithRed:223/255. green:225/255. blue:229/255. alpha:1.].CGColor;
  373. self.signTypeView.layer.cornerRadius = 4.;
  374. self.signTypeView.layer.masksToBounds = YES;
  375. self.signTypeBackView.wantsLayer = YES;
  376. self.signTypeBackView.layer.cornerRadius = 2.;
  377. self.signTypeBackView.layer.masksToBounds = YES;
  378. self.signTypeBackView.layer.backgroundColor = [NSColor whiteColor].CGColor;
  379. for (NSButton *button in @[self.inputButton, self.drawingButton, self.pictureButton]) {
  380. [button setTitleColor:[NSColor colorWithRed:37/255.f green:38/255.f blue:41/255.f alpha:1.f]];
  381. button.font = [NSFont fontWithName:fontName size:12];
  382. }
  383. // Input
  384. self.fontVC = [[KMDesignSelect alloc] initWithType:SelectTypeCombox];
  385. [self.fontBox.superview addSubview:self.fontVC.view];
  386. [self.fontVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
  387. make.left.equalTo(self.fontBox);
  388. make.centerY.equalTo(self.fontBox);
  389. make.size.mas_equalTo(NSMakeSize(200, 32));
  390. }];
  391. self.fontBox.hidden = YES;
  392. [self.fontVC addItemsWithObjectValues:self.fontValues];
  393. self.fontVC.stringValue = self.fontDefaultValue;
  394. self.fontVC.delete = self;
  395. // Drawing
  396. self.drawSizeVC = [[KMDesignSelect alloc] initWithType:SelectTypeCombox];
  397. [self.drawSizeBox.superview addSubview:self.drawSizeVC.view];
  398. [self.drawSizeVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
  399. make.left.equalTo(self.drawSizeBox);
  400. make.centerY.equalTo(self.drawSizeBox);
  401. make.size.mas_equalTo(NSMakeSize(85, 32));
  402. }];
  403. self.drawSizeBox.hidden = YES;
  404. self.drawSizeVC.delete = self;
  405. NSMutableArray *drawSizes = [NSMutableArray array];
  406. for (NSString *value in self.drawSizeValues) {
  407. [drawSizes addObject:[NSString stringWithFormat:@"%@ pt", value]];
  408. }
  409. [self.drawSizeVC addItemsWithObjectValues:drawSizes.copy];
  410. self.drawSizeVC.stringValue = [NSString stringWithFormat:@"%@ pt", self.drawSizeDefaultValue];
  411. self.trackpadButton.title = NSLocalizedString(@"Trackpad", nil);
  412. self.trackpadButtonVC = [[KMDesignButton alloc] initWithType:DesignButtonTypeCheckBox];
  413. [self.trackpadButton.superview addSubview:self.trackpadButtonVC.view];
  414. self.trackpadButtonVC.stringValue = NSLocalizedString(@"Trackpad", nil);
  415. self.trackpadButtonVC.state = KMDesignTokenStateNorm;
  416. [self.trackpadButtonVC checkbox_radioWithImageHeight:[[NSLayoutConstraint alloc] init]];
  417. self.trackpadButtonVC.target = self;
  418. self.trackpadButtonVC.action = @selector(trackpadButton_Click:);
  419. [self.trackpadButtonVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
  420. make.left.equalTo(self.trackpadButton);
  421. make.centerY.equalTo(self.drawSizeVC.view);
  422. make.width.mas_equalTo(120);
  423. make.height.mas_equalTo(22);
  424. }];
  425. self.trackpadButton.hidden = YES;
  426. self.drawTipView.wantsLayer = YES;
  427. self.drawTipView.layer.backgroundColor = [NSColor colorWithRed:189/255.f green:223/255.f blue:253/255.f alpha:1.f].CGColor;
  428. self.drawTipLabel.stringValue = NSLocalizedString(@"Press any key to disable the touchpad", nil);
  429. self.drawTipLabel.textColor = [NSColor colorWithRed:37/255.f green:38/255.f blue:41/255.f alpha:1.f];
  430. self.drawTipLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:14];
  431. // Picture
  432. self.pictureBackView.emptyTipLbl.textColor = [NSColor colorWithRed:148/255.f green:152/255.f blue:156/255.f alpha:1];
  433. self.pictureBackView.emptyTipLbl.font = [NSFont fontWithName:fontName size:14];
  434. self.pictureClearBackBtn.title = NSLocalizedString(@"Clear background", nil);
  435. self.pictureClearBackBtnVC = [[KMDesignButton alloc] initWithType:DesignButtonTypeCheckBox];
  436. [self.pictureClearBackBtn.superview addSubview:self.pictureClearBackBtnVC.view];
  437. self.pictureClearBackBtnVC.stringValue = NSLocalizedString(@"Clear background", nil);
  438. self.pictureClearBackBtnVC.state = KMDesignTokenStateNorm;
  439. [self.pictureClearBackBtnVC checkbox_radioWithImageHeight:[[NSLayoutConstraint alloc] init]];
  440. self.pictureClearBackBtnVC.target = self;
  441. self.pictureClearBackBtnVC.action = @selector(pictureClearBackBtnAction:);
  442. self.pictureClearBackBtn.hidden = YES;
  443. [self.pictureClearBackBtnVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
  444. make.edges.equalTo(self.pictureClearBackBtn);
  445. }];
  446. }
  447. - (void)setType:(kKMPDFSignatureType)type {
  448. _type = type;
  449. CGRect rect = self.signTypeBackView.frame;
  450. if (_type == kKMPDFSignatureType_Text) {
  451. rect.origin.x = 1;
  452. } else if (_type == kKMPDFSignatureType_Ink) {
  453. rect.origin.x = 2 + 75;
  454. } else if (_type == kKMPDFSignatureType_Image) {
  455. rect.origin.x = 2 + 75*2;
  456. }
  457. rect.origin.y = (CGRectGetHeight(self.signTypeBackView.superview.frame) - CGRectGetHeight(self.signTypeBackView.frame))/2.;
  458. self.signTypeBackView.frame = rect;
  459. self.clearButton.hidden = YES;
  460. if (_type == kKMPDFSignatureType_Text) {
  461. } else if (_type == kKMPDFSignatureType_Ink) {
  462. } else if (_type == kKMPDFSignatureType_Image) {
  463. if (self.pictureBackView.signatureImage) {
  464. self.clearButton.title = NSLocalizedString(@"Reselect", nil);
  465. self.clearButton.hidden = NO;
  466. }
  467. }
  468. }
  469. #pragma mark - mouse
  470. - (void)mouseDown:(NSEvent *)event {
  471. [self dismissHelpTip];
  472. }
  473. #pragma mark - Private Methods
  474. -(void)localizedString
  475. {
  476. self.titleLabel.stringValue = NSLocalizedString(@"Create Signature", "");
  477. [self.trackpadButton setTitle:NSLocalizedString(@"Trackpad", nil)];
  478. [self.clearButton setTitle:NSLocalizedString(@"Clear", nil)];
  479. [self.trackpadLabel setStringValue:NSLocalizedString(@"Press \"esc\" to disable the Trackpad.", nil)];
  480. NSArray *seletorFonts = @[@"Mistral", @"Bradley Hand", @"Brush Script MT", @"SignPainter", @"Edwardian Script ITC", @"American Typewriter",@"Baoli SC",@"Snell Roundhand", @"Apple Chancery", @"Monotype Corsiva"];
  481. NSArray *fonts = [[NSFontManager sharedFontManager] availableFontFamilies];
  482. for (NSString *fontName in fonts) {
  483. NSDictionary *attrited = @{NSFontAttributeName:[NSFont fontWithName:fontName size:12.0]};
  484. NSAttributedString *string = [[NSAttributedString alloc] initWithString:fontName attributes:attrited];
  485. NSMenuItem *item = [[NSMenuItem alloc] init];
  486. item.attributedTitle = string;
  487. [self.fontBox.menu addItem:item];
  488. }
  489. NSString * fontName = nil;
  490. if (!recentlyFonts) {
  491. recentlyFonts = [[NSMutableArray alloc] init];
  492. }
  493. if (recentlyFonts.count < 1) {
  494. for (NSString *name in seletorFonts) {
  495. if ([fonts containsObject:name]) {
  496. fontName = name;
  497. break;
  498. }
  499. }
  500. if (fontName) {
  501. [recentlyFonts addObject:fontName];
  502. }
  503. } else {
  504. fontName = recentlyFonts.firstObject;
  505. }
  506. // if ([fonts containsObject:fontName]) {
  507. // self.keyboardView.fontName = fontName;
  508. // [self.fontBox setTitle:fontName];
  509. // } else {
  510. // self.keyboardView.fontName = fonts.firstObject;
  511. // [self.fontBox setTitle:fonts.firstObject];
  512. // }
  513. self.keyboardView.fontName = self.fontDefaultValue;
  514. for (NSInteger i = 0; i<recentlyFonts.count; i++) {
  515. NSString *fontName = recentlyFonts[i];
  516. NSDictionary *attrited = @{NSFontAttributeName:[NSFont fontWithName:fontName size:12.0]};
  517. NSAttributedString *string = [[NSAttributedString alloc] initWithString:fontName attributes:attrited];
  518. NSMenuItem *item = [[NSMenuItem alloc] init];
  519. item.attributedTitle = string;
  520. [self.fontBox.menu insertItem:item atIndex:1 + i];
  521. }
  522. NSMenuItem *sep = [NSMenuItem separatorItem];
  523. [self.fontBox.menu insertItem:sep atIndex:(recentlyFonts.count)+1];
  524. self.selectItem = [self.fontBox.menu itemAtIndex:1];
  525. self.selectItem.state = NSControlStateValueOn;
  526. [self.inputButton setTitle:NSLocalizedString(@"Keyboard", nil)];
  527. [self.drawingButton setTitle:NSLocalizedString(@"Trackpad", nil)];
  528. [self.pictureButton setTitle:NSLocalizedString(@"Image", nil)];
  529. }
  530. - (void)showHelpTip:(NSButton *)sender {
  531. if (self.popover) {
  532. [self dismissHelpTip];
  533. return;
  534. }
  535. NSPopover *pop = [[NSPopover alloc] init];
  536. self.popover = pop;
  537. KMSignatureHelpViewController *controller = [[KMSignatureHelpViewController alloc] init];
  538. pop.contentViewController = controller;
  539. controller.tipString = NSLocalizedString(@"Remove white background from images", nil);
  540. [pop setValue:@YES forKey:@"shouldHideAnchor"];
  541. [pop showRelativeToRect:NSMakeRect(0, -8, NSWidth(sender.bounds), NSHeight(sender.bounds)) ofView:sender preferredEdge:NSRectEdgeMaxY];
  542. }
  543. - (void)dismissHelpTip {
  544. [self.popover close];
  545. self.popover = nil;
  546. }
  547. #pragma mark Button Mehtods
  548. - (IBAction)inputButton_Click:(id)sender {
  549. self.contentBox.contentView = self.inputView;
  550. self.type = kKMPDFSignatureType_Text;
  551. }
  552. - (IBAction)drawingButton_Click:(id)sender {
  553. self.contentBox.contentView = self.drawingView;
  554. self.type = kKMPDFSignatureType_Ink;
  555. }
  556. - (IBAction)pictureButton_Click:(id)sender {
  557. self.contentBox.contentView = self.pictureView;
  558. self.type = kKMPDFSignatureType_Image;
  559. }
  560. - (void)clearButton_Click:(id)sender
  561. {
  562. if (kKMPDFSignatureType_Image == self.type) {
  563. [self.pictureBackView reSelectImage];
  564. } else if (kKMPDFSignatureType_Ink == self.type) {
  565. [self.drawView clearImage];
  566. }else if (kKMPDFSignatureType_Text == self.type) {
  567. [self.keyboardView clearImage];
  568. }
  569. self.applyButtonVC.enabled = NO;
  570. }
  571. - (void)applyButton_Click:(id)sender
  572. {
  573. KMSignature *signature = [[KMSignature alloc] init];
  574. if (kKMPDFSignatureType_Text == self.type) {
  575. NSImage *image = [self.keyboardView signatureImage];
  576. if (!image) {
  577. NSAlert *alert = [[NSAlert alloc] init];
  578. [alert setAlertStyle:NSAlertStyleCritical];
  579. [alert setMessageText:NSLocalizedString(@"Unable to add new signatures. Please try again.",nil)];
  580. [alert runModal];
  581. return;
  582. }
  583. signature.pathsImage = image;
  584. signature.signatureType = kKMPDFSignatureType_Text;
  585. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  586. [signatureManager loadAllSignatureList];
  587. [signatureManager addSignature:signature];
  588. [signatureManager saveSingaturesToFile];
  589. } else if(kKMPDFSignatureType_Image == self.type) {
  590. NSImage *image = [self.pictureBackView signatureImage];
  591. if (!image) {
  592. NSAlert *alert = [[NSAlert alloc] init];
  593. [alert setAlertStyle:NSAlertStyleCritical];
  594. [alert setMessageText:NSLocalizedString(@"Unable to add new signatures. Please try again.",nil)];
  595. [alert runModal];
  596. return;
  597. }
  598. signature.pathsImage = image;
  599. signature.signatureType = kKMPDFSignatureType_Image;
  600. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  601. [signatureManager loadAllSignatureList];
  602. [signatureManager addSignature:signature];
  603. [signatureManager saveSingaturesToFile];
  604. } else {
  605. NSImage *image = [self.drawView signatureImage];
  606. if (!image) {
  607. NSAlert *alert = [[NSAlert alloc] init];
  608. [alert setAlertStyle:NSAlertStyleCritical];
  609. [alert setMessageText:NSLocalizedString(@"Unable to add new signatures. Please try again.",nil)];
  610. [alert runModal];
  611. return;
  612. }
  613. [signature addPath:self.drawView.drawBezierPath];
  614. signature.signatureType = kKMPDFSignatureType_Ink;
  615. signature.signatureColor = self.drawView.drawColor;
  616. signature.pathsImage = image;
  617. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  618. [signatureManager loadAllSignatureList];
  619. [signatureManager addSignature:signature];
  620. [signatureManager saveSingaturesToFile];
  621. }
  622. self.selectedSignature = signature;
  623. [self dismissSheet:nil];
  624. }
  625. - (IBAction)trackpadButton_Click:(id)sender
  626. {
  627. if (self.trackpadButtonVC.state == KMDesignTokenStateChecked) {
  628. self.trackpadButtonVC.state = KMDesignTokenStateNorm;
  629. self.trackpadLabel.hidden = YES;
  630. self.drawTipView.hidden = YES;
  631. self.drawView.isAcceptsTouch = NO;
  632. } else {
  633. self.trackpadButtonVC.state = KMDesignTokenStateChecked;
  634. self.trackpadLabel.hidden = YES;
  635. self.drawTipView.hidden = NO;
  636. self.drawView.isAcceptsTouch = YES;
  637. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  638. self.drawTipView.hidden = YES;
  639. });
  640. }
  641. }
  642. - (IBAction)boxItemClicked_Font:(id)sender
  643. {
  644. NSString * name = self.fontBox.selectedItem.title;
  645. self.fontBox.title = name;
  646. self.keyboardView.fontName = name;
  647. if ([recentlyFonts containsObject:name]) {
  648. NSInteger index = [recentlyFonts indexOfObject:name];
  649. [recentlyFonts removeObject:name];
  650. [self.fontBox.menu removeItemAtIndex:index +1];
  651. }
  652. if (recentlyFonts.count >0) {
  653. [recentlyFonts insertObject:name atIndex:0];
  654. } else {
  655. [recentlyFonts addObject:name];
  656. }
  657. if (recentlyFonts.count >5) {
  658. [recentlyFonts removeLastObject];
  659. [self.fontBox.menu removeItemAtIndex:recentlyFonts.count];
  660. }
  661. NSDictionary *attrited = @{NSFontAttributeName:[NSFont fontWithName:name size:12.0]};
  662. NSAttributedString *string = [[NSAttributedString alloc] initWithString:name attributes:attrited];
  663. NSMenuItem *item = [[NSMenuItem alloc] init];
  664. item.attributedTitle = string;
  665. [self.fontBox.menu insertItem:item atIndex:1];
  666. self.selectItem.state = NSControlStateValueOff;
  667. self.selectItem = [self.fontBox.menu itemAtIndex:1];
  668. self.selectItem.state = NSControlStateValueOn;
  669. }
  670. - (IBAction)colorTextButtonAction:(id)sender {
  671. if ([sender isEqual:self.keyboardColorBtn1] ||
  672. [sender isEqual:self.keyboardColorBtn2] ||
  673. [sender isEqual:self.keyboardColorBtn3] ||
  674. [sender isEqual:self.keyboardColorBtn4]) {
  675. self.keyboardView.keyboardColor = [NSColor colorWithCGColor:((NSButton *)sender).layer.backgroundColor];
  676. CGRect rect = self.keyboardView.frame;
  677. rect.origin.y = CGRectGetMidY(((NSButton *)sender).frame)-16;
  678. rect.origin.x = CGRectGetMidX(((NSButton *)sender).frame)-16;
  679. rect.size.width = rect.size.height = 32;
  680. self.keyboardColorSelView.frame = rect;
  681. } else {
  682. [[NSColorPanel sharedColorPanel] setTarget:self];
  683. [[NSColorPanel sharedColorPanel] setAction:@selector(keyboardColorPanelColorDidChange:)];
  684. [[NSColorPanel sharedColorPanel] orderFront:nil];
  685. }
  686. }
  687. - (IBAction)drawColorBtnClicked:(NSButton *)sender {
  688. if ([sender isEqual:self.drawColorBtn1] ||
  689. [sender isEqual:self.drawColorBtn2] ||
  690. [sender isEqual:self.drawColorBtn3] ||
  691. [sender isEqual:self.drawColorBtn4]) {
  692. self.drawView.drawColor = [NSColor colorWithCGColor:((NSButton *)sender).layer.backgroundColor];
  693. CGRect rect = self.drawColorSelView.frame;
  694. rect.origin.y = CGRectGetMidY(((NSButton *)sender).frame)-16;
  695. rect.origin.x = CGRectGetMidX(((NSButton *)sender).frame)-16;
  696. rect.size.width = rect.size.height = 32;
  697. self.drawColorSelView.frame = rect;
  698. }
  699. }
  700. - (IBAction)drawSizeBtnClicked:(NSPopUpButton *)sender {
  701. NSString * name = self.drawSizeBox.selectedItem.title;
  702. self.drawSizeBox.title = name;
  703. self.drawView.strokeRadius = [[@[@"0.5",@"1.0",@"1.5",@"2.0",@"2.5",@"3.0"] objectAtIndex:(self.drawSizeBox.indexOfSelectedItem -1)] floatValue];
  704. }
  705. //Picture
  706. - (IBAction)pictureClearBackBtnAction:(NSButton *)sender {
  707. BOOL clearBack = YES;
  708. if (self.pictureClearBackBtnVC.state == KMDesignTokenStateChecked) {
  709. self.pictureClearBackBtnVC.state = KMDesignTokenStateNorm;
  710. clearBack = NO;
  711. } else {
  712. self.pictureClearBackBtnVC.state = KMDesignTokenStateChecked;
  713. clearBack = YES;
  714. }
  715. self.pictureBackView.clearBackground = clearBack;
  716. }
  717. - (void)keyboardColorPanelColorDidChange:(id)sender
  718. {
  719. self.textColorButton.layer.borderColor= [NSColor clearColor].CGColor;
  720. CGFloat red,green,blue,alpha;
  721. NSColor *color = [NSColorPanel sharedColorPanel].color ? : [NSColor clearColor];
  722. [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
  723. self.keyboardView.keyboardColor = color;
  724. self.textColorButton.layer.borderColor = [NSColor colorWithRed:33.0/255.0 green:124.0/255.0 blue:234.0/255.0 alpha:1.0].CGColor;
  725. }
  726. #pragma mark KMDrawViewDelegate Methods
  727. - (void)drawViewDidFinishTouchMode:(KMDrawView *)view
  728. {
  729. self.trackpadButtonVC.state = KMDesignTokenStateNorm;
  730. [self.trackpadLabel setHidden:YES];
  731. // [NSAnimationContext beginGrouping];
  732. // [NSAnimationContext currentContext].duration = 1.5;
  733. // [NSAnimationContext currentContext].timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  734. // self.scrollView.animator.frame = originFrame;
  735. self.drawTipView.animator.hidden = YES;
  736. // [NSAnimationContext endGrouping];
  737. }
  738. #pragma mark KMChangeSignatureTextDelegate Methods
  739. - (void)changeSignatureText:(BOOL)isTure {
  740. self.applyButtonVC.enabled = isTure;
  741. }
  742. #pragma mark Show Methods
  743. - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
  744. {
  745. if (contextInfo != NULL && self.handler) {
  746. self.handler(self.selectedSignature);
  747. }
  748. }
  749. - (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void (^)(KMSignature *signature))handler;
  750. {
  751. windowController_signature = self;
  752. [NSApp beginSheet:[self window]
  753. modalForWindow:window
  754. modalDelegate:self
  755. didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
  756. contextInfo:(__bridge void * _Null_unspecified)(handler ? handler : NULL)];
  757. self.handler = handler;
  758. }
  759. - (void)dismissSheet:(id)sender {
  760. windowController_signature = nil;
  761. if (sender) {
  762. [NSApp endSheet:[self window] returnCode:0];
  763. } else {
  764. [NSApp endSheet:[self window] returnCode:1];
  765. }
  766. [[self window] orderOut:self];
  767. }
  768. #pragma mark -
  769. #pragma mark - KMSelectPopButtonDelegate
  770. - (void)km_comboBoxSelectionDidChange:(KMDesignSelect *)obj {
  771. if ([obj isEqual:self.fontVC]) {
  772. NSInteger index = self.fontVC.indexOfSelectedItem;
  773. if (index < 0) {
  774. index = 0;
  775. }
  776. self.keyboardView.fontName = self.fontVC.items[index];
  777. } else if ([obj isEqual:self.drawSizeVC]) {
  778. NSInteger index = self.drawSizeVC.indexOfSelectedItem;
  779. if (index < 0) {
  780. index = 0;
  781. }
  782. self.drawView.strokeRadius = [self.drawSizeValues[index] floatValue];
  783. }
  784. }
  785. #pragma mark NSTabViewDelegate Methods
  786. - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
  787. {
  788. return YES;
  789. }
  790. - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
  791. {
  792. BOOL isImage = NO;
  793. if ([tabView indexOfTabViewItem:tabViewItem] == 0) {
  794. self.type = kKMPDFSignatureType_Text;
  795. if ([self.keyboardView signatureImage]) {
  796. isImage = YES;
  797. }
  798. } else if ([tabView indexOfTabViewItem:tabViewItem] == 1) {
  799. self.type = kKMPDFSignatureType_Ink;
  800. if ([self.drawView signatureImage]) {
  801. isImage = YES;
  802. }
  803. } else if ([tabView indexOfTabViewItem:tabViewItem] == 2) {
  804. self.type = kKMPDFSignatureType_Image;
  805. if ([self.pictureBackView signatureImage]) {
  806. isImage = YES;
  807. }
  808. }
  809. if (isImage) {
  810. self.applyButtonVC.enabled = YES;
  811. } else {
  812. self.applyButtonVC.enabled = NO;
  813. }
  814. }
  815. @end