1// 2// KBPrefFileView.m 3// Keybase 4// 5// Created by Gabriel on 4/6/15. 6// Copyright (c) 2015 Gabriel Handford. All rights reserved. 7// 8 9#import "KBPrefFileView.h" 10#import <Tikppa/Tikppa.h> 11 12@interface KBPrefFileView () 13@property KBLabel *label; 14@property NSTextField *textField; 15@property KBButton *browseButton; 16@property id<KBPreferences> preferences; 17@end 18 19@implementation KBPrefFileView 20 21- (void)viewInit { 22 [super viewInit]; 23 24 _label = [KBLabel label]; 25 [self addSubview:_label]; 26 27 _textField = [[NSTextField alloc] init]; 28 _textField.focusRingType = NSFocusRingTypeNone; 29 //_textField.editable = NO; 30 //_textField.selectable = YES; 31 _textField.font = KBAppearance.currentAppearance.textFont; 32 _textField.lineBreakMode = NSLineBreakByTruncatingHead; 33 [self addSubview:_textField]; 34 35 GHWeakSelf gself = self; 36 _browseButton = [KBButton buttonWithText:@"Browse" style:KBButtonStyleDefault options:KBButtonOptionsToolbar]; 37 _browseButton.targetBlock = ^{ [gself chooseInput]; }; 38 [self addSubview:_browseButton]; 39 40 YOSelf yself = self; 41 self.viewLayout = [YOLayout layoutWithLayoutBlock:^CGSize(id<YOLayout> layout, CGSize size) { 42 CGFloat x = 0; 43 CGFloat y = 0; 44 x += [layout sizeToFitVerticalInFrame:CGRectMake(x, y + 6, 100, 0) view:yself.label].size.width + 10; 45 46 [layout sizeToFitVerticalInFrame:CGRectMake(x, y + 2, size.width - x - 90, 0) view:yself.textField]; 47 48 y += [layout sizeToFitVerticalInFrame:CGRectMake(size.width - 80, y, 80, 0) view:yself.browseButton].size.height; 49 50 //y += [layout sizeToFitVerticalInFrame:CGRectMake(x + 5, y, size.width - x - 20, 0) view:yself.infoLabel].size.height; 51 52 return CGSizeMake(size.width, y); 53 }]; 54} 55 56- (void)setLabelText:(NSString *)labelText identifier:(NSString *)identifier preferences:(id<KBPreferences>)preferences { 57 self.identifier = identifier; 58 self.preferences = preferences; 59 [_label setText:labelText style:KBTextStyleDefault alignment:NSRightTextAlignment lineBreakMode:NSLineBreakByClipping]; 60 NSString *path = [self.preferences valueForIdentifier:self.identifier]; 61 _textField.stringValue = path ? path : @""; 62 [self setNeedsLayout]; 63} 64 65- (void)chooseInput { 66 NSOpenPanel *panel = [NSOpenPanel openPanel]; 67 panel.prompt = @"OK"; 68 panel.title = @"Choose a file..."; 69 panel.allowsMultipleSelection = NO; 70 GHWeakSelf gself = self; 71 [panel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) { 72 if (result == NSFileHandlingPanelOKButton) { 73 for (NSURL *URL in [panel URLs]) { 74 if ([URL isFileURL]) { 75 gself.textField.stringValue = URL.path ? URL.path : @""; 76 } 77 } 78 } 79 }]; 80} 81 82@end 83