KMToolbar.m 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. //
  2. // KMToolbar.m
  3. // SignFlow
  4. //
  5. // Copyright © 2022 Kdan Mobile. All rights reserved.
  6. //
  7. #import "KMToolbar.h"
  8. #import <Masonry/Masonry.h>
  9. #import <PDF_Master-Swift.h>
  10. NSString *KMNewToolbarSpaceItemIdentifier = @"KMNewToolbarSpaceItemIdentifier";
  11. NSString *KMToolbarDividerItemIdentifier = @"KMToolbarDividerItemIdentifier";
  12. #define KMToolbarSpaceItemWidth 30.0
  13. #define KMToolbarLineItemWidth 1.0
  14. #define KMToolbarItemSpace (6.0+2.0)
  15. #pragma mark - KMToolbarClickButton
  16. @implementation KMToolbarClickButton
  17. @end
  18. #pragma mark - KMToolBoxItem
  19. @interface KMToolBoxItem()<KMCustomButtonPopMenuViewControllerDelegate,KMCustomButtonPopMenuViewControllerDataSources,NSPopoverDelegate>
  20. @property(nonatomic,retain) NSBox *imageViewBox;
  21. @property(nonatomic,retain) NSButton *imageViewBtn;
  22. @property(nonatomic,retain) NSButton *nameBtn;
  23. @property(nonatomic,retain) KMToolbarClickButton *clickButton;
  24. @property(nonatomic,retain) NSButton *needExpandButton;
  25. @property (nonatomic,retain) NSPopover *popOver;
  26. @property (nonatomic,retain) KMCustomButtonPopMenuViewController *menuViewController;
  27. @property (nonatomic,retain) NSString *itemIdentifier;
  28. @property (nonatomic,retain) NSImage *kNormalImage;
  29. @property (nonatomic,retain) NSString *originalHelpTip;
  30. @end
  31. @implementation KMToolBoxItem
  32. - (void)dealloc {
  33. [[NSNotificationCenter defaultCenter] removeObserver:self];
  34. }
  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 setTitleColorWithColor:[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 setTitleColorWithColor:[KMAppearance titleColor] font:nil];
  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 setTitleColorWithColor:[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 setTitleColorWithColor:[KMAppearance titleColor] font:nil];
  277. }
  278. }
  279. }
  280. }
  281. - (void)setIsMainTool:(BOOL)isMainTool {
  282. _isMainTool = isMainTool;
  283. if (_isMainTool) {
  284. [self.nameBtn setTitleColorWithColor:[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]];
  285. } else {
  286. [self.nameBtn setTitleColorWithColor:[KMAppearance titleColor] font:nil];
  287. }
  288. [self layoutView];
  289. }
  290. - (void)setPopOver:(NSPopover *)popOver
  291. {
  292. if (![_popOver isEqual:popOver]) {
  293. _popOver = popOver;
  294. if (popOver) {
  295. self.layer.backgroundColor = self.selectedBackgroundColor.CGColor;
  296. } else {
  297. if (self.isSelected) {
  298. self.layer.backgroundColor = self.selectedBackgroundColor.CGColor;
  299. }else {
  300. self.layer.backgroundColor = self.normalBackgroundColor.CGColor;
  301. }
  302. }
  303. }
  304. }
  305. - (NSButton *)imageViewBtn
  306. {
  307. if (!_imageViewBtn) {
  308. _imageViewBtn = [[NSButton alloc] init];
  309. _imageViewBtn.bezelStyle = NSBezelStyleRegularSquare;
  310. _imageViewBtn.bordered = NO;
  311. _imageViewBtn.imagePosition = NSImageOnly;
  312. }
  313. return _imageViewBtn;
  314. }
  315. - (KMToolbarClickButton *)clickButton
  316. {
  317. if (!_clickButton) {
  318. _clickButton = [[KMToolbarClickButton alloc] init];
  319. _clickButton.bezelStyle = NSBezelStyleRegularSquare;
  320. _clickButton.bordered = NO;
  321. _clickButton.imagePosition = NSImageOnly;
  322. _clickButton.clickObject = self;
  323. }
  324. return _clickButton;
  325. }
  326. - (NSButton *)needExpandButton
  327. {
  328. if (!_needExpandButton) {
  329. _needExpandButton = [[NSButton alloc] init];
  330. _needExpandButton.bezelStyle = NSBezelStyleRegularSquare;
  331. _needExpandButton.bordered = NO;
  332. _needExpandButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnTriDownNor"];
  333. _needExpandButton.imagePosition = NSImageOnly;
  334. }
  335. return _needExpandButton;
  336. }
  337. - (NSButton *)nameBtn
  338. {
  339. if (!_nameBtn) {
  340. _nameBtn = [[NSButton alloc] init];
  341. _nameBtn.bezelStyle = NSBezelStyleRegularSquare;
  342. _nameBtn.bordered = NO;
  343. _nameBtn.imagePosition = NSNoImage;
  344. _nameBtn.font = [NSFont systemFontOfSize:12.0];
  345. [_nameBtn setTitle:_titleName ? : @" "];
  346. }
  347. return _nameBtn;
  348. }
  349. -(NSBox *)imageViewBox
  350. {
  351. if (!_imageViewBox) {
  352. _imageViewBox = [[NSBox alloc] init];
  353. [_imageViewBox setBorderWidth:0.0f];
  354. _imageViewBox.contentViewMargins = NSMakeSize(0, 0);
  355. _imageViewBox.boxType = NSBoxCustom;
  356. }
  357. return _imageViewBox;
  358. }
  359. - (NSView *)linView
  360. {
  361. if (!_linView) {
  362. _linView = [[NSView alloc] init];
  363. _linView.wantsLayer = YES;
  364. _linView.layer.backgroundColor = [NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:1].CGColor;
  365. _linView.layer.cornerRadius = 2.0;
  366. }
  367. return _linView;
  368. }
  369. -(void)setImage:(NSImage *)image
  370. {
  371. if (image != _image) {
  372. _image = image;
  373. }
  374. self.imageViewBtn.image = image;
  375. }
  376. -(void)setTitleName:(NSString *)titleName
  377. {
  378. if (titleName != _titleName) {
  379. _titleName = titleName;
  380. }
  381. self.nameBtn.title = titleName ? : KMToolbarDividerItemIdentifier;
  382. [self.nameBtn setTitleColorWithColor:[KMAppearance titleColor] font:nil];
  383. }
  384. - (void)setTarget:(id)target
  385. {
  386. _target = target;
  387. self.clickButton.target = target;
  388. }
  389. - (void)setBtnTag:(NSInteger)btnTag
  390. {
  391. _btnTag = btnTag;
  392. self.clickButton.tag = btnTag;
  393. }
  394. - (void)setBtnAction:(SEL)btnAction
  395. {
  396. _btnAction = btnAction;
  397. self.clickButton.action = btnAction;
  398. }
  399. -(void)setToolTip:(NSString *)toolTip
  400. {
  401. self.clickButton.toolTip = toolTip?:@"";
  402. self.originalHelpTip = self.clickButton.toolTip;
  403. if(self.isShowCustomToolTip) {
  404. self.clickButton.toolTip = @"";
  405. }
  406. }
  407. - (void)setIsShowCustomToolTip:(BOOL)isShowCustomToolTip {
  408. _isShowCustomToolTip = isShowCustomToolTip;
  409. if(isShowCustomToolTip) {
  410. self.clickButton.toolTip = @"";
  411. }
  412. }
  413. - (void)setUnEnabled:(BOOL)unEnabled
  414. {
  415. if (unEnabled) {
  416. self.clickButton.enabled = NO;
  417. self.nameBtn.enabled = NO;
  418. self.imageViewBtn.enabled = NO;
  419. self.needExpandButton.enabled = NO;
  420. } else {
  421. self.clickButton.enabled = YES;
  422. self.nameBtn.enabled = YES;
  423. self.imageViewBtn.enabled = YES;
  424. self.needExpandButton.enabled = YES;
  425. }
  426. }
  427. #pragma mark - Private
  428. - (void)addTrackingArea
  429. {
  430. NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveInKeyWindow owner:self userInfo:nil];
  431. [self addTrackingArea:trackingArea];
  432. }
  433. - (void)mouseEntered:(NSEvent *)event {
  434. [super mouseEntered:event];
  435. if (![self.window isKeyWindow]) {
  436. return;
  437. }
  438. if ([self.itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier] || self.customizeView || self.image == nil) {
  439. return;
  440. }
  441. if (!self.isSelected) {
  442. self.layer.backgroundColor = self.selectedBackgroundColor.CGColor;
  443. if(self.image && self.alternateImage) {
  444. self.kNormalImage = self.image;
  445. self.imageViewBtn.image = self.alternateImage;
  446. if(self.nameBtn.superview) {
  447. [self.nameBtn setTitleColorWithColor:[KMAppearance titleColor] font:nil];
  448. }
  449. }
  450. }
  451. if (self.needExpandAction) {
  452. self.needExpandButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnTriDownSel"];
  453. [self showPop:self];
  454. }else if (_isShowCustomToolTip) {
  455. [self performSelector:@selector(showHUDHint) withObject:nil afterDelay:0.1];
  456. }
  457. }
  458. - (void)mouseExited:(NSEvent *)event {
  459. [super mouseExited:event];
  460. if (!_isSelected && !self.needExpandAction) {
  461. self.layer.backgroundColor = self.normalBackgroundColor.CGColor;
  462. if(self.image && self.alternateImage) {
  463. self.imageViewBtn.image = self.kNormalImage ? : self.image;
  464. }
  465. }
  466. if(self.needExpandAction && !_isSelected) {
  467. self.layer.backgroundColor = self.normalBackgroundColor.CGColor;
  468. if(self.image && self.alternateImage) {
  469. self.imageViewBtn.image = self.kNormalImage ? : self.image;
  470. }
  471. self.needExpandButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnTriDownNor"];
  472. }
  473. if (![self.window isKeyWindow]) {
  474. return;
  475. }
  476. if(self.nameBtn.superview && !_isSelected && !_isMainTool) {
  477. [self.nameBtn setTitleColorWithColor:[KMAppearance titleColor] font:nil];
  478. }
  479. if (_isShowCustomToolTip && !self.needExpandAction) {
  480. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showHUDHint) object:nil];
  481. [self closePop];
  482. }
  483. }
  484. - (void)showPop:(NSView *)sender {
  485. }
  486. - (void)showHUDHint {
  487. // KMToolbarItemPopViewController *popViewController = [[[KMToolbarItemPopViewController alloc] init] autorelease];
  488. // self.popOver = [[[NSPopover alloc] init] autorelease];
  489. // self.popOver.contentViewController = popViewController;
  490. // self.popOver.animates = NO;
  491. // self.popOver.behavior = NSPopoverBehaviorSemitransient;
  492. // self.popOver.backgroundColor = [KMAppearance KMBluegrey01Color];
  493. //
  494. // self.popOver.contentSize = popViewController.view.frame.size;
  495. // [popViewController updateWithHelpTip:self.originalHelpTip];
  496. // [self.popOver showRelativeToRect:self.bounds ofView:self preferredEdge:NSRectEdgeMinY];
  497. }
  498. - (void)windowClosedPop:(NSNotification *)sender
  499. {
  500. if ([sender.object isEqual:self.popOver]) {
  501. self.popOver = nil;
  502. }
  503. }
  504. - (void)closePop {
  505. [self.popOver close];
  506. self.popOver = nil;
  507. }
  508. @end
  509. #pragma mark - KMNewToolbar
  510. @interface KMToolbar ()
  511. @property (nonatomic,retain) NSString *toolbarIdentifier;
  512. @property (nonatomic,retain) NSArray *items;
  513. @property (nonatomic,retain) NSArray *visibleItems;
  514. @property (nonatomic,retain) NSMutableArray *invisibleItems;
  515. @property (nonatomic,retain) NSView *contentView;
  516. @property (nonatomic,retain) NSButton *moreButton;
  517. @end
  518. @implementation KMToolbar
  519. #pragma mark - Init Methods
  520. - (instancetype)initWithIdentifier:(NSString *)identifier
  521. {
  522. if (self = [super init]) {
  523. self.toolbarIdentifier = identifier;
  524. [self addTrackingArea];
  525. }
  526. return self;
  527. }
  528. - (instancetype)initWithCoder:(NSCoder *)decoder
  529. {
  530. if (self = [super initWithCoder:decoder]) {
  531. self.wantsLayer = YES;
  532. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  533. [self addTrackingArea];
  534. }
  535. return self;
  536. }
  537. - (instancetype)initWithFrame:(NSRect)frameRect
  538. {
  539. if (self = [super initWithFrame:frameRect]) {
  540. self.wantsLayer = YES;
  541. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  542. [self addTrackingArea];
  543. }
  544. return self;
  545. }
  546. - (void)dealloc
  547. {
  548. [[NSNotificationCenter defaultCenter] removeObserver:self];
  549. }
  550. -(void)mouseMoved:(NSEvent *)event
  551. {
  552. [self.window mouseMoved:event];
  553. [super mouseMoved:event];
  554. }
  555. - (void)addTrackingArea
  556. {
  557. NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options: NSTrackingInVisibleRect | NSTrackingActiveInKeyWindow | NSTrackingMouseMoved owner:self userInfo:nil];
  558. [self addTrackingArea:trackingArea];
  559. }
  560. #pragma mark - Private Methods
  561. - (NSButton *)moreButton
  562. {
  563. if (!_moreButton) {
  564. _moreButton = [[NSButton alloc] init];
  565. _moreButton.bezelStyle = NSBezelStyleRegularSquare;
  566. _moreButton.bordered = NO;
  567. _moreButton.font = [NSFont systemFontOfSize:20];
  568. _moreButton.title = @" »";
  569. _moreButton.hidden = YES;
  570. _moreButton.target = self;
  571. _moreButton.action = @selector(moreButtonAction:);
  572. _moreButton.wantsLayer = YES;
  573. _moreButton.layer.backgroundColor = [NSColor colorWithRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1].CGColor;
  574. }
  575. return _moreButton;
  576. }
  577. - (void)moreButtonAction:(id)sender
  578. {
  579. NSMenu *menu = [[NSMenu alloc] init];
  580. for (KMToolBoxItem *item in self.invisibleItems) {
  581. if ([item.itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier]) {
  582. [menu addItem:[NSMenuItem separatorItem]];
  583. } else if (item.menuFormRepresentation) {
  584. [menu addItem:item.menuFormRepresentation];
  585. }
  586. }
  587. [menu popUpMenuPositioningItem:menu.itemArray.firstObject
  588. atLocation:NSMakePoint(CGRectGetMaxX(self.moreButton.frame),
  589. CGRectGetMaxY(self.moreButton.frame)-5)
  590. inView:self];
  591. }
  592. - (NSView *)contentView
  593. {
  594. if (!_contentView) {
  595. _contentView = [[NSView alloc] init];
  596. }
  597. return _contentView;
  598. }
  599. - (void)updateMainView
  600. {
  601. NSView *centerView = [[NSView alloc] init];
  602. [self addSubview:centerView];
  603. NSView *leftView = [[NSView alloc] init];
  604. leftView.wantsLayer = YES;
  605. leftView.layer.backgroundColor = [NSColor colorWithRed:247/255.f green:248/255.f blue:250/255.f alpha:1.f].CGColor;
  606. [self addSubview:leftView];
  607. [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
  608. make.left.equalTo(self.mas_left).offset(0);
  609. make.top.equalTo(self.mas_top).offset(0);
  610. make.bottom.equalTo(self.mas_bottom).offset(0);
  611. }];
  612. NSArray *leftitemIdentifiers = nil;
  613. if ([self.delegate respondsToSelector:@selector(toolbarLeftDefaultItemIdentifiers:)]) {
  614. leftitemIdentifiers = [self.delegate toolbarLeftDefaultItemIdentifiers:self];
  615. }
  616. KMToolBoxItem *leftlastItem = nil;
  617. for (NSUInteger i=0; i< leftitemIdentifiers.count; i++) {
  618. NSString *itemIdentifier = leftitemIdentifiers[i];
  619. if (![itemIdentifier isKindOfClass:[NSString class]]) {
  620. return;
  621. }
  622. KMToolBoxItem *item = nil;
  623. if ([itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier]) {
  624. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  625. } else if ([itemIdentifier isEqualToString:KMNewToolbarSpaceItemIdentifier]) {
  626. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  627. item.layer.backgroundColor = [NSColor clearColor].CGColor;
  628. item.imageViewBox.borderColor = [NSColor clearColor];
  629. item.layer.cornerRadius = 0.0;
  630. } else if ([self.delegate respondsToSelector:@selector(toolbar:itemForItemIdentifier:)]) {
  631. item = [self.delegate toolbar:self itemForItemIdentifier:itemIdentifier];
  632. }
  633. if (!item) {
  634. continue;
  635. }
  636. [leftView addSubview:item];
  637. if (leftlastItem) {
  638. if (i == leftitemIdentifiers.count - 1) {
  639. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  640. make.top.equalTo(@10);
  641. make.bottom.equalTo(@(-10));
  642. make.left.equalTo(leftlastItem.mas_right).offset(KMToolbarItemSpace);
  643. make.right.equalTo(leftView.mas_right).offset(0);
  644. }];
  645. } else {
  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. }];
  651. }
  652. } else {
  653. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  654. make.top.equalTo(@10);
  655. make.bottom.equalTo(@(-10));
  656. make.left.equalTo(@20);
  657. }];
  658. }
  659. leftlastItem = item;
  660. }
  661. NSView *rightView = [[NSView alloc] init];
  662. [self addSubview:rightView];
  663. rightView.wantsLayer = YES;
  664. rightView.layer.backgroundColor = [NSColor colorWithRed:247/255.f green:248/255.f blue:250/255.f alpha:1.f].CGColor;
  665. [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
  666. make.right.equalTo(self.mas_right).offset(0);
  667. make.top.equalTo(self.mas_top).offset(0);
  668. make.bottom.equalTo(self.mas_bottom).offset(0);
  669. }];
  670. NSArray *rightitemIdentifiers = nil;
  671. if ([self.delegate respondsToSelector:@selector(toolbarRightDefaultItemIdentifiers:)]) {
  672. rightitemIdentifiers = [self.delegate toolbarRightDefaultItemIdentifiers:self];
  673. }
  674. KMToolBoxItem *rightlastItem = nil;
  675. for (NSUInteger i=0; i< rightitemIdentifiers.count; i++) {
  676. NSString *itemIdentifier = rightitemIdentifiers[i];
  677. if (![itemIdentifier isKindOfClass:[NSString class]]) {
  678. return;
  679. }
  680. KMToolBoxItem *item = nil;
  681. if ([itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier]) {
  682. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  683. } else if ([itemIdentifier isEqualToString:KMNewToolbarSpaceItemIdentifier]) {
  684. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  685. item.layer.backgroundColor = [NSColor clearColor].CGColor;
  686. item.imageViewBox.borderColor = [NSColor clearColor];
  687. item.layer.cornerRadius = 0.0;
  688. } else if ([self.delegate respondsToSelector:@selector(toolbar:itemForItemIdentifier:)]) {
  689. item = [self.delegate toolbar:self itemForItemIdentifier:itemIdentifier];
  690. }
  691. if (!item) {
  692. continue;
  693. }
  694. [rightView addSubview:item];
  695. if (rightlastItem) {
  696. if (i == rightitemIdentifiers.count - 1) {
  697. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  698. make.top.equalTo(@6);
  699. make.bottom.equalTo(@(-6));
  700. make.left.equalTo(rightlastItem.mas_right).offset(KMToolbarItemSpace);
  701. make.right.equalTo(rightView.mas_right).offset(-20);
  702. }];
  703. } else {
  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. }];
  709. }
  710. } else {
  711. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  712. make.top.equalTo(@6);
  713. make.bottom.equalTo(@(-6));
  714. make.left.equalTo(@0);
  715. }];
  716. }
  717. rightlastItem = item;
  718. }
  719. // NSView *centerView = [[NSView alloc] init];
  720. // [self addSubview:centerView];
  721. [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
  722. // make.right.equalTo(rightView.mas_left).offset(0);
  723. make.right.mas_equalTo(0);
  724. make.top.equalTo(self.mas_top).offset(0);
  725. make.bottom.equalTo(self.mas_bottom).offset(0);
  726. // make.left.equalTo(leftView.mas_right).offset(0);
  727. make.left.mas_equalTo(0);
  728. }];
  729. if(!self.contentView.superview) {
  730. [centerView addSubview:self.contentView];
  731. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  732. make.centerX.equalTo(@0);
  733. make.centerY.equalTo(@0);
  734. }];
  735. }
  736. for (KMToolBoxItem *item in self.items) {
  737. [item removeFromSuperview];
  738. }
  739. self.items = nil;
  740. CGFloat posX = 0;
  741. NSMutableArray *items = [NSMutableArray array];
  742. NSArray *itemIdentifiers = nil;
  743. if ([self.delegate respondsToSelector:@selector(toolbarDefaultItemIdentifiers:)]) {
  744. itemIdentifiers = [self.delegate toolbarDefaultItemIdentifiers:self];
  745. }
  746. KMToolBoxItem *lastItem = nil;
  747. for (NSUInteger i=0; i< itemIdentifiers.count - self.invisibleItems.count; i++) {
  748. NSString *itemIdentifier = itemIdentifiers[i];
  749. if (![itemIdentifier isKindOfClass:[NSString class]]) {
  750. return;
  751. }
  752. KMToolBoxItem *item = nil;
  753. if ([itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier]) {
  754. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  755. } else if ([itemIdentifier isEqualToString:KMNewToolbarSpaceItemIdentifier]) {
  756. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  757. item.layer.backgroundColor = [NSColor clearColor].CGColor;
  758. item.imageViewBox.borderColor = [NSColor clearColor];
  759. item.layer.cornerRadius = 0.0;
  760. } else if ([self.delegate respondsToSelector:@selector(toolbar:itemForItemIdentifier:)]) {
  761. item = [self.delegate toolbar:self itemForItemIdentifier:itemIdentifier];
  762. }
  763. if (!item) {
  764. continue;
  765. }
  766. [self.contentView addSubview:item];
  767. if (lastItem) {
  768. if (i == itemIdentifiers.count - 1) {
  769. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  770. make.top.bottom.equalTo(@0);
  771. make.left.equalTo(lastItem.mas_right).offset(KMToolbarItemSpace);
  772. make.right.equalTo(self.contentView.mas_right).offset(0);
  773. }];
  774. } else {
  775. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  776. make.top.bottom.equalTo(@0);
  777. make.left.equalTo(lastItem.mas_right).offset(KMToolbarItemSpace);
  778. }];
  779. }
  780. } else {
  781. if (item.image) {
  782. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  783. make.top.bottom.equalTo(@0);
  784. make.left.equalTo(@0);
  785. }];
  786. } else {
  787. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  788. make.top.bottom.equalTo(@0);
  789. make.left.equalTo(@0);
  790. make.height.offset(40);
  791. }];
  792. }
  793. }
  794. [items addObject:item];
  795. lastItem = item;
  796. posX += item.frame.size.width;
  797. }
  798. self.items = items;
  799. [self addSubview:self.moreButton];
  800. }
  801. // UI 20 16
  802. // item w 28 h 24
  803. - (void)updateView {
  804. if(!self.contentView.superview) {
  805. [self addSubview:self.contentView];
  806. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  807. make.centerX.equalTo(@0);
  808. make.centerY.equalTo(@0);
  809. }];
  810. }
  811. for (KMToolBoxItem *item in self.items) {
  812. [item removeFromSuperview];
  813. }
  814. self.items = nil;
  815. CGFloat posX = 0;
  816. NSMutableArray *items = [NSMutableArray array];
  817. NSArray *itemIdentifiers = nil;
  818. if ([self.delegate respondsToSelector:@selector(toolbarDefaultItemIdentifiers:)]) {
  819. itemIdentifiers = [self.delegate toolbarDefaultItemIdentifiers:self];
  820. }
  821. KMToolBoxItem *lastItem = nil;
  822. for (NSUInteger i=0; i< itemIdentifiers.count - self.invisibleItems.count; i++) {
  823. NSString *itemIdentifier = itemIdentifiers[i];
  824. if (![itemIdentifier isKindOfClass:[NSString class]]) {
  825. return;
  826. }
  827. KMToolBoxItem *item = nil;
  828. if ([itemIdentifier isEqualToString:KMToolbarDividerItemIdentifier]) {
  829. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  830. } else if ([itemIdentifier isEqualToString:KMNewToolbarSpaceItemIdentifier]) {
  831. item = [[KMToolBoxItem alloc] initWithItemIdentifier:itemIdentifier postition:KMToolBoxImagePosition_Only withPopMenu:nil];
  832. item.layer.backgroundColor = [NSColor clearColor].CGColor;
  833. item.imageViewBox.borderColor = [NSColor clearColor];
  834. item.layer.cornerRadius = 0.0;
  835. } else if ([self.delegate respondsToSelector:@selector(toolbar:itemForItemIdentifier:)]) {
  836. item = [self.delegate toolbar:self itemForItemIdentifier:itemIdentifier];
  837. }
  838. if (!item) {
  839. continue;
  840. }
  841. [self.contentView addSubview:item];
  842. if (lastItem) {
  843. if (i == itemIdentifiers.count - 1) {
  844. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  845. make.top.bottom.equalTo(@0);
  846. make.left.equalTo(lastItem.mas_right).offset(KMToolbarItemSpace);
  847. make.right.equalTo(self.contentView.mas_right).offset(0);
  848. }];
  849. } else {
  850. if ([itemIdentifier isEqualToString:KMNewToolbarSpaceItemIdentifier]) {
  851. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  852. make.top.bottom.equalTo(@0);
  853. make.left.equalTo(lastItem.mas_right).offset(2);
  854. }];
  855. } else {
  856. if ([lastItem.itemIdentifier isEqualToString:KMNewToolbarSpaceItemIdentifier]) {
  857. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  858. make.top.bottom.equalTo(@0);
  859. make.left.equalTo(lastItem.mas_right).offset(2);
  860. }];
  861. } else {
  862. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  863. make.top.bottom.equalTo(@0);
  864. make.left.equalTo(lastItem.mas_right).offset(KMToolbarItemSpace);
  865. }];
  866. }
  867. }
  868. }
  869. } else {
  870. if (item.image) {
  871. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  872. make.top.bottom.equalTo(@0);
  873. make.left.equalTo(@0);
  874. }];
  875. } else {
  876. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  877. make.top.bottom.equalTo(@0);
  878. make.left.equalTo(@0);
  879. make.height.offset(40);
  880. }];
  881. }
  882. }
  883. [items addObject:item];
  884. lastItem = item;
  885. posX += item.frame.size.width;
  886. }
  887. self.items = items;
  888. [self addSubview:self.moreButton];
  889. }
  890. - (void)updateLayer {
  891. [super updateLayer];
  892. self.layer.backgroundColor = NSColor.clearColor.CGColor;
  893. }
  894. #pragma mark - View Methods
  895. - (void)viewWillMoveToWindow:(NSWindow *)newWindow {
  896. [super viewWillMoveToWindow:newWindow];
  897. if (newWindow && !_contentView) {
  898. NSArray *leftitemIdentifiers = nil;
  899. if ([self.delegate respondsToSelector:@selector(toolbarLeftDefaultItemIdentifiers:)]) {
  900. leftitemIdentifiers = [self.delegate toolbarLeftDefaultItemIdentifiers:self];
  901. }
  902. if (leftitemIdentifiers.count >0) {
  903. [self updateMainView];
  904. }else {
  905. [self updateView];
  906. }
  907. [self resizeSubviewsWithOldSize:self.frame.size];
  908. //
  909. // [[NSNotificationCenter defaultCenter] addObserver:self
  910. // selector:@selector(toolbarCustomChangeNotification:)
  911. // name:KMToolbarCustomChangeNotification
  912. // object:nil];
  913. NSBox *topLine = [[NSBox alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0.5)];
  914. topLine.boxType = NSBoxSeparator;
  915. topLine.fillColor = NSColor.blackColor;
  916. [self addSubview:topLine];
  917. [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
  918. make.left.right.equalTo(self);
  919. make.bottom.equalTo(self.mas_bottom).offset(-(0.5));
  920. make.height.offset(0.5);
  921. }];
  922. NSBox *bottomLine = [[NSBox alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0.5)];
  923. bottomLine.boxType = NSBoxSeparator;
  924. bottomLine.fillColor = NSColor.blackColor;
  925. [self addSubview:bottomLine];
  926. [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
  927. make.left.right.equalTo(self);
  928. make.top.equalTo(self.mas_top).offset(0.1);
  929. make.height.offset(0.5);
  930. }];
  931. };
  932. if (newWindow) {
  933. [self addTrackingArea];
  934. }
  935. }
  936. - (void)resizeSubviewsWithOldSize:(NSSize)oldSize
  937. {
  938. [super resizeSubviewsWithOldSize:oldSize];
  939. if (!_contentView) {
  940. return;
  941. }
  942. CGFloat moreButtonWidth = 30;
  943. CGFloat leftButtonWidth = 10;
  944. NSRect frame = self.contentView.frame;
  945. __block NSUInteger count = self.items.count;
  946. [self.items enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  947. KMToolBoxItem *item = (KMToolBoxItem *)obj;
  948. if (CGRectGetMaxX(item.frame) <= self.frame.size.width-moreButtonWidth-leftButtonWidth) {
  949. count = idx;
  950. *stop = YES;
  951. }
  952. }];
  953. if (count >= self.items.count) {
  954. return;
  955. }
  956. frame.size.width = CGRectGetMaxX([self.items[count] frame]);
  957. if (count == self.items.count-1) {
  958. frame.origin.x = (self.frame.size.width-frame.size.width)/2.0;
  959. self.moreButton.hidden = YES;
  960. self.visibleItems = self.items;
  961. self.invisibleItems = nil;
  962. [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  963. make.centerX.equalTo(@0);
  964. make.centerY.equalTo(@0);
  965. }];
  966. } else {
  967. frame.origin.x = leftButtonWidth;
  968. // self.moreButton.frame = NSMakeRect(self.frame.size.width-moreButtonWidth, 0,
  969. // moreButtonWidth, self.frame.size.height);
  970. [self.moreButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  971. make.top.bottom.equalTo(@0);
  972. make.width.offset(moreButtonWidth);
  973. make.right.equalTo(self.mas_right).offset(0);
  974. }];
  975. self.moreButton.hidden = NO;
  976. self.visibleItems = [self.items subarrayWithRange:NSMakeRange(0, count+1)];
  977. // self.invisibleItems = [self.items subarrayWithRange:NSMakeRange(count+1, self.items.count-count-1)];
  978. [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  979. make.left.equalTo(self.mas_left).offset(10);
  980. make.centerY.equalTo(@0);
  981. }];
  982. }
  983. self.invisibleItems = @[].mutableCopy;
  984. [self.items enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  985. KMToolBoxItem *item = (KMToolBoxItem *)obj;
  986. if (CGRectGetMaxX(item.frame) <= self.frame.size.width-moreButtonWidth-leftButtonWidth) {
  987. item.hidden = NO;
  988. }else {
  989. item.hidden = YES;
  990. [self.invisibleItems addObject:item];
  991. }
  992. }];
  993. }
  994. #pragma mark - Public Methods
  995. - (void)reloadData
  996. {
  997. NSArray *leftitemIdentifiers = nil;
  998. if ([self.delegate respondsToSelector:@selector(toolbarLeftDefaultItemIdentifiers:)]) {
  999. leftitemIdentifiers = [self.delegate toolbarLeftDefaultItemIdentifiers:self];
  1000. }
  1001. if (leftitemIdentifiers.count >0) {
  1002. [self updateMainView];
  1003. }else {
  1004. [self updateView];
  1005. }
  1006. [self resizeSubviewsWithOldSize:self.frame.size];
  1007. }
  1008. - (KMToolBoxItem *)itemForItemIdentifier:(NSString *)itemIdentifier {
  1009. for (KMToolBoxItem *item in self.items) {
  1010. if ([item.itemIdentifier isEqualToString:itemIdentifier]) {
  1011. return item;
  1012. }
  1013. }
  1014. return nil;
  1015. }
  1016. @end