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    bool consumesClick(const glm::ivec2& position) override {
   45        return true;
   46    }
   47
   48    void setScroll(glm::uvec2 scroll) {
   49        if (mScroll == scroll) [[unlikely]] {
   50            return;
   51        }
   52        mScroll = scroll;
   53        updateContentsScroll();
   54    }
   55
   56    void setScrollX(unsigned scroll) {
   57        if (mScroll.x == scroll) [[unlikely]] {
   58            return;
   59        }
   60        mScroll.x = scroll;
   61        updateContentsScroll();
   62    }
   63
   64    void setScrollY(unsigned scroll) {
   65        if (mScroll.y == scroll) [[unlikely]] {
   66            return;
   67        }
   68        mScroll.y = scroll;
   69        updateContentsScroll();
   70    }
   71
   72    [[nodiscard]]
   73    auto scroll() const noexcept {
   74        return APropertyDef {
   75            this,
   76            &AScrollAreaViewport::mScroll,
   77            &AScrollAreaViewport::setScroll,
   78            mScrollChanged,
   79        };
   80    }
   81
   96    template<aui::invocable ApplyLayoutUpdate>
   97    void compensateLayoutUpdatesByScroll(_<AView> anchor, ApplyLayoutUpdate&& applyLayoutUpdate, glm::ivec2 diffMask = glm::ivec2(1, 1));
   98
   99private:
  100    _<Inner> mInner;
  101    _<AView> mContents;
  102
  103    glm::uvec2 mScroll = {0, 0};
  104    emits<glm::uvec2> mScrollChanged;
  105
  106    void updateContentsScroll();
  107};
  108
  109template <aui::invocable ApplyLayoutUpdate>
  111    _<AView> anchor, ApplyLayoutUpdate&& applyLayoutUpdate, glm::ivec2 diffMask) {
  112    auto queryRelativePosition = [&] {
  113      glm::ivec2 accumulator{};
  114      for (auto v = anchor.get(); v != nullptr && v->getParent() != this; v = v->getParent()) {
  115          accumulator += v->getPosition();
  116      }
  117      return accumulator;
  118    };
  119    auto before = queryRelativePosition();
  120    applyLayoutUpdate();
  121    auto after = queryRelativePosition();
  122    auto diff = after - before;
  123    diff *= diffMask;
  124    mScroll = glm::max(glm::ivec2(mScroll) + diff, glm::ivec2(0));
  125    updateContentsScroll();
  126}
void applyGeometryToChildren() override
Applies geometry to all children with no preconditions.
bool consumesClick(const glm::ivec2 &position) override
Determines whether this AView processes this click or passes it thru.
Definition AScrollAreaViewport.h:44
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:110
void setContents(const _< AViewContainer > &container)
Moves (like via std::move) all children and layout of the specified container to this container.
auto position() const
Top left corner's position relative to top left corner's position of the parent AView.
Definition AView.h:102
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:215
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:577