Skip to content
Repos

DevPlaceApp

C++ 3.9 KB93 lines Opens in site

Directory Raw Download

#pragma once
// Spec: SPEC.md UX-VIEWS-01. SwiftUI primitives as factory functions returning ViewPtr.
#include <functional>
#include <initializer_list>

#include "ui/Bitmap.hpp"
#include "ui/View.hpp"

namespace dp::ui {

// ---- Text ----
struct StyledSpan {
  std::string text;
  std::optional<FontSpec> font;
  std::optional<Color> color;
  bool underline = false;
  bool strikethrough = false;
  std::optional<Color> background;
  std::string link;
};

using LinkHandler = std::function<void(const std::string& url)>;
void setDefaultLinkHandler(LinkHandler handler);
void invokeLinkHandler(const std::string& url);

ViewPtr Text(const std::string& text);
ViewPtr RichText(std::vector<StyledSpan> spans);

// ---- Images ----
enum class ContentMode { Fit, Fill };
ViewPtr Icon(const std::string& symbol, float size = 17);  // tinted by foreground, sized in points
ViewPtr ImageView(std::function<BitmapPtr()> provider, ContentMode mode = ContentMode::Fit,
                  ViewPtr placeholder = nullptr);  // resizable; aspect ratio preserved
ViewPtr BitmapView(BitmapPtr bitmap);

// ---- Shapes ----
ViewPtr RectangleShape(Color fill);
ViewPtr RoundedRectShape(float radius, Color fill);
ViewPtr CircleShape(Color fill);
ViewPtr CapsuleShape(Color fill);
ViewPtr GradientRoundedRect(float radius, Color from, Color to, Point start, Point end);
ViewPtr GradientCircle(Color from, Color to, Point start, Point end);
ViewPtr StrokedRoundedRect(float radius, Color color, float width, float opacity = 1);
ViewPtr Spacer(float minLength = 0);
ViewPtr Divider(Color color, float thickness = 0.5f);

// ---- Containers ----
ViewPtr VStack(std::vector<ViewPtr> children, float spacing = 8, HAlign align = HAlign::Center);
ViewPtr HStack(std::vector<ViewPtr> children, float spacing = 8, VAlign align = VAlign::Center);
ViewPtr ZStack(std::vector<ViewPtr> children, Alignment align = Alignment::center());
ViewPtr EmptyView();
// Flattens into the parent stack (SwiftUI Group / ForEach).
ViewPtr Group(std::vector<ViewPtr> children);
// Choose the first child whose ideal width fits (SwiftUI ViewThatFits, horizontal axis only).
ViewPtr ViewThatFits(std::vector<ViewPtr> options);

// ---- Scrolling ----
struct ScrollOptions {
  Axis axis = Axis::Vertical;
  bool showsIndicator = true;
  std::function<void()> onRefresh;  // pull to refresh (vertical only)
  bool refreshing = false;
  Insets contentInset;
  std::string scrollKey;  // programmatic scroll target (ScrollCommand)
  // defaultScrollAnchor(.bottom): content shorter than the viewport sits at the bottom, the first layout shows
  // the bottom, and growing content stays pinned while the view is within `anchorThreshold` of the bottom.
  bool anchorBottom = false;
  float anchorThreshold = 80;
  // onScrollGeometryChange equivalent: called after every layout pass with offset, maximum offset and viewport.
  std::function<void(float offset, float maxOffset, float viewport)> onGeometry;
};
ViewPtr ScrollView(ViewPtr content, ScrollOptions options = {});
// Programmatic scroll requests, consumed by the next layout pass (ScrollViewReader).
struct ScrollCommand {
  static void toTop(const std::string& scrollKey);
  static void toOffset(const std::string& scrollKey, float y);
};

// ---- Buttons ----
using ButtonStyleFn = std::function<ViewPtr(ViewPtr label, bool pressed)>;
struct ButtonStyles {
  static ButtonStyleFn automatic();   // accent tinted label, dims while pressed
  static ButtonStyleFn plain();       // label as is, dims while pressed
  static ButtonStyleFn form();        // FormButtonStyle: leading aligned, gray while pressed
  static ButtonStyleFn accentGradient(bool circle = false);
  static ButtonStyleFn none();        // no pressed feedback
};
ViewPtr Button(ViewPtr label, std::function<void()> action, ButtonStyleFn style = ButtonStyles::automatic());
ViewPtr Button(const std::string& title, std::function<void()> action, ButtonStyleFn style = ButtonStyles::automatic());

ViewPtr ProgressSpinner(float size = 20);

}  // namespace dp::ui
29.0ms