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