123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- //
- // KMPopMenuViewController.m
- // SignFlow
- //
- // Created by Kdan on 2020/10/23.
- //
- #import "KMCustomButtonPopMenuViewController.h"
- #import "NSButton+TitleColor.h"
- #import <Masonry/Masonry.h>
- #import "KMPopMenuButtonCell.h"
- @interface KMPopMenuButton : NSButton
- @end
- @implementation KMPopMenuButton
- - (instancetype)initWithFrame:(NSRect)frameRect {
- self = [super initWithFrame:frameRect];
- if (self) {
- [self addTrackingArea];
- self.wantsLayer = YES;
- self.layer.backgroundColor = [NSColor clearColor].CGColor;
- self.font = [NSFont systemFontOfSize:14];
- }
- return self;
- }
- + (Class)cellClass {
- return [KMPopMenuButtonCell class];
- }
- - (void)addTrackingArea {
- NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveAlways |NSTrackingMouseMoved owner:self userInfo:nil];
- [self addTrackingArea:trackingArea];
- }
- - (void)mouseEntered:(NSEvent *)event {
- [super mouseEntered:event];
-
- if (self.enabled) {
- if (@available(macOS 10.14, *)) {
- self.layer.backgroundColor = [NSColor controlAccentColor].CGColor;
- } else {
- self.layer.backgroundColor = [NSColor blueColor].CGColor;
- }
- [self setTitleColor:[NSColor whiteColor]];
- }
- }
- - (void)mouseExited:(NSEvent *)event {
- [super mouseExited:event];
-
- if (self.enabled) {
- self.layer.backgroundColor = [NSColor clearColor].CGColor;
- [self setTitleColor:[NSColor labelColor]];
- }
- }
- - (NSSize)intrinsicContentSize {
- CGSize size = [super intrinsicContentSize];
- size.width += 45;
- return size;
- }
- - (void)setState:(NSControlStateValue)state {
- [super setState:state];
- if (state == NSControlStateValueOn) {
- if (@available(macOS 10.14, *)) {
- self.layer.backgroundColor = [NSColor controlAccentColor].CGColor;
- } else {
- self.layer.backgroundColor = [NSColor blueColor].CGColor;
- }
- [self setTitleColor:[NSColor whiteColor]];
- } else {
- self.layer.backgroundColor = [NSColor clearColor].CGColor;
- [self setTitleColor:[NSColor labelColor]];
- }
- }
- - (void)setTitle:(NSString *)title {
- [super setTitle:title];
-
- [self setTitleColor:[NSColor labelColor]];
- }
- @end
- @interface KMCustomButtonPopMenuViewController ()
- @end
- @implementation KMCustomButtonPopMenuViewController
- #pragma mark - Life cycle
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- NSInteger numberOfLine = 0;
- if ([self.dataSources respondsToSelector:@selector(numberOfLine)]) {
- numberOfLine = [self.dataSources numberOfLine];
- }
- if (numberOfLine < 1) {
- return;
- }
- NSView *referenceView = self.view;
- for (NSInteger i = 0; i < numberOfLine ; i++) {
- NSImage *image = nil;
- NSString *title = nil;
- if ([self.dataSources respondsToSelector:@selector(imageForLineAtIndex:)]) {
- image = [self.dataSources imageForLineAtIndex:i];
- }
- if ([self.dataSources respondsToSelector:@selector(stringForLineAtIndex:)]) {
- title = [self.dataSources stringForLineAtIndex:i];
- }
- KMPopMenuButton *v = nil;
- if (image) {
- v = [KMPopMenuButton buttonWithTitle:title image:image target:self action:@selector(buttonClicked:)];
- } else {
- v = [KMPopMenuButton buttonWithTitle:title target:self action:@selector(buttonClicked:)];
- }
- v.bordered = NO;
- v.tag = i;
- if ([self.dataSources respondsToSelector:@selector(itemEnableAtIndex:)]) {
- v.enabled = [self.dataSources itemEnableAtIndex:i];
- }
-
- [self.view addSubview:v];
-
- [v mas_makeConstraints:^(MASConstraintMaker *make) {
- if ([referenceView isEqual:self.view]) {
- make.top.equalTo(referenceView).offset(10);
- } else {
- make.top.equalTo(referenceView.mas_bottom).offset(2);
- }
- make.left.right.equalTo(self.view);
- make.height.offset(24);
- }];
- referenceView = v;
-
- if ([self.dataSources respondsToSelector:@selector(needHightLightLineAtIndex:)]) {
- if ([self.dataSources needHightLightLineAtIndex:i]) {
- v.state = NSControlStateValueOn;
- }
- }
-
- if ([self.dataSources respondsToSelector:@selector(needInsertSeperateLineAtIndex:)]) {
- if ([self.dataSources needInsertSeperateLineAtIndex:i]) {
- NSBox *box = [[NSBox alloc] initWithFrame:CGRectZero];
- box.boxType = NSBoxSeparator;
- [self.view addSubview:box];
- [box mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(referenceView.mas_bottom).offset(2);
- make.left.equalTo(self.view).offset(21);
- make.centerX.equalTo(self.view);
- }];
- referenceView = box;
- }
- }
- if (i == numberOfLine - 1) {
- [referenceView mas_updateConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.view).offset(-10);
- }];
- }
- }
- }
- - (void)buttonClicked:(NSButton *)sender {
- if ([self.delegate respondsToSelector:@selector(customViewButtonPopDidSelectIndex:)]) {
- [self.delegate customViewButtonPopDidSelectIndex:sender.tag];
- }
- }
- @end
|