summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/bookmark_bar_folder_view.mm
blob: e8e91dd2e2b7e095a42dc51952310c0846667146 (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
// Copyright (c) 2010 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/cocoa/bookmark_bar_folder_view.h"

#import "chrome/browser/browser_theme_provider.h"
#import "chrome/browser/cocoa/bookmark_bar_controller.h"

@implementation BookmarkBarFolderView

- (id<BookmarkButtonControllerProtocol>)controller {
  return [[self window] windowController];
}

- (void)awakeFromNib {
  [super awakeFromNib];

  // BackgroundGradientView's awakeFromNib does a |showsDivider_ = YES|.
  // Make sure we turn it off.
  [self setShowsDivider:NO];

  NSArray* types = [NSArray arrayWithObject:kBookmarkButtonDragType];
  [self registerForDraggedTypes:types];
}

- (void)dealloc {
  [self unregisterDraggedTypes];
  [super dealloc];
}

- (void)drawRect:(NSRect)rect {
  [self drawBackground];

  // TODO(jrg): copied from bookmark_bar_view but orientation changed.
  // Code dup sucks but I'm not sure I can take 16 lines and make it
  // generic for horiz vs vertical while keeping things simple.
  // TODO(jrg): when throwing it all away and using animations, try
  // hard to make a common routine for both.
  // http://crbug.com/35966, http://crbug.com/35968

  // Draw the bookmark-button-dragging drop indicator if necessary.
  if (dropIndicatorShown_) {
    const CGFloat kBarHeight = 1;
    const CGFloat kBarHorizPad = 4;
    const CGFloat kBarOpacity = 0.85;

    NSRect uglyBlackBar =
        NSMakeRect(kBarHorizPad, dropIndicatorPosition_,
                   NSWidth([self bounds]) - 2*kBarHorizPad,
                   kBarHeight);
    // themeProvider is nil in unit tests.
    ThemeProvider* themeProvider = [[self controller] themeProvider];
    if (themeProvider) {
      NSColor* uglyBlackBarColor = themeProvider->
          GetNSColor(BrowserThemeProvider::COLOR_BOOKMARK_TEXT, true);
      [[uglyBlackBarColor colorWithAlphaComponent:kBarOpacity] setFill];
      [[NSBezierPath bezierPathWithRect:uglyBlackBar] fill];
    }
  }
}

// Virtually identical to [BookmarkBarView draggingEntered:].
// TODO(jrg): find a way to share code.  Lack of multiple inheritance
// makes things more of a pain but there should be no excuse for laziness.
// http://crbug.com/35966
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)info {
  inDrag_ = YES;

  NSData* data = [[info draggingPasteboard]
                     dataForType:kBookmarkButtonDragType];
  // [info draggingSource] is nil if not the same application.
  if (data && [info draggingSource]) {
    // Find the position of the drop indicator.
    BookmarkButton* button = nil;
    [data getBytes:&button length:sizeof(button)];

    BOOL showIt = [[self controller]
                      shouldShowIndicatorShownForPoint:[info draggingLocation]];
    if (!showIt) {
      if (dropIndicatorShown_) {
        dropIndicatorShown_ = NO;
        [self setNeedsDisplay:YES];
      }
    } else {
      CGFloat y =
          [[self controller]
              indicatorPosForDragOfButton:button
                                  toPoint:[info draggingLocation]];

      // Need an update if the indicator wasn't previously shown or if it has
      // moved.
      if (!dropIndicatorShown_ || dropIndicatorPosition_ != y) {
        dropIndicatorShown_ = YES;
        dropIndicatorPosition_ = y;
        [self setNeedsDisplay:YES];
      }
    }

    [[self controller] draggingEntered:info];  // allow hover-open to work
    return NSDragOperationMove;
  }

  return NSDragOperationNone;
}

- (void)draggingExited:(id<NSDraggingInfo>)info {
  [[self controller] draggingExited:info];

  // Regardless of the type of dragging which ended, we need to get rid of the
  // drop indicator if one was shown.
  if (dropIndicatorShown_) {
    dropIndicatorShown_ = NO;
    [self setNeedsDisplay:YES];
  }
}

- (void)draggingEnded:(id<NSDraggingInfo>)info {
  // Awkwardness since views open and close out from under us.
  if (inDrag_) {
    inDrag_ = NO;

    // This line makes sure menus get closed when a drag isn't
    // completed.
    [[self controller] closeAllBookmarkFolders];
  }

  [self draggingExited:info];
}

- (BOOL)wantsPeriodicDraggingUpdates {
  // TODO(jrg): This should probably return |YES| and the controller should
  // slide the existing bookmark buttons interactively to the side to make
  // room for the about-to-be-dropped bookmark.
  // http://crbug.com/35968
  return NO;
}

- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)info {
  // For now it's the same as draggingEntered:.
  // TODO(jrg): once we return YES for wantsPeriodicDraggingUpdates,
  // this should ping the [self controller] to perform animations.
  // http://crbug.com/35968
  return [self draggingEntered:info];
}

- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)info {
  return YES;
}

// Implement NSDraggingDestination protocol method
// performDragOperation: for bookmarks.
- (BOOL)performDragOperationForBookmark:(id<NSDraggingInfo>)info {
  BOOL doDrag = NO;
  NSData* data = [[info draggingPasteboard]
                   dataForType:kBookmarkButtonDragType];
  // [info draggingSource] is nil if not the same application.
  if (data && [info draggingSource]) {
    BookmarkButton* button = nil;
    [data getBytes:&button length:sizeof(button)];
    doDrag = [[self controller] dragButton:button to:[info draggingLocation]];
  }
  return doDrag;
}

- (BOOL)performDragOperation:(id<NSDraggingInfo>)info {
  NSPasteboard* pboard = [info draggingPasteboard];
  if ([pboard dataForType:kBookmarkButtonDragType]) {
    if ([self performDragOperationForBookmark:info])
      return YES;
    // Fall through....
  }
  return NO;
}

@end  // BookmarkBarFolderView


@implementation BookmarkBarFolderView(TestingAPI)

- (void)setDropIndicatorShown:(BOOL)shown {
  dropIndicatorShown_ = shown;
}

@end