KMToolbar.m 41 KB

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