<commit_before>// Copyright (c) 2014 <NAME>.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

#include <iostream>
#include <volo.h>

using namespace volo;

const Glib::ustring blank_page = "about:blank";

WebContext WebContext::get_default() {
	return WebContext{};
}

void WebContext::set_process_model(WebKitProcessModel model) {
	webkit_web_context_set_process_model(wc, model);
}


static void WebView_load_changed_callback(WebKitWebView *wv,
	WebKitLoadEvent ev, gpointer signal) {

	static_cast<sigc::signal<void, WebKitLoadEvent> *>(signal)->emit(ev);
}

static void BackForwardList_changed_callback(WebKitBackForwardList *list,
	WebKitBackForwardListItem *item, gpointer removed, gpointer signal) {

	static_cast<sigc::signal<void, WebKitBackForwardList *> *>(signal)->emit(list);
}

static void WebView_notify_title_callback(GObject *, GParamSpec *, gpointer signal) {
	static_cast<sigc::signal<void> *>(signal)->emit();
}

WebView::WebView(WebKitWebView *wv) : Gtk::Widget{GTK_WIDGET(wv)} {
	g_signal_connect(wv, "load-changed",
		G_CALLBACK(WebView_load_changed_callback),
		&signal_load_changed);

	auto bfl = webkit_web_view_get_back_forward_list(wv);
	g_signal_connect(bfl, "changed",
		G_CALLBACK(BackForwardList_changed_callback),
		&signal_back_forward_list_changed);

	g_signal_connect(wv, "notify::title",
		G_CALLBACK(WebView_notify_title_callback),
		&signal_notify_title);
}

WebView::WebView(const Glib::ustring& uri) : WebView{} {
	load_uri(uri);
}

void WebView::load_uri(const Glib::ustring& uri) {
	webkit_web_view_load_uri(gobj(), uri.c_str());
}

void WebView::go_back() {
	webkit_web_view_go_back(gobj());
};

void WebView::go_forward() {
	webkit_web_view_go_forward(gobj());
};

sigc::connection WebView::connect_load_changed(
	std::function<void(WebKitLoadEvent)> handler) {

	return signal_load_changed.connect(handler);
}

sigc::connection WebView::connect_back_forward_list_changed(
	std::function<void(WebKitBackForwardList *)> handler) {

	return signal_back_forward_list_changed.connect(handler);
}

sigc::connection WebView::connect_notify_title(std::function<void()> handler) {
	return signal_notify_title.connect(handler);
}

void WebView::on_load_changed(WebKitLoadEvent ev) {
	std::cout << ev << '\n';
}


const std::initializer_list<Glib::ustring> default_session = {
	"https://duckduckgo.com/lite",
	"https://github.com",
};

Browser::Browser(const std::vector<Glib::ustring>& uris) {
	auto wc = WebContext::get_default();
	wc.set_process_model(WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);

	// Set navigation button images.  No gtkmm constructor wraps
	// gtk_button_new_from_icon_name (due to a conflict with
	// gtk_button_new_with_label) so this must be done in the Browser
	// constructor.
	back.set_image_from_icon_name("go-previous");
	fwd.set_image_from_icon_name("go-next");
	//stop.set_image_from_icon_name("process-stop");
	//reload.set_image_from_icon_name("view-refresh");
	new_tab.set_image_from_icon_name("add");

	auto histnav_style = histnav.get_style_context();
	histnav_style->add_class("raised");
	histnav_style->add_class("linked");
	histnav.add(back);
	histnav.add(fwd);
	navbar.pack_start(histnav);

	nav_entry.set_input_purpose(Gtk::INPUT_PURPOSE_URL);
	nav_entry.set_hexpand(true);
	navbar.set_custom_title(nav_entry);

	new_tab.set_can_focus(false);
	new_tab.set_relief(Gtk::RELIEF_NONE);
	new_tab.show();
	navbar.pack_end(new_tab);

	navbar.set_show_close_button(true);

	set_title("volo");
	set_default_size(1024, 768);
	set_titlebar(navbar);
	nb.set_scrollable();
	add(nb);

	tabs.reserve(uris.size());
	for (const auto& uri : uris) {
		open_new_tab(uri);
	}

	nb.signal_switch_page().connect([this] (auto, guint page_num) {
		switch_page(page_num);
	});
	nb.signal_page_added().connect([this] (...) {
		nb.set_show_tabs(true);
	});
	nb.signal_page_removed().connect([this] (...) {
		nb.set_show_tabs(nb.get_n_pages() > 1);
	});
	new_tab.signal_clicked().connect([this] {
		auto n = open_new_tab("https://duckduckgo.com/lite");
		nb.set_current_page(n);
	});

	show_webview(0, tabs.front()->wv);

	show_all_children();
}

Browser::Tab::Tab(const Glib::ustring& uri) : wv{uri} {
	tab_title = make_managed<Gtk::Label>("New tab");
	tab_close = make_managed<Gtk::Button>();
	tab_content = make_managed<Gtk::Grid>();

	tab_title->set_can_focus(false);
	tab_title->set_hexpand(true);
	tab_title->set_ellipsize(Pango::ELLIPSIZE_END);
	tab_title->set_size_request(150, -1);

	tab_close->set_image_from_icon_name("window-close");

	tab_content->set_can_focus(false);
	tab_content->set_can_focus(false);

	tab_content->add(*tab_title);
	tab_content->add(*tab_close);
}

