Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/browser/ui/cocoa/intents/web_intent_progress_view_controller.mm

Issue 11009017: Mac Web Intents Part 11: Progress view (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "chrome/browser/ui/cocoa/intents/web_intent_progress_view_controller.h"
6
7 #include "base/memory/scoped_nsobject.h"
8 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_control_u tils.h"
9 #import "chrome/browser/ui/cocoa/flipped_view.h"
10 #import "chrome/browser/ui/cocoa/spinner_progress_indicator.h"
11 #import "chrome/browser/ui/constrained_window.h"
12 #include "grit/theme_resources.h"
13 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"
14 #include "ui/base/resource/resource_bundle.h"
15
16 namespace {
17
18 // Vertical space between the progress indicator and the message text field.
19 const CGFloat kProgressMessageFieldSpacing = 15.0;
20
21 // Joins the two strings with a space between them.
22 NSAttributedString* JoinString(NSAttributedString* string1,
23 NSAttributedString* string2) {
24 if (![string1 length])
25 return string2;
26 if (![string2 length])
27 return string1;
28
29 NSMutableAttributedString* result =
30 [[[NSMutableAttributedString alloc] init] autorelease];
31 [result appendAttributedString:string1];
32 scoped_nsobject<NSAttributedString> space(
33 [[NSAttributedString alloc] initWithString:@" "]);
34 [result appendAttributedString:space];
35 [result appendAttributedString:string2];
36 return result;
37 }
38
39 } // namespace
40
41 @interface WebIntentProgressViewController ()
42 // Updates the message text field and resizes it to fit the given width.
43 - (void)updateTextFieldAndResizeToWidth:(CGFloat)width;
44 @end
45
46 @implementation WebIntentProgressViewController
47
48 - (id)init {
49 if ((self = [super init])) {
50 scoped_nsobject<NSView> view(
51 [[FlippedView alloc] initWithFrame:NSZeroRect]);
52 [self setView:view];
53
54 messageTextField_.reset([constrained_window::CreateLabel() retain]);
55 [[self view] addSubview:messageTextField_];
56
57 progressIndicator_.reset(
58 [[SpinnerProgressIndicator alloc] initWithFrame:NSZeroRect]);
59 [progressIndicator_ sizeToFit];
60 [[self view] addSubview:progressIndicator_];
61 }
62 return self;
63 }
64
65 - (SpinnerProgressIndicator*)progressIndicator {
66 return progressIndicator_;
67 }
68
69 - (void)setTitle:(NSString*)title {
70 title_.reset([title retain]);
71 }
72
73 - (void)setMessage:(NSString*)message {
74 message_.reset([message retain]);
75 }
76
77 - (void)setPercentDone:(int)percent {
78 if (percent == -1) {
79 [progressIndicator_ setIsIndeterminate:YES];
80 } else {
81 [progressIndicator_ setIsIndeterminate:NO];
82 [progressIndicator_ setPercentDone:percent];
83 }
84 }
85
86 - (NSSize)minimumSizeForInnerWidth:(CGFloat)innerWidth {
87 NSSize progressSize = [progressIndicator_ frame].size;
88 CGFloat width = std::max(innerWidth, progressSize.width);
89 [self updateTextFieldAndResizeToWidth:width];
90 CGFloat height = progressSize.height + NSHeight([messageTextField_ frame]) +
91 kProgressMessageFieldSpacing;
92 return NSMakeSize(width, height);
93 }
94
95 - (void)layoutSubviewsWithinFrame:(NSRect)innerFrame {
96 [self updateTextFieldAndResizeToWidth:NSWidth(innerFrame)];
97
98 NSRect progressFrame = [progressIndicator_ frame];
99 progressFrame.origin.x = NSMidX(innerFrame) - NSWidth(progressFrame) / 2.0;
100 progressFrame.origin.y = NSMinY(innerFrame) +
101 NSHeight(innerFrame) / 3.0 - NSHeight(progressFrame) / 2.0;
102
103 NSRect textFrame = [messageTextField_ frame];
104 CGFloat newHeight = NSMaxY(progressFrame) + NSHeight(textFrame) +
105 kProgressMessageFieldSpacing;
106 if (newHeight > NSHeight(innerFrame))
107 progressFrame.origin.y = NSMinY(innerFrame);
108 [progressIndicator_ setFrame:progressFrame];
109
110 textFrame.origin.x = NSMinX(innerFrame);
111 textFrame.origin.y = NSMaxY(progressFrame) + kProgressMessageFieldSpacing;
112 [messageTextField_ setFrame:textFrame];
113 }
114
115 - (void)updateTextFieldAndResizeToWidth:(CGFloat)width {
116 NSAttributedString* title = constrained_window::GetAttributedLabelString(
117 title_,
118 ConstrainedWindow::kBoldTextFontStyle,
119 NSCenterTextAlignment,
120 NSLineBreakByWordWrapping);
121 NSAttributedString* message = constrained_window::GetAttributedLabelString(
122 message_,
123 ConstrainedWindow::kTextFontStyle,
124 NSCenterTextAlignment,
125 NSLineBreakByWordWrapping);
126 [messageTextField_ setAttributedStringValue:JoinString(title, message)];
127
128 NSRect frame = NSZeroRect;
129 frame.size.width = width;
130 [messageTextField_ setFrame:frame];
131 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:
132 messageTextField_];
133 }
134
135 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698