AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AButton.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#pragma once
13
14#include "AView.h"
15#include "AUI/Common/AString.h"
16#include "AUI/Render/IRenderer.h"
17#include "AUI/Layout/AHorizontalLayout.h"
18#include "ALabel.h"
19#include "AViewContainer.h"
20#include "AUI/ASS/Selector/AAssSelector.h"
21
27class API_AUI_VIEWS AButton : public AAbstractLabel {
28public:
29 AButton();
30
31 explicit AButton(AString text) noexcept: AAbstractLabel(std::move(text)) {}
32
33 virtual ~AButton() = default;
34
35 [[nodiscard]]
36 bool isDefault() const noexcept {
37 return mDefault;
38 }
39
40 void setDefault(bool isDefault = true);
41
42 bool consumesClick(const glm::ivec2& pos) override;
43
44signals:
45 emits<bool> defaultState;
46 emits<> becameDefault;
47 emits<> noLongerDefault;
48
49private:
50 AFieldSignalEmitter<bool> mDefault = AFieldSignalEmitter<bool>(defaultState, becameDefault, noLongerDefault);
51};
52
56class AButtonEx : public AViewContainer {
57public:
58 AButtonEx() {
59 addAssName(".btn");
60 }
61
62 ~AButtonEx() override = default;
63};
64
65namespace declarative {
66 struct Button : aui::ui_building::layouted_container_factory<AHorizontalLayout, AButtonEx> {
68
69 Button(AString text) : layouted_container_factory<AHorizontalLayout, AButtonEx>({Label{std::move(text)}}) {}
70
71 Button(const char* text) : layouted_container_factory<AHorizontalLayout, AButtonEx>({Label{text}}) {}
72 };
73}
74
75namespace ass::button {
77 private:
78 _unique<IAssSubSelector> mWrapped;
79
80 public:
81 template<typename T>
82 Default(T value): mWrapped(new T(std::move(value))) {}
83
84
85 bool isPossiblyApplicable(AView* view) override {
86 return mWrapped->isPossiblyApplicable(view) && dynamic_cast<AButton*>(view);
87 }
88
89 bool isStateApplicable(AView* view) override {
90 if (!mWrapped->isStateApplicable(view))
91 return false;
92
93 if (auto c = dynamic_cast<AButton*>(view)) {
94 return c->isDefault();
95 }
96 return false;
97 }
98
99 void setupConnections(AView* view, const _<AAssHelper>& helper) override {
100 IAssSubSelector::setupConnections(view, helper);
101 mWrapped->setupConnections(view, helper);
102
103 if (auto c = dynamic_cast<AButton*>(view)) {
104 c->defaultState.clearAllConnectionsWith(helper.get());
105 AObject::connect(c->defaultState, slot(helper)::onInvalidateStateAss);
106 }
107 }
108 };
109}
Represents an abstract text display view.
Definition: AAbstractLabel.h:30
bool consumesClick(const glm::ivec2 &pos) override
Determines whether this AView processes this click or passes it thru.
Definition: AAbstractLabel.cpp:241
Unlike AButton, AButtonEx is a container which looks like a button.
Definition: AButton.h:56
Button with text, which can be pushed to make some action.
Definition: AButton.h:27
Definition: AFieldSignalEmitter.h:51
Represents a Unicode character string.
Definition: AString.h:37
A trivial modifiable view that represents a set of views.
Definition: AViewContainer.h:33
Base class of all UI objects.
Definition: AView.h:77
void addAssName(const AString &assName)
Adds an ASS class to this AView.
Definition: AView.cpp:281
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
Definition: AAssSelector.h:28
#define slot(v)
Passes some variable and type of the variable separated by comma. It's convenient to use with the con...
Definition: kAUI.h:90
Definition: Text.h:21
Definition: AButton.h:76
Definition: class_of.h:57
Definition: Declarative.h:91
Definition: AButton.h:66