1 // Copyright (c) 2010 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.
5 #import "chrome/browser/ui/cocoa/content_settings/cookie_details_view_controller.h"
7 #include "base/mac/bundle_locations.h"
8 #include "base/strings/sys_string_conversions.h"
9 #import "chrome/browser/ui/cocoa/content_settings/cookie_tree_node.h"
10 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
11 #include "ui/base/l10n/l10n_util_mac.h"
14 static const int kExtraMarginBelowWhenExpirationEditable = 5;
17 #pragma mark View Controller
19 @implementation CookieDetailsViewController
20 @dynamic hasExpiration;
23 return [super initWithNibName:@"CookieDetailsView"
24 bundle:base::mac::FrameworkBundle()];
27 - (void)awakeFromNib {
28 DCHECK(objectController_);
31 // Finds and returns the y offset of the lowest-most non-hidden
32 // text field in the view. This is used to shrink the view
33 // appropriately so that it just fits its visible content.
34 - (void)getLowestLabelVerticalPosition:(NSView*)view
35 lowestLabelPosition:(float&)lowestLabelPosition {
36 if (![view isHidden]) {
37 if ([view isKindOfClass:[NSTextField class]]) {
38 NSRect frame = [view frame];
39 if (frame.origin.y < lowestLabelPosition) {
40 lowestLabelPosition = frame.origin.y;
43 for (NSView* subview in [view subviews]) {
44 [self getLowestLabelVerticalPosition:subview
45 lowestLabelPosition:lowestLabelPosition];
50 - (void)setContentObject:(id)content {
51 // Make sure the view is loaded before we set the content object,
52 // otherwise, the KVO notifications to update the content don't
53 // reach the view and all of the detail values are default
55 NSView* view = [self view];
57 [objectController_ setValue:content forKey:@"content"];
59 // View needs to be re-tweaked after setting the content object,
60 // since the expiration date may have changed, changing the
61 // size of the expiration popup.
62 [tweaker_ tweakUI:view];
65 - (void)shrinkViewToFit {
66 // Adjust the information pane to be exactly the right size
67 // to hold the visible text information fields.
68 NSView* view = [self view];
69 NSRect frame = [view frame];
70 float lowestLabelPosition = frame.origin.y + frame.size.height;
71 [self getLowestLabelVerticalPosition:view
72 lowestLabelPosition:lowestLabelPosition];
73 float verticalDelta = lowestLabelPosition - frame.origin.y;
75 // Popup menu for the expiration is taller than the plain
76 // text, give it some more room.
77 if ([[[objectController_ content] details] canEditExpiration]) {
78 verticalDelta -= kExtraMarginBelowWhenExpirationEditable;
81 frame.origin.y += verticalDelta;
82 frame.size.height -= verticalDelta;
83 [[self view] setFrame:frame];
86 - (void)configureBindingsForTreeController:(NSTreeController*)treeController {
87 // There seems to be a bug in the binding logic that it's not possible
88 // to bind to the selection of the tree controller, the bind seems to
89 // require an additional path segment in the key, thus the use of
90 // selection.self rather than just selection below.
91 [objectController_ bind:@"contentObject"
92 toObject:treeController
93 withKeyPath:@"selection.self"
97 - (IBAction)setCookieDoesntHaveExplicitExpiration:(id)sender {
98 [[[objectController_ content] details] setHasExpiration:NO];
101 - (IBAction)setCookieHasExplicitExpiration:(id)sender {
102 [[[objectController_ content] details] setHasExpiration:YES];
105 - (BOOL)hasExpiration {
106 return [[[objectController_ content] details] hasExpiration];