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

Unified Diff: content/public/android/java/src/org/chromium/content/browser/ContentViewAndroidDelegate.java

Issue 2103243002: Factor out ContentViewAndroidDelegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 4 years, 5 months 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: content/public/android/java/src/org/chromium/content/browser/ContentViewAndroidDelegate.java
diff --git a/content/public/android/java/src/org/chromium/content/browser/ContentViewAndroidDelegate.java b/content/public/android/java/src/org/chromium/content/browser/ContentViewAndroidDelegate.java
new file mode 100644
index 0000000000000000000000000000000000000000..49bf1250fbf6d2ac21f8098b4ec69173f28b5126
--- /dev/null
+++ b/content/public/android/java/src/org/chromium/content/browser/ContentViewAndroidDelegate.java
@@ -0,0 +1,99 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.content.browser;
+
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.FrameLayout.LayoutParams;
+
+import org.chromium.base.ApiCompatibilityUtils;
+import org.chromium.base.VisibleForTesting;
+import org.chromium.ui.base.ViewAndroidDelegate;
+import org.chromium.ui.gfx.DeviceDisplayInfo;
+
+/**
+ * A {@link ViewAndroidDelegate} that delegates to the current container view.
+ *
+ * <p>This delegate handles the replacement of container views transparently so
+ * that clients can safely hold to instances of this class.
+ */
+public class ContentViewAndroidDelegate implements ViewAndroidDelegate {
+ private static final String TAG = "cr.ContentViewAndroidDelegate";
+
+ /**
+ * Represents the position of an anchor view.
+ */
+ @VisibleForTesting
+ protected static class Position {
+ public final float mX;
+ public final float mY;
+ public final float mWidth;
+ public final float mHeight;
+
+ public Position(float x, float y, float width, float height) {
+ mX = x;
+ mY = y;
+ mWidth = width;
+ mHeight = height;
+ }
+ }
+
+ protected final RenderCoordinates mRenderCoordinates;
no sievers 2016/07/12 21:57:27 Looks like this is the only content dependency. If
Jinsuk Kim 2016/07/14 07:51:52 Moved to native ViewAndroid.
+ private final ViewGroup mContainerView;
+
+ public ContentViewAndroidDelegate(ViewGroup containerView, RenderCoordinates rc) {
+ mContainerView = containerView;
+ mRenderCoordinates = rc;
+ }
+
+ @Override
+ public void addView(View anchorView, float x, float y, float width, float height) {
+ mContainerView.addView(anchorView);
+ setAnchorViewPosition(mContainerView, anchorView, x, y, width, height);
+ }
+
+ protected void setAnchorViewPosition(ViewGroup containerView, View view, float x, float y,
+ float width, float height) {
+ if (view.getParent() == null || width == 0.f || height == 0.f) {
+ // Ignore. setAnchorViewPosition has been called after the anchor view has
+ // already been released.
+ return;
+ }
+ assert view.getParent() == containerView;
+
+ float scale =
+ (float) DeviceDisplayInfo.create(containerView.getContext()).getDIPScale();
+
+ // The anchor view should not go outside the bounds of the ContainerView.
+ int leftMargin = Math.round(x * scale);
+ int topMargin = Math.round(mRenderCoordinates.getContentOffsetYPix() + y * scale);
+ int scaledWidth = Math.round(width * scale);
+ setAnchorViewLayoutParams(containerView, view, x, y, width, height, scale,
+ leftMargin, topMargin, scaledWidth);
+ }
+
+ protected void setAnchorViewLayoutParams(ViewGroup containerView, View view, float x, float y,
+ float width, float height, float scale, int leftMargin, int topMargin,
+ int scaledWidth) {
+ int startMargin;
+ if (ApiCompatibilityUtils.isLayoutRtl(containerView)) {
+ startMargin = containerView.getMeasuredWidth() - Math.round((width + x) * scale);
+ } else {
+ startMargin = leftMargin;
+ }
+ if (scaledWidth + startMargin > containerView.getWidth()) {
+ scaledWidth = containerView.getWidth() - startMargin;
+ }
+ LayoutParams lp = new LayoutParams(scaledWidth, Math.round(height * scale));
+ ApiCompatibilityUtils.setMarginStart(lp, startMargin);
+ lp.topMargin = topMargin;
+ view.setLayoutParams(lp);
+ }
+
+ @Override
+ public void releaseAnchorView(View anchorView) {
+ mContainerView.removeView(anchorView);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698