diff --git a/.gitignore b/.gitignore index 96e46c4..9798a03 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build .cache -imgui.ini \ No newline at end of file +imgui.ini +.DS_Store diff --git a/build_example.sh b/build_example.sh index 4ffdcb2..d976cc4 100755 --- a/build_example.sh +++ b/build_example.sh @@ -1,4 +1,4 @@ #!/bin/bash -cmake -B build -DCMAKE_BUILD_TYPE=Release -cmake --build build \ No newline at end of file +cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_POLICY_VERSION_MINIMUM=3.5 +cmake --build build diff --git a/examples/hello_world.html b/examples/hello_world.html index be955a7..8183bcd 100644 --- a/examples/hello_world.html +++ b/examples/hello_world.html @@ -26,8 +26,8 @@

ImHTML Example ({clicks} clicks)

Text

-

- Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam +

+ Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet @@ -36,7 +36,7 @@

Text

Background Color & Border

Box Test

Links

- GitHub + GitHub

Flexbox

1
@@ -46,7 +46,7 @@

Flexbox

Image

- + ./images/example.jpg

Lists

  • Item 1
  • diff --git a/fonts/JetBrainsMono-Bold.ttf b/fonts/JetBrainsMono-Bold.ttf new file mode 100644 index 0000000..977400f Binary files /dev/null and b/fonts/JetBrainsMono-Bold.ttf differ diff --git a/fonts/NotoSans-Bold.ttf b/fonts/NotoSans-Bold.ttf new file mode 100644 index 0000000..899a4cb Binary files /dev/null and b/fonts/NotoSans-Bold.ttf differ diff --git a/imhtml.cpp b/imhtml.cpp index 72e107b..5360803 100644 --- a/imhtml.cpp +++ b/imhtml.cpp @@ -143,6 +143,7 @@ class BrowserContainer : public litehtml::document_container { private: ImVec2 bottomRight = ImVec2(0, 0); std::string title = "Browser"; + std::string tooltip = ""; std::string loadUrl = ""; std::string currentUrl = ""; std::vector history = {}; @@ -158,6 +159,7 @@ class BrowserContainer : public litehtml::document_container { bottomRight.x = std::max(bottomRight.x, point.x); bottomRight.y = std::max(bottomRight.y, point.y); } + std::string get_tooltip() { return tooltip; } std::string get_title() { return title; } std::string pop_load_url() { if (loadUrl.empty()) { @@ -211,6 +213,11 @@ class BrowserContainer : public litehtml::document_container { } ImFont* font = resolveFont(config, descr.family, font_style); + if (font != nullptr) { + IMHTML_PRINTF("[ImHTML] Resolved font for weight=%i style=%i\n", static_cast(descr.weight), static_cast(descr.style)); + } else { + IMHTML_PRINTF("[ImHTML] Failed to resolve font\n"); + } auto rf = std::make_unique(); rf->Font = font; @@ -895,7 +902,27 @@ class BrowserContainer : public litehtml::document_container { } virtual void on_mouse_event(const litehtml::element::ptr& el, litehtml::mouse_event event) override { - // TODO + if (el != nullptr && ImGui::IsWindowHovered()) { + const char* attr = el->get_attr("tooltip"); + if (event == litehtml::mouse_event_enter) { + if (attr != nullptr) { + tooltip = std::string(attr); + } else { + const char* tag = el->get_tagName(); + if (tag != nullptr) { + if (config.AllowHrefTooltips && std::string(tag) == "a" && (attr = el->get_attr("href")) != nullptr) { + tooltip = std::string(attr); + } else if (config.AllowImgAltTooltips && std::string(tag) == "img" && (attr = el->get_attr("alt")) != nullptr) { + tooltip = std::string(attr); + } + } + } + } else if (event == litehtml::mouse_event_leave) { + tooltip = ""; + } + } else { + tooltip = ""; + } } virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, @@ -1135,6 +1162,10 @@ bool Canvas(const char* id, const char* html, float width, std::string* clickedU ImGui::ItemSize(bb.GetSize()); ImGui::ItemAdd(bb, ImGui::GetID(id)); + if (!state.container->get_tooltip().empty()) { + ImGui::SetTooltip("%s", state.container->get_tooltip().c_str()); + } + if (std::string url = state.container->pop_load_url(); !url.empty()) { if (clickedURL) { *clickedURL = url; diff --git a/imhtml.hpp b/imhtml.hpp index 41b4e48..4253fe3 100644 --- a/imhtml.hpp +++ b/imhtml.hpp @@ -41,6 +41,9 @@ struct FontFamily { * Configuration for the HTML renderer */ struct Config { + bool AllowHrefTooltips = true; + bool AllowImgAltTooltips = true; + float BaseFontSize = 16.0f; // fallback when not found in FontFamilies, or no specific family provided diff --git a/main.cpp b/main.cpp index 3ae931a..66baf1f 100644 --- a/main.cpp +++ b/main.cpp @@ -82,7 +82,7 @@ static std::string ReplaceAll(std::string str, const std::string &from, const st } // Main code -int main(int, char **) { +int main(int argc, char **argv) { glfwSetErrorCallback(GlfwErrorCallback); if (!glfwInit()) return 1; @@ -138,6 +138,12 @@ int main(int, char **) { ImGui::StyleColorsLight(); ImHTML::Config *config = ImHTML::GetConfig(); + if (argc > 1) { + if (atoi(argv[1]) == 0) { + config->AllowHrefTooltips = false; + config->AllowImgAltTooltips = false; + } + } std::unordered_map> image_cache; @@ -175,10 +181,12 @@ int main(int, char **) { fonts->AddFontDefault(); ImFont *sans_font = fonts->AddFontFromFileTTF("fonts/NotoSans-Regular.ttf", 18.0f); ImFont *mono_font = fonts->AddFontFromFileTTF("fonts/JetBrainsMono-Regular.ttf", 18.0f); + ImFont *sans_font_bold = fonts->AddFontFromFileTTF("fonts/NotoSans-Bold.ttf", 22.0f); + ImFont *mono_font_bold = fonts->AddFontFromFileTTF("fonts/JetBrainsMono-Bold.ttf", 22.0f); - ImHTML::FontFamily mono = {.Regular = mono_font, .Bold = mono_font, .Italic = mono_font, .BoldItalic = mono_font}; + ImHTML::FontFamily mono = {.Regular = mono_font, .Bold = mono_font_bold, .Italic = mono_font, .BoldItalic = mono_font_bold}; config->FontFamilies["monospace"] = mono; - ImHTML::FontFamily sans = {.Regular = sans_font, .Bold = sans_font, .Italic = sans_font, .BoldItalic = sans_font}; + ImHTML::FontFamily sans = {.Regular = sans_font, .Bold = sans_font_bold, .Italic = sans_font, .BoldItalic = sans_font_bold}; config->FontFamilies["sans-serif"] = sans; // Setup scaling