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 #import "base/mac/mac_util.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "chrome/browser/ui/cocoa/content_settings/cookie_tree_node.h"
11 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
12 #include "ui/base/l10n/l10n_util_mac.h"
13 #include "ui/base/resource/resource_bundle.h"
16 static const int kExtraMarginBelowWhenExpirationEditable = 5;
19 #pragma mark View Controller
21 @implementation CookieDetailsViewController
22 @dynamic hasExpiration;
25 return [super initWithNibName:@"CookieDetailsView"
26 bundle:base::mac::FrameworkBundle()];
29 - (void)awakeFromNib {
30 DCHECK(objectController_);
33 // Finds and returns the y offset of the lowest-most non-hidden
34 // text field in the view. This is used to shrink the view
35 // appropriately so that it just fits its visible content.
36 - (void)getLowestLabelVerticalPosition:(NSView*)view
37 lowestLabelPosition:(float&)lowestLabelPosition {
38 if (![view isHidden]) {
39 if ([view isKindOfClass:[NSTextField class]]) {
40 NSRect frame = [view frame];
41 if (frame.origin.y < lowestLabelPosition) {
42 lowestLabelPosition = frame.origin.y;
45 for (NSView* subview in [view subviews]) {
46 [self getLowestLabelVerticalPosition:subview
47 lowestLabelPosition:lowestLabelPosition];
52 - (void)setContentObject:(id)content {
53 // Make sure the view is loaded before we set the content object,
54 // otherwise, the KVO notifications to update the content don't
55 // reach the view and all of the detail values are default
57 NSView* view = [self view];
59 [objectController_ setValue:content forKey:@"content"];
61 // View needs to be re-tweaked after setting the content object,
62 // since the expiration date may have changed, changing the
63 // size of the expiration popup.
64 [tweaker_ tweakUI:view];
67 - (void)shrinkViewToFit {
68 // Adjust the information pane to be exactly the right size
69 // to hold the visible text information fields.
70 NSView* view = [self view];
71 NSRect frame = [view frame];
72 float lowestLabelPosition = frame.origin.y + frame.size.height;
73 [self getLowestLabelVerticalPosition:view
74 lowestLabelPosition:lowestLabelPosition];
75 float verticalDelta = lowestLabelPosition - frame.origin.y;
77 // Popup menu for the expiration is taller than the plain
78 // text, give it some more room.
79 if ([[[objectController_ content] details] canEditExpiration]) {
80 verticalDelta -= kExtraMarginBelowWhenExpirationEditable;
83 frame.origin.y += verticalDelta;
84 frame.size.height -= verticalDelta;
85 [[self view] setFrame:frame];
88 - (void)configureBindingsForTreeController:(NSTreeController*)treeController {
89 // There seems to be a bug in the binding logic that it's not possible
90 // to bind to the selection of the tree controller, the bind seems to
91 // require an additional path segment in the key, thus the use of
92 // selection.self rather than just selection below.
93 [objectController_ bind:@"contentObject"
94 toObject:treeController
95 withKeyPath:@"selection.self"
99 - (IBAction)setCookieDoesntHaveExplicitExpiration:(id)sender {
100 [[[objectController_ content] details] setHasExpiration:NO];
103 - (IBAction)setCookieHasExplicitExpiration:(id)sender {
104 [[[objectController_ content] details] setHasExpiration:YES];
107 - (BOOL)hasExpiration {
108 return [[[objectController_ content] details] hasExpiration];