5#include "fifechan/widgets/statusbar.hpp"
13#include "fifechan/font.hpp"
14#include "fifechan/graphics.hpp"
30 mIconArea = std::make_unique<Container>();
31 mIconArea->setLayout(Container::LayoutPolicy::Horizontal);
32 mIconArea->setOpaque(
false);
33 mIconArea->setWidth(24);
34 mIconArea->setHeight(24);
37 mLeftArea = std::make_unique<Container>();
38 mLeftArea->setLayout(Container::LayoutPolicy::Horizontal);
39 mLeftArea->setOpaque(
false);
42 mRightArea = std::make_unique<Container>();
43 mRightArea->setLayout(Container::LayoutPolicy::Horizontal);
44 mRightArea->setOpaque(
false);
45 mRightArea->setWidth(100);
46 add(mRightArea.get());
69 std::string textToDraw = mText;
72 int const iconAreaWidth = 24;
73 int const rightAreaWidth = 100;
74 int const textPadding = 4;
79 if (!mText.empty() && font !=
nullptr) {
80 textWidth = font->getWidth(mText);
85 int const minimumWidth = iconAreaWidth + rightAreaWidth + textWidth + textPadding;
89 if (mShrinkPriorityEnabled && currentWidth < minimumWidth) {
91 int const availableForText = currentWidth - iconAreaWidth - rightAreaWidth - textPadding;
94 if (availableForText < textWidth && availableForText > 0) {
95 if (mTruncateWithEllipsis && availableForText > 3) {
97 int const ellipsisWidth = font !=
nullptr ? font->getWidth(
"...") : 15;
98 int const availableForTextContent = availableForText - ellipsisWidth;
100 if (availableForTextContent > 0) {
104 for (
size_t i = 0; i < mText.size(); ++i) {
105 charWidth += font !=
nullptr ? font->getWidth(std::string(1, mText.at(i))) : 8;
106 if (charWidth > availableForTextContent) {
109 charsToShow =
static_cast<int>(i) + 1;
111 textToDraw = mText.substr(0, charsToShow) +
"...";
116 }
else if (!mTruncateWithEllipsis) {
120 for (
size_t i = 0; i < mText.size(); ++i) {
121 charWidth += font !=
nullptr ? font->getWidth(std::string(1, mText.at(i))) : 8;
122 if (charWidth > availableForText) {
125 charsToShow =
static_cast<int>(i) + 1;
127 textToDraw = mText.substr(0, charsToShow);
134 if (!textToDraw.empty()) {
137 if (font !=
nullptr) {
146 textPadding + iconAreaWidth,
148 getWidth() - (iconAreaWidth + rightAreaWidth + (2 * textPadding)),
153 graphics->
drawText(textToDraw, textPadding, textY, Graphics::Alignment::Left);
163 return mIconArea.get();
168 return mLeftArea.get();
173 return mRightArea.get();
178 mShrinkPriorityEnabled = enabled;
183 return mShrinkPriorityEnabled;
188 mTruncateWithEllipsis = enabled;
193 return mTruncateWithEllipsis;
A composite widget capable of holding and managing child widgets.
void draw(Graphics *graphics) override
Draws the widget.
virtual void setLayout(LayoutPolicy policy)
Sets the layout of the container.
virtual bool isOpaque() const
Checks if the container is opaque or not.
virtual void add(Widget *widget)
Adds a widget to the container.
Abstract interface for font rendering.
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
virtual void popClipArea()
Removes the top most clip area from the stack.
void drawText(std::string const &text, int x, int y)
Draws text with a default left alignment.
virtual bool pushClipArea(Rectangle area)
Pushes a clip area onto the stack.
virtual void setFont(Font *font)
Sets the font to use when drawing text.
virtual void drawLine(int x1, int y1, int x2, int y2)=0
Draws a line.
virtual void setColor(Color const &color)=0
Sets the color to use when drawing.
void setExpandChildren(bool expand)
Sets whether children should expand to fill available width.
Represents a rectangular area (X, Y, Width, Height).
void draw(Graphics *graphics) override
Draws the status bar.
bool isShrinkPriorityEnabled() const
Checks if shrink priority handling is enabled.
bool isTruncateWithEllipsis() const
Checks if truncation with ellipsis is enabled.
std::string const & getText() const
Gets the status text.
Container * getLeftArea() const
Access to the left (flexible) area.
Container * getIconArea() const
Access to the icon area (fixed size, left).
void setShrinkPriorityEnabled(bool enabled)
Enables or disables shrink priority handling.
void setTruncateWithEllipsis(bool enabled)
Enables or disables truncation with ellipsis.
Container * getRightArea() const
Access to the right (fixed) area.
void setText(std::string const &text)
Sets the status text.
Used replacement tokens by configure_file():