AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AButton.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 "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::view_container_layout<AHorizontalLayout, AButtonEx> {
   67        using aui::ui_building::view_container_layout<AHorizontalLayout, AButtonEx>::layouted_container_factory;
   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 {
   76    struct Default : IAssSubSelector {
   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.clearAllOutgoingConnectionsWith(helper.get());
  105                AObject::connect(c->defaultState, slot(helper)::onInvalidateStateAss);
  106            }
  107        }
  108    };
  109}
auto text() const
Label's text property.
Definition AAbstractLabel.h:37
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
bool consumesClick(const glm::ivec2 &pos) override
Determines whether this AView processes this click or passes it thru.
Stores a value and observes it's changes, emitting a signal.
Definition AFieldSignalEmitter.h:21
Places views in a row.
Definition AHorizontalLayout.h:42
Represents a Unicode character string.
Definition AString.h:38
Base class of all UI objects.
Definition AView.h:78
void addAssName(const AString &assName)
Adds an ASS class to this AView.
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
Definition AAssSelector.h:28
class_of c
Selects views that are of the specified classes.
Definition class_of.h:84
static decltype(auto) connect(const Signal &signal, Object *object, Function &&function)
Connects signal to the slot of the specified object.
Definition AObject.h:86
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:572
#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:88