summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.mm
blob: 61fa6aca710b8f8a6769c24a2e3754662c9a6ba6 (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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// 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.

#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h"

#import "base/logging.h"
#import "chrome/browser/ui/chrome_style.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_button.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_control_utils.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_window.h"
#import "chrome/browser/ui/cocoa/hover_close_button.h"
#include "skia/ext/skia_utils_mac.h"
#include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"
#include "ui/base/cocoa/controls/hyperlink_button_cell.h"
#include "ui/base/cocoa/window_size_constants.h"

namespace {

const CGFloat kWindowMinWidth = 500;
const CGFloat kButtonGap = 6;

}  // namespace

@interface ConstrainedWindowAlert ()
// Position the alert buttons within the given window width.
- (void)layoutButtonsWithWindowWidth:(CGFloat)windowWidth;
// Resize the given text field to fit within the window and position it starting
// at |yPos|. Returns the new value of yPos.
- (CGFloat)layoutTextField:(NSTextField*)textField
                      yPos:(CGFloat)yPos
               windowWidth:(CGFloat)windowWidth;
// If a link has been set, resizes the link to fit within the window and
// position it starting at |yPos|. Returns the new value of yPos.
- (CGFloat)layoutLinkAtYPos:(CGFloat)yPos
                windowWidth:(CGFloat)windowWidth;
// Positions the accessory view starting at yPos. Returns the new value of yPos.
- (CGFloat)layoutAccessoryViewAtYPos:(CGFloat)yPos;
// Update the position of the close button.
- (void)layoutCloseButtonWithWindowWidth:(CGFloat)windowWidth
                            windowHeight:(CGFloat)windowHeight;
@end

@implementation ConstrainedWindowAlert

- (id)init {
  if ((self = [super init])) {
    window_.reset([[ConstrainedWindowCustomWindow alloc]
        initWithContentRect:ui::kWindowSizeDeterminedLater]);
    NSView* contentView = [window_ contentView];

    informativeTextField_.reset([constrained_window::CreateLabel() retain]);
    [contentView addSubview:informativeTextField_];
    messageTextField_.reset([constrained_window::CreateLabel() retain]);
    [contentView addSubview:messageTextField_];

    closeButton_.reset(
        [[WebUIHoverCloseButton alloc] initWithFrame:NSZeroRect]);
    [contentView addSubview:closeButton_];
  }
  return self;
}

- (NSString*)informativeText {
  return [informativeTextField_ stringValue];
}

- (void)setInformativeText:(NSString*)string {
  [informativeTextField_ setAttributedStringValue:
      constrained_window::GetAttributedLabelString(
          string,
          chrome_style::kTextFontStyle,
          NSNaturalTextAlignment,
          NSLineBreakByWordWrapping)];
}

- (NSString*)messageText {
  return [messageTextField_ stringValue];
}

- (void)setMessageText:(NSString*)string {
  [messageTextField_ setAttributedStringValue:
      constrained_window::GetAttributedLabelString(
          string,
          chrome_style::kTitleFontStyle,
          NSNaturalTextAlignment,
          NSLineBreakByWordWrapping)];
}

- (void)setLinkText:(NSString*)text target:(id)target action:(SEL)action {
  if (![text length]) {
    [linkView_ removeFromSuperview];
    linkView_.reset(nil);
    return;
  }

  if (!linkView_.get()) {
    linkView_.reset(
        [[HyperlinkButtonCell buttonWithString:[NSString string]] retain]);
    [[window_ contentView] addSubview:linkView_];
  }

  [linkView_ setTitle:text];
  [linkView_ setTarget:target];
  [linkView_ setAction:action];
}

- (NSView*)accessoryView {
  return accessoryView_;
}

- (void)setAccessoryView:(NSView*)accessoryView {
  [accessoryView_ removeFromSuperview];
  accessoryView_.reset([accessoryView retain]);
  [[window_ contentView] addSubview:accessoryView_];
}

- (NSArray*)buttons {
  return buttons_;
}

- (NSButton*)closeButton {
  return closeButton_;
}

- (NSWindow*)window {
  return window_;
}

- (void)addButtonWithTitle:(NSString*)title
             keyEquivalent:(NSString*)keyEquivalent
                    target:(id)target
                    action:(SEL)action {
  if (!buttons_.get())
    buttons_.reset([[NSMutableArray alloc] init]);
  base::scoped_nsobject<NSButton> button(
      [[ConstrainedWindowButton alloc] initWithFrame:NSZeroRect]);
  [button setTitle:title];
  [button setKeyEquivalent:keyEquivalent];
  [button setTarget:target];
  [button setAction:action];
  [buttons_ addObject:button];
  [[window_ contentView] addSubview:button];
}

- (void)layout {
  // Button width.
  CGFloat buttonWidth = 0;
  for (NSButton* button in buttons_.get()) {
    [button sizeToFit];
    NSSize size = [button frame].size;
    buttonWidth += size.width;
  }
  if ([buttons_ count])
    buttonWidth += ([buttons_ count] - 1) * kButtonGap;

  // Window width.
  CGFloat windowWidth = buttonWidth;
  if (accessoryView_.get())
    windowWidth = std::max(windowWidth, NSWidth([accessoryView_ frame]));
  windowWidth += chrome_style::kHorizontalPadding * 2;
  windowWidth = std::max(windowWidth, kWindowMinWidth);

  // Layout controls.
  [self layoutButtonsWithWindowWidth:windowWidth];
  CGFloat curY = [buttons_ count] ? NSMaxY([[buttons_ lastObject] frame])
      : chrome_style::kClientBottomPadding;
  CGFloat availableMessageWidth =
      windowWidth - chrome_style::GetCloseButtonSize() - kButtonGap;
  curY = [self layoutLinkAtYPos:curY
                    windowWidth:availableMessageWidth];
  curY = [self layoutAccessoryViewAtYPos:curY];
  curY = [self layoutTextField:informativeTextField_
                          yPos:curY
                   windowWidth:windowWidth];
  curY = [self layoutTextField:messageTextField_
                          yPos:curY
                   windowWidth:availableMessageWidth];

  CGFloat windowHeight = curY + chrome_style::kTitleTopPadding;
  [self layoutCloseButtonWithWindowWidth:windowWidth
                            windowHeight:windowHeight];

  // Update window frame.
  NSRect windowFrame = NSMakeRect(0, 0, windowWidth, windowHeight);
  windowFrame = [window_ frameRectForContentRect:windowFrame];
  [window_ setFrame:windowFrame display:NO];
}

- (void)layoutButtonsWithWindowWidth:(CGFloat)windowWidth {
  // Layout first 2 button right to left.
  CGFloat curX = windowWidth - chrome_style::kHorizontalPadding;
  const int buttonCount = [buttons_ count];
  for (int i = 0; i < std::min(2, buttonCount); ++i) {
    NSButton* button = [buttons_ objectAtIndex:i];
    NSRect rect = [button frame];
    rect.origin.x = curX - NSWidth(rect);
    rect.origin.y = chrome_style::kClientBottomPadding;
    [button setFrameOrigin:rect.origin];
    curX = NSMinX(rect) - kButtonGap;
  }

  // Layout remaining buttons left to right.
  curX = chrome_style::kHorizontalPadding;
  for (int i = buttonCount - 1; i >= 2; --i) {
    NSButton* button = [buttons_ objectAtIndex:i];
    [button setFrameOrigin:
        NSMakePoint(curX, chrome_style::kClientBottomPadding)];
    curX += NSMaxX([button frame]) + kButtonGap;
  }
}

- (CGFloat)layoutTextField:(NSTextField*)textField
                      yPos:(CGFloat)yPos
               windowWidth:(CGFloat)windowWidth {
  if (![[textField stringValue] length]) {
    [textField setHidden:YES];
    return yPos;
  }

  [textField setHidden:NO];
  NSRect rect;
  rect.origin.y = yPos + chrome_style::kRowPadding;
  rect.origin.x = chrome_style::kHorizontalPadding;
  rect.size.width = windowWidth -
      chrome_style::kHorizontalPadding * 2;
  rect.size.height = 1;
  [textField setFrame:rect];
  [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:textField];
  return NSMaxY([textField frame]);
}

- (CGFloat)layoutLinkAtYPos:(CGFloat)yPos
                windowWidth:(CGFloat)windowWidth {
  if (!linkView_.get())
    return yPos;

  NSRect availableBounds = NSMakeRect(
      0,
      0,
      windowWidth - chrome_style::kHorizontalPadding * 2,
      CGFLOAT_MAX);
  NSSize size = [[linkView_ cell] cellSizeForBounds:availableBounds];

  NSRect rect;
  rect.origin.y = yPos + chrome_style::kRowPadding;
  rect.origin.x = chrome_style::kHorizontalPadding;
  rect.size = size;
  [linkView_ setFrame:rect];
  return NSMaxY([linkView_ frame]);
}

- (CGFloat)layoutAccessoryViewAtYPos:(CGFloat)yPos {
  if (!accessoryView_.get())
    return yPos;
  NSRect frame = [accessoryView_ frame];
  frame.origin.y = yPos + chrome_style::kRowPadding;
  frame.origin.x = chrome_style::kHorizontalPadding;
  [accessoryView_ setFrameOrigin:frame.origin];
  return NSMaxY(frame);
}

- (void)layoutCloseButtonWithWindowWidth:(CGFloat)windowWidth
                            windowHeight:(CGFloat)windowHeight {
  NSRect frame;
  frame.size.width = chrome_style::GetCloseButtonSize();
  frame.size.height = chrome_style::GetCloseButtonSize();
  frame.origin.x = windowWidth -
      chrome_style::kCloseButtonPadding - NSWidth(frame);
  frame.origin.y = windowHeight -
      chrome_style::kCloseButtonPadding - NSHeight(frame);
  [closeButton_ setFrame:frame];
}

- (NSButton*)linkView {
  return linkView_;
}

@end