module XMonad.Util.Image
(
Placement(..),
iconPosition,
drawIcon,
) where
import XMonad
import XMonad.Prelude
import XMonad.Util.Font (stringToPixel)
data Placement = OffsetLeft Int Int
| OffsetRight Int Int
| CenterLeft Int
| CenterRight Int
deriving (Int -> Placement -> ShowS
[Placement] -> ShowS
Placement -> String
(Int -> Placement -> ShowS)
-> (Placement -> String)
-> ([Placement] -> ShowS)
-> Show Placement
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Placement -> ShowS
showsPrec :: Int -> Placement -> ShowS
$cshow :: Placement -> String
show :: Placement -> String
$cshowList :: [Placement] -> ShowS
showList :: [Placement] -> ShowS
Show, ReadPrec [Placement]
ReadPrec Placement
Int -> ReadS Placement
ReadS [Placement]
(Int -> ReadS Placement)
-> ReadS [Placement]
-> ReadPrec Placement
-> ReadPrec [Placement]
-> Read Placement
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Placement
readsPrec :: Int -> ReadS Placement
$creadList :: ReadS [Placement]
readList :: ReadS [Placement]
$creadPrec :: ReadPrec Placement
readPrec :: ReadPrec Placement
$creadListPrec :: ReadPrec [Placement]
readListPrec :: ReadPrec [Placement]
Read)
imageDims :: [[Bool]] -> (Int, Int)
imageDims :: [[Bool]] -> (Int, Int)
imageDims [[Bool]]
img = ([Bool] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Bool] -> Maybe [Bool] -> [Bool]
forall a. a -> Maybe a -> a
fromMaybe [] ([[Bool]] -> Maybe [Bool]
forall a. [a] -> Maybe a
listToMaybe [[Bool]]
img)), [[Bool]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Bool]]
img)
iconPosition :: Rectangle -> Placement -> [[Bool]] -> (Position,Position)
iconPosition :: Rectangle -> Placement -> [[Bool]] -> (Int32, Int32)
iconPosition Rectangle{} (OffsetLeft Int
x Int
y) [[Bool]]
_ = (Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
x, Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
y)
iconPosition (Rectangle Int32
_ Int32
_ Word32
w Word32
_) (OffsetRight Int
x Int
y) [[Bool]]
icon =
let (Int
icon_w, Int
_) = [[Bool]] -> (Int, Int)
imageDims [[Bool]]
icon
in (Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
w Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
x Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
icon_w, Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
y)
iconPosition (Rectangle Int32
_ Int32
_ Word32
_ Word32
h) (CenterLeft Int
x) [[Bool]]
icon =
let (Int
_, Int
icon_h) = [[Bool]] -> (Int, Int)
imageDims [[Bool]]
icon
in (Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
x, Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Word32
h Word32 -> Word32 -> Word32
forall a. Integral a => a -> a -> a
`div` Word32
2) Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Int
icon_h Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2))
iconPosition (Rectangle Int32
_ Int32
_ Word32
w Word32
h) (CenterRight Int
x) [[Bool]]
icon =
let (Int
icon_w, Int
icon_h) = [[Bool]] -> (Int, Int)
imageDims [[Bool]]
icon
in (Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
w Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
x Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
icon_w, Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Word32
h Word32 -> Word32 -> Word32
forall a. Integral a => a -> a -> a
`div` Word32
2) Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Int
icon_h Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2))
iconToPoints :: [[Bool]] -> [Point]
iconToPoints :: [[Bool]] -> [Point]
iconToPoints [[Bool]]
icon =
let labels_inside :: [[(Int32, Bool)]]
labels_inside = ([Bool] -> [(Int32, Bool)]) -> [[Bool]] -> [[(Int32, Bool)]]
forall a b. (a -> b) -> [a] -> [b]
map ([Int32] -> [Bool] -> [(Int32, Bool)]
forall a b. [a] -> [b] -> [(a, b)]
zip ((Int32 -> Int32) -> Int32 -> [Int32]
forall a. (a -> a) -> a -> [a]
iterate (Int32
1Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+) Int32
0)) [[Bool]]
icon
filtered_inside :: [[Int32]]
filtered_inside = ([(Int32, Bool)] -> [Int32]) -> [[(Int32, Bool)]] -> [[Int32]]
forall a b. (a -> b) -> [a] -> [b]
map (\[(Int32, Bool)]
l -> [Int32
x | (Int32
x, Bool
t) <- [(Int32, Bool)]
l, Bool
t]) [[(Int32, Bool)]]
labels_inside
labels_outside :: [(Int32, [Int32])]
labels_outside = [Int32] -> [[Int32]] -> [(Int32, [Int32])]
forall a b. [a] -> [b] -> [(a, b)]
zip ((Int32 -> Int32) -> Int32 -> [Int32]
forall a. (a -> a) -> a -> [a]
iterate (Int32
1Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+) Int32
0) [[Int32]]
filtered_inside
in [Int32 -> Int32 -> Point
Point Int32
x Int32
y | (Int32
y, [Int32]
l) <- [(Int32, [Int32])]
labels_outside, Int32
x <- [Int32]
l]
movePoint :: Position -> Position -> Point -> Point
movePoint :: Int32 -> Int32 -> Point -> Point
movePoint Int32
x Int32
y (Point Int32
a Int32
b) = Int32 -> Int32 -> Point
Point (Int32
a Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
x) (Int32
b Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
y)
movePoints :: Position -> Position -> [Point] -> [Point]
movePoints :: Int32 -> Int32 -> [Point] -> [Point]
movePoints Int32
x Int32
y = (Point -> Point) -> [Point] -> [Point]
forall a b. (a -> b) -> [a] -> [b]
map (Int32 -> Int32 -> Point -> Point
movePoint Int32
x Int32
y)
drawIcon :: (Functor m, MonadIO m) => Display -> Drawable -> GC -> String
->String -> Position -> Position -> [[Bool]] -> m ()
drawIcon :: forall (m :: * -> *).
(Functor m, MonadIO m) =>
Display
-> Drawable
-> GC
-> String
-> String
-> Int32
-> Int32
-> [[Bool]]
-> m ()
drawIcon Display
dpy Drawable
drw GC
gc String
fc String
bc Int32
x Int32
y [[Bool]]
icon = do
let (Int
i_w, Int
i_h) = [[Bool]] -> (Int, Int)
imageDims [[Bool]]
icon
fcolor <- Display -> String -> m Drawable
forall (m :: * -> *).
(Functor m, MonadIO m) =>
Display -> String -> m Drawable
stringToPixel Display
dpy String
fc
bcolor <- stringToPixel dpy bc
io $ setForeground dpy gc bcolor
io $ fillRectangle dpy drw gc x y (fi i_w) (fi i_h)
io $ setForeground dpy gc fcolor
io $ drawPoints dpy drw gc (movePoints x y (iconToPoints icon)) coordModeOrigin