File: sad_tab_view_cocoa.mm

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (198 lines) | stat: -rw-r--r-- 6,530 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) 2012 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.

#include "chrome/browser/ui/cocoa/tab_contents/sad_tab_view_cocoa.h"

#import "base/mac/foundation_util.h"
#include "components/grit/components_scaled_resources.h"
#import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
#import "ui/base/cocoa/controls/blue_label_button.h"
#import "ui/base/cocoa/controls/hyperlink_text_view.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/resource/resource_bundle.h"

namespace {

// Maximum width used by page contents.
const CGFloat kMaxContainerWidth = 600;
// Padding between icon and title.
const CGFloat kIconTitleSpacing = 40;
// Padding between title and message.
const CGFloat kTitleMessageSpacing = 18;
// Padding between message and link.
const CGFloat kMessageLinkSpacing = 50;
// Padding between message and button.
const CGFloat kMessageButtonSpacing = 44;
// Minimum margins on all sides.
const CGFloat kTabMargin = 13;
// Maximum margin on top.
const CGFloat kMaxTopMargin = 130;

NSTextField* MakeLabelTextField(NSRect frame) {
  // Not a scoped_nsobject for easy property access.
  NSTextField* ret = [[[NSTextField alloc] initWithFrame:frame] autorelease];
  ret.autoresizingMask = NSViewWidthSizable;
  ret.editable = NO;
  ret.drawsBackground = NO;
  ret.bezeled = NO;
  return ret;
}

}  // namespace

@interface SadTabContainerView : NSView
@end

@implementation SadTabContainerView
- (BOOL)isFlipped {
  return YES;
}
@end

@interface SadTabView ()<NSTextViewDelegate>
@end

@implementation SadTabView {
  NSView* container_;
  NSTextField* message_;
  HyperlinkTextView* help_;
  NSButton* button_;
  chrome::SadTab* sadTab_;
}

- (instancetype)initWithFrame:(NSRect)frame sadTab:(chrome::SadTab*)sadTab {
  if ((self = [super initWithFrame:frame])) {
    sadTab_ = sadTab;

    self.wantsLayer = YES;
    self.layer.backgroundColor =
        [NSColor colorWithCalibratedWhite:245.0f / 255.0f alpha:1.0].CGColor;
    container_ = [[SadTabContainerView new] autorelease];

    NSImage* iconImage = ResourceBundle::GetSharedInstance()
                             .GetNativeImageNamed(IDR_CRASH_SAD_TAB)
                             .ToNSImage();
    NSImageView* icon = [[NSImageView new] autorelease];
    icon.image = iconImage;
    icon.frameSize = iconImage.size;
    [container_ addSubview:icon];

    NSTextField* title = MakeLabelTextField(
        NSMakeRect(0, NSMaxY(icon.frame) + kIconTitleSpacing, 0, 0));
    title.font = [NSFont systemFontOfSize:24];
    title.textColor =
        [NSColor colorWithCalibratedWhite:38.0f / 255.0f alpha:1.0];

    NSMutableParagraphStyle* titleStyle =
        [[NSMutableParagraphStyle new] autorelease];
    titleStyle.lineSpacing = 6;
    title.attributedStringValue = [[[NSAttributedString alloc]
        initWithString:l10n_util::GetNSString(sadTab->GetTitle())
            attributes:@{NSParagraphStyleAttributeName : titleStyle}]
        autorelease];

    [title sizeToFit];
    [container_ addSubview:title];

    NSFont* messageFont = [NSFont systemFontOfSize:14];
    NSColor* messageColor =
        [NSColor colorWithCalibratedWhite:81.0f / 255.0f alpha:1.0];

    message_ = MakeLabelTextField(NSMakeRect(0, NSMaxY(title.frame), 0, 0));
    message_.frameOrigin =
        NSMakePoint(0, NSMaxY(title.frame) + kTitleMessageSpacing);
    base::mac::ObjCCast<NSCell>(message_.cell).wraps = YES;
    message_.font = messageFont;
    message_.textColor = messageColor;
    message_.stringValue = l10n_util::GetNSString(sadTab->GetMessage());
    [container_ addSubview:message_];

    NSString* helpLinkTitle =
        l10n_util::GetNSString(sadTab->GetHelpLinkTitle());
    help_ = [[[HyperlinkTextView alloc]
        initWithFrame:NSMakeRect(0, 0, 1, message_.font.pointSize + 4)]
        autorelease];
    help_.delegate = self;
    help_.autoresizingMask = NSViewWidthSizable;
    help_.textContainer.lineFragmentPadding = 2;  // To align with message_.
    [help_ setMessage:helpLinkTitle
             withFont:messageFont
         messageColor:messageColor];
    [help_ addLinkRange:NSMakeRange(0, helpLinkTitle.length)
                withURL:@(sadTab->GetHelpLinkURL())
              linkColor:messageColor];
    [help_ sizeToFit];
    [container_ addSubview:help_];

    button_ = [[BlueLabelButton new] autorelease];
    button_.target = self;
    button_.action = @selector(buttonClicked);
    button_.title = l10n_util::GetNSString(sadTab->GetButtonTitle());
    [button_ sizeToFit];
    [container_ addSubview:button_];

    [self addSubview:container_];
    [self resizeSubviewsWithOldSize:self.bounds.size];
  }
  return self;
}

- (BOOL)isOpaque {
  return YES;
}

- (BOOL)isFlipped {
  return YES;
}

- (void)updateLayer {
  // Currently, updateLayer is only called once. If that changes, a DCHECK in
  // SadTab::RecordFirstPaint will pipe up and we should add a guard here.
  sadTab_->RecordFirstPaint();
}

- (void)resizeSubviewsWithOldSize:(NSSize)oldSize {
  [super resizeSubviewsWithOldSize:oldSize];

  NSSize size = self.bounds.size;
  NSSize containerSize = NSMakeSize(
      std::min(size.width - 2 * kTabMargin, kMaxContainerWidth), size.height);

  // Set the container's size first because text wrapping depends on its width.
  container_.frameSize = containerSize;

  // |message_| can wrap to variable number of lines.
  [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:message_];

  help_.frameOrigin =
      NSMakePoint(0, NSMaxY(message_.frame) + kMessageLinkSpacing);

  button_.frameOrigin =
      NSMakePoint(containerSize.width - NSWidth(button_.bounds),
                  NSMaxY(message_.frame) + kMessageButtonSpacing);

  containerSize.height = NSMaxY(button_.frame);
  container_.frameSize = containerSize;

  // Center. Top margin is must be between kTabMargin and kMaxTopMargin.
  container_.frameOrigin = NSMakePoint(
      floor((size.width - containerSize.width) / 2),
      std::min(kMaxTopMargin,
               std::max(kTabMargin,
                        size.height - containerSize.height - kTabMargin)));
}

- (void)buttonClicked {
  sadTab_->PerformAction(chrome::SadTab::Action::BUTTON);
}

// Called when someone clicks on the embedded link.
- (BOOL)textView:(NSTextView*)textView
    clickedOnLink:(id)link
          atIndex:(NSUInteger)charIndex {
  sadTab_->PerformAction(chrome::SadTab::Action::HELP_LINK);
  return YES;
}

@end