#include "mologram/ui/media/ImageViewer.hpp" namespace mologram::ui::media { using namespace ttgui; ViewerWidget::ViewerWidget(MediaImageSource& source, std::vector items, size_t index) : source_(source), items_(std::move(items)), index_(items_.empty() ? 0 : std::min(index, items_.size() - 1)) { image_ = emplace(); image_->source = &source_; focusable = true; focusPolicy = FocusPolicy::Strong; show(); } void ViewerWidget::show() { if (items_.empty()) return; elapsedMs_ = 0; if (onShown) onShown(items_[index_]); image_->imageId = items_[index_].id; source_.download(items_[index_].id); // the viewer always wants the full file if (index_ + 1 < items_.size()) { source_.download(items_[index_ + 1].id); source_.request(items_[index_ + 1].id); } // prefetch neighbours if (index_ > 0) { source_.download(items_[index_ - 1].id); source_.request(items_[index_ - 1].id); } } void ViewerWidget::go(int delta) { if (items_.empty()) return; long long n = (long long)index_ + delta; if (n < 0 || n >= (long long)items_.size()) return; index_ = (size_t)n; show(); } bool ViewerWidget::onEvent(ttgui::Event& e) { if (e.type == ttgui::EventType::Tick && autoMs > 0 && !paused_ && !items_.empty()) { elapsedMs_ += e.dt * 1000.0; if (elapsedMs_ >= (double)autoMs) { if (index_ + 1 < items_.size()) go(1); else if (onClose) onClose(); elapsedMs_ = 0; } } return Widget::onEvent(e); } void ViewerWidget::layout() { image_->cfg = cfg; image_->bounds = {0, 0, bounds.w, std::max(1, bounds.h - 1)}; image_->layout(); } void ViewerWidget::draw(Canvas& c) { layout(); Rect a = absoluteBounds(); CellStyle s; s.fg = Color::rgb(255, 255, 255); s.bg = Color::rgb(20, 20, 20); c.fill(a, " ", s); CellStyle bar = s; bar.bg = Color::rgb(60, 60, 60); c.fill({a.x, a.bottom() - 1, a.w, 1}, " ", bar); if (items_.empty()) return; std::string left = items_[index_].title.empty() ? "(untitled)" : items_[index_].title; std::string pos = " " + std::to_string(index_ + 1) + "/" + std::to_string(items_.size()); std::string keys = autoMs > 0 ? "Esc close browse space pause o open s save" : "Esc close browse f fit o open s save"; std::string note; std::string why = source_.failure(items_[index_].id); if (!why.empty()) { note = why; if (items_[index_].video) note += " (press o to play it with the system player)"; } if (!extraKeys.empty()) keys += " " + extraKeys; std::string text = left + pos + (paused_ ? " [paused]" : "") + " " + (note.empty() ? keys : note); if (autoMs > 0 && !paused_) { // progress of the current slide across the footer int w = (int)(std::min(1.0, elapsedMs_ / (double)autoMs) * a.w); CellStyle fill = bar; fill.bg = Color::rgb(36, 80, 180); c.fill({a.x, a.bottom() - 1, w, 1}, " ", fill); } c.drawText(a.x, a.bottom() - 1, ellipsize(text, a.w), bar, a.w); } bool ViewerWidget::onKey(KeyEventData& k) { if (k.code == KeyCode::Escape || (k.isChar() && (k.text == "q" || k.text == "Q"))) { if (onClose) onClose(); return true; } if (k.code == KeyCode::Left || k.code == KeyCode::PageUp || (k.isChar() && k.text == "p")) { go(-1); return true; } if (k.code == KeyCode::Right || k.code == KeyCode::PageDown || (k.isChar() && k.text == "n") || (k.isChar() && k.text == " " && autoMs == 0)) { go(1); return true; } if (k.isChar() && k.text == " " && autoMs > 0) { paused_ = !paused_; return true; } if (k.isChar() && k.text == "f") { image_->fit = image_->fit == ImageFit::Contain ? ImageFit::Cover : ImageFit::Contain; return true; } if (k.isChar() && k.text == "o") { if (onOpenExternal && !items_.empty()) onOpenExternal(items_[index_]); return true; } if (k.isChar() && k.text == "s") { if (onSave && !items_.empty()) onSave(items_[index_]); return true; } if (onExtraKey && !items_.empty() && onExtraKey(k, items_[index_])) return true; return false; } bool ViewerWidget::onMouse(MouseEventData& m) { Rect a = absoluteBounds(); if (m.kind == MouseKind::DoubleClick) { if (onClose) onClose(); return true; } if (m.kind == MouseKind::Wheel) { go(m.wheelDelta > 0 ? -1 : 1); return true; } if (m.kind == MouseKind::Down && m.button == 0 && m.pos.y < a.bottom() - 1) { int third = std::max(1, a.w / 3); if (m.pos.x < a.x + third) { go(-1); return true; } if (m.pos.x >= a.right() - third) { go(1); return true; } } return Widget::onMouse(m); } Window* ImageViewer::open(Application& app, MediaImageSource& source, std::vector items, size_t index, std::function openExternal, std::function save) { if (items.empty()) return nullptr; std::string title = items[std::min(index, items.size() - 1)].title; Window* win = app.wm.createWindow(title.empty() ? "Viewer" : title, "grey", app.wm.workArea); win->appName = "ImageViewer"; win->maximized = true; win->popFrames = 0; auto* v = new ViewerWidget(source, std::move(items), index); v->cfg = &app.cfg; v->onClose = [&app, win]() { app.wm.closeWindow(win); }; v->onOpenExternal = std::move(openExternal); v->onSave = std::move(save); v->image()->occluded = [&app, win](Rect r) { return ImageWidget::occludedByWindows(app.wm, win, r); }; win->setContent(std::unique_ptr(v)); win->bounds = app.wm.workArea; win->layout(); app.setFocused(v); return win; } } // namespace mologram::ui::media