!7

cmodule MarkedFaces extends DynSCP {
  File dir;

  transient DynamicHStack buttons;
  transient ButtonImageLoader imageLoader;
  
  start {
    dir = ai_markedFacesDir();
    ownResource(vmBus_onDirChange(dir, r updateButtons));
  }

  JComponent visualize2() {
    setDir(dir);
    ret getComponent();
  }
  
  void setDir(File dir) {
    temp enter();
    setField(+dir);

    buttons = makeNiceButtons();
    imageLoader = new ButtonImageLoader(buttons) {
      void setAButtonsImage(JButton b, File f) {
        BufferedImage img = loadBufferedImage(f);
        BufferedImage preview = scaleBufferedImageToHeight(imageHeight, img);
        Rect face = cast unstruct(getFileInfoField(f, "Face position"));
        if (face != null)
          drawRect(preview, rescaleRect_imageToImage(face, img, preview), Color.white);
        setButtonImage(b, preview);
      }
    };
    updateButtons();
    setComponent(jHigherScrollPane(jfullcenter(buttons)));
  }
  
  void updateButtons {
    if (dir == null || buttons == null) ret;
    temp enter();
    L<File> imageFiles = listImageFiles(dir);
    L<TextImageAction> l = filesForNiceButtons(latestFilesFirst(imageFiles), f showImage);
    for (final TextImageAction tia : l)
      tia.popupMenuMaker = imageFilesPopupMenuFunction(tia);
    replaceNiceButtons(buttons, l);
    setModuleName(n2(l, "Marked Face"));
    if (imageLoader != null) imageLoader.update();
  }
}