Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

browser/gui: Add a --exit-after-load flag #782

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 131 additions & 127 deletions browser/gui/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,164 +244,168 @@ void App::set_scale(unsigned scale) {
engine_.set_layout_width(window_size.x / scale_);
}

int App::run() {
while (window_.isOpen()) {
sf::Event event{};
while (window_.pollEvent(event)) {
// ImGui needs a few iterations to do what it wants to do. This was
// pretty much picked at random after I still occasionally got
// unexpected results when giving it 2 iterations.
process_iterations_ = 5;
ImGui::SFML::ProcessEvent(event);

switch (event.type) {
case sf::Event::Closed: {
window_.close();
break;
}
case sf::Event::Resized: {
canvas_->set_viewport_size(event.size.width, event.size.height);
engine_.set_layout_width(event.size.width / scale_);
void App::step() {
sf::Event event{};
while (window_.pollEvent(event)) {
// ImGui needs a few iterations to do what it wants to do. This was
// pretty much picked at random after I still occasionally got
// unexpected results when giving it 2 iterations.
process_iterations_ = 5;
ImGui::SFML::ProcessEvent(event);

switch (event.type) {
case sf::Event::Closed: {
window_.close();
break;
}
case sf::Event::Resized: {
canvas_->set_viewport_size(event.size.width, event.size.height);
engine_.set_layout_width(event.size.width / scale_);
break;
}
case sf::Event::KeyPressed: {
if (ImGui::GetIO().WantCaptureKeyboard) {
break;
}
case sf::Event::KeyPressed: {
if (ImGui::GetIO().WantCaptureKeyboard) {

switch (event.key.code) {
case sf::Keyboard::Key::J: {
scroll(event.key.shift ? -20 : -5);
break;
}

switch (event.key.code) {
case sf::Keyboard::Key::J: {
scroll(event.key.shift ? -20 : -5);
break;
}
case sf::Keyboard::Key::K: {
scroll(event.key.shift ? 20 : 5);
break;
}
case sf::Keyboard::Key::L: {
if (!event.key.control) {
break;
}
focus_url_input();
break;
}
case sf::Keyboard::Key::F1: {
render_debug_ = !render_debug_;
break;
}
case sf::Keyboard::Key::F2: {
switch_canvas();
break;
}
case sf::Keyboard::Key::F4: {
display_debug_gui_ = !display_debug_gui_;
break;
}
case sf::Keyboard::Key::Left: {
if (!event.key.alt) {
break;
}
navigate_back();
case sf::Keyboard::Key::K: {
scroll(event.key.shift ? 20 : 5);
break;
}
case sf::Keyboard::Key::L: {
if (!event.key.control) {
break;
}
case sf::Keyboard::Key::Right: {
if (!event.key.alt) {
break;
}
navigate_forward();
focus_url_input();
break;
}
case sf::Keyboard::Key::F1: {
render_debug_ = !render_debug_;
break;
}
case sf::Keyboard::Key::F2: {
switch_canvas();
break;
}
case sf::Keyboard::Key::F4: {
display_debug_gui_ = !display_debug_gui_;
break;
}
case sf::Keyboard::Key::Left: {
if (!event.key.alt) {
break;
}
case sf::Keyboard::Key::Backspace: {
navigate_back();
navigate_back();
break;
}
case sf::Keyboard::Key::Right: {
if (!event.key.alt) {
break;
}
default:
break;
}
break;
}
case sf::Event::MouseMoved: {
if (!page_loaded_) {
navigate_forward();
break;
}

auto window_position = geom::Position{event.mouseMove.x, event.mouseMove.y};
auto document_position = to_document_position(std::move(window_position));
auto const *hovered = get_hovered_node(document_position);
nav_widget_extra_info_ =
fmt::format("{},{}: {}", document_position.x, document_position.y, element_text(hovered));

// If imgui is dealing with the mouse, we do nothing and let imgui change the cursor.
if (ImGui::GetIO().WantCaptureMouse) {
ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NoMouseCursorChange;
case sf::Keyboard::Key::Backspace: {
navigate_back();
break;
}
default:
break;
}
break;
}
case sf::Event::MouseMoved: {
if (!page_loaded_) {
break;
}

// Otherwise we tell imgui not to mess with the cursor, and change it according to what we're
// currently hovering over.
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
if (try_get_uri(hovered).has_value()) {
cursor_.loadFromSystem(sf::Cursor::Hand);
} else {
cursor_.loadFromSystem(sf::Cursor::Arrow);
}
window_.setMouseCursor(cursor_);
auto window_position = geom::Position{event.mouseMove.x, event.mouseMove.y};
auto document_position = to_document_position(std::move(window_position));
auto const *hovered = get_hovered_node(document_position);
nav_widget_extra_info_ =
fmt::format("{},{}: {}", document_position.x, document_position.y, element_text(hovered));

// If imgui is dealing with the mouse, we do nothing and let imgui change the cursor.
if (ImGui::GetIO().WantCaptureMouse) {
ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NoMouseCursorChange;
break;
}
case sf::Event::MouseButtonReleased: {
if (ImGui::GetIO().WantCaptureMouse || event.mouseButton.button != sf::Mouse::Left) {
break;
}

auto window_position = geom::Position{event.mouseButton.x, event.mouseButton.y};
auto document_position = to_document_position(std::move(window_position));
auto const *hovered = get_hovered_node(std::move(document_position));
if (auto uri = try_get_uri(hovered); uri.has_value()) {
url_buf_ = std::string{*uri};
navigate();
}
// Otherwise we tell imgui not to mess with the cursor, and change it according to what we're
// currently hovering over.
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
if (try_get_uri(hovered).has_value()) {
cursor_.loadFromSystem(sf::Cursor::Hand);
} else {
cursor_.loadFromSystem(sf::Cursor::Arrow);
}
window_.setMouseCursor(cursor_);

break;
}
case sf::Event::MouseButtonReleased: {
if (ImGui::GetIO().WantCaptureMouse || event.mouseButton.button != sf::Mouse::Left) {
break;
}
case sf::Event::MouseWheelScrolled: {
if (ImGui::GetIO().WantCaptureMouse
|| event.mouseWheelScroll.wheel != sf::Mouse::Wheel::VerticalWheel) {
break;
}

scroll(std::lround(event.mouseWheelScroll.delta) * kMouseWheelScrollFactor);
break;
auto window_position = geom::Position{event.mouseButton.x, event.mouseButton.y};
auto document_position = to_document_position(std::move(window_position));
auto const *hovered = get_hovered_node(std::move(document_position));
if (auto uri = try_get_uri(hovered); uri.has_value()) {
url_buf_ = std::string{*uri};
navigate();
}
default:

break;
}
case sf::Event::MouseWheelScrolled: {
if (ImGui::GetIO().WantCaptureMouse
|| event.mouseWheelScroll.wheel != sf::Mouse::Wheel::VerticalWheel) {
break;
}

scroll(std::lround(event.mouseWheelScroll.delta) * kMouseWheelScrollFactor);
break;
}
default:
break;
}
}

if (process_iterations_ == 0) {
// The sleep duration was picked at random.
std::this_thread::sleep_for(std::chrono::milliseconds{5});
continue;
}
process_iterations_ -= 1;

run_overlay();
run_nav_widget();
if (display_debug_gui_) {
run_http_response_widget();
run_dom_widget();
run_stylesheet_widget();
run_layout_widget();
}
if (process_iterations_ == 0) {
// The sleep duration was picked at random.
std::this_thread::sleep_for(std::chrono::milliseconds{5});
return;
}
process_iterations_ -= 1;

clear_render_surface();
run_overlay();
run_nav_widget();
if (display_debug_gui_) {
run_http_response_widget();
run_dom_widget();
run_stylesheet_widget();
run_layout_widget();
}

if (page_loaded_) {
render_layout();
}
clear_render_surface();

if (page_loaded_) {
render_layout();
}

render_overlay();
show_render_surface();
render_overlay();
show_render_surface();
}

int App::run() {
while (window_.isOpen()) {
step();
}

return 0;
Expand Down
1 change: 1 addition & 0 deletions browser/gui/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class App final {
~App();

void set_scale(unsigned scale);
void step();
int run();

private:
Expand Down
16 changes: 15 additions & 1 deletion browser/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ int main(int argc, char **argv) {

std::optional<std::string> page_provided{std::nullopt};
std::optional<unsigned> scale{std::nullopt};
bool exit_after_load{false};

for (int i = 1; i < argc; ++i) {
auto arg = std::string_view{argv[i]};

Expand All @@ -54,6 +56,11 @@ int main(int argc, char **argv) {
continue;
}

if (arg == "--exit-after-load"sv) {
exit_after_load = true;
continue;
}

if (i == argc - 1) {
page_provided = std::string{arg};
break;
Expand All @@ -65,5 +72,12 @@ int main(int argc, char **argv) {

browser::gui::App app{kBrowserTitle, page_provided.value_or(std::string{kStartpage}), page_provided.has_value()};
app.set_scale(scale.value_or(os::active_window_scale_factor()));
return app.run();

if (!exit_after_load) {
return app.run();
}

app.step();
spdlog::info("Page loaded, exiting...");
return 0;
}