5#include "fifechan/widgets/statusbar.hpp"
14#include "fifechan/font.hpp"
15#include "fifechan/graphics.hpp"
31 mIconArea = std::make_unique<Container>();
32 mIconArea->setLayout(Container::LayoutPolicy::Horizontal);
33 mIconArea->setOpaque(
false);
34 mIconArea->setWidth(24);
35 mIconArea->setHeight(24);
38 mLeftArea = std::make_unique<Container>();
39 mLeftArea->setLayout(Container::LayoutPolicy::Horizontal);
40 mLeftArea->setOpaque(
false);
43 mRightArea = std::make_unique<Container>();
44 mRightArea->setLayout(Container::LayoutPolicy::Horizontal);
45 mRightArea->setOpaque(
false);
46 mRightArea->setWidth(100);
47 add(mRightArea.get());
62 assert(
"graphics must not be null" && graphics !=
nullptr);
72 std::string textToDraw = mText;
75 int const iconAreaWidth = 24;
76 int const rightAreaWidth = 100;
77 int const textPadding = 4;
82 if (!mText.empty() && font !=
nullptr) {
83 textWidth = font->getWidth(mText);
88 int const minimumWidth = iconAreaWidth + rightAreaWidth + textWidth + textPadding;
92 if (mShrinkPriorityEnabled && currentWidth < minimumWidth) {
94 int const availableForText = currentWidth - iconAreaWidth - rightAreaWidth - textPadding;
97 if (availableForText < textWidth && availableForText > 0) {
98 if (mTruncateWithEllipsis && availableForText > 3) {
100 int const ellipsisWidth = font !=
nullptr ? font->getWidth(
"...") : 15;
101 int const availableForTextContent = availableForText - ellipsisWidth;
103 if (availableForTextContent > 0) {
107 for (
size_t i = 0; i < mText.size(); ++i) {
108 charWidth += font !=
nullptr ? font->getWidth(std::string(1, mText.at(i))) : 8;
109 if (charWidth > availableForTextContent) {
112 charsToShow =
static_cast<int>(i) + 1;
114 textToDraw = mText.substr(0, charsToShow) +
"...";
119 }
else if (!mTruncateWithEllipsis) {
123 for (
size_t i = 0; i < mText.size(); ++i) {
124 charWidth += font !=
nullptr ? font->getWidth(std::string(1, mText.at(i))) : 8;
125 if (charWidth > availableForText) {
128 charsToShow =
static_cast<int>(i) + 1;
130 textToDraw = mText.substr(0, charsToShow);
137 if (!textToDraw.empty()) {
140 if (font !=
nullptr) {
149 textPadding + iconAreaWidth,
151 getWidth() - (iconAreaWidth + rightAreaWidth + (2 * textPadding)),
156 graphics->
drawText(textToDraw, textPadding, textY, Graphics::Alignment::Left);
166 return mIconArea.get();
171 return mLeftArea.get();
176 return mRightArea.get();
181 mShrinkPriorityEnabled = enabled;
186 return mShrinkPriorityEnabled;
191 mTruncateWithEllipsis = enabled;
196 return mTruncateWithEllipsis;
A composite widget capable of holding and managing child widgets.
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 draw(Graphics *graphics) override
Draws the widget.
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():