AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
Align.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//
   13// Created by Alex2772 on 12/5/2021.
   14//
   15
   16#pragma once
   17
   18#include <AUI/Util/AMetric.h>
   19#include <AUI/Common/ASide.h>
   20#include "Traits.h"
   21
   22namespace uitest::impl {
   23    template<ASide side>
   24    struct side_value {};
   25
   26    template<>
   27    struct side_value<ASide::LEFT> {
   28        int operator()(const _<AView>& view) { return view->getPositionInWindow().x; }
   29    };
   30
   31    template<>
   32    struct side_value<ASide::TOP> {
   33        int operator()(const _<AView>& view) { return view->getPositionInWindow().y; }
   34    };
   35
   36    template<>
   37    struct side_value<ASide::RIGHT> {
   38        int operator()(const _<AView>& view) { return view->getPositionInWindow().x + view->getSize().x; }
   39    };
   40
   41    template<>
   42    struct side_value<ASide::BOTTOM> {
   43        int operator()(const _<AView>& view) { return view->getPositionInWindow().y + view->getSize().y; }
   44    };
   45
   46
   47    template<ASide side>
   48    struct align {
   49        AOptional<int> value;
   50
   51        bool operator()(const _<AView>& v) {
   52            int current = side_value<side>{}(v);
   53            if (!value) {
   54                value = current;
   55                return true;
   56            }
   57            return current == *value;
   58        }
   59    };
   60
   61    template<ASide side>
   62    struct less_of {
   63        UIMatcher matcher;
   64
   65        less_of(UIMatcher matcher) : matcher(std::move(matcher)) {}
   66
   67        bool operator()(const _<AView>& lhs) {
   68            auto views = matcher.toSet();
   69            return std::all_of(views.begin(), views.end(), [&](const _<AView>& rhs) {
   70                return side_value<side>{}(lhs) < side_value<side>{}(rhs);
   71            });
   72        }
   73    };
   74
   75    template<ASide side>
   76    struct greater_of {
   77        UIMatcher matcher;
   78
   79        greater_of(UIMatcher matcher) : matcher(std::move(matcher)) {}
   80
   81        bool operator()(const _<AView>& lhs) {
   82            auto views = matcher.toSet();
   83            return std::all_of(views.begin(), views.end(), [&](const _<AView>& rhs) {
   84                return side_value<side>{}(lhs) > side_value<side>{}(rhs);
   85            });
   86        }
   87    };
   88}
   89
   90using areLeftAligned = uitest::impl::align<ASide::LEFT>;
   91using areTopAligned = uitest::impl::align<ASide::TOP>;
   92using areRightAligned = uitest::impl::align<ASide::RIGHT>;
   93using areBottomAligned = uitest::impl::align<ASide::BOTTOM>;
   94
   95
   96using isTopAboveTopOf = uitest::impl::less_of<ASide::TOP>;
   97using isTopBelowTopOf = uitest::impl::greater_of<ASide::TOP>;
   98using isBottomAboveBottomOf = uitest::impl::less_of<ASide::BOTTOM>;
   99using isBottomBelowBottomOf = uitest::impl::greater_of<ASide::BOTTOM>;
  100
  101
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition AOptional.h:33
Definition UIMatcher.h:21
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
@ RIGHT
Definition AFloat.h:35
@ LEFT
Definition AFloat.h:29
Definition Align.h:48
Definition Traits.h:23
Definition Align.h:76
Definition Align.h:62
Definition Align.h:24