#include "screens/conversation/ComposerRow.hpp"
#include <algorithm>
#include "spec/SocialSpec.hpp"
namespace dp {
using namespace ui;
namespace {
class ComposerRowView : public View {
public:
ComposerRowView(ViewPtr field, ViewPtr below, ViewPtr trailing) {
addChild(std::move(field));
addChild(std::move(below));
addChild(std::move(trailing));
}
protected:
struct Metrics {
float width, fieldH, belowH, trailW, trailH, columnW;
};
Metrics metrics(const Proposal& p) {
using M = spec::SocialMetrics;
const Size t = children_[2]->measure(Proposal::ideal());
const float total = p.w ? std::min(*p.w, 10000.0f) : 400.0f;
const float col = std::max(0.0f, total - t.w - M::kComposerRowSpacing);
const Size f = children_[0]->measure(Proposal{col, std::nullopt});
const Size b = children_[1]->measure(Proposal{col, std::nullopt});
return {total, f.h, b.h, t.w, t.h, col};
}
Size compute(const Proposal& p) override {
using M = spec::SocialMetrics;
const Metrics m = metrics(p);
return {m.width, std::max(m.fieldH, m.trailH) + M::kComposerSpacing + m.belowH};
}
public:
void place(const Rect& r) override {
using M = spec::SocialMetrics;
frame_ = r;
const Metrics m = metrics(Proposal{r.w, r.h});
const float above = std::max(m.fieldH, m.trailH);
children_[0]->place({r.x, r.y + above - m.fieldH, m.columnW, m.fieldH});
children_[1]->place({r.x, r.y + above + M::kComposerSpacing, m.columnW, m.belowH});
children_[2]->place({r.x + m.columnW + M::kComposerRowSpacing, r.y + above - m.trailH, m.trailW, m.trailH});
}
};
} // namespace
ViewPtr ComposerRow(ViewPtr field, ViewPtr below, ViewPtr trailing) {
return std::make_shared<ComposerRowView>(std::move(field), std::move(below), std::move(trailing));
}
} // namespace dp