1//
2//  KBFolderUsersListView.m
3//  Keybase
4//
5//  Created by Gabriel on 4/30/15.
6//  Copyright (c) 2015 Gabriel Handford. All rights reserved.
7//
8
9#import "KBFolderUsersListView.h"
10
11#import "KBUserPermission.h"
12#import "KBUserImageView.h"
13
14@interface KBFolderUsersListView ()
15@end
16
17@implementation KBFolderUsersListView
18
19- (void)viewInit {
20  [super viewInit];
21
22  self.scrollView.borderType = NSBezelBorder;
23  self.view.focusRingType = NSFocusRingTypeNone;
24
25  self.view.usesAlternatingRowBackgroundColors = YES;
26  self.view.columnAutoresizingStyle = NSTableViewFirstColumnOnlyAutoresizingStyle;
27  self.view.intercellSpacing = CGSizeZero;
28
29  NSTableColumn *nameColumn = [[NSTableColumn alloc] initWithIdentifier:@"name"];
30  nameColumn.title = @"Name";
31  nameColumn.minWidth = 100;
32  [self.view addTableColumn:nameColumn];
33  NSTableColumn *permissionColumn = [[NSTableColumn alloc] initWithIdentifier:@"permission"];
34  permissionColumn.title = @"Permissions";
35  permissionColumn.minWidth = 160;
36  [self.view addTableColumn:permissionColumn];
37
38  self.view.rowHeight = 32;
39
40  KBUserPermission *test = [[KBUserPermission alloc] init];
41  test.user = [[KBRUser alloc] init];
42  test.user.username = @"t_alice";
43  test.permission = @"Read only";
44  [self.dataSource addObjects:@[test]];
45  [self reloadData];
46}
47
48- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
49  KBUserPermission *userPermission = [self.dataSource objectAtIndexPath:[NSIndexPath indexPathForItem:row inSection:0]];
50
51  if ([tableColumn.identifier isEqualToString:@"name"]) {
52    NSString *identifier = @"KBFolderUsersListView.name";
53    KBUserPermissionLabel *view = [self.view makeViewWithIdentifier:identifier owner:self];
54    if (!view) {
55      view = [[KBUserPermissionLabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
56      view.identifier = identifier;
57    }
58    view.style = KBImageLabelStyleLarge;
59    [view setUserPermission:userPermission];
60    return view;
61  } else if ([tableColumn.identifier isEqualToString:@"permission"]) {
62
63    NSPopUpButton *button = [[NSPopUpButton alloc] initWithFrame:CGRectMake(0, 0, 160, 20) pullsDown:NO];
64    button.bordered = NO;
65    button.font = [KBImageLabel fontForStyle:KBImageLabelStyleLarge];
66    [button addItemsWithTitles:@[@"Read only", @"Write"]];
67    [button selectItemAtIndex:0];
68    return button;
69  } else {
70    NSAssert(NO, @"Unhandled table column");
71    return nil;
72  }
73}
74
75@end
76
77
78@implementation KBUserPermissionLabel
79
80- (void)viewInit {
81  [super viewInit];
82  self.imageView.roundedRatio = 1.0;
83}
84
85- (void)setUserPermission:(KBUserPermission *)userPermission {
86  [self.nameLabel setText:userPermission.user.username font:[self.class fontForStyle:self.style] color:KBAppearance.currentAppearance.textColor alignment:NSLeftTextAlignment lineBreakMode:NSLineBreakByTruncatingTail];
87  [self.imageView kb_setUsername:userPermission.user.username];
88  [self setNeedsLayout];
89}
90
91@end
92