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