#include "screens/components/AttachmentViewerScreen.hpp" #include #include #include #include "net/NetworkService.hpp" #include "screens/components/AttachmentFileDialogs.hpp" #include "spec/ComposeSpec.hpp" #include "ui/Controls.hpp" #include "ui/Modifiers.hpp" #include "ui/Overlays.hpp" #include "ui/Palette.hpp" #include "ui/RemoteImages.hpp" #include "ui/TextInput.hpp" namespace dp { using namespace ui; using spec::ComposeSpec; void ZoomState::setScale(float value) { scale = std::clamp(value, ComposeSpec::kZoomMin, ComposeSpec::kZoomMax); if (scale <= ComposeSpec::kZoomMin) offsetX = offsetY = 0; } void ZoomState::zoomBy(float factor) { setScale(scale * factor); } void ZoomState::pan(float dx, float dy) { if (scale <= ComposeSpec::kZoomMin) return; offsetX += dx; offsetY += dy; } namespace { class ZoomableImageView : public View { public: ZoomableImageView(BitmapPtr bitmap, std::shared_ptr zoom) : bitmap_(std::move(bitmap)), zoom_(std::move(zoom)) {} bool acceptsPress() const override { return true; } bool wantsDrag() const override { return true; } bool scrollable() const override { return true; } void hits(Point p, std::vector& out) override { if (frame_.contains(p)) out.push_back(this); } void onDragMove(Point, Point delta) override { zoom_->pan(delta.x, delta.y); } bool onScrollWheel(float, float dy) override { if (!(SDL_GetModState() & SDL_KMOD_CTRL) || dy == 0) return false; zoom_->zoomBy(dy < 0 ? ComposeSpec::kZoomWheelStep : 1.0f / ComposeSpec::kZoomWheelStep); return true; } protected: Size compute(const Proposal& p) override { return {p.w && *p.w < kInf / 2 ? *p.w : 0.0f, p.h && *p.h < kInf / 2 ? *p.h : 0.0f}; } public: void draw(Canvas& c) override { const float bw = static_cast(bitmap_->width()), bh = static_cast(bitmap_->height()); const float fit = std::min(frame_.w / bw, frame_.h / bh); const float w = bw * fit * zoom_->scale, h = bh * fit * zoom_->scale; c.pushClip(frame_); c.drawBitmap(*bitmap_, {frame_.x + (frame_.w - w) / 2 + zoom_->offsetX, frame_.y + (frame_.h - h) / 2 + zoom_->offsetY, w, h}); c.popClip(); } private: BitmapPtr bitmap_; std::shared_ptr zoom_; }; ViewPtr zoomButton(const std::string& symbol, const std::string& label, std::function action) { ViewPtr icon = Icon(symbol, 20)->foreground(Color::rgb(0xFFFFFF))->frame(ComposeSpec::kZoomButtonSize, ComposeSpec::kZoomButtonSize) ->background(Color{0, 0, 0, 120}, Corners::all(ComposeSpec::kZoomButtonSize / 2)); return Button(icon, std::move(action), ButtonStyles::plain())->id(label); } } // namespace FullscreenImageViewerScreen::FullscreenImageViewerScreen(std::string url) : url_(std::move(url)), zoom_(std::make_shared()), alive_(std::make_shared(true)) {} ViewPtr FullscreenImageViewerScreen::body() { BitmapPtr bmp = RemoteImages::instance().get(url_, ComposeSpec::kViewerRasterPx); if (!bmp) return ProgressSpinner(32)->maxSize()->background(Palette::bg0()); auto zoom = zoom_; ViewPtr controls = HStack({zoomButton("minus.circle.fill", "zoom-out", [zoom] { zoom->zoomBy(1.0f / ComposeSpec::kZoomStep); }), Text(std::to_string(static_cast(std::lround(zoom_->scale * 100))) + "%") ->font(FontSpec::footnote().with(Weight::Semibold))->foreground(Color::rgb(0xFFFFFF)) ->paddingH(10)->paddingV(6)->background(Color{0, 0, 0, 120}, Corners::all(14)), zoomButton("plus.circle.fill", "zoom-in", [zoom] { zoom->zoomBy(ComposeSpec::kZoomStep); })}, 10) ->paddingBottom(16); return ZStack({std::make_shared(bmp, zoom_)->maxSize(), controls->maxSize(Alignment::bottom())}) ->maxSize()->background(Palette::bg0()); } ViewPtr FullscreenImageViewerScreen::toolbarLeading() { return ToolbarTextButton(ComposeSpec::kDone, [this] { Overlays::instance().dismissSheetContaining(this); }); } ViewPtr FullscreenImageViewerScreen::toolbarTrailing() { if (!RemoteImages::instance().get(url_, ComposeSpec::kViewerRasterPx)) return nullptr; return OnTapRect(Icon("square.and.arrow.up", 17)->foreground(Palette::fg1())->frame(36, 36) ->background(Palette::fill(), Corners::all(18)), [this](Rect anchor) { share(anchor); }); } void FullscreenImageViewerScreen::share(Rect anchor) { const std::string url = url_; auto alive = alive_; Overlays::instance().menu(anchor, { MenuItem{ComposeSpec::kShareSave, "square.and.arrow.up", false, false, [this, alive] { if (*alive) save(); }}, MenuItem{ComposeSpec::kShareCopy, "link", false, false, [url] { Clipboard::set(url); Overlays::instance().toast(ComposeSpec::kCopied); }}}); } void FullscreenImageViewerScreen::save() { std::string name = AttachmentFileDialogs::lastPathComponent(url_); if (name.empty()) name = ComposeSpec::kSaveTitle; const std::string url = url_; AttachmentFileDialogs::saveFile(name, [url](const std::string& path) { if (path.empty()) return; HttpRequest req; req.path = url; req.maxResponseBytes = ComposeSpec::kMaxSaveBytes; NetworkService::instance().send(req, [path](Result r) { const bool ok = r.ok() && r.value().status >= 200 && r.value().status < 300 && AttachmentFileDialogs::writeFile(path, r.value().body); Overlays::instance().toast(ok ? ComposeSpec::kSaved : ComposeSpec::kSaveFailed); }); }); } } // namespace dp