KMToolbar.m 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. //
  2. // KMToolbar.m
  3. // SignFlow
  4. //
  5. // Copyright © 2022 Kdan Mobile. All rights reserved.
  6. //
  7. #import "KMToolbar.h"
  8. #import "NSButton+TitleColor.h"
  9. #import "NSWindow+PopOver.h"
  10. #import <Masonry/Masonry.h>
  11. //#import "NSPopover+PDFListView.h"
  12. #if VERSION_DMG
  13. #import <PDF_Master-Swift.h>
  14. #else
  15. #import <PDF_Master-Swift.h>
  16. #endif
  17. NSString *KMNewToolbarSpaceItemIdentifier = @"KMNewToolbarSpaceItemIdentifier";
  18. NSString *KMToolbarDividerItemIdentifier = @"KMToolbarDividerItemIdentifier";
  19. #define KMToolbarSpaceItemWidth 30.0
  20. #define KMToolbarLineItemWidth 1.0
  21. #define KMToolbarItemSpace 6.0
  22. #pragma mark - KMToolbarClickButton
  23. @implementation KMToolbarClickButton
  24. @end
  25. #pragma mark - KMToolBoxItem
  26. @interface KMToolBoxItem()<KMCustomButtonPopMenuViewControllerDelegate,KMCustomButtonPopMenuViewControllerDataSources,NSPopoverDelegate>
  27. @property(nonatomic,retain) NSBox *imageViewBox;
  28. @property(nonatomic,retain) NSButton *imageViewBtn;
  29. @property(nonatomic,retain) NSButton *nameBtn;
  30. @property(nonatomic,retain) KMToolbarClickButton *clickButton;
  31. @property(nonatomic,retain) NSButton *needExpandButton;
  32. @property (nonatomic,retain) NSPopover *popOver;
  33. @property (nonatomic,retain) KMCustomButtonPopMenuViewController *menuViewController;
  34. @property (nonatomic,retain) NSString *itemIdentifier;
  35. @property (nonatomic,retain) NSImage *kNormalImage;
  36. @property (nonatomic,retain) NSString *originalHelpTip;
  37. @end
  38. @implementation KMToolBoxItem
  39. - (void)drawRect:(NSRect)dirtyRect {
  40. if ([self.itemIdentifier isEqual:KMToolbarDividerItemIdentifier]) {
  41. CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
  42. CGContextSaveGState(context);
  43. CGContextTranslateCTM(context, CGRectGetWidth(dirtyRect)/2., CGRectGetHeight(dirtyRect)/2. - 10);
  44. CGContextMoveToPoint(context, 0, 0);
  45. CGContextAddLineToPoint(context, 0, 20);
  46. CGContextSetStrokeColorWithColor(context, [NSColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor);
  47. CGContextStrokePath(context);
  48. CGContextRestoreGState(context);
  49. }
  50. }
  51. - (instancetype)initWithItemIdentifier:(NSString *)itemIdentifier
  52. postition:(KMToolBoxImagePosition)imagePositionImagePosition
  53. withPopMenu:(KMCustomButtonPopMenuViewController *)popMenuViewController
  54. {
  55. if (self == [super init]) {
  56. self.boxImagePosition = imagePositionImagePosition;
  57. self.menuViewController = popMenuViewController;
  58. self.itemIdentifier = itemIdentifier;
  59. self.wantsLayer = YES;
  60. self.layer.cornerRadius = 4;
  61. self.layer.masksToBounds = YES;
  62. self.normalBackgroundColor = [NSColor clearColor];
  63. self.selectedBackgroundColor = [NSColor colorWithRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1];
  64. [self addTrackingArea];
  65. [self layoutView];
  66. if (popMenuViewController) {
  67. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowClosedPop:) name:@"KMPopOverClosedByWindowNotification" object:nil];
  68. }
  69. }
  70. return self;
  71. }
  72. - (instancetype)initWithItemIdentifier:(NSString *)itemIdentifier {
  73. if (self == [super init]) {
  74. self.itemIdentifier = itemIdentifier;
  75. self.boxImagePosition = KMToolBoxImagePosition_Left;
  76. self.wantsLayer = YES;
  77. self.layer.cornerRadius = 5;
  78. self.layer.masksToBounds = YES;
  79. self.normalBackgroundColor = [NSColor clearColor];
  80. self.selectedBackgroundColor = [NSColor colorWithRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1];
  81. [self addTrackingArea];
  82. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowClosedPop:) name:@"KMPopOverClosedByWindowNotification" object:nil];
  83. }
  84. return self;
  85. }
  86. #pragma mark - View
  87. - (void)layoutView
  88. {
  89. CGFloat offset = 4;
  90. CGFloat offsetY = 2;
  91. CGFloat offsetX = 4;
  92. if ([self.nameBtn superview]) {
  93. [self.nameBtn removeFromSuperview];
  94. }
  95. if ([self.linView superview]) {
  96. [self.linView removeFromSuperview];
  97. }
  98. if([self.imageViewBox superview]) {
  99. [self.imageViewBox removeFromSuperview];
  100. }
  101. if([self.imageViewBtn superview]) {
  102. [self.imageViewBtn removeFromSuperview];
  103. }
  104. if(self.customizeView) {
  105. if([self.customizeView superview]) {
  106. [self.customizeView removeFromSuperview];
  107. }
  108. [self addSubview:self.customizeView];
  109. [self.customizeView mas_makeConstraints:^(MASConstraintMaker *make) {
  110. make.left.right.equalTo(@0);
  111. make.centerY.equalTo(@0);
  112. make.width.offset(self.customizeView.frame.size.width);
  113. make.height.offset(self.customizeView.frame.size.height);
  114. }];
  115. return;
  116. }
  117. if (KMToolBoxImagePosition_Only == self.boxImagePosition && ![self.itemIdentifier isEqual:KMToolbarDividerItemIdentifier]) {
  118. [self addSubview:self.imageViewBox];
  119. [self.imageViewBox mas_makeConstraints:^(MASConstraintMaker *make) {
  120. make.left.right.top.bottom.equalTo(@0);
  121. }];
  122. [self.imageViewBox.contentView addSubview:self.imageViewBtn];
  123. [self.imageViewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  124. make.left.equalTo(self.imageViewBox.mas_left).offset(offsetX);
  125. make.top.equalTo(self.imageViewBox.mas_top).offset(offsetY);
  126. make.bottom.equalTo(self.imageViewBox.mas_bottom).offset(-offsetY);
  127. make.right.equalTo(self.imageViewBox.mas_right).offset(-offsetX);
  128. }];
  129. } else if (KMToolBoxImagePosition_Left == self.boxImagePosition) {
  130. [self addSubview:self.imageViewBox];
  131. [self.imageViewBox mas_makeConstraints:^(MASConstraintMaker *make) {
  132. make.left.top.bottom.equalTo(@0);
  133. }];
  134. [self.imageViewBox.contentView addSubview:self.imageViewBtn];
  135. [self.imageViewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  136. make.right.equalTo(@0);
  137. make.left.equalTo(self.imageViewBox.mas_left).offset(2 * offsetX);
  138. make.top.equalTo(self.imageViewBox.mas_top).offset(offsetY);
  139. make.bottom.equalTo(self.imageViewBox.mas_bottom).offset(-offsetY);
  140. }];
  141. [self addSubview:self.nameBtn];
  142. [self.nameBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  143. make.centerY.equalTo(@0);
  144. make.left.equalTo(self.imageViewBox.mas_right).offset(0);
  145. if(self.needExpandAction) {
  146. make.right.equalTo(self.mas_right).offset(- 2 * offsetX-8);
  147. } else {
  148. make.right.equalTo(self.mas_right).offset(- 2 * offsetX);
  149. }
  150. }];
  151. if(self.needExpandAction) {
  152. self.needExpandButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnTriDownNor"];
  153. [self addSubview:self.needExpandButton];
  154. [self.needExpandButton mas_makeConstraints:^(MASConstraintMaker *make) {
  155. make.centerY.equalTo(@0);
  156. make.width.height.equalTo(@8);
  157. make.right.equalTo(self.mas_right).offset(-offset);
  158. }];
  159. }
  160. self.layer.cornerRadius = 6;
  161. } else if (KMToolBoxImagePosition_ExpandLeft == self.boxImagePosition) {
  162. [self addSubview:self.imageViewBox];
  163. [self.imageViewBox mas_makeConstraints:^(MASConstraintMaker *make) {
  164. make.left.top.bottom.equalTo(@0);
  165. }];
  166. [self.imageViewBox.contentView addSubview:self.imageViewBtn];
  167. [self.imageViewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  168. make.right.equalTo(@0);
  169. make.left.equalTo(self.imageViewBox.mas_left).offset(offsetX);
  170. make.top.equalTo(self.imageViewBox.mas_top).offset(offsetY);
  171. make.bottom.equalTo(self.imageViewBox.mas_bottom).offset(-offsetY);
  172. }];
  173. self.needExpandButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnTriDownNor"];
  174. [self addSubview:self.needExpandButton];
  175. [self.needExpandButton mas_makeConstraints:^(MASConstraintMaker *make) {
  176. make.centerY.equalTo(@0);
  177. make.width.height.equalTo(@8);
  178. make.right.equalTo(self.mas_right).offset(-offsetX);
  179. }];
  180. [self addSubview:self.nameBtn];
  181. [self.nameBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  182. make.centerY.equalTo(@0);
  183. make.left.equalTo(self.imageViewBox.mas_right).offset(0);
  184. make.right.equalTo(self.needExpandButton.mas_left).offset(0);
  185. }];
  186. } else if(KMToolBoxImagePosition_Above == self.boxImagePosition){
  187. [self addSubview:self.nameBtn];
  188. self.nameBtn.alignment = NSTextAlignmentCenter;
  189. [self.nameBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  190. make.left.right.equalTo(@0);
  191. make.width.greaterThanOrEqualTo(@32);
  192. make.bottom.equalTo(self.mas_bottom).offset(0);
  193. }];
  194. [self addSubview:self.imageViewBox];
  195. [self.imageViewBox mas_makeConstraints:^(MASConstraintMaker *make) {
  196. make.top.equalTo(@0);
  197. make.width.equalTo(@32);
  198. make.centerX.equalTo(@0);
  199. make.bottom.equalTo(self.nameBtn.mas_top).offset(0);
  200. }];
  201. [self.imageViewBox.contentView addSubview:self.imageViewBtn];
  202. [self.imageViewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  203. make.left.top.right.equalTo(@(offset));
  204. make.bottom.right.equalTo(@(-offset));
  205. }];
  206. }
  207. if([self.nameBtn superview] && self.isMainTool) {
  208. [self addSubview:self.linView];
  209. [self.linView mas_makeConstraints:^(MASConstraintMaker *make) {
  210. make.width.offset(32);
  211. make.height.offset(3);
  212. make.centerX.equalTo(self.mas_centerX);
  213. make.bottom.equalTo(self.mas_bottom).offset(0);
  214. }];
  215. _nameBtn.font = [NSFont systemFontOfSize:14.0];
  216. self.linView.hidden = YES;
  217. } else {
  218. _nameBtn.font = [NSFont systemFontOfSize:12.0];
  219. }
  220. if (![self.itemIdentifier isEqual:KMToolbarDividerItemIdentifier]) {
  221. self.imageViewBox.borderColor = [NSColor clearColor];
  222. self.imageViewBox.borderWidth = 1.0;
  223. self.imageViewBox.cornerRadius = 7.0;
  224. [self addSubview:self.clickButton];
  225. [self.clickButton mas_makeConstraints:^(MASConstraintMaker *make) {
  226. make.left.right.top.bottom.equalTo(@0);
  227. }];
  228. }
  229. }
  230. #pragma mark - Get&Set
  231. - (void)setBoxImagePosition:(KMToolBoxImagePosition)boxImagePosition
  232. {
  233. _boxImagePosition = boxImagePosition;
  234. [self layoutView];
  235. }
  236. - (void)setCustomizeView:(NSView *)customizeView {
  237. _customizeView = customizeView;
  238. [self layoutView];
  239. }
  240. - (void)setIsSelected:(BOOL)isSelected
  241. {
  242. _isSelected = isSelected;
  243. if (![self.itemIdentifier isEqual: KMToolbarDividerItemIdentifier]) {
  244. if (isSelected) {
  245. if (_isMainTool) {
  246. self.layer.backgroundColor = self.normalBackgroundColor.CGColor;
  247. [self.nameBtn setTitleColor:[KMAppearance titleColor] font:[NSFont fontWithName:@"SFProText-Semibold" size:14]];
  248. self.linView.hidden = NO;
  249. return;
  250. }
  251. self.layer.backgroundColor = self.selectedBackgroundColor.CGColor;
  252. if(self.image && self.alternateImage) {
  253. if (self.selectedImage) {
  254. self.imageViewBtn.image = self.selectedImage;
  255. } else {
  256. self.imageViewBtn.image = self.alternateImage;
  257. }
  258. }
  259. if(self.nameBtn.superview) {
  260. [self.nameBtn setTitleColor:[KMAppearance titleColor]];
  261. }
  262. if(self.needExpandAction) {
  263. self.needExpandButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnTriDownSel"];
  264. }
  265. } else {
  266. if (_isMainTool) {
  267. self.layer.backgroundColor = self.normalBackgroundColor.CGColor;
  268. [self.nameBtn setTitleColor:[NSColor colorWithRed:97.0/255.0 green:100.0/255.0 blue:105.0/255.0 alpha:1] font:[NSFont fontWithName:@"SFProText-Regular" size:14]];
  269. self.linView.hidden = YES;
  270. return;
  271. }
  272. self.layer.backgroundColor = self.normalBackgroundColor.CGColor;
  273. if(self.needExpandAction) {
  274. self.needExpandButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnTriDownNor"];
  275. }
  276. if(self.image) {
  277. self.imageViewBtn.image = self.image;
  278. }
  279. if(self.nameBtn.superview) {
  280. [self.nameBtn setTitleColor:[KMAppearance titleColor]];
  281. }
  282. }
  283. }
  284. }
  285. - (void)setIsMainTool:(BOOL)isMainTool {
  286. _isMainTool = isMainTool;
  287. if (_isMainTool) {
  288. [self.nameBtn setTitleColor:[NSColor colorWithRed:97.0/255.0 green:100.0/255.0 blue:105.0/255.0 alpha:1] font:[NSFont fontWithName:@"SFProText-Regular" size:14]];
  289. } else {
  290. [self.nameBtn setTitleColor:[KMAppearance titleColor]];
  291. }
  292. [self layoutView];
  293. }
  294. - (void)setPopOver:(NSPopover *)popOver
  295. {
  296. if (![_popOver isEqual:popOver]) {
  297. _popOver = popOver;
  298. if (popOver) {
  299. self.layer.backgroundColor = self.selectedBackgroundColor.CGColor;
  300. } else {
  301. if (self.isSelected) {
  302. self.layer.backgroundColor = self.selectedBackgroundColor.CGColor;
  303. }else {
  304. self.layer.backgroundColor = self.normalBackgroundColor.CGColor;
  305. }
  306. }
  307. }
  308. }
  309. - (NSButton *)imageViewBtn
  310. {
  311. if (!_imageViewBtn) {
  312. _imageViewBtn = [[NSButton alloc] init];
  313. _imageViewBtn.bezelStyle = NSBezelStyleRegularSquare;
  314. _imageViewBtn.bordered = NO;
  315. _imageViewBtn.imagePosition = NSImageOnly;
  316. }
  317. return _imageViewBtn;
  318. }
  319. - (KMToolbarClickButton *)clickButton
  320. {
  321. if (!_clickButton) {
  322. _clickButton = [[KMToolbarClickButton alloc] init];
  323. _clickButton.bezelStyle = NSBezelStyleRegularSquare;
  324. _clickButton.bordered = NO;
  325. _clickButton.imagePosition = NSImageOnly;
  326. _clickButton.clickObject = self;
  327. }
  328. return _clickButton;
  329. }
  330. - (NSButton *)needExpandButton
  331. {
  332. if (!_needExpandButton) {
  333. _needExpandButton = [[NSButton alloc] init];
  334. _needExpandButton.bezelStyle = NSBezelStyleRegularSquare;
  335. _needExpandButton.bordered = NO;
  336. _needExpandButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnTriDownNor"];
  337. _needExpandButton.imagePosition = NSImageOnly;
  338. }
  339. return _needExpandButton;
  340. }
  341. - (NSButton *)nameBtn
  342. {
  343. if (!_nameBtn) {
  344. _nameBtn = [[NSButton alloc] init];
  345. _nameBtn.bezelStyle = NSBezelStyleRegularSquare;
  346. _nameBtn.bordered = NO;
  347. _nameBtn.imagePosition = NSNoImage;
  348. _nameBtn.font = [NSFont systemFontOfSize:12.0];
  349. [_nameBtn setTitle:_titleName ? : @" "];
  350. }
  351. return _nameBtn;
  352. }
  353. -(NSBox *)imageViewBox
  354. {
  355. if (!_imageViewBox) {
  356. _imageViewBox = [[NSBox alloc] init];
  357. [_imageViewBox setBorderWidth:0.0f];
  358. _imageViewBox.contentViewMargins = NSMakeSize(0, 0);
  359. _imageViewBox.boxType = NSBoxCustom;
  360. }
  361. return _imageViewBox;
  362. }
  363. - (NSView *)linView
  364. {
  365. if (!_linView) {
  366. _linView = [[NSView alloc] init];
  367. _linView.wantsLayer = YES;
  368. _linView.layer.backgroundColor = [NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:1].CGColor;
  369. _linView.layer.cornerRadius = 2.0;
  370. }
  371. return _linView;
  372. }
  373. -(void)setImage:(NSImage *)image
  374. {
  375. if (image != _image) {
  376. _image = image;
  377. }
  378. self.imageViewBtn.image = image;
  379. }
  380. -(void)setTitleName:(NSString *)titleName
  381. {
  382. if (titleName != _titleName) {
  383. _titleName = titleName;
  384. }
  385. self.nameBtn.title = titleName ? : KMToolbarDividerItemIdentifier;
  386. [self.nameBtn setTitleColor:[KMAppearance titleColor]];
  387. }
  388. - (void)setTarget:(id)target
  389. {
  390. _target = target;
  391. self.clickButton.target = target;
  392. }
  393. - (void)setBtnTag:(NSInteger)btnTag
  394. {
  395. _btnTag = btnTag;
  396. self.clickButton.tag = btnTag;
  397. }
  398. - (void)setBtnAction:(SEL)btnAction
  399. {
  400. _btnAction = btnAction;
  401. self.clickButton.action = btnAction;
  402. }
  403. -(void)setToolTip:(NSString *)toolTip
  404. {
  405. self.clickButton.toolTip = toolTip?:@"";
  406. self.originalHelpTip = self.clickButton.toolTip;
  407. if(self.isShowCustomToolTip) {
  408. self.clickButton.toolTip = @"";
  409. }
  410. }
  411. - (void)setIsShowCustomToolTip:(BOOL)isShowCustomToolTip {
  412. _isShowCustomToolTip = isShowCustomToolTip;
  413. if(isShowCustomToolTip) {
  414. self.clickButton.toolTip = @"";
  415. }
  416. }
  417. - (void)setUnEnabled:(BOOL)unEnabled
  418. {
  419. if (unEnabled) {
  420. self.clickButton.enabled = NO;
  421. self.nameBtn.enabled = NO;
  422. self.imageViewBtn.enabled = NO;
  423. self.needExpandButton.enabled = NO;
  424. } else {
  425. self.clickButton.enabled = YES;
  426. self.nameBtn.enabled = YES;
  427. self.imageViewBtn.enabled = YES;
  428. self.needExpandButton.enabled = YES;
  429. }
  430. }
  431. #pragma mark - Private
  432. - (void)addTrackingArea
  433. {
  434. NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveInKeyWindow owner:self userInfo:nil];
  435. [self addTrackingArea:trackingArea];
  436. }
  437. - (void)mouseEntered:(NSEvent *)event {
  438. [super mouseEntered:event];
  439. if (![self.window isKeyWindow]) {
  440. return;
  441. }
  442. if ([self.itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier] || self.customizeView || self.image == nil) {
  443. return;
  444. }
  445. if (!self.isSelected) {
  446. self.layer.backgroundColor = self.selectedBackgroundColor.CGColor;
  447. if(self.image && self.alternateImage) {
  448. self.kNormalImage = self.image;
  449. self.imageViewBtn.image = self.alternateImage;
  450. if(self.nameBtn.superview) {
  451. [self.nameBtn setTitleColor:[KMAppearance titleColor]];
  452. }
  453. }
  454. }
  455. if (self.needExpandAction) {
  456. self.needExpandButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnTriDownSel"];
  457. [self showPop:self];
  458. }else if (_isShowCustomToolTip) {
  459. [self performSelector:@selector(showHUDHint) withObject:nil afterDelay:0.1];
  460. }
  461. }
  462. - (void)mouseExited:(NSEvent *)event {
  463. [super mouseExited:event];
  464. if (![self.window isKeyWindow]) {
  465. return;
  466. }
  467. if (!_isSelected && !self.needExpandAction) {
  468. self.layer.backgroundColor = self.normalBackgroundColor.CGColor;
  469. if(self.image && self.alternateImage) {
  470. self.imageViewBtn.image = self.kNormalImage ? : self.image;
  471. }
  472. }
  473. if(self.needExpandAction && !_isSelected) {
  474. self.layer.backgroundColor = self.normalBackgroundColor.CGColor;
  475. if(self.image && self.alternateImage) {
  476. self.imageViewBtn.image = self.kNormalImage ? : self.image;
  477. }
  478. self.needExpandButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnTriDownNor"];
  479. }
  480. if(self.nameBtn.superview && !_isSelected && !_isMainTool) {
  481. [self.nameBtn setTitleColor:[KMAppearance titleColor]];
  482. }
  483. if (_isShowCustomToolTip && !self.needExpandAction) {
  484. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showHUDHint) object:nil];
  485. [self closePop];
  486. }
  487. }
  488. - (void)showPop:(NSView *)sender {
  489. }
  490. - (void)showHUDHint {
  491. // KMToolbarItemPopViewController *popViewController = [[[KMToolbarItemPopViewController alloc] init] autorelease];
  492. // self.popOver = [[[NSPopover alloc] init] autorelease];
  493. // self.popOver.contentViewController = popViewController;
  494. // self.popOver.animates = NO;
  495. // self.popOver.behavior = NSPopoverBehaviorSemitransient;
  496. // self.popOver.backgroundColor = [KMAppearance KMBluegrey01Color];
  497. //
  498. // self.popOver.contentSize = popViewController.view.frame.size;
  499. // [popViewController updateWithHelpTip:self.originalHelpTip];
  500. // [self.popOver showRelativeToRect:self.bounds ofView:self preferredEdge:NSRectEdgeMinY];
  501. }
  502. - (void)windowClosedPop:(NSNotification *)sender
  503. {
  504. if ([sender.object isEqual:self.popOver]) {
  505. self.popOver = nil;
  506. }
  507. }
  508. - (void)closePop {
  509. [self.popOver close];
  510. self.popOver = nil;
  511. }
  512. @end
  513. #pragma mark - KMNewToolbar
  514. @interface KMToolbar ()
  515. @property (nonatomic,retain) NSString *toolbarIdentifier;
  516. @property (nonatomic,retain) NSArray *items;
  517. @property (nonatomic,retain) NSArray *visibleItems;
  518. @property (nonatomic,retain) NSMutableArray *invisibleItems;
  519. @property (nonatomic,retain) NSView *contentView;
  520. @property (nonatomic,retain) NSButton *moreButton;
  521. @end
  522. @implementation KMToolbar
  523. #pragma mark - Init Methods
  524. - (instancetype)initWithIdentifier:(NSString *)identifier
  525. {
  526. if (self = [super init]) {
  527. self.toolbarIdentifier = identifier;
  528. [self addTrackingArea];
  529. }
  530. return self;
  531. }
  532. - (instancetype)initWithCoder:(NSCoder *)decoder
  533. {
  534. if (self = [super initWithCoder:decoder]) {
  535. self.wantsLayer = YES;
  536. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  537. [self addTrackingArea];
  538. }
  539. return self;
  540. }
  541. - (instancetype)initWithFrame:(NSRect)frameRect
  542. {
  543. if (self = [super initWithFrame:frameRect]) {
  544. self.wantsLayer = YES;
  545. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  546. [self addTrackingArea];
  547. }
  548. return self;
  549. }
  550. - (void)dealloc
  551. {
  552. [[NSNotificationCenter defaultCenter] removeObserver:self];
  553. }
  554. -(void)mouseMoved:(NSEvent *)event
  555. {
  556. [self.window mouseMoved:event];
  557. [super mouseMoved:event];
  558. }
  559. - (void)addTrackingArea
  560. {
  561. NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options: NSTrackingInVisibleRect | NSTrackingActiveInKeyWindow | NSTrackingMouseMoved owner:self userInfo:nil];
  562. [self addTrackingArea:trackingArea];
  563. }
  564. #pragma mark - Private Methods
  565. - (NSButton *)moreButton
  566. {
  567. if (!_moreButton) {
  568. _moreButton = [[NSButton alloc] init];
  569. _moreButton.bezelStyle = NSBezelStyleRegularSquare;
  570. _moreButton.bordered = NO;
  571. _moreButton.font = [NSFont systemFontOfSize:20];
  572. _moreButton.title = @" »";
  573. _moreButton.hidden = YES;
  574. _moreButton.target = self;
  575. _moreButton.action = @selector(moreButtonAction:);
  576. _moreButton.wantsLayer = YES;
  577. _moreButton.layer.backgroundColor = [NSColor colorWithRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1].CGColor;
  578. }
  579. return _moreButton;
  580. }
  581. - (void)moreButtonAction:(id)sender
  582. {
  583. NSMenu *menu = [[NSMenu alloc] init];
  584. for (KMToolBoxItem *item in self.invisibleItems) {
  585. if ([item.itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier]) {
  586. [menu addItem:[NSMenuItem separatorItem]];
  587. } else if (item.menuFormRepresentation) {
  588. [menu addItem:item.menuFormRepresentation];
  589. }
  590. }
  591. [menu popUpMenuPositioningItem:menu.itemArray.firstObject
  592. atLocation:NSMakePoint(CGRectGetMaxX(self.moreButton.frame),
  593. CGRectGetMaxY(self.moreButton.frame)-5)
  594. inView:self];
  595. }
  596. - (NSView *)contentView
  597. {
  598. if (!_contentView) {
  599. _contentView = [[NSView alloc] init];
  600. }
  601. return _contentView;
  602. }
  603. - (void)updateMainView
  604. {
  605. NSView *centerView = [[NSView alloc] init];
  606. [self addSubview:centerView];
  607. NSView *leftView = [[NSView alloc] init];
  608. leftView.wantsLayer = YES;
  609. leftView.layer.backgroundColor = [NSColor colorWithRed:247/255.f green:248/255.f blue:250/255.f alpha:1.f].CGColor;
  610. [self addSubview:leftView];
  611. [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
  612. make.left.equalTo(self.mas_left).offset(0);
  613. make.top.equalTo(self.mas_top).offset(0);
  614. make.bottom.equalTo(self.mas_bottom).offset(0);
  615. }];
  616. NSArray *leftitemIdentifiers = nil;
  617. if ([self.delegate respondsToSelector:@selector(toolbarLeftDefaultItemIdentifiers:)]) {
  618. leftitemIdentifiers = [self.delegate toolbarLeftDefaultItemIdentifiers:self];
  619. }
  620. KMToolBoxItem *leftlastItem = nil;
  621. for (NSUInteger i=0; i< leftitemIdentifiers.count; i++) {
  622. NSString *itemIdentifier = leftitemIdentifiers[i];
  623. if (![itemIdentifier isKindOfClass:[NSString class]]) {
  624. return;
  625. }
  626. KMToolBoxItem *item = nil;
  627. if ([itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier]) {
  628. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  629. } else if ([itemIdentifier isEqualToString:KMNewToolbarSpaceItemIdentifier]) {
  630. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  631. item.layer.backgroundColor = [NSColor clearColor].CGColor;
  632. item.imageViewBox.borderColor = [NSColor clearColor];
  633. item.layer.cornerRadius = 0.0;
  634. } else if ([self.delegate respondsToSelector:@selector(toolbar:itemForItemIdentifier:)]) {
  635. item = [self.delegate toolbar:self itemForItemIdentifier:itemIdentifier];
  636. }
  637. if (!item) {
  638. continue;
  639. }
  640. [leftView addSubview:item];
  641. if (leftlastItem) {
  642. if (i == leftitemIdentifiers.count - 1) {
  643. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  644. make.top.equalTo(@10);
  645. make.bottom.equalTo(@(-10));
  646. make.left.equalTo(leftlastItem.mas_right).offset(KMToolbarItemSpace);
  647. make.right.equalTo(leftView.mas_right).offset(0);
  648. }];
  649. } else {
  650. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  651. make.top.equalTo(@10);
  652. make.bottom.equalTo(@(-10));
  653. make.left.equalTo(leftlastItem.mas_right).offset(KMToolbarItemSpace);
  654. }];
  655. }
  656. } else {
  657. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  658. make.top.equalTo(@10);
  659. make.bottom.equalTo(@(-10));
  660. make.left.equalTo(@20);
  661. }];
  662. }
  663. leftlastItem = item;
  664. }
  665. NSView *rightView = [[NSView alloc] init];
  666. [self addSubview:rightView];
  667. rightView.wantsLayer = YES;
  668. rightView.layer.backgroundColor = [NSColor colorWithRed:247/255.f green:248/255.f blue:250/255.f alpha:1.f].CGColor;
  669. [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
  670. make.right.equalTo(self.mas_right).offset(0);
  671. make.top.equalTo(self.mas_top).offset(0);
  672. make.bottom.equalTo(self.mas_bottom).offset(0);
  673. }];
  674. NSArray *rightitemIdentifiers = nil;
  675. if ([self.delegate respondsToSelector:@selector(toolbarRightDefaultItemIdentifiers:)]) {
  676. rightitemIdentifiers = [self.delegate toolbarRightDefaultItemIdentifiers:self];
  677. }
  678. KMToolBoxItem *rightlastItem = nil;
  679. for (NSUInteger i=0; i< rightitemIdentifiers.count; i++) {
  680. NSString *itemIdentifier = rightitemIdentifiers[i];
  681. if (![itemIdentifier isKindOfClass:[NSString class]]) {
  682. return;
  683. }
  684. KMToolBoxItem *item = nil;
  685. if ([itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier]) {
  686. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  687. } else if ([itemIdentifier isEqualToString:KMNewToolbarSpaceItemIdentifier]) {
  688. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  689. item.layer.backgroundColor = [NSColor clearColor].CGColor;
  690. item.imageViewBox.borderColor = [NSColor clearColor];
  691. item.layer.cornerRadius = 0.0;
  692. } else if ([self.delegate respondsToSelector:@selector(toolbar:itemForItemIdentifier:)]) {
  693. item = [self.delegate toolbar:self itemForItemIdentifier:itemIdentifier];
  694. }
  695. if (!item) {
  696. continue;
  697. }
  698. [rightView addSubview:item];
  699. if (rightlastItem) {
  700. if (i == rightitemIdentifiers.count - 1) {
  701. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  702. make.top.equalTo(@6);
  703. make.bottom.equalTo(@(-6));
  704. make.left.equalTo(rightlastItem.mas_right).offset(KMToolbarItemSpace);
  705. make.right.equalTo(rightView.mas_right).offset(-20);
  706. }];
  707. } else {
  708. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  709. make.top.equalTo(@6);
  710. make.bottom.equalTo(@(-6));
  711. make.left.equalTo(rightlastItem.mas_right).offset(KMToolbarItemSpace);
  712. }];
  713. }
  714. } else {
  715. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  716. make.top.equalTo(@6);
  717. make.bottom.equalTo(@(-6));
  718. make.left.equalTo(@0);
  719. }];
  720. }
  721. rightlastItem = item;
  722. }
  723. // NSView *centerView = [[NSView alloc] init];
  724. // [self addSubview:centerView];
  725. [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
  726. // make.right.equalTo(rightView.mas_left).offset(0);
  727. make.right.mas_equalTo(0);
  728. make.top.equalTo(self.mas_top).offset(0);
  729. make.bottom.equalTo(self.mas_bottom).offset(0);
  730. // make.left.equalTo(leftView.mas_right).offset(0);
  731. make.left.mas_equalTo(0);
  732. }];
  733. if(!self.contentView.superview) {
  734. [centerView addSubview:self.contentView];
  735. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  736. make.centerX.equalTo(@0);
  737. make.centerY.equalTo(@0);
  738. }];
  739. }
  740. for (KMToolBoxItem *item in self.items) {
  741. [item removeFromSuperview];
  742. }
  743. self.items = nil;
  744. CGFloat posX = 0;
  745. NSMutableArray *items = [NSMutableArray array];
  746. NSArray *itemIdentifiers = nil;
  747. if ([self.delegate respondsToSelector:@selector(toolbarDefaultItemIdentifiers:)]) {
  748. itemIdentifiers = [self.delegate toolbarDefaultItemIdentifiers:self];
  749. }
  750. KMToolBoxItem *lastItem = nil;
  751. for (NSUInteger i=0; i< itemIdentifiers.count - self.invisibleItems.count; i++) {
  752. NSString *itemIdentifier = itemIdentifiers[i];
  753. if (![itemIdentifier isKindOfClass:[NSString class]]) {
  754. return;
  755. }
  756. KMToolBoxItem *item = nil;
  757. if ([itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier]) {
  758. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  759. } else if ([itemIdentifier isEqualToString:KMNewToolbarSpaceItemIdentifier]) {
  760. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  761. item.layer.backgroundColor = [NSColor clearColor].CGColor;
  762. item.imageViewBox.borderColor = [NSColor clearColor];
  763. item.layer.cornerRadius = 0.0;
  764. } else if ([self.delegate respondsToSelector:@selector(toolbar:itemForItemIdentifier:)]) {
  765. item = [self.delegate toolbar:self itemForItemIdentifier:itemIdentifier];
  766. }
  767. if (!item) {
  768. continue;
  769. }
  770. [self.contentView addSubview:item];
  771. if (lastItem) {
  772. if (i == itemIdentifiers.count - 1) {
  773. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  774. make.top.bottom.equalTo(@0);
  775. make.left.equalTo(lastItem.mas_right).offset(KMToolbarItemSpace);
  776. make.right.equalTo(self.contentView.mas_right).offset(0);
  777. }];
  778. } else {
  779. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  780. make.top.bottom.equalTo(@0);
  781. make.left.equalTo(lastItem.mas_right).offset(KMToolbarItemSpace);
  782. }];
  783. }
  784. } else {
  785. if (item.image) {
  786. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  787. make.top.bottom.equalTo(@0);
  788. make.left.equalTo(@0);
  789. }];
  790. } else {
  791. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  792. make.top.bottom.equalTo(@0);
  793. make.left.equalTo(@0);
  794. make.height.offset(40);
  795. }];
  796. }
  797. }
  798. [items addObject:item];
  799. lastItem = item;
  800. posX += item.frame.size.width;
  801. }
  802. self.items = items;
  803. [self addSubview:self.moreButton];
  804. }
  805. - (void)updateView {
  806. if(!self.contentView.superview) {
  807. [self addSubview:self.contentView];
  808. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  809. make.centerX.equalTo(@0);
  810. make.centerY.equalTo(@0);
  811. }];
  812. }
  813. for (KMToolBoxItem *item in self.items) {
  814. [item removeFromSuperview];
  815. }
  816. self.items = nil;
  817. CGFloat posX = 0;
  818. NSMutableArray *items = [NSMutableArray array];
  819. NSArray *itemIdentifiers = nil;
  820. if ([self.delegate respondsToSelector:@selector(toolbarDefaultItemIdentifiers:)]) {
  821. itemIdentifiers = [self.delegate toolbarDefaultItemIdentifiers:self];
  822. }
  823. KMToolBoxItem *lastItem = nil;
  824. for (NSUInteger i=0; i< itemIdentifiers.count - self.invisibleItems.count; i++) {
  825. NSString *itemIdentifier = itemIdentifiers[i];
  826. if (![itemIdentifier isKindOfClass:[NSString class]]) {
  827. return;
  828. }
  829. KMToolBoxItem *item = nil;
  830. if ([itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier]) {
  831. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  832. } else if ([itemIdentifier isEqualToString:KMNewToolbarSpaceItemIdentifier]) {
  833. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  834. item.layer.backgroundColor = [NSColor clearColor].CGColor;
  835. item.imageViewBox.borderColor = [NSColor clearColor];
  836. item.layer.cornerRadius = 0.0;
  837. } else if ([self.delegate respondsToSelector:@selector(toolbar:itemForItemIdentifier:)]) {
  838. item = [self.delegate toolbar:self itemForItemIdentifier:itemIdentifier];
  839. }
  840. if (!item) {
  841. continue;
  842. }
  843. [self.contentView addSubview:item];
  844. if (lastItem) {
  845. if (i == itemIdentifiers.count - 1) {
  846. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  847. make.top.bottom.equalTo(@0);
  848. make.left.equalTo(lastItem.mas_right).offset(KMToolbarItemSpace);
  849. make.right.equalTo(self.contentView.mas_right).offset(0);
  850. }];
  851. } else {
  852. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  853. make.top.bottom.equalTo(@0);
  854. make.left.equalTo(lastItem.mas_right).offset(KMToolbarItemSpace);
  855. }];
  856. }
  857. } else {
  858. if (item.image) {
  859. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  860. make.top.bottom.equalTo(@0);
  861. make.left.equalTo(@0);
  862. }];
  863. } else {
  864. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  865. make.top.bottom.equalTo(@0);
  866. make.left.equalTo(@0);
  867. make.height.offset(40);
  868. }];
  869. }
  870. }
  871. [items addObject:item];
  872. lastItem = item;
  873. posX += item.frame.size.width;
  874. }
  875. self.items = items;
  876. [self addSubview:self.moreButton];
  877. }
  878. - (void)updateLayer {
  879. [super updateLayer];
  880. self.layer.backgroundColor = NSColor.clearColor.CGColor;
  881. }
  882. #pragma mark - View Methods
  883. - (void)viewWillMoveToWindow:(NSWindow *)newWindow {
  884. [super viewWillMoveToWindow:newWindow];
  885. if (newWindow && !_contentView) {
  886. NSArray *leftitemIdentifiers = nil;
  887. if ([self.delegate respondsToSelector:@selector(toolbarLeftDefaultItemIdentifiers:)]) {
  888. leftitemIdentifiers = [self.delegate toolbarLeftDefaultItemIdentifiers:self];
  889. }
  890. if (leftitemIdentifiers.count >0) {
  891. [self updateMainView];
  892. }else {
  893. [self updateView];
  894. }
  895. [self resizeSubviewsWithOldSize:self.frame.size];
  896. //
  897. // [[NSNotificationCenter defaultCenter] addObserver:self
  898. // selector:@selector(toolbarCustomChangeNotification:)
  899. // name:KMToolbarCustomChangeNotification
  900. // object:nil];
  901. NSBox *topLine = [[NSBox alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0.5)];
  902. topLine.boxType = NSBoxSeparator;
  903. topLine.fillColor = NSColor.blackColor;
  904. [self addSubview:topLine];
  905. [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
  906. make.left.right.equalTo(self);
  907. make.bottom.equalTo(self.mas_bottom).offset(-(0.5));
  908. make.height.offset(0.5);
  909. }];
  910. NSBox *bottomLine = [[NSBox alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0.5)];
  911. bottomLine.boxType = NSBoxSeparator;
  912. bottomLine.fillColor = NSColor.blackColor;
  913. [self addSubview:bottomLine];
  914. [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
  915. make.left.right.equalTo(self);
  916. make.top.equalTo(self.mas_top).offset(0.1);
  917. make.height.offset(0.5);
  918. }];
  919. };
  920. if (newWindow) {
  921. [self addTrackingArea];
  922. }
  923. }
  924. - (void)resizeSubviewsWithOldSize:(NSSize)oldSize
  925. {
  926. [super resizeSubviewsWithOldSize:oldSize];
  927. if (!_contentView) {
  928. return;
  929. }
  930. CGFloat moreButtonWidth = 30;
  931. CGFloat leftButtonWidth = 10;
  932. NSRect frame = self.contentView.frame;
  933. __block NSUInteger count = self.items.count;
  934. [self.items enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  935. KMToolBoxItem *item = (KMToolBoxItem *)obj;
  936. if (CGRectGetMaxX(item.frame) <= self.frame.size.width-moreButtonWidth-leftButtonWidth) {
  937. count = idx;
  938. *stop = YES;
  939. }
  940. }];
  941. if (count >= self.items.count) {
  942. return;
  943. }
  944. frame.size.width = CGRectGetMaxX([self.items[count] frame]);
  945. if (count == self.items.count-1) {
  946. frame.origin.x = (self.frame.size.width-frame.size.width)/2.0;
  947. self.moreButton.hidden = YES;
  948. self.visibleItems = self.items;
  949. self.invisibleItems = nil;
  950. [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  951. make.centerX.equalTo(@0);
  952. make.centerY.equalTo(@0);
  953. }];
  954. } else {
  955. frame.origin.x = leftButtonWidth;
  956. // self.moreButton.frame = NSMakeRect(self.frame.size.width-moreButtonWidth, 0,
  957. // moreButtonWidth, self.frame.size.height);
  958. [self.moreButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  959. make.top.bottom.equalTo(@0);
  960. make.width.offset(moreButtonWidth);
  961. make.right.equalTo(self.mas_right).offset(0);
  962. }];
  963. self.moreButton.hidden = NO;
  964. self.visibleItems = [self.items subarrayWithRange:NSMakeRange(0, count+1)];
  965. // self.invisibleItems = [self.items subarrayWithRange:NSMakeRange(count+1, self.items.count-count-1)];
  966. [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  967. make.left.equalTo(self.mas_left).offset(10);
  968. make.centerY.equalTo(@0);
  969. }];
  970. }
  971. self.invisibleItems = @[].mutableCopy;
  972. [self.items enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  973. KMToolBoxItem *item = (KMToolBoxItem *)obj;
  974. if (CGRectGetMaxX(item.frame) <= self.frame.size.width-moreButtonWidth-leftButtonWidth) {
  975. item.hidden = NO;
  976. }else {
  977. item.hidden = YES;
  978. [self.invisibleItems addObject:item];
  979. }
  980. }];
  981. }
  982. #pragma mark - Public Methods
  983. - (void)reloadData
  984. {
  985. NSArray *leftitemIdentifiers = nil;
  986. if ([self.delegate respondsToSelector:@selector(toolbarLeftDefaultItemIdentifiers:)]) {
  987. leftitemIdentifiers = [self.delegate toolbarLeftDefaultItemIdentifiers:self];
  988. }
  989. if (leftitemIdentifiers.count >0) {
  990. [self updateMainView];
  991. }else {
  992. [self updateView];
  993. }
  994. [self resizeSubviewsWithOldSize:self.frame.size];
  995. }
  996. @end