summaryrefslogtreecommitdiff
path: root/chromium/ui/app_list/cocoa/current_user_menu_item_view.mm
blob: 6aac34c6a997bfad59afb232f4427dfca3b5608e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ui/app_list/cocoa/current_user_menu_item_view.h"

#include "base/logging.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "grit/ui_resources.h"
#include "ui/app_list/app_list_view_delegate.h"
#include "ui/base/resource/resource_bundle.h"

namespace {

// Padding on the left of the indicator icon.
const CGFloat kMenuLeftMargin = 3;

}

@interface CurrentUserMenuItemView ()

// Adds a text label in the custom view in the menu showing the current user.
- (NSTextField*)addLabelWithFrame:(NSPoint)origin
                        labelText:(const string16&)labelText;

@end

@implementation CurrentUserMenuItemView

- (id)initWithDelegate:(app_list::AppListViewDelegate*)delegate {
  DCHECK(delegate);
  if ((self = [super initWithFrame:NSZeroRect])) {
    NSImage* userImage = ui::ResourceBundle::GetSharedInstance().
        GetNativeImageNamed(IDR_APP_LIST_USER_INDICATOR).AsNSImage();
    NSRect imageRect = NSMakeRect(kMenuLeftMargin, 0, 0, 0);
    imageRect.size = [userImage size];
    base::scoped_nsobject<NSImageView> userImageView(
        [[NSImageView alloc] initWithFrame:imageRect]);
    [userImageView setImage:userImage];
    [self addSubview:userImageView];

    NSPoint labelOrigin = NSMakePoint(NSMaxX(imageRect), 0);
    NSTextField* userField =
        [self addLabelWithFrame:labelOrigin
                      labelText:delegate->GetCurrentUserName()];

    labelOrigin.y = NSMaxY([userField frame]);
    NSTextField* emailField =
        [self addLabelWithFrame:labelOrigin
                      labelText:delegate->GetCurrentUserEmail()];
    [emailField setTextColor:[NSColor disabledControlTextColor]];

    // Size the container view to fit the longest label.
    NSRect labelFrame = [emailField frame];
    if (NSWidth([userField frame]) > NSWidth(labelFrame))
      labelFrame.size.width = NSWidth([userField frame]);
    [self setFrameSize:NSMakeSize(
        NSMaxX(labelFrame) + NSMaxX(imageRect),
        NSMaxY(labelFrame))];
  }
  return self;
}

- (NSTextField*)addLabelWithFrame:(NSPoint)origin
                        labelText:(const string16&)labelText {
  NSRect labelFrame = NSZeroRect;
  labelFrame.origin = origin;
  base::scoped_nsobject<NSTextField> label(
      [[NSTextField alloc] initWithFrame:labelFrame]);
  [label setStringValue:base::SysUTF16ToNSString(labelText)];
  [label setEditable:NO];
  [label setBordered:NO];
  [label setDrawsBackground:NO];
  [label setFont:[NSFont menuFontOfSize:0]];
  [label sizeToFit];
  [self addSubview:label];
  return label.autorelease();
}

- (BOOL)isFlipped {
  return YES;
}

@end