KMSignatureWindowController.m 26 KB

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