static boolean isNestedTree(@Nullable Component component) { return (isLayoutSpecWithSizeSpec(component) || (component != null && component.getCachedLayout() != null)); }
/** * Only use if absolutely needed! This removes the cached layout so this component will be * remeasured even if it has alread been measured with the same size specs. */ public void releaseCachedLayout() { final InternalNode cachedLayout = getCachedLayout(); if (cachedLayout != null) { LayoutState.releaseNodeTree(cachedLayout, true /* isNestedTree */); clearCachedLayout(); } }
@Override public void run() { // Wait for component to have a cached layout, then make copy. try { lockWaitMeasure.await(5, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new IllegalStateException(); } resultCopyComponent[0] = component.makeShallowCopy(); cachedLayouts[1] = resultCopyComponent[0].getCachedLayout(); testFinished.countDown(); } });
@Override public void run() { component.measure(mContext, 100, 100, new Size()); cachedLayouts[0] = component.getCachedLayout(); lockWaitMeasure.countDown(); } });
@Override public void run() { component.measure(mContext, 100, 100, new Size()); cachedLayouts[0] = component.getCachedLayout(); lockWaitMeasure.countDown(); } });
@Override public void run() { // Wait for component to have a cached layout, then make copy. try { lockWaitMeasure.await(5, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new IllegalStateException(); } resultCopyComponent[0] = component.makeShallowCopy(); resultCopyComponent[0].measure(mContext, 100, 100, new Size()); cachedLayouts[1] = resultCopyComponent[0].getCachedLayout(); testFinished.countDown(); } });
@Test public void testShallowCopyCachedLayoutSameThread() { Component component = TestDrawableComponent.create(mContext).build(); component.measure(mContext, 100, 100, new Size()); assertThat(component.getCachedLayout()).isNotNull(); assertThat(component.getCachedLayout()).isNotNull(); Component copyComponent = component.makeShallowCopy(); assertThat(copyComponent.getCachedLayout()).isNotNull(); }
@Nullable static InternalNode consumeCachedLayout( Component component, InternalNode holder, int widthSpec, int heightSpec) { final InternalNode cachedLayout = component.getCachedLayout(); if (cachedLayout != null) { final boolean hasValidDirection = InternalNode.hasValidLayoutDirectionInNestedTree(holder, cachedLayout); final boolean hasCompatibleSizeSpec = hasCompatibleSizeSpec( cachedLayout.getLastWidthSpec(), cachedLayout.getLastHeightSpec(), widthSpec, heightSpec, cachedLayout.getLastMeasuredWidth(), cachedLayout.getLastMeasuredHeight()); // Transfer the cached layout to the node without releasing it if it's compatible. if (hasValidDirection && hasCompatibleSizeSpec) { component.clearCachedLayout(); return cachedLayout; } else { component.releaseCachedLayout(); return null; } } else { return null; } }
InternalNode lastMeasuredLayout = getCachedLayout(); if (lastMeasuredLayout == null || !MeasureComparisonUtils.isMeasureSpecCompatible(
assertThat(component.getCachedLayout()).isNull(); assertThat(resultCopyComponent[0].getCachedLayout()).isNull();
assertThat(component.getCachedLayout()).isNull(); assertThat(resultCopyComponent[0].getCachedLayout()).isNull();
@Test public void testComponentCreateAndRetrieveCachedLayout() { final ComponentContext c = new ComponentContext(application); final int unspecifiedSizeSpec = makeSizeSpec(0, UNSPECIFIED); final int exactSizeSpec = makeSizeSpec(50, EXACTLY); final Component textComponent = Text.create(c) .textSizePx(16) .text("test") .build(); final Size textSize = new Size(); textComponent.measure( c, exactSizeSpec, unspecifiedSizeSpec, textSize); assertThat(textComponent.getCachedLayout()).isNotNull(); InternalNode cachedLayout = textComponent.getCachedLayout(); assertThat(cachedLayout).isNotNull(); assertThat(cachedLayout.getLastWidthSpec()).isEqualTo(exactSizeSpec); assertThat(cachedLayout.getLastHeightSpec()).isEqualTo(unspecifiedSizeSpec); textComponent.clearCachedLayout(); assertThat(textComponent.getCachedLayout()).isNull(); }
assertThat(componentSpy.getCachedLayout()).isNotNull(); final InternalNode cachedLayout = componentSpy.getCachedLayout(); assertThat(cachedLayout.getChildCount()).isEqualTo(0); assertThat(cachedLayout.getRootComponent()).isInstanceOf(TestDrawableComponent.class);
assertThat(componentSpy.getCachedLayout()).isNotNull(); final InternalNode cachedLayout = componentSpy.getCachedLayout(); assertThat(cachedLayout.getChildCount()).isEqualTo(1); assertThat(((InternalNode) cachedLayout.getChildAt(0)).getRootComponent())
assertThat(sizeDependentComponentSpy.getCachedLayout()).isNotNull(); final InternalNode cachedLayout = sizeDependentComponentSpy.getCachedLayout(); assertThat(cachedLayout.getChildCount()).isEqualTo(0); assertThat(cachedLayout.getRootComponent()).isInstanceOf(TestDrawableComponent.class);
assertThat(sizeDependentComponentSpy.getCachedLayout()).isNotNull(); final InternalNode cachedLayout = sizeDependentComponentSpy.getCachedLayout(); assertThat(cachedLayout.getChildCount()).isEqualTo(2); assertThat(((InternalNode) cachedLayout.getChildAt(0)).getRootComponent())
size); final InternalNode internalNode = ((Component) innerComponent).getCachedLayout(); internalNode.setLastWidthSpec(widthSpec); internalNode.setLastHeightSpec(heightSpec);
size); final InternalNode internalNode = ((Component) innerComponent).getCachedLayout(); internalNode.setLastWidthSpec(widthSpec); internalNode.setLastHeightSpec(heightSpec);
size); final InternalNode internalNode = ((Component) innerComponent).getCachedLayout(); internalNode.setLastWidthSpec(widthSpec); internalNode.setLastHeightSpec(heightSpec);
size); final InternalNode internalNode = ((Component) innerComponent).getCachedLayout(); internalNode.setLastWidthSpec(widthSpec); internalNode.setLastHeightSpec(heightSpec);