<commit_before>//
//  yas_sample_bg.mm
//

#include "yas_sample_bg.h"

using namespace yas;

struct sample::bg::impl : base::impl {
    ui::rect_plane _rect_plane = ui::make_rect_plane(1);

    impl() {
        _rect_plane.data().set_rect_position({.origin = {-0.5f, -0.5f}, .size = {1.0f, 1.0f}}, 0);
        auto &node = _rect_plane.node();
        node.set_scale({.v = 0.0f});
        node.set_color({.v = 0.75f});
    }

    void prepare(sample::bg &bg) {
        _rect_plane.node().dispatch_method(ui::node::method::renderer_changed);
        _renderer_observer = _rect_plane.node().subject().make_observer(
            ui::node::method::renderer_changed,
            [weak_bg = to_weak(bg), view_size_observer = base{nullptr}](auto const &context) mutable {
                if (auto bg = weak_bg.lock()) {
                    auto impl = bg.impl_ptr<sample::bg::impl>();
                    auto node = context.value;
                    if (auto renderer = node.renderer()) {
                        view_size_observer = _make_view_size_observer(node, renderer);
                    } else {
                        view_size_observer = nullptr;
                    }
                }
            });
    }

   private:
    static base _make_view_size_observer(ui::node &node, ui::renderer &renderer) {
        auto set_scale = [](ui::node &node, ui::uint_size const &view_size) {
            node.set_scale({static_cast<float>(view_size.width), static_cast<float>(view_size.height)});
        };

        set_scale(node, renderer.view_size());

        return renderer.subject().make_observer(
            ui::renderer::method::view_size_changed,
            [weak_node = to_weak(node), set_scale = std::move(set_scale)](auto const &context) {
                if (auto node = weak_node.lock()) {
                    auto const &renderer = context.value;
                    set_scale(node, renderer.view_size());
                }
            });
    }

    ui::node::observer_t _renderer_observer = nullptr;
};

sample::bg::bg() : base(std::make_shared<impl>()) {
    impl_ptr<impl>()->prepare(*this);
}

sample::bg::bg(std::nullptr_t) : base(nullptr) {
}

ui::rect_plane &sample::bg::rect_plane() {
    return impl_ptr<impl>()->_rect_plane;
}<commit_msg>sample bg<commit_after>//
//  yas_sample_bg.mm
//

#include "yas_sample_bg.h"

using namespace yas;

struct sample::bg::impl : base::impl {
    ui::rect_plane _rect_plane = ui::make_rect_plane(1);

    impl() {
        auto &node = _rect_plane.node();
        node.set_color({.v = 0.75f});
    }

    void prepare(sample::bg &bg) {
        _rect_plane.node().dispatch_method(ui::node::method::renderer_changed);
        _renderer_observer = _rect_plane.node().subject().make_observer(ui::node::method::renderer_changed, [
            weak_bg = to_weak(bg), observer = base{nullptr}, safe_area_observer = base{nullptr}
        ](auto const &context) mutable {
            if (auto bg = weak_bg.lock()) {
                auto impl = bg.impl_ptr<sample::bg::impl>();
                auto node = context.value;
                if (auto renderer = node.renderer()) {
                    observer = _make_observer(node, renderer, weak_bg);
                } else {
                    observer = nullptr;
                }
            }
        });
    }

   private:
    static base _make_observer(ui::node &node, ui::renderer &renderer, weak<sample::bg> &weak_bg) {
        auto set_rect = [weak_bg](ui::node &node, ui::renderer const &renderer) {
            if (auto bg = weak_bg.lock()) {
                bg.rect_plane().data().set_rect_position(renderer.safe_area_layout_guide_rect().region(), 0);
            }
        };

        set_rect(node, renderer);

        return renderer.subject().make_wild_card_observer(
            [weak_node = to_weak(node), set_rect = std::move(set_rect)](auto const &context) {
                if (auto node = weak_node.lock()) {
                    switch (context.key) {
                        case ui::renderer::method::view_size_changed:
                        case ui::renderer::method::safe_area_insets_changed: {
                            auto const &renderer = context.value;
                            set_rect(node, renderer);
                        } break;

                        default:
                            break;
                    }
                }
            });
    }

    ui::node::observer_t _renderer_observer = nullptr;
};

sample::bg::bg() : base(std::make_shared<impl>()) {
    impl_ptr<impl>()->prepare(*this);
}

sample::bg::bg(std::nullptr_t) : base(nullptr) {
}

ui::rect_plane &sample::bg::rect_plane() {
    return impl_ptr<impl>()->_rect_plane;
}
{'fraction_non_alphanumeric': 0.1447732595273579, 'fraction_numerical': 0.004258037044922291, 'mean_word_length': 2.1236702127659575, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6593011', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1435, 'n_words': 231}