Skip to content
Repos

DevPlaceApp

C++ 2.8 KB98 lines Opens in site

Directory Raw Download

#pragma once
// Spec: SPEC.md UX-HOST-01. Modal presentations: alerts, confirmation dialogs, sheets, full screen covers,
// popup menus and toasts. One global stack, drawn above the app tree.
#include <functional>
#include <memory>
#include <string>
#include <vector>

#include "ui/Navigation.hpp"
#include "ui/TextInput.hpp"

namespace dp::ui {

struct AlertButton {
  enum class Role { Normal, Cancel, Destructive };
  std::string title;
  Role role = Role::Normal;
  std::function<void()> action;
};

struct AlertField {
  std::string placeholder;
  bool secure = false;
  std::shared_ptr<std::string> text = std::make_shared<std::string>();
};

struct AlertSpec {
  std::string title;
  std::string message;
  std::vector<AlertButton> buttons;
  std::vector<AlertField> fields;
};

struct MenuItem {
  std::string title;
  std::string icon;  // optional symbol
  bool destructive = false;
  bool checked = false;
  std::function<void()> action;
};

enum class SheetStyle { Sheet, FullScreen };

class Overlays {
 public:
  static Overlays& instance();

  void alert(AlertSpec spec);
  // Bottom action sheet (SwiftUI confirmationDialog).
  void confirmationDialog(std::string title, std::string message, std::vector<AlertButton> buttons);
  void sheet(ScreenPtr root, SheetStyle style = SheetStyle::Sheet, std::function<void()> onDismiss = nullptr);
  void menu(Rect anchor, std::vector<MenuItem> items);
  // Popover card anchored to a rect; the builder runs every frame.
  void popover(Rect anchor, Size size, std::function<ViewPtr()> builder);
  void toast(std::string text);
  // Close the topmost presentation (Escape, Done). Returns false when nothing was open.
  bool dismissTop();
  // Close the sheet that hosts `screen` (screens call this from their own buttons).
  void dismissSheetContaining(const Screen* screen);
  bool any() const { return !entries_.empty(); }
  bool busy() const;
  void clear();

  ViewPtr view(Size window);

 private:
  struct Entry {
    enum class Kind { Alert, Dialog, Sheet, Menu, Popover } kind = Kind::Alert;
    std::function<ViewPtr()> builder;
    Size popSize;
    AlertSpec alert;
    std::vector<MenuItem> items;
    Rect anchor;
    std::shared_ptr<Navigator> nav;
    SheetStyle style = SheetStyle::Sheet;
    std::function<void()> onDismiss;
    double start = 0;
    double closeStart = -1;
    bool fieldsFocused = false;
    std::vector<std::shared_ptr<TextInputHandle>> handles;
  };
  struct Toast {
    std::string text;
    double start = 0;
  };

  ViewPtr alertView(Entry& e);
  ViewPtr dialogView(Entry& e);
  ViewPtr sheetView(Entry& e, Size window);
  ViewPtr menuView(Entry& e, Size window);
  ViewPtr popoverView(Entry& e, Size window);
  void close(std::size_t index);

  std::vector<std::shared_ptr<Entry>> entries_;
  std::vector<Toast> toasts_;
};

}  // namespace dp::ui
40.7ms