Skip to content

Commit

Permalink
Move font-size-related functions out of Termonad.App
Browse files Browse the repository at this point in the history
  • Loading branch information
cdepillabout committed Nov 4, 2023
1 parent 826609a commit e95efdf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 44 deletions.
44 changes: 2 additions & 42 deletions src/Termonad/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 117,10 @@ import Termonad.Types
, modFontSize
, newEmptyTMState
, tmNotebookTabTermContainer
, tmNotebookTabs
, tmNotebookTabs, createFontDescFromConfig
)
import Termonad.XML (interfaceText, menuText)
import Termonad.Window (doFind, findAbove, findBelow, showAboutDialog, updateFLTabPos, notebookPageReorderedCallback)
import Termonad.Window (doFind, findAbove, findBelow, showAboutDialog, updateFLTabPos, notebookPageReorderedCallback, modifyFontSizeForAllTerms)

setupScreenStyle :: IO ()
setupScreenStyle = do
Expand Down Expand Up @@ -166,46 166,6 @@ setupScreenStyle = do
cssProvider
(fromIntegral STYLE_PROVIDER_PRIORITY_APPLICATION)

createFontDescFromConfig :: TMConfig -> IO FontDescription
createFontDescFromConfig tmConfig = do
let fontConf = tmConfig ^. lensOptions . lensFontConfig
createFontDesc (fontSize fontConf) (fontFamily fontConf)

createFontDesc :: FontSize -> Text -> IO FontDescription
createFontDesc fontSz fontFam = do
fontDesc <- fontDescriptionNew
fontDescriptionSetFamily fontDesc fontFam
setFontDescSize fontDesc fontSz
pure fontDesc

setFontDescSize :: FontDescription -> FontSize -> IO ()
setFontDescSize fontDesc (FontSizePoints points) =
fontDescriptionSetSize fontDesc $ fromIntegral (points * fromIntegral SCALE)
setFontDescSize fontDesc (FontSizeUnits units) =
fontDescriptionSetAbsoluteSize fontDesc $ units * fromIntegral SCALE

adjustFontDescSize :: (FontSize -> FontSize) -> FontDescription -> IO ()
adjustFontDescSize f fontDesc = do
currFontSz <- fontSizeFromFontDescription fontDesc
let newFontSz = f currFontSz
setFontDescSize fontDesc newFontSz

modifyFontSizeForAllTerms :: (FontSize -> FontSize) -> TMState -> TMWindowId -> IO ()
modifyFontSizeForAllTerms modFontSizeFunc mvarTMState tmWinId = do
tmState <- readMVar mvarTMState
let fontDesc = tmState ^. lensTMStateFontDesc
adjustFontDescSize modFontSizeFunc fontDesc
let terms =
tmState ^..
lensTMStateWindows .
ix tmWinId .
lensTMWindowNotebook .
lensTMNotebookTabs .
traverse .
lensTMNotebookTabTerm .
lensTerm
foldMap (\vteTerm -> terminalSetFont vteTerm (Just fontDesc)) terms

-- | Try to figure out whether Termonad should exit. This also used to figure
-- out if Termonad should close a given terminal.
--
Expand Down
27 changes: 26 additions & 1 deletion src/Termonad/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 26,7 @@ import GI.Gtk
, notebookGetNthPage
, notebookGetNPages
)
import GI.Pango (FontDescription, fontDescriptionGetSize, fontDescriptionGetSizeIsAbsolute, pattern SCALE, fontDescriptionGetFamily)
import GI.Pango (FontDescription, fontDescriptionGetSize, fontDescriptionGetSizeIsAbsolute, pattern SCALE, fontDescriptionGetFamily, fontDescriptionNew, fontDescriptionSetFamily, fontDescriptionSetSize, fontDescriptionSetAbsoluteSize)
import GI.Vte (Terminal, CursorBlinkMode(..))
import Termonad.Gtk (widgetEq)
import Termonad.IdMap (IdMap, IdMapKey, singletonIdMap, lookupIdMap)
Expand Down Expand Up @@ -343,6 343,31 @@ fontSizeFromFontDescription fontDesc = do
let fontRatio :: Double = fromIntegral currSize / fromIntegral SCALE
in FontSizePoints $ round fontRatio

-- | Create a 'FontDescription' from a 'FontSize' and font family.
createFontDesc
:: FontSize
-> Text
-- ^ font family
-> IO FontDescription
createFontDesc fontSz fontFam = do
fontDesc <- fontDescriptionNew
fontDescriptionSetFamily fontDesc fontFam
setFontDescSize fontDesc fontSz
pure fontDesc

-- | Set the size of a 'FontDescription' from a 'FontSize'.
setFontDescSize :: FontDescription -> FontSize -> IO ()
setFontDescSize fontDesc (FontSizePoints points) =
fontDescriptionSetSize fontDesc $ fromIntegral (points * fromIntegral SCALE)
setFontDescSize fontDesc (FontSizeUnits units) =
fontDescriptionSetAbsoluteSize fontDesc $ units * fromIntegral SCALE

-- | Create a 'FontDescription' from the 'fontSize' and 'fontFamily' inside a 'TMConfig'.
createFontDescFromConfig :: TMConfig -> IO FontDescription
createFontDescFromConfig tmConfig = do
let fontConf = fontConfig (options tmConfig)
createFontDesc (fontSize fontConf) (fontFamily fontConf)

-- | Settings for the font to be used in Termonad.
data FontConfig = FontConfig
{ fontFamily :: !Text
Expand Down
24 changes: 23 additions & 1 deletion src/Termonad/Window.hs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 131,32 @@ import Termonad.Types
, newEmptyTMState
, tmNotebookTabTermContainer
, tmNotebookTabs
, tmStateApp, getTMWindowFromTMState, tmWindowAppWin
, tmStateApp, getTMWindowFromTMState, tmWindowAppWin, setFontDescSize
)
import Termonad.XML (interfaceText, menuText)

modifyFontSizeForAllTerms :: (FontSize -> FontSize) -> TMState -> TMWindowId -> IO ()
modifyFontSizeForAllTerms modFontSizeFunc mvarTMState tmWinId = do
tmState <- readMVar mvarTMState
let fontDesc = tmState ^. lensTMStateFontDesc
adjustFontDescSize modFontSizeFunc fontDesc
let terms =
tmState ^..
lensTMStateWindows .
ix tmWinId .
lensTMWindowNotebook .
lensTMNotebookTabs .
traverse .
lensTMNotebookTabTerm .
lensTerm
foldMap (\vteTerm -> terminalSetFont vteTerm (Just fontDesc)) terms
where
adjustFontDescSize :: (FontSize -> FontSize) -> FontDescription -> IO ()
adjustFontDescSize f fontDesc = do
currFontSz <- fontSizeFromFontDescription fontDesc
let newFontSz = f currFontSz
setFontDescSize fontDesc newFontSz

-- | This is the callback for when a page in a 'Notebook' has been reordered
-- (normally caused by a drag-and-drop event).
notebookPageReorderedCallback
Expand Down

0 comments on commit e95efdf

Please sign in to comment.