Skip to content
Repos

DevPlaceApp

C++ 2.6 KB83 lines Opens in site

Directory Raw Download

#pragma once
// Spec: SPEC.md FR-SHELL-01, UX-HOST-01. NavigationStack: Screen objects (presenter plus body) pushed on a
// Navigator; the Navigator builds the chrome (bar, back button, titles) and animates push and pop.
#include <memory>
#include <string>
#include <vector>

#include "ui/Views.hpp"

namespace dp::ui {

class Navigator;

class Screen {
 public:
  enum class TitleMode { Large, Inline };

  virtual ~Screen() = default;
  // Content below the bar. Rebuilt every frame.
  virtual ViewPtr body() = 0;
  virtual std::string title() const { return ""; }
  virtual TitleMode titleMode() const { return TitleMode::Large; }
  virtual ViewPtr toolbarLeading() { return nullptr; }
  virtual ViewPtr toolbarTrailing() { return nullptr; }
  virtual bool hidesBar() const { return false; }
  virtual void onAppear() {}
  virtual void onDisappear() {}
  virtual void onReselect() {}  // tab reselect while this screen is the root
  virtual bool busy() const { return false; }

  Navigator* navigator() const { return navigator_; }

 private:
  friend class Navigator;
  Navigator* navigator_ = nullptr;
};

using ScreenPtr = std::shared_ptr<Screen>;

class Navigator {
 public:
  explicit Navigator(ScreenPtr root);

  void push(ScreenPtr screen);
  bool pop();
  void popToRoot();
  void replaceStack(std::vector<ScreenPtr> screens);  // no animation
  std::size_t depth() const { return stack_.size(); }
  Screen* top() const { return stack_.empty() ? nullptr : stack_.back().get(); }
  Screen* root() const { return stack_.empty() ? nullptr : stack_.front().get(); }
  void reselect();
  bool busy() const;

  // Builds the whole stack UI (bar plus content, with transition).
  ViewPtr view();
  // Back button / edge swipe progress hook used by the chrome.
  void edgeSwipeEnded(float dx);
  bool ownsScreen(const Screen* screen) const;

 private:
  struct Transition {
    bool active = false;
    bool push = true;
    ScreenPtr other;  // screen leaving (pop) or covered (push)
    double start = 0;
  };
  ViewPtr page(Screen& s, bool showBack);
  ViewPtr bar(Screen& s, bool showBack);
  void syncLifecycle();

  std::vector<ScreenPtr> stack_;
  Transition tr_;
  Screen* lastTop_ = nullptr;
};

// ---- chrome helpers screens use ----
ViewPtr NavLargeTitle(const std::string& title);  // first row of a large-title screen's content
ViewPtr ToolbarIconButton(const std::string& symbol, std::function<void()> action);
ViewPtr ToolbarTextButton(const std::string& title, std::function<void()> action, bool prominent = false);
// Content centered in a column of at most spec::Layout::kMaxContentWidth (UX-RESP-01).
ViewPtr ContentColumn(ViewPtr content);

}  // namespace dp::ui
43.1ms