From 02dd1005fee63a66bd8ed9afa647f373ec554a34 Mon Sep 17 00:00:00 2001
From: pommicket <pommicket@gmail.com>
Date: Thu, 20 Jul 2023 20:11:16 -0700
Subject: make ted DPI aware on windows

---
 config.c |  2 +-
 main.c   |  4 +++-
 ted.c    | 29 +++++++++++++++++++++++++++++
 ted.h    |  1 +
 4 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/config.c b/config.c
index 6a14689..3544273 100644
--- a/config.c
+++ b/config.c
@@ -112,7 +112,7 @@ static const SettingU8 settings_u8[] = {
 	{"tags-max-depth", &settings_zero.tags_max_depth, 1, 100, false},
 };
 static const SettingU16 settings_u16[] = {
-	{"text-size", &settings_zero.text_size, TEXT_SIZE_MIN, TEXT_SIZE_MAX, false},
+	{"text-size", &settings_zero.text_size_no_dpi, TEXT_SIZE_MIN, TEXT_SIZE_MAX, false},
 	{"max-menu-width", &settings_zero.max_menu_width, 10, U16_MAX, false},
 	{"error-display-time", &settings_zero.error_display_time, 0, U16_MAX, false},
 	{"framerate-cap", &settings_zero.framerate_cap, 3, 1000, false},
diff --git a/main.c b/main.c
index d7979d4..9f697aa 100644
--- a/main.c
+++ b/main.c
@@ -270,6 +270,8 @@ INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 		WideCharToMultiByte(CP_UTF8, 0, wide_arg, len, argv[i], bufsz - 1, NULL, NULL);
 	}
 	LocalFree(wide_argv);
+	SetProcessDPIAware();
+	
 #else
 int main(int argc, char **argv) {
 #endif
@@ -511,7 +513,7 @@ int main(int argc, char **argv) {
 	PROFILE_TIME(fonts_start)
 	ted_load_fonts(ted);
 	PROFILE_TIME(fonts_end)
-	 
+	
 	PROFILE_TIME(create_start)
 	{
 		TextBuffer *lbuffer = &ted->line_buffer;
diff --git a/ted.c b/ted.c
index 56df17d..5585685 100644
--- a/ted.c
+++ b/ted.c
@@ -1,6 +1,9 @@
 // various core ted functions (opening files, displaying errors, etc.)
 
 #include "ted.h"
+#if _WIN32
+	#include <SDL_syswm.h>
+#endif
 
 void die(const char *fmt, ...) {
 	char buf[256] = {0};
@@ -336,7 +339,32 @@ static Font *ted_load_multifont(Ted *ted, const char *filenames) {
 	return first_font;
 }
 
+static float ted_get_ui_scaling(Ted *ted) {
+#if _WIN32
+	SDL_SysWMinfo wm_info;
+	SDL_VERSION(&wm_info.version);
+	if (!SDL_GetWindowWMInfo(ted->window, &wm_info))
+		return 1;
+	HWND hwnd = wm_info.info.win.window;
+	UINT dpi = GetDpiForWindow(hwnd);
+	if (!dpi)
+		return 1;
+	return (float)dpi / 96.0f;
+#else
+	return 1;
+#endif
+}
+
 void ted_load_fonts(Ted *ted) {
+	{
+		// compute text size
+		float scaling = ted_get_ui_scaling(ted);
+		arr_foreach_ptr(ted->all_settings, Settings, set) {
+			u16 size = (u16)roundf(scaling * (float)set->text_size_no_dpi);
+			set->text_size = clamp_u16(size, TEXT_SIZE_MIN, TEXT_SIZE_MAX);
+		}
+	}
+	
 	ted_free_fonts(ted);
 	Settings *settings = ted_active_settings(ted);
 	ted->font = ted_load_multifont(ted, settings->font);
@@ -794,3 +822,4 @@ void ted_color_settings_for_message_type(MessageType type, ColorSetting *bg_colo
 		break;
 	}
 }
+
diff --git a/ted.h b/ted.h
index 819001f..e5f31fb 100644
--- a/ted.h
+++ b/ted.h
@@ -250,6 +250,7 @@ typedef struct {
 	u32 max_file_size;
 	u32 max_file_size_view_only;
 	u16 framerate_cap;
+	u16 text_size_no_dpi;
 	u16 text_size;
 	u16 max_menu_width;
 	u16 error_display_time;
-- 
cgit v1.2.3