Skip to content

Commit

Permalink
Detect font scale factor on macOS HiDPI (#246)
Browse files Browse the repository at this point in the history
Due to Dear ImGui issue ocornut/imgui#5301, the font scaling on HiDPI setup (only on macOS) is not correct and results in blurry text. This PR is a workaround for that.

* simple scale technique worked.
* check DisplayFramebufferScale
* start making a shim
* automatic detection of scale factor on macos
* make nix build succeed.
* remove stale code
* compile shim.mm only on macOS
  • Loading branch information
wavewave committed Sep 22, 2023
1 parent 696c9d9 commit a8fbbf8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
22 changes: 17 additions & 5 deletions daemon/app/ghc-specter-daemon/Render.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
Expand All @@ -16,6 +17,7 @@ import Control.Monad.IO.Class (MonadIO (..))
import Control.Monad.Trans.Reader (ReaderT (runReaderT))
import Data.Maybe (isNothing)
import Foreign.C.String (CString, withCString)
import Foreign.C.Types (CFloat (..))
import Foreign.Marshal.Alloc (callocBytes, free)
import Foreign.Marshal.Utils (toBool)
import Foreign.Ptr (nullPtr)
Expand Down Expand Up @@ -45,7 +47,8 @@ import ImGui.Enum
ImGuiMouseButton_ (..),
)
import ImGui.ImGuiIO.Implementation
( imGuiIO_Fonts_get,
( imGuiIO_FontGlobalScale_set,
imGuiIO_Fonts_get,
imGuiIO_MouseWheelH_get,
imGuiIO_MouseWheel_get,
)
Expand All @@ -71,6 +74,14 @@ import Util.GUI
)
import Util.Render (SharedState (..))

#ifdef __MACOS__
foreign import ccall unsafe "detectScaleFactor"
c_detectScaleFactor :: IO CFloat
#else
c_detectScaleFactor :: IO CFloat
c_detectScaleFactor = pure 1.0
#endif

singleFrame ::
ImGuiIO ->
GLFWwindow ->
Expand Down Expand Up @@ -183,16 +194,17 @@ prepareAssets io = do
let free_sans_path = dir </> "assets" </> "FreeSans.ttf"
free_mono_path = dir </> "assets" </> "FreeMono.ttf"
fonts <- imGuiIO_Fonts_get io
-- _fontDefault <- imFontAtlas_AddFontDefault fonts
scale <- c_detectScaleFactor
_fontDefault <-
withCString free_sans_path $ \cstr -> do
imFontAtlas_AddFontFromFileTTF fonts cstr (8 * 2.0)
imFontAtlas_AddFontFromFileTTF fonts cstr (13 * scale)
imGuiIO_FontGlobalScale_set io (1.0 / scale)
fontSans <-
withCString free_sans_path $ \cstr -> do
imFontAtlas_AddFontFromFileTTF fonts cstr 8
imFontAtlas_AddFontFromFileTTF fonts cstr (8 * scale)
fontMono <-
withCString free_mono_path $ \cstr ->
imFontAtlas_AddFontFromFileTTF fonts cstr 8
imFontAtlas_AddFontFromFileTTF fonts cstr (8 * scale)
pure (fontSans, fontMono)

main ::
Expand Down
16 changes: 16 additions & 0 deletions daemon/app/ghc-specter-daemon/cbits/shim.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifdef __MACOS__
#include <Cocoa/Cocoa.h>
#endif

extern "C" {

float detectScaleFactor( void ) {
#ifdef __MACOS__
return NSScreen.mainScreen.backingScaleFactor;
#else
return 1.0;
#endif
}

}

6 changes: 6 additions & 0 deletions daemon/ghc-specter-daemon.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ library
Paths_ghc_specter_daemon
hs-source-dirs:
src
if os(darwin)
cpp-options: -D__MACOS__
cxx-options: -D__MACOS__
frameworks: Cocoa
cxx-sources:
app/ghc-specter-daemon/cbits/shim.mm
ghc-options: -Wall -Wunused-packages -Werror
build-depends:
OGDF
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@
"ghc-specter-plugin" =
hself.callCabal2nix "ghc-specter-plugin" ./plugin {};
"ghc-specter-daemon" =
hself.callCabal2nix "ghc-specter-daemon" ./daemon {};
final.haskell.lib.overrideCabal
(hself.callCabal2nix "ghc-specter-daemon" ./daemon {})
(old: {
libraryFrameworkDepends =
final.lib.optional pkgs.stdenv.isDarwin final.darwin.apple_sdk.frameworks.Cocoa;
});
"ghc-build-analyzer" =
hself.callCabal2nix "ghc-build-analyzer" ./ghc-build-analyzer {};
};
Expand Down Expand Up @@ -116,7 +121,8 @@
pkgs.ormolu
pyenv
]
++ (pkgs.lib.optional (pkgs.stdenv.isLinux && !pkgs.lib.inPureEvalMode) nixGL.packages.${system}.default);
++ (pkgs.lib.optional (pkgs.stdenv.isLinux && !pkgs.lib.inPureEvalMode) nixGL.packages.${system}.default)
++ (pkgs.lib.optionals (pkgs.stdenv.isDarwin) [pkgs.darwin.apple_sdk.frameworks.Cocoa]);
shellHook = ''
export PS1="\n[${prompt}:\w]$ \0"
'';
Expand Down

0 comments on commit a8fbbf8

Please sign in to comment.