use fixed number of digits in infobar

This commit is contained in:
sheaf 2020-08-11 12:22:56 +02:00
parent 07f9981c41
commit d74d4fd5cc
2 changed files with 26 additions and 11 deletions

View file

@ -62,6 +62,8 @@ import MetaBrush.UI.InfoBar
-- --
-- * On a scroll event, modifies the viewport coordinates as required. -- * On a scroll event, modifies the viewport coordinates as required.
-- * On a scroll event or a mouse motion event, updates the info bar coordinate display. -- * On a scroll event or a mouse motion event, updates the info bar coordinate display.
--
-- TODO: also update on change of document.
keepViewportCoordinatesUpdated keepViewportCoordinatesUpdated
:: STM.TVar ( Maybe Int ) :: STM.TVar ( Maybe Int )
-> STM.TVar ( IntMap Document ) -> STM.TVar ( IntMap Document )
@ -141,9 +143,9 @@ keepViewportCoordinatesUpdated
newZoomFactor :: Double newZoomFactor :: Double
newZoomFactor newZoomFactor
| dy > 0 | dy > 0
= max 0.0078125 ( oldZoomFactor / 2 ) = max 0.0078125 ( oldZoomFactor / sqrt 2 )
| otherwise | otherwise
= oldZoomFactor * 2 = min 256 ( oldZoomFactor * sqrt 2 )
newCenter :: Point2D Double newCenter :: Point2D Double
newCenter newCenter
= ( 1 - oldZoomFactor / newZoomFactor ) *^ ( oldCenter --> mousePos :: Vector2D Double ) = ( 1 - oldZoomFactor / newZoomFactor ) *^ ( oldCenter --> mousePos :: Vector2D Double )

View file

@ -13,10 +13,14 @@ module MetaBrush.UI.InfoBar
where where
-- base -- base
import Control.Arrow
( second )
import Control.Monad import Control.Monad
( void ) ( void )
import Data.Foldable import Data.Foldable
( for_ ) ( for_ )
import Numeric
( showFFloat )
-- gi-cairo-connector -- gi-cairo-connector
import qualified GI.Cairo.Render.Connector as Cairo import qualified GI.Cairo.Render.Connector as Cairo
@ -34,7 +38,7 @@ import qualified Data.Text as Text
-- MetaBrush -- MetaBrush
import Math.Vector2D import Math.Vector2D
( Point2D(..), Vector2D(..) ) ( Point2D(..) )
import MetaBrush.Asset.Colours import MetaBrush.Asset.Colours
( Colours ) ( Colours )
import MetaBrush.Asset.Cursor import MetaBrush.Asset.Cursor
@ -87,7 +91,7 @@ createInfoBar colours infoBar = do
-- Magnifier -- Magnifier
magnifierArea <- GTK.drawingAreaNew magnifierArea <- GTK.drawingAreaNew
zoomText <- GTK.labelNew ( Just "100%" ) zoomText <- GTK.labelNew ( Just " 100.00%" )
GTK.boxPackStart zoomBox magnifierArea True True 0 GTK.boxPackStart zoomBox magnifierArea True True 0
GTK.boxPackStart zoomBox zoomText True True 0 GTK.boxPackStart zoomBox zoomText True True 0
@ -102,7 +106,7 @@ createInfoBar colours infoBar = do
-- Cursor position -- Cursor position
cursorPosArea <- GTK.drawingAreaNew cursorPosArea <- GTK.drawingAreaNew
cursorPosText <- GTK.labelNew ( Just "x: 50.00 px\ny: 50.00 px" ) cursorPosText <- GTK.labelNew ( Just "x: 0.00\ny: 0.00" )
GTK.boxPackStart cursorPosBox cursorPosArea False False 0 GTK.boxPackStart cursorPosBox cursorPosArea False False 0
GTK.boxPackStart cursorPosBox cursorPosText False False 0 GTK.boxPackStart cursorPosBox cursorPosText False False 0
@ -117,7 +121,7 @@ createInfoBar colours infoBar = do
-- Top left position -- Top left position
topLeftPosArea <- GTK.drawingAreaNew topLeftPosArea <- GTK.drawingAreaNew
topLeftPosText <- GTK.labelNew ( Just "x: 0.00 px\ny: 0.00 px" ) topLeftPosText <- GTK.labelNew ( Just "x: 0.00\ny: 0.00" )
GTK.boxPackStart topLeftPosBox topLeftPosArea False False 0 GTK.boxPackStart topLeftPosBox topLeftPosArea False False 0
GTK.boxPackStart topLeftPosBox topLeftPosText False False 0 GTK.boxPackStart topLeftPosBox topLeftPosText False False 0
@ -130,7 +134,7 @@ createInfoBar colours infoBar = do
-- Bottom right position -- Bottom right position
botRightPosArea <- GTK.drawingAreaNew botRightPosArea <- GTK.drawingAreaNew
botRightPosText <- GTK.labelNew ( Just "x: 100.00 px\ny: 100.00 px" ) botRightPosText <- GTK.labelNew ( Just "x: 0.000\ny: 0.00" )
GTK.boxPackStart botRightPosBox botRightPosArea False False 0 GTK.boxPackStart botRightPosBox botRightPosArea False False 0
GTK.boxPackStart botRightPosBox botRightPosText False False 0 GTK.boxPackStart botRightPosBox botRightPosText False False 0
@ -162,8 +166,17 @@ updateInfoBar
, botRightPos = Point2D r b , botRightPos = Point2D r b
} }
) = do ) = do
GTK.labelSetText zoomText $ Text.pack ( show ( 100 * zoom ) <> "%" ) GTK.labelSetText zoomText $ Text.pack ( fixed 5 2 ( 100 * zoom ) <> "%" )
GTK.labelSetText cursorPosText $ Text.pack ( "x: " <> show mx <> "\ny: " <> show my ) GTK.labelSetText cursorPosText $ Text.pack ( "x: " <> fixed 6 2 mx <> "\ny: " <> fixed 6 2 my )
GTK.labelSetText topLeftPosText $ Text.pack ( "x: " <> show l <> "\ny: " <> show t ) GTK.labelSetText topLeftPosText $ Text.pack ( "x: " <> fixed 6 2 l <> "\ny: " <> fixed 6 2 t )
GTK.labelSetText botRightPosText $ Text.pack ( "x: " <> show r <> "\ny: " <> show b ) GTK.labelSetText botRightPosText $ Text.pack ( "x: " <> fixed 6 2 r <> "\ny: " <> fixed 6 2 b )
fixed :: Int -> Int -> Double -> String
fixed digitsBefore digitsAfter x = case second tail . break ( == '.' ) $ showFFloat ( Just digitsAfter ) x "" of
( as, bs ) ->
let
l, r :: Int
l = length as
r = length bs
in
replicate ( digitsBefore - l ) ' ' <> as <> "." <> bs <> replicate ( digitsAfter - r ) '0'