OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.widget; | 5 package org.chromium.chrome.browser.widget; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.res.Resources; | 8 import android.content.res.Resources; |
9 import android.support.design.internal.BottomNavigationItemView; | 9 import android.support.design.internal.BottomNavigationItemView; |
10 import android.support.design.internal.BottomNavigationMenuView; | 10 import android.support.design.internal.BottomNavigationMenuView; |
(...skipping 21 matching lines...) Expand all Loading... |
32 /** | 32 /** |
33 * Displays and controls a {@link BottomNavigationView} fixed to the bottom of t
he | 33 * Displays and controls a {@link BottomNavigationView} fixed to the bottom of t
he |
34 * {@link BottomSheet}. Also manages {@link BottomSheetContent} displayed in the
BottomSheet. | 34 * {@link BottomSheet}. Also manages {@link BottomSheetContent} displayed in the
BottomSheet. |
35 */ | 35 */ |
36 public class BottomSheetContentController extends BottomNavigationView | 36 public class BottomSheetContentController extends BottomNavigationView |
37 implements BottomSheetObserver, OnNavigationItemSelectedListener { | 37 implements BottomSheetObserver, OnNavigationItemSelectedListener { |
38 private BottomSheet mBottomSheet; | 38 private BottomSheet mBottomSheet; |
39 private TabModelSelector mTabModelSelector; | 39 private TabModelSelector mTabModelSelector; |
40 private float mDistanceBelowToolbarPx; | 40 private float mDistanceBelowToolbarPx; |
41 private int mSelectedItemId; | 41 private int mSelectedItemId; |
| 42 private boolean mDefaultContentInitialized; |
42 | 43 |
43 private final Map<Integer, BottomSheetContent> mBottomSheetContents = new Ha
shMap<>(); | 44 private final Map<Integer, BottomSheetContent> mBottomSheetContents = new Ha
shMap<>(); |
44 | 45 |
45 public BottomSheetContentController(Context context, AttributeSet atts) { | 46 public BottomSheetContentController(Context context, AttributeSet atts) { |
46 super(context, atts); | 47 super(context, atts); |
47 } | 48 } |
48 | 49 |
49 /** | 50 /** |
50 * Initializes the {@link BottomSheetContentController}. | 51 * Initializes the {@link BottomSheetContentController}. |
51 * @param bottomSheet The {@link BottomSheet} associated with this bottom na
v. | 52 * @param bottomSheet The {@link BottomSheet} associated with this bottom na
v. |
(...skipping 11 matching lines...) Expand all Loading... |
63 + res.getDimensionPixelOffset(R.dimen.bottom_nav_space_from_tool
bar); | 64 + res.getDimensionPixelOffset(R.dimen.bottom_nav_space_from_tool
bar); |
64 | 65 |
65 setOnNavigationItemSelectedListener(this); | 66 setOnNavigationItemSelectedListener(this); |
66 disableShiftingMode(); | 67 disableShiftingMode(); |
67 } | 68 } |
68 | 69 |
69 /** | 70 /** |
70 * Initialize the default {@link BottomSheetContent}. | 71 * Initialize the default {@link BottomSheetContent}. |
71 */ | 72 */ |
72 public void initializeDefaultContent() { | 73 public void initializeDefaultContent() { |
73 mBottomSheet.showContent(getSheetContentForId(R.id.action_home)); | 74 if (mDefaultContentInitialized) return; |
74 mSelectedItemId = R.id.action_home; | 75 showBottomSheetContent(R.id.action_home); |
| 76 mDefaultContentInitialized = true; |
75 } | 77 } |
76 | 78 |
77 @Override | 79 @Override |
78 public boolean onNavigationItemSelected(MenuItem item) { | 80 public boolean onNavigationItemSelected(MenuItem item) { |
79 if (mSelectedItemId == item.getItemId()) return false; | 81 if (mSelectedItemId == item.getItemId()) return false; |
80 | 82 |
81 showBottomSheetContent(item.getItemId()); | 83 showBottomSheetContent(item.getItemId()); |
82 return true; | 84 return true; |
83 } | 85 } |
84 | 86 |
85 @Override | 87 @Override |
86 public void onTransitionPeekToHalf(float transitionFraction) { | 88 public void onTransitionPeekToHalf(float transitionFraction) { |
87 float offsetY = (mBottomSheet.getMinOffset() - mBottomSheet.getSheetOffs
etFromBottom()) | 89 float offsetY = (mBottomSheet.getMinOffset() - mBottomSheet.getSheetOffs
etFromBottom()) |
88 + mDistanceBelowToolbarPx; | 90 + mDistanceBelowToolbarPx; |
89 setTranslationY((int) Math.max(offsetY, 0f)); | 91 setTranslationY((int) Math.max(offsetY, 0f)); |
90 setVisibility(MathUtils.areFloatsEqual(transitionFraction, 0f) ? View.GO
NE : View.VISIBLE); | 92 setVisibility(MathUtils.areFloatsEqual(transitionFraction, 0f) ? View.GO
NE : View.VISIBLE); |
91 } | 93 } |
92 | 94 |
93 @Override | 95 @Override |
94 public void onSheetOpened() {} | 96 public void onSheetOpened() { |
| 97 if (!mDefaultContentInitialized && mTabModelSelector.getCurrentTab() !=
null) { |
| 98 initializeDefaultContent(); |
| 99 } |
| 100 } |
95 | 101 |
96 @Override | 102 @Override |
97 public void onSheetClosed() { | 103 public void onSheetClosed() { |
98 Iterator<Entry<Integer, BottomSheetContent>> contentIterator = | 104 Iterator<Entry<Integer, BottomSheetContent>> contentIterator = |
99 mBottomSheetContents.entrySet().iterator(); | 105 mBottomSheetContents.entrySet().iterator(); |
100 while (contentIterator.hasNext()) { | 106 while (contentIterator.hasNext()) { |
101 Entry<Integer, BottomSheetContent> entry = contentIterator.next(); | 107 Entry<Integer, BottomSheetContent> entry = contentIterator.next(); |
102 if (entry.getKey() == R.id.action_home) continue; | 108 if (entry.getKey() == R.id.action_home) continue; |
103 | 109 |
104 entry.getValue().destroy(); | 110 entry.getValue().destroy(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 // There are some bugs related to programatically selecting menu items t
hat are fixed in | 168 // There are some bugs related to programatically selecting menu items t
hat are fixed in |
163 // newer support library versions. | 169 // newer support library versions. |
164 // TODO(twellington): remove this after the support library is rolled. | 170 // TODO(twellington): remove this after the support library is rolled. |
165 if (mSelectedItemId != 0) getMenu().findItem(mSelectedItemId).setChecked
(false); | 171 if (mSelectedItemId != 0) getMenu().findItem(mSelectedItemId).setChecked
(false); |
166 mSelectedItemId = navItemId; | 172 mSelectedItemId = navItemId; |
167 getMenu().findItem(mSelectedItemId).setChecked(true); | 173 getMenu().findItem(mSelectedItemId).setChecked(true); |
168 | 174 |
169 mBottomSheet.showContent(getSheetContentForId(mSelectedItemId)); | 175 mBottomSheet.showContent(getSheetContentForId(mSelectedItemId)); |
170 } | 176 } |
171 } | 177 } |
OLD | NEW |