Skip to content
Repos

DevPlaceApp

C++ 2.7 KB83 lines Opens in site

Directory Raw Download

#include "screens/components/AttachmentViews.hpp"

#include "screens/Router.hpp"
#include "screens/components/AccessibleLabel.hpp"
#include "screens/components/AttachmentViewerScreen.hpp"
#include "spec/ComposeSpec.hpp"
#include "ui/Modifiers.hpp"
#include "ui/Overlays.hpp"
#include "ui/Palette.hpp"
#include "ui/RemoteImageView.hpp"

namespace dp {

using namespace ui;
using spec::ComposeSpec;

namespace {

// Gives its child a fixed width to height ratio at the proposed width.
class AspectTileView : public View {
 public:
  AspectTileView(float ratio, ViewPtr child) : ratio_(ratio) { addChild(std::move(child)); }

 protected:
  Size compute(const Proposal& p) override {
    const float w = p.w && *p.w < kInf / 2 ? *p.w : 320.0f;
    children_[0]->measure(Proposal{w, w / ratio_});
    return {w, w / ratio_};
  }

 public:
  void place(const Rect& r) override {
    frame_ = r;
    children_[0]->place(r);
  }

 private:
  float ratio_;
};

ViewPtr videoTile(const std::string& url) {
  ViewPtr badge = Icon("play.circle.fill", ComposeSpec::kPlayBadgeSize)->foreground(Color::rgb(0xFFFFFF))
                      ->shadow(Color{0, 0, 0, 90}, 4, 0, 1, Corners::all(ComposeSpec::kPlayBadgeSize / 2));
  ViewPtr tile = std::make_shared<AspectTileView>(ComposeSpec::kVideoAspect, ZStack({RectangleShape(Palette::bg2()), badge}));
  return Button(tile, [url] { Router::instance().openUrl(url); }, ButtonStyles::plain());
}

ViewPtr imageTile(const std::string& url) {
  ViewPtr spinner = ProgressSpinner(20)->maxWidth();
  ViewPtr image = RemoteImage(url, ContentMode::Fit, spinner, ComposeSpec::kInlineRasterPx);
  return Button(image, [url] { presentFullscreenImage(url); }, ButtonStyles::plain());
}

}  // namespace

std::string AttachmentLinks::resolve(const std::string& url) {
  if (!url.empty() && url[0] == '/') return std::string(ComposeSpec::kHostPrefix) + url;
  return url;
}

void presentFullscreenImage(const std::string& url) {
  Overlays::instance().sheet(std::make_shared<FullscreenImageViewerScreen>(url), SheetStyle::FullScreen);
}

ViewPtr AttachmentViewer(const Attachment& a) {
  const std::string url = AttachmentLinks::resolve(a.url);
  if (url.empty()) return EmptyView();
  if (a.isVideo) return WithAccessibleLabel(videoTile(url), ComposeSpec::kA11yVideo);
  if (a.isImage) return WithAccessibleLabel(imageTile(url), ComposeSpec::kA11yImage);
  return EmptyView();
}

ViewPtr AttachmentViews(const std::vector<Attachment>& attachments, float spacing) {
  std::vector<ViewPtr> rows;
  for (const Attachment& a : attachments) {
    if (rows.size() >= ComposeSpec::kMaxAttachmentViews) break;
    if (a.isVideo || a.isImage) rows.push_back(AttachmentViewer(a));
  }
  if (rows.empty()) return EmptyView();
  return VStack(rows, spacing, HAlign::Leading);
}

}  // namespace dp
36.6ms