From 8031ab6bd73bbdb1d59a5eeef898317ec23ebe3f Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 6 Jul 2015 10:56:02 -0700 Subject: [PATCH] Add padding values to View.idl Rather than hardcoding the size and presence of the notification area in Dart, we now expose padding values on the view. These values are set to non-zero values when there are UI elements that overlap the view. We currently respect only the top padding, but this CL paves the way to respect padding in other directions. We still hardcode the size of the notification area in Java. A future CL will retrieve this value from the Android framework. Fixes #257 R=ianh@google.com Review URL: https://codereview.chromium.org/1220353002. --- engine/core/view/View.h | 6 ++++ engine/core/view/View.idl | 5 +++ engine/public/platform/sky_display_metrics.h | 4 +++ sdk/lib/theme/view_configuration.dart | 1 - sdk/lib/widgets/scaffold.dart | 4 ++- services/engine/sky_engine.mojom | 12 ++++++- .../sky/shell/PlatformViewAndroid.java | 27 +++++++++++++-- .../org/domokit/sky/shell/SkyActivity.java | 6 +++- shell/ios/sky_surface.mm | 7 ++-- shell/testing/test_runner.cc | 5 ++- shell/ui/engine.cc | 33 +++++++++---------- shell/ui/engine.h | 7 ++-- 12 files changed, 85 insertions(+), 32 deletions(-) diff --git a/engine/core/view/View.h b/engine/core/view/View.h index b5a5599fb..71147f4b9 100644 --- a/engine/core/view/View.h +++ b/engine/core/view/View.h @@ -25,6 +25,12 @@ public: static PassRefPtr create(const base::Closure& scheduleFrameCallback); double devicePixelRatio() const { return m_displayMetrics.device_pixel_ratio; } + + double paddingTop() const { return m_displayMetrics.padding_top; } + double paddingRight() const { return m_displayMetrics.padding_right; } + double paddingBottom() const { return m_displayMetrics.padding_bottom; } + double paddingLeft() const { return m_displayMetrics.padding_left; } + double width() const; double height() const; diff --git a/engine/core/view/View.idl b/engine/core/view/View.idl index 97f075fae..2fb0301a3 100644 --- a/engine/core/view/View.idl +++ b/engine/core/view/View.idl @@ -5,6 +5,11 @@ interface View { readonly attribute double devicePixelRatio; + readonly attribute double paddingTop; + readonly attribute double paddingRight; + readonly attribute double paddingBottom; + readonly attribute double paddingLeft; + // TODO(ianh): convert this to returning a Size readonly attribute double width; readonly attribute double height; diff --git a/engine/public/platform/sky_display_metrics.h b/engine/public/platform/sky_display_metrics.h index c045f773f..abe63b336 100644 --- a/engine/public/platform/sky_display_metrics.h +++ b/engine/public/platform/sky_display_metrics.h @@ -12,6 +12,10 @@ namespace blink { struct SkyDisplayMetrics { WebSize physical_size; float device_pixel_ratio = 1.0; + double padding_top = 0.0; + double padding_right = 0.0; + double padding_bottom = 0.0; + double padding_left = 0.0; }; } // namespace blink diff --git a/sdk/lib/theme/view_configuration.dart b/sdk/lib/theme/view_configuration.dart index b31a53838..bebad2949 100644 --- a/sdk/lib/theme/view_configuration.dart +++ b/sdk/lib/theme/view_configuration.dart @@ -5,7 +5,6 @@ // Modeled after Android's ViewConfiguration: // https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/ViewConfiguration.java -const double kNotificationAreaHeight = 25.0; // TODO(ianh): Figure out actual specced height for status bar const double kStatusBarHeight = 50.0; diff --git a/sdk/lib/widgets/scaffold.dart b/sdk/lib/widgets/scaffold.dart index e85aaa83a..3f8ab015f 100644 --- a/sdk/lib/widgets/scaffold.dart +++ b/sdk/lib/widgets/scaffold.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:sky' as sky; + import '../rendering/box.dart'; import '../rendering/object.dart'; import '../theme/view_configuration.dart'; @@ -102,7 +104,7 @@ class RenderScaffold extends RenderBox { } if (_slots[ScaffoldSlots.toolbar] != null) { RenderBox toolbar = _slots[ScaffoldSlots.toolbar]; - double toolbarHeight = kToolBarHeight + kNotificationAreaHeight; + double toolbarHeight = kToolBarHeight + sky.view.paddingTop; toolbar.layout(new BoxConstraints.tight(new Size(size.width, toolbarHeight))); assert(toolbar.parentData is BoxParentData); toolbar.parentData.position = Point.origin; diff --git a/services/engine/sky_engine.mojom b/services/engine/sky_engine.mojom index 6954e8014..b96436fca 100644 --- a/services/engine/sky_engine.mojom +++ b/services/engine/sky_engine.mojom @@ -6,8 +6,18 @@ module sky; import "sky/services/engine/input_event.mojom"; +struct ViewportMetrics { + int32 physical_width; + int32 physical_height; + float device_pixel_ratio = 1.0; + double padding_top; + double padding_right; + double padding_bottom; + double padding_left; +}; + interface SkyEngine { - OnViewportMetricsChanged(int32 width, int32 height, float device_pixel_ratio); + OnViewportMetricsChanged(ViewportMetrics metrics); OnInputEvent(InputEvent event); RunFromNetwork(string url); diff --git a/shell/android/org/domokit/sky/shell/PlatformViewAndroid.java b/shell/android/org/domokit/sky/shell/PlatformViewAndroid.java index 23f766729..aa807efee 100644 --- a/shell/android/org/domokit/sky/shell/PlatformViewAndroid.java +++ b/shell/android/org/domokit/sky/shell/PlatformViewAndroid.java @@ -24,6 +24,7 @@ import org.chromium.mojom.sky.InputEvent; import org.chromium.mojom.sky.PointerData; import org.chromium.mojom.sky.PointerKind; import org.chromium.mojom.sky.SkyEngine; +import org.chromium.mojom.sky.ViewportMetrics; /** * A view containing Sky @@ -37,9 +38,21 @@ public class PlatformViewAndroid extends SurfaceView private SkyEngine.Proxy mSkyEngine; private final SurfaceHolder.Callback mSurfaceCallback; private GestureProvider mGestureProvider; + private final EdgeDims mPadding; + + /** + * Dimensions in each of the four cardinal directions. + */ + public static class EdgeDims { + public double top = 0.0; + public double right = 0.0; + public double bottom = 0.0; + public double left = 0.0; + } - public PlatformViewAndroid(Context context) { + public PlatformViewAndroid(Context context, EdgeDims padding) { super(context); + mPadding = padding; setFocusable(true); setFocusableInTouchMode(true); @@ -53,7 +66,17 @@ public class PlatformViewAndroid extends SurfaceView @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { assert mSkyEngine != null; - mSkyEngine.onViewportMetricsChanged(width, height, density); + ViewportMetrics metrics = new ViewportMetrics(); + metrics.physicalWidth = width; + metrics.physicalHeight = height; + metrics.devicePixelRatio = density; + if (mPadding != null) { + metrics.paddingTop = mPadding.top; + metrics.paddingRight = mPadding.right; + metrics.paddingBottom = mPadding.bottom; + metrics.paddingLeft = mPadding.left; + } + mSkyEngine.onViewportMetricsChanged(metrics); } @Override diff --git a/shell/android/org/domokit/sky/shell/SkyActivity.java b/shell/android/org/domokit/sky/shell/SkyActivity.java index de8481771..ef1fa1cc4 100644 --- a/shell/android/org/domokit/sky/shell/SkyActivity.java +++ b/shell/android/org/domokit/sky/shell/SkyActivity.java @@ -28,16 +28,20 @@ public class SkyActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + PlatformViewAndroid.EdgeDims edgeDims = new PlatformViewAndroid.EdgeDims(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); getWindow().setStatusBarColor(0x40000000); + // TODO(abarth): We should get this value from the Android framework somehow. + edgeDims.top = 25.0; } getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); SkyMain.ensureInitialized(getApplicationContext()); - mView = new PlatformViewAndroid(this); + mView = new PlatformViewAndroid(this, edgeDims); setContentView(mView); mTracingController = new TracingController(this); diff --git a/shell/ios/sky_surface.mm b/shell/ios/sky_surface.mm index 2e16c1efe..518c9cc3b 100644 --- a/shell/ios/sky_surface.mm +++ b/shell/ios/sky_surface.mm @@ -86,8 +86,11 @@ static sky::InputEventPtr BasicInputEventFromRecognizer( CGSize size = self.bounds.size; CGFloat scale = [UIScreen mainScreen].scale; - _sky_engine->OnViewportMetricsChanged(size.width * scale, - size.height * scale, scale); + ViewportMetricsPtr metrics = ViewportMetrics::New(); + metrics->physical_width = size.width * scale; + metrics->physical_height = size.height * scale; + metrics->device_pixel_ratio = scale; + _sky_engine->OnViewportMetricsChanged(metrics.Pass()); } - (void)configureLayerDefaults { diff --git a/shell/testing/test_runner.cc b/shell/testing/test_runner.cc index 80c64531a..7c55e1b98 100644 --- a/shell/testing/test_runner.cc +++ b/shell/testing/test_runner.cc @@ -61,7 +61,10 @@ TestRunner::TestRunner() CHECK(!g_test_runner) << "Only create one TestRunner."; shell_view_->view()->ConnectToEngine(GetProxy(&sky_engine_)); - sky_engine_->OnViewportMetricsChanged(320, 640, 1.0); + ViewportMetricsPtr metrics = ViewportMetrics::New(); + metrics->physical_width = 320; + metrics->physical_height = 640; + sky_engine_->OnViewportMetricsChanged(metrics.Pass()); } TestRunner::~TestRunner() { diff --git a/shell/ui/engine.cc b/shell/ui/engine.cc index 8ff836d44..7893ec51f 100644 --- a/shell/ui/engine.cc +++ b/shell/ui/engine.cc @@ -58,7 +58,6 @@ Engine::Config::~Config() { Engine::Engine(const Config& config) : config_(config), animator_(new Animator(config, this)), - device_pixel_ratio_(1.0f), binding_(this), weak_factory_(this) { } @@ -102,7 +101,8 @@ skia::RefPtr Engine::Paint() { if (sky_view_) { skia::RefPtr picture = sky_view_->Paint(); canvas->clear(SK_ColorBLACK); - canvas->scale(device_pixel_ratio_, device_pixel_ratio_); + canvas->scale(display_metrics_.device_pixel_ratio, + display_metrics_.device_pixel_ratio); if (picture) canvas->drawPicture(picture.get()); } @@ -128,27 +128,24 @@ void Engine::OnOutputSurfaceDestroyed() { base::Bind(&GPUDelegate::OnOutputSurfaceDestroyed, config_.gpu_delegate)); } -void Engine::OnViewportMetricsChanged(int width, int height, - float device_pixel_ratio) { - physical_size_.SetSize(width, height); - device_pixel_ratio_ = device_pixel_ratio; +void Engine::OnViewportMetricsChanged(ViewportMetricsPtr metrics) { + physical_size_.SetSize(metrics->physical_width, metrics->physical_height); - if (sky_view_) - UpdateSkyViewSize(); -} + display_metrics_.physical_size = physical_size_; + display_metrics_.device_pixel_ratio = metrics->device_pixel_ratio; + display_metrics_.padding_top = metrics->padding_top; + display_metrics_.padding_right = metrics->padding_right; + display_metrics_.padding_bottom = metrics->padding_bottom; + display_metrics_.padding_left = metrics->padding_left; -void Engine::UpdateSkyViewSize() { - CHECK(sky_view_); - blink::SkyDisplayMetrics metrics; - metrics.physical_size = physical_size_; - metrics.device_pixel_ratio = device_pixel_ratio_; - sky_view_->SetDisplayMetrics(metrics); + if (sky_view_) + sky_view_->SetDisplayMetrics(display_metrics_); } void Engine::OnInputEvent(InputEventPtr event) { TRACE_EVENT0("sky", "Engine::OnInputEvent"); scoped_ptr web_event = - ConvertEvent(event, device_pixel_ratio_); + ConvertEvent(event, display_metrics_.device_pixel_ratio); if (!web_event) return; if (sky_view_) @@ -159,7 +156,7 @@ void Engine::RunFromLibrary(const std::string& name) { sky_view_ = blink::SkyView::Create(this); sky_view_->RunFromLibrary(blink::WebString::fromUTF8(name), dart_library_provider_.get()); - UpdateSkyViewSize(); + sky_view_->SetDisplayMetrics(display_metrics_); } void Engine::RunFromSnapshotStream( @@ -167,7 +164,7 @@ void Engine::RunFromSnapshotStream( mojo::ScopedDataPipeConsumerHandle snapshot) { sky_view_ = blink::SkyView::Create(this); sky_view_->RunFromSnapshot(blink::WebString::fromUTF8(name), snapshot.Pass()); - UpdateSkyViewSize(); + sky_view_->SetDisplayMetrics(display_metrics_); } void Engine::RunFromNetwork(const mojo::String& url) { diff --git a/shell/ui/engine.h b/shell/ui/engine.h index ebb605cb8..f4c51ffac 100644 --- a/shell/ui/engine.h +++ b/shell/ui/engine.h @@ -62,8 +62,7 @@ class Engine : public UIDelegate, void OnOutputSurfaceDestroyed() override; // SkyEngine implementation: - void OnViewportMetricsChanged(int width, int height, - float device_pixel_ratio) override; + void OnViewportMetricsChanged(ViewportMetricsPtr metrics) override; void OnInputEvent(InputEventPtr event) override; void RunFromNetwork(const mojo::String& url) override; @@ -89,8 +88,6 @@ class Engine : public UIDelegate, void RunFromSnapshotStream(const std::string& name, mojo::ScopedDataPipeConsumerHandle snapshot); - void UpdateSkyViewSize(); - Config config_; scoped_ptr animator_; @@ -98,8 +95,8 @@ class Engine : public UIDelegate, scoped_ptr dart_library_provider_; std::unique_ptr sky_view_; - float device_pixel_ratio_; gfx::Size physical_size_; + blink::SkyDisplayMetrics display_metrics_; mojo::Binding binding_; base::WeakPtrFactory weak_factory_; -- GitLab