1
0
mirror of synced 2025-01-10 21:41:53 +01:00

impr: Refactor previous commits to work with other environments too

This commit is contained in:
WerWolv 2024-12-28 21:37:45 +01:00
parent 534a2f1d28
commit 1298f2b688
7 changed files with 30 additions and 24 deletions

View File

@ -493,6 +493,7 @@ namespace hex {
*/
float getNativeScale();
float getBackingScaleFactor();
/**
* @brief Gets the current main window position

View File

@ -18,6 +18,8 @@
#include <algorithm>
#include <GLFW/glfw3.h>
#include <hex/helpers/utils_macos.hpp>
#if defined(OS_WINDOWS)
#include <windows.h>
#else
@ -25,6 +27,10 @@
#include <unistd.h>
#endif
#if defined(OS_WEB)
#include <emscripten.h>
#endif
namespace hex {
@ -608,6 +614,22 @@ namespace hex {
return impl::s_nativeScale;
}
float getBackingScaleFactor() {
#if defined(OS_WINDOWS)
return 1.0F;
#elif defined(OS_MACOS)
return ::getBackingScaleFactor();
#elif defined(OS_LINUX)
return 1.0F;
#elif defined(OS_WEB)
return EM_ASM_INT({
return window.devicePixelRatio;
});
#else
return 1.0F;
#endif
}
ImVec2 getMainWindowPosition() {
if ((ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) != ImGuiConfigFlags_None)

View File

@ -223,11 +223,7 @@ namespace ImGuiExt {
}
Texture Texture::fromSVG(const char *path, int width, int height, Filter filter) {
#if defined(OS_MACOS)
const auto scaleFactor = getBackingScaleFactor();
#else
const auto scaleFactor = 1.0F;
#endif
const auto scaleFactor = hex::ImHexApi::System::getBackingScaleFactor();
auto document = lunasvg::Document::loadFromFile(path);
auto bitmap = document->renderToBitmap(width * scaleFactor, height * scaleFactor);
@ -247,11 +243,7 @@ namespace ImGuiExt {
}
Texture Texture::fromSVG(std::span<const std::byte> buffer, int width, int height, Filter filter) {
#if defined(OS_MACOS)
const auto scaleFactor = getBackingScaleFactor();
#else
const auto scaleFactor = 1.0F;
#endif
const auto scaleFactor = hex::ImHexApi::System::getBackingScaleFactor();
auto document = lunasvg::Document::loadFromData(reinterpret_cast<const char*>(buffer.data()), buffer.size());
auto bitmap = document->renderToBitmap(width * scaleFactor, height * scaleFactor);

View File

@ -469,11 +469,7 @@ namespace hex::init {
if (meanScale <= 0.0F)
meanScale = 1.0F;
#if defined(OS_MACOS)
meanScale /= getBackingScaleFactor();
#elif defined(OS_WEB)
meanScale = 1.0F;
#endif
meanScale /= hex::ImHexApi::System::getBackingScaleFactor();
ImHexApi::System::impl::setGlobalScale(meanScale);
ImHexApi::System::impl::setNativeScale(meanScale);

View File

@ -96,9 +96,6 @@ namespace hex {
auto win = static_cast<Window *>(glfwGetWindowUserPointer(window));
win->fullFrame();
});
// Done to get super crisp font rendering together with GLFW_COCOA_RETINA_FRAMEBUFFER
ImGui::GetIO().FontGlobalScale = 1.0F / getBackingScaleFactor();
}
void Window::beginNativeWindowFrame() {

View File

@ -746,6 +746,8 @@ namespace hex {
// NOTE: This needs to be done before a new frame is started, otherwise ImGui won't handle docking correctly
LayoutManager::process();
WorkspaceManager::process();
ImGui::GetIO().FontGlobalScale = 1.0F / ImHexApi::System::getBackingScaleFactor();
}
void Window::initGLFW() {

View File

@ -218,15 +218,11 @@ namespace hex::fonts {
}
private:
float getAdjustedFontSize(float fontSize) {
float getAdjustedFontSize(float fontSize) const {
// Since macOS reports half the framebuffer size that's actually available,
// we'll multiply all font sizes by that and then divide the global font scale
// by the same amount to get super crisp font rendering.
#if defined(OS_MACOS)
return fontSize * getBackingScaleFactor();
#else
return fontSize;
#endif
return fontSize * hex::ImHexApi::System::getBackingScaleFactor();
}
private: