------------------------------------------------------------------------------
-- |
-- Module: Xmobar.X11.Boxes
-- Copyright: (c) 2022, 2024 Jose Antonio Ortega Ruiz
-- License: BSD3-style (see LICENSE)
--
-- Maintainer: jao@gnu.org
-- Stability: unstable
-- Portability: unportable
-- Start date: Fri Sep 16, 2022 04:01
--
-- Borders and boxes
--
------------------------------------------------------------------------------

module Xmobar.Draw.Boxes (Line, boxLines, BoxRect, borderRect) where

import qualified Xmobar.Config.Types as T

type Line = (Double, Double, Double, Double)
type BoxRect = (Double, Double, Double, Double)

-- | Computes the coordinates of a list of lines representing a Box.
-- The Box is to be positioned between x0 and x1, with height ht, and drawn
-- with line width lw.  The returned lists are coordinates of the beginning
-- and end of each line.
boxLines :: T.Box -> Double -> Double -> Double -> [Line]
boxLines :: Box -> Double -> Double -> Double -> [Line]
boxLines (T.Box BoxBorder
bd BoxOffset
offset CInt
lw String
_ BoxMargins
margins) Double
ht Double
x0 Double
x1 =
  case BoxBorder
bd of
    BoxBorder
T.BBTop    -> [Line
rtop]
    BoxBorder
T.BBBottom -> [Line
rbot]
    BoxBorder
T.BBVBoth  -> [Line
rtop, Line
rbot]
    BoxBorder
T.BBLeft   -> [Line
rleft]
    BoxBorder
T.BBRight  -> [Line
rright]
    BoxBorder
T.BBHBoth  -> [Line
rleft, Line
rright]
    BoxBorder
T.BBFull   -> [Line
rtop, Line
rbot, Line
rleft, Line
rright]
  where
    (T.BoxMargins Int32
top Int32
right Int32
bot Int32
left) = BoxMargins
margins
    (T.BoxOffset Align
align Int32
m) = BoxOffset
offset
    ma :: Double
ma = Int32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
m
    (Double
p0, Double
p1) = case Align
align of
                 Align
T.L -> (Double
0, -Double
ma)
                 Align
T.C -> (Double
ma, -Double
ma)
                 Align
T.R -> (Double
ma, Double
0)
    lc :: Double
lc = CInt -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
lw Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2
    xmin :: Double
xmin = Double
x0 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Int32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
left Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
lc
    xmax :: Double
xmax = Double
x1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Int32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
right Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
lc
    ymin :: Double
ymin = Int32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
lc
    ymax :: Double
ymax = Double
ht Double -> Double -> Double
forall a. Num a => a -> a -> a
- Int32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
bot Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
lc
    rtop :: Line
rtop = (Double
xmin Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
p0, Double
ymin, Double
xmax Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
p1, Double
ymin)
    rbot :: Line
rbot = (Double
xmin Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
p0, Double
ymax, Double
xmax Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
p1, Double
ymax)
    rleft :: Line
rleft = (Double
xmin, Double
ymin Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
p0, Double
xmin, Double
ymax Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
p1)
    rright :: Line
rright = (Double
xmax, Double
ymin Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
p0, Double
xmax, Double
ymax Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
p1)

-- | Computes the rectangle (x, y, width, height) for the given Border.
borderRect :: T.Border -> Double -> Double -> BoxRect
borderRect :: Border -> Double -> Double -> Line
borderRect Border
bdr Double
w Double
h =
  case Border
bdr of
    Border
T.TopB       -> (Double
0, Double
0, Double
w Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1, Double
0)
    Border
T.BottomB    -> (Double
0, Double
h Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1, Double
w Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1, Double
0)
    Border
T.FullB      -> (Double
0, Double
0, Double
w Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1, Double
h Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1)
    T.TopBM Int
m    -> (Double
0, Int -> Double
fi Int
m, Double
w Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1, Double
0)
    T.BottomBM Int
m -> (Double
0, Double
h Double -> Double -> Double
forall a. Num a => a -> a -> a
- Int -> Double
fi Int
m, Double
w Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1, Double
0)
    T.FullBM Int
m   -> (Int -> Double
fi Int
m, Int -> Double
fi Int
m, Double
w Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
fi Int
m, Double
h Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
fi Int
m)
    Border
T.NoBorder   -> (-Double
1, -Double
1, -Double
1, -Double
1)
  where fi :: Int -> Double
fi = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral