This source file includes following definitions.
- Round
- newtab_button_h_offset
- newtab_button_v_offset
- tab_h_offset
- newtab_button_asset_width
- newtab_button_asset_height
- stacked_tab_left_clip
- stacked_tab_right_clip
- GetClipboardText
- AnimationEnded
- AnimationCanceled
- ConvertPointToViewAndGetEventHandler
- ConvertPointToViewAndGetTooltipHandler
- EventSourceFromEvent
- set_background_offset
- destroyed_
- HasHitTestMask
- GetHitTestMask
- OnMouseReleased
- OnPaint
- OnGestureEvent
- ShouldWindowContentsBeTransparent
- GetBackgroundImage
- GetImageForState
- GetImageForScale
- tab_
- AnimationEnded
- AnimationCanceled
- CompleteRemove
- HighlightCloseButton
- immersive_style_
- AddObserver
- RemoveObserver
- SetLayoutType
- GetNewTabButtonBounds
- SizeTabButtonToTopOfTabStrip
- StartHighlight
- StopAllHighlighting
- AddTabAt
- MoveTab
- RemoveTabAt
- SetTabData
- PrepareForCloseAt
- SetSelection
- TabTitleChangedNotLoading
- tab_at
- GetModelIndexOfTab
- GetModelCount
- IsValidModelIndex
- IsDragSessionActive
- IsActiveDropTarget
- IsTabStripEditable
- IsTabStripCloseable
- UpdateLoadingAnimations
- IsPositionInWindowCaption
- IsRectInWindowCaption
- SetBackgroundOffset
- newtab_button
- SetImmersiveStyle
- IsAnimating
- StopAnimating
- FileSupported
- GetSelectionModel
- SupportsMultipleSelection
- SelectTab
- ExtendSelectionTo
- ToggleSelected
- AddSelectionFromAnchorTo
- CloseTab
- ShowContextMenuForTab
- IsActiveTab
- IsTabSelected
- IsTabPinned
- MaybeStartDrag
- ContinueDrag
- EndDrag
- GetTabAt
- OnMouseEventInTab
- ShouldPaintTab
- IsImmersiveStyle
- MouseMovedOutOfHost
- Layout
- PaintChildren
- GetClassName
- GetPreferredSize
- OnDragEntered
- OnDragUpdated
- OnDragExited
- OnPerformDrop
- GetAccessibleState
- GetEventHandlerForRect
- GetTooltipHandlerForPoint
- GetImmersiveHeight
- GetMiniTabCount
- ButtonPressed
- GetViewByID
- OnMousePressed
- OnMouseDragged
- OnMouseReleased
- OnMouseCaptureLost
- OnMouseMoved
- OnMouseEntered
- OnGestureEvent
- Init
- CreateTab
- StartInsertTabAnimation
- StartMoveTabAnimation
- StartRemoveTabAnimation
- ScheduleRemoveTabAnimation
- AnimateToIdealBounds
- ShouldHighlightCloseButtonAfterRemove
- DoLayout
- DragActiveTab
- SetIdealBoundsFromPositions
- StackDraggedTabs
- IsStackingDraggedTabs
- LayoutDraggedTabsAt
- CalculateBoundsForDraggedTabs
- GetSizeNeededForTabs
- RemoveTabFromViewModel
- RemoveAndDeleteTab
- UpdateTabsClosingMap
- StartedDraggingTabs
- DraggedTabsDetached
- StoppedDraggingTabs
- StoppedDraggingTab
- OwnDragController
- DestroyDragController
- ReleaseDragController
- PaintClosingTabs
- UpdateLayoutTypeFromMouseEvent
- GetCurrentTabWidths
- GetDesiredTabWidths
- ResizeLayoutTabs
- ResizeLayoutTabsFromTouch
- StartResizeLayoutTabsFromTouchTimer
- SetTabBoundsForDrag
- AddMessageLoopObserver
- RemoveMessageLoopObserver
- GetDropBounds
- UpdateDropIndex
- SetDropIndex
- GetDropEffect
- GetDropArrowImage
- file_supported
- PrepareForAnimation
- GenerateIdealBounds
- GenerateIdealBoundsForMiniTabs
- new_tab_button_width
- button_v_offset
- tab_area_width
- StartResizeLayoutAnimation
- StartMiniTabAnimation
- StartMouseInitiatedRemoveTabAnimation
- IsPointInTab
- GetStartXForNormalTabs
- FindTabForEvent
- FindTabForEventFrom
- FindTabHitByPoint
- GetTabXCoordinates
- SwapLayoutIfNecessary
- NeedsTouchLayout
- SetResetToShrinkOnExit
- GetAdjustLayout
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#if defined(OS_WIN)
#include <windowsx.h>
#endif
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include "base/compiler_specific.h"
#include "base/metrics/histogram.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/tabs/stacked_tab_strip_layout.h"
#include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/browser/ui/views/tabs/tab_drag_controller.h"
#include "chrome/browser/ui/views/tabs/tab_strip_controller.h"
#include "chrome/browser/ui/views/tabs/tab_strip_observer.h"
#include "chrome/browser/ui/views/touch_uma/touch_uma.h"
#include "content/public/browser/user_metrics.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/base/default_theme_provider.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/layout.h"
#include "ui/base/models/list_selection_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/animation/animation_container.h"
#include "ui/gfx/animation/throb_animation.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/display.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/path.h"
#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/screen.h"
#include "ui/gfx/size.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/mouse_watcher_view_host.h"
#include "ui/views/rect_based_targeting_utils.h"
#include "ui/views/view_model_utils.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/non_client_view.h"
#if defined(OS_WIN)
#include "ui/gfx/win/hwnd_util.h"
#include "ui/views/widget/monitor_win.h"
#include "ui/views/win/hwnd_util.h"
#include "win8/util/win8_util.h"
#endif
using base::UserMetricsAction;
using ui::DropTargetEvent;
namespace {
static const int kTabStripAnimationVSlop = 40;
static const int kGlassFrameInactiveTabAlpha = 200;
static const int kGlassFrameInactiveTabAlphaMultiSelection = 150;
static const int kInactiveTabAndNewTabButtonAlphaAsh = 230;
static const int kInactiveTabAndNewTabButtonAlpha = 255;
static const int kTabEdgeRatioInverse = 4;
static int drop_indicator_width;
static int drop_indicator_height;
static inline int Round(double x) {
return static_cast<int>(floor(x + 0.5));
}
static const int kMaxStackedCount = 4;
static const int kStackedPadding = 6;
#if !defined(USE_ASH)
const int kMouseMoveTimeMS = 200;
const int kMouseMoveCountBeforeConsiderReal = 3;
#endif
const int kTouchResizeLayoutTimeMS = 2000;
int newtab_button_h_offset() {
static int value = -1;
if (value == -1) {
switch (ui::GetDisplayLayout()) {
case ui::LAYOUT_DESKTOP:
value = -11;
break;
case ui::LAYOUT_TOUCH:
value = -13;
break;
default:
NOTREACHED();
}
}
return value;
}
int newtab_button_v_offset() {
static int value = -1;
if (value == -1) {
switch (ui::GetDisplayLayout()) {
case ui::LAYOUT_DESKTOP:
value = 7;
break;
case ui::LAYOUT_TOUCH:
value = 8;
break;
default:
NOTREACHED();
}
}
return value;
}
int tab_h_offset() {
static int value = -1;
if (value == -1) {
switch (ui::GetDisplayLayout()) {
case ui::LAYOUT_DESKTOP:
value = -26;
break;
case ui::LAYOUT_TOUCH:
value = -34;
break;
default:
NOTREACHED();
}
}
return value;
}
int newtab_button_asset_width() {
static int value = -1;
if (value == -1) {
switch (ui::GetDisplayLayout()) {
case ui::LAYOUT_DESKTOP:
value = 34;
break;
case ui::LAYOUT_TOUCH:
value = 46;
break;
default:
NOTREACHED();
}
}
return value;
}
int newtab_button_asset_height() {
static int value = -1;
if (value == -1) {
switch (ui::GetDisplayLayout()) {
case ui::LAYOUT_DESKTOP:
value = 18;
break;
case ui::LAYOUT_TOUCH:
value = 24;
break;
default:
NOTREACHED();
}
}
return value;
}
int stacked_tab_left_clip() {
static int value = -1;
if (value == -1) {
switch (ui::GetDisplayLayout()) {
case ui::LAYOUT_DESKTOP:
value = 20;
break;
case ui::LAYOUT_TOUCH:
value = 26;
break;
default:
NOTREACHED();
}
}
return value;
}
int stacked_tab_right_clip() {
static int value = -1;
if (value == -1) {
switch (ui::GetDisplayLayout()) {
case ui::LAYOUT_DESKTOP:
value = 20;
break;
case ui::LAYOUT_TOUCH:
value = 26;
break;
default:
NOTREACHED();
}
}
return value;
}
base::string16 GetClipboardText() {
if (!ui::Clipboard::IsSupportedClipboardType(ui::CLIPBOARD_TYPE_SELECTION))
return base::string16();
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
CHECK(clipboard);
base::string16 clipboard_text;
clipboard->ReadText(ui::CLIPBOARD_TYPE_SELECTION, &clipboard_text);
return clipboard_text;
}
class ResetDraggingStateDelegate
: public views::BoundsAnimator::OwnedAnimationDelegate {
public:
explicit ResetDraggingStateDelegate(Tab* tab) : tab_(tab) {
}
virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE {
tab_->set_dragging(false);
}
virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE {
tab_->set_dragging(false);
}
private:
Tab* tab_;
DISALLOW_COPY_AND_ASSIGN(ResetDraggingStateDelegate);
};
views::View* ConvertPointToViewAndGetEventHandler(
views::View* source,
views::View* dest,
const gfx::Point& point_in_source) {
gfx::Point dest_point(point_in_source);
views::View::ConvertPointToTarget(source, dest, &dest_point);
return dest->HitTestPoint(dest_point) ?
dest->GetEventHandlerForPoint(dest_point) : NULL;
}
views::View* ConvertPointToViewAndGetTooltipHandler(
views::View* source,
views::View* dest,
const gfx::Point& point_in_source) {
gfx::Point dest_point(point_in_source);
views::View::ConvertPointToTarget(source, dest, &dest_point);
return dest->GetTooltipHandlerForPoint(dest_point);
}
TabDragController::EventSource EventSourceFromEvent(
const ui::LocatedEvent& event) {
return event.IsGestureEvent() ? TabDragController::EVENT_SOURCE_TOUCH :
TabDragController::EVENT_SOURCE_MOUSE;
}
}
class NewTabButton : public views::ImageButton {
public:
NewTabButton(TabStrip* tab_strip, views::ButtonListener* listener);
virtual ~NewTabButton();
void set_background_offset(const gfx::Point& offset) {
background_offset_ = offset;
}
protected:
virtual bool HasHitTestMask() const OVERRIDE;
virtual void GetHitTestMask(HitTestSource source,
gfx::Path* path) const OVERRIDE;
#if defined(OS_WIN)
virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
#endif
virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
private:
bool ShouldWindowContentsBeTransparent() const;
gfx::ImageSkia GetBackgroundImage(views::CustomButton::ButtonState state,
ui::ScaleFactor scale_factor) const;
gfx::ImageSkia GetImageForState(views::CustomButton::ButtonState state,
ui::ScaleFactor scale_factor) const;
gfx::ImageSkia GetImageForScale(ui::ScaleFactor scale_factor) const;
TabStrip* tab_strip_;
gfx::Point background_offset_;
bool* destroyed_;
DISALLOW_COPY_AND_ASSIGN(NewTabButton);
};
NewTabButton::NewTabButton(TabStrip* tab_strip, views::ButtonListener* listener)
: views::ImageButton(listener),
tab_strip_(tab_strip),
destroyed_(NULL) {
#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
set_triggerable_event_flags(triggerable_event_flags() |
ui::EF_MIDDLE_MOUSE_BUTTON);
#endif
}
NewTabButton::~NewTabButton() {
if (destroyed_)
*destroyed_ = true;
}
bool NewTabButton::HasHitTestMask() const {
return !tab_strip_->SizeTabButtonToTopOfTabStrip();
}
void NewTabButton::GetHitTestMask(HitTestSource source, gfx::Path* path) const {
DCHECK(path);
SkScalar w = SkIntToScalar(width());
SkScalar v_offset = SkIntToScalar(newtab_button_v_offset());
path->moveTo(0, v_offset + 1);
path->lineTo(w - 7, v_offset + 1);
path->lineTo(w - 4, v_offset + 4);
path->lineTo(w, v_offset + 16);
path->lineTo(w - 1, v_offset + 17);
path->lineTo(7, v_offset + 17);
path->lineTo(4, v_offset + 13);
path->lineTo(0, v_offset + 1);
path->close();
}
#if defined(OS_WIN)
void NewTabButton::OnMouseReleased(const ui::MouseEvent& event) {
if (event.IsOnlyRightMouseButton()) {
gfx::Point point = event.location();
views::View::ConvertPointToScreen(this, &point);
bool destroyed = false;
destroyed_ = &destroyed;
gfx::ShowSystemMenuAtPoint(views::HWNDForView(this), point);
if (destroyed)
return;
destroyed_ = NULL;
SetState(views::CustomButton::STATE_NORMAL);
return;
}
views::ImageButton::OnMouseReleased(event);
}
#endif
void NewTabButton::OnPaint(gfx::Canvas* canvas) {
gfx::ImageSkia image =
GetImageForScale(ui::GetSupportedScaleFactor(canvas->image_scale()));
canvas->DrawImageInt(image, 0, height() - image.height());
}
void NewTabButton::OnGestureEvent(ui::GestureEvent* event) {
views::ImageButton::OnGestureEvent(event);
event->SetHandled();
}
bool NewTabButton::ShouldWindowContentsBeTransparent() const {
return GetWidget() &&
GetWidget()->GetTopLevelWidget()->ShouldWindowContentsBeTransparent();
}
gfx::ImageSkia NewTabButton::GetBackgroundImage(
views::CustomButton::ButtonState state,
ui::ScaleFactor scale_factor) const {
int background_id = 0;
if (ShouldWindowContentsBeTransparent()) {
background_id = IDR_THEME_TAB_BACKGROUND_V;
} else if (tab_strip_->controller()->IsIncognito()) {
background_id = IDR_THEME_TAB_BACKGROUND_INCOGNITO;
#if defined(OS_WIN)
} else if (win8::IsSingleWindowMetroMode()) {
background_id = IDR_THEME_TAB_BACKGROUND_V;
#endif
} else {
background_id = IDR_THEME_TAB_BACKGROUND;
}
int alpha = 0;
switch (state) {
case views::CustomButton::STATE_NORMAL:
case views::CustomButton::STATE_HOVERED:
alpha = ShouldWindowContentsBeTransparent() ? kGlassFrameInactiveTabAlpha
: 255;
break;
case views::CustomButton::STATE_PRESSED:
alpha = 145;
break;
default:
NOTREACHED();
break;
}
gfx::ImageSkia* mask =
GetThemeProvider()->GetImageSkiaNamed(IDR_NEWTAB_BUTTON_MASK);
int height = mask->height();
int width = mask->width();
float scale = ui::GetImageScale(scale_factor);
if (!mask->HasRepresentation(scale))
scale_factor = ui::SCALE_FACTOR_100P;
gfx::Canvas canvas(gfx::Size(width, height), scale, false);
gfx::ImageSkia* background =
GetThemeProvider()->GetImageSkiaNamed(background_id);
int offset_y = GetThemeProvider()->HasCustomImage(background_id) ?
0 : background_offset_.y();
float x_scale = 1.0f;
int x = GetMirroredX() + background_offset_.x();
if (base::i18n::IsRTL()) {
x_scale = -1.0f;
x += width;
}
canvas.TileImageInt(*background, x, newtab_button_v_offset() + offset_y,
x_scale, 1.0f, 0, 0, width, height);
if (alpha != 255) {
SkPaint paint;
paint.setColor(SkColorSetARGB(alpha, 255, 255, 255));
paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
paint.setStyle(SkPaint::kFill_Style);
canvas.DrawRect(gfx::Rect(0, 0, width, height), paint);
}
if (state == views::CustomButton::STATE_HOVERED)
canvas.FillRect(GetLocalBounds(), SkColorSetARGB(64, 255, 255, 255));
return gfx::ImageSkiaOperations::CreateMaskedImage(
gfx::ImageSkia(canvas.ExtractImageRep()), *mask);
}
gfx::ImageSkia NewTabButton::GetImageForState(
views::CustomButton::ButtonState state,
ui::ScaleFactor scale_factor) const {
const int overlay_id = state == views::CustomButton::STATE_PRESSED ?
IDR_NEWTAB_BUTTON_P : IDR_NEWTAB_BUTTON;
gfx::ImageSkia* overlay = GetThemeProvider()->GetImageSkiaNamed(overlay_id);
gfx::Canvas canvas(
gfx::Size(overlay->width(), overlay->height()),
ui::GetImageScale(scale_factor),
false);
canvas.DrawImageInt(GetBackgroundImage(state, scale_factor), 0, 0);
const int kGlassFrameOverlayAlpha = 178;
const int kOpaqueFrameOverlayAlpha = 230;
uint8 alpha = ShouldWindowContentsBeTransparent() ?
kGlassFrameOverlayAlpha : kOpaqueFrameOverlayAlpha;
canvas.DrawImageInt(*overlay, 0, 0, alpha);
return gfx::ImageSkia(canvas.ExtractImageRep());
}
gfx::ImageSkia NewTabButton::GetImageForScale(
ui::ScaleFactor scale_factor) const {
if (!hover_animation_->is_animating())
return GetImageForState(state(), scale_factor);
return gfx::ImageSkiaOperations::CreateBlendedImage(
GetImageForState(views::CustomButton::STATE_NORMAL, scale_factor),
GetImageForState(views::CustomButton::STATE_HOVERED, scale_factor),
hover_animation_->GetCurrentValue());
}
class TabStrip::RemoveTabDelegate
: public views::BoundsAnimator::OwnedAnimationDelegate {
public:
RemoveTabDelegate(TabStrip* tab_strip, Tab* tab);
virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE;
private:
void CompleteRemove();
void HighlightCloseButton();
TabStrip* tabstrip_;
Tab* tab_;
DISALLOW_COPY_AND_ASSIGN(RemoveTabDelegate);
};
TabStrip::RemoveTabDelegate::RemoveTabDelegate(TabStrip* tab_strip,
Tab* tab)
: tabstrip_(tab_strip),
tab_(tab) {
}
void TabStrip::RemoveTabDelegate::AnimationEnded(
const gfx::Animation* animation) {
CompleteRemove();
}
void TabStrip::RemoveTabDelegate::AnimationCanceled(
const gfx::Animation* animation) {
CompleteRemove();
}
void TabStrip::RemoveTabDelegate::CompleteRemove() {
DCHECK(tab_->closing());
tabstrip_->RemoveAndDeleteTab(tab_);
HighlightCloseButton();
}
void TabStrip::RemoveTabDelegate::HighlightCloseButton() {
if (tabstrip_->IsDragSessionActive() ||
!tabstrip_->ShouldHighlightCloseButtonAfterRemove()) {
return;
}
views::Widget* widget = tabstrip_->GetWidget();
if (!widget)
return;
widget->SynthesizeMouseMoveEvent();
}
const char TabStrip::kViewClassName[] = "TabStrip";
const int TabStrip::kMiniToNonMiniGap = 3;
TabStrip::TabStrip(TabStripController* controller)
: controller_(controller),
newtab_button_(NULL),
current_unselected_width_(Tab::GetStandardSize().width()),
current_selected_width_(Tab::GetStandardSize().width()),
available_width_for_tabs_(-1),
in_tab_close_(false),
animation_container_(new gfx::AnimationContainer()),
bounds_animator_(this),
layout_type_(TAB_STRIP_LAYOUT_SHRINK),
adjust_layout_(false),
reset_to_shrink_on_exit_(false),
mouse_move_count_(0),
immersive_style_(false) {
Init();
}
TabStrip::~TabStrip() {
FOR_EACH_OBSERVER(TabStripObserver, observers_,
TabStripDeleted(this));
StopAnimating(false);
DestroyDragController();
RemoveMessageLoopObserver();
RemoveAllChildViews(true);
}
void TabStrip::AddObserver(TabStripObserver* observer) {
observers_.AddObserver(observer);
}
void TabStrip::RemoveObserver(TabStripObserver* observer) {
observers_.RemoveObserver(observer);
}
void TabStrip::SetLayoutType(TabStripLayoutType layout_type,
bool adjust_layout) {
adjust_layout_ = adjust_layout;
if (layout_type == layout_type_)
return;
const int active_index = controller_->GetActiveIndex();
int active_center = 0;
if (active_index != -1) {
active_center = ideal_bounds(active_index).x() +
ideal_bounds(active_index).width() / 2;
}
layout_type_ = layout_type;
SetResetToShrinkOnExit(false);
SwapLayoutIfNecessary();
if (touch_layout_.get() && active_index != -1) {
touch_layout_->SetActiveTabLocation(
active_center - ideal_bounds(active_index).width() / 2);
AnimateToIdealBounds();
}
}
gfx::Rect TabStrip::GetNewTabButtonBounds() {
return newtab_button_->bounds();
}
bool TabStrip::SizeTabButtonToTopOfTabStrip() {
views::Widget* widget = GetWidget();
return browser_defaults::kSizeTabButtonToTopOfTabStrip ||
(widget && (widget->IsMaximized() || widget->IsFullscreen()));
}
void TabStrip::StartHighlight(int model_index) {
tab_at(model_index)->StartPulse();
}
void TabStrip::StopAllHighlighting() {
for (int i = 0; i < tab_count(); ++i)
tab_at(i)->StopPulse();
}
void TabStrip::AddTabAt(int model_index,
const TabRendererData& data,
bool is_active) {
if (drag_controller_.get() && !drag_controller_->is_mutating() &&
drag_controller_->is_dragging_window()) {
EndDrag(END_DRAG_COMPLETE);
}
Tab* tab = CreateTab();
tab->SetData(data);
UpdateTabsClosingMap(model_index, 1);
tabs_.Add(tab, model_index);
AddChildView(tab);
if (touch_layout_.get()) {
GenerateIdealBoundsForMiniTabs(NULL);
int add_types = 0;
if (data.mini)
add_types |= StackedTabStripLayout::kAddTypeMini;
if (is_active)
add_types |= StackedTabStripLayout::kAddTypeActive;
touch_layout_->AddTab(model_index, add_types, GetStartXForNormalTabs());
}
if (tab_count() > 1 && GetWidget() && GetWidget()->IsVisible())
StartInsertTabAnimation(model_index);
else
DoLayout();
SwapLayoutIfNecessary();
FOR_EACH_OBSERVER(TabStripObserver, observers_,
TabStripAddedTabAt(this, model_index));
}
void TabStrip::MoveTab(int from_model_index,
int to_model_index,
const TabRendererData& data) {
DCHECK_GT(tabs_.view_size(), 0);
Tab* last_tab = tab_at(tab_count() - 1);
tab_at(from_model_index)->SetData(data);
if (touch_layout_.get()) {
tabs_.MoveViewOnly(from_model_index, to_model_index);
int mini_count = 0;
GenerateIdealBoundsForMiniTabs(&mini_count);
touch_layout_->MoveTab(
from_model_index, to_model_index, controller_->GetActiveIndex(),
GetStartXForNormalTabs(), mini_count);
} else {
tabs_.Move(from_model_index, to_model_index);
}
StartMoveTabAnimation();
if (TabDragController::IsAttachedTo(this) &&
(last_tab != tab_at(tab_count() - 1) || last_tab->dragging())) {
newtab_button_->SetVisible(false);
}
SwapLayoutIfNecessary();
FOR_EACH_OBSERVER(TabStripObserver, observers_,
TabStripMovedTab(this, from_model_index, to_model_index));
}
void TabStrip::RemoveTabAt(int model_index) {
if (touch_layout_.get()) {
Tab* tab = tab_at(model_index);
tab->set_closing(true);
int old_x = tabs_.ideal_bounds(model_index).x();
RemoveTabFromViewModel(model_index);
touch_layout_->RemoveTab(model_index, GenerateIdealBoundsForMiniTabs(NULL),
old_x);
ScheduleRemoveTabAnimation(tab);
} else if (in_tab_close_ && model_index != GetModelCount()) {
StartMouseInitiatedRemoveTabAnimation(model_index);
} else {
StartRemoveTabAnimation(model_index);
}
SwapLayoutIfNecessary();
FOR_EACH_OBSERVER(TabStripObserver, observers_,
TabStripRemovedTabAt(this, model_index));
}
void TabStrip::SetTabData(int model_index, const TabRendererData& data) {
Tab* tab = tab_at(model_index);
bool mini_state_changed = tab->data().mini != data.mini;
tab->SetData(data);
if (mini_state_changed) {
if (touch_layout_.get()) {
int mini_tab_count = 0;
int start_x = GenerateIdealBoundsForMiniTabs(&mini_tab_count);
touch_layout_->SetXAndMiniCount(start_x, mini_tab_count);
}
if (GetWidget() && GetWidget()->IsVisible())
StartMiniTabAnimation();
else
DoLayout();
}
SwapLayoutIfNecessary();
}
void TabStrip::PrepareForCloseAt(int model_index, CloseTabSource source) {
if (!in_tab_close_ && IsAnimating()) {
StopAnimating(true);
}
if (!GetWidget())
return;
int model_count = GetModelCount();
if (model_index + 1 != model_count && model_count > 1) {
Tab* last_tab = tab_at(model_count - 1);
Tab* tab_being_removed = tab_at(model_index);
available_width_for_tabs_ = last_tab->x() + last_tab->width() -
tab_being_removed->width() - tab_h_offset();
if (model_index == 0 && tab_being_removed->data().mini &&
!tab_at(1)->data().mini) {
available_width_for_tabs_ -= kMiniToNonMiniGap;
}
}
in_tab_close_ = true;
resize_layout_timer_.Stop();
if (source == CLOSE_TAB_FROM_TOUCH) {
StartResizeLayoutTabsFromTouchTimer();
} else {
AddMessageLoopObserver();
}
}
void TabStrip::SetSelection(const ui::ListSelectionModel& old_selection,
const ui::ListSelectionModel& new_selection) {
if (touch_layout_.get()) {
touch_layout_->SetActiveIndex(new_selection.active());
if (!views::ViewModelUtils::IsAtIdealBounds(tabs_))
AnimateToIdealBounds();
SchedulePaint();
} else {
bool tiny_tabs = current_unselected_width_ != current_selected_width_;
if (!IsAnimating() && (!in_tab_close_ || tiny_tabs)) {
DoLayout();
} else {
SchedulePaint();
}
}
ui::ListSelectionModel::SelectedIndices no_longer_selected =
base::STLSetDifference<ui::ListSelectionModel::SelectedIndices>(
old_selection.selected_indices(),
new_selection.selected_indices());
for (size_t i = 0; i < no_longer_selected.size(); ++i)
tab_at(no_longer_selected[i])->StopMiniTabTitleAnimation();
}
void TabStrip::TabTitleChangedNotLoading(int model_index) {
Tab* tab = tab_at(model_index);
if (tab->data().mini && !tab->IsActive())
tab->StartMiniTabTitleAnimation();
}
Tab* TabStrip::tab_at(int index) const {
return static_cast<Tab*>(tabs_.view_at(index));
}
int TabStrip::GetModelIndexOfTab(const Tab* tab) const {
return tabs_.GetIndexOfView(tab);
}
int TabStrip::GetModelCount() const {
return controller_->GetCount();
}
bool TabStrip::IsValidModelIndex(int model_index) const {
return controller_->IsValidIndex(model_index);
}
bool TabStrip::IsDragSessionActive() const {
return drag_controller_.get() != NULL;
}
bool TabStrip::IsActiveDropTarget() const {
for (int i = 0; i < tab_count(); ++i) {
Tab* tab = tab_at(i);
if (tab->dragging())
return true;
}
return false;
}
bool TabStrip::IsTabStripEditable() const {
return !IsDragSessionActive() && !IsActiveDropTarget();
}
bool TabStrip::IsTabStripCloseable() const {
return !IsDragSessionActive();
}
void TabStrip::UpdateLoadingAnimations() {
controller_->UpdateLoadingAnimations();
}
bool TabStrip::IsPositionInWindowCaption(const gfx::Point& point) {
return IsRectInWindowCaption(gfx::Rect(point, gfx::Size(1, 1)));
}
bool TabStrip::IsRectInWindowCaption(const gfx::Rect& rect) {
views::View* v = GetEventHandlerForRect(rect);
if (v == this)
return true;
gfx::RectF rect_in_newtab_coords_f(rect);
View::ConvertRectToTarget(this, newtab_button_, &rect_in_newtab_coords_f);
gfx::Rect rect_in_newtab_coords = gfx::ToEnclosingRect(
rect_in_newtab_coords_f);
if (newtab_button_->GetLocalBounds().Intersects(rect_in_newtab_coords) &&
!newtab_button_->HitTestRect(rect_in_newtab_coords))
return true;
return false;
}
void TabStrip::SetBackgroundOffset(const gfx::Point& offset) {
for (int i = 0; i < tab_count(); ++i)
tab_at(i)->set_background_offset(offset);
newtab_button_->set_background_offset(offset);
}
views::View* TabStrip::newtab_button() {
return newtab_button_;
}
void TabStrip::SetImmersiveStyle(bool enable) {
if (immersive_style_ == enable)
return;
immersive_style_ = enable;
}
bool TabStrip::IsAnimating() const {
return bounds_animator_.IsAnimating();
}
void TabStrip::StopAnimating(bool layout) {
if (!IsAnimating())
return;
bounds_animator_.Cancel();
if (layout)
DoLayout();
}
void TabStrip::FileSupported(const GURL& url, bool supported) {
if (drop_info_.get() && drop_info_->url == url)
drop_info_->file_supported = supported;
}
const ui::ListSelectionModel& TabStrip::GetSelectionModel() {
return controller_->GetSelectionModel();
}
bool TabStrip::SupportsMultipleSelection() {
return touch_layout_.get() == NULL;
}
void TabStrip::SelectTab(Tab* tab) {
int model_index = GetModelIndexOfTab(tab);
if (IsValidModelIndex(model_index))
controller_->SelectTab(model_index);
}
void TabStrip::ExtendSelectionTo(Tab* tab) {
int model_index = GetModelIndexOfTab(tab);
if (IsValidModelIndex(model_index))
controller_->ExtendSelectionTo(model_index);
}
void TabStrip::ToggleSelected(Tab* tab) {
int model_index = GetModelIndexOfTab(tab);
if (IsValidModelIndex(model_index))
controller_->ToggleSelected(model_index);
}
void TabStrip::AddSelectionFromAnchorTo(Tab* tab) {
int model_index = GetModelIndexOfTab(tab);
if (IsValidModelIndex(model_index))
controller_->AddSelectionFromAnchorTo(model_index);
}
void TabStrip::CloseTab(Tab* tab, CloseTabSource source) {
if (tab->closing()) {
for (TabsClosingMap::const_iterator i(tabs_closing_map_.begin());
i != tabs_closing_map_.end(); ++i) {
std::vector<Tab*>::const_iterator j =
std::find(i->second.begin(), i->second.end(), tab);
if (j != i->second.end()) {
if (i->first + 1 < GetModelCount())
controller_->CloseTab(i->first + 1, source);
return;
}
}
NOTREACHED();
return;
}
int model_index = GetModelIndexOfTab(tab);
if (IsValidModelIndex(model_index))
controller_->CloseTab(model_index, source);
}
void TabStrip::ShowContextMenuForTab(Tab* tab,
const gfx::Point& p,
ui::MenuSourceType source_type) {
controller_->ShowContextMenuForTab(tab, p, source_type);
}
bool TabStrip::IsActiveTab(const Tab* tab) const {
int model_index = GetModelIndexOfTab(tab);
return IsValidModelIndex(model_index) &&
controller_->IsActiveTab(model_index);
}
bool TabStrip::IsTabSelected(const Tab* tab) const {
int model_index = GetModelIndexOfTab(tab);
return IsValidModelIndex(model_index) &&
controller_->IsTabSelected(model_index);
}
bool TabStrip::IsTabPinned(const Tab* tab) const {
if (tab->closing())
return false;
int model_index = GetModelIndexOfTab(tab);
return IsValidModelIndex(model_index) &&
controller_->IsTabPinned(model_index);
}
void TabStrip::MaybeStartDrag(
Tab* tab,
const ui::LocatedEvent& event,
const ui::ListSelectionModel& original_selection) {
if (IsAnimating() || tab->closing() ||
controller_->HasAvailableDragActions() == 0) {
return;
}
if (IsImmersiveStyle())
return;
int model_index = GetModelIndexOfTab(tab);
if (!IsValidModelIndex(model_index)) {
CHECK(false);
return;
}
std::vector<Tab*> tabs;
int size_to_selected = 0;
int x = tab->GetMirroredXInView(event.x());
int y = event.y();
for (int i = 0; i < tab_count(); ++i) {
Tab* other_tab = tab_at(i);
if (IsTabSelected(other_tab)) {
tabs.push_back(other_tab);
if (other_tab == tab) {
size_to_selected = GetSizeNeededForTabs(tabs);
x = size_to_selected - tab->width() + x;
}
}
}
DCHECK(!tabs.empty());
DCHECK(std::find(tabs.begin(), tabs.end(), tab) != tabs.end());
ui::ListSelectionModel selection_model;
if (!original_selection.IsSelected(model_index))
selection_model.Copy(original_selection);
drag_controller_.reset();
TabDragController::DetachBehavior detach_behavior =
TabDragController::DETACHABLE;
TabDragController::MoveBehavior move_behavior =
TabDragController::REORDER;
DCHECK(event.type() == ui::ET_MOUSE_PRESSED ||
event.type() == ui::ET_GESTURE_BEGIN);
if (touch_layout_.get() &&
((event.type() == ui::ET_MOUSE_PRESSED &&
(((event.flags() & ui::EF_FROM_TOUCH) &&
static_cast<const ui::MouseEvent&>(event).IsLeftMouseButton()) ||
(!(event.flags() & ui::EF_FROM_TOUCH) &&
static_cast<const ui::MouseEvent&>(event).IsControlDown()))) ||
(event.type() == ui::ET_GESTURE_BEGIN && !event.IsControlDown()))) {
move_behavior = TabDragController::MOVE_VISIBILE_TABS;
}
#if defined(OS_WIN)
if (win8::IsSingleWindowMetroMode())
detach_behavior = TabDragController::NOT_DETACHABLE;
#endif
drag_controller_.reset(new TabDragController);
drag_controller_->Init(
this, tab, tabs, gfx::Point(x, y), event.x(), selection_model,
detach_behavior, move_behavior, EventSourceFromEvent(event));
}
void TabStrip::ContinueDrag(views::View* view, const ui::LocatedEvent& event) {
if (drag_controller_.get() &&
drag_controller_->event_source() == EventSourceFromEvent(event)) {
gfx::Point screen_location(event.location());
views::View::ConvertPointToScreen(view, &screen_location);
drag_controller_->Drag(screen_location);
}
}
bool TabStrip::EndDrag(EndDragReason reason) {
if (!drag_controller_.get())
return false;
bool started_drag = drag_controller_->started_drag();
drag_controller_->EndDrag(reason);
return started_drag;
}
Tab* TabStrip::GetTabAt(Tab* tab, const gfx::Point& tab_in_tab_coordinates) {
gfx::Point local_point = tab_in_tab_coordinates;
ConvertPointToTarget(tab, this, &local_point);
views::View* view = GetEventHandlerForPoint(local_point);
if (!view)
return NULL;
while (view && view != this && view->id() != VIEW_ID_TAB)
view = view->parent();
return view && view->id() == VIEW_ID_TAB ? static_cast<Tab*>(view) : NULL;
}
void TabStrip::OnMouseEventInTab(views::View* source,
const ui::MouseEvent& event) {
UpdateLayoutTypeFromMouseEvent(source, event);
}
bool TabStrip::ShouldPaintTab(const Tab* tab, gfx::Rect* clip) {
if (!(touch_layout_.get() || IsStackingDraggedTabs()))
return true;
int index = GetModelIndexOfTab(tab);
if (index == -1)
return true;
int active_index = IsStackingDraggedTabs() ?
controller_->GetActiveIndex() : touch_layout_->active_index();
if (active_index == tab_count())
active_index--;
if (index < active_index) {
if (tab_at(index)->x() == tab_at(index + 1)->x())
return false;
if (tab_at(index)->x() > tab_at(index + 1)->x())
return true;
clip->SetRect(0, 0, tab_at(index + 1)->x() - tab_at(index)->x() +
stacked_tab_left_clip(),
tab_at(index)->height());
} else if (index > active_index && index > 0) {
const gfx::Rect& tab_bounds(tab_at(index)->bounds());
const gfx::Rect& previous_tab_bounds(tab_at(index - 1)->bounds());
if (tab_bounds.x() == previous_tab_bounds.x())
return false;
if (tab_bounds.x() < previous_tab_bounds.x())
return true;
if (previous_tab_bounds.right() + tab_h_offset() != tab_bounds.x()) {
int x = previous_tab_bounds.right() - tab_bounds.x() -
stacked_tab_right_clip();
clip->SetRect(x, 0, tab_bounds.width() - x, tab_bounds.height());
}
}
return true;
}
bool TabStrip::IsImmersiveStyle() const {
return immersive_style_;
}
void TabStrip::MouseMovedOutOfHost() {
ResizeLayoutTabs();
if (reset_to_shrink_on_exit_) {
reset_to_shrink_on_exit_ = false;
SetLayoutType(TAB_STRIP_LAYOUT_SHRINK, true);
controller_->LayoutTypeMaybeChanged();
}
}
void TabStrip::Layout() {
if (last_layout_size_ == size())
return;
if (IsDragSessionActive())
return;
DoLayout();
}
void TabStrip::PaintChildren(gfx::Canvas* canvas) {
Tab* active_tab = NULL;
std::vector<Tab*> tabs_dragging;
std::vector<Tab*> selected_tabs;
int selected_tab_count = 0;
bool is_dragging = false;
int active_tab_index = -1;
const bool stacking = (layout_type_ == TAB_STRIP_LAYOUT_STACKED) ||
IsStackingDraggedTabs();
const chrome::HostDesktopType host_desktop_type =
chrome::GetHostDesktopTypeForNativeView(GetWidget()->GetNativeView());
const int inactive_tab_alpha =
host_desktop_type == chrome::HOST_DESKTOP_TYPE_ASH ?
kInactiveTabAndNewTabButtonAlphaAsh :
kInactiveTabAndNewTabButtonAlpha;
if (inactive_tab_alpha < 255)
canvas->SaveLayerAlpha(inactive_tab_alpha);
PaintClosingTabs(canvas, tab_count());
for (int i = tab_count() - 1; i >= 0; --i) {
Tab* tab = tab_at(i);
if (tab->IsSelected())
selected_tab_count++;
if (tab->dragging() && !stacking) {
is_dragging = true;
if (tab->IsActive()) {
active_tab = tab;
active_tab_index = i;
} else {
tabs_dragging.push_back(tab);
}
} else if (!tab->IsActive()) {
if (!tab->IsSelected()) {
if (!stacking)
tab->Paint(canvas);
} else {
selected_tabs.push_back(tab);
}
} else {
active_tab = tab;
active_tab_index = i;
}
PaintClosingTabs(canvas, i);
}
if (stacking && active_tab_index >= 0) {
for (int i = 0; i < active_tab_index; ++i) {
Tab* tab = tab_at(i);
tab->Paint(canvas);
}
for (int i = tab_count() - 1; i > active_tab_index; --i) {
Tab* tab = tab_at(i);
tab->Paint(canvas);
}
}
if (inactive_tab_alpha < 255)
canvas->Restore();
if (GetWidget()->ShouldWindowContentsBeTransparent()) {
SkPaint paint;
int alpha = selected_tab_count > 1 ?
kGlassFrameInactiveTabAlphaMultiSelection :
kGlassFrameInactiveTabAlpha;
paint.setColor(SkColorSetARGB(alpha, 255, 255, 255));
paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
paint.setStyle(SkPaint::kFill_Style);
canvas->DrawRect(gfx::Rect(0, 0, width(), height() - 2), paint);
}
for (size_t i = 0; i < selected_tabs.size(); ++i)
selected_tabs[i]->Paint(canvas);
if (active_tab && !is_dragging)
active_tab->Paint(canvas);
if (inactive_tab_alpha < 255)
canvas->SaveLayerAlpha(inactive_tab_alpha);
newtab_button_->Paint(canvas);
if (inactive_tab_alpha < 255)
canvas->Restore();
for (size_t i = 0; i < tabs_dragging.size(); ++i)
tabs_dragging[i]->Paint(canvas);
if (active_tab && is_dragging)
active_tab->Paint(canvas);
}
const char* TabStrip::GetClassName() const {
return kViewClassName;
}
gfx::Size TabStrip::GetPreferredSize() {
int needed_width;
if (touch_layout_.get() || adjust_layout_) {
needed_width = Tab::GetTouchWidth() +
(2 * kStackedPadding * kMaxStackedCount);
} else {
needed_width = Tab::GetMinimumSelectedSize().width();
}
needed_width += new_tab_button_width();
if (immersive_style_)
return gfx::Size(needed_width, Tab::GetImmersiveHeight());
return gfx::Size(needed_width, Tab::GetMinimumUnselectedSize().height());
}
void TabStrip::OnDragEntered(const DropTargetEvent& event) {
StopAnimating(true);
UpdateDropIndex(event);
GURL url;
base::string16 title;
if (event.data().GetURLAndTitle(
ui::OSExchangeData::CONVERT_FILENAMES, &url, &title) &&
url.is_valid()) {
drop_info_->url = url;
if (url.SchemeIsFile())
controller()->CheckFileSupported(url);
}
}
int TabStrip::OnDragUpdated(const DropTargetEvent& event) {
UpdateDropIndex(event);
if (!drop_info_->file_supported)
return ui::DragDropTypes::DRAG_NONE;
return GetDropEffect(event);
}
void TabStrip::OnDragExited() {
SetDropIndex(-1, false);
}
int TabStrip::OnPerformDrop(const DropTargetEvent& event) {
if (!drop_info_.get())
return ui::DragDropTypes::DRAG_NONE;
const int drop_index = drop_info_->drop_index;
const bool drop_before = drop_info_->drop_before;
const bool file_supported = drop_info_->file_supported;
SetDropIndex(-1, false);
GURL url;
base::string16 title;
if (!file_supported ||
!event.data().GetURLAndTitle(
ui::OSExchangeData::CONVERT_FILENAMES, &url, &title) ||
!url.is_valid())
return ui::DragDropTypes::DRAG_NONE;
controller()->PerformDrop(drop_before, drop_index, url);
return GetDropEffect(event);
}
void TabStrip::GetAccessibleState(ui::AXViewState* state) {
state->role = ui::AX_ROLE_TAB_LIST;
}
views::View* TabStrip::GetEventHandlerForRect(const gfx::Rect& rect) {
if (!views::UsePointBasedTargeting(rect))
return View::GetEventHandlerForRect(rect);
const gfx::Point point(rect.CenterPoint());
if (!touch_layout_.get()) {
views::View* v = View::GetEventHandlerForRect(rect);
if (v && v != this && strcmp(v->GetClassName(), Tab::kViewClassName))
return v;
views::View* tab = FindTabHitByPoint(point);
if (tab)
return tab;
} else {
if (newtab_button_->visible()) {
views::View* view =
ConvertPointToViewAndGetEventHandler(this, newtab_button_, point);
if (view)
return view;
}
Tab* tab = FindTabForEvent(point);
if (tab)
return ConvertPointToViewAndGetEventHandler(this, tab, point);
}
return this;
}
views::View* TabStrip::GetTooltipHandlerForPoint(const gfx::Point& point) {
if (!HitTestPoint(point))
return NULL;
if (!touch_layout_.get()) {
views::View* v = View::GetTooltipHandlerForPoint(point);
if (v && v != this && strcmp(v->GetClassName(), Tab::kViewClassName))
return v;
views::View* tab = FindTabHitByPoint(point);
if (tab)
return tab;
} else {
if (newtab_button_->visible()) {
views::View* view =
ConvertPointToViewAndGetTooltipHandler(this, newtab_button_, point);
if (view)
return view;
}
Tab* tab = FindTabForEvent(point);
if (tab)
return ConvertPointToViewAndGetTooltipHandler(this, tab, point);
}
return this;
}
int TabStrip::GetImmersiveHeight() {
return Tab::GetImmersiveHeight();
}
int TabStrip::GetMiniTabCount() const {
int mini_count = 0;
while (mini_count < tab_count() && tab_at(mini_count)->data().mini)
mini_count++;
return mini_count;
}
void TabStrip::ButtonPressed(views::Button* sender, const ui::Event& event) {
if (sender == newtab_button_) {
content::RecordAction(UserMetricsAction("NewTab_Button"));
UMA_HISTOGRAM_ENUMERATION("Tab.NewTab", TabStripModel::NEW_TAB_BUTTON,
TabStripModel::NEW_TAB_ENUM_COUNT);
if (event.IsMouseEvent()) {
const ui::MouseEvent& mouse = static_cast<const ui::MouseEvent&>(event);
if (mouse.IsOnlyMiddleMouseButton()) {
base::string16 clipboard_text = GetClipboardText();
if (!clipboard_text.empty())
controller()->CreateNewTabWithLocation(clipboard_text);
return;
}
}
controller()->CreateNewTab();
if (event.type() == ui::ET_GESTURE_TAP)
TouchUMA::RecordGestureAction(TouchUMA::GESTURE_NEWTAB_TAP);
}
}
const views::View* TabStrip::GetViewByID(int view_id) const {
if (tab_count() > 0) {
if (view_id == VIEW_ID_TAB_LAST) {
return tab_at(tab_count() - 1);
} else if ((view_id >= VIEW_ID_TAB_0) && (view_id < VIEW_ID_TAB_LAST)) {
int index = view_id - VIEW_ID_TAB_0;
if (index >= 0 && index < tab_count()) {
return tab_at(index);
} else {
return NULL;
}
}
}
return View::GetViewByID(view_id);
}
bool TabStrip::OnMousePressed(const ui::MouseEvent& event) {
UpdateLayoutTypeFromMouseEvent(this, event);
return false;
}
bool TabStrip::OnMouseDragged(const ui::MouseEvent& event) {
ContinueDrag(this, event);
return true;
}
void TabStrip::OnMouseReleased(const ui::MouseEvent& event) {
EndDrag(END_DRAG_COMPLETE);
UpdateLayoutTypeFromMouseEvent(this, event);
}
void TabStrip::OnMouseCaptureLost() {
EndDrag(END_DRAG_CAPTURE_LOST);
}
void TabStrip::OnMouseMoved(const ui::MouseEvent& event) {
UpdateLayoutTypeFromMouseEvent(this, event);
}
void TabStrip::OnMouseEntered(const ui::MouseEvent& event) {
SetResetToShrinkOnExit(true);
}
void TabStrip::OnGestureEvent(ui::GestureEvent* event) {
SetResetToShrinkOnExit(false);
switch (event->type()) {
case ui::ET_GESTURE_SCROLL_END:
case ui::ET_SCROLL_FLING_START:
case ui::ET_GESTURE_END:
EndDrag(END_DRAG_COMPLETE);
if (adjust_layout_) {
SetLayoutType(TAB_STRIP_LAYOUT_STACKED, true);
controller_->LayoutTypeMaybeChanged();
}
break;
case ui::ET_GESTURE_LONG_PRESS:
if (drag_controller_.get())
drag_controller_->SetMoveBehavior(TabDragController::REORDER);
break;
case ui::ET_GESTURE_LONG_TAP: {
EndDrag(END_DRAG_CANCEL);
gfx::Point local_point = event->location();
Tab* tab = FindTabForEvent(local_point);
if (tab) {
ConvertPointToScreen(this, &local_point);
ShowContextMenuForTab(tab, local_point, ui::MENU_SOURCE_TOUCH);
}
break;
}
case ui::ET_GESTURE_SCROLL_UPDATE:
ContinueDrag(this, *event);
break;
case ui::ET_GESTURE_BEGIN:
EndDrag(END_DRAG_CANCEL);
break;
case ui::ET_GESTURE_TAP: {
const int active_index = controller_->GetActiveIndex();
DCHECK_NE(-1, active_index);
Tab* active_tab = tab_at(active_index);
TouchUMA::GestureActionType action = TouchUMA::GESTURE_TABNOSWITCH_TAP;
if (active_tab->tab_activated_with_last_gesture_begin())
action = TouchUMA::GESTURE_TABSWITCH_TAP;
TouchUMA::RecordGestureAction(action);
break;
}
default:
break;
}
event->SetHandled();
}
void TabStrip::Init() {
set_id(VIEW_ID_TAB_STRIP);
set_notify_enter_exit_on_child(true);
newtab_button_bounds_.SetRect(0,
0,
newtab_button_asset_width(),
newtab_button_asset_height() +
newtab_button_v_offset());
newtab_button_ = new NewTabButton(this, this);
newtab_button_->SetTooltipText(
l10n_util::GetStringUTF16(IDS_TOOLTIP_NEW_TAB));
newtab_button_->SetAccessibleName(
l10n_util::GetStringUTF16(IDS_ACCNAME_NEWTAB));
newtab_button_->SetImageAlignment(views::ImageButton::ALIGN_LEFT,
views::ImageButton::ALIGN_BOTTOM);
AddChildView(newtab_button_);
if (drop_indicator_width == 0) {
gfx::ImageSkia* drop_image = GetDropArrowImage(true);
drop_indicator_width = drop_image->width();
drop_indicator_height = drop_image->height();
}
}
Tab* TabStrip::CreateTab() {
Tab* tab = new Tab(this);
tab->set_animation_container(animation_container_.get());
return tab;
}
void TabStrip::StartInsertTabAnimation(int model_index) {
PrepareForAnimation();
in_tab_close_ = false;
available_width_for_tabs_ = -1;
GenerateIdealBounds();
Tab* tab = tab_at(model_index);
if (model_index == 0) {
tab->SetBounds(0, ideal_bounds(model_index).y(), 0,
ideal_bounds(model_index).height());
} else {
Tab* last_tab = tab_at(model_index - 1);
tab->SetBounds(last_tab->bounds().right() + tab_h_offset(),
ideal_bounds(model_index).y(), 0,
ideal_bounds(model_index).height());
}
AnimateToIdealBounds();
}
void TabStrip::StartMoveTabAnimation() {
PrepareForAnimation();
GenerateIdealBounds();
AnimateToIdealBounds();
}
void TabStrip::StartRemoveTabAnimation(int model_index) {
PrepareForAnimation();
Tab* tab = tab_at(model_index);
tab->set_closing(true);
RemoveTabFromViewModel(model_index);
ScheduleRemoveTabAnimation(tab);
}
void TabStrip::ScheduleRemoveTabAnimation(Tab* tab) {
GenerateIdealBounds();
AnimateToIdealBounds();
gfx::Rect tab_bounds = tab->bounds();
tab_bounds.set_width(0);
bounds_animator_.AnimateViewTo(tab, tab_bounds);
bounds_animator_.SetAnimationDelegate(tab, new RemoveTabDelegate(this, tab),
true);
if (TabDragController::IsAttachedTo(this)) {
bounds_animator_.StopAnimatingView(newtab_button_);
newtab_button_->SetBoundsRect(newtab_button_bounds_);
}
}
void TabStrip::AnimateToIdealBounds() {
for (int i = 0; i < tab_count(); ++i) {
Tab* tab = tab_at(i);
if (!tab->dragging())
bounds_animator_.AnimateViewTo(tab, ideal_bounds(i));
}
bounds_animator_.AnimateViewTo(newtab_button_, newtab_button_bounds_);
}
bool TabStrip::ShouldHighlightCloseButtonAfterRemove() {
return in_tab_close_;
}
void TabStrip::DoLayout() {
last_layout_size_ = size();
StopAnimating(false);
SwapLayoutIfNecessary();
if (touch_layout_.get())
touch_layout_->SetWidth(size().width() - new_tab_button_width());
GenerateIdealBounds();
views::ViewModelUtils::SetViewBoundsToIdealBounds(tabs_);
SchedulePaint();
bounds_animator_.StopAnimatingView(newtab_button_);
newtab_button_->SetBoundsRect(newtab_button_bounds_);
}
void TabStrip::DragActiveTab(const std::vector<int>& initial_positions,
int delta) {
DCHECK_EQ(tab_count(), static_cast<int>(initial_positions.size()));
if (!touch_layout_.get()) {
StackDraggedTabs(delta);
return;
}
SetIdealBoundsFromPositions(initial_positions);
touch_layout_->DragActiveTab(delta);
DoLayout();
}
void TabStrip::SetIdealBoundsFromPositions(const std::vector<int>& positions) {
if (static_cast<size_t>(tab_count()) != positions.size())
return;
for (int i = 0; i < tab_count(); ++i) {
gfx::Rect bounds(ideal_bounds(i));
bounds.set_x(positions[i]);
set_ideal_bounds(i, bounds);
}
}
void TabStrip::StackDraggedTabs(int delta) {
DCHECK(!touch_layout_.get());
GenerateIdealBounds();
const int active_index = controller_->GetActiveIndex();
DCHECK_NE(-1, active_index);
if (delta < 0) {
const int adjusted_delta =
std::min(ideal_bounds(active_index).x() -
kStackedPadding * std::min(active_index, kMaxStackedCount),
-delta);
for (int i = 0; i <= active_index; ++i) {
const int min_x = std::min(i, kMaxStackedCount) * kStackedPadding;
gfx::Rect new_bounds(ideal_bounds(i));
new_bounds.set_x(std::max(min_x, new_bounds.x() - adjusted_delta));
set_ideal_bounds(i, new_bounds);
}
const bool is_active_mini = tab_at(active_index)->data().mini;
const int active_width = ideal_bounds(active_index).width();
for (int i = active_index + 1; i < tab_count(); ++i) {
const int max_x = ideal_bounds(active_index).x() +
(kStackedPadding * std::min(i - active_index, kMaxStackedCount));
gfx::Rect new_bounds(ideal_bounds(i));
int new_x = std::max(new_bounds.x() + delta, max_x);
if (new_x == max_x && !tab_at(i)->data().mini && !is_active_mini &&
new_bounds.width() != active_width)
new_x += (active_width - new_bounds.width());
new_bounds.set_x(new_x);
set_ideal_bounds(i, new_bounds);
}
} else {
const int last_tab_width = ideal_bounds(tab_count() - 1).width();
const int last_tab_x = width() - new_tab_button_width() - last_tab_width;
if (active_index == tab_count() - 1 &&
ideal_bounds(tab_count() - 1).x() == last_tab_x)
return;
const int adjusted_delta =
std::min(last_tab_x -
kStackedPadding * std::min(tab_count() - active_index - 1,
kMaxStackedCount) -
ideal_bounds(active_index).x(),
delta);
for (int last_index = tab_count() - 1, i = last_index; i >= active_index;
--i) {
const int max_x = last_tab_x -
std::min(tab_count() - i - 1, kMaxStackedCount) * kStackedPadding;
gfx::Rect new_bounds(ideal_bounds(i));
int new_x = std::min(max_x, new_bounds.x() + adjusted_delta);
if (new_x == max_x && !tab_at(i)->data().mini &&
new_bounds.width() != last_tab_width)
new_x += (last_tab_width - new_bounds.width());
new_bounds.set_x(new_x);
set_ideal_bounds(i, new_bounds);
}
for (int i = active_index - 1; i >= 0; --i) {
const int min_x = ideal_bounds(active_index).x() -
std::min(active_index - i, kMaxStackedCount) * kStackedPadding;
gfx::Rect new_bounds(ideal_bounds(i));
new_bounds.set_x(std::min(min_x, new_bounds.x() + delta));
set_ideal_bounds(i, new_bounds);
}
if (ideal_bounds(tab_count() - 1).right() >= newtab_button_->x())
newtab_button_->SetVisible(false);
}
views::ViewModelUtils::SetViewBoundsToIdealBounds(tabs_);
SchedulePaint();
}
bool TabStrip::IsStackingDraggedTabs() const {
return drag_controller_.get() && drag_controller_->started_drag() &&
(drag_controller_->move_behavior() ==
TabDragController::MOVE_VISIBILE_TABS);
}
void TabStrip::LayoutDraggedTabsAt(const std::vector<Tab*>& tabs,
Tab* active_tab,
const gfx::Point& location,
bool initial_drag) {
if (tab_at(tab_count() - 1)->dragging())
newtab_button_->SetVisible(false);
std::vector<gfx::Rect> bounds;
CalculateBoundsForDraggedTabs(tabs, &bounds);
DCHECK_EQ(tabs.size(), bounds.size());
int active_tab_model_index = GetModelIndexOfTab(active_tab);
int active_tab_index = static_cast<int>(
std::find(tabs.begin(), tabs.end(), active_tab) - tabs.begin());
for (size_t i = 0; i < tabs.size(); ++i) {
Tab* tab = tabs[i];
gfx::Rect new_bounds = bounds[i];
new_bounds.Offset(location.x(), location.y());
int consecutive_index =
active_tab_model_index - (active_tab_index - static_cast<int>(i));
if ((initial_drag &&
GetModelIndexOfTab(tabs[i]) != consecutive_index) ||
bounds_animator_.IsAnimating(tabs[i])) {
bounds_animator_.SetTargetBounds(tabs[i], new_bounds);
} else {
tab->SetBoundsRect(new_bounds);
}
}
}
void TabStrip::CalculateBoundsForDraggedTabs(const std::vector<Tab*>& tabs,
std::vector<gfx::Rect>* bounds) {
int x = 0;
for (size_t i = 0; i < tabs.size(); ++i) {
Tab* tab = tabs[i];
if (i > 0 && tab->data().mini != tabs[i - 1]->data().mini)
x += kMiniToNonMiniGap;
gfx::Rect new_bounds = tab->bounds();
new_bounds.set_origin(gfx::Point(x, 0));
bounds->push_back(new_bounds);
x += tab->width() + tab_h_offset();
}
}
int TabStrip::GetSizeNeededForTabs(const std::vector<Tab*>& tabs) {
int width = 0;
for (size_t i = 0; i < tabs.size(); ++i) {
Tab* tab = tabs[i];
width += tab->width();
if (i > 0 && tab->data().mini != tabs[i - 1]->data().mini)
width += kMiniToNonMiniGap;
}
if (tabs.size() > 0)
width += tab_h_offset() * static_cast<int>(tabs.size() - 1);
return width;
}
void TabStrip::RemoveTabFromViewModel(int index) {
tabs_closing_map_[index].push_back(tab_at(index));
UpdateTabsClosingMap(index + 1, -1);
tabs_.Remove(index);
}
void TabStrip::RemoveAndDeleteTab(Tab* tab) {
scoped_ptr<Tab> deleter(tab);
for (TabsClosingMap::iterator i(tabs_closing_map_.begin());
i != tabs_closing_map_.end(); ++i) {
std::vector<Tab*>::iterator j =
std::find(i->second.begin(), i->second.end(), tab);
if (j != i->second.end()) {
i->second.erase(j);
if (i->second.empty())
tabs_closing_map_.erase(i);
return;
}
}
NOTREACHED();
}
void TabStrip::UpdateTabsClosingMap(int index, int delta) {
if (tabs_closing_map_.empty())
return;
if (delta == -1 &&
tabs_closing_map_.find(index - 1) != tabs_closing_map_.end() &&
tabs_closing_map_.find(index) != tabs_closing_map_.end()) {
const std::vector<Tab*>& tabs(tabs_closing_map_[index]);
tabs_closing_map_[index - 1].insert(
tabs_closing_map_[index - 1].end(), tabs.begin(), tabs.end());
}
TabsClosingMap updated_map;
for (TabsClosingMap::iterator i(tabs_closing_map_.begin());
i != tabs_closing_map_.end(); ++i) {
if (i->first > index)
updated_map[i->first + delta] = i->second;
else if (i->first < index)
updated_map[i->first] = i->second;
}
if (delta > 0 && tabs_closing_map_.find(index) != tabs_closing_map_.end())
updated_map[index + delta] = tabs_closing_map_[index];
tabs_closing_map_.swap(updated_map);
}
void TabStrip::StartedDraggingTabs(const std::vector<Tab*>& tabs) {
controller()->OnStartedDraggingTabs();
if (!drag_controller_.get())
newtab_button_->SetVisible(false);
PrepareForAnimation();
for (int i = 0; i < tab_count(); ++i)
tab_at(i)->set_dragging(false);
for (size_t i = 0; i < tabs.size(); ++i) {
tabs[i]->set_dragging(true);
bounds_animator_.StopAnimatingView(tabs[i]);
}
GenerateIdealBounds();
for (size_t i = 0; i < tabs.size(); ++i) {
int tab_data_index = GetModelIndexOfTab(tabs[i]);
DCHECK_NE(-1, tab_data_index);
tabs[i]->SetBoundsRect(ideal_bounds(tab_data_index));
}
SchedulePaint();
}
void TabStrip::DraggedTabsDetached() {
controller()->OnStoppedDraggingTabs();
newtab_button_->SetVisible(true);
}
void TabStrip::StoppedDraggingTabs(const std::vector<Tab*>& tabs,
const std::vector<int>& initial_positions,
bool move_only,
bool completed) {
controller()->OnStoppedDraggingTabs();
newtab_button_->SetVisible(true);
if (move_only && touch_layout_.get()) {
if (completed) {
touch_layout_->SizeToFit();
} else {
SetIdealBoundsFromPositions(initial_positions);
}
}
bool is_first_tab = true;
for (size_t i = 0; i < tabs.size(); ++i)
StoppedDraggingTab(tabs[i], &is_first_tab);
}
void TabStrip::StoppedDraggingTab(Tab* tab, bool* is_first_tab) {
int tab_data_index = GetModelIndexOfTab(tab);
if (tab_data_index == -1) {
return;
}
if (*is_first_tab) {
*is_first_tab = false;
PrepareForAnimation();
GenerateIdealBounds();
AnimateToIdealBounds();
}
bounds_animator_.AnimateViewTo(tab, ideal_bounds(tab_data_index));
bounds_animator_.SetAnimationDelegate(
tab, new ResetDraggingStateDelegate(tab), true);
}
void TabStrip::OwnDragController(TabDragController* controller) {
if (controller != drag_controller_.get())
drag_controller_.reset(controller);
}
void TabStrip::DestroyDragController() {
newtab_button_->SetVisible(true);
drag_controller_.reset();
}
TabDragController* TabStrip::ReleaseDragController() {
return drag_controller_.release();
}
void TabStrip::PaintClosingTabs(gfx::Canvas* canvas, int index) {
if (tabs_closing_map_.find(index) == tabs_closing_map_.end())
return;
const std::vector<Tab*>& tabs = tabs_closing_map_[index];
for (std::vector<Tab*>::const_reverse_iterator i(tabs.rbegin());
i != tabs.rend(); ++i) {
(*i)->Paint(canvas);
}
}
void TabStrip::UpdateLayoutTypeFromMouseEvent(views::View* source,
const ui::MouseEvent& event) {
if (!GetAdjustLayout())
return;
switch (event.type()) {
case ui::ET_MOUSE_PRESSED:
mouse_move_count_ = 0;
last_mouse_move_time_ = base::TimeTicks();
SetResetToShrinkOnExit((event.flags() & ui::EF_FROM_TOUCH) == 0);
if (reset_to_shrink_on_exit_ && touch_layout_.get()) {
gfx::Point tab_strip_point(event.location());
views::View::ConvertPointToTarget(source, this, &tab_strip_point);
Tab* tab = FindTabForEvent(tab_strip_point);
if (tab && touch_layout_->IsStacked(GetModelIndexOfTab(tab))) {
SetLayoutType(TAB_STRIP_LAYOUT_SHRINK, true);
controller_->LayoutTypeMaybeChanged();
}
}
break;
case ui::ET_MOUSE_MOVED: {
#if defined(USE_ASH)
SetResetToShrinkOnExit(true);
#else
gfx::Point location(event.location());
ConvertPointToTarget(source, this, &location);
if (location == last_mouse_move_location_)
return;
last_mouse_move_location_ = location;
if ((event.flags() & ui::EF_FROM_TOUCH) == 0 &&
(event.flags() & ui::EF_IS_SYNTHESIZED) == 0) {
if ((base::TimeTicks::Now() - last_mouse_move_time_).InMilliseconds() <
kMouseMoveTimeMS) {
if (mouse_move_count_++ == kMouseMoveCountBeforeConsiderReal)
SetResetToShrinkOnExit(true);
} else {
mouse_move_count_ = 1;
last_mouse_move_time_ = base::TimeTicks::Now();
}
} else {
last_mouse_move_time_ = base::TimeTicks();
}
#endif
break;
}
case ui::ET_MOUSE_RELEASED: {
gfx::Point location(event.location());
ConvertPointToTarget(source, this, &location);
last_mouse_move_location_ = location;
mouse_move_count_ = 0;
last_mouse_move_time_ = base::TimeTicks();
if ((event.flags() & ui::EF_FROM_TOUCH) == ui::EF_FROM_TOUCH) {
SetLayoutType(TAB_STRIP_LAYOUT_STACKED, true);
controller_->LayoutTypeMaybeChanged();
}
break;
}
default:
break;
}
}
void TabStrip::GetCurrentTabWidths(double* unselected_width,
double* selected_width) const {
*unselected_width = current_unselected_width_;
*selected_width = current_selected_width_;
}
void TabStrip::GetDesiredTabWidths(int tab_count,
int mini_tab_count,
double* unselected_width,
double* selected_width) const {
DCHECK(tab_count >= 0 && mini_tab_count >= 0 && mini_tab_count <= tab_count);
const double min_unselected_width = Tab::GetMinimumUnselectedSize().width();
const double min_selected_width = Tab::GetMinimumSelectedSize().width();
*unselected_width = min_unselected_width;
*selected_width = min_selected_width;
if (tab_count == 0) {
return;
}
int available_width;
if (available_width_for_tabs_ < 0) {
available_width = width() - new_tab_button_width();
} else {
available_width = available_width_for_tabs_;
}
if (mini_tab_count > 0) {
available_width -= mini_tab_count * (Tab::GetMiniWidth() + tab_h_offset());
tab_count -= mini_tab_count;
if (tab_count == 0) {
*selected_width = *unselected_width = Tab::GetStandardSize().width();
return;
}
available_width -= kMiniToNonMiniGap;
}
const int total_offset = tab_h_offset() * (tab_count - 1);
const double desired_tab_width = std::min((static_cast<double>(
available_width - total_offset) / static_cast<double>(tab_count)),
static_cast<double>(Tab::GetStandardSize().width()));
*unselected_width = std::max(desired_tab_width, min_unselected_width);
*selected_width = std::max(desired_tab_width, min_selected_width);
if (tab_count > 1) {
if ((min_unselected_width < min_selected_width) &&
(desired_tab_width < min_selected_width)) {
*unselected_width = std::max(static_cast<double>(
available_width - total_offset - min_selected_width) /
static_cast<double>(tab_count - 1), min_unselected_width);
} else if ((min_unselected_width > min_selected_width) &&
(desired_tab_width < min_unselected_width)) {
*selected_width = std::max(available_width - total_offset -
(min_unselected_width * (tab_count - 1)), min_selected_width);
}
}
}
void TabStrip::ResizeLayoutTabs() {
if (tab_count() == 0)
return;
RemoveMessageLoopObserver();
in_tab_close_ = false;
available_width_for_tabs_ = -1;
int mini_tab_count = GetMiniTabCount();
if (mini_tab_count == tab_count()) {
return;
}
StartResizeLayoutAnimation();
}
void TabStrip::ResizeLayoutTabsFromTouch() {
if (!drag_controller_.get())
ResizeLayoutTabs();
else
StartResizeLayoutTabsFromTouchTimer();
}
void TabStrip::StartResizeLayoutTabsFromTouchTimer() {
resize_layout_timer_.Stop();
resize_layout_timer_.Start(
FROM_HERE, base::TimeDelta::FromMilliseconds(kTouchResizeLayoutTimeMS),
this, &TabStrip::ResizeLayoutTabsFromTouch);
}
void TabStrip::SetTabBoundsForDrag(const std::vector<gfx::Rect>& tab_bounds) {
StopAnimating(false);
DCHECK_EQ(tab_count(), static_cast<int>(tab_bounds.size()));
for (int i = 0; i < tab_count(); ++i)
tab_at(i)->SetBoundsRect(tab_bounds[i]);
last_layout_size_ = gfx::Size();
}
void TabStrip::AddMessageLoopObserver() {
if (!mouse_watcher_.get()) {
mouse_watcher_.reset(
new views::MouseWatcher(
new views::MouseWatcherViewHost(
this, gfx::Insets(0, 0, kTabStripAnimationVSlop, 0)),
this));
}
mouse_watcher_->Start();
}
void TabStrip::RemoveMessageLoopObserver() {
mouse_watcher_.reset(NULL);
}
gfx::Rect TabStrip::GetDropBounds(int drop_index,
bool drop_before,
bool* is_beneath) {
DCHECK_NE(drop_index, -1);
int center_x;
if (drop_index < tab_count()) {
Tab* tab = tab_at(drop_index);
if (drop_before)
center_x = tab->x() - (tab_h_offset() / 2);
else
center_x = tab->x() + (tab->width() / 2);
} else {
Tab* last_tab = tab_at(drop_index - 1);
center_x = last_tab->x() + last_tab->width() + (tab_h_offset() / 2);
}
center_x = GetMirroredXInView(center_x);
gfx::Point drop_loc(center_x - drop_indicator_width / 2,
-drop_indicator_height);
ConvertPointToScreen(this, &drop_loc);
gfx::Rect drop_bounds(drop_loc.x(), drop_loc.y(), drop_indicator_width,
drop_indicator_height);
gfx::Screen* screen = gfx::Screen::GetScreenFor(GetWidget()->GetNativeView());
gfx::Display display = screen->GetDisplayMatching(drop_bounds);
*is_beneath = !display.bounds().Contains(drop_bounds);
if (*is_beneath)
drop_bounds.Offset(0, drop_bounds.height() + height());
return drop_bounds;
}
void TabStrip::UpdateDropIndex(const DropTargetEvent& event) {
const int x = GetMirroredXInView(event.x());
for (int i = GetMiniTabCount(); i < tab_count(); ++i) {
Tab* tab = tab_at(i);
const int tab_max_x = tab->x() + tab->width();
const int hot_width = tab->width() / kTabEdgeRatioInverse;
if (x < tab_max_x) {
if (x < tab->x() + hot_width)
SetDropIndex(i, true);
else if (x >= tab_max_x - hot_width)
SetDropIndex(i + 1, true);
else
SetDropIndex(i, false);
return;
}
}
SetDropIndex(tab_count(), true);
}
void TabStrip::SetDropIndex(int tab_data_index, bool drop_before) {
controller()->OnDropIndexUpdate(tab_data_index, drop_before);
if (tab_data_index == -1) {
if (drop_info_.get())
drop_info_.reset(NULL);
return;
}
if (drop_info_.get() && drop_info_->drop_index == tab_data_index &&
drop_info_->drop_before == drop_before) {
return;
}
bool is_beneath;
gfx::Rect drop_bounds = GetDropBounds(tab_data_index, drop_before,
&is_beneath);
if (!drop_info_.get()) {
drop_info_.reset(
new DropInfo(tab_data_index, drop_before, !is_beneath, GetWidget()));
} else {
drop_info_->drop_index = tab_data_index;
drop_info_->drop_before = drop_before;
if (is_beneath == drop_info_->point_down) {
drop_info_->point_down = !is_beneath;
drop_info_->arrow_view->SetImage(
GetDropArrowImage(drop_info_->point_down));
}
}
drop_info_->arrow_window->SetBounds(drop_bounds);
drop_info_->arrow_window->Show();
}
int TabStrip::GetDropEffect(const ui::DropTargetEvent& event) {
const int source_ops = event.source_operations();
if (source_ops & ui::DragDropTypes::DRAG_COPY)
return ui::DragDropTypes::DRAG_COPY;
if (source_ops & ui::DragDropTypes::DRAG_LINK)
return ui::DragDropTypes::DRAG_LINK;
return ui::DragDropTypes::DRAG_MOVE;
}
gfx::ImageSkia* TabStrip::GetDropArrowImage(bool is_down) {
return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
is_down ? IDR_TAB_DROP_DOWN : IDR_TAB_DROP_UP);
}
TabStrip::DropInfo::DropInfo(int drop_index,
bool drop_before,
bool point_down,
views::Widget* context)
: drop_index(drop_index),
drop_before(drop_before),
point_down(point_down),
file_supported(true) {
arrow_view = new views::ImageView;
arrow_view->SetImage(GetDropArrowImage(point_down));
arrow_window = new views::Widget;
views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
params.keep_on_top = true;
params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
params.accept_events = false;
params.can_activate = false;
params.bounds = gfx::Rect(drop_indicator_width, drop_indicator_height);
params.context = context->GetNativeView();
arrow_window->Init(params);
arrow_window->SetContentsView(arrow_view);
}
TabStrip::DropInfo::~DropInfo() {
arrow_window->Close();
}
void TabStrip::PrepareForAnimation() {
if (!IsDragSessionActive() && !TabDragController::IsAttachedTo(this)) {
for (int i = 0; i < tab_count(); ++i)
tab_at(i)->set_dragging(false);
}
}
void TabStrip::GenerateIdealBounds() {
int new_tab_y = 0;
if (touch_layout_.get()) {
if (tabs_.view_size() == 0)
return;
int new_tab_x = tabs_.ideal_bounds(tabs_.view_size() - 1).right() +
newtab_button_h_offset();
newtab_button_bounds_.set_origin(gfx::Point(new_tab_x, new_tab_y));
return;
}
double unselected, selected;
GetDesiredTabWidths(tab_count(), GetMiniTabCount(), &unselected, &selected);
current_unselected_width_ = unselected;
current_selected_width_ = selected;
int tab_height = Tab::GetStandardSize().height();
int first_non_mini_index = 0;
double tab_x = GenerateIdealBoundsForMiniTabs(&first_non_mini_index);
for (int i = first_non_mini_index; i < tab_count(); ++i) {
Tab* tab = tab_at(i);
DCHECK(!tab->data().mini);
double tab_width = tab->IsActive() ? selected : unselected;
double end_of_tab = tab_x + tab_width;
int rounded_tab_x = Round(tab_x);
set_ideal_bounds(
i,
gfx::Rect(rounded_tab_x, 0, Round(end_of_tab) - rounded_tab_x,
tab_height));
tab_x = end_of_tab + tab_h_offset();
}
int new_tab_x;
if (abs(Round(unselected) - Tab::GetStandardSize().width()) > 1 &&
!in_tab_close_) {
new_tab_x = width() - newtab_button_bounds_.width();
} else {
new_tab_x = Round(tab_x - tab_h_offset()) + newtab_button_h_offset();
}
newtab_button_bounds_.set_origin(gfx::Point(new_tab_x, new_tab_y));
}
int TabStrip::GenerateIdealBoundsForMiniTabs(int* first_non_mini_index) {
int next_x = 0;
int mini_width = Tab::GetMiniWidth();
int tab_height = Tab::GetStandardSize().height();
int index = 0;
for (; index < tab_count() && tab_at(index)->data().mini; ++index) {
set_ideal_bounds(index,
gfx::Rect(next_x, 0, mini_width, tab_height));
next_x += mini_width + tab_h_offset();
}
if (index > 0 && index < tab_count())
next_x += kMiniToNonMiniGap;
if (first_non_mini_index)
*first_non_mini_index = index;
return next_x;
}
int TabStrip::new_tab_button_width() {
return newtab_button_asset_width() + newtab_button_h_offset();
}
int TabStrip::button_v_offset() {
return newtab_button_v_offset();
}
int TabStrip::tab_area_width() const {
return width() - new_tab_button_width();
}
void TabStrip::StartResizeLayoutAnimation() {
PrepareForAnimation();
GenerateIdealBounds();
AnimateToIdealBounds();
}
void TabStrip::StartMiniTabAnimation() {
in_tab_close_ = false;
available_width_for_tabs_ = -1;
PrepareForAnimation();
GenerateIdealBounds();
AnimateToIdealBounds();
}
void TabStrip::StartMouseInitiatedRemoveTabAnimation(int model_index) {
Tab* tab_closing = tab_at(model_index);
int delta = tab_closing->width() + tab_h_offset();
DCHECK_NE(model_index + 1, tab_count());
if (tab_closing->data().mini && model_index + 1 < tab_count() &&
!tab_at(model_index + 1)->data().mini) {
delta += kMiniToNonMiniGap;
}
for (int i = model_index + 1; i < tab_count(); ++i) {
gfx::Rect bounds = ideal_bounds(i);
bounds.set_x(bounds.x() - delta);
set_ideal_bounds(i, bounds);
}
newtab_button_bounds_.set_x(newtab_button_bounds_.x() - delta);
PrepareForAnimation();
tab_closing->set_closing(true);
RemoveTabFromViewModel(model_index);
AnimateToIdealBounds();
gfx::Rect tab_bounds = tab_closing->bounds();
tab_bounds.set_width(0);
bounds_animator_.AnimateViewTo(tab_closing, tab_bounds);
bounds_animator_.SetAnimationDelegate(
tab_closing,
new RemoveTabDelegate(this, tab_closing),
true);
}
bool TabStrip::IsPointInTab(Tab* tab,
const gfx::Point& point_in_tabstrip_coords) {
gfx::Point point_in_tab_coords(point_in_tabstrip_coords);
View::ConvertPointToTarget(this, tab, &point_in_tab_coords);
return tab->HitTestPoint(point_in_tab_coords);
}
int TabStrip::GetStartXForNormalTabs() const {
int mini_tab_count = GetMiniTabCount();
if (mini_tab_count == 0)
return 0;
return mini_tab_count * (Tab::GetMiniWidth() + tab_h_offset()) +
kMiniToNonMiniGap;
}
Tab* TabStrip::FindTabForEvent(const gfx::Point& point) {
if (touch_layout_.get()) {
int active_tab_index = touch_layout_->active_index();
if (active_tab_index != -1) {
Tab* tab = FindTabForEventFrom(point, active_tab_index, -1);
if (!tab)
tab = FindTabForEventFrom(point, active_tab_index + 1, 1);
return tab;
} else if (tab_count()) {
return FindTabForEventFrom(point, 0, 1);
}
} else {
for (int i = 0; i < tab_count(); ++i) {
if (IsPointInTab(tab_at(i), point))
return tab_at(i);
}
}
return NULL;
}
Tab* TabStrip::FindTabForEventFrom(const gfx::Point& point,
int start,
int delta) {
if (start == tab_count())
start += delta;
for (int i = start; i >= 0 && i < tab_count(); i += delta) {
if (IsPointInTab(tab_at(i), point))
return tab_at(i);
}
return NULL;
}
views::View* TabStrip::FindTabHitByPoint(const gfx::Point& point) {
for (int i = 0; i < tab_count(); ++i) {
Tab* next_tab = i < (tab_count() - 1) ? tab_at(i + 1) : NULL;
if (next_tab && next_tab->IsActive() && IsPointInTab(next_tab, point))
return next_tab;
if (IsPointInTab(tab_at(i), point))
return tab_at(i);
}
return NULL;
}
std::vector<int> TabStrip::GetTabXCoordinates() {
std::vector<int> results;
for (int i = 0; i < tab_count(); ++i)
results.push_back(ideal_bounds(i).x());
return results;
}
void TabStrip::SwapLayoutIfNecessary() {
bool needs_touch = NeedsTouchLayout();
bool using_touch = touch_layout_.get() != NULL;
if (needs_touch == using_touch)
return;
if (needs_touch) {
gfx::Size tab_size(Tab::GetMinimumSelectedSize());
tab_size.set_width(Tab::GetTouchWidth());
touch_layout_.reset(new StackedTabStripLayout(
tab_size,
tab_h_offset(),
kStackedPadding,
kMaxStackedCount,
&tabs_));
touch_layout_->SetWidth(width() - new_tab_button_width());
GenerateIdealBoundsForMiniTabs(NULL);
touch_layout_->SetXAndMiniCount(GetStartXForNormalTabs(),
GetMiniTabCount());
touch_layout_->SetActiveIndex(controller_->GetActiveIndex());
} else {
touch_layout_.reset();
}
PrepareForAnimation();
GenerateIdealBounds();
AnimateToIdealBounds();
}
bool TabStrip::NeedsTouchLayout() const {
if (layout_type_ == TAB_STRIP_LAYOUT_SHRINK)
return false;
int mini_tab_count = GetMiniTabCount();
int normal_count = tab_count() - mini_tab_count;
if (normal_count <= 1 || normal_count == mini_tab_count)
return false;
int x = GetStartXForNormalTabs();
int available_width = width() - x - new_tab_button_width();
return (Tab::GetTouchWidth() * normal_count +
tab_h_offset() * (normal_count - 1)) > available_width;
}
void TabStrip::SetResetToShrinkOnExit(bool value) {
if (!GetAdjustLayout())
return;
if (value && layout_type_ == TAB_STRIP_LAYOUT_SHRINK)
value = false;
if (value == reset_to_shrink_on_exit_)
return;
reset_to_shrink_on_exit_ = value;
if (reset_to_shrink_on_exit_)
AddMessageLoopObserver();
else
RemoveMessageLoopObserver();
}
bool TabStrip::GetAdjustLayout() const {
if (!adjust_layout_)
return false;
#if defined(USE_AURA)
return chrome::GetHostDesktopTypeForNativeView(
GetWidget()->GetNativeView()) == chrome::HOST_DESKTOP_TYPE_ASH;
#else
return ui::GetDisplayLayout() == ui::LAYOUT_TOUCH;
#endif
}