AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AAbstractTypeableView.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//
   13// Created by alex2 on 5/23/2021.
   14//
   15
   16
   17#pragma once
   18
   19#include "AAbstractTypeable.h"
   20#include <AUI/Platform/AWindow.h>
   21
   26template<aui::derived_from<AView> Super>
   27class AAbstractTypeableView
   28        : public Super,
   29          public AAbstractTypeable,
   30          public std::conditional_t<aui::derived_from<Super, IFontView>, std::monostate, IFontView> /* implement
   31                                                                                                       IFontView if
   32                                                                                                       Super doesn't */{
   33public:
   34    AAbstractTypeableView() {
   35        AObject::connect(mBlinkTimer->fired, this, [&]() {
   36            if (this->hasFocus() && mCursorBlinkCount < 60) {
   37                mCursorBlinkVisible = !mCursorBlinkVisible;
   38                mCursorBlinkCount += 1;
   39                this->redraw();
   40            }
   41        });
   42    }
   43
   44
   48    auto text() const {
   49        return APropertyDef {
   50            this,
   51            &AAbstractTypeable::getText,
   52            &AAbstractTypeable::setText,
   53            textChanging,
   54        };
   55    }
   56
   57    ~AAbstractTypeableView() override = default;
   58
   59    void onKeyDown(AInput::Key key) override {
   60        Super::onKeyDown(key);
   61        if (key == AInput::ESCAPE) {
   62            AWindow::current()->setFocusedView(nullptr);
   63        }
   64        AAbstractTypeable::handleKey(key);
   65        if (key == AInput::Key::RETURN && !AInput::isKeyDown(AInput::LSHIFT) && !AInput::isKeyDown(AInput::RSHIFT)) {
   66            emit this->actionButtonPressed;
   67        }
   68    }
   69
   70    void onKeyRepeat(AInput::Key key) override {
   71        Super::onKeyRepeat(key);
   72        AAbstractTypeable::handleKey(key);
   73    }
   74
   75    void onFocusLost() override {
   76        Super::onFocusLost();
   77        if (mTextChangedFlag) {
   78            mTextChangedFlag = false;
   79            if (textChanged) {
   80                emit textChanged(text());
   81            }
   82        }
   83    }
   84
   85    void onPointerPressed(const APointerPressedEvent& event) override {
   86        Super::onPointerPressed(event);
   87        ACursorSelectable::handleMousePressed(event);
   88        updateCursorBlinking();
   89    }
   90
   91    void onPointerDoubleClicked(const APointerPressedEvent& event) override {
   92        Super::onPointerDoubleClicked(event);
   93        ACursorSelectable::handleMouseDoubleClicked(event);
   94        updateCursorBlinking();
   95    }
   96
   97    void onPointerMove(glm::vec2 pos, const APointerMoveEvent& event) override {
   98        Super::onPointerMove(pos, event);
   99        ACursorSelectable::handleMouseMove(pos);
  100    }
  101
  102    void onPointerReleased(const APointerReleasedEvent& event) override {
  103        Super::onPointerReleased(event);
  104        if (!event.triggerClick) return;
  105
  106        if (event.pointerIndex != APointerIndex::button(AInput::RBUTTON)) {
  107            ACursorSelectable::handleMouseReleased(event);
  108        }
  109    }
  110
  111    bool wantsTouchscreenKeyboard() override {
  112        return true;
  113    }
  114
  115    bool handlesNonMouseNavigation() override {
  116        return true;
  117    }
  118
  119    void onFocusAcquired() override {
  120        Super::onFocusAcquired();
  121        updateCursorBlinking();
  122    }
  123
  124    void invalidateAllStyles() override {
  125        // order is intentional
  126        this->invalidateAllStylesFont();
  127        Super::invalidateAllStyles();
  128    }
  129
  130    bool isLButtonPressed() override {
  131        return this->isPressed();
  132    }
  133
  134    void drawCursor(IRenderer& renderer, glm::ivec2 position) {
  135        if (!this->hasFocus()) {
  136            return;
  137        }
  138        drawCursorImpl(renderer, position, this->getFontStyle().size);
  139    }
  140
  141protected:
  142    AMenuModel composeContextMenu() override {
  143        return composeContextMenuImpl();
  144    }
  145
  146    void commitStyle() override {
  147        Super::commitStyle();
  148        this->commitStyleFont();
  149    }
  150
  151    int getVerticalAlignmentOffset() noexcept {
  152        return (glm::max)(0, int(glm::ceil((Super::getContentHeight() - this->getFontStyle().size) / 2.0)));
  153    }
  154
  155    void cursorSelectableRedraw() override {
  156        this->redraw();
  157    }
  158
  159    void onSelectionChanged() override {
  160        onCursorIndexChanged();
  161        if (selectionChanged) emit selectionChanged(selection());
  162    }
  163
  164private:
  165    void emitTextChanged(const AString& text) override {
  166        emit textChanged(text);
  167    }
  168
  169    void emitTextChanging(const AString& text) override {
  170        emit textChanging(text);
  171    }
  172
  173    void emitActionButtonPressed() override {
  174        emit actionButtonPressed;
  175    }
  176
  177    void typeableInvalidateFont() override {
  178        this->invalidateFont();
  179    }
  180};
Basic implementation of type shortcuts and selection for editable text fields.
Definition AAbstractTypeableView.h:32
auto text() const
Text property.
Definition AAbstractTypeableView.h:48
emits< AString > textChanging
When the user changed one or more symbols.
Definition AAbstractTypeable.h:130
virtual const AString & getText() const =0
Represents a Unicode character string.
Definition AString.h:38
static AWindowBase * current()
Base class for rendering.
Definition IRenderer.h:149
static decltype(auto) connect(const Signal &signal, Object *object, Function &&function)
Connects signal to the slot of the specified object.
Definition AObject.h:86
#define emit
emits the specified signal in context of this object.
Definition AObject.h:343
Pointing method move event.
Definition APointerMoveEvent.h:21
Pointing method press event.
Definition APointerPressedEvent.h:21
Pointing method press event.
Definition APointerReleasedEvent.h:19
bool triggerClick
Whether the pointer release event triggers click event or not.
Definition APointerReleasedEvent.h:33
APointerIndex pointerIndex
Which button of the pointing device is triggered the event (AInput::LBUTTON if not present) or finger...
Definition APointerReleasedEvent.h:28
Property implementation to use with custom getter/setter.
Definition AProperty.h:234