Skip to content

Commit

Permalink
Merge pull request flutter#2280 from abarth/window_insets
Browse files Browse the repository at this point in the history
Improve handling of window insets on Android
  • Loading branch information
abarth committed Jan 21, 2016
2 parents d9a4acd + 33d7553 commit 8b4d246
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 78 deletions.
16 changes: 6 additions & 10 deletions sky/engine/core/window/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,15 @@ void Window::UpdateWindowMetrics(const SkyDisplayMetrics& metrics) {
if (!dart_state)
return;
DartState::Scope scope(dart_state);

double device_pixel_ratio = metrics.device_pixel_ratio;
double width = metrics.physical_size.width / device_pixel_ratio;
double height = metrics.physical_size.height / device_pixel_ratio;

DartInvokeField(library_.value(), "_updateWindowMetrics", {
ToDart(device_pixel_ratio),
ToDart(width),
ToDart(height),
ToDart(metrics.padding_top),
ToDart(metrics.padding_right),
ToDart(metrics.padding_bottom),
ToDart(metrics.padding_left),
ToDart(metrics.physical_size.width / device_pixel_ratio),
ToDart(metrics.physical_size.height / device_pixel_ratio),
ToDart(metrics.physical_padding_top / device_pixel_ratio),
ToDart(metrics.physical_padding_right / device_pixel_ratio),
ToDart(metrics.physical_padding_bottom / device_pixel_ratio),
ToDart(metrics.physical_padding_left / device_pixel_ratio),
});
}

Expand Down
10 changes: 5 additions & 5 deletions sky/engine/public/platform/sky_display_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
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;
WebSize physical_size;
int physical_padding_top = 0;
int physical_padding_right = 0;
int physical_padding_bottom = 0;
int physical_padding_left = 0;
};

} // namespace blink
Expand Down
10 changes: 5 additions & 5 deletions sky/services/engine/sky_engine.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ enum AppLifecycleState {
};

struct ViewportMetrics {
float device_pixel_ratio = 1.0;
int32 physical_width;
int32 physical_height;
float device_pixel_ratio = 1.0;
double padding_top;
double padding_right;
double padding_bottom;
double padding_left;
int32 physical_padding_top;
int32 physical_padding_right;
int32 physical_padding_bottom;
int32 physical_padding_left;
};

struct ServicesData {
Expand Down
3 changes: 2 additions & 1 deletion sky/shell/platform/android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
android:hardwareAccelerated="true"
android:launchMode="standard"
android:name="SkyActivity"
android:theme="@android:style/Theme.Black.NoTitleBar">
android:theme="@android:style/Theme.Black.NoTitleBar"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
Expand All @@ -14,6 +15,7 @@
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.WindowInsets;

import org.chromium.base.JNINamespace;
import org.chromium.mojo.bindings.InterfaceRequest;
Expand Down Expand Up @@ -51,47 +53,24 @@ public class PlatformViewAndroid extends SurfaceView {
private SkyEngine.Proxy mSkyEngine;
private PlatformServiceProvider mServiceProvider;
private final SurfaceHolder.Callback mSurfaceCallback;
private final EdgeDims mPadding;
private final ViewportMetrics mMetrics;
private final KeyboardServiceState mKeyboardState;
private final RawKeyboardServiceState mRawKeyboardState;

/**
* 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, EdgeDims padding) {
public PlatformViewAndroid(Context context) {
super(context);
mPadding = padding;

mMetrics = new ViewportMetrics();
mMetrics.devicePixelRatio = context.getResources().getDisplayMetrics().density;
setFocusable(true);
setFocusableInTouchMode(true);

attach();
assert mNativePlatformView != 0;

final float density = context.getResources().getDisplayMetrics().density;

mSurfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
assert mSkyEngine != null;
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
Expand Down Expand Up @@ -137,15 +116,6 @@ void destroy() {
mNativePlatformView = 0;
}

@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
if (visibility == View.VISIBLE) {
requestFocusFromTouch();
requestFocus();
}
}

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return mKeyboardState.createInputConnection(outAttrs);
Expand Down Expand Up @@ -257,6 +227,24 @@ public boolean onTouchEvent(MotionEvent event) {
return true;
}

@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
mMetrics.physicalWidth = width;
mMetrics.physicalHeight = height;
mSkyEngine.onViewportMetricsChanged(mMetrics);
super.onSizeChanged(width, height, oldWidth, oldHeight);
}

@Override
public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
mMetrics.physicalPaddingTop = insets.getSystemWindowInsetTop();
mMetrics.physicalPaddingRight = insets.getSystemWindowInsetRight();
mMetrics.physicalPaddingBottom = insets.getSystemWindowInsetBottom();
mMetrics.physicalPaddingLeft = insets.getSystemWindowInsetLeft();
mSkyEngine.onViewportMetricsChanged(mMetrics);
return super.onApplyWindowInsets(insets);
}

private void configureLocalServices(ServiceRegistry registry) {
registry.register(KeyboardService.MANAGER.getName(), new ServiceFactory() {
@Override
Expand Down
23 changes: 10 additions & 13 deletions sky/shell/platform/android/org/domokit/sky/shell/SkyActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import org.chromium.base.PathUtils;
Expand Down Expand Up @@ -57,23 +59,18 @@ private String[] getArgsFromIntent(Intent intent) {
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);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(0x40000000);
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
// TODO(abarth): We should get this value from the Android framework somehow.
edgeDims.top = 25.0;
// TODO(abarth): Unclear if we want to use fullscreen if we don't have
// a transparent system bar.
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

String[] args = getArgsFromIntent(getIntent());
SkyMain.ensureInitialized(getApplicationContext(), args);
mView = new PlatformViewAndroid(this, edgeDims);
mView = new PlatformViewAndroid(this);
ActivityImpl.setCurrentActivity(this);
setContentView(mView);
mTracingController = new TracingController(this);
Expand Down Expand Up @@ -175,7 +172,7 @@ public boolean loadBundleByName(String name) {
}

public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
super.onConfigurationChanged(newConfig);

Locale locale = getResources().getConfiguration().locale;
mView.getEngine().onLocaleChanged(locale.getLanguage(),
Expand Down
6 changes: 3 additions & 3 deletions sky/shell/platform/ios/sky_surface.mm
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ - (void)layoutSubviews {
CGFloat scale = [UIScreen mainScreen].scale;

sky::ViewportMetricsPtr metrics = sky::ViewportMetrics::New();
metrics->device_pixel_ratio = scale;
metrics->physical_width = size.width * scale;
metrics->physical_height = size.height * scale;
metrics->device_pixel_ratio = scale;
metrics->padding_top =
[UIApplication sharedApplication].statusBarFrame.size.height;
metrics->physical_padding_top =
[UIApplication sharedApplication].statusBarFrame.size.height * scale;

_sky_engine->OnViewportMetricsChanged(metrics.Pass());
}
Expand Down
10 changes: 5 additions & 5 deletions sky/shell/ui/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ void Engine::SetServices(ServicesDataPtr services) {
void Engine::OnViewportMetricsChanged(ViewportMetricsPtr metrics) {
physical_size_.SetSize(metrics->physical_width, metrics->physical_height);

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;
display_metrics_.physical_size = physical_size_;
display_metrics_.physical_padding_top = metrics->physical_padding_top;
display_metrics_.physical_padding_right = metrics->physical_padding_right;
display_metrics_.physical_padding_bottom = metrics->physical_padding_bottom;
display_metrics_.physical_padding_left = metrics->physical_padding_left;

if (sky_view_)
sky_view_->SetDisplayMetrics(display_metrics_);
Expand Down

0 comments on commit 8b4d246

Please sign in to comment.