Commit 1723bf2a authored by Bo Liu's avatar Bo Liu Committed by Commit Bot

weblayer: Bottom control listen to resize

Bottom control positioning depends on knowing the surface size, but is
not informed of surface size changes.

This CL adds the bottom controls as a listener on ContentViewRenderView.
Also query java for the controls offset.

Bug: 1091717
Change-Id: I2c1ec6be7d1dd470e21590c66b09b4f99dbfe5c5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2232753Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Bo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#775796}
parent 6d927583
......@@ -5,6 +5,7 @@
#include "weblayer/browser/browser_controls_container_view.h"
#include "base/android/jni_string.h"
#include "base/bind.h"
#include "base/feature_list.h"
#include "cc/layers/ui_resource_layer.h"
#include "content/public/browser/android/compositor.h"
......@@ -33,9 +34,19 @@ BrowserControlsContainerView::BrowserControlsContainerView(
content_view_render_view_(content_view_render_view),
is_top_(is_top) {
DCHECK(content_view_render_view_);
if (!is_top_) {
content_view_render_view_->SetHeightChangedListener(
base::BindRepeating(&BrowserControlsContainerView::ContentHeightChanged,
base::Unretained(this)));
}
}
BrowserControlsContainerView::~BrowserControlsContainerView() = default;
BrowserControlsContainerView::~BrowserControlsContainerView() {
if (!is_top_) {
content_view_render_view_->SetHeightChangedListener(
base::RepeatingClosure());
}
}
int BrowserControlsContainerView::GetControlsHeight() {
return controls_layer_ ? controls_layer_->bounds().height() : 0;
......@@ -82,28 +93,20 @@ void BrowserControlsContainerView::DeleteControlsLayer(JNIEnv* env) {
}
void BrowserControlsContainerView::SetTopControlsOffset(JNIEnv* env,
int controls_offset_y,
int content_offset_y) {
DCHECK(is_top_);
// |controls_layer_| may not be created if the controls view has 0 height.
if (controls_layer_)
controls_layer_->SetPosition(gfx::PointF(0, controls_offset_y));
controls_layer_->SetPosition(gfx::PointF(0, GetControlsOffset()));
if (web_contents()) {
web_contents()->GetNativeView()->GetLayer()->SetPosition(
gfx::PointF(0, content_offset_y));
}
}
void BrowserControlsContainerView::SetBottomControlsOffset(
JNIEnv* env,
int controls_offset_y) {
void BrowserControlsContainerView::SetBottomControlsOffset(JNIEnv* env) {
DCHECK(!is_top_);
// |controls_layer_| may not be created if the controls view has 0 height.
if (controls_layer_) {
controls_layer_->SetPosition(
gfx::PointF(0, content_view_render_view_->height() -
GetControlsHeight() + controls_offset_y));
}
DoSetBottomControlsOffset();
}
void BrowserControlsContainerView::SetControlsSize(
......@@ -143,6 +146,26 @@ void BrowserControlsContainerView::DidToggleFullscreenModeForTab(
entered_fullscreen);
}
void BrowserControlsContainerView::ContentHeightChanged() {
DCHECK(!is_top_);
DoSetBottomControlsOffset();
}
int BrowserControlsContainerView::GetControlsOffset() {
return Java_BrowserControlsContainerView_getControlsOffset(
AttachCurrentThread(), java_browser_controls_container_view_);
}
void BrowserControlsContainerView::DoSetBottomControlsOffset() {
DCHECK(!is_top_);
// |controls_layer_| may not be created if the controls view has 0 height.
if (!controls_layer_)
return;
controls_layer_->SetPosition(
gfx::PointF(0, content_view_render_view_->height() - GetControlsHeight() +
GetControlsOffset()));
}
static jlong
JNI_BrowserControlsContainerView_CreateBrowserControlsContainerView(
JNIEnv* env,
......
......@@ -55,9 +55,8 @@ class BrowserControlsContainerView : public content::WebContentsObserver {
// Sets the offsets of the controls and content. See
// BrowserControlsContainerView's javadoc for details on this.
void SetTopControlsOffset(JNIEnv* env,
int controls_offset_y,
int content_offset_y);
void SetBottomControlsOffset(JNIEnv* env, int controls_offset_y);
void SetBottomControlsOffset(JNIEnv* env);
// Sets the size of |controls_layer_|.
void SetControlsSize(JNIEnv* env,
......@@ -75,6 +74,12 @@ class BrowserControlsContainerView : public content::WebContentsObserver {
void DidToggleFullscreenModeForTab(bool entered_fullscreen,
bool will_cause_resize) override;
// Only used for bottom controls.
void ContentHeightChanged();
int GetControlsOffset();
void DoSetBottomControlsOffset();
base::android::ScopedJavaGlobalRef<jobject>
java_browser_controls_container_view_;
ContentViewRenderView* content_view_render_view_;
......
......@@ -8,6 +8,7 @@
#include <android/native_window_jni.h>
#include <memory>
#include <utility>
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
......@@ -37,7 +38,15 @@ ContentViewRenderView::ContentViewRenderView(JNIEnv* env,
java_obj_.Reset(env, obj);
}
ContentViewRenderView::~ContentViewRenderView() = default;
ContentViewRenderView::~ContentViewRenderView() {
DCHECK(height_changed_listener_.is_null());
}
void ContentViewRenderView::SetHeightChangedListener(
base::RepeatingClosure callback) {
DCHECK(height_changed_listener_.is_null() || callback.is_null());
height_changed_listener_ = std::move(callback);
}
// static
static jlong JNI_ContentViewRenderView_Init(
......@@ -75,11 +84,15 @@ void ContentViewRenderView::OnPhysicalBackingSizeChanged(
const JavaParamRef<jobject>& jweb_contents,
jint width,
jint height) {
bool height_changed = height_ != height;
height_ = height;
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(jweb_contents);
gfx::Size size(width, height);
web_contents->GetNativeView()->OnPhysicalBackingSizeChanged(size);
if (height_changed && !height_changed_listener_.is_null())
height_changed_listener_.Run();
}
void ContentViewRenderView::SurfaceCreated(JNIEnv* env) {
......
......@@ -8,6 +8,7 @@
#include <memory>
#include "base/android/jni_weak_ref.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
......@@ -38,6 +39,7 @@ class ContentViewRenderView : public content::CompositorClient {
// Height, in pixels.
int height() const { return height_; }
void SetHeightChangedListener(base::RepeatingClosure callback);
// Methods called from Java via JNI -----------------------------------------
void Destroy(JNIEnv* env);
......@@ -83,6 +85,7 @@ class ContentViewRenderView : public content::CompositorClient {
int current_surface_format_ = 0;
base::RepeatingClosure height_changed_listener_;
int height_ = 0;
DISALLOW_COPY_AND_ASSIGN(ContentViewRenderView);
......
......@@ -371,10 +371,10 @@ class BrowserControlsContainerView extends FrameLayout {
}
if (mIsTop) {
BrowserControlsContainerViewJni.get().setTopControlsOffset(
mNativeBrowserControlsContainerView, mControlsOffset, mContentOffset);
mNativeBrowserControlsContainerView, mContentOffset);
} else {
BrowserControlsContainerViewJni.get().setBottomControlsOffset(
mNativeBrowserControlsContainerView, mControlsOffset);
mNativeBrowserControlsContainerView);
}
}
......@@ -395,6 +395,11 @@ class BrowserControlsContainerView extends FrameLayout {
if (mView != null) mView.setVisibility(View.VISIBLE);
}
@CalledByNative
private int getControlsOffset() {
return mControlsOffset;
}
@CalledByNative
private void didToggleFullscreenModeForTab(final boolean isFullscreen) {
// Delay hiding until after the animation. This comes from Chrome code.
......@@ -435,9 +440,8 @@ class BrowserControlsContainerView extends FrameLayout {
void deleteBrowserControlsContainerView(long nativeBrowserControlsContainerView);
void createControlsLayer(long nativeBrowserControlsContainerView, int id);
void deleteControlsLayer(long nativeBrowserControlsContainerView);
void setTopControlsOffset(
long nativeBrowserControlsContainerView, int controlsOffsetY, int contentOffsetY);
void setBottomControlsOffset(long nativeBrowserControlsContainerView, int controlsOffsetY);
void setTopControlsOffset(long nativeBrowserControlsContainerView, int contentOffsetY);
void setBottomControlsOffset(long nativeBrowserControlsContainerView);
void setControlsSize(long nativeBrowserControlsContainerView, int width, int height);
void updateControlsResource(long nativeBrowserControlsContainerView);
void setWebContents(long nativeBrowserControlsContainerView, WebContents webContents);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment