Skip to content

Commit

Permalink
automatic detection of scale factor on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
wavewave committed Sep 22, 2023
1 parent 5a3fdcd commit a6ece1b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
25 changes: 9 additions & 16 deletions daemon/app/ghc-specter-daemon/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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 (CInt (..))
import Foreign.C.Types (CFloat (..))
import Foreign.Marshal.Alloc (callocBytes, free)
import Foreign.Marshal.Utils (toBool)
import Foreign.Ptr (nullPtr)
Expand Down Expand Up @@ -46,16 +46,11 @@ import ImGui.Enum
ImGuiMouseButton_ (..),
)
import ImGui.ImGuiIO.Implementation
( imGuiIO_DisplayFramebufferScale_get,
imGuiIO_FontGlobalScale_set,
( imGuiIO_FontGlobalScale_set,
imGuiIO_Fonts_get,
imGuiIO_MouseWheelH_get,
imGuiIO_MouseWheel_get,
)
import ImGui.ImVec2.Implementation
( imVec2_x_get,
imVec2_y_get,
)
import Paths_ghc_specter_daemon (getDataDir)
import Render.BlockerView qualified as BlockerView
import Render.Console (consoleInputBufferSize)
Expand All @@ -78,8 +73,8 @@ import Util.GUI
)
import Util.Render (SharedState (..))

foreign import ccall unsafe "scale"
c_scale :: IO CInt
foreign import ccall unsafe "detectScaleFactor"
c_detectScaleFactor :: IO CFloat

singleFrame ::
ImGuiIO ->
Expand Down Expand Up @@ -194,23 +189,21 @@ prepareAssets io = do
free_mono_path = dir </> "assets" </> "FreeMono.ttf"
fonts <- imGuiIO_Fonts_get io
-- _fontDefault <- imFontAtlas_AddFontDefault fonts
let scale = 2.0
scale <- c_detectScaleFactor
_fontDefault <-
withCString free_sans_path $ \cstr -> do
imFontAtlas_AddFontFromFileTTF fonts cstr (13 * scale)
v <- imGuiIO_DisplayFramebufferScale_get io
scaleX <- imVec2_x_get v
scaleY <- imVec2_y_get v
print (scaleX, scaleY)
-- v <- imGuiIO_DisplayFramebufferScale_get io
-- scaleX <- imVec2_x_get v
-- scaleY <- imVec2_y_get v
-- print (scaleX, scaleY)
imGuiIO_FontGlobalScale_set io (1.0 / scale)
fontSans <-
withCString free_sans_path $ \cstr -> do
imFontAtlas_AddFontFromFileTTF fonts cstr (8 * scale)
fontMono <-
withCString free_mono_path $ \cstr ->
imFontAtlas_AddFontFromFileTTF fonts cstr (8 * scale)
s <- c_scale
putStrLn $ "scale = " <> show s
pure (fontSans, fontMono)

main ::
Expand Down
12 changes: 0 additions & 12 deletions daemon/app/ghc-specter-daemon/cbits/shim.cpp

This file was deleted.

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
}

}

5 changes: 3 additions & 2 deletions daemon/ghc-specter-daemon.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ library
hs-source-dirs:
src
if os(darwin)
cxx-options: -D__MACOSX__
cxx-options: -D__MACOS__
frameworks: Cocoa
cxx-sources:
app/ghc-specter-daemon/cbits/shim.cpp
app/ghc-specter-daemon/cbits/shim.mm
ghc-options: -Wall -Wunused-packages -Werror
build-depends:
OGDF
Expand Down
3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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 a6ece1b

Please sign in to comment.