#include "screens/components/MessageBubble.hpp"
#include <algorithm>
#include "screens/components/AttachmentViews.hpp"
#include "screens/components/RelativeTime.hpp"
#include "screens/components/SelectableTextPopover.hpp"
#include "spec/SocialSpec.hpp"
#include "ui/Markdown.hpp"
#include "ui/Modifiers.hpp"
#include "ui/Palette.hpp"
namespace dp {
using namespace ui;
using M = spec::SocialMetrics;
using B = spec::BubbleColors;
Color mixColor(Color base, Color other, float f) {
auto ch = [f](std::uint8_t a, std::uint8_t b) {
return static_cast<std::uint8_t>(std::clamp(static_cast<float>(a) + (static_cast<float>(b) - static_cast<float>(a)) * f, 0.0f, 255.0f) + 0.5f);
};
return {ch(base.r, other.r), ch(base.g, other.g), ch(base.b, other.b), base.a};
}
BubbleGradient BubbleGradient::forMessage(bool isMine, bool dark) {
Color base = dark ? spec::Theme::kBg1.dark : spec::Theme::kBg1.light;
float brighten = 0, darken = 0;
if (isMine) {
base = dark ? mixColor(Palette::accent(), Color::rgb(0x000000), B::kMineDarkAccentBlack)
: mixColor(Palette::accent(), Color::rgb(0xFFFFFF), B::kMineLightAccentWhite);
brighten = dark ? B::kTopBrightenMineDark : B::kTopBrightenMineLight;
darken = dark ? B::kBottomDarkenMineDark : B::kBottomDarkenMineLight;
} else {
brighten = dark ? B::kTopBrightenOtherDark : B::kTopBrightenOtherLight;
darken = dark ? B::kBottomDarkenOtherDark : B::kBottomDarkenOtherLight;
}
return {mixColor(base, Color::rgb(0xFFFFFF), brighten), mixColor(base, Color::rgb(0x000000), darken)};
}
namespace {
Corners bubbleCorners(bool mine) {
return {M::kBubbleRadius, M::kBubbleRadius, mine ? M::kBubbleTailRadius : M::kBubbleRadius,
mine ? M::kBubbleRadius : M::kBubbleTailRadius};
}
// Uneven rounded shape: vertical gradient fill or the 1 pt border, depending on the mode.
class BubbleShape : public View {
public:
BubbleShape(bool mine, bool stroke) : mine_(mine), stroke_(stroke) {}
protected:
Size compute(const Proposal& p) override {
return {p.w ? std::min(*p.w, 10000.0f) : 10.0f, p.h ? std::min(*p.h, 10000.0f) : 10.0f};
}
public:
void draw(Canvas& c) override {
const Corners r = bubbleCorners(mine_);
if (stroke_) {
c.strokeRRect(frame_, r, Palette::fg2(), 1, M::kBubbleBorderOpacity);
} else {
const BubbleGradient g = BubbleGradient::forMessage(mine_, Appearance::dark());
c.fillRRectGradient(frame_, r, g.top, g.bottom, {0.5f, 0}, {0.5f, 1});
}
}
private:
bool mine_, stroke_;
};
// HuggingWidthLayout: the child is proposed `maxWidth` whatever the parent offers, so the bubble hugs its content.
class HuggingWidth : public WrapperView {
public:
HuggingWidth(ViewPtr child, float maxWidth) : WrapperView(std::move(child)), maxWidth_(maxWidth) {}
protected:
Size compute(const Proposal& p) override { return child()->measure(Proposal{maxWidth_, p.h}); }
private:
float maxWidth_;
};
// Text blocks hug their natural width: min(proposed, ideal). The Swift bubble hugs short messages
// (HuggingWidthLayout), so the markdown block must not stretch to the offered width.
class HugContent : public WrapperView {
public:
explicit HugContent(ViewPtr child) : WrapperView(std::move(child)) {}
protected:
Size compute(const Proposal& p) override {
const float ideal = child()->measure(Proposal::ideal()).w;
const float w = p.w ? std::min(*p.w, ideal) : ideal;
return child()->measure(Proposal{w, p.h});
}
};
ViewPtr doubleCheckmark(bool read) {
if (!read) return nullptr;
auto check = [] { return Icon("checkmark", M::kCheckSize)->weight(Weight::Semibold); };
return ZStack({check()->offset(M::kCheckOffsetA, 0), check()->offset(M::kCheckOffsetB, 0)})
->foreground(Palette::accent())->id(spec::SocialStrings::kRead);
}
} // namespace
ViewPtr MessageBubbleView(const Message& message, float maxBubbleWidth, QuoteComposer quote) {
const bool mine = message.isMine;
std::vector<ViewPtr> parts;
if (!message.data.content.empty()) {
parts.push_back(SelectableTextPopover(std::make_shared<HugContent>(MarkdownView(message.data.content)), message.data.content, std::move(quote)));
}
for (const Attachment& a : message.attachments) parts.push_back(AttachmentViewer(a));
std::vector<ViewPtr> footer{RelativeTimeLabel(message.data.createdAt)};
if (mine) {
if (ViewPtr check = doubleCheckmark(message.data.read)) footer.push_back(check);
}
parts.push_back(HStack(footer, M::kBubbleFooterSpacing));
ViewPtr stack = VStack(parts, M::kBubbleSpacing, mine ? HAlign::Trailing : HAlign::Leading)
->paddingH(M::kBubblePadH)->paddingV(M::kBubblePadV);
ViewPtr hug = std::make_shared<HuggingWidth>(stack, maxBubbleWidth);
ViewPtr body = hug->foreground(Palette::fg1())->background(std::make_shared<BubbleShape>(mine, false));
body = std::make_shared<ClipView>(body, bubbleCorners(mine), false);
body = body->overlay(std::make_shared<BubbleShape>(mine, true));
return body->maxWidth(mine ? Alignment::trailing() : Alignment::leading())->id(message.id());
}
} // namespace dp