AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
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>
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:
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 ~AAbstractTypeableView() override = default;
44
45 void onKeyDown(AInput::Key key) override {
46 Super::onKeyDown(key);
47 AAbstractTypeable::handleKey(key);
48 if (key == AInput::Key::RETURN && !AInput::isKeyDown(AInput::LSHIFT) && !AInput::isKeyDown(AInput::RSHIFT)) {
49 emit this->actionButtonPressed;
50 }
51 }
52
53 void onKeyRepeat(AInput::Key key) override {
54 Super::onKeyRepeat(key);
55 AAbstractTypeable::handleKey(key);
56 }
57
58 void onFocusLost() override {
59 Super::onFocusLost();
60 if (mTextChangedFlag) {
61 mTextChangedFlag = false;
62 if (textChanged) {
63 emit textChanged(text());
64 }
65 }
66 }
67
68 void onPointerPressed(const APointerPressedEvent& event) override {
69 Super::onPointerPressed(event);
70 ACursorSelectable::handleMousePressed(event);
71 updateCursorBlinking();
72 }
73
74 void onPointerDoubleClicked(const APointerPressedEvent& event) override {
75 Super::onPointerDoubleClicked(event);
76 ACursorSelectable::handleMouseDoubleClicked(event);
77 updateCursorBlinking();
78 }
79
80 void onPointerMove(glm::vec2 pos, const APointerMoveEvent& event) override {
81 Super::onPointerMove(pos, event);
82 ACursorSelectable::handleMouseMove(pos);
83 }
84
85 void onPointerReleased(const APointerReleasedEvent& event) override {
86 Super::onPointerReleased(event);
87 if (!event.triggerClick) return;
88
89 if (event.pointerIndex != APointerIndex::button(AInput::RBUTTON)) {
90 ACursorSelectable::handleMouseReleased(event);
91 }
92 }
93
94 bool wantsTouchscreenKeyboard() override {
95 return true;
96 }
97
98 bool handlesNonMouseNavigation() override {
99 return true;
100 }
101
102 void onFocusAcquired() override {
103 Super::onFocusAcquired();
104 updateCursorBlinking();
105 }
106
107 void invalidateAllStyles() override {
108 // order is intentional
109 this->invalidateAllStylesFont();
110 Super::invalidateAllStyles();
111 }
112
113 bool isLButtonPressed() override {
114 return this->isPressed();
115 }
116
117 void drawCursor(IRenderer& renderer, glm::ivec2 position) {
118 if (!this->hasFocus()) {
119 return;
120 }
121 drawCursorImpl(renderer, position, this->getFontStyle().size);
122 }
123
124protected:
125 AMenuModel composeContextMenu() override {
126 return composeContextMenuImpl();
127 }
128
129 void commitStyle() override {
130 Super::commitStyle();
131 this->commitStyleFont();
132 }
133
134 int getVerticalAlignmentOffset() noexcept {
135 return (glm::max)(0, int(glm::ceil((Super::getContentHeight() - this->getFontStyle().size) / 2.0)));
136 }
137
138 void cursorSelectableRedraw() override {
139 this->redraw();
140 }
141
142 void onSelectionChanged() override {
143 onCursorIndexChanged();
144 if (selectionChanged) emit selectionChanged(selection());
145 }
146
147private:
148 void emitTextChanged(const AString& text) override {
149 emit textChanged(text);
150 }
151
152 void emitTextChanging(const AString& text) override {
153 emit textChanging(text);
154 }
155
156 void emitActionButtonPressed() override {
157 emit actionButtonPressed;
158 }
159
160 void typeableInvalidateFont() override {
161 this->invalidateFont();
162 }
163};
Basic implementation of type shortcuts and selection for editable text fields.
Definition: AAbstractTypeableView.h:31
Base class for AAbstractTypeableView which is template class.
Definition: AAbstractTypeable.h:25
AOptional< AInput::Key > button() const noexcept
Definition: APointerIndex.h:56
Represents a Unicode character string.
Definition: AString.h:37
A std::vector with AUI extensions.
Definition: AVector.h:38
Base class for rendering.
Definition: IRenderer.h:149
#define emit
emits the specified signal in context of this object.
Definition: AObject.h:196
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
Definition: Text.h:21