AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AAbstractLabel.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 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:
   32    AAbstractLabel();
   33
   37    auto text() const {
   38        return APropertyDef {
   39            this,
   40            &AAbstractLabel::mText,
   41            &AAbstractLabel::setText,
   42            mTextChanged,
   43        };
   44    }
   45
   46    explicit AAbstractLabel(AString text) noexcept: mText(std::move(text)) {}
   47
   48    void render(ARenderContext context) override;
   49    void doRenderText(IRenderer& render);
   50    int getContentMinimumWidth() override;
   51    int getContentMinimumHeight() override;
   52
   53    const _<IDrawable>& getIcon() const {
   54        return mIcon;
   55    }
   56
   57    AString toString() const override;
   58
   59    void setIcon(const _<IDrawable>& drawable) {
   60        mIcon = drawable;
   61        redraw();
   62    }
   63
   64    void setIconColor(const AColor& iconColor) {
   65        mIconColor = iconColor;
   66    }
   67
   68    void doPrerender(IRenderer& render);
   69
   70    void onDpiChanged() override;
   71
   72    void setText(AString newText);
   73
   74    void invalidateFont() override;
   75
   76    void setVerticalAlign(VerticalAlign verticalAlign) {
   77        if (mVerticalAlign == verticalAlign) {
   78            return;
   79        }
   80        mVerticalAlign = verticalAlign;
   81        invalidateFont();
   82    }
   83
   84    void setTextOverflow(ATextOverflow textOverflow) {
   85        if (mTextOverflow == textOverflow) {
   86            return;
   87        }
   88        mTextOverflow = textOverflow;
   89        markMinContentSizeInvalid();
   90    }
   91
   92    void setTextTransform(TextTransform textTransform) {
   93        if (mTextTransform == textTransform) {
   94            return;
   95        }
   96        mTextTransform = textTransform;
   97        invalidateFont();
   98    }
   99
  100    void setSize(glm::ivec2 size) override;
  101
  102    void invalidateAllStyles() override;
  103
  104protected:
  105    _<IRenderer::IPrerenderedString> mPrerendered;
  106
  107    const _<IRenderer::IPrerenderedString>& getPrerendered() {
  108        return mPrerendered;
  109    }
  110
  111    void commitStyle() override;
  112    //void userProcessStyleSheet(const std::function<void(css, const std::function<void(property)>&)>& processor) override;
  113
  114
  115    // for correct selection positioning (used in ASelectableLabel)
  116    int mTextLeftOffset = 0;
  117    bool mIsTextTooLarge = false;
  118
  119private:
  120    AString mText;
  121    emits<AString> mTextChanged;
  122    _<IDrawable> mIcon;
  123    VerticalAlign mVerticalAlign = VerticalAlign::DEFAULT;
  124    TextTransform mTextTransform = TextTransform::NONE;
  125    AColor mIconColor = {1, 1, 1, 1};
  126
  127    glm::ivec2 getIconSize() const;
  128
  132    ATextOverflow mTextOverflow = ATextOverflow::NONE;
  133
  134    AString getTransformedText();
  135
  136    void processTextOverflow(AString& text);
  137
  138    template<class Iterator>
  139    Iterator findFirstOverflowedIndex(const Iterator& begin, const Iterator& end, int overflowingWidth);
  140
  141    template<class Iterator>
  142    void processTextOverflow(Iterator begin, Iterator end, int overflowingWidth);
  143
  144};
  145
  146
  147template<>
  149public:
  150    static auto property(const _<AAbstractLabel>& view) {
  151        return view->text();
  152    }
  153
  154    static void setup(const _<AAbstractLabel>& view) {}
  155
  156    static auto getGetter() {
  157        return (ASignal<AString> AAbstractLabel::*) nullptr;
  158    }
  159
  160    static auto getSetter() {
  161        return &AAbstractLabel::setText;
  162    }
  163};
  164
Represents an abstract text display view.
Definition AAbstractLabel.h:30
AString toString() const override
auto text() const
Label's text property.
Definition AAbstractLabel.h:37
void invalidateAllStyles() override
Invalidates all styles, causing to iterate over all rules in global and parent stylesheets.
void render(ARenderContext context) override
Draws this AView. Noone should call this function except rendering routine.
int getContentMinimumHeight() override
int getContentMinimumWidth() override
Represents a 4-component floating point color (RGBA).
Definition AColor.h:26
Definition ASignal.h:148
Represents a Unicode character string.
Definition AString.h:38
void redraw()
Request window manager to redraw this AView.
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
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
VerticalAlign
Controls the text vertical alignment of AView.
Definition VerticalAlign.h:24
TextTransform
Controls the text transform of AView.
Definition TextTransform.h:23
ATextOverflow
Controls behavior of the overflowed text. Relevant to AAbstractLabel and its derivatives only.
Definition AOverflow.h:47
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:577
Defines how View handles properties of FieldType type.
Definition ADataBinding.h:38
static void(View::*)(const FieldType &v) getSetter()
Returns setter for ADataBinding (deprecated)
Definition ADataBinding.h:64
static void setup(const _< View > &view)
Called then view linked with field.
Definition ADataBinding.h:44
static auto property(const _< View > &view)
Returns property definition for FieldType.
Definition ADataBinding.h:50
static ASignal< FieldType >View::* getGetter()
Returns getter for ADataBinding (deprecated)
Definition ADataBinding.h:56
Property implementation to use with custom getter/setter.
Definition APropertyDef.h:23
Render context passed to AView::render.
Definition ARenderContext.h:43