#include "ui/Controls.hpp" #include #include #include #include "spec/Layout.hpp" #include "ui/Modifiers.hpp" #include "ui/Palette.hpp" namespace dp::ui { using spec::Layout; namespace { float easeInOut(float t) { return t < 0.5f ? 2 * t * t : 1 - std::pow(-2 * t + 2, 2.0f) / 2; } class RectTapView : public WrapperView { public: RectTapView(ViewPtr c, std::function h) : WrapperView(std::move(c)), handler_(std::move(h)) {} bool acceptsPress() const override { return true; } void onTap(Point) override { if (handler_) handler_(frame_); } void hits(Point p, std::vector& out) override { if (!frame_.contains(p)) return; WrapperView::hits(p, out); if (!Env::cur().disabled) out.push_back(this); } private: std::function handler_; }; class SwitchView : public View { public: SwitchView(BoolBinding v, Color tint) : value_(std::move(v)), tint_(tint) {} bool acceptsPress() const override { return true; } void onTap(Point) override { if (disabled_) return; if (value_.set) value_.set(!value_.get()); Slot& s = UiState::instance().slot(id_); s.stamp = UiState::instance().now; } void assignIds(std::uint64_t parent, std::uint64_t index) override { View::assignIds(parent, index); disabled_ = Env::cur().disabled; } void hits(Point p, std::vector& out) override { if (frame_.contains(p) && !disabled_) out.push_back(this); } protected: Size compute(const Proposal&) override { return {Layout::kToggleWidth, Layout::kToggleHeight}; } public: void draw(Canvas& c) override { UiState& ui = UiState::instance(); Slot& s = ui.slot(id_); const bool on = value_.get ? value_.get() : false; const float target = on ? 1.0f : 0.0f; if (!s.flag) { // first frame: no animation s.anim = target; s.flag = true; } if (s.anim != target) { const float t = std::clamp(static_cast((ui.now - s.stamp) / Layout::kToggleSeconds), 0.0f, 1.0f); const float from = on ? 0.0f : 1.0f; s.anim = from + (target - from) * easeInOut(t); if (t >= 1.0f) s.anim = target; ui.animating = true; } const float p = s.anim; const Color off = Palette::fill().opacity(1.0f); const Color offSolid = Appearance::dark() ? Color::rgb(0x39393D) : Color::rgb(0xE9E9EA); (void)off; auto mix = [&](Color a, Color b) { return Color{static_cast(a.r + (b.r - a.r) * p), static_cast(a.g + (b.g - a.g) * p), static_cast(a.b + (b.b - a.b) * p), 255}; }; c.fillRRect(frame_, Corners::all(frame_.h / 2), mix(offSolid, tint_)); const float knob = frame_.h - 4; const float x = frame_.x + 2 + (frame_.w - knob - 4) * p; c.shadow({x, frame_.y + 2, knob, knob}, Corners::all(knob / 2), Color{0, 0, 0, 70}, 3, {0, 1.5f}); c.fillRRect({x, frame_.y + 2, knob, knob}, Corners::all(knob / 2), Color::rgb(0xFFFFFF)); if (disabled_) c.fillRRect(frame_, Corners::all(frame_.h / 2), Palette::bg2(), 0.5f); } private: BoolBinding value_; Color tint_; bool disabled_ = false; }; int daysIn(int year, unsigned month) { using namespace std::chrono; const year_month_day_last last{std::chrono::year{year} / std::chrono::month{month} / std::chrono::last}; return static_cast(static_cast(last.day())); } int weekdayOf(int year, unsigned month, unsigned day) { using namespace std::chrono; const sys_days d{std::chrono::year{year} / std::chrono::month{month} / std::chrono::day{day}}; return static_cast(weekday{d}.c_encoding()); // 0 = Sunday } const char* kMonthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; std::string formatDate(const CivilDate& d) { static const char* kShort[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; return std::string(kShort[(d.month - 1) % 12]) + " " + std::to_string(d.day) + ", " + std::to_string(d.year); } } // namespace ViewPtr OnTapRect(ViewPtr content, std::function handler) { return std::make_shared(std::move(content), std::move(handler)); } ViewPtr Switch(BoolBinding value, Color tint) { return std::make_shared(std::move(value), tint); } ViewPtr Toggle(ViewPtr label, BoolBinding value, Color tint) { return HStack({label->maxWidth(Alignment::leading()), Switch(std::move(value), tint)}, 12); } ViewPtr MenuPicker(ViewPtr label, std::vector options, int selectedTag, std::function onSelect) { std::string current; for (const auto& o : options) if (o.tag == selectedTag) current = o.title; ViewPtr trailing = HStack({Text(current)->foreground(Palette::fg2()), Icon("chevron.up.chevron.down", 13)->foreground(Palette::fg2())}, 4); ViewPtr row = HStack({label->maxWidth(Alignment::leading()), trailing}, 8); return OnTapRect(row, [options, selectedTag, onSelect](Rect anchor) { std::vector items; for (const auto& o : options) { const int tag = o.tag; items.push_back({o.title, "", false, tag == selectedTag, [onSelect, tag] { onSelect(tag); }}); } Overlays::instance().menu(anchor, std::move(items)); }); } CivilDate CivilDate::today() { using namespace std::chrono; const year_month_day ymd{floor(system_clock::now())}; return {static_cast(ymd.year()), static_cast(ymd.month()), static_cast(ymd.day())}; } bool CivilDate::after(const CivilDate& o) const { if (year != o.year) return year > o.year; if (month != o.month) return month > o.month; return day > o.day; } ViewPtr DatePicker(ViewPtr label, std::function get, std::function set, CivilDate maxDate) { ViewPtr chip = Text(formatDate(get()))->padding(Insets::symmetric(12, 7))->background(Palette::fill(), Corners::all(10)); ViewPtr row = HStack({label->maxWidth(Alignment::leading()), chip}, 8); return OnTapRect(row, [get, set, maxDate](Rect anchor) { auto view = std::make_shared(get()); // month being shown Overlays::instance().popover(anchor, Size{320, 340}, [view, get, set, maxDate] { const CivilDate selected = get(); std::vector grid; const int first = weekdayOf(view->year, view->month, 1); const int days = daysIn(view->year, view->month); std::vector weekdays; for (const char* w : {"S", "M", "T", "W", "T", "F", "S"}) { weekdays.push_back(Text(w)->font(FontSpec::footnote().with(Weight::Semibold))->foreground(Palette::fg2())->frame(40, 28)); } grid.push_back(HStack(weekdays, 0)); int day = 1; for (int row = 0; row < 6 && day <= days; ++row) { std::vector cells; for (int col = 0; col < 7; ++col) { const int index = row * 7 + col; if (index < first || day > days) { cells.push_back(EmptyView()->frame(40, 40)); continue; } const CivilDate d{view->year, view->month, static_cast(day)}; const bool future = d.after(maxDate); const bool sel = d == selected; ViewPtr cell = Text(std::to_string(day))->foreground(sel ? Palette::onGradient() : (future ? Palette::fg2().opacity(0.4f) : Palette::fg1())) ->frame(36, 36)->background(sel ? Palette::accent() : Color::clear(), Corners::all(18))->frame(40, 40); if (!future) cell = Button(cell, [set, d] { set(d); Overlays::instance().dismissTop(); }, ButtonStyles::plain()); cells.push_back(cell); ++day; } grid.push_back(HStack(cells, 0)); } auto shift = [view](int delta) { int m = static_cast(view->month) + delta; int y = view->year; while (m < 1) { m += 12; --y; } while (m > 12) { m -= 12; ++y; } view->year = y; view->month = static_cast(m); view->day = 1; }; ViewPtr header = HStack({Text(std::string(kMonthNames[(view->month - 1) % 12]) + " " + std::to_string(view->year))->font(FontSpec::headline()), Spacer(), Button(Icon("chevron.left", 16)->frame(36, 36), [shift] { shift(-1); }), Button(Icon("chevron.right", 16)->frame(36, 36), [shift] { shift(1); })}, 4)->paddingH(12); return VStack({header, VStack(grid, 0)}, 8, HAlign::Center)->paddingV(10); }); }); } ViewPtr FormRow(ViewPtr content) { return content->paddingH(Layout::kFormSideInset)->paddingV(11)->frameFlex(std::nullopt, std::nullopt, kInf, Layout::kFormRowMinHeight, std::nullopt, std::nullopt, Alignment::leading()); } ViewPtr GroupedCard(std::vector rows, Color background) { std::vector items; for (std::size_t i = 0; i < rows.size(); ++i) { if (i) items.push_back(Divider(Palette::separator(), 0.5f)->paddingLeading(Layout::kFormSideInset)); items.push_back(rows[i]); } return VStack(items, 0)->background(background, Corners::all(Layout::kFormRadius))->clipRounded(Layout::kFormRadius); } ViewPtr Form(std::vector sections, ScrollOptions scroll) { std::vector blocks; for (FormSection& s : sections) { if (s.rows.empty()) continue; std::vector block; if (!s.header.empty()) { block.push_back(Text(s.header)->font(FontSpec::footnote().with(Weight::Medium))->foreground(Palette::fg2())->maxWidth(Alignment::leading())->paddingH(Layout::kFormSideInset)->paddingBottom(2)); } std::vector rows; for (ViewPtr& r : s.rows) rows.push_back(FormRow(r)); block.push_back(GroupedCard(std::move(rows), Palette::bg1())->maxWidth()); if (!s.footer.empty()) { block.push_back(Text(s.footer)->font(FontSpec::footnote())->foreground(Palette::fg2())->maxWidth(Alignment::leading())->paddingH(Layout::kFormSideInset)); } blocks.push_back(VStack(block, 6, HAlign::Leading)); } ViewPtr content = VStack(blocks, 24, HAlign::Leading)->padding(Insets{8, 16, 24, 16}); return ScrollView(content->maxWidth(Alignment::top()), std::move(scroll)); } ViewPtr TabView(std::vector tabs, int selected, std::function onSelect, std::function onReselect) { const int count = static_cast(tabs.size()); selected = std::clamp(selected, 0, std::max(0, count - 1)); ViewPtr content = tabs.empty() ? EmptyView() : tabs[static_cast(selected)].content(); std::vector items; for (int i = 0; i < count; ++i) { const TabItem& t = tabs[static_cast(i)]; const bool sel = i == selected; const Color col = sel ? Palette::accent() : Palette::fg2(); ViewPtr icon = Icon(t.icon, Layout::kTabIconSize)->foreground(col)->frame(Layout::kTabIconSize + 6, Layout::kTabIconSize + 2); if (t.badge > 0) { const std::string txt = t.badge > 99 ? "99+" : std::to_string(t.badge); ViewPtr badge = Text(txt)->font(FontSpec::system(11, Weight::Semibold))->foreground(Color::rgb(0xFFFFFF))->paddingH(5) ->frame(std::nullopt, 17)->frameFlex(17, std::nullopt, std::nullopt, std::nullopt, std::nullopt, std::nullopt) ->background(Palette::red(), Corners::all(8.5f)); icon = icon->overlay(badge->offset(10, -6), Alignment::topTrailing()); } ViewPtr item = VStack({icon, Text(t.title)->font(FontSpec::system(Layout::kTabLabelSize, Weight::Medium))->foreground(col)->lineLimit(1)}, 2) ->maxSize()->background(sel ? Palette::fill() : Color::clear(), Corners::all(26)); auto sel_cb = onSelect; auto re_cb = onReselect; items.push_back(Button(item, [i, selected, sel_cb, re_cb] { if (i == selected) { if (re_cb) re_cb(i); } else if (sel_cb) sel_cb(i); }, ButtonStyles::none())->id("tab" + std::to_string(i))); } ViewPtr bar = HStack(items, 4)->padding(4)->frame(std::nullopt, Layout::kTabBarHeight) ->background(Palette::bg1(), Corners::all(Layout::kTabBarHeight / 2)) ->border(Palette::separator(), Layout::kTabBarHeight / 2, 0.5f) ->shadow(Color{0, 0, 0, 50}, 12, 0, 4, Corners::all(Layout::kTabBarHeight / 2)) ->frameFlex(std::nullopt, std::nullopt, 480, std::nullopt, std::nullopt, std::nullopt) ->paddingH(Layout::kTabBarSideInset)->paddingBottom(Layout::kTabBarBottomInset)->paddingTop(6)->maxWidth(Alignment::bottom()); return VStack({content->maxSize(Alignment::topLeading()), bar}, 0)->maxSize(Alignment::topLeading()); } ViewPtr CharacterCounter(int count, int maxCount, int minCount) { auto fmt = [](int v) { std::string s = std::to_string(v), out; int n = 0; for (auto it = s.rbegin(); it != s.rend(); ++it) { if (n && n % 3 == 0) out.insert(out.begin(), ','); out.insert(out.begin(), *it); ++n; } return out; }; const bool bad = (minCount >= 0 && count < minCount) || count > maxCount; return RichText({StyledSpan{fmt(count), FontSpec::system(12).monospaced(), bad ? Palette::red() : Palette::fg2(), false, false, {}, {}}, StyledSpan{"/" + fmt(maxCount), FontSpec::system(12).monospaced(), Palette::fg2(), false, false, {}, {}}}); } } // namespace dp::ui