OLD | NEW |
---|---|
(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_view_controller_progress.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 scoped_nsobject<NSAttributedString>([string2 retain]); | |
Robert Sesek
2012/10/03 19:11:44
This returns a strong reference while the |result|
sail
2012/10/10 02:00:10
Done.
Oops, fixed.
| |
26 if (![string2 length]) | |
27 return scoped_nsobject<NSAttributedString>([string1 retain]); | |
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]; | |
Nico
2012/10/02 03:47:25
ugh!
(I don't know a shorter way to do this eithe
| |
36 return result; | |
37 } | |
38 | |
39 } // namespace | |
40 | |
41 @interface WebIntentViewControllerProgress () | |
42 // Updates the message text field and resizes it to fit the given width. | |
43 - (void)updateTextFieldAndResizeToWidth:(CGFloat)width; | |
44 @end | |
45 | |
46 @implementation WebIntentViewControllerProgress | |
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 | |
90 [self updateTextFieldAndResizeToWidth:width]; | |
91 CGFloat height = progressSize.height + NSHeight([messageTextField_ frame]) + | |
92 kProgressMessageFieldSpacing; | |
93 return NSMakeSize(width, height); | |
94 } | |
95 | |
96 - (void)layoutWithInnerFrame:(NSRect)innerFrame { | |
97 [self updateTextFieldAndResizeToWidth:NSWidth(innerFrame)]; | |
98 | |
99 NSRect progressFrame = [progressIndicator_ frame]; | |
100 progressFrame.origin.x = NSMidX(innerFrame) - NSWidth(progressFrame) / 2.0; | |
101 progressFrame.origin.y = NSMinY(innerFrame) + | |
102 NSHeight(innerFrame) / 3.0 - NSHeight(progressFrame) / 2.0; | |
103 | |
104 NSRect textFrame = [messageTextField_ frame]; | |
105 CGFloat newHeight = NSMaxY(progressFrame) + NSHeight(textFrame) + | |
106 kProgressMessageFieldSpacing; | |
107 if (newHeight > NSHeight(innerFrame)) | |
108 progressFrame.origin.y = NSMinY(innerFrame); | |
109 [progressIndicator_ setFrame:progressFrame]; | |
110 | |
111 textFrame.origin.x = NSMinX(innerFrame); | |
112 textFrame.origin.y = NSMaxY(progressFrame) + kProgressMessageFieldSpacing; | |
113 [messageTextField_ setFrame:textFrame]; | |
114 } | |
115 | |
116 - (void)updateTextFieldAndResizeToWidth:(CGFloat)width { | |
117 NSAttributedString* title = constrained_window::GetAttributedLabelString( | |
118 title_, | |
119 ConstrainedWindow::kBoldTextFontStyle, | |
120 NSCenterTextAlignment, | |
121 NSLineBreakByWordWrapping); | |
122 NSAttributedString* message = constrained_window::GetAttributedLabelString( | |
123 message_, | |
124 ConstrainedWindow::kTextFontStyle, | |
125 NSCenterTextAlignment, | |
126 NSLineBreakByWordWrapping); | |
127 [messageTextField_ setAttributedStringValue:JoinString(title, message)]; | |
128 | |
129 NSRect frame = NSZeroRect; | |
130 frame.size.width = width; | |
131 [messageTextField_ setFrame:frame]; | |
132 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField: | |
133 messageTextField_]; | |
134 } | |
135 | |
136 @end | |
OLD | NEW |