KMPDFMergeFileNameTabelViewCell.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // KMPDFMergeFileNameTabelViewCell.m
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/2/23.
  6. //
  7. #import "KMPDFMergeFileNameTabelViewCell.h"
  8. #if VERSION_DMG
  9. #import <PDF_Master-Swift.h>
  10. #else
  11. #import <PDF_Master-Swift.h>
  12. #endif
  13. @interface KMPDFMergeFileNameTabelViewCell ()
  14. @property (nonatomic, strong) NSTextField *numberLabel;
  15. @property (nonatomic, strong) NSImageView *iconImageView;
  16. @property (nonatomic, strong) NSTextField *fileNameLabel;
  17. @property (nonatomic, strong) NSTextField *pageNumberLabel;
  18. @end
  19. @implementation KMPDFMergeFileNameTabelViewCell
  20. - (instancetype)initWithFrame:(NSRect)frameRect {
  21. if (self = [super initWithFrame:frameRect]) {
  22. [self addSubview:self.numberLabel];
  23. [self addSubview:self.iconImageView];
  24. [self addSubview:self.fileNameLabel];
  25. [self addSubview:self.pageNumberLabel];
  26. }
  27. return self;
  28. }
  29. - (BOOL)isFlipped {
  30. return YES;
  31. }
  32. - (void)layout {
  33. [super layout];
  34. CGFloat width = NSWidth(self.bounds);
  35. CGFloat height = NSHeight(self.bounds);
  36. CGFloat labelH = 22;
  37. self.numberLabel.frame = NSMakeRect(16, (height-labelH)*0.5, 30+16, labelH);
  38. CGFloat iconSize = 60;
  39. self.iconImageView.frame = NSMakeRect(NSMaxX(self.numberLabel.frame)+6, (height-iconSize)*0.5, iconSize, iconSize);
  40. CGFloat fileNameX = NSMaxX(self.iconImageView.frame)+8;
  41. self.fileNameLabel.frame = NSMakeRect(fileNameX, 13, width-fileNameX, labelH);
  42. self.pageNumberLabel.frame = NSMakeRect(fileNameX, NSMaxY(self.fileNameLabel.frame)+8, width-fileNameX, labelH);
  43. }
  44. #pragma mark - Getting
  45. - (NSTextField *)numberLabel {
  46. if (!_numberLabel) {
  47. _numberLabel = [NSTextField labelWithString:@""];
  48. _numberLabel.textColor = [NSColor colorWithRed:37/255.f green:38/255.f blue:41/255.f alpha:1.f];
  49. _numberLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:14];
  50. _numberLabel.alignment = NSTextAlignmentCenter;
  51. _numberLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  52. }
  53. return _numberLabel;
  54. }
  55. - (NSImageView *)iconImageView {
  56. if (!_iconImageView) {
  57. _iconImageView = [[NSImageView alloc] init];
  58. }
  59. return _iconImageView;
  60. }
  61. - (NSTextField *)fileNameLabel {
  62. if (!_fileNameLabel) {
  63. _fileNameLabel = [NSTextField labelWithString:@""];
  64. _fileNameLabel.textColor = [NSColor colorWithRed:37/255.f green:38/255.f blue:41/255.f alpha:1.f];
  65. _fileNameLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:14];
  66. _fileNameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  67. }
  68. return _fileNameLabel;
  69. }
  70. - (NSTextField *)pageNumberLabel {
  71. if (!_pageNumberLabel) {
  72. _pageNumberLabel = [NSTextField labelWithString:@""];
  73. _pageNumberLabel.textColor = [NSColor colorWithRed:148/255.f green:152/255.f blue:156/255.f alpha:1.f];
  74. _pageNumberLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:14];
  75. }
  76. return _pageNumberLabel;
  77. }
  78. @end
  79. @interface KMPDFMergePageRangeTabelViewCell ()<KMSelectPopButtonDelegate>
  80. @property (nonatomic, strong) KMMergeSelect *comboBox;
  81. @end
  82. @implementation KMPDFMergePageRangeTabelViewCell
  83. - (instancetype)initWithFrame:(NSRect)frameRect {
  84. if (self = [super initWithFrame:frameRect]) {
  85. [self addSubview:self.comboBox.view];
  86. [self.comboBox removeAllItems];
  87. [self.comboBox addItemsWithObjectValues:@[NSLocalizedString(@"All Pages", nil),NSLocalizedString(@"Odd Pages", nil),NSLocalizedString(@"Even Pages", nil),NSLocalizedString(@"Customized", nil)]];
  88. self.comboBox.stringValue = NSLocalizedString(@"All Pages", nil);
  89. self.comboBox.editable = false;
  90. self.comboBox.delete = self;
  91. self.comboBox.placeholderString = NSLocalizedString(@"eg .1,3-5,10", nil);
  92. }
  93. return self;
  94. }
  95. - (BOOL)isFlipped {
  96. return YES;
  97. }
  98. - (void)updatePageRange:(KMPDFSelectPageStringType)pageRange pageStrings:(nonnull NSString *)pageStrings {
  99. [self updatePageRange:pageRange pageStrings:pageStrings isFirstResponser:YES];
  100. }
  101. - (void)updatePageRange:(KMPDFSelectPageStringType)pageRange pageStrings:(NSString *)pageStrings isFirstResponser:(BOOL)isFirstResponser {
  102. if (pageRange == KMPDFSeleectPageType_AllPages) {
  103. self.comboBox.stringValue = NSLocalizedString(@"All Pages", nil);
  104. self.comboBox.editable = false;
  105. self.comboBox.alert = NO;
  106. } else if (pageRange == KMPDFSeleectPageType_OnlyOdd) {
  107. self.comboBox.stringValue = NSLocalizedString(@"Odd Pages", nil);
  108. self.comboBox.editable = false;
  109. self.comboBox.alert = NO;
  110. } else if (pageRange == KMPDFSeleectPageType_OnlyEven) {
  111. self.comboBox.stringValue = NSLocalizedString(@"Even Pages", nil);
  112. self.comboBox.editable = false;
  113. self.comboBox.alert = NO;
  114. } else {
  115. self.comboBox.stringValue = pageStrings;
  116. self.comboBox.editable = YES;
  117. if ([pageStrings isEqual: @""]) {
  118. self.comboBox.alert = YES;
  119. } else {
  120. self.comboBox.alert = NO;
  121. }
  122. if (isFirstResponser || [pageStrings isEqual: @""]) {
  123. if (![self.window.firstResponder isEqual:self.comboBox.textField]) {
  124. if ([NSThread isMainThread]) {
  125. [self.window makeFirstResponder:self.comboBox.textField];
  126. } else {
  127. dispatch_async(dispatch_get_main_queue(), ^{
  128. [self.window makeFirstResponder:self.comboBox.textField];
  129. });
  130. }
  131. }
  132. }
  133. }
  134. }
  135. - (void)updateRageRangeState:(NSInteger)state {
  136. if (state == 9) {
  137. self.comboBox.state = KMDesignTokenStateError_def;
  138. } else if (state == 10) {
  139. self.comboBox.state = KMDesignTokenStateError_focus;
  140. }
  141. }
  142. - (void)layout {
  143. [super layout];
  144. CGFloat width = NSWidth(self.bounds);
  145. CGFloat height = NSHeight(self.bounds);
  146. CGFloat comboBoxH = 26;
  147. self.comboBox.view.frame = NSMakeRect(10, (height-comboBoxH)*0.5, width-10, comboBoxH);
  148. }
  149. #pragma mark - KMSelectPopButtonDelegate
  150. - (void)km_comboBoxSelectionDidChange:(KMDesignSelect *)obj {
  151. if (self.callback) {
  152. NSInteger index = obj.indexOfSelectedItem;
  153. if (index < 0) {
  154. index = 0;
  155. }
  156. if (index == self.comboBox.numberOfItems-1) {
  157. self.comboBox.editable = true;
  158. dispatch_async(dispatch_get_main_queue(), ^{
  159. self.comboBox.stringValue = @"";
  160. });
  161. [self.window makeFirstResponder:self.comboBox.textField];
  162. } else {
  163. self.comboBox.editable = false;
  164. self.comboBox.alert = false;
  165. }
  166. self.callback(index);
  167. }
  168. }
  169. - (void)km_controlTextDidChange:(KMDesignSelect *)obj {
  170. if (self.textDidChange) {
  171. self.textDidChange(obj.stringValue);
  172. }
  173. }
  174. - (void)km_controlTextDidEndEditing:(KMDesignSelect *)obj {
  175. if (self.textDidEndEdit) {
  176. self.textDidEndEdit(obj.stringValue, self);
  177. }
  178. }
  179. - (void)km_SelectPopoverWillShow:(KMDesignSelect *)obj {
  180. if ([self.comboBox isEqual:obj]) {
  181. NSMutableArray *disableItems = [NSMutableArray array];
  182. if (self.pageCount <= 1) {
  183. [disableItems addObject:NSLocalizedString(@"Even Pages", nil)];
  184. }
  185. self.comboBox.disItems = disableItems.copy;
  186. }
  187. }
  188. #pragma mark - Getting
  189. - (KMDesignSelect *)comboBox {
  190. if (!_comboBox) {
  191. _comboBox = [[KMMergeSelect alloc] initWithType:1];
  192. }
  193. return _comboBox;
  194. }
  195. @end
  196. @interface KMPDFMergeSizeTabelViewCell ()
  197. @property (nonatomic, strong) NSTextField *label;
  198. @property (nonatomic, strong) NSButton *button;
  199. @end
  200. @implementation KMPDFMergeSizeTabelViewCell
  201. - (instancetype)initWithFrame:(NSRect)frameRect {
  202. if (self = [super initWithFrame:frameRect]) {
  203. [self addSubview:self.label];
  204. [self addSubview:self.button];
  205. self.button.target = self;
  206. self.button.action = @selector(buttonAction);
  207. }
  208. return self;
  209. }
  210. - (BOOL)isFlipped {
  211. return YES;
  212. }
  213. - (void)layout {
  214. [super layout];
  215. CGFloat height = NSHeight(self.bounds);
  216. CGFloat labelH = 22;
  217. self.label.frame = NSMakeRect(16, (height-labelH)*0.5, 70, labelH);
  218. CGFloat buttonSize = 20;
  219. self.button.frame = NSMakeRect(NSMaxX(self.label.frame)+5, (height-buttonSize)*0.5, buttonSize, buttonSize);
  220. }
  221. - (void)buttonAction {
  222. if (self.callback) {
  223. self.callback();
  224. }
  225. }
  226. #pragma mark - Getting
  227. - (NSTextField *)label {
  228. if (!_label) {
  229. _label = [NSTextField labelWithString:@""];
  230. _label.textColor = [NSColor colorWithRed:148/255.f green:152/255.f blue:156/255.f alpha:1.f];
  231. _label.font = [NSFont fontWithName:@"SFProText-Regular" size:14];
  232. }
  233. return _label;
  234. }
  235. - (NSButton *)button {
  236. if (!_button) {
  237. _button = [[NSButton alloc] init];
  238. _button.bordered = false;
  239. _button.image = [NSImage imageNamed:@"KMImageNameSecureClearIcon"];
  240. }
  241. return _button;
  242. }
  243. @end