AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ACheckBox.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 "ALabel.h"
   15#include "AViewContainer.h"
   16#include <AUI/ASS/Selector/Selected.h>
   17
   18
   31class API_AUI_VIEWS ACheckBox : public AView, public ass::ISelectable {
   32public:
   33    ACheckBox();
   34
   35    [[nodiscard]]
   36    auto checked() const {
   37        return APropertyDef {
   38            this,
   39            &ACheckBox::mChecked,
   40            &ACheckBox::setChecked,
   41            mCheckedChanged,
   42        };
   43    }
   44
   45    void toggle() {
   46        setChecked(!checked());
   47    }
   48
   49    void check() {
   50        setChecked(true);
   51    }
   52
   53    void uncheck() {
   54        setChecked(false);
   55    }
   56
   57    void setChecked(bool checked = true) {
   58        mChecked = checked;
   59        emit customCssPropertyChanged();
   60        emit ACheckBox::mCheckedChanged(checked);
   61    }
   62
   63    void setUnchecked(bool unchecked = true) {
   64        setChecked(!unchecked);
   65    }
   66
   67    bool consumesClick(const glm::ivec2& pos) override;
   68
   69protected:
   70    bool selectableIsSelectedImpl() override;
   71
   72private:
   73    bool mChecked = false;
   74    emits<bool> mCheckedChanged;
   75};
   76
   77
   86class API_AUI_VIEWS ACheckBoxWrapper: public AViewContainerBase {
   87public:
   92    explicit ACheckBoxWrapper(const _<AView>& viewToWrap);
   93
   94    [[nodiscard]]
   95    auto checked() const {
   96        return mCheckBox->checked();
   97    }
   98
   99    void toggle() {
  100        setChecked(!checked());
  101    }
  102
  103    void check() {
  104        setChecked(true);
  105    }
  106
  107    void uncheck() {
  108        setChecked(false);
  109    }
  110
  111    void setChecked(bool checked = true) {
  112        mCheckBox->setChecked(checked);
  113    }
  114
  115    void setUnchecked(bool unchecked = true) {
  116        setChecked(!unchecked);
  117    }
  118
  119private:
  120    _<ACheckBox> mCheckBox;
  121
  122};
  123
  124
  125template<>
  127public:
  128    static auto property(const _<ACheckBox>& view) {
  129        return view->checked();
  130    }
  131    static void setup(const _<ACheckBox>& view) {}
  132
  133    static auto getGetter() {
  134        return &ACheckBox::checked;
  135    }
  136
  137    static auto getSetter() {
  138        return &ACheckBox::setChecked;
  139    }
  140};
  141
  142
  143template<>
  145public:
  146    static auto property(const _<ACheckBoxWrapper>& view) {
  147        return view->checked();
  148    }
  149    static void setup(const _<ACheckBoxWrapper>& view) {}
  150
  151    static auto getGetter() {
  152        return &ACheckBoxWrapper::checked;
  153    }
  154
  155    static auto getSetter() {
  156        return &ACheckBoxWrapper::setChecked;
  157    }
  158};
  159
  160namespace declarative {
  164    struct CheckBox: aui::ui_building::view<ACheckBox> {
  165        CheckBox() = default;
  166    };
  167
  171    struct CheckBoxWrapper: aui::ui_building::view<ACheckBoxWrapper> {
  176        explicit CheckBoxWrapper(const _<AView>& viewToWrap): aui::ui_building::view<ACheckBoxWrapper>(viewToWrap) {}
  177    };
  178}
View container with a checkbox.
Definition ACheckBox.h:86
ACheckBoxWrapper(const _< AView > &viewToWrap)
Construct ACheckBoxWrapper with a view.
A check box (without label).
Definition ACheckBox.h:31
bool consumesClick(const glm::ivec2 &pos) override
Determines whether this AView processes this click or passes it thru.
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
Interface to work with ass::Selected selector.
Definition Selected.h:21
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:572
#define emit
emits the specified signal in context of this object.
Definition AObject.h:343
Defines how View handles properties of FieldType type.
Definition ADataBinding.h:37
static void(View::*)(const FieldType &v) getSetter()
Returns setter for ADataBinding (deprecated)
Definition ADataBinding.h:63
static void setup(const _< View > &view)
Called then view linked with field.
Definition ADataBinding.h:43
static auto property(const _< View > &view)
Returns property definition for FieldType.
Definition ADataBinding.h:49
static ASignal< FieldType >View::* getGetter()
Returns getter for ADataBinding (deprecated)
Definition ADataBinding.h:55
Property implementation to use with custom getter/setter.
Definition AProperty.h:234
Declarative view trait.
Definition Declarative.h:180
CheckBoxWrapper(const _< AView > &viewToWrap)
Construct ACheckBoxWrapper with a view.
Definition ACheckBox.h:176