Skip to content

Commit

Permalink
browser/gui: Display favicons if available
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Nov 14, 2023
1 parent f9d6b69 commit e69b430
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions browser/gui/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "render/render.h"
#include "uri/uri.h"

#include <SFML/Graphics/Image.hpp>
#include <SFML/Window/Event.hpp>
#include <fmt/format.h>
#include <imgui-SFML.h>
Expand All @@ -24,6 +25,7 @@
#include <cstdlib>
#include <functional>
#include <optional>
#include <ranges>
#include <sstream>
#include <string_view>
#include <utility>
Expand Down Expand Up @@ -327,6 +329,7 @@ int App::run() {
}

void App::navigate() {
// TODO(robinlinden): Unset favicon. May require us to have a Hastur icon.
page_loaded_ = false;
auto uri = uri::Uri::parse(url_buf_, engine_.uri());
browse_history_.push(uri);
Expand Down Expand Up @@ -395,6 +398,27 @@ void App::on_page_loaded() {
window_.setTitle(browser_title_);
}

// TODO(robinlinden): Non-blocking load of favicon.
// https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#icon
auto is_favicon_link = [](dom::Element const *v) {
auto rel = v->attributes.find("rel");
return rel != end(v->attributes) && rel->second == "icon" && v->attributes.contains("href");
};

for (auto const &link : dom::nodes_by_xpath(engine_.dom().html(), "/html/head/link")
| std::views::filter(is_favicon_link) | std::views::reverse) {
auto uri = uri::Uri::parse(link->attributes.at("href"), engine_.uri());
auto icon = engine_.load(uri).response;
sf::Image favicon;
if (icon.err != protocol::Error::Ok || !favicon.loadFromMemory(icon.body.data(), icon.body.size())) {
spdlog::warn("Error loading favicon from '{}': {}", uri.uri, to_string(icon.err));
continue;
}

window_.setIcon(favicon.getSize().x, favicon.getSize().y, favicon.getPixelsPtr());
break;
}

update_status_line();
response_headers_str_ = engine_.response().headers.to_string();
dom_str_ = dom::to_string(engine_.dom());
Expand Down

0 comments on commit e69b430

Please sign in to comment.