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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
|
// Copyright (c) 2009 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.
#include "chrome/browser/views/extensions/extension_shelf.h"
#include <algorithm>
#include "app/resource_bundle.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/stl_util-inl.h"
#include "base/string_util.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/extensions/extension_process_manager.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/views/extensions/extension_view.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_service.h"
#include "skia/ext/skia_utils.h"
#include "views/controls/label.h"
#include "views/screen.h"
#include "views/widget/root_view.h"
namespace {
// Margins around the content.
static const int kTopMargin = 2;
static const int kBottomMargin = 2;
static const int kLeftMargin = 0;
static const int kRightMargin = 0;
// Padding on left and right side of an extension toolstrip.
static const int kToolstripPadding = 2;
// Width of the toolstrip divider.
static const int kToolstripDividerWidth = 2;
// Preferred height of the ExtensionShelf.
static const int kShelfHeight = 29;
// Height of the toolstrip within the shelf.
static const int kToolstripHeight = kShelfHeight - (kTopMargin + kBottomMargin);
// Colors for the ExtensionShelf.
static const SkColor kBackgroundColor = SkColorSetRGB(230, 237, 244);
static const SkColor kBorderColor = SkColorSetRGB(201, 212, 225);
static const SkColor kDividerHighlightColor = SkColorSetRGB(247, 250, 253);
// Text colors for the handle
static const SkColor kHandleTextColor = SkColorSetRGB(6, 45, 117);
static const SkColor kHandleTextHighlightColor =
SkColorSetARGB(200, 255, 255, 255);
// Handle padding
static const int kHandlePadding = 4;
// TODO(erikkay) convert back to a gradient when Glen figures out the
// specs.
// static const SkColor kBackgroundColor = SkColorSetRGB(237, 244, 252);
// static const SkColor kTopGradientColor = SkColorSetRGB(222, 234, 248);
// Delays for showing and hiding the shelf handle.
static const int kHideDelayMs = 500;
} // namespace
// A view that holds the place for a toolstrip in the shelf while the toolstrip
// is being dragged or moved.
// TODO(erikkay) this should draw a dimmed out version of the toolstrip.
class ExtensionShelf::PlaceholderView : public views::View {
public:
PlaceholderView() {}
void SetWidth(int width) {
SetBounds(x(), y(), width, height());
PreferredSizeChanged();
}
// ExtensionShelf resizes its views to their preferred size at layout,
// so just always prefer the current size.
gfx::Size GetPreferredSize() { return size(); }
private:
DISALLOW_COPY_AND_ASSIGN(PlaceholderView);
};
// A wrapper class for the ExtensionHost displayed as a toolstrip.
// The class itself also acts as the View for the handle of the toolstrip
// it represents.
class ExtensionShelf::Toolstrip : public views::View,
public BrowserBubble::Delegate {
public:
Toolstrip(ExtensionShelf* shelf, ExtensionHost* host,
const Extension::ToolstripInfo& info);
virtual ~Toolstrip();
// View
virtual void Paint(gfx::Canvas* canvas);
virtual gfx::Size GetPreferredSize();
virtual void Layout();
virtual void OnMouseEntered(const views::MouseEvent& event);
virtual void OnMouseExited(const views::MouseEvent& event);
virtual bool OnMousePressed(const views::MouseEvent& event);
virtual bool OnMouseDragged(const views::MouseEvent& event);
virtual void OnMouseReleased(const views::MouseEvent& event, bool canceled);
virtual bool IsFocusable() const { return true; }
virtual void ChildPreferredSizeChanged(View* child);
// Retrieves the handle for this toolstrip, creating it if necessary.
BrowserBubble* GetHandle();
// Adjust the size and position of the handle.
void LayoutHandle();
// Is the handle for this toolstrip currently visible.
bool handle_visible() { return (handle_.get() && handle_->visible()); }
// Is the toolstrip opened in expanded mode.
bool expanded() { return expanded_; }
// Is the toolstrip being dragged.
bool dragging() { return dragging_; }
// Accessors for the host and its view.
ExtensionHost* host() const { return host_; }
ExtensionView* view() const { return host_->view(); }
// The view that's currently displayed in the shelf. This could be
// either the ExtensionView or |placeholder_view_| depending on the
// current state.
View* GetShelfView() const {
if (placeholder_view_)
return placeholder_view_;
return view();
}
// Detaching the toolstrip from the shelf means that the ExtensionView is
// displayed inside of the BrowserBubble rather than the shelf. This may
// still visually appear to be part of the shelf (expanded) or completely
// separate (dragging).
// If |browser| is true, it also will detach/attach to the browser window.
void DetachFromShelf(bool browser);
void AttachToShelf(bool browser);
// Show / Hide the shelf handle.
void ShowShelfHandle();
void DoShowShelfHandle();
void HideShelfHandle(int delay_ms);
void DoHideShelfHandle();
void StopHandleTimer();
// BrowserBubble::Delegate
virtual void BubbleBrowserWindowMoved(BrowserBubble* bubble);
virtual void BubbleBrowserWindowClosing(BrowserBubble* bubble);
private:
// The actual renderer that this toolstrip contains.
ExtensionHost* host_;
// Manifest definition of this toolstrip.
Extension::ToolstripInfo info_;
// The handle is a BrowserBubble so that it can exist as an independent,
// floating window. It also acts as the container for the ExtensionView when
// it's being dragged.
scoped_ptr<BrowserBubble> handle_;
// Used for drawing the name of the extension in the handle.
scoped_ptr<views::Label> title_;
// Pointer back to the containing shelf.
ExtensionShelf* shelf_;
// When dragging, a placeholder view is put into the shelf to hold space
// for the ExtensionView. This view is parent owned when it's in the view
// hierarchy, so there's no ownership issues here.
PlaceholderView* placeholder_view_;
// Current state of the toolstrip, currently can't both be expanded and
// dragging.
// TODO(erikkay) Support dragging while expanded.
bool dragging_;
bool expanded_;
// If dragging, where did the drag start from.
gfx::Point initial_drag_location_;
// Timers for tracking mouse hovering.
ScopedRunnableMethodFactory<ExtensionShelf::Toolstrip> timer_factory_;
DISALLOW_COPY_AND_ASSIGN(Toolstrip);
};
ExtensionShelf::Toolstrip::Toolstrip(ExtensionShelf* shelf,
ExtensionHost* host,
const Extension::ToolstripInfo& info)
: host_(host),
info_(info),
shelf_(shelf),
placeholder_view_(NULL),
dragging_(false),
expanded_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(timer_factory_(this)) {
DCHECK(host->view());
// We're owned by shelf_, not the bubble that we get inserted in and out of.
SetParentOwned(false);
std::wstring name = UTF8ToWide(host_->extension()->name());
// |title_| isn't actually put in the view hierarchy. We just use it
// to draw in place. The reason for this is so that we can properly handle
// the various mouse events necessary for hovering and dragging.
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
title_.reset(new views::Label(name, rb.GetFont(ResourceBundle::BaseFont)));
title_->SetColor(kHandleTextColor);
title_->SetDrawHighlighted(true);
title_->SetHighlightColor(kHandleTextHighlightColor);
title_->SetBounds(kHandlePadding, kHandlePadding, 100, 100);
title_->SizeToPreferredSize();
SizeToPreferredSize();
}
ExtensionShelf::Toolstrip::~Toolstrip() {
if (handle_.get() && handle_->attached())
handle_->DetachFromBrowser();
}
void ExtensionShelf::Toolstrip::Paint(gfx::Canvas* canvas) {
canvas->FillRectInt(kBackgroundColor, 0, 0, width(), height());
canvas->FillRectInt(kBorderColor, 0, 0, width(), 1);
canvas->FillRectInt(kBorderColor, 0, 0, 1, height() - 1);
canvas->FillRectInt(kBorderColor, width() - 1, 0, 1, height() - 1);
int ext_width = view()->width() + kToolstripPadding +
kToolstripDividerWidth;
if (ext_width < width()) {
canvas->FillRectInt(kBorderColor, ext_width, height() - 1,
width() - ext_width, 1);
}
// Draw the title using a Label as a stamp.
// See constructor for comment about this.
title_->ProcessPaint(canvas);
if (dragging_) {
// when we're dragging, draw the bottom border.
canvas->FillRectInt(kBorderColor, 0, height() - 1, width(), 1);
}
}
gfx::Size ExtensionShelf::Toolstrip::GetPreferredSize() {
gfx::Size sz = title_->GetPreferredSize();
sz.set_width(std::max(view()->width(), sz.width()));
if (!expanded_) {
sz.Enlarge(2 + kHandlePadding * 2, kHandlePadding * 2);
}
if (dragging_ || expanded_) {
gfx::Size extension_size = view()->GetPreferredSize();
sz.Enlarge(0, extension_size.height() + 2);
}
return sz;
}
void ExtensionShelf::Toolstrip::Layout() {
if (dragging_ || expanded_) {
int y = title_->bounds().bottom() + kHandlePadding + 1;
view()->SetBounds(1, y, view()->width(), view()->height());
}
}
void ExtensionShelf::Toolstrip::OnMouseEntered(const views::MouseEvent& event) {
if (dragging_ || expanded_)
return;
ShowShelfHandle();
}
void ExtensionShelf::Toolstrip::OnMouseExited(const views::MouseEvent& event) {
if (dragging_ || expanded_)
return;
HideShelfHandle(kHideDelayMs);
}
bool ExtensionShelf::Toolstrip::OnMousePressed(const views::MouseEvent& event) {
initial_drag_location_ = event.location();
return true;
}
bool ExtensionShelf::Toolstrip::OnMouseDragged(const views::MouseEvent& event) {
if (expanded_) {
// Do nothing for now.
} else if (!dragging_) {
int y_delta = abs(initial_drag_location_.y() - event.location().y());
int x_delta = abs(initial_drag_location_.x() - event.location().x());
if (y_delta > GetVerticalDragThreshold() ||
x_delta > GetHorizontalDragThreshold()) {
dragging_ = true;
StopHandleTimer();
DetachFromShelf(true);
}
} else {
// When freely dragging a window, you can really only trust the
// actual screen point. Local coordinate conversions don't work.
gfx::Point screen = views::Screen::GetCursorScreenPoint();
// However, the handle is actually a child of the browser window
// so we need to convert it back to local coordinates.
gfx::Point origin(0, 0);
views::View::ConvertPointToScreen(shelf_->GetRootView(), &origin);
screen.set_x(screen.x() - origin.x() - initial_drag_location_.x());
screen.set_y(screen.y() - origin.y() - initial_drag_location_.y());
// TODO(erikkay) as this gets dragged around, update the placeholder view
// on the shelf to show where it will get dropped to.
handle_->MoveTo(screen.x(), screen.y());
}
return true;
}
void ExtensionShelf::Toolstrip::OnMouseReleased(const views::MouseEvent& event,
bool canceled) {
StopHandleTimer();
if (dragging_) {
// Drop the toolstrip roughly where it is now.
views::View::OnMouseReleased(event, canceled);
dragging_ = false;
// |this| and |shelf_| are in different view hierarchies, so we need to
// convert to screen coordinates and back again to map locations.
gfx::Point loc = event.location();
View::ConvertPointToScreen(this, &loc);
View::ConvertPointToView(NULL, shelf_, &loc);
shelf_->DropExtension(this, loc, canceled);
AttachToShelf(true);
} else if (!canceled &&
info_.mole.is_valid() && info_.toolstrip.is_valid()) {
// Toggle mole to either expanded or collapsed.
expanded_ = !expanded_;
view()->set_is_toolstrip(!expanded_);
if (expanded_) {
host_->NavigateToURL(info_.mole);
StopHandleTimer();
DetachFromShelf(false);
gfx::Size extension_size = view()->GetPreferredSize();
extension_size.set_height(info_.mole_height);
view()->SetPreferredSize(extension_size);
LayoutHandle();
} else {
gfx::Size extension_size = view()->GetPreferredSize();
extension_size.set_height(kToolstripHeight);
view()->SetPreferredSize(extension_size);
host_->NavigateToURL(info_.toolstrip);
AttachToShelf(false);
}
// This is to prevent flickering as the page loads and lays out.
// Once the navigation is finished, ExtensionView will wind up setting
// visibility to true.
view()->SetVisible(false);
}
}
BrowserBubble* ExtensionShelf::Toolstrip::GetHandle() {
if (!handle_.get()) {
handle_.reset(new BrowserBubble(this, shelf_->GetWidget(),
gfx::Point(0, 0)));
handle_->set_delegate(this);
LayoutHandle();
}
return handle_.get();
}
void ExtensionShelf::Toolstrip::LayoutHandle() {
if (!handle_.get())
return;
int handle_width = std::max(view()->width(), width());
gfx::Point origin(-kToolstripPadding, -(height() + kToolstripPadding - 1));
if (expanded_) {
origin.set_y(GetShelfView()->height() - height());
views::View::ConvertPointToView(GetShelfView(), shelf_->GetRootView(),
&origin);
} else {
views::View::ConvertPointToWidget(view(), &origin);
}
SetBounds(0, 0, handle_width, height());
handle_->SetBounds(origin.x(), origin.y(), handle_width, height());
handle_->ResizeToView();
}
void ExtensionShelf::Toolstrip::ChildPreferredSizeChanged(View* child) {
if (child == view()) {
child->SizeToPreferredSize();
Layout();
if (expanded_) {
LayoutHandle();
placeholder_view_->SetWidth(child->width());
shelf_->Layout();
}
}
}
void ExtensionShelf::Toolstrip::BubbleBrowserWindowMoved(
BrowserBubble* bubble) {
HideShelfHandle(0);
}
void ExtensionShelf::Toolstrip::BubbleBrowserWindowClosing(
BrowserBubble* bubble) {
DoHideShelfHandle();
}
void ExtensionShelf::Toolstrip::DetachFromShelf(bool browserDetach) {
DCHECK(!placeholder_view_);
if (browserDetach && handle_->attached())
handle_->DetachFromBrowser();
// Construct a placeholder view to replace the view.
placeholder_view_ = new PlaceholderView();
placeholder_view_->SetBounds(view()->bounds());
shelf_->AddChildView(placeholder_view_);
AddChildView(view());
SizeToPreferredSize();
handle_->ResizeToView();
Layout();
}
void ExtensionShelf::Toolstrip::AttachToShelf(bool browserAttach) {
if (browserAttach && !handle_->attached())
handle_->AttachToBrowser();
// Move the view back into the shelf and remove the old placeholder.
shelf_->AddChildView(view());
// The size of the view may have changed, so just set the position.
view()->SetX(placeholder_view_->x());
view()->SetY(placeholder_view_->y());
// Remove the old placeholder.
shelf_->RemoveChildView(placeholder_view_);
delete placeholder_view_;
placeholder_view_ = NULL;
SizeToPreferredSize();
Layout();
shelf_->Layout();
}
void ExtensionShelf::Toolstrip::DoShowShelfHandle() {
GetHandle();
if (!handle_->visible()) {
LayoutHandle();
handle_->Show();
}
}
void ExtensionShelf::Toolstrip::DoHideShelfHandle() {
if (!handle_visible())
return;
handle_->Hide();
if (handle_->attached())
handle_->DetachFromBrowser();
handle_.reset(NULL);
shelf_->Layout();
}
void ExtensionShelf::Toolstrip::StopHandleTimer() {
if (!timer_factory_.empty())
timer_factory_.RevokeAll();
}
void ExtensionShelf::Toolstrip::ShowShelfHandle() {
StopHandleTimer();
if (handle_visible())
return;
MessageLoop::current()->PostDelayedTask(FROM_HERE,
timer_factory_.NewRunnableMethod(
&ExtensionShelf::Toolstrip::DoShowShelfHandle),
1000);
}
void ExtensionShelf::Toolstrip::HideShelfHandle(int delay_ms) {
StopHandleTimer();
if (!handle_visible() || dragging_ || expanded_)
return;
if (delay_ms) {
MessageLoop::current()->PostDelayedTask(FROM_HERE,
timer_factory_.NewRunnableMethod(
&ExtensionShelf::Toolstrip::DoHideShelfHandle),
delay_ms);
} else {
DoHideShelfHandle();
}
}
////////////////////////////////////////////////////////////////////////////////
ExtensionShelf::ExtensionShelf(Browser* browser)
: model_(new ExtensionShelfModel(browser)) {
model_->AddObserver(this);
LoadFromModel();
EnableCanvasFlippingForRTLUI(true);
}
ExtensionShelf::~ExtensionShelf() {
int count = model_->count();
for (int i = 0; i < count; ++i) {
delete ToolstripAtIndex(i);
model_->SetToolstripDataAt(i, NULL);
}
model_->RemoveObserver(this);
}
void ExtensionShelf::Paint(gfx::Canvas* canvas) {
#if 0
// TODO(erikkay) re-enable this when Glen has the gradient values worked out.
SkPaint paint;
paint.setShader(skia::CreateGradientShader(0,
height(),
kTopGradientColor,
kBackgroundColor))->safeUnref();
canvas->FillRectInt(0, 0, width(), height(), paint);
#else
canvas->FillRectInt(kBackgroundColor, 0, 0, width(), height());
#endif
canvas->FillRectInt(kBorderColor, 0, 0, width(), 1);
canvas->FillRectInt(kBorderColor, 0, height() - 1, width(), 1);
int count = GetChildViewCount();
for (int i = 0; i < count; ++i) {
int right = GetChildViewAt(i)->bounds().right() + kToolstripPadding;
int h = height() - 2;
canvas->FillRectInt(kBorderColor, right, 1, 1, h);
canvas->FillRectInt(kDividerHighlightColor, right + 1, 1, 1, h);
}
SkRect background_rect = {
SkIntToScalar(0),
SkIntToScalar(1),
SkIntToScalar(1),
SkIntToScalar(height() - 2)};
InitBackground(canvas, background_rect);
}
gfx::Size ExtensionShelf::GetPreferredSize() {
if (model_->count())
return gfx::Size(0, kShelfHeight);
return gfx::Size(0, 0);
}
void ExtensionShelf::ChildPreferredSizeChanged(View* child) {
Toolstrip *toolstrip = ToolstripForView(static_cast<ExtensionView*>(child));
if (!toolstrip)
return;
Layout();
}
void ExtensionShelf::Layout() {
if (!GetParent())
return;
int x = kLeftMargin;
int y = kTopMargin;
int content_height = height() - kTopMargin - kBottomMargin;
int max_x = width() - kRightMargin;
int count = model_->count();
for (int i = 0; i < count; ++i) {
x += kToolstripPadding; // left padding
Toolstrip* toolstrip = ToolstripAtIndex(i);
if (!toolstrip) // can be NULL while in the process of removing
continue;
View* view = toolstrip->GetShelfView();
gfx::Size pref = view->GetPreferredSize();
int next_x = x + pref.width() + kToolstripPadding; // right padding
if (view == toolstrip->view())
toolstrip->view()->set_is_clipped(next_x >= max_x);
view->SetBounds(x, y, pref.width(), content_height);
view->Layout();
if (toolstrip->handle_visible())
toolstrip->LayoutHandle();
x = next_x + kToolstripDividerWidth;
}
SchedulePaint();
}
void ExtensionShelf::OnMouseEntered(const views::MouseEvent& event) {
}
void ExtensionShelf::OnMouseExited(const views::MouseEvent& event) {
}
bool ExtensionShelf::GetAccessibleRole(AccessibilityTypes::Role* role) {
DCHECK(role);
*role = AccessibilityTypes::ROLE_TOOLBAR;
return true;
}
bool ExtensionShelf::GetAccessibleName(std::wstring* name) {
DCHECK(name);
if (!accessible_name_.empty()) {
name->assign(accessible_name_);
return true;
}
return false;
}
void ExtensionShelf::SetAccessibleName(const std::wstring& name) {
accessible_name_.assign(name);
}
void ExtensionShelf::ToolstripInsertedAt(ExtensionHost* host,
int index) {
model_->SetToolstripDataAt(index,
new Toolstrip(this, host, model_->ToolstripInfoAt(index)));
bool had_views = GetChildViewCount() > 0;
ExtensionView* view = host->view();
if (!background_.empty())
view->SetBackground(background_);
AddChildView(view);
view->SetContainer(this);
if (!had_views)
PreferredSizeChanged();
Layout();
}
void ExtensionShelf::ToolstripRemovingAt(ExtensionHost* host, int index) {
// Delete the Toolstrip view and remove it from the model.
Toolstrip* toolstrip = ToolstripAtIndex(index);
View* view = toolstrip->GetShelfView();
RemoveChildView(view);
delete toolstrip;
model_->SetToolstripDataAt(index, NULL);
// Technically, the toolstrip is still in the model at this point, but
// the Layout code handles this case.
Layout();
}
void ExtensionShelf::ToolstripDraggingFrom(ExtensionHost* host, int index) {
}
void ExtensionShelf::ToolstripMoved(ExtensionHost* host, int from_index,
int to_index) {
Layout();
}
void ExtensionShelf::ToolstripChangedAt(ExtensionHost* toolstrip, int index) {
}
void ExtensionShelf::ExtensionShelfEmpty() {
PreferredSizeChanged();
}
void ExtensionShelf::ShelfModelReloaded() {
// None of the child views are parent owned, so nothing is being leaked here.
RemoveAllChildViews(false);
LoadFromModel();
}
void ExtensionShelf::OnExtensionMouseEvent(ExtensionView* view) {
Toolstrip *toolstrip = ToolstripForView(view);
if (toolstrip)
toolstrip->ShowShelfHandle();
}
void ExtensionShelf::OnExtensionMouseLeave(ExtensionView* view) {
Toolstrip *toolstrip = ToolstripForView(view);
if (toolstrip)
toolstrip->HideShelfHandle(kHideDelayMs);
}
void ExtensionShelf::DropExtension(Toolstrip* toolstrip, const gfx::Point& pt,
bool cancel) {
Toolstrip* dest_toolstrip = ToolstripAtX(pt.x());
if (!dest_toolstrip) {
if (pt.x() > 0)
dest_toolstrip = ToolstripAtIndex(model_->count() - 1);
else
dest_toolstrip = ToolstripAtIndex(0);
}
if (toolstrip == dest_toolstrip)
return;
int from = model_->IndexOfToolstrip(toolstrip->host());
int to = model_->IndexOfToolstrip(dest_toolstrip->host());
DCHECK(from != to);
model_->MoveToolstripAt(from, to);
}
void ExtensionShelf::InitBackground(gfx::Canvas* canvas, const SkRect& subset) {
if (!background_.empty())
return;
const SkBitmap& background = canvas->getDevice()->accessBitmap(false);
// Extract the correct subset of the toolstrip background into a bitmap. We
// must use a temporary here because extractSubset() returns a bitmap that
// references pixels in the original one and we want to actually make a copy
// that will have a long lifetime.
SkBitmap temp;
temp.setConfig(background.config(),
static_cast<int>(subset.width()),
static_cast<int>(subset.height()));
SkRect mapped_subset = subset;
bool result = canvas->getTotalMatrix().mapRect(&mapped_subset);
DCHECK(result);
SkIRect isubset;
mapped_subset.round(&isubset);
result = background.extractSubset(&temp, isubset);
if (!result)
return;
temp.copyTo(&background_, temp.config());
DCHECK(background_.readyToDraw());
// Tell all extension views about the new background
int count = model_->count();
for (int i = 0; i < count; ++i)
ToolstripAtIndex(i)->view()->SetBackground(background_);
}
ExtensionShelf::Toolstrip* ExtensionShelf::ToolstripAtX(int x) {
int count = model_->count();
if (count == 0)
return NULL;
if (x < 0)
return NULL;
for (int i = 0; i < count; ++i) {
Toolstrip* toolstrip = ToolstripAtIndex(i);
View* view = toolstrip->GetShelfView();
if (x > (view->x() + view->width() + kToolstripPadding))
continue;
return toolstrip;
}
return NULL;
}
ExtensionShelf::Toolstrip* ExtensionShelf::ToolstripAtIndex(int index) {
return static_cast<Toolstrip*>(model_->ToolstripDataAt(index));
}
ExtensionShelf::Toolstrip* ExtensionShelf::ToolstripForView(
ExtensionView* view) {
int count = model_->count();
for (int i = 0; i < count; ++i) {
Toolstrip* toolstrip = ToolstripAtIndex(i);
if (view == toolstrip->view())
return toolstrip;
}
NOTREACHED();
return NULL;
}
void ExtensionShelf::LoadFromModel() {
int count = model_->count();
for (int i = 0; i < count; ++i)
ToolstripInsertedAt(model_->ToolstripAt(i), i);
}
|