KMSignatureWindowController.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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 "NSImage+PDFListView.h"
  10. #import "NSMenu_SKExtensions.h"
  11. #import <PDF_Reader_Pro-Swift.h>
  12. static NSMutableArray * recentlyFonts;
  13. #pragma mark KMSignatureColorButton
  14. @interface KMSignatureColorButton : NSButton
  15. @property (nonatomic, retain) NSColor *circleColor;
  16. @property (nonatomic, retain) NSImage *drawImage;
  17. @end
  18. @implementation KMSignatureColorButton
  19. - (void)dealloc {
  20. }
  21. //- (void)setCircleColor:(NSColor *)circleColor {
  22. // if (_circleColor != circleColor) {
  23. // _circleColor = circleColor;
  24. // }
  25. // [self setNeedsDisplay:YES];
  26. //}
  27. - (id)initWithCoder:(NSCoder *)coder {
  28. self = [super initWithCoder:coder];
  29. if (self) {
  30. self.wantsLayer = YES;
  31. self.layer.cornerRadius = 12;
  32. self.layer.masksToBounds = YES;
  33. self.layer.borderWidth = 1.5;
  34. }
  35. return self;
  36. }
  37. - (void)drawRect:(NSRect)rect {
  38. [super drawRect:rect];
  39. if (_circleColor) {
  40. NSBezierPath *path3 = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(3, 3, rect.size.width - 6, rect.size.height - 6)];
  41. [self.circleColor set];
  42. [path3 fill];
  43. } else if (self.drawImage) {
  44. [self.drawImage drawInRect:CGRectMake(3,3,rect.size.width -6,rect.size.height - 6)
  45. fromRect:NSZeroRect
  46. operation:NSCompositingOperationSourceOver
  47. fraction:1.0];
  48. }
  49. }
  50. @end
  51. #pragma mark KMSignatureButton
  52. @interface KMSignatureButton : NSButton
  53. @end
  54. @implementation KMSignatureButton
  55. - (NSMenu *)menuForEvent:(NSEvent *)theEvent {
  56. NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
  57. NSMenuItem *item = [menu addItemWithTitle:NSLocalizedString(@"Delete", nil) action:@selector(delete) keyEquivalent:@""];
  58. item.target = self;
  59. item = [menu addItemWithTitle:NSLocalizedString(@"Export", @"Menu item title") action:nil keyEquivalent:@""];
  60. NSMenu * tSubMenu = [NSMenu menu];
  61. NSMenuItem *tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PNG", @"Menu item title") action:@selector(export:) target:self atIndex:0];
  62. tMenuItem.tag = 0;
  63. tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"JPG", @"Menu item title") action:@selector(export:) target:self atIndex:1];
  64. tMenuItem.tag = 1;
  65. tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PDF", @"Menu item title") action:@selector(export:) target:self atIndex:2];
  66. tMenuItem.tag = 2;
  67. item.submenu = tSubMenu;
  68. return menu;
  69. }
  70. - (void)delete {
  71. [[NSNotificationCenter defaultCenter] postNotificationName:@"kKMSignatureDeleteNotification" object:[NSNumber numberWithInteger:self.tag]];
  72. }
  73. - (void)export:(NSMenuItem *)sender {
  74. NSInteger index = self.tag;
  75. NSInteger type = sender.tag;
  76. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  77. [signatureManager loadAllSignatureList];
  78. KMSignature *signature = signatureManager.signatureList[index];
  79. NSImage *image = signature.pathsImage;
  80. if (type == 0) {
  81. NSData *data = image.TIFFRepresentation;
  82. NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
  83. [imageRep setSize:image.size];
  84. NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
  85. NSSavePanel *savePanel = [NSSavePanel savePanel];
  86. savePanel.allowedFileTypes = @[@"png"];
  87. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  88. if (result) {
  89. if ([imageData writeToURL:savePanel.URL atomically:YES]) {
  90. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  91. inFileViewerRootedAtPath:@""];
  92. }
  93. }
  94. }];
  95. } else if (type == 1) {
  96. NSData *data = image.TIFFRepresentation;
  97. NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
  98. [imageRep setSize:image.size];
  99. NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypeJPEG properties:@{}];
  100. NSSavePanel *savePanel = [NSSavePanel savePanel];
  101. savePanel.allowedFileTypes = @[@"jpg"];
  102. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  103. if (result) {
  104. if ([imageData writeToURL:savePanel.URL atomically:YES]) {
  105. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  106. inFileViewerRootedAtPath:@""];
  107. }
  108. }
  109. }];
  110. } else {
  111. PDFDocument *pdf = [[PDFDocument alloc] init];
  112. PDFPage *page = [[PDFPage alloc] initWithImage:image];
  113. [pdf insertPage:page atIndex:pdf.pageCount];
  114. NSSavePanel *savePanel = [NSSavePanel savePanel];
  115. savePanel.allowedFileTypes = @[@"pdf"];
  116. [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  117. if (result) {
  118. if ([pdf writeToURL:savePanel.URL]) {
  119. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  120. inFileViewerRootedAtPath:@""];
  121. }
  122. }
  123. }];
  124. }
  125. }
  126. @end
  127. #pragma mark KMSignatureWindowController
  128. @interface KMSignatureWindowController () <NSTabViewDelegate,KMDrawViewSignatureDelegate,KMChangeSignatureTextDelegate>
  129. @property (nonatomic) IBOutlet NSTabView *creatTabview;
  130. @property (nonatomic) IBOutlet NSButton *cancelBtton;
  131. @property (nonatomic) IBOutlet NSButton *applyButton;
  132. @property (nonatomic) IBOutlet NSButton *clearButton;
  133. @property (nonatomic) IBOutlet KMPDFSignatureTextView *keyboardView;
  134. @property (nonatomic) IBOutlet NSPopUpButton *fontBox;
  135. @property (nonatomic) IBOutlet KMSignatureColorButton *textBlackColorButton;
  136. @property (nonatomic) IBOutlet KMSignatureColorButton *textBlueColorButton;
  137. @property (nonatomic) IBOutlet KMSignatureColorButton *textPurpleColorButton;
  138. @property (nonatomic) IBOutlet KMSignatureColorButton *textGreenColorButton;
  139. @property (nonatomic) IBOutlet KMSignatureColorButton *textOrangeColorButton;
  140. @property (nonatomic) IBOutlet KMSignatureColorButton *textRedColorButton;
  141. @property (nonatomic,assign) IBOutlet KMSignatureColorButton *textColorButton;
  142. @property (nonatomic) IBOutlet KMDrawSignatureView *drawView;
  143. @property (nonatomic) IBOutlet NSButton *trackpadButton;
  144. @property (assign) IBOutlet NSSlider *slider;
  145. @property (nonatomic) IBOutlet KMSignatureColorButton *mouseBlackColorButton;
  146. @property (nonatomic) IBOutlet KMSignatureColorButton *mouseBlueColorButton;
  147. @property (nonatomic) IBOutlet KMSignatureColorButton *mousePurpleColorButton;
  148. @property (nonatomic) IBOutlet KMSignatureColorButton *mouseGreenColorButton;
  149. @property (nonatomic) IBOutlet KMSignatureColorButton *mouseOrangeColorButton;
  150. @property (nonatomic) IBOutlet KMSignatureColorButton *mouseRedColorButton;
  151. @property (nonatomic, assign) IBOutlet KMSignatureColorButton *mouseColorButton;
  152. @property (nonatomic) IBOutlet KMPDFSignatureImageView *pictureBackView;
  153. @property (nonatomic) IBOutlet NSTextField *trackpadLabel;
  154. @property (assign) IBOutlet NSTextField *widthSizeTextField;
  155. @property (nonatomic, retain) KMSignature *selectedSignature;
  156. @property (nonatomic, retain) NSMenuItem *selectItem;
  157. @property (nonatomic, assign) KMPDFSignatureType type;
  158. @end
  159. @implementation KMSignatureWindowController
  160. - (id)init {
  161. if (self = [super initWithWindowNibName:@"KMSignatureWindowController"]) {
  162. }
  163. return self;
  164. }
  165. - (void)dealloc {
  166. _creatTabview.delegate = nil;
  167. _drawView.delegate = nil;
  168. _keyboardView.delegate = nil;
  169. [[NSNotificationCenter defaultCenter] removeObserver:self];
  170. [NSDistributedNotificationCenter.defaultCenter removeObserver:self];
  171. [[NSColorPanel sharedColorPanel] setTarget:nil];
  172. [[NSColorPanel sharedColorPanel] setAction:nil];
  173. }
  174. #pragma mark - View Methods
  175. - (void)windowDidLoad {
  176. [super windowDidLoad];
  177. self.type = KMPDFSignatureTypeText;
  178. self.textBlackColorButton.circleColor = self.mouseBlackColorButton.circleColor = [NSColor blackColor];
  179. self.textRedColorButton.circleColor =
  180. self.mouseRedColorButton.circleColor = [NSColor colorWithRed:221.0f/255.0f green:2.0f/255.0f blue:2.0f/255.0f alpha:1.0f];
  181. self.textBlueColorButton.circleColor =
  182. self.mouseBlueColorButton.circleColor = [NSColor colorWithRed:0.0 green:52.0/255.0 blue:146.0/255.0 alpha:1.0];
  183. self.textGreenColorButton.circleColor =
  184. self.mouseGreenColorButton.circleColor = [NSColor colorWithRed:39.0f/255.0f green:190.0f/255.0f blue:253.0f/255.0f alpha:1.0f];
  185. self.textOrangeColorButton.circleColor =
  186. self.mouseOrangeColorButton.circleColor = [NSColor colorWithRed:253.0f/255.0f green:126.0f/255.0f blue:21.0f/255.0f alpha:1.0f];
  187. self.textPurpleColorButton.circleColor =
  188. self.mousePurpleColorButton.circleColor = [NSColor colorWithRed:100.0/255.0 green:13.0/255.0 blue:168.0/255.0 alpha:1.0];
  189. self.textColorButton.drawImage = self.mouseColorButton.drawImage = [NSImage imageNamed:@"view_color"];
  190. self.widthSizeTextField.backgroundColor = [KMAppearance KMColor_Layout_L1];
  191. self.widthSizeTextField.wantsLayer = YES;
  192. self.widthSizeTextField.layer.borderWidth = 0;
  193. [self localizedString];
  194. [self.cancelBtton setTarget:self];
  195. [self.cancelBtton setAction:@selector(dismissSheet:)];
  196. [self.clearButton setTarget:self];
  197. [self.clearButton setAction:@selector(clearButton_Click:)];
  198. self.applyButton.enabled = NO;
  199. [self.applyButton setTarget:self];
  200. [self.applyButton setAction:@selector(applyButton_Click:)];
  201. self.keyboardView.wantsLayer = YES;
  202. self.drawView.delegate = self;
  203. self.drawView.wantsLayer = YES;
  204. self.drawView.layer.masksToBounds = YES;
  205. self.pictureBackView.wantsLayer = YES;
  206. [self.creatTabview selectFirstTabViewItem:nil];
  207. [self.trackpadLabel setHidden:YES];
  208. _slider.floatValue = self.drawView.strokeRadius;
  209. _widthSizeTextField.stringValue = [NSString stringWithFormat:@"%.1f",self.drawView.strokeRadius];
  210. __block typeof(self) blockSelf = self;
  211. self.keyboardView.delegate = self;
  212. self.drawView.changeDrawCallback = ^(BOOL isTure) {
  213. if (isTure) {
  214. blockSelf.applyButton.enabled = YES;
  215. } else {
  216. blockSelf.applyButton.enabled = NO;
  217. }
  218. };
  219. self.pictureBackView.changeSignatureImageCallback = ^(BOOL isTure) {
  220. if (isTure) {
  221. blockSelf.applyButton.enabled = YES;
  222. } else {
  223. blockSelf.applyButton.enabled = NO;
  224. }
  225. };
  226. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(widthSizeTextFieldNotification:) name:NSControlTextDidChangeNotification object:_widthSizeTextField];
  227. [NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(themeChanged:) name:@"AppleInterfaceThemeChangedNotification" object: nil];
  228. [self updateViewColor];
  229. }
  230. #pragma mark - Private Methods
  231. - (void)localizedString {
  232. self.creatTabview.delegate = self;
  233. [self.trackpadButton setTitle:NSLocalizedString(@"Trackpad", nil)];
  234. [self.cancelBtton setTitle:NSLocalizedString(@"Cancel",nil)];
  235. [self.applyButton setTitle:NSLocalizedString(@"Save", nil)];
  236. [self.clearButton setTitle:NSLocalizedString(@"Clear", nil)];
  237. [self.trackpadLabel setStringValue:NSLocalizedString(@"Press \"esc\" to disable the Trackpad.", nil)];
  238. NSArray *seletorFonts = @[@"Mistral", @"Bradley Hand", @"Brush Script MT", @"SignPainter", @"Edwardian Script ITC", @"American Typewriter",@"Baoli SC",@"Snell Roundhand", @"Apple Chancery", @"Monotype Corsiva"];
  239. NSArray *fonts = [[NSFontManager sharedFontManager] availableFontFamilies];
  240. for (NSString *fontName in fonts) {
  241. NSDictionary *attrited = @{NSFontAttributeName:[NSFont fontWithName:fontName size:12.0]};
  242. NSAttributedString *string = [[NSAttributedString alloc] initWithString:fontName attributes:attrited];
  243. NSMenuItem *item = [[NSMenuItem alloc] init];
  244. item.attributedTitle = string;
  245. [self.fontBox.menu addItem:item];
  246. }
  247. NSString * fontName = nil;
  248. if (!recentlyFonts) {
  249. recentlyFonts = [[NSMutableArray alloc] init];
  250. }
  251. if (recentlyFonts.count < 1) {
  252. for (NSString *name in seletorFonts) {
  253. if ([fonts containsObject:name]) {
  254. fontName = name;
  255. break;
  256. }
  257. }
  258. if (fontName) {
  259. [recentlyFonts addObject:fontName];
  260. }
  261. } else {
  262. fontName = recentlyFonts.firstObject;
  263. }
  264. if ([fonts containsObject:fontName]) {
  265. self.keyboardView.fontName = fontName;
  266. [self.fontBox setTitle:fontName];
  267. } else {
  268. self.keyboardView.fontName = fonts.firstObject;
  269. [self.fontBox setTitle:fonts.firstObject];
  270. }
  271. for (NSUInteger i = 0; i<recentlyFonts.count; i++) {
  272. NSString *fontName = recentlyFonts[i];
  273. NSDictionary *attrited = @{NSFontAttributeName:[NSFont fontWithName:fontName size:12.0]};
  274. NSAttributedString *string = [[NSAttributedString alloc] initWithString:fontName attributes:attrited];
  275. NSMenuItem *item = [[NSMenuItem alloc] init];
  276. item.attributedTitle = string;
  277. [self.fontBox.menu insertItem:item atIndex:1 + i];
  278. }
  279. NSMenuItem *sep = [NSMenuItem separatorItem];
  280. [self.fontBox.menu insertItem:sep atIndex:(recentlyFonts.count)+1];
  281. self.selectItem= [self.fontBox.menu itemAtIndex:1];
  282. self.selectItem.state = NSControlStateValueOn;
  283. [[self.creatTabview tabViewItemAtIndex:0] setLabel:NSLocalizedString(@"Keyboard", nil)];
  284. [[self.creatTabview tabViewItemAtIndex:1] setLabel:NSLocalizedString(@"Trackpad", nil)];
  285. [[self.creatTabview tabViewItemAtIndex:2] setLabel:NSLocalizedString(@"Image", nil)];
  286. [self colorInkButtonAction:self.mouseBlackColorButton];
  287. [self colorTextButtonAction:self.textBlackColorButton];
  288. }
  289. - (void)updateViewColor {
  290. if ([KMAppearance isDarkMode]) {
  291. self.keyboardView.layer.backgroundColor = [NSColor colorWithRed:57/255. green:60/255. blue:62/255. alpha:1.].CGColor;
  292. self.drawView.layer.backgroundColor = [NSColor colorWithRed:57/255. green:60/255. blue:62/255. alpha:1.].CGColor;
  293. self.pictureBackView.layer.backgroundColor = [NSColor colorWithRed:57/255. green:60/255. blue:62/255. alpha:1.].CGColor;
  294. } else {
  295. self.keyboardView.layer.backgroundColor = [NSColor whiteColor].CGColor;
  296. self.drawView.layer.backgroundColor = [NSColor whiteColor].CGColor;
  297. self.pictureBackView.layer.backgroundColor = [NSColor whiteColor].CGColor;
  298. }
  299. }
  300. #pragma mark - Button Mehtods
  301. - (void)clearButton_Click:(id)sender {
  302. if (KMPDFSignatureTypeImage == self.type) {
  303. [self.pictureBackView clearImage];
  304. } else if (KMPDFSignatureTypeInk == self.type) {
  305. [self.drawView clearImage];
  306. } else if (KMPDFSignatureTypeText == self.type) {
  307. [self.keyboardView clearImage];
  308. }
  309. self.applyButton.enabled = NO;
  310. }
  311. - (NSImage*)changeColor:(NSColor*)color oldImage:(NSImage *)oldImage {
  312. NSImage *newImage = [oldImage copy];
  313. [newImage lockFocus];
  314. [color set];
  315. NSRect imageRect=NSMakeRect(0, 0, oldImage.size.width, oldImage.size.height);
  316. NSRectFillUsingOperation(imageRect, NSCompositingOperationSourceAtop);
  317. [newImage unlockFocus];
  318. return newImage;
  319. }
  320. - (void)applyButton_Click:(id)sender {
  321. KMSignature *signature = [[KMSignature alloc] init];
  322. if (KMPDFSignatureTypeText == self.type) {
  323. NSImage *image = [self.keyboardView signatureImage];
  324. if (!image) {
  325. NSAlert *alert = [[NSAlert alloc] init];
  326. [alert setAlertStyle:NSAlertStyleCritical];
  327. [alert setMessageText:NSLocalizedString(@"Unable to add new signatures. Please try again.",nil)];
  328. [alert runModal];
  329. return;
  330. }
  331. signature.pathsImage = image;
  332. signature.signatureType = KMPDFSignatureTypeText;
  333. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  334. [signatureManager loadAllSignatureList];
  335. [signatureManager addSignature:signature];
  336. [signatureManager saveSingaturesToFile];
  337. } else if(KMPDFSignatureTypeImage == self.type) {
  338. NSImage *image = [self.pictureBackView signatureImage];
  339. if (!image) {
  340. NSAlert *alert = [[NSAlert alloc] init];
  341. [alert setAlertStyle:NSAlertStyleCritical];
  342. [alert setMessageText:NSLocalizedString(@"Unable to add new signatures. Please try again.",nil)];
  343. [alert runModal];
  344. return;
  345. }
  346. signature.pathsImage = image;
  347. signature.signatureType = KMPDFSignatureTypeImage;
  348. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  349. [signatureManager loadAllSignatureList];
  350. [signatureManager addSignature:signature];
  351. [signatureManager saveSingaturesToFile];
  352. } else {
  353. NSImage *image = [self.drawView signatureImage];
  354. if (!image) {
  355. NSAlert *alert = [[NSAlert alloc] init];
  356. [alert setAlertStyle:NSAlertStyleCritical];
  357. [alert setMessageText:NSLocalizedString(@"Unable to add new signatures. Please try again.",nil)];
  358. [alert runModal];
  359. return;
  360. }
  361. [signature addPath:self.drawView.drawBezierPath];
  362. signature.signatureType = KMPDFSignatureTypeInk;
  363. signature.signatureColor = self.drawView.drawColor;
  364. signature.pathsImage = image;
  365. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  366. [signatureManager loadAllSignatureList];
  367. [signatureManager addSignature:signature];
  368. [signatureManager saveSingaturesToFile];
  369. }
  370. self.selectedSignature = signature;
  371. [self dismissSheet:nil];
  372. }
  373. - (IBAction)trackpadButton_Click:(id)sender {
  374. if (self.trackpadButton.cell.state) {
  375. [self.trackpadLabel setHidden:NO];
  376. self.drawView.isAcceptsTouch = YES;
  377. } else {
  378. [self.trackpadLabel setHidden:YES];
  379. self.drawView.isAcceptsTouch = NO;
  380. }
  381. }
  382. - (IBAction)boxItemClicked_Font:(id)sender {
  383. NSString * name = self.fontBox.selectedItem.title;
  384. self.fontBox.title = name;
  385. self.keyboardView.fontName = name;
  386. if ([recentlyFonts containsObject:name]) {
  387. NSInteger index = [recentlyFonts indexOfObject:name];
  388. [recentlyFonts removeObject:name];
  389. [self.fontBox.menu removeItemAtIndex:index +1];
  390. }
  391. if (recentlyFonts.count >0) {
  392. [recentlyFonts insertObject:name atIndex:0];
  393. } else {
  394. [recentlyFonts addObject:name];
  395. }
  396. if (recentlyFonts.count >5) {
  397. [recentlyFonts removeLastObject];
  398. [self.fontBox.menu removeItemAtIndex:recentlyFonts.count];
  399. }
  400. NSDictionary *attrited = @{NSFontAttributeName:[NSFont fontWithName:name size:12.0]};
  401. NSAttributedString *string = [[NSAttributedString alloc] initWithString:name attributes:attrited];
  402. NSMenuItem *item = [[NSMenuItem alloc] init];
  403. item.attributedTitle = string;
  404. [self.fontBox.menu insertItem:item atIndex:1];
  405. self.selectItem.state = NSControlStateValueOff;
  406. self.selectItem = [self.fontBox.menu itemAtIndex:1];
  407. self.selectItem.state = NSControlStateValueOn;
  408. }
  409. - (IBAction)slider_Change:(id)sender {
  410. self.drawView.strokeRadius = self.slider.floatValue;
  411. _widthSizeTextField.stringValue = [NSString stringWithFormat:@"%.1f",self.slider.floatValue];
  412. }
  413. - (IBAction)colorTextButtonAction:(KMSignatureColorButton *)sender {
  414. if (sender.tag != 6) {
  415. self.textBlackColorButton.layer.borderColor =
  416. self.textRedColorButton.layer.borderColor =
  417. self.textBlueColorButton.layer.borderColor =
  418. self.textGreenColorButton.layer.borderColor =
  419. self.textOrangeColorButton.layer.borderColor =
  420. self.textPurpleColorButton.layer.borderColor =
  421. self.textColorButton.layer.borderColor= [NSColor clearColor].CGColor;
  422. self.keyboardView.keyboardColor = sender.circleColor;
  423. sender.layer.borderColor = [KMAppearance KMColor_Interactive_A0].CGColor;
  424. } else {
  425. [[NSColorPanel sharedColorPanel] setTarget:self];
  426. [[NSColorPanel sharedColorPanel] setAction:@selector(keyboardColorPanelColorDidChange:)];
  427. [[NSColorPanel sharedColorPanel] orderFront:nil];
  428. }
  429. }
  430. - (IBAction)colorInkButtonAction:(KMSignatureColorButton *)sender {
  431. if (sender.tag != 6) {
  432. self.mouseBlackColorButton.layer.borderColor =
  433. self.mouseRedColorButton.layer.borderColor =
  434. self.mouseBlueColorButton.layer.borderColor =
  435. self.mouseGreenColorButton.layer.borderColor =
  436. self.mouseOrangeColorButton.layer.borderColor =
  437. self.mousePurpleColorButton.layer.borderColor =
  438. self.mouseColorButton.layer.borderColor = [NSColor clearColor].CGColor;
  439. sender.layer.borderColor = [KMAppearance KMColor_Interactive_A0].CGColor;
  440. self.drawView.drawColor = sender.circleColor;
  441. } else {
  442. [[NSColorPanel sharedColorPanel] setTarget:self];
  443. [[NSColorPanel sharedColorPanel] setAction:@selector(ColorPanelColorDidChange:)];
  444. [[NSColorPanel sharedColorPanel] orderFront:nil];
  445. }
  446. }
  447. - (void)ColorPanelColorDidChange:(id)sender {
  448. self.mouseBlackColorButton.layer.borderColor =
  449. self.mouseRedColorButton.layer.borderColor =
  450. self.mouseBlueColorButton.layer.borderColor =
  451. self.mouseGreenColorButton.layer.borderColor =
  452. self.mouseOrangeColorButton.layer.borderColor =
  453. self.mousePurpleColorButton.layer.borderColor =
  454. self.mouseColorButton.layer.borderColor = [NSColor clearColor].CGColor;
  455. CGFloat red,green,blue,alpha;
  456. NSColor *color = [(NSColorPanel*)sender color];
  457. [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
  458. self.drawView.drawColor = color;
  459. self.mouseColorButton.layer.borderColor = [NSColor colorWithRed:33.0/255.0 green:124.0/255.0 blue:234.0/255.0 alpha:1.0].CGColor;
  460. }
  461. - (void)keyboardColorPanelColorDidChange:(id)sender {
  462. self.textBlackColorButton.layer.borderColor =
  463. self.textRedColorButton.layer.borderColor =
  464. self.textBlueColorButton.layer.borderColor =
  465. self.textGreenColorButton.layer.borderColor =
  466. self.textOrangeColorButton.layer.borderColor =
  467. self.textPurpleColorButton.layer.borderColor =
  468. self.textColorButton.layer.borderColor= [NSColor clearColor].CGColor;
  469. CGFloat red,green,blue,alpha;
  470. NSColor *color = [NSColorPanel sharedColorPanel].color ? : [NSColor clearColor];
  471. [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
  472. self.keyboardView.keyboardColor = color;
  473. self.textColorButton.layer.borderColor = [NSColor colorWithRed:33.0/255.0 green:124.0/255.0 blue:234.0/255.0 alpha:1.0].CGColor;
  474. }
  475. #pragma mark - KMDrawSignatureViewDelegate Methods
  476. - (void)drawViewDidFinishTouchMode:(KMDrawSignatureView *)view {
  477. [self.trackpadButton.cell setState:0];
  478. [self.trackpadLabel setHidden:YES];
  479. }
  480. #pragma mark KMChangeSignatureTextDelegate Methods
  481. - (void)changeSignatureText:(BOOL)isTure {
  482. self.applyButton.enabled = isTure;
  483. }
  484. #pragma mark - show Methods
  485. - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
  486. if (contextInfo != NULL) {
  487. void (^handler)(KMSignature*) = (__bridge void(^)(KMSignature*))contextInfo;
  488. if (handler) {
  489. handler(self.selectedSignature);
  490. handler = nil;
  491. }
  492. }
  493. }
  494. - (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void (^)(KMSignature *signature))handler; {
  495. [NSApp beginSheet:[self window]
  496. modalForWindow:window
  497. modalDelegate:self
  498. didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
  499. contextInfo:(__bridge void * _Null_unspecified)(handler ? handler : NULL)];
  500. }
  501. - (void)dismissSheet:(id)sender {
  502. if (sender) {
  503. [NSApp endSheet:[self window] returnCode:0];
  504. } else {
  505. [NSApp endSheet:[self window] returnCode:1];
  506. }
  507. [[self window] orderOut:self];
  508. }
  509. #pragma mark - NSTabViewDelegate Methods
  510. - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem {
  511. return YES;
  512. }
  513. - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem {
  514. BOOL isImage = NO;
  515. if ([tabView indexOfTabViewItem:tabViewItem] == 0) {
  516. self.type = KMPDFSignatureTypeText;
  517. if ([self.keyboardView signatureImage]) {
  518. isImage = YES;
  519. }
  520. } else if ([tabView indexOfTabViewItem:tabViewItem] == 1) {
  521. self.type = KMPDFSignatureTypeInk;
  522. if ([self.drawView signatureImage]) {
  523. isImage = YES;
  524. }
  525. } else if ([tabView indexOfTabViewItem:tabViewItem] == 2) {
  526. self.type = KMPDFSignatureTypeImage;
  527. if ([self.pictureBackView signatureImage]) {
  528. isImage = YES;
  529. }
  530. }
  531. if (isImage) {
  532. self.applyButton.enabled = YES;
  533. } else {
  534. self.applyButton.enabled = NO;
  535. }
  536. }
  537. #pragma mark - NSNotification
  538. - (void)widthSizeTextFieldNotification:(NSNotification *)notification {
  539. NSTextView *field = (NSTextView *)notification.object;
  540. if ([field isEqual:_widthSizeTextField]) {
  541. CGFloat widthSize = [_widthSizeTextField.stringValue floatValue];
  542. if (widthSize < 0.1) {
  543. _widthSizeTextField.stringValue = @"0.1";
  544. _drawView.strokeRadius = _slider.floatValue = 0.1;
  545. } else if (widthSize > 4) {
  546. _widthSizeTextField.stringValue = @"4.0";
  547. _drawView.strokeRadius = _slider.floatValue = 4.0;
  548. } else {
  549. _drawView.strokeRadius = _slider.floatValue = widthSize;
  550. }
  551. }
  552. }
  553. - (void)themeChanged:(NSNotification *)notification {
  554. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  555. [self updateViewColor];
  556. });
  557. }
  558. @end