Skip to content

Commit

Permalink
Make theme and DPI scaling updatable without re-launch
Browse files Browse the repository at this point in the history
  • Loading branch information
JVital2013 committed Jan 21, 2024
1 parent 788870a commit b4ffde1
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 114 deletions.
39 changes: 39 additions & 0 deletions android/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion android/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include <EGL/egl.h>
#include <GLES3/gl3.h>

// Standard bound functions
float funcDeviceScale();
void funcRebuildFonts();
void funcSetMousePos(int, int);
std::pair<int, int> funcBeginFrame();
void funcEndFrame();
Expand Down
41 changes: 3 additions & 38 deletions android/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -91,27 +90,23 @@ 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<satdump::LoadingScreenSink> loading_screen_sink = std::make_shared<satdump::LoadingScreenSink>(display_scale);
std::shared_ptr<satdump::LoadingScreenSink> loading_screen_sink = std::make_shared<satdump::LoadingScreenSink>();
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);
loading_screen_sink.reset();

//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"); });
Expand Down Expand Up @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions src-core/core/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace backend
{
SATDUMP_DLL float device_scale;

SATDUMP_DLL std::function<void()> rebuildFonts;
SATDUMP_DLL std::function<void(int, int)> setMousePos;
SATDUMP_DLL std::function<std::pair<int, int>()> beginFrame;
SATDUMP_DLL std::function<void()> endFrame;
Expand Down
3 changes: 3 additions & 0 deletions src-core/core/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace backend
{
SATDUMP_DLL extern float device_scale;

SATDUMP_DLL extern std::function<void()> rebuildFonts;
SATDUMP_DLL extern std::function<void(int, int)> setMousePos;
SATDUMP_DLL extern std::function<std::pair<int, int>()> beginFrame;
SATDUMP_DLL extern std::function<void()> endFrame;
Expand Down
10 changes: 4 additions & 6 deletions src-core/core/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include "imgui/imgui_internal.h"
#include "logger.h"
#include <filesystem>
#include "core/module.h"
#include "module.h"
#include "backend.h"
#include "resources.h"

#ifdef __APPLE__
Expand Down Expand Up @@ -148,11 +149,6 @@ namespace style
ImGui::PopStyleColor(3);
}

void setFonts()
{
setFonts(ui_scale);
}

void setFonts(float dpi_scaling)
{
ImGuiIO &io = ImGui::GetIO();
Expand All @@ -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()
Expand Down
1 change: 0 additions & 1 deletion src-core/core/style.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ namespace style
void setDarkStyle();
void beginDisabled();
void endDisabled();
void setFonts();
void setFonts(float dpi_scaling);

float macos_framebuffer_scale();
Expand Down
3 changes: 2 additions & 1 deletion src-interface/loader/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace satdump
{
LoadingScreenSink::LoadingScreenSink(float scale) : scale{scale}
LoadingScreenSink::LoadingScreenSink()
{
image::Image<uint8_t> image;
std::random_device dev;
Expand Down Expand Up @@ -64,6 +64,7 @@ namespace satdump
void LoadingScreenSink::push_frame(std::string str)
{
std::pair<int, int> 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);
Expand Down
3 changes: 1 addition & 2 deletions src-interface/loader/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 18 additions & 17 deletions src-interface/main_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace satdump
std::shared_ptr<RecorderApplication> recorder_app;
std::shared_ptr<ViewerApplication> viewer_app;

bool update_ui = true;
bool in_app = false; // true;
bool open_recorder;

Expand All @@ -37,17 +38,14 @@ namespace satdump
std::shared_ptr<NotifyLoggerSink> notify_logger_sink;
std::shared_ptr<StatusLoggerSink> status_logger_sink;

void initMainUI(float device_scale)
void initMainUI()
{
ImPlot::CreateContext();

audio::registerSinks();
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<char>(ifs)), (std::istreambuf_iterator<char>()));
Expand All @@ -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<float>();
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<bool>())
style::setLightStyle();
else
style::setDarkStyle();
}

void exitMainUI()
{
recorder_app->save_settings();
Expand All @@ -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<float>();
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<bool>())
style::setLightStyle();
else
style::setDarkStyle();

style::setFonts(ui_scale);
update_ui = false;
}

std::pair<int, int> dims = backend::beginFrame();
// ImGui::ShowDemoWindow();

Expand Down
4 changes: 2 additions & 2 deletions src-interface/main_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

namespace satdump
{
extern bool update_ui;
extern ctpl::thread_pool ui_thread_pool;

extern std::shared_ptr<RecorderApplication> recorder_app;
extern std::shared_ptr<ViewerApplication> viewer_app;

void initMainUI(float device_scale);
void updateUI(float device_scale);
void initMainUI();
void exitMainUI();
void renderMainUI();
}
1 change: 1 addition & 0 deletions src-interface/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ namespace satdump

config::saveUserConfig();
saved_message.set_message(style::theme.green, "Settings saved");
satdump::update_ui = true;
}

saved_message.draw();
Expand Down
16 changes: 8 additions & 8 deletions src-interface/viewer/image_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
32 changes: 31 additions & 1 deletion src-ui/backend.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
#include <GLFW/glfw3.h>
#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);
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit b4ffde1

Please sign in to comment.