Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Unified Diff: ui/android/java/src/org/chromium/ui/ColorPickerSimple.java

Issue 23026006: Add support for color input datalist on Android (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/android/java/src/org/chromium/ui/ColorPickerSimple.java
diff --git a/ui/android/java/src/org/chromium/ui/ColorPickerSimple.java b/ui/android/java/src/org/chromium/ui/ColorPickerSimple.java
index 979405a4078fce54e3cd606e6d6987acaaba8c4d..1644ba86dab4da6be77da0c0819c82523e771f11 100644
--- a/ui/android/java/src/org/chromium/ui/ColorPickerSimple.java
+++ b/ui/android/java/src/org/chromium/ui/ColorPickerSimple.java
@@ -11,40 +11,42 @@ import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ListView;
+import android.widget.AdapterView;
+import org.chromium.ui.ColorSuggestionListAdapter.OnColorSuggestionClickListener;
+
+import java.util.ArrayList;
+import java.util.Arrays;
/**
* Draws a grid of (predefined) colors and allows the user to choose one of
* those colors.
*/
-public class ColorPickerSimple extends View {
- private static final int ROW_COUNT = 2;
-
- private static final int COLUMN_COUNT = 4;
-
- private static final int GRID_CELL_COUNT = ROW_COUNT * COLUMN_COUNT;
-
- private static final int[] COLORS = { Color.RED,
- Color.CYAN,
- Color.BLUE,
- Color.GREEN,
- Color.MAGENTA,
- Color.YELLOW,
- Color.BLACK,
- Color.WHITE
- };
-
- private Paint mBorderPaint;
-
- private Rect[] mBounds;
-
- private Paint[] mPaints;
-
- private OnColorChangedListener mOnColorTouchedListener;
-
- private int mLastTouchedXPosition;
-
- private int mLastTouchedYPosition;
+public class ColorPickerSimple extends ListView implements OnColorSuggestionClickListener {
+
+ private OnColorChangedListener mOnColorChangedListener;
+
+ private static final int[] DEFAULT_COLORS = { Color.RED,
+ Color.CYAN,
+ Color.BLUE,
+ Color.GREEN,
+ Color.MAGENTA,
+ Color.YELLOW,
+ Color.BLACK,
+ Color.WHITE
+ };
+
+ private static final int[] DEFAULT_COLOR_LABEL_IDS = { R.string.color_picker_button_red,
+ R.string.color_picker_button_cyan,
+ R.string.color_picker_button_blue,
+ R.string.color_picker_button_green,
+ R.string.color_picker_button_magenta,
+ R.string.color_picker_button_yellow,
+ R.string.color_picker_button_black,
+ R.string.color_picker_button_white
+ };
public ColorPickerSimple(Context context) {
super(context);
@@ -59,131 +61,35 @@ public class ColorPickerSimple extends View {
}
/**
- * Initializes the listener and precalculates the grid and color positions.
+ * Initializes the listener and sets the adapter for the given list of suggestions. If the
+ * suggestions is null a default set of colors will be used.
*
+ * @param suggestions The list of suggestions that should be displayed.
* @param onColorChangedListener The listener that gets notified when the user touches
* a color.
*/
- public void init(OnColorChangedListener onColorChangedListener) {
- mOnColorTouchedListener = onColorChangedListener;
-
- // This will get calculated when the layout size is updated.
- mBounds = null;
-
- mPaints = new Paint[GRID_CELL_COUNT];
- for (int i = 0; i < GRID_CELL_COUNT; ++i) {
- Paint newPaint = new Paint();
- newPaint.setColor(COLORS[i]);
- mPaints[i] = newPaint;
- }
-
- mBorderPaint = new Paint();
- int borderColor = getContext().getResources().getColor(R.color.color_picker_border_color);
- mBorderPaint.setColor(borderColor);
-
- // Responds to the user touching the grid and works out which color has been chosen as
- // a result, depending on the X,Y coordinate. Note that we respond to the click event
- // here, but the onClick() method doesn't provide us with the X,Y coordinates, so we
- // track them in onTouchEvent() below. This way the grid reacts properly to touch events
- // whereas if we put this onClick() code in onTouchEvent below then we get some strange
- // interactions with the ScrollView in the parent ColorPickerDialog.
- setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if (mOnColorTouchedListener != null && getWidth() > 0 && getHeight() > 0) {
- int column = mLastTouchedXPosition * COLUMN_COUNT / getWidth();
- int row = mLastTouchedYPosition * ROW_COUNT / getHeight();
-
- int colorIndex = (row * COLUMN_COUNT) + column;
- if (colorIndex >= 0 && colorIndex < COLORS.length) {
- mOnColorTouchedListener.onColorChanged(COLORS[colorIndex]);
- }
- }
+ public void init(ColorSuggestion[] suggestions,
+ OnColorChangedListener onColorChangedListener) {
+ mOnColorChangedListener = onColorChangedListener;
+
+ if (suggestions == null) {
+ suggestions = new ColorSuggestion[DEFAULT_COLORS.length];
+ for (int i = 0; i < suggestions.length; ++i) {
+ suggestions[i] =
+ new ColorSuggestion(
+ DEFAULT_COLORS[i],
+ getContext().getString(DEFAULT_COLOR_LABEL_IDS[i]));
}
- });
- }
-
- /**
- * Draws the grid of colors, based on the rectangles calculated in onSizeChanged().
- * Also draws borders in between the colored rectangles.
- *
- * @param canvas The canvas the colors are drawn onto.
- */
- @Override
- public void onDraw(Canvas canvas) {
- if (mBounds == null || mPaints == null) {
- return;
- }
-
- canvas.drawColor(Color.WHITE);
-
- // Draw the actual colored rectangles.
- for (int i = 0; i < GRID_CELL_COUNT; ++i) {
- canvas.drawRect(mBounds[i], mPaints[i]);
- }
-
- // Draw 1px borders between the rows.
- for (int i = 0; i < ROW_COUNT - 1; ++i) {
- canvas.drawLine(0,
- mBounds[i * COLUMN_COUNT].bottom + 1,
- getWidth(),
- mBounds[i * COLUMN_COUNT].bottom + 1,
- mBorderPaint);
}
- // Draw 1px borders between the columns.
- for (int j = 0; j < COLUMN_COUNT - 1; ++j) {
- canvas.drawLine(mBounds[j].right + 1,
- 0,
- mBounds[j].right + 1,
- getHeight(),
- mBorderPaint);
- }
+ ColorSuggestionListAdapter adapter = new ColorSuggestionListAdapter(getContext(),
+ suggestions);
+ adapter.setOnColorSuggestionClickListener(this);
+ setAdapter(adapter);
}
- /**
- * Stores the X,Y coordinates of the touch so that we can use them in the onClick() listener
- * above to work out where the click was on the grid.
- *
- * @param event The MotionEvent the X,Y coordinates are retrieved from.
- */
@Override
- public boolean onTouchEvent(MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- mLastTouchedXPosition = (int) event.getX();
- mLastTouchedYPosition = (int) event.getY();
- }
- return super.onTouchEvent(event);
- }
-
- /**
- * Recalculates the color grid with the new sizes.
- */
- @Override
- protected void onSizeChanged(int width, int height, int oldw, int oldh) {
- calculateGrid(width, height);
- }
-
- /**
- * Calculates the sizes and positions of the cells in the grid, splitting
- * them up as evenly as possible. Leaves 3 pixels between each cell so that
- * we can draw a border between them as well, and leaves a pixel around the
- * edge.
- */
- private void calculateGrid(final int width, final int height) {
- mBounds = new Rect[GRID_CELL_COUNT];
-
- for (int i = 0; i < ROW_COUNT; ++i) {
- for (int j = 0; j < COLUMN_COUNT; ++j) {
- int left = j * (width + 1) / COLUMN_COUNT + 1;
- int right = (j + 1) * (width + 1) / COLUMN_COUNT - 2;
-
- int top = i * (height + 1) / ROW_COUNT + 1;
- int bottom = (i + 1) * (height + 1) / ROW_COUNT - 2;
-
- Rect rect = new Rect(left, top, right, bottom);
- mBounds[(i * COLUMN_COUNT) + j] = rect;
- }
- }
+ public void onColorSuggestionClick(ColorSuggestion suggestion) {
+ mOnColorChangedListener.onColorChanged(suggestion.mColor);
}
}
« no previous file with comments | « ui/android/java/src/org/chromium/ui/ColorPickerDialog.java ('k') | ui/android/java/src/org/chromium/ui/ColorSuggestion.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698