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    bool consumesClick(const glm::ivec2& pos) override;
   71
   72    void onDpiChanged() override;
   73
   74    void setText(AString newText);
   75
   76    void invalidateFont() override;
   77
   78    void setVerticalAlign(VerticalAlign verticalAlign) {
   79        if (mVerticalAlign == verticalAlign) {
   80            return;
   81        }
   82        mVerticalAlign = verticalAlign;
   83        invalidateFont();
   84    }
   85
   86    void setTextOverflow(ATextOverflow textOverflow) {
   87        if (mTextOverflow == textOverflow) {
   88            return;
   89        }
   90        mTextOverflow = textOverflow;
   91        markMinContentSizeInvalid();
   92    }
   93
   94    void setTextTransform(TextTransform textTransform) {
   95        if (mTextTransform == textTransform) {
   96            return;
   97        }
   98        mTextTransform = textTransform;
   99        invalidateFont();
  100    }
  101
  102    void setSize(glm::ivec2 size) override;
  103
  104    void invalidateAllStyles() override;
  105
  106protected:
  107    _<IRenderer::IPrerenderedString> mPrerendered;
  108
  109    const _<IRenderer::IPrerenderedString>& getPrerendered() {
  110        return mPrerendered;
  111    }
  112
  113    void commitStyle() override;
  114    //void userProcessStyleSheet(const std::function<void(css, const std::function<void(property)>&)>& processor) override;
  115
  116
  117    // for correct selection positioning (used in ASelectableLabel)
  118    int mTextLeftOffset = 0;
  119    bool mIsTextTooLarge = false;
  120
  121private:
  122    AString mText;
  123    emits<AString> mTextChanged;
  124    _<IDrawable> mIcon;
  125    VerticalAlign mVerticalAlign = VerticalAlign::DEFAULT;
  126    TextTransform mTextTransform = TextTransform::NONE;
  127    AColor mIconColor = {1, 1, 1, 1};
  128
  129    glm::ivec2 getIconSize() const;
  130
  134    ATextOverflow mTextOverflow = ATextOverflow::NONE;
  135
  136    AString getTransformedText();
  137
  138    void processTextOverflow(AString& text);
  139
  140    template<class Iterator>
  141    Iterator findFirstOverflowedIndex(const Iterator& begin, const Iterator& end, int overflowingWidth);
  142
  143    template<class Iterator>
  144    void processTextOverflow(Iterator begin, Iterator end, int overflowingWidth);
  145
  146};
  147
  148
  149template<>
  151public:
  152    static auto property(const _<AAbstractLabel>& view) {
  153        return view->text();
  154    }
  155
  156    static void setup(const _<AAbstractLabel>& view) {}
  157
  158    static auto getGetter() {
  159        return (ASignal<AString> AAbstractLabel::*) nullptr;
  160    }
  161
  162    static auto getSetter() {
  163        return &AAbstractLabel::setText;
  164    }
  165};
  166
Represents an abstract text display view.
Definition AAbstractLabel.h:30
AString toString() const override
bool consumesClick(const glm::ivec2 &pos) override
Determines whether this AView processes this click or passes it thru.
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:572
Defines how View handles properties of FieldType type.
Definition ADataBinding.h:37
static void(View::*)(const FieldType &v) getSetter()
Returns setter for ADataBinding (deprecated)
Definition ADataBinding.h:63
static void setup(const _< View > &view)
Called then view linked with field.
Definition ADataBinding.h:43
static auto property(const _< View > &view)
Returns property definition for FieldType.
Definition ADataBinding.h:49
static ASignal< FieldType >View::* getGetter()
Returns getter for ADataBinding (deprecated)
Definition ADataBinding.h:55
Property implementation to use with custom getter/setter.
Definition AProperty.h:234
Render context passed to AView::render.
Definition ARenderContext.h:43