areViewsPresent static method

void areViewsPresent(
  1. String listenerId,
  2. dynamic searchElements,
  3. double pixRatio,
  4. int screenWidth,
  5. int screenHeight,
)

Implementation

static void areViewsPresent(String listenerId, searchElements,
    double pixRatio, int screenWidth, int screenHeight) {
  try {
    plotlineDebugLog("PlotlineDebug: areViewsPresent: $listenerId, $searchElements, $pixRatio, $screenWidth, $screenHeight");
    screenHeight = (MediaQuery.of(_ctx).size.height * pixRatio).round();
    screenWidth = (MediaQuery.of(_ctx).size.width * pixRatio).round();
    List<Map<String, dynamic>> presentElements = [];
    for (int i = 0; i < searchElements.length; i++) {
      var key = searchElements[i]["clientElementId"];
      if (!key.contains("<'") && !key.contains("'>")) {
        key = "[<'$key'>]";
      }
      if(visibilityMap[key] == 100.0) {
        plotlineDebugLog("PlotlineDebug: areViewsPresent: visibility=100: $listenerId, $searchElements, $pixRatio, $screenWidth, $screenHeight");
        BuildContext? element = findViewByKey(key, _ctx);
        if (element == null) {
          // A null lookup is otherwise silent and looks the same as a match.
          // Most likely cause: gating detached this PView, so it rendered the
          // bare child and nothing in the tree carries ValueKey(valueKey) any
          // more. Otherwise: stale _ctx, or the widget is not under it.
          plotlineDebugLog(
              "PlotlineDebug: areViewsPresent: ELEMENT NOT FOUND for $key "
              "(visibility was 100 but findViewByKey returned null - PView "
              "detached by gating, stale _ctx, or the widget is not under the "
              "tracked BuildContext): $listenerId");
        } else {
          plotlineDebugLog("PlotlineDebug: areViewsPresent: element != null: $listenerId, $searchElements, $pixRatio, $screenWidth, $screenHeight");
          RenderBox box = element.findRenderObject() as RenderBox;
          Offset position = box.localToGlobal(Offset.zero);
          if (isWithinBounds(
              element, position, pixRatio, screenWidth, screenHeight)) {
            plotlineDebugLog("PlotlineDebug: areViewsPresent: isWithinBounds: $listenerId, $searchElements, $pixRatio, $screenWidth, $screenHeight");
            presentElements.add(searchElements[i]);
          } else if (shouldEnableDebug) {
            // Geometry rejection is otherwise silent; the numbers say why.
            // Read the size off `box`, not `element`: BuildContext.size also
            // throws when the render object is dirty for layout, and that
            // throw would abort areViewsPresent before it replies to native.
            plotlineDebugLog(
                "PlotlineDebug: areViewsPresent: OUT OF BOUNDS for $key "
                "pos=(${position.dx},${position.dy}) "
                "size=${box.hasSize ? box.size : 'unlaid-out'} "
                "pixRatio=$pixRatio screen=${screenWidth}x$screenHeight: $listenerId");
          }
        }
      } else {
        plotlineDebugLog("PlotlineDebug: areViewsPresent: visibility of element with key $key = ${visibilityMap[key]} END: $listenerId, $searchElements, $pixRatio, $screenWidth, $screenHeight}");
        plotlineDebugLog("PlotlineDebug: areViewsPresent: visibilityMap is $visibilityMap END: $listenerId, $searchElements, $pixRatio, $screenWidth, $screenHeight");
        if (shouldEnableDebug) {
          // Whether the widget is actually on screen decides if the gate or
          // the tree is at fault, which the visibility value alone cannot say.
          try {
            final BuildContext? probe = findViewByKey(key, _ctx);
            if (probe == null) {
              plotlineDebugLog(
                  "PlotlineDebug: areViewsPresent: PROBE $key inTree=false: $listenerId");
            } else {
              final RenderObject? ro = probe.findRenderObject();
              final RenderBox? pb = ro is RenderBox && ro.hasSize ? ro : null;
              if (pb == null) {
                plotlineDebugLog(
                    "PlotlineDebug: areViewsPresent: PROBE $key inTree=true "
                    "inBounds=unlaid-out: $listenerId");
              } else {
                // Bounds computed off the RenderBox, not isWithinBounds: that
                // returns false on a throw, which would read as a real
                // rejection and hide the distinction this probe exists for.
                final Offset pos = pb.localToGlobal(Offset.zero);
                final int x = (pos.dx * pixRatio).round();
                final int y = (pos.dy * pixRatio).round();
                final int w = (pb.size.width * pixRatio).round();
                final int h = (pb.size.height * pixRatio).round();
                final bool inBounds = x >= 0 &&
                    y >= 0 &&
                    (x + w) <= screenWidth &&
                    (y + h) <= screenHeight &&
                    w > 0 &&
                    h > 0;
                plotlineDebugLog(
                    "PlotlineDebug: areViewsPresent: PROBE $key inTree=true "
                    "inBounds=$inBounds pos=($x,$y) size=${w}x$h "
                    "pixRatio=$pixRatio screen=${screenWidth}x$screenHeight: $listenerId");
              }
            }
          } catch (e) {
            // Never let a diagnostic abort the reply; native waits on it.
            plotlineDebugLog("PlotlineDebug: areViewsPresent: PROBE $key failed: $e");
          }
        }
      }
    }

    plotlineChannel.invokeMethod('areViewsPresent', <String, dynamic>{
      "listenerId": listenerId,
      "presentElements": presentElements
    });
  } catch (e) {
    plotlineDebugLog("Error in areViewsPresent: $e");
  }
}