|
| 1 | +#include <hex/api/content_registry/user_interface.hpp> |
| 2 | +#include <hex/api/theme_manager.hpp> |
| 3 | +#include <hex/helpers/logger.hpp> |
| 4 | +#include <fonts/vscode_icons.hpp> |
| 5 | +#include <fonts/tabler_icons.hpp> |
| 6 | +#include <fmt/format.h> |
| 7 | +#include "lib/core/globals.h" |
| 8 | +#include "lib/config/config.h" |
| 9 | +#include "lib/data/flux.h" |
| 10 | +#include "lib/data/sector.h" |
| 11 | +#include "lib/config/proto.h" |
| 12 | +#include "globals.h" |
| 13 | +#include "logview.h" |
| 14 | +#include "datastore.h" |
| 15 | +#include "utils.h" |
| 16 | +#include <implot.h> |
| 17 | +#include <implot_internal.h> |
| 18 | + |
| 19 | +using namespace hex; |
| 20 | + |
| 21 | +static std::stringstream logstream; |
| 22 | +static std::unique_ptr<LogRenderer> logRenderer = |
| 23 | + LogRenderer::create(logstream); |
| 24 | +static bool scrollToEndOnNextRedraw = false; |
| 25 | + |
| 26 | +LogView::LogView(): View::Window("fluxengine.view.log.name", ICON_VS_DEBUG) {} |
| 27 | + |
| 28 | +void LogView::drawContent() |
| 29 | +{ |
| 30 | + ImGui::PushStyleColor(ImGuiCol_FrameBg, 0x00000000); |
| 31 | + ON_SCOPE_EXIT |
| 32 | + { |
| 33 | + ImGui::PopStyleColor(); |
| 34 | + }; |
| 35 | + |
| 36 | + auto buffer = logstream.view(); |
| 37 | + ImGui::InputTextMultiline("##logview", |
| 38 | + (char*)buffer.data(), |
| 39 | + buffer.size(), |
| 40 | + ImGui::GetContentRegionAvail(), |
| 41 | + ImGuiInputTextFlags_ReadOnly); |
| 42 | + |
| 43 | + /* Hacky code to scroll to the end of the window if something's changed. |
| 44 | + * See https://github.com/ocornut/imgui/issues/5484 |
| 45 | + */ |
| 46 | + |
| 47 | + if (scrollToEndOnNextRedraw) |
| 48 | + { |
| 49 | + scrollToEndOnNextRedraw = false; |
| 50 | + |
| 51 | + ImGuiContext& g = *GImGui; |
| 52 | + const char* child_window_name = NULL; |
| 53 | + ImFormatStringToTempBuffer(&child_window_name, |
| 54 | + NULL, |
| 55 | + "%s/%s_%08X", |
| 56 | + g.CurrentWindow->Name, |
| 57 | + "##logview", |
| 58 | + ImGui::GetID("##logview")); |
| 59 | + ImGuiWindow* child_window = ImGui::FindWindowByName(child_window_name); |
| 60 | + ImGui::SetScrollY(child_window, child_window->ScrollMax.y); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void LogView::logMessage(const AnyLogMessage& message) |
| 65 | +{ |
| 66 | + logRenderer->add(message); |
| 67 | + scrollToEndOnNextRedraw = true; |
| 68 | +} |
0 commit comments