Skip to content

Commit

Permalink
Support OS-level DPI scaling on Windows & Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
John Novak committed Jul 20, 2024
1 parent 01b5cc5 commit 39d44ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/csdwindow.nim
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ proc setTheme(win; s: WindowTheme) =
label.colorDown = s.buttonInactiveColor

# }}}
# # {{{ theme*
# # {{{ theme=*
proc `theme=`*(win; s: WindowTheme) =
win.setTheme(s)

Expand Down Expand Up @@ -648,16 +648,21 @@ proc renderFrame*(win: CSDWindow, vg: NVGContext) =
koi.endFrame()

# }}}
# {{{ renderFramePreCb*
# {{{ renderFramePreCb=*
proc `renderFramePreCb=`*(win; p: RenderFramePreProc) =
g_renderFramePreProc = p

# }}}
# {{{ renderFrameCb*
# {{{ renderFrameCb=*
proc `renderFrameCb=`*(win; p: RenderFrameProc) =
g_window = win
g_renderFrameProc = p

# }}}
# {{{ contentScaleCb=*
proc `contentScaleCb=`*(win; cb: WindowContentScaleCb) =
win.w.windowContentScaleCb = cb

# }}}

# vim: et:ts=2:sw=2:fdm=marker
29 changes: 24 additions & 5 deletions src/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2390,10 +2390,18 @@ proc loadImage(path: string; a): Option[Paint] =
proc setSwapInterval(a) =
glfw.swapInterval(if a.prefs.vsync: 1 else: 0)

# }}}
# {{{ getFinalUIScaleFactor()
proc getFinalUIScaleFactor(a): float =
when defined(macosx):
a.prefs.scaleFactor
else:
a.win.contentScale.xScale * a.prefs.scaleFactor

# }}}
# {{{ updateUIScaleFactor()
proc updateUIScaleFactor(a) =
koi.setScale(a.prefs.scaleFactor)
koi.setScale(getFinalUIScaleFactor(a))

# }}}

Expand Down Expand Up @@ -3044,9 +3052,11 @@ proc updateTheme(a) =
a.theme.windowTheme = cfg.getObjectOrEmpty("ui.window").toWindowTheme
a.win.theme = a.theme.windowTheme

a.ui.drawLevelParams.initDrawLevelParams(a.theme.levelTheme, a.vg,
scaleFactor = a.prefs.scaleFactor,
pxRatio = koi.getPxRatio())
a.ui.drawLevelParams.initDrawLevelParams(
a.theme.levelTheme, a.vg,
scaleFactor = getFinalUIScaleFactor(a),
pxRatio = koi.getPxRatio()
)

# }}}
# {{{ switchTheme()
Expand Down Expand Up @@ -10159,6 +10169,12 @@ proc renderUI(a) =

# }}}

# {{{ windowContentScaleCb()
proc windowContentScaleCb(window: Window, width, height: float) =
g_app.updateUIScaleFactor()
g_app.updateUI = true

# }}}
# {{{ renderFramePreCb()
proc renderFramePreCb(a) =

Expand Down Expand Up @@ -10788,7 +10804,10 @@ proc initApp(configFile: Option[string], mapFile: Option[string],

# Init window
a.win.renderFramePreCb = proc (win: CSDWindow) = renderFramePreCb(g_app)
a.win.renderFrameCb = proc (win: CSDWindow) = renderFrameCb(g_app)
a.win.renderFrameCb = proc (win: CSDWindow) = renderFrameCb(g_app)

when not defined(macosx):
a.win.contentScaleCb = windowContentScaleCb

a.win.dropCb = dropCb

Expand Down

0 comments on commit 39d44ed

Please sign in to comment.