protected void registerSynchronizers()

in view/src/main/java/jetbrains/jetpad/projectional/view/toGwt/BaseViewMapper.java [76:187]


  protected void registerSynchronizers(SynchronizersConfiguration conf) {
    super.registerSynchronizers(conf);

    Style targetStyle = getTarget().getStyle();

    if (!isDomPosition()) {
      targetStyle.setPosition(Style.Position.ABSOLUTE);
    } else {
      targetStyle.setPosition(Style.Position.RELATIVE);
    }

    if (!isDomPosition() || !isDomLayout()) {
      final ReadableProperty<Rectangle> positionInParent;
      if (getParent() instanceof BaseViewMapper) {
        final BaseViewMapper<?, ?> parent = (BaseViewMapper<?, ?>) getParent();
        positionInParent = new DerivedProperty<Rectangle>(getSource().bounds(), parent.getSource().bounds()) {
          @Override
          public Rectangle doGet() {
            Rectangle sourceBounds = getSource().bounds().get();
            Rectangle parentSourceBounds = parent.getSource().bounds().get();
            return sourceBounds.sub(parentSourceBounds.origin);
          }
        };
      } else {
        positionInParent = getSource().bounds();
      }

      final Value<Boolean> valid = new Value<>(false);

      conf.add(Synchronizers.forEventSource(EventSources.composite(positionInParent, getSource().border()), new Runnable() {
        @Override
        public void run() {
          valid.set(false);
          whenValid(new Runnable() {
            @Override
            public void run() {
              if (valid.get()) return;
              final Rectangle value = positionInParent.get();
              Style style = getTarget().getStyle();

              if (!isDomPosition()) {
                style.setLeft(value.origin.x, Style.Unit.PX);
                style.setTop(value.origin.y, Style.Unit.PX);
              }

              if (!isDomLayout()) {
                int width = value.dimension.x;
                int height = value.dimension.y;

                style.setWidth(width, Style.Unit.PX);
                style.setHeight(height, Style.Unit.PX);
              }
              valid.set(true);
            }
          });
        }
      }));
    }

    if (!isCustomBackgroundSync()) {
      conf.add(Synchronizers.forPropsOneWay(getSource().background(), new WritableProperty<Color>() {
        @Override
        public void set(Color value) {
          Style style = getTarget().getStyle();
          if (value == null) {
            style.setBackgroundColor(null);
          } else {
            style.setBackgroundColor(value.toCssColor());
          }
        }
      }));
    }

    conf.add(Synchronizers.forPropsOneWay(getSource().border(), new WritableProperty<Color>() {
      @Override
      public void set(Color value) {
        Style style = getTarget().getStyle();
        if (value != null) {
          style.setOutlineColor(value.toCssColor());
          style.setOutlineWidth(1, Style.Unit.PX);
          style.setOutlineStyle(Style.OutlineStyle.SOLID);
        } else {
          style.clearOutlineStyle();
          style.clearOutlineColor();
          style.clearBorderWidth();
        }
      }
    }));

    conf.add(Synchronizers.forPropsOneWay(getSource().visible(), new WritableProperty<Boolean>() {
      @Override
      public void set(final Boolean value) {
        whenValid(new Runnable() {
          @Override
          public void run() {
            getTarget().getStyle().setDisplay(value ? Style.Display.BLOCK : Style.Display.NONE);
          }
        });
      }
    }));

    conf.add(Synchronizers.forPropsOneWay(getSource().hasShadow(), new WritableProperty<Boolean>() {
      @Override
      public void set(Boolean value) {
        if (value) {
          getTarget().getStyle().setProperty("boxShadow", "2px 2px 4px black");
        } else {
          getTarget().getStyle().setProperty("boxShadow", null);
        }
      }
    }));
  }