AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AScrollAreaViewport.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 "AViewContainer.h"
   16#include "AScrollbar.h"
   17#include "glm/fwd.hpp"
   18
   19
   28class API_AUI_VIEWS AScrollAreaViewport: public AViewContainerBase {
   29public:
   30    class Inner;
   31
   32    AScrollAreaViewport();
   33    ~AScrollAreaViewport() override;
   34
   35    void setContents(_<AView> content);
   36
   37    [[nodiscard]]
   38    const _<AView>& contents() const noexcept {
   39        return mContents;
   40    }
   41
   42    void applyGeometryToChildren() override;
   43  
   44    void setScroll(glm::uvec2 scroll) {
   45        if (mScroll == scroll) [[unlikely]] {
   46            return;
   47        }
   48        mScroll = scroll;
   49        updateContentsScroll();
   50    }
   51
   52    void setScrollX(unsigned scroll) {
   53        if (mScroll.x == scroll) [[unlikely]] {
   54            return;
   55        }
   56        mScroll.x = scroll;
   57        updateContentsScroll();
   58    }
   59
   60    void setScrollY(unsigned scroll) {
   61        if (mScroll.y == scroll) [[unlikely]] {
   62            return;
   63        }
   64        mScroll.y = scroll;
   65        updateContentsScroll();
   66    }
   67
   68    [[nodiscard]]
   69    auto scroll() const noexcept {
   70        return APropertyDef {
   71            this,
   72            &AScrollAreaViewport::mScroll,
   73            &AScrollAreaViewport::setScroll,
   74            mScrollChanged,
   75        };
   76    }
   77
   92    template<aui::invocable ApplyLayoutUpdate>
   93    void compensateLayoutUpdatesByScroll(_<AView> anchor, ApplyLayoutUpdate&& applyLayoutUpdate, glm::ivec2 diffMask = glm::ivec2(1, 1));
   94
   95private:
   96    _<Inner> mInner;
   97    _<AView> mContents;
   98
   99    glm::uvec2 mScroll = {0, 0};
  100    emits<glm::uvec2> mScrollChanged;
  101
  102    void updateContentsScroll();
  103};
  104
  105template <aui::invocable ApplyLayoutUpdate>
  107    _<AView> anchor, ApplyLayoutUpdate&& applyLayoutUpdate, glm::ivec2 diffMask) {
  108    auto queryRelativePosition = [&] {
  109      glm::ivec2 accumulator{};
  110      for (auto v = anchor.get(); v != nullptr && v->getParent() != this; v = v->getParent()) {
  111          accumulator += v->getPosition();
  112      }
  113      return accumulator;
  114    };
  115    auto before = queryRelativePosition();
  116    applyLayoutUpdate();
  117    auto after = queryRelativePosition();
  118    auto diff = after - before;
  119    diff *= diffMask;
  120    mScroll = glm::max(glm::ivec2(mScroll) + diff, glm::ivec2(0));
  121    updateContentsScroll();
  122}
void compensateLayoutUpdatesByScroll(_< AView > anchor, ApplyLayoutUpdate &&applyLayoutUpdate, glm::ivec2 diffMask=glm::ivec2(1, 1))
Compensates layout updates made in applyLayoutUpdate by scrolling by a diff of relative position of a...
Definition AScrollAreaViewport.h:106
void setContents(const _< AViewContainer > &container)
Moves (like via std::move) all children and layout of the specified container to this container.
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:572
Property implementation to use with custom getter/setter.
Definition AProperty.h:234