Skip to content

Commit

Permalink
feat: Added DPI awareness on Windows, added FiraCode as optional defa…
Browse files Browse the repository at this point in the history
…ult font
  • Loading branch information
WerWolv committed Jul 5, 2024
1 parent 9aaf6f3 commit b652565
Show file tree
Hide file tree
Showing 12 changed files with 644 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/libimhex/include/hex/api/event_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ namespace hex {
EVENT_DEF(EventAbnormalTermination, int);
EVENT_DEF(EventThemeChanged);
EVENT_DEF(EventOSThemeChanged);
EVENT_DEF(EventDPIChanged, float, float);
EVENT_DEF(EventWindowFocused, bool);

/**
Expand Down
1 change: 1 addition & 0 deletions lib/libimhex/include/hex/api/theme_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace hex {
*/
static void addStyleHandler(const std::string &name, const StyleMap &styleMap);

static void reapplyCurrentTheme();

static std::vector<std::string> getThemeNames();
static const std::string &getImageTheme();
Expand Down
5 changes: 5 additions & 0 deletions lib/libimhex/source/api/theme_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ namespace hex {
}


void ThemeManager::reapplyCurrentTheme() {
ThemeManager::changeTheme(s_currTheme);
}


void ThemeManager::addThemeHandler(const std::string &name, const ColorMap &colorMap, const std::function<ImColor(u32)> &getFunction, const std::function<void(u32, ImColor)> &setFunction) {
std::unique_lock lock(s_themeMutex);

Expand Down
31 changes: 31 additions & 0 deletions main/gui/source/window/win_window.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <hex/api/content_registry.hpp>
#include <hex/api/theme_manager.hpp>

#include "window.hpp"


Expand Down Expand Up @@ -45,6 +48,22 @@ namespace hex {
// Custom Window procedure for receiving OS events
static LRESULT commonWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DPICHANGED: {
int interfaceScaleSetting = int(hex::ContentRegistry::Settings::read<float>("hex.builtin.setting.interface", "hex.builtin.setting.interface.scaling_factor", 0.0F) * 10.0F);
if (interfaceScaleSetting != 0)
break;

const auto newScale = LOWORD(wParam) / 96.0F;
const auto oldScale = ImHexApi::System::getNativeScale();

EventDPIChanged::post(oldScale, newScale);
ImHexApi::System::impl::setNativeScale(newScale);

ThemeManager::reapplyCurrentTheme();
ImGui::GetStyle().ScaleAllSizes(newScale);

return TRUE;
}
case WM_COPYDATA: {
// Handle opening files in existing instance

Expand Down Expand Up @@ -371,6 +390,18 @@ namespace hex {


void Window::initNative() {
// Setup DPI Awareness
{
using SetProcessDpiAwarenessContextFunc = HRESULT(WINAPI *)(DPI_AWARENESS_CONTEXT);

SetProcessDpiAwarenessContextFunc SetProcessDpiAwarenessContext =
(SetProcessDpiAwarenessContextFunc)(void*)GetProcAddress(GetModuleHandleW(L"user32.dll"), "SetProcessDpiAwarenessContext");

if (SetProcessDpiAwarenessContext != nullptr) {
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
}
}

if (ImHexApi::System::isDebugBuild()) {
// If the application is running in debug mode, ImHex runs under the CONSOLE subsystem,
// so we don't need to do anything besides enabling ANSI colors
Expand Down
16 changes: 16 additions & 0 deletions main/gui/source/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ namespace hex {
m_popupsToOpen.push_back(name);
});

EventDPIChanged::subscribe([this](float oldScaling, float newScaling) {
if (oldScaling == newScaling || oldScaling == 0 || newScaling == 0)
return;

int width, height;
glfwGetWindowSize(m_window, &width, &height);

width = float(width) * newScaling / oldScaling;
height = float(height) * newScaling / oldScaling;

ImHexApi::System::impl::setMainWindowSize(width, height);
glfwSetWindowSize(m_window, width, height);
});


LayoutManager::registerLoadCallback([this](std::string_view line) {
int width = 0, height = 0;
sscanf(line.data(), "MainWindowSize=%d,%d", &width, &height);
Expand Down Expand Up @@ -827,6 +842,7 @@ namespace hex {

auto win = static_cast<Window *>(glfwGetWindowUserPointer(window));
win->m_unlockFrameRate = true;
win->fullFrame();
});

// Register window resize callback
Expand Down
1 change: 1 addition & 0 deletions plugins/builtin/romfs/lang/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@
"hex.builtin.setting.font.font_italic": "Italic",
"hex.builtin.setting.font.font_antialias": "Antialiasing",
"hex.builtin.setting.font.font_path": "Custom Font Path",
"hex.builtin.setting.font.pixel_perfect_default_font": "Use a Pixel-Perfect default font",
"hex.builtin.setting.font.font_size": "Font Size",
"hex.builtin.setting.font.font_size.tooltip": "The font size can only be adjusted when a custom font has been selected above.\n\nThis is because ImHex uses a pixel-perfect bitmap font by default. Scaling it by any non-integer factor will only cause it to become blurry.",
"hex.builtin.setting.general": "General",
Expand Down
20 changes: 13 additions & 7 deletions plugins/builtin/source/content/init_tasks.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <hex/api/imhex_api.hpp>
#include <hex/api/content_registry.hpp>
#include <hex/api/event_manager.hpp>
#include <hex/api_urls.hpp>

#include <hex/api/task_manager.hpp>
Expand Down Expand Up @@ -98,15 +99,20 @@ namespace hex::plugin::builtin {
}

bool configureUIScale() {
int interfaceScaleSetting = int(ContentRegistry::Settings::read<float>("hex.builtin.setting.interface", "hex.builtin.setting.interface.scaling_factor", 1.0F) * 10.0F);
EventDPIChanged::subscribe([](float, float newScaling) {
int interfaceScaleSetting = int(ContentRegistry::Settings::read<float>("hex.builtin.setting.interface", "hex.builtin.setting.interface.scaling_factor", 0.0F) * 10.0F);

float interfaceScaling;
if (interfaceScaleSetting == 0)
interfaceScaling = ImHexApi::System::getNativeScale();
else
interfaceScaling = interfaceScaleSetting / 10.0F;
float interfaceScaling;
if (interfaceScaleSetting == 0)
interfaceScaling = newScaling;
else
interfaceScaling = interfaceScaleSetting / 10.0F;

ImHexApi::System::impl::setGlobalScale(interfaceScaling);
ImHexApi::System::impl::setGlobalScale(interfaceScaling);
});

const auto nativeScale = ImHexApi::System::getNativeScale();
EventDPIChanged::post(nativeScale, nativeScale);

return true;
}
Expand Down
12 changes: 12 additions & 0 deletions plugins/builtin/source/content/settings_entries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ namespace hex::plugin::builtin {
return m_value;
}

float getValue() const {
return m_value;
}

private:
float m_value = 1.0F;
};
Expand Down Expand Up @@ -834,6 +838,14 @@ namespace hex::plugin::builtin {
.requiresRestart()
.setEnabledCallback(customFontsEnabled);

ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.font", "hex.builtin.setting.font.custom_font", "hex.builtin.setting.font.pixel_perfect_default_font", true)
.setEnabledCallback([customFontPathSetting] {
auto &fontPath = static_cast<Widgets::FilePicker &>(customFontPathSetting.getWidget());

return fontPath.getPath().empty();
})
.requiresRestart();

const auto customFontSettingsEnabled = [customFontEnabledSetting, customFontPathSetting] {
auto &customFontsEnabled = static_cast<Widgets::Checkbox &>(customFontEnabledSetting.getWidget());
auto &fontPath = static_cast<Widgets::FilePicker &>(customFontPathSetting.getWidget());
Expand Down
Loading

0 comments on commit b652565

Please sign in to comment.