Skip to content

Commit

Permalink
Expose a hasRenderedFirstFrame() method in FlutterView (flutter#34275)…
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Carroll authored Jun 11, 2019
1 parent 8040117 commit 2d2cfc0
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class FlutterView extends FrameLayout {
// Internal view hierarchy references.
@Nullable
private FlutterRenderer.RenderSurface renderSurface;
private boolean didRenderFirstFrame;

// Connections to a Flutter execution context.
@Nullable
Expand Down Expand Up @@ -97,6 +98,13 @@ public void onAccessibilityChanged(boolean isAccessibilityEnabled, boolean isTou
}
};

private final OnFirstFrameRenderedListener onFirstFrameRenderedListener = new OnFirstFrameRenderedListener() {
@Override
public void onFirstFrameRendered() {
didRenderFirstFrame = true;
}
};

/**
* Constructs a {@code FlutterView} programmatically, without any XML attributes.
* <p>
Expand Down Expand Up @@ -171,11 +179,34 @@ private void init() {
break;
}

// Register a listener for the first frame render event to set didRenderFirstFrame.
renderSurface.addOnFirstFrameRenderedListener(onFirstFrameRenderedListener);

// FlutterView needs to be focusable so that the InputMethodManager can interact with it.
setFocusable(true);
setFocusableInTouchMode(true);
}

/**
* Returns true if an attached {@link FlutterEngine} has rendered at least 1 frame to this
* {@code FlutterView}.
* <p>
* Returns false if no {@link FlutterEngine} is attached.
* <p>
* This flag is specific to a given {@link FlutterEngine}. The following hypothetical timeline
* demonstrates how this flag changes over time.
* <ol>
* <li>{@code flutterEngineA} is attached to this {@code FlutterView}: returns false</li>
* <li>{@code flutterEngineA} renders its first frame to this {@code FlutterView}: returns true</li>
* <li>{@code flutterEngineA} is detached from this {@code FlutterView}: returns false</li>
* <li>{@code flutterEngineB} is attached to this {@code FlutterView}: returns false</li>
* <li>{@code flutterEngineB} renders its first frame to this {@code FlutterView}: returns true</li>
* </ol>
*/
public boolean hasRenderedFirstFrame() {
return didRenderFirstFrame;
}

/**
* Adds the given {@code listener} to this {@code FlutterView}, to be notified upon Flutter's
* first rendered frame.
Expand Down Expand Up @@ -471,6 +502,7 @@ public void attachToFlutterEngine(@NonNull FlutterEngine flutterEngine) {
this.flutterEngine = flutterEngine;

// Instruct our FlutterRenderer that we are now its designated RenderSurface.
didRenderFirstFrame = false;
this.flutterEngine.getRenderer().attachToRenderSurface(renderSurface);

// Initialize various components that know how to process Android View I/O
Expand Down Expand Up @@ -536,6 +568,7 @@ public void detachFromFlutterEngine() {
textInputPlugin.getInputMethodManager().restartInput(this);

// Instruct our FlutterRenderer that we are no longer interested in being its RenderSurface.
didRenderFirstFrame = false;
flutterEngine.getRenderer().detachFromRenderSurface();
flutterEngine = null;

Expand Down

0 comments on commit 2d2cfc0

Please sign in to comment.