#include "ui/Overlays.hpp" #include #include #include "spec/Layout.hpp" #include "ui/Palette.hpp" namespace dp::ui { using spec::Layout; namespace { float easeOut(float t) { return 1.0f - (1.0f - t) * (1.0f - t) * (1.0f - t); } constexpr std::size_t kMaxEntries = 8; constexpr std::size_t kMaxToasts = 3; constexpr double kToastSeconds = 2.2; Color roleColor(AlertButton::Role r) { return r == AlertButton::Role::Destructive ? Palette::red() : Palette::accent(); } ViewPtr hairline(bool horizontal) { return horizontal ? Divider(Palette::separator(), 0.5f) : RectangleShape(Palette::separator())->frame(0.5f, std::nullopt); } } // namespace Overlays& Overlays::instance() { static Overlays o; return o; } void Overlays::clear() { entries_.clear(); toasts_.clear(); } bool Overlays::busy() const { const double now = UiState::instance().now; for (const auto& e : entries_) { if (e->kind == Entry::Kind::Sheet && e->nav && e->nav->busy()) return true; if (now - e->start < Layout::kSheetSeconds) return true; } return !toasts_.empty(); } void Overlays::alert(AlertSpec spec) { if (entries_.size() >= kMaxEntries) return; auto e = std::make_shared(); e->kind = Entry::Kind::Alert; if (spec.buttons.empty()) spec.buttons.push_back({"OK", AlertButton::Role::Cancel, nullptr}); e->alert = std::move(spec); for (std::size_t i = 0; i < e->alert.fields.size(); ++i) e->handles.push_back(std::make_shared()); e->start = UiState::instance().now; entries_.push_back(std::move(e)); } void Overlays::confirmationDialog(std::string title, std::string message, std::vector buttons) { if (entries_.size() >= kMaxEntries) return; auto e = std::make_shared(); e->kind = Entry::Kind::Dialog; e->alert.title = std::move(title); e->alert.message = std::move(message); e->alert.buttons = std::move(buttons); e->start = UiState::instance().now; entries_.push_back(std::move(e)); } void Overlays::sheet(ScreenPtr root, SheetStyle style, std::function onDismiss) { if (entries_.size() >= kMaxEntries) return; auto e = std::make_shared(); e->kind = Entry::Kind::Sheet; e->nav = std::make_shared(std::move(root)); e->style = style; e->onDismiss = std::move(onDismiss); e->start = UiState::instance().now; entries_.push_back(std::move(e)); } void Overlays::menu(Rect anchor, std::vector items) { if (entries_.size() >= kMaxEntries || items.empty()) return; auto e = std::make_shared(); e->kind = Entry::Kind::Menu; e->items = std::move(items); e->anchor = anchor; e->start = UiState::instance().now; entries_.push_back(std::move(e)); } void Overlays::popover(Rect anchor, Size size, std::function builder) { if (entries_.size() >= kMaxEntries || !builder) return; auto e = std::make_shared(); e->kind = Entry::Kind::Popover; e->anchor = anchor; e->popSize = size; e->builder = std::move(builder); e->start = UiState::instance().now; entries_.push_back(std::move(e)); } void Overlays::toast(std::string text) { if (toasts_.size() >= kMaxToasts) toasts_.erase(toasts_.begin()); toasts_.push_back({std::move(text), UiState::instance().now}); } void Overlays::close(std::size_t index) { if (index >= entries_.size()) return; auto e = entries_[index]; if (e->kind == Entry::Kind::Sheet) { if (e->closeStart >= 0) return; e->closeStart = UiState::instance().now; if (e->onDismiss) e->onDismiss(); return; } entries_.erase(entries_.begin() + static_cast(index)); } bool Overlays::dismissTop() { for (std::size_t i = entries_.size(); i > 0; --i) { Entry& e = *entries_[i - 1]; if (e.closeStart >= 0) continue; if (e.kind == Entry::Kind::Alert) { for (const auto& b : e.alert.buttons) { if (b.role == AlertButton::Role::Cancel) { auto action = b.action; close(i - 1); if (action) action(); return true; } } close(i - 1); return true; } if (e.kind == Entry::Kind::Sheet && e.nav && e.nav->depth() > 1) { e.nav->pop(); return true; } close(i - 1); return true; } return false; } void Overlays::dismissSheetContaining(const Screen* screen) { for (std::size_t i = 0; i < entries_.size(); ++i) { Entry& e = *entries_[i]; if (e.kind != Entry::Kind::Sheet || !e.nav) continue; if (e.nav->root() == screen || e.nav->ownsScreen(screen)) { close(i); return; } } } // ---- views ---- ViewPtr Overlays::alertView(Entry& e) { const AlertSpec& a = e.alert; std::vector body; if (!a.title.empty()) body.push_back(Text(a.title)->font(FontSpec::headline())->multilineAlign(TextAlign::Center)->maxWidth()); if (!a.message.empty()) body.push_back(Text(a.message)->font(FontSpec::footnote())->multilineAlign(TextAlign::Center)->maxWidth()); for (std::size_t i = 0; i < a.fields.size(); ++i) { const AlertField& f = a.fields[i]; auto text = f.text; TextInputOptions o; o.placeholder = f.placeholder; o.secure = f.secure; o.handle = e.handles[i]; ViewPtr field = TextField(Binding{[text] { return *text; }, [text](const std::string& v) { *text = v; }}, o) ->padding(8) ->background(Palette::bg2(), Corners::all(8)) ->border(Palette::fg2(), 8, 1, 0.3f); body.push_back(field); } if (!e.fieldsFocused && !e.handles.empty() && e.handles[0]->id != 0) { e.handles[0]->focus(); e.fieldsFocused = true; } ViewPtr top = VStack(body, 6)->padding(Insets{20, 16, 16, 16}); std::vector buttons; Entry* self = &e; auto make = [this, self](const AlertButton& b) { ViewPtr label = Text(b.title)->weight(b.role == AlertButton::Role::Cancel ? Weight::Semibold : Weight::Regular) ->foreground(roleColor(b.role))->frame(std::nullopt, 44)->maxWidth(); auto action = b.action; return Button(label, [this, self, action] { std::size_t idx = entries_.size(); for (std::size_t i = 0; i < entries_.size(); ++i) if (entries_[i].get() == self) idx = i; if (idx < entries_.size()) close(idx); if (action) action(); }, ButtonStyles::plain()); }; ViewPtr buttonArea; if (a.buttons.size() == 2) { buttonArea = HStack({make(a.buttons[0])->layoutPriority(0), hairline(false)->frame(0.5f, 44), make(a.buttons[1])}, 0); } else { std::vector col; for (std::size_t i = 0; i < a.buttons.size(); ++i) { if (i) col.push_back(hairline(true)); col.push_back(make(a.buttons[i])); } buttonArea = VStack(col, 0); } ViewPtr card = VStack({top, hairline(true), buttonArea}, 0) ->frame(Layout::kAlertWidth, std::nullopt) ->background(Palette::bg1(), Corners::all(Layout::kAlertRadius)) ->clipRounded(Layout::kAlertRadius) ->border(Palette::separator(), Layout::kAlertRadius, 0.5f) ->shadow(Color{0, 0, 0, 80}, 20, 0, 8, Corners::all(Layout::kAlertRadius)); return card; } ViewPtr Overlays::dialogView(Entry& e) { Entry* self = &e; auto closeSelf = [this, self] { for (std::size_t i = 0; i < entries_.size(); ++i) if (entries_[i].get() == self) { close(i); return; } }; std::vector rows; ViewPtr cancel; if (!e.alert.title.empty() || !e.alert.message.empty()) { std::vector head; if (!e.alert.title.empty()) head.push_back(Text(e.alert.title)->font(FontSpec::footnote().with(Weight::Semibold))->foreground(Palette::fg2())->multilineAlign(TextAlign::Center)->maxWidth()); if (!e.alert.message.empty()) head.push_back(Text(e.alert.message)->font(FontSpec::footnote())->foreground(Palette::fg2())->multilineAlign(TextAlign::Center)->maxWidth()); rows.push_back(VStack(head, 4)->padding(14)); } for (const AlertButton& b : e.alert.buttons) { auto action = b.action; ViewPtr label = Text(b.title)->font(FontSpec::title3())->foreground(roleColor(b.role))->frame(std::nullopt, 56)->maxWidth(); ViewPtr btn = Button(label, [closeSelf, action] { closeSelf(); if (action) action(); }, ButtonStyles::plain()); if (b.role == AlertButton::Role::Cancel) cancel = btn; else { rows.push_back(hairline(true)); rows.push_back(btn); } } ViewPtr group = VStack(rows, 0)->background(Palette::bg1(), Corners::all(Layout::kAlertRadius))->clipRounded(Layout::kAlertRadius); std::vector col{group}; if (cancel) col.push_back(cancel->background(Palette::bg1(), Corners::all(Layout::kAlertRadius))->clipRounded(Layout::kAlertRadius)); return VStack(col, 8)->frameFlex(std::nullopt, std::nullopt, 420, std::nullopt, std::nullopt, std::nullopt)->padding(12); } ViewPtr Overlays::sheetView(Entry& e, Size window) { const double now = UiState::instance().now; float t = std::clamp(static_cast((now - e.start) / Layout::kSheetSeconds), 0.0f, 1.0f); float slide = 1.0f - easeOut(t); if (e.closeStart >= 0) { const float c = std::clamp(static_cast((now - e.closeStart) / (Layout::kSheetSeconds * 0.7)), 0.0f, 1.0f); slide = easeOut(c); t = 1.0f - c; } ViewPtr content = e.nav->view(); if (e.style == SheetStyle::FullScreen) { return content->maxSize()->background(Palette::bg2())->offsetFraction(0, slide); } const float width = std::min(window.w, 640.0f); const float top = window.w > 700 ? 40.0f : Layout::kSheetTopGap; ViewPtr panel = content->frame(width, window.h - top) ->background(Palette::bg2(), Corners{Layout::kSheetRadius, Layout::kSheetRadius, 0, 0}) ->clipRounded(Layout::kSheetRadius) ->shadow(Color{0, 0, 0, 70}, 16, 0, -2, Corners::all(Layout::kSheetRadius)); return panel->offsetFraction(0, slide)->maxSize(Alignment::bottom()); } ViewPtr Overlays::menuView(Entry& e, Size window) { Entry* self = &e; auto closeSelf = [this, self] { for (std::size_t i = 0; i < entries_.size(); ++i) if (entries_[i].get() == self) { close(i); return; } }; std::vector rows; for (std::size_t i = 0; i < e.items.size(); ++i) { const MenuItem& m = e.items[i]; if (i) rows.push_back(Divider(Palette::separator(), 0.5f)); auto action = m.action; ViewPtr row = HStack({m.checked ? Icon("checkmark", 14)->weight(Weight::Semibold) : Icon("checkmark", 14)->opacity(0.0f), Text(m.title)->lineLimit(1)->foreground(m.destructive ? Palette::red() : Palette::fg1())->maxWidth(Alignment::leading()), m.icon.empty() ? EmptyView() : Icon(m.icon, 16)->foreground(m.destructive ? Palette::red() : Palette::fg1())}, 10) ->paddingH(14)->frame(std::nullopt, Layout::kMenuRowHeight)->maxWidth(); rows.push_back(Button(row, [closeSelf, action] { closeSelf(); if (action) action(); }, ButtonStyles::plain())); } const float w = Layout::kMenuWidth; const float h = static_cast(e.items.size()) * Layout::kMenuRowHeight; float x = std::clamp(e.anchor.right() - w, 8.0f, std::max(8.0f, window.w - w - 8.0f)); float y = e.anchor.bottom() + 6; if (y + h > window.h - 8) y = std::max(8.0f, e.anchor.y - h - 6); ViewPtr card = VStack(rows, 0)->frame(w, std::nullopt)->background(Palette::bg1(), Corners::all(Layout::kMenuRadius)) ->clipRounded(Layout::kMenuRadius)->border(Palette::separator(), Layout::kMenuRadius, 0.5f) ->shadow(Color{0, 0, 0, 70}, 16, 0, 6, Corners::all(Layout::kMenuRadius)); return card->offset(x, y)->maxSize(Alignment::topLeading()); } ViewPtr Overlays::popoverView(Entry& e, Size window) { const float w = e.popSize.w, h = e.popSize.h; const float x = std::clamp(e.anchor.right() - w, 8.0f, std::max(8.0f, window.w - w - 8.0f)); float y = e.anchor.bottom() + 6; if (y + h > window.h - 8) y = std::max(8.0f, e.anchor.y - h - 6); ViewPtr card = e.builder()->frame(w, h)->background(Palette::bg1(), Corners::all(Layout::kMenuRadius)) ->clipRounded(Layout::kMenuRadius)->border(Palette::separator(), Layout::kMenuRadius, 0.5f) ->shadow(Color{0, 0, 0, 70}, 16, 0, 6, Corners::all(Layout::kMenuRadius)); return card->offset(x, y)->maxSize(Alignment::topLeading()); } ViewPtr Overlays::view(Size window) { UiState& ui = UiState::instance(); // Drop finished sheets and expired toasts. for (std::size_t i = entries_.size(); i > 0; --i) { Entry& e = *entries_[i - 1]; if (e.kind == Entry::Kind::Sheet && e.closeStart >= 0 && ui.now - e.closeStart > Layout::kSheetSeconds * 0.7) { entries_.erase(entries_.begin() + static_cast(i - 1)); } } toasts_.erase(std::remove_if(toasts_.begin(), toasts_.end(), [&](const Toast& t) { return ui.now - t.start > kToastSeconds; }), toasts_.end()); std::vector layers; for (std::size_t i = 0; i < entries_.size(); ++i) { Entry& e = *entries_[i]; const float fade = std::clamp(static_cast((ui.now - e.start) / Layout::kFadeSeconds), 0.0f, 1.0f); if (fade < 1.0f) ui.animating = true; const std::size_t index = i; switch (e.kind) { case Entry::Kind::Alert: { layers.push_back(RectangleShape(Color{0, 0, 0, 110})->opacity(fade)->maxSize()); layers.push_back(alertView(e)->opacity(fade)->maxSize()); break; } case Entry::Kind::Dialog: { layers.push_back(RectangleShape(Color{0, 0, 0, 110})->opacity(fade)->maxSize()->onTap([this, index] { close(index); })); layers.push_back(dialogView(e)->maxSize(Alignment::bottom())->opacity(fade)); break; } case Entry::Kind::Sheet: { if (e.style == SheetStyle::Sheet) { const bool closing = e.closeStart >= 0; layers.push_back(RectangleShape(Color{0, 0, 0, 110})->opacity(closing ? 0.0f : fade)->maxSize()->onTap([this, index] { close(index); })); } layers.push_back(sheetView(e, window)); ui.animating = ui.animating || ui.now - e.start < Layout::kSheetSeconds || e.closeStart >= 0; break; } case Entry::Kind::Popover: { layers.push_back(RectangleShape(Color{0, 0, 0, 1})->maxSize()->onTap([this, index] { close(index); })); layers.push_back(popoverView(e, window)->opacity(fade)); break; } case Entry::Kind::Menu: { layers.push_back(RectangleShape(Color{0, 0, 0, 1})->maxSize()->onTap([this, index] { close(index); })); layers.push_back(menuView(e, window)->opacity(fade)); break; } } } if (!toasts_.empty()) { std::vector col; for (const Toast& t : toasts_) { col.push_back(Text(t.text)->font(FontSpec::footnote().with(Weight::Medium))->foreground(Palette::fg1())->padding(Insets::symmetric(14, 9)) ->background(Palette::bg1(), Corners::all(18))->border(Palette::separator(), 18, 0.5f) ->shadow(Color{0, 0, 0, 50}, 8, 0, 2, Corners::all(18))); } layers.push_back(VStack(col, 6)->paddingBottom(96)->maxSize(Alignment::bottom())); ui.animating = true; } if (layers.empty()) return EmptyView(); return ZStack(std::move(layers), Alignment::topLeading())->maxSize(Alignment::topLeading()); } } // namespace dp::ui