diff --git a/android/backend.cpp b/android/backend.cpp index f1be83712..4e62e08de 100644 --- a/android/backend.cpp +++ b/android/backend.cpp @@ -5,6 +5,42 @@ extern EGLDisplay g_EglDisplay; extern EGLSurface g_EglSurface; +float funcDeviceScale() +{ + JavaVM* java_vm = g_App->activity->vm; + JNIEnv* java_env = NULL; + + jint jni_return = java_vm->GetEnv((void**)&java_env, JNI_VERSION_1_6); + if (jni_return == JNI_ERR) + throw std::runtime_error("Could not get JNI environement"); + + jni_return = java_vm->AttachCurrentThread(&java_env, NULL); + if (jni_return != JNI_OK) + throw std::runtime_error("Could not attach to thread"); + + jclass native_activity_clazz = java_env->GetObjectClass(g_App->activity->clazz); + if (native_activity_clazz == NULL) + throw std::runtime_error("Could not get MainActivity class"); + + jmethodID method_id = java_env->GetMethodID(native_activity_clazz, "get_dpi", "()F"); + if (method_id == NULL) + throw std::runtime_error("Could not get methode ID"); + + jfloat jflt = java_env->CallFloatMethod(g_App->activity->clazz, method_id); + + jni_return = java_vm->DetachCurrentThread(); + if (jni_return != JNI_OK) + throw std::runtime_error("Could not detach from thread"); + + return jflt; +} + +void funcRebuildFonts() +{ + ImGui_ImplOpenGL3_DestroyFontsTexture(); + ImGui_ImplOpenGL3_CreateFontsTexture(); +} + void funcSetMousePos(int, int) { // Not implemented on Android @@ -44,6 +80,9 @@ void funcSetIcon(uint8_t*, int, int) void bindBackendFunctions() { + backend::device_scale = funcDeviceScale(); + + backend::rebuildFonts = funcRebuildFonts; backend::setMousePos = funcSetMousePos; backend::beginFrame = funcBeginFrame; backend::endFrame = funcEndFrame; diff --git a/android/backend.h b/android/backend.h index b4aa0f6a8..88ebb6def 100644 --- a/android/backend.h +++ b/android/backend.h @@ -7,7 +7,8 @@ #include #include -// Standard bound functions +float funcDeviceScale(); +void funcRebuildFonts(); void funcSetMousePos(int, int); std::pair funcBeginFrame(); void funcEndFrame(); diff --git a/android/main.cpp b/android/main.cpp index d91e25d6f..d297880e8 100644 --- a/android/main.cpp +++ b/android/main.cpp @@ -17,7 +17,6 @@ static int ShowSoftKeyboardInput(); static int HideSoftKeyboardInput(); static int PollUnicodeChars(); static int GetAssetData(const char *filename, void **out_data); -static float get_dpi(); #include "logger.h" #include "init.h" @@ -91,17 +90,16 @@ void init(struct android_app *app) // ImGui::StyleColorsDark(); // ImGui::StyleColorsClassic(); - float display_scale = get_dpi(); initLogger(); - style::setFonts(display_scale); + style::setFonts(backend::device_scale); HideSoftKeyboardInput(); eglSwapInterval(g_EglDisplay, 0); - std::shared_ptr loading_screen_sink = std::make_shared(display_scale); + std::shared_ptr loading_screen_sink = std::make_shared(); logger->add_sink(loading_screen_sink); satdump::tle_do_update_on_init = false; satdump::initSatdump(); - satdump::initMainUI(display_scale); + satdump::initMainUI(); //Shut down loading screen logger->del_sink(loading_screen_sink); @@ -109,9 +107,6 @@ void init(struct android_app *app) //Set font again to adjust for DPI eglSwapInterval(g_EglDisplay, 1); - style::setFonts(); - ImGui_ImplOpenGL3_DestroyFontsTexture(); - ImGui_ImplOpenGL3_CreateFontsTexture(); // TLE satdump::ui_thread_pool.push([&](int) { satdump::autoUpdateTLE(satdump::user_path + "/satdump_tles.txt"); }); @@ -431,36 +426,6 @@ static int PollUnicodeChars() return 0; } -static float get_dpi() -{ - JavaVM* java_vm = g_App->activity->vm; - JNIEnv* java_env = NULL; - - jint jni_return = java_vm->GetEnv((void**)&java_env, JNI_VERSION_1_6); - if (jni_return == JNI_ERR) - throw std::runtime_error("Could not get JNI environement"); - - jni_return = java_vm->AttachCurrentThread(&java_env, NULL); - if (jni_return != JNI_OK) - throw std::runtime_error("Could not attach to thread"); - - jclass native_activity_clazz = java_env->GetObjectClass(g_App->activity->clazz); - if (native_activity_clazz == NULL) - throw std::runtime_error("Could not get MainActivity class"); - - jmethodID method_id = java_env->GetMethodID(native_activity_clazz, "get_dpi", "()F"); - if (method_id == NULL) - throw std::runtime_error("Could not get methode ID"); - - jfloat jflt = java_env->CallFloatMethod(g_App->activity->clazz, method_id); - - jni_return = java_vm->DetachCurrentThread(); - if (jni_return != JNI_OK) - throw std::runtime_error("Could not detach from thread"); - - return jflt; -} - // Helper to retrieve data placed into the assets/ directory (android/app/src/main/assets) static int GetAssetData(const char *filename, void **outData) { diff --git a/src-core/core/backend.cpp b/src-core/core/backend.cpp index f9d04d0b6..5854c1524 100644 --- a/src-core/core/backend.cpp +++ b/src-core/core/backend.cpp @@ -3,6 +3,9 @@ namespace backend { + SATDUMP_DLL float device_scale; + + SATDUMP_DLL std::function rebuildFonts; SATDUMP_DLL std::function setMousePos; SATDUMP_DLL std::function()> beginFrame; SATDUMP_DLL std::function endFrame; diff --git a/src-core/core/backend.h b/src-core/core/backend.h index f38469fa3..d1b480c4e 100644 --- a/src-core/core/backend.h +++ b/src-core/core/backend.h @@ -6,6 +6,9 @@ namespace backend { + SATDUMP_DLL extern float device_scale; + + SATDUMP_DLL extern std::function rebuildFonts; SATDUMP_DLL extern std::function setMousePos; SATDUMP_DLL extern std::function()> beginFrame; SATDUMP_DLL extern std::function endFrame; diff --git a/src-core/core/style.cpp b/src-core/core/style.cpp index a0b5cc1b1..d175c904d 100644 --- a/src-core/core/style.cpp +++ b/src-core/core/style.cpp @@ -5,7 +5,8 @@ #include "imgui/imgui_internal.h" #include "logger.h" #include -#include "core/module.h" +#include "module.h" +#include "backend.h" #include "resources.h" #ifdef __APPLE__ @@ -148,11 +149,6 @@ namespace style ImGui::PopStyleColor(3); } - void setFonts() - { - setFonts(ui_scale); - } - void setFonts(float dpi_scaling) { ImGuiIO &io = ImGui::GetIO(); @@ -175,6 +171,8 @@ namespace style //hugeFont = io.Fonts->AddFontFromFileTTF(resources::getResourcePath("fonts/Roboto-Medium.ttf").c_str(), 128.0f * font_scaling); //, &config, ranges); io.Fonts->Build(); io.FontGlobalScale = 1 / macos_fbs; + + backend::rebuildFonts(); } float macos_framebuffer_scale() diff --git a/src-core/core/style.h b/src-core/core/style.h index f1c0608e6..4a70e0421 100644 --- a/src-core/core/style.h +++ b/src-core/core/style.h @@ -36,7 +36,6 @@ namespace style void setDarkStyle(); void beginDisabled(); void endDisabled(); - void setFonts(); void setFonts(float dpi_scaling); float macos_framebuffer_scale(); diff --git a/src-interface/loader/loader.cpp b/src-interface/loader/loader.cpp index 38ee9d1b4..f1acd4d21 100644 --- a/src-interface/loader/loader.cpp +++ b/src-interface/loader/loader.cpp @@ -11,7 +11,7 @@ namespace satdump { - LoadingScreenSink::LoadingScreenSink(float scale) : scale{scale} + LoadingScreenSink::LoadingScreenSink() { image::Image image; std::random_device dev; @@ -64,6 +64,7 @@ namespace satdump void LoadingScreenSink::push_frame(std::string str) { std::pair dims = backend::beginFrame(); + float scale = backend::device_scale; ImGui::SetNextWindowPos({0, 0}); ImGui::SetNextWindowSize({(float)dims.first, (float)dims.second}); ImGui::Begin("Loading Screen", nullptr, NOWINDOW_FLAGS | ImGuiWindowFlags_NoDecoration); diff --git a/src-interface/loader/loader.h b/src-interface/loader/loader.h index 66d9e0086..a8dd1bace 100644 --- a/src-interface/loader/loader.h +++ b/src-interface/loader/loader.h @@ -7,13 +7,12 @@ namespace satdump class LoadingScreenSink : public slog::LoggerSink { public: - LoadingScreenSink(float scale); + LoadingScreenSink(); void push_frame(std::string str); ~LoadingScreenSink(); protected: void receive(slog::LogMsg log); private: - float scale; intptr_t image_texture; bool loader_constant; std::string title; diff --git a/src-interface/main_ui.cpp b/src-interface/main_ui.cpp index 2667455e8..f05e67683 100644 --- a/src-interface/main_ui.cpp +++ b/src-interface/main_ui.cpp @@ -29,6 +29,7 @@ namespace satdump std::shared_ptr recorder_app; std::shared_ptr viewer_app; + bool update_ui = true; bool in_app = false; // true; bool open_recorder; @@ -37,7 +38,7 @@ namespace satdump std::shared_ptr notify_logger_sink; std::shared_ptr status_logger_sink; - void initMainUI(float device_scale) + void initMainUI() { ImPlot::CreateContext(); @@ -45,9 +46,6 @@ namespace satdump offline::setup(); settings::setup(); - // Setup DPI/Theme - updateUI(device_scale); - // Load credits MD std::ifstream ifs(resources::getResourcePath("credits.md")); std::string credits_markdown((std::istreambuf_iterator(ifs)), (std::istreambuf_iterator())); @@ -74,19 +72,6 @@ namespace satdump logger->add_sink(notify_logger_sink); } - void updateUI(float device_scale) - { - float manual_dpi_scaling = config::main_cfg["user_interface"]["manual_dpi_scaling"]["value"].get(); - ui_scale = device_scale * manual_dpi_scaling; - ImGui::GetStyle() = ImGuiStyle(); - ImGui::GetStyle().ScaleAllSizes(ui_scale); - - if (config::main_cfg["user_interface"]["light_theme"]["value"].get()) - style::setLightStyle(); - else - style::setDarkStyle(); - } - void exitMainUI() { recorder_app->save_settings(); @@ -98,6 +83,22 @@ namespace satdump void renderMainUI() { + if (update_ui) + { + float manual_dpi_scaling = config::main_cfg["user_interface"]["manual_dpi_scaling"]["value"].get(); + ui_scale = backend::device_scale * manual_dpi_scaling; + ImGui::GetStyle() = ImGuiStyle(); + ImGui::GetStyle().ScaleAllSizes(ui_scale); + + if (config::main_cfg["user_interface"]["light_theme"]["value"].get()) + style::setLightStyle(); + else + style::setDarkStyle(); + + style::setFonts(ui_scale); + update_ui = false; + } + std::pair dims = backend::beginFrame(); // ImGui::ShowDemoWindow(); diff --git a/src-interface/main_ui.h b/src-interface/main_ui.h index 4d100bc2b..67b6f4aef 100644 --- a/src-interface/main_ui.h +++ b/src-interface/main_ui.h @@ -9,13 +9,13 @@ namespace satdump { + extern bool update_ui; extern ctpl::thread_pool ui_thread_pool; extern std::shared_ptr recorder_app; extern std::shared_ptr viewer_app; - void initMainUI(float device_scale); - void updateUI(float device_scale); + void initMainUI(); void exitMainUI(); void renderMainUI(); } diff --git a/src-interface/settings.cpp b/src-interface/settings.cpp index 406634f1d..02da8a9ae 100644 --- a/src-interface/settings.cpp +++ b/src-interface/settings.cpp @@ -205,6 +205,7 @@ namespace satdump config::saveUserConfig(); saved_message.set_message(style::theme.green, "Settings saved"); + satdump::update_ui = true; } saved_message.draw(); diff --git a/src-interface/viewer/image_handler.cpp b/src-interface/viewer/image_handler.cpp index 1a4791e8a..ceb6bf745 100644 --- a/src-interface/viewer/image_handler.cpp +++ b/src-interface/viewer/image_handler.cpp @@ -749,14 +749,14 @@ namespace satdump ImGui::TextColored(style::theme.red, "Disable correction!"); else ImGui::TextColored(style::theme.yellow, "The old algorithm will\n" - "deal with very bad (noisy) data\n" - "better.\n" - "The new one is preferred if\n" - "possible though, as results\n" - "are a lot nicer! :-)\n" - "If you had to use this\n" - "and the data was not that bad\n" - "please report as a bug!"); + "deal with very bad (noisy) data\n" + "better.\n" + "The new one is preferred if\n" + "possible though, as results\n" + "are a lot nicer! :-)\n" + "If you had to use this\n" + "and the data was not that bad\n" + "please report as a bug!"); ImGui::EndTooltip(); } diff --git a/src-ui/backend.cpp b/src-ui/backend.cpp index fb50a4b07..111416487 100644 --- a/src-ui/backend.cpp +++ b/src-ui/backend.cpp @@ -1,11 +1,38 @@ #include #include "core/style.h" -#include "core/backend.h" #include "backend.h" extern GLFWwindow* window; extern bool fallback_gl; +float funcDeviceScale() +{ + float display_scale; +#if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3) + glfwGetWindowContentScale(window, &display_scale, nullptr); + display_scale /= style::macos_framebuffer_scale(); +#else + display_scale = 1.0f; +#endif + return display_scale; +} + +void funcRebuildFonts() +{ +#ifndef IMGUI_IMPL_OPENGL_ES2 + if (fallback_gl) + { + ImGui_ImplOpenGL2_DestroyFontsTexture(); + ImGui_ImplOpenGL2_CreateFontsTexture(); + } + else +#endif + { + ImGui_ImplOpenGL3_DestroyFontsTexture(); + ImGui_ImplOpenGL3_CreateFontsTexture(); + } +} + void funcSetMousePos(int x, int y) { glfwSetCursorPos(window, x, y); @@ -62,6 +89,9 @@ void funcSetIcon(uint8_t *image, int w, int h) void bindBackendFunctions() { + backend::device_scale = funcDeviceScale(); + + backend::rebuildFonts = funcRebuildFonts; backend::setMousePos = funcSetMousePos; backend::beginFrame = funcBeginFrame; backend::endFrame = funcEndFrame; diff --git a/src-ui/backend.h b/src-ui/backend.h index 54ccddfbe..121eb9323 100644 --- a/src-ui/backend.h +++ b/src-ui/backend.h @@ -4,8 +4,11 @@ #include "imgui/imgui_impl_glfw.h" #include "imgui/imgui.h" #include "imgui/imgui_impl_opengl3.h" +#include "core/backend.h" #include "gl.h" +float funcDeviceScale(); +void funcRebuildFonts(); void funcSetMousePos(int x, int y); std::pair funcBeginFrame(); void funcEndFrame(); diff --git a/src-ui/main.cpp b/src-ui/main.cpp index 53fafc2cd..05fe3fa5d 100644 --- a/src-ui/main.cpp +++ b/src-ui/main.cpp @@ -29,20 +29,8 @@ static void glfw_error_callback(int error, const char *description) void window_content_scale_callback(GLFWwindow *, float xscale, float) { - satdump::updateUI(xscale / style::macos_framebuffer_scale()); - style::setFonts(); -#ifndef IMGUI_IMPL_OPENGL_ES2 - if (fallback_gl) - { - ImGui_ImplOpenGL2_DestroyFontsTexture(); - ImGui_ImplOpenGL2_CreateFontsTexture(); - } - else -#endif - { - ImGui_ImplOpenGL3_DestroyFontsTexture(); - ImGui_ImplOpenGL3_CreateFontsTexture(); - } + backend::device_scale = xscale / style::macos_framebuffer_scale(); + satdump::update_ui = true; } void bindBaseTextureFunctions(); @@ -158,20 +146,15 @@ int main(int argc, char *argv[]) ImGui_ImplOpenGL3_Init(OPENGL_VERSIONS_GLSL[selected_glsl]); // Handle DPI changes - float display_scale; #if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3) glfwSetWindowContentScaleCallback(window, window_content_scale_callback); - glfwGetWindowContentScale(window, &display_scale, nullptr); - display_scale /= style::macos_framebuffer_scale(); -#else - display_scale = 1.0f; #endif // Set font - style::setFonts(display_scale); + style::setFonts(backend::device_scale); // Init Loading Screen - std::shared_ptr loading_screen_sink = std::make_shared(display_scale); + std::shared_ptr loading_screen_sink = std::make_shared(); logger->add_sink(loading_screen_sink); // Init SatDump @@ -189,28 +172,13 @@ int main(int argc, char *argv[]) satdump::config::main_cfg["cli"] = parse_common_flags(argc - 1, &argv[1]); // Init UI - satdump::initMainUI(display_scale); + satdump::initMainUI(); // Shut down loading screen logger->del_sink(loading_screen_sink); loading_screen_sink.reset(); glfwSwapInterval(1); // Enable vsync for the rest of the program - // Set font again to adjust for DPI - style::setFonts(); -#ifndef IMGUI_IMPL_OPENGL_ES2 - if (fallback_gl) - { - ImGui_ImplOpenGL2_DestroyFontsTexture(); - ImGui_ImplOpenGL2_CreateFontsTexture(); - } - else -#endif - { - ImGui_ImplOpenGL3_DestroyFontsTexture(); - ImGui_ImplOpenGL3_CreateFontsTexture(); - } - if (satdump::processing::is_processing) { try_get_params_from_input_file(parameters, input_file);