"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/packages/flutter/lib/src/painting/geometry.dart" (13 Nov 2020, 2888 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 
    6 import 'dart:math' as math;
    7 
    8 import 'basic_types.dart';
    9 
   10 /// Position a child box within a container box, either above or below a target
   11 /// point.
   12 ///
   13 /// The container's size is described by `size`.
   14 ///
   15 /// The target point is specified by `target`, as an offset from the top left of
   16 /// the container.
   17 ///
   18 /// The child box's size is given by `childSize`.
   19 ///
   20 /// The return value is the suggested distance from the top left of the
   21 /// container box to the top left of the child box.
   22 ///
   23 /// The suggested position will be above the target point if `preferBelow` is
   24 /// false, and below the target point if it is true, unless it wouldn't fit on
   25 /// the preferred side but would fit on the other side.
   26 ///
   27 /// The suggested position will place the nearest side of the child to the
   28 /// target point `verticalOffset` from the target point (even if it cannot fit
   29 /// given that constraint).
   30 ///
   31 /// The suggested position will be at least `margin` away from the edge of the
   32 /// container. If possible, the child will be positioned so that its center is
   33 /// aligned with the target point. If the child cannot fit horizontally within
   34 /// the container given the margin, then the child will be centered in the
   35 /// container.
   36 ///
   37 /// Used by [Tooltip] to position a tooltip relative to its parent.
   38 ///
   39 /// The arguments must not be null.
   40 Offset positionDependentBox({
   41   required Size size,
   42   required Size childSize,
   43   required Offset target,
   44   required bool preferBelow,
   45   double verticalOffset = 0.0,
   46   double margin = 10.0,
   47 }) {
   48   assert(size != null);
   49   assert(childSize != null);
   50   assert(target != null);
   51   assert(verticalOffset != null);
   52   assert(preferBelow != null);
   53   assert(margin != null);
   54   // VERTICAL DIRECTION
   55   final bool fitsBelow = target.dy + verticalOffset + childSize.height <= size.height - margin;
   56   final bool fitsAbove = target.dy - verticalOffset - childSize.height >= margin;
   57   final bool tooltipBelow = preferBelow ? fitsBelow || !fitsAbove : !(fitsAbove || !fitsBelow);
   58   double y;
   59   if (tooltipBelow)
   60     y = math.min(target.dy + verticalOffset, size.height - margin);
   61   else
   62     y = math.max(target.dy - verticalOffset - childSize.height, margin);
   63   // HORIZONTAL DIRECTION
   64   double x;
   65   if (size.width - margin * 2.0 < childSize.width) {
   66     x = (size.width - childSize.width) / 2.0;
   67   } else {
   68     final double normalizedTargetX = target.dx.clamp(margin, size.width - margin);
   69     final double edge = margin + childSize.width / 2.0;
   70     if (normalizedTargetX < edge) {
   71       x = margin;
   72     } else if (normalizedTargetX > size.width - edge) {
   73       x = size.width - margin - childSize.width;
   74     } else {
   75       x = normalizedTargetX - childSize.width / 2.0;
   76     }
   77   }
   78   return Offset(x, y);
   79 }