"""Configure DPI before constructing the desktop window."""

import ctypes
import sys


def enable_dpi():
    # This runs in pythonw, which owns the windows, before Tk creates a root.
    if sys.platform == "win32":
        ctypes.windll.user32.SetProcessDPIAware()


def configure_dpi(root):
    if sys.platform == "win32":
        dpi = getattr(ctypes.windll.user32, "GetDpiForWindow", None)
        if dpi:
            dpi.argtypes = [ctypes.c_void_p]
            dpi.restype = ctypes.c_uint
            value = dpi(root.winfo_id()) or 96
            root.tk.call("tk", "scaling", value / 72)
            return value / 96
    return max(1., float(root.tk.call("tk", "scaling")) * .75)
