KMToolbar.m 42 KB

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