KMToolbar.m 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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 (![self.window isKeyWindow]) {
  468. return;
  469. }
  470. if (!_isSelected && !self.needExpandAction) {
  471. self.layer.backgroundColor = self.normalBackgroundColor.CGColor;
  472. if(self.image && self.alternateImage) {
  473. self.imageViewBtn.image = self.kNormalImage ? : self.image;
  474. }
  475. }
  476. if(self.needExpandAction && !_isSelected) {
  477. self.layer.backgroundColor = self.normalBackgroundColor.CGColor;
  478. if(self.image && self.alternateImage) {
  479. self.imageViewBtn.image = self.kNormalImage ? : self.image;
  480. }
  481. self.needExpandButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnTriDownNor"];
  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. @end