KMCustomButtonPopMenuViewController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // KMPopMenuViewController.m
  3. // SignFlow
  4. //
  5. // Created by Kdan on 2020/10/23.
  6. //
  7. #import "KMCustomButtonPopMenuViewController.h"
  8. #import "NSButton+TitleColor.h"
  9. #import <Masonry/Masonry.h>
  10. #import "KMPopMenuButtonCell.h"
  11. @interface KMPopMenuButton : NSButton
  12. @end
  13. @implementation KMPopMenuButton
  14. - (instancetype)initWithFrame:(NSRect)frameRect {
  15. self = [super initWithFrame:frameRect];
  16. if (self) {
  17. [self addTrackingArea];
  18. self.wantsLayer = YES;
  19. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  20. self.font = [NSFont systemFontOfSize:14];
  21. }
  22. return self;
  23. }
  24. + (Class)cellClass {
  25. return [KMPopMenuButtonCell class];
  26. }
  27. - (void)addTrackingArea {
  28. NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveAlways |NSTrackingMouseMoved owner:self userInfo:nil];
  29. [self addTrackingArea:trackingArea];
  30. }
  31. - (void)mouseEntered:(NSEvent *)event {
  32. [super mouseEntered:event];
  33. if (self.enabled) {
  34. if (@available(macOS 10.14, *)) {
  35. self.layer.backgroundColor = [NSColor controlAccentColor].CGColor;
  36. } else {
  37. self.layer.backgroundColor = [NSColor blueColor].CGColor;
  38. }
  39. [self setTitleColor:[NSColor whiteColor]];
  40. }
  41. }
  42. - (void)mouseExited:(NSEvent *)event {
  43. [super mouseExited:event];
  44. if (self.enabled) {
  45. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  46. [self setTitleColor:[NSColor labelColor]];
  47. }
  48. }
  49. - (NSSize)intrinsicContentSize {
  50. CGSize size = [super intrinsicContentSize];
  51. size.width += 45;
  52. return size;
  53. }
  54. - (void)setState:(NSControlStateValue)state {
  55. [super setState:state];
  56. if (state == NSControlStateValueOn) {
  57. if (@available(macOS 10.14, *)) {
  58. self.layer.backgroundColor = [NSColor controlAccentColor].CGColor;
  59. } else {
  60. self.layer.backgroundColor = [NSColor blueColor].CGColor;
  61. }
  62. [self setTitleColor:[NSColor whiteColor]];
  63. } else {
  64. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  65. [self setTitleColor:[NSColor labelColor]];
  66. }
  67. }
  68. - (void)setTitle:(NSString *)title {
  69. [super setTitle:title];
  70. [self setTitleColor:[NSColor labelColor]];
  71. }
  72. @end
  73. @interface KMCustomButtonPopMenuViewController ()
  74. @end
  75. @implementation KMCustomButtonPopMenuViewController
  76. #pragma mark - Life cycle
  77. - (void)viewDidLoad {
  78. [super viewDidLoad];
  79. NSInteger numberOfLine = 0;
  80. if ([self.dataSources respondsToSelector:@selector(numberOfLine)]) {
  81. numberOfLine = [self.dataSources numberOfLine];
  82. }
  83. if (numberOfLine < 1) {
  84. return;
  85. }
  86. NSView *referenceView = self.view;
  87. for (NSInteger i = 0; i < numberOfLine ; i++) {
  88. NSImage *image = nil;
  89. NSString *title = nil;
  90. if ([self.dataSources respondsToSelector:@selector(imageForLineAtIndex:)]) {
  91. image = [self.dataSources imageForLineAtIndex:i];
  92. }
  93. if ([self.dataSources respondsToSelector:@selector(stringForLineAtIndex:)]) {
  94. title = [self.dataSources stringForLineAtIndex:i];
  95. }
  96. KMPopMenuButton *v = nil;
  97. if (image) {
  98. v = [KMPopMenuButton buttonWithTitle:title image:image target:self action:@selector(buttonClicked:)];
  99. } else {
  100. v = [KMPopMenuButton buttonWithTitle:title target:self action:@selector(buttonClicked:)];
  101. }
  102. v.bordered = NO;
  103. v.tag = i;
  104. if ([self.dataSources respondsToSelector:@selector(itemEnableAtIndex:)]) {
  105. v.enabled = [self.dataSources itemEnableAtIndex:i];
  106. }
  107. [self.view addSubview:v];
  108. [v mas_makeConstraints:^(MASConstraintMaker *make) {
  109. if ([referenceView isEqual:self.view]) {
  110. make.top.equalTo(referenceView).offset(10);
  111. } else {
  112. make.top.equalTo(referenceView.mas_bottom).offset(2);
  113. }
  114. make.left.right.equalTo(self.view);
  115. make.height.offset(24);
  116. }];
  117. referenceView = v;
  118. if ([self.dataSources respondsToSelector:@selector(needHightLightLineAtIndex:)]) {
  119. if ([self.dataSources needHightLightLineAtIndex:i]) {
  120. v.state = NSControlStateValueOn;
  121. }
  122. }
  123. if ([self.dataSources respondsToSelector:@selector(needInsertSeperateLineAtIndex:)]) {
  124. if ([self.dataSources needInsertSeperateLineAtIndex:i]) {
  125. NSBox *box = [[NSBox alloc] initWithFrame:CGRectZero];
  126. box.boxType = NSBoxSeparator;
  127. [self.view addSubview:box];
  128. [box mas_makeConstraints:^(MASConstraintMaker *make) {
  129. make.top.equalTo(referenceView.mas_bottom).offset(2);
  130. make.left.equalTo(self.view).offset(21);
  131. make.centerX.equalTo(self.view);
  132. }];
  133. referenceView = box;
  134. }
  135. }
  136. if (i == numberOfLine - 1) {
  137. [referenceView mas_updateConstraints:^(MASConstraintMaker *make) {
  138. make.bottom.equalTo(self.view).offset(-10);
  139. }];
  140. }
  141. }
  142. }
  143. - (void)buttonClicked:(NSButton *)sender {
  144. if ([self.delegate respondsToSelector:@selector(customViewButtonPopDidSelectIndex:)]) {
  145. [self.delegate customViewButtonPopDidSelectIndex:sender.tag];
  146. }
  147. }
  148. @end