AUI Framework  develop
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
   27class API_AUI_VIEWS ACheckBox : public AView, public ass::ISelectable {
   28public:
   29    ACheckBox();
   30
   31    [[nodiscard]]
   32    auto checked() const {
   33        return APropertyDef {
   34            this,
   35            &ACheckBox::mChecked,
   36            &ACheckBox::setChecked,
   37            mCheckedChanged,
   38        };
   39    }
   40
   41    void toggle() {
   42        setChecked(!checked());
   43    }
   44
   45    void check() {
   46        setChecked(true);
   47    }
   48
   49    void uncheck() {
   50        setChecked(false);
   51    }
   52
   53    void setChecked(bool checked = true) {
   54        mChecked = checked;
   55        emit customCssPropertyChanged();
   56        emit ACheckBox::mCheckedChanged(checked);
   57    }
   58
   59    void setUnchecked(bool unchecked = true) {
   60        setChecked(!unchecked);
   61    }
   62
   63    bool consumesClick(const glm::ivec2& pos) override;
   64
   65protected:
   66    bool selectableIsSelectedImpl() override;
   67
   68private:
   69    bool mChecked = false;
   70    emits<bool> mCheckedChanged;
   71};
   72
   73
   78class API_AUI_VIEWS ACheckBoxWrapper: public AViewContainerBase {
   79public:
   80    explicit ACheckBoxWrapper(const _<AView>& viewToWrap);
   81
   82    [[nodiscard]]
   83    auto checked() const {
   84        return mCheckBox->checked();
   85    }
   86
   87    void toggle() {
   88        setChecked(!checked());
   89    }
   90
   91    void check() {
   92        setChecked(true);
   93    }
   94
   95    void uncheck() {
   96        setChecked(false);
   97    }
   98
   99    void setChecked(bool checked = true) {
  100        mCheckBox->setChecked(checked);
  101    }
  102
  103    void setUnchecked(bool unchecked = true) {
  104        setChecked(!unchecked);
  105    }
  106
  107private:
  108    _<ACheckBox> mCheckBox;
  109
  110};
  111
  112
  113template<>
  115public:
  116    static auto property(const _<ACheckBox>& view) {
  117        return view->checked();
  118    }
  119    static void setup(const _<ACheckBox>& view) {}
  120
  121    static auto getGetter() {
  122        return &ACheckBox::checked;
  123    }
  124
  125    static auto getSetter() {
  126        return &ACheckBox::setChecked;
  127    }
  128};
  129
  130
  131template<>
  133public:
  134    static auto property(const _<ACheckBoxWrapper>& view) {
  135        return view->checked();
  136    }
  137    static void setup(const _<ACheckBoxWrapper>& view) {}
  138
  139    static auto getGetter() {
  140        return &ACheckBoxWrapper::checked;
  141    }
  142
  143    static auto getSetter() {
  144        return &ACheckBoxWrapper::setChecked;
  145    }
  146};
  147
  148namespace declarative {
  149    using CheckBox = aui::ui_building::view<ACheckBox>;
  150    using CheckBoxWrapper = aui::ui_building::view<ACheckBoxWrapper>;
  151}
View container with a checkbox.
Definition ACheckBox.h:78
A check box (without label).
Definition ACheckBox.h:27
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