AUI Framework  master
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
   29class API_AUI_VIEWS AButton : public AAbstractLabel {
   30public:
   31    AButton();
   32
   33    explicit AButton(AString text) noexcept: AAbstractLabel(std::move(text)) {}
   34
   35    virtual ~AButton() = default;
   36
   37    [[nodiscard]]
   38    bool isDefault() const noexcept {
   39        return mDefault;
   40    }
   41
   42    void setDefault(bool isDefault = true);
   43
   44    bool consumesClick(const glm::ivec2& pos) override;
   45
   46signals:
   47    emits<bool> defaultState;
   48    emits<> becameDefault;
   49    emits<> noLongerDefault;
   50
   51private:
   52    AFieldSignalEmitter<bool> mDefault = AFieldSignalEmitter<bool>(defaultState, becameDefault, noLongerDefault);
   53};
   54
   58class AButtonEx : public AViewContainer {
   59public:
   60    AButtonEx() {
   61        addAssName(".btn");
   62    }
   63
   64    ~AButtonEx() override = default;
   65};
   66
   67namespace declarative {
   71struct Button : aui::ui_building::view_container_layout<AHorizontalLayout, AButtonEx> {
   81    Button(AString text) : layouted_container_factory<AHorizontalLayout, AButtonEx>({ Label { std::move(text) } }) {}
   82
   92    Button(const char* text) : layouted_container_factory<AHorizontalLayout, AButtonEx>({ Label { text } }) {}
   93
  106    template <typename... Views>
  107    Button(Views&&... views)
  108        : layouted_container_factory<AHorizontalLayout, AButtonEx>(std::forward<Views>(views)...) {}
  109};
  110}   // namespace declarative
  111
  112namespace ass::button {
  113struct Default : IAssSubSelector {
  114private:
  115    _unique<IAssSubSelector> mWrapped;
  116
  117    public:
  118        template<typename T>
  119        Default(T value): mWrapped(new T(std::move(value))) {}
  120
  121
  122        bool isPossiblyApplicable(AView* view) override {
  123            return mWrapped->isPossiblyApplicable(view) && dynamic_cast<AButton*>(view);
  124        }
  125
  126        bool isStateApplicable(AView* view) override {
  127            if (!mWrapped->isStateApplicable(view))
  128                return false;
  129
  130            if (auto c = dynamic_cast<AButton*>(view)) {
  131                return c->isDefault();
  132            }
  133            return false;
  134        }
  135
  136        void setupConnections(AView* view, const _<AAssHelper>& helper) override {
  137            IAssSubSelector::setupConnections(view, helper);
  138            mWrapped->setupConnections(view, helper);
  139
  140            if (auto c = dynamic_cast<AButton*>(view)) {
  141                c->defaultState.clearAllOutgoingConnectionsWith(helper.get());
  142                AObject::connect(c->defaultState, slot(helper)::onInvalidateStateAss);
  143            }
  144        }
  145    };
  146}
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:58
Button with text, which can be pushed to make some action.
Definition AButton.h:29
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
Button(const char *text)
Basic label initializer.
Definition AButton.h:92
Button(AString text)
Basic label initializer.
Definition AButton.h:81
Button(Views &&... views)
An explicit form of AButton where you can put any views in it, i.e., icons.
Definition AButton.h:107
Declarative form of ALabel.
Definition ALabel.h:35