AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ABasicListEditor.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
   15#include <AUI/View/AViewContainer.h>
   16#include <AUI/Model/IListModel.h>
   17#include <AUI/Model/IMutableListModel.h>
   18#include <AUI/Platform/AWindow.h>
   19#include <AUI/Util/UIBuildingHelpers.h>
   20#include <AUI/View/AListView.h>
   21#include <AUI/View/AButton.h>
   22
   23
   27class ABasicListEditor: public AViewContainerBase {
   28private:
   29
   30public:
   31
   32    class Builder {
   33    private:
   34        _<IListModel<AString>> mModel;
   35        AString mTitle;
   36        std::function<void()> mNewCallback;
   37        std::function<void(const AListModelIndex&)> mModifyCallback;
   38
   39    public:
   40        Builder(const _<IListModel<AString>>& model): mModel(model) {}
   41
   42
   43        Builder& withNewButton(const std::function<void()>& onNew) {
   44            mNewCallback = onNew;
   45            return *this;
   46        }
   47        Builder& withModifyButton(const std::function<void(const AListModelIndex&)>& onModify) {
   48            mModifyCallback = onModify;
   49            return *this;
   50        }
   51
   52        _<AWindow> buildWindow(const AString& title, AWindow* parent = nullptr) {
   53            auto window = AWindow::wrapViewToWindow(buildContainer(),
   54                                                    title,
   55                                                    300_dp,
   56                                                    400_dp,
   57                                                    parent,
   58                                                    WindowStyle::MODAL |
   59                                                    WindowStyle::NO_RESIZE |
   60                                                    WindowStyle::NO_MINIMIZE_MAXIMIZE);
   61            return window;
   62        }
   63
   64        _<ABasicListEditor> buildContainer() {
   65            auto c = _new<ABasicListEditor>();
   66            auto list = _new<AListView>(mModel);
   67            list with_style { ass::MinSize { 200_dp, {} } };
   68
   69            auto modifyButton = mModifyCallback ? _new<AButton>("Modify...").connect(&AView::clicked, c, [callback = mModifyCallback, list] {
   70                callback(list->getSelectionModel().first());
   71            }) : nullptr;
   72
   73            _<AButton> removeButton;
   74            if (auto model = _cast<IRemovableListModel<AString>>(mModel)) {
   75                removeButton = _new<AButton>("Remove").connect(&AView::clicked, c, [list, model] {
   76                    model->removeItems(list->getSelectionModel());
   77                });
   78            }
   79
   80            if (mModel->listSize() != 0) {
   81                list->selectItem(0);
   82            } else {
   83                AUI_NULLSAFE(removeButton)->disable();
   84                AUI_NULLSAFE(modifyButton)->disable();
   85            }
   86            auto updateEnabledState = [list, modifyButton, removeButton]() {
   87                auto& s = list->getSelectionModel().getIndices();
   88                AUI_NULLSAFE(modifyButton)->setDisabled(s.empty());
   89                AUI_NULLSAFE(removeButton)->setDisabled(s.empty());
   90            };
   91            connect(list->selectionChanged, c, updateEnabledState);
   92            connect(mModel->dataRemoved, c, updateEnabledState);
   93            connect(mModel->dataInserted, c, updateEnabledState);
   94
   95            c->setContents(Horizontal {
   96                list,
   97                Vertical {
   98                    mNewCallback ? _new<AButton>("New...").connect(&AView::clicked, c, [callback = mNewCallback] {
   99                        callback();
  100                    }) : nullptr,
  101                    modifyButton,
  102                    removeButton
  103                },
  104            });
  105
  106            return c;
  107        }
  108    };
  109};
  110
  111
Helper UI to edit list models.
Definition ABasicListEditor.h:27
Definition AListModelIndex.h:20
Represents a Unicode character string.
Definition AString.h:38
emits clicked
Left mouse button clicked.
Definition AView.h:933
Represents a window in the underlying windowing system.
Definition AWindow.h:45
static _< AWindow > wrapViewToWindow(const _< AView > &view, const AString &title, int width=854_dp, int height=500_dp, AWindow *parent=nullptr, WindowStyle ws=WindowStyle::DEFAULT)
Wraps your AView to window.
Definition IListModel.h:23
Definition IMutableListModel.h:20
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
static decltype(auto) connect(const Signal &signal, Object *object, Function &&function)
Connects signal to the slot of the specified object.
Definition AObject.h:86
#define with_style
Allows to define a style to the view right in place.
Definition kAUI.h:287
Controls the min size of AView.
Definition MinSize.h:28