X.Prompt: Document getComplWinDim

This commit is contained in:
slotThe 2021-04-14 20:27:02 +02:00
parent 96640f7aae
commit 2f6546a8d6

View File

@ -1524,41 +1524,60 @@ destroyComplWin = do
modify (\s -> s { complWin = Nothing, complWinDim = Nothing }) modify (\s -> s { complWin = Nothing, complWinDim = Nothing })
Nothing -> return () Nothing -> return ()
-- | Given the completions that we would like to show, calculate the
-- required dimensions for the completion windows.
getComplWinDim :: [String] -> XP ComplWindowDim getComplWinDim :: [String] -> XP ComplWindowDim
getComplWinDim compl = do getComplWinDim compl = do
st <- get XPS{ config = cfg, screen = scr, fontS = fs, dpy } <- get
let (c,(scr,fs)) = (config &&& screen &&& fontS) st let winWidth = case position cfg of
wh = case position c of CenteredAt{ xpWidth } -> floor $ fi (rect_width scr) * xpWidth
CenteredAt _ w -> floor $ fi (rect_width scr) * w
_ -> rect_width scr _ -> rect_width scr
ht = height c -- Height of a single completion row
bw = promptBorderWidth c ht = height cfg
bw = promptBorderWidth cfg
tws <- mapM (textWidthXMF (dpy st) fs) compl tws <- mapM (textWidthXMF dpy fs) compl
let max_compl_len = (fi ht `div` 2) + maximum tws let -- Length of widest completion we will print
limit_max_columns = maybe id min (maxComplColumns c) maxComplLen = (fi ht `div` 2) + maximum tws
columns = max 1 $ limit_max_columns $ wh `div` fi max_compl_len -- Height of the screen rectangle _without_ the prompt window
column_width = wh `div` columns remHeight = rect_height scr - ht
rem_height = rect_height scr - ht
(rows,r) = length compl `divMod` fi columns maxColumns = maybe id min (maxComplColumns cfg)
needed_rows = max 1 (rows + if r == 0 then 0 else 1) columns = max 1 . maxColumns $ winWidth `div` fi maxComplLen
limit_max_number = maybe id min (maxComplRows c) columnWidth = winWidth `div` columns
actual_max_number_of_rows = limit_max_number $ rem_height `div` ht
actual_rows = min actual_max_number_of_rows (fi needed_rows) (fullRows, lastRow) = length compl `divMod` fi columns
actual_height = actual_rows * ht allRows = max 1 (fullRows + if lastRow == 0 then 0 else 1)
(x,y) = case position c of -- Maximum number of rows allowed by the config and the screen dimensions
Top -> (0,ht - bw) maxRows = maybe id min (maxComplRows cfg) (remHeight `div` ht)
Bottom -> (0, 0 + rem_height - actual_height + bw) -- Actual number of rows to be drawn
rows = min maxRows (fi allRows)
rowHeight = rows * ht
-- Starting x and y position of the completion windows.
(x, y) = bimap (rect_x scr +) ((rect_y scr +) . fi) $ case position cfg of
Top -> (0, ht - bw)
Bottom -> (0, remHeight - rowHeight + bw)
CenteredAt py w CenteredAt py w
| py <= 1/2 -> (floor $ fi (rect_width scr) * ((1 - w) / 2), floor (py * fi (rect_height scr) + fi ht/2) - bw) | py <= 1/2 ->
| otherwise -> (floor $ fi (rect_width scr) * ((1 - w) / 2), floor (py * fi (rect_height scr) - fi ht/2) - actual_height + bw) ( floor $ fi (rect_width scr) * ((1 - w) / 2)
(asc,desc) <- io $ textExtentsXMF fs $ head compl , floor (py * fi (rect_height scr) + (fi ht / 2)) - bw
let yp = fi $ (ht + fi (asc - desc)) `div` 2 )
xp = (asc + desc) `div` 2 | otherwise ->
yy = map fi . take (fi actual_rows) $ [yp,(yp + ht)..] ( floor $ fi (rect_width scr) * ((1 - w) / 2)
xx = take (fi columns) [xp,(xp + fi column_width)..] , floor (py * fi (rect_height scr) - (fi ht / 2)) - rowHeight + bw
)
return $ ComplWindowDim (rect_x scr + x) (rect_y scr + fi y) wh actual_height xx yy -- Get font ascent and descent. Coherence condition: we will print
-- everything using the same font.
(asc, desc) <- io $ textExtentsXMF fs $ head compl
let yp = fi $ (ht + fi (asc - desc)) `div` 2 -- y position of the first row
yRows = take (fi rows) [yp, yp + fi ht ..] -- y positions of all rows
xp = (asc + desc) `div` 2 -- x position of the first column
xCols = take (fi columns) [xp, xp + fi columnWidth ..] -- x positions of all columns
pure $ ComplWindowDim x y winWidth rowHeight xCols yRows
drawComplWin :: Window -> [String] -> XP () drawComplWin :: Window -> [String] -> XP ()
drawComplWin w compl = do drawComplWin w compl = do