int Browser::open_new_tab(const Glib::ustring& uri) {
	tabs.emplace_back(std::make_unique<Tab>(uri));<commit_msg>Reduce smallest tab title size to 50 base units.<commit_after>// Copyright (c) 2014 <NAME>.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

#include <iostream>
#include <volo.h>

using namespace volo;

const Glib::ustring blank_page = "about:blank";

WebContext WebContext::get_default() {
	return WebContext{};
}

void WebContext::set_process_model(WebKitProcessModel model) {
	webkit_web_context_set_process_model(wc, model);
}


static void WebView_load_changed_callback(WebKitWebView *wv,
	WebKitLoadEvent ev, gpointer signal) {

	static_cast<sigc::signal<void, WebKitLoadEvent> *>(signal)->emit(ev);
}

static void BackForwardList_changed_callback(WebKitBackForwardList *list,
	WebKitBackForwardListItem *item, gpointer removed, gpointer signal) {

	static_cast<sigc::signal<void, WebKitBackForwardList *> *>(signal)->emit(list);
}

static void WebView_notify_title_callback(GObject *, GParamSpec *, gpointer signal) {
	static_cast<sigc::signal<void> *>(signal)->emit();
}

WebView::WebView(WebKitWebView *wv) : Gtk::Widget{GTK_WIDGET(wv)} {
	g_signal_connect(wv, "load-changed",
		G_CALLBACK(WebView_load_changed_callback),
		&signal_load_changed);

	auto bfl = webkit_web_view_get_back_forward_list(wv);
	g_signal_connect(bfl, "changed",
		G_CALLBACK(BackForwardList_changed_callback),
		&signal_back_forward_list_changed);

	g_signal_connect(wv, "notify::title",
		G_CALLBACK(WebView_notify_title_callback),
		&signal_notify_title);
}

WebView::WebView(const Glib::ustring& uri) : WebView{} {
	load_uri(uri);
}

void WebView::load_uri(const Glib::ustring& uri) {
	webkit_web_view_load_uri(gobj(), uri.c_str());
}

void WebView::go_back() {
	webkit_web_view_go_back(gobj());
};

void WebView::go_forward() {
	webkit_web_view_go_forward(gobj());
};

sigc::connection WebView::connect_load_changed(
	std::function<void(WebKitLoadEvent)> handler) {

	return signal_load_changed.connect(handler);
}

sigc::connection WebView::connect_back_forward_list_changed(
	std::function<void(WebKitBackForwardList *)> handler) {

	return signal_back_forward_list_changed.connect(handler);
}

sigc::connection WebView::connect_notify_title(std::function<void()> handler) {
	return signal_notify_title.connect(handler);
}

void WebView::on_load_changed(WebKitLoadEvent ev) {
	std::cout << ev << '\n';
}


const std::initializer_list<Glib::ustring> default_session = {
	"https://duckduckgo.com/lite",
	"https://github.com",
};

Browser::Browser(const std::vector<Glib::ustring>& uris) {
	auto wc = WebContext::get_default();
	wc.set_process_model(WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);

	// Set navigation button images.  No gtkmm constructor wraps
	// gtk_button_new_from_icon_name (due to a conflict with
	// gtk_button_new_with_label) so this must be done in the Browser
	// constructor.
	back.set_image_from_icon_name("go-previous");
	fwd.set_image_from_icon_name("go-next");
	//stop.set_image_from_icon_name("process-stop");
	//reload.set_image_from_icon_name("view-refresh");
	new_tab.set_image_from_icon_name("add");

	auto histnav_style = histnav.get_style_context();
	histnav_style->add_class("raised");
	histnav_style->add_class("linked");
	histnav.add(back);
	histnav.add(fwd);
	navbar.pack_start(histnav);

	nav_entry.set_input_purpose(Gtk::INPUT_PURPOSE_URL);
	nav_entry.set_hexpand(true);
	navbar.set_custom_title(nav_entry);

	new_tab.set_can_focus(false);
	new_tab.set_relief(Gtk::RELIEF_NONE);
	new_tab.show();
	navbar.pack_end(new_tab);

	navbar.set_show_close_button(true);

	set_title("volo");
	set_default_size(1024, 768);
	set_titlebar(navbar);
	nb.set_scrollable();
	add(nb);

	tabs.reserve(uris.size());
	for (const auto& uri : uris) {
		open_new_tab(uri);
	}

	nb.signal_switch_page().connect([this] (auto, guint page_num) {
		switch_page(page_num);
	});
	nb.signal_page_added().connect([this] (...) {
		nb.set_show_tabs(true);
	});
	nb.signal_page_removed().connect([this] (...) {
		nb.set_show_tabs(nb.get_n_pages() > 1);
	});
	new_tab.signal_clicked().connect([this] {
		auto n = open_new_tab("https://duckduckgo.com/lite");
		nb.set_current_page(n);
	});

	show_webview(0, tabs.front()->wv);

	show_all_children();
}

Browser::Tab::Tab(const Glib::ustring& uri) : wv{uri} {
	tab_title = make_managed<Gtk::Label>("New tab");
	tab_close = make_managed<Gtk::Button>();
	tab_content = make_managed<Gtk::Grid>();

	tab_title->set_can_focus(false);
	tab_title->set_hexpand(true);
	tab_title->set_ellipsize(Pango::ELLIPSIZE_END);
	tab_title->set_size_request(50, -1);

	tab_close->set_image_from_icon_name("window-close");

	tab_content->set_can_focus(false);
	tab_content->set_can_focus(false);

	tab_content->add(*tab_title);
	tab_content->add(*tab_close);
}

int Browser::open_new_tab(const Glib::ustring& uri) {
	tabs.emplace_back(std::make_unique<Tab>(uri));
{'fraction_non_alphanumeric': 0.14755288262131896, 'fraction_numerical': 0.003629199502281211, 'mean_word_length': 7.401567944250871, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 71, 'https://': 6, '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': '387917', 'n_tokens_mistral': 3733, 'n_tokens_neox': 3605, 'n_words': 503}