michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.home; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.TypedArray; michael@0: import android.graphics.Canvas; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.util.AttributeSet; michael@0: import android.view.LayoutInflater; michael@0: import android.view.View; michael@0: import android.view.ViewTreeObserver; michael@0: import android.view.accessibility.AccessibilityEvent; michael@0: import android.widget.HorizontalScrollView; michael@0: import android.widget.LinearLayout; michael@0: import android.widget.TextView; michael@0: michael@0: /** michael@0: * {@code TabMenuStrip} is the view used to display {@code HomePager} tabs michael@0: * on tablets. See {@code TabMenuStripLayout} for details about how the michael@0: * tabs are created and updated. michael@0: */ michael@0: public class TabMenuStrip extends HorizontalScrollView michael@0: implements HomePager.Decor { michael@0: michael@0: // Offset between the selected tab title and the edge of the screen, michael@0: // except for the first and last tab in the tab strip. michael@0: private static final int TITLE_OFFSET_DIPS = 24; michael@0: michael@0: private final int titleOffset; michael@0: private final TabMenuStripLayout layout; michael@0: michael@0: public TabMenuStrip(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: michael@0: // Disable the scroll bar. michael@0: setHorizontalScrollBarEnabled(false); michael@0: michael@0: titleOffset = (int) (TITLE_OFFSET_DIPS * getResources().getDisplayMetrics().density); michael@0: michael@0: layout = new TabMenuStripLayout(context, attrs); michael@0: addView(layout, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); michael@0: } michael@0: michael@0: @Override michael@0: public void onAddPagerView(String title) { michael@0: layout.onAddPagerView(title); michael@0: } michael@0: michael@0: @Override michael@0: public void removeAllPagerViews() { michael@0: layout.removeAllViews(); michael@0: } michael@0: michael@0: @Override michael@0: public void onPageSelected(final int position) { michael@0: layout.onPageSelected(position); michael@0: } michael@0: michael@0: @Override michael@0: public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { michael@0: layout.onPageScrolled(position, positionOffset, positionOffsetPixels); michael@0: michael@0: final View selectedTitle = layout.getChildAt(position); michael@0: if (selectedTitle == null) { michael@0: return; michael@0: } michael@0: michael@0: final int selectedTitleOffset = (int) (positionOffset * selectedTitle.getWidth()); michael@0: michael@0: int titleLeft = selectedTitle.getLeft() + selectedTitleOffset; michael@0: if (position > 0) { michael@0: titleLeft -= titleOffset; michael@0: } michael@0: michael@0: int titleRight = selectedTitle.getRight() + selectedTitleOffset; michael@0: if (position < layout.getChildCount() - 1) { michael@0: titleRight += titleOffset; michael@0: } michael@0: michael@0: final int scrollX = getScrollX(); michael@0: if (titleLeft < scrollX) { michael@0: // Tab strip overflows to the left. michael@0: scrollTo(titleLeft, 0); michael@0: } else if (titleRight > scrollX + getWidth()) { michael@0: // Tab strip overflows to the right. michael@0: scrollTo(titleRight - getWidth(), 0); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void setOnTitleClickListener(HomePager.OnTitleClickListener onTitleClickListener) { michael@0: layout.setOnTitleClickListener(onTitleClickListener); michael@0: } michael@0: }