OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.content.browser.input; | 5 package org.chromium.content.browser.input; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.res.TypedArray; | 8 import android.content.res.TypedArray; |
9 import android.util.TypedValue; | 9 import android.util.TypedValue; |
10 import android.view.Gravity; | 10 import android.view.Gravity; |
11 import android.view.LayoutInflater; | 11 import android.view.LayoutInflater; |
12 import android.view.View; | 12 import android.view.View; |
13 import android.view.View.OnClickListener; | 13 import android.view.View.OnClickListener; |
14 import android.view.ViewGroup; | 14 import android.view.ViewGroup; |
15 import android.view.ViewGroup.LayoutParams; | 15 import android.view.ViewGroup.LayoutParams; |
16 import android.widget.PopupWindow; | 16 import android.widget.PopupWindow; |
17 | 17 |
18 /** | 18 /** |
19 * Paste popup implementation based on TextView.PastePopupMenu. | 19 * Paste popup implementation based on TextView.PastePopupMenu. |
20 */ | 20 */ |
21 public class PastePopupMenu implements OnClickListener { | 21 public class PastePopupMenu implements OnClickListener { |
22 private final View mParent; | 22 private final View mParent; |
23 private final PastePopupMenuDelegate mDelegate; | 23 private final PastePopupMenuDelegate mDelegate; |
24 private final Context mContext; | 24 private final Context mContext; |
25 private final PopupWindow mContainer; | 25 private final PopupWindow mContainer; |
26 private int mRawPositionX; | 26 private int mRawPositionX; |
27 private int mRawPositionY; | 27 private int mRawPositionY; |
28 private int mPositionX; | 28 private int mPositionX; |
29 private int mPositionY; | 29 private int mPositionY; |
30 private int mStatusBarHeight; | |
30 private final View[] mPasteViews; | 31 private final View[] mPasteViews; |
31 private final int[] mPasteViewLayouts; | 32 private final int[] mPasteViewLayouts; |
32 private final int mLineOffsetY; | 33 private final int mLineOffsetY; |
33 private final int mWidthOffsetX; | 34 private final int mWidthOffsetX; |
34 | 35 |
35 /** | 36 /** |
36 * Provider of paste functionality for the given popup. | 37 * Provider of paste functionality for the given popup. |
37 */ | 38 */ |
38 public interface PastePopupMenuDelegate { | 39 public interface PastePopupMenuDelegate { |
39 /** | 40 /** |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 for (int i = 0; i < attrs.length(); ++i) { | 75 for (int i = 0; i < attrs.length(); ++i) { |
75 mPasteViewLayouts[i] = attrs.getResourceId(attrs.getIndex(i), 0); | 76 mPasteViewLayouts[i] = attrs.getResourceId(attrs.getIndex(i), 0); |
76 } | 77 } |
77 attrs.recycle(); | 78 attrs.recycle(); |
78 | 79 |
79 // Convert line offset dips to pixels. | 80 // Convert line offset dips to pixels. |
80 mLineOffsetY = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_D IP, | 81 mLineOffsetY = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_D IP, |
81 5.0f, mContext.getResources().getDisplayMetrics()); | 82 5.0f, mContext.getResources().getDisplayMetrics()); |
82 mWidthOffsetX = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_ DIP, | 83 mWidthOffsetX = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_ DIP, |
83 30.0f, mContext.getResources().getDisplayMetrics()); | 84 30.0f, mContext.getResources().getDisplayMetrics()); |
85 mStatusBarHeight = 0; | |
jdduke (slow)
2014/07/30 15:24:16
Nit: No need to initialize to 0, it will always be
kingshuk.j
2014/07/31 04:40:04
Done.
| |
86 int resourceId = mContext.getResources() | |
jdduke (slow)
2014/07/30 15:24:16
Can we call this statusBarHeightResourceId?
kingshuk.j
2014/07/31 04:40:04
Done.
| |
87 .getIdentifier("status_bar_height", "dimen", "android"); | |
88 if (resourceId > 0) { | |
89 mStatusBarHeight = mContext.getResources() | |
90 .getDimensionPixelSize(resourceId); | |
91 } | |
84 } | 92 } |
85 | 93 |
86 /** | 94 /** |
87 * Shows the paste popup at an appropriate location relative to the specifie d position. | 95 * Shows the paste popup at an appropriate location relative to the specifie d position. |
88 */ | 96 */ |
89 public void showAt(int x, int y) { | 97 public void showAt(int x, int y) { |
90 if (!canPaste()) return; | 98 if (!canPaste()) return; |
91 updateContent(true); | 99 updateContent(true); |
92 positionAt(x, y); | 100 positionAt(x, y); |
93 } | 101 } |
(...skipping 30 matching lines...) Expand all Loading... | |
124 int height = contentView.getMeasuredHeight(); | 132 int height = contentView.getMeasuredHeight(); |
125 | 133 |
126 mPositionX = (int) (x - width / 2.0f); | 134 mPositionX = (int) (x - width / 2.0f); |
127 mPositionY = y - height - mLineOffsetY; | 135 mPositionY = y - height - mLineOffsetY; |
128 | 136 |
129 final int[] coords = new int[2]; | 137 final int[] coords = new int[2]; |
130 mParent.getLocationInWindow(coords); | 138 mParent.getLocationInWindow(coords); |
131 coords[0] += mPositionX; | 139 coords[0] += mPositionX; |
132 coords[1] += mPositionY; | 140 coords[1] += mPositionY; |
133 | 141 |
142 int statusBarHeight = 0; | |
jdduke (slow)
2014/07/30 15:24:16
Let's rename this to something like |minOffsetY|.
kingshuk.j
2014/07/31 04:40:04
Done.
| |
143 if (mParent.getSystemUiVisibility() == View.SYSTEM_UI_FLAG_VISIBLE) { | |
144 statusBarHeight = mStatusBarHeight; | |
145 } | |
146 | |
134 final int screenWidth = mContext.getResources().getDisplayMetrics().widt hPixels; | 147 final int screenWidth = mContext.getResources().getDisplayMetrics().widt hPixels; |
135 if (coords[1] < 0) { | 148 if (coords[1] < statusBarHeight) { |
136 updateContent(false); | 149 updateContent(false); |
137 // Update dimensions from new view | 150 // Update dimensions from new view |
138 contentView = mContainer.getContentView(); | 151 contentView = mContainer.getContentView(); |
139 width = contentView.getMeasuredWidth(); | 152 width = contentView.getMeasuredWidth(); |
140 height = contentView.getMeasuredHeight(); | 153 height = contentView.getMeasuredHeight(); |
141 | 154 |
142 // Vertical clipping, move under edited line and to the side of inse rtion cursor | 155 // Vertical clipping, move under edited line and to the side of inse rtion cursor |
143 // TODO bottom clipping in case there is no system bar | 156 // TODO bottom clipping in case there is no system bar |
144 coords[1] += height; | 157 coords[1] += height; |
145 coords[1] += mLineOffsetY; | 158 coords[1] += mLineOffsetY; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 } | 208 } |
196 | 209 |
197 private boolean canPaste() { | 210 private boolean canPaste() { |
198 return mDelegate.canPaste(); | 211 return mDelegate.canPaste(); |
199 } | 212 } |
200 | 213 |
201 private void paste() { | 214 private void paste() { |
202 mDelegate.paste(); | 215 mDelegate.paste(); |
203 } | 216 } |
204 } | 217 } |
OLD | NEW |