AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AAbstractLabel.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include <AUI/Enum/VerticalAlign.h>
15#include <AUI/Enum/TextTransform.h>
16#include "AUI/Platform/AWindowBase.h"
17#include "AView.h"
18#include "AUI/Render/IRenderer.h"
19#include "AUI/Common/AString.h"
20#include "AUI/Image/IDrawable.h"
21#include "AUI/Enum/WordBreak.h"
22#include "AUI/Font/IFontView.h"
23#include <AUI/Util/ADataBinding.h>
24#include <AUI/Common/IStringable.h>
25#include <AUI/Util/Declarative.h>
26
30class API_AUI_VIEWS AAbstractLabel : public AView, public IStringable, public IFontView {
31public:
33
34 explicit AAbstractLabel(AString text) noexcept: mText(std::move(text)) {}
35
36 void render(ARenderContext context) override;
37 void doRenderText(IRenderer& render);
38 int getContentMinimumWidth() override;
39 int getContentMinimumHeight() override;
40
41 const _<IDrawable>& getIcon() const {
42 return mIcon;
43 }
44
45 AString toString() const override;
46
47 void setIcon(const _<IDrawable>& drawable) {
48 mIcon = drawable;
49 redraw();
50 }
51
52 void setIconColor(const AColor& iconColor) {
53 mIconColor = iconColor;
54 }
55
56 void doPrerender(IRenderer& render);
57
58 bool consumesClick(const glm::ivec2& pos) override;
59
60 void onDpiChanged() override;
61
62 void setText(AString newText);
63
64 [[nodiscard]]
65 const AString& text() const {
66 return mText;
67 }
68
69 void invalidateFont() override;
70
71 void setVerticalAlign(VerticalAlign verticalAlign) {
72 if (mVerticalAlign == verticalAlign) {
73 return;
74 }
75 mVerticalAlign = verticalAlign;
76 invalidateFont();
77 }
78
79 void setTextOverflow(ATextOverflow textOverflow) {
80 if (mTextOverflow == textOverflow) {
81 return;
82 }
83 mTextOverflow = textOverflow;
84 markMinContentSizeInvalid();
85 }
86
87 void setTextTransform(TextTransform textTransform) {
88 if (mTextTransform == textTransform) {
89 return;
90 }
91 mTextTransform = textTransform;
92 invalidateFont();
93 }
94
95 void setSize(glm::ivec2 size) override;
96
97 void invalidateAllStyles() override;
98
99protected:
101
102 const _<IRenderer::IPrerenderedString>& getPrerendered() {
103 return mPrerendered;
104 }
105
106 void commitStyle() override;
107 //void userProcessStyleSheet(const std::function<void(css, const std::function<void(property)>&)>& processor) override;
108
109
110 // for correct selection positioning (used in ASelectableLabel)
111 int mTextLeftOffset = 0;
112 bool mIsTextTooLarge = false;
113
114private:
115 AString mText;
116 _<IDrawable> mIcon;
117 VerticalAlign mVerticalAlign = VerticalAlign::DEFAULT;
118 TextTransform mTextTransform = TextTransform::NONE;
119 AColor mIconColor = {1, 1, 1, 1};
120
121 glm::ivec2 getIconSize() const;
122
126 ATextOverflow mTextOverflow = ATextOverflow::NONE;
127
128 AString getTransformedText();
129
130 void processTextOverflow(AString& text);
131
132 template<class Iterator>
133 Iterator findFirstOverflowedIndex(const Iterator& begin, const Iterator& end, int overflowingWidth);
134
135 template<class Iterator>
136 void processTextOverflow(Iterator begin, Iterator end, int overflowingWidth);
137};
138
139
140template<>
142public:
143 static void setup(const _<AAbstractLabel>& view) {}
144
145 static auto getGetter() {
146 return (ASignal<AString> AAbstractLabel::*) nullptr;
147 }
148
149 static auto getSetter() {
150 return &AAbstractLabel::setText;
151 }
152};
153
Represents an abstract text display view.
Definition: AAbstractLabel.h:30
Represents a 4-component floating point color.
Definition: AColor.h:27
Definition: ASignal.h:24
Represents a Unicode character string.
Definition: AString.h:37
Base class of all UI objects.
Definition: AView.h:77
virtual int getContentMinimumHeight()
Definition: AView.cpp:252
virtual bool consumesClick(const glm::ivec2 &pos)
Determines whether this AView processes this click or passes it thru.
Definition: AView.cpp:534
void redraw()
Request window manager to redraw this AView.
Definition: AView.cpp:68
virtual int getContentMinimumWidth()
Definition: AView.cpp:248
virtual void render(ARenderContext ctx)
Draws this AView. Noone should call this function except rendering routine.
Definition: AView.cpp:142
virtual void invalidateAllStyles()
Invalidates all styles, causing to iterate over all rules in global and parent stylesheets.
Definition: AView.cpp:179
Interface of a AView that works with fonts (i.e., ALabel, ATextField, AText, etc.....
Definition: IFontView.h:19
Base class for rendering.
Definition: IRenderer.h:149
Object that can be converted to string.
Definition: IStringable.h:29
virtual AString toString() const =0
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
VerticalAlign
Controls the text vertical alignment of AView.
Definition: VerticalAlign.h:24
TextTransform
Controls the text transform of AView.
Definition: TextTransform.h:24
ATextOverflow
Controls behavior of the overflowed text. Relevant to AAbstractLabel and its derivatives only.
Definition: AOverflow.h:49
Definition: ADataBinding.h:24
static void setup(const _< View > &view)
Definition: ADataBinding.h:30
Render context passed to AView::render.
Definition: ARenderContext.h:43
Definition: Text.h:21