<commit_before>#include "book.hpp"

#include "../../context.hpp"

using namespace morda;

book::book(std::shared_ptr<morda::context> c, const puu::forest& desc) :
		widget(std::move(c), desc),
		pile(nullptr, puu::forest())
{}

void book::push(std::shared_ptr<page> pg){
	if(!pg){
		throw std::logic_error("book: tried to push nullptr");
	}
	
	auto& lp = this->get_layout_params(*pg);
	lp.dims.set(widget::layout_params::fill);
	
	auto b = utki::make_shared_from_this(*this);
	
	this->context->run_from_ui_thread([b, pg](){
		if(!b->pages.empty()){
			ASSERT(b->children().size() == 1)
			ASSERT(b->active_page_index < b->pages.size())
			auto p = dynamic_cast<page*>(b->pages[b->active_page_index].get());
			if(p){
				p->on_hide();
			}
			b->clear();
		}

		b->active_page_index = b->pages.size();
		b->pages.push_back(pg);
		pg->parent_book = b.get();
		b->push_back(pg);
		pg->on_show();
	});
}

void book::tear_out(page& pg)noexcept{
	ASSERT(&pg.get_parent_book() == this)
	
	auto i = std::find_if(this->pages.begin(), this->pages.end(), [&pg](const decltype(this->pages)::value_type& v) -> bool {
		return v.get() == &pg;
	});

	ASSERT(i != this->pages.end())

	bool is_active_page = std::distance(this->pages.begin(), i) == ssize_t(this->active_page_index);
	
	this->pages.erase(i);
	pg.parent_book = nullptr;

	if(this->active_page_index != 0){
		--this->active_page_index;
	}<commit_msg>stuff<commit_after>#include "book.hpp"

#include "../../context.hpp"

using namespace morda;

book::book(std::shared_ptr<morda::context> c, const puu::forest& desc) :
		widget(std::move(c), desc),
		pile(nullptr, puu::forest())
{}

void book::push(std::shared_ptr<page> pg){
	if(!pg){
		throw std::logic_error("book: tried to push nullptr");
	}
	
	auto& lp = this->get_layout_params(*pg);
	lp.dims.set(widget::layout_params::fill);
	
	auto b = utki::make_shared_from_this(*this);
	
	this->context->run_from_ui_thread([b, pg](){
		if(!b->pages.empty()){
			ASSERT(b->children().size() == 1)
			ASSERT(b->active_page_index < b->pages.size())
			auto p = dynamic_cast<page*>(b->pages[b->active_page_index].get());
			if(p){
				p->on_hide();
			}
			b->clear();
		}

		b->active_page_index = b->pages.size();
		b->pages.push_back(pg);
		pg->parent_book = b.get();
		b->push_back(pg);
		pg->on_show();
	});
}

void book::tear_out(page& pg)noexcept{
	ASSERT(&pg.get_parent_book() == this)
	
	auto i = std::find_if(this->pages.begin(), this->pages.end(), [&pg](const decltype(this->pages)::value_type& v) -> bool {
		return v.get() == &pg;
	});

	ASSERT(i != this->pages.end())

	bool is_active_page = size_t(std::distance(this->pages.begin(), i)) == this->active_page_index;
	
	this->pages.erase(i);
	pg.parent_book = nullptr;

	if(this->active_page_index != 0){
		--this->active_page_index;
	}
{'fraction_non_alphanumeric': 0.22102619303911014, 'fraction_numerical': 0.0014352350197344816, 'mean_word_length': 4.748453608247423, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 63, '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': '5885710', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1084, 'n_words': 164}