nano-ui-0.2.0.0: lib/NanoUI/Internal/Widgets/Caption.hs
-- | Client-side decorations for a frameless window: caption buttons,
-- window border, and title-bar drag regions. Window actions are handled by
-- the backend ('NanoUI.Sdl.Internal.Chrome.windowCaption' in @nano-ui-sdl@).
-- Glyphs are rendered as pixel-aligned geometry rather than font glyphs.
module NanoUI.Internal.Widgets.Caption
( -- * Buttons
CaptionGlyph (..)
, CaptionAction (..)
, CaptionConfig (..)
, defaultCaptionConfig
, captionButton
, captionButtons
, captionButtonsConfigured
-- * The window's border
, WindowFrame (..)
, defaultWindowFrame
, windowFrame
-- * Geometry
, captionBarHeight
, dragSpans
) where
import Control.Monad (when)
import Data.List (sortOn)
import Data.Maybe (fromMaybe)
import NanoUI.Internal.Context (Context (..), scopeRawTheme)
import NanoUI.Internal.Layout.Arena (getArenaScope)
import NanoUI.Internal.Monad (NanoUI, styled, themed, withContext)
import NanoUI.Internal.Style
import NanoUI.Internal.Types (Color, Rect (..), V2 (..), lerpColor, rectUnion)
import NanoUI.Path qualified as P
import NanoUI.Widgets.Custom
import NanoUI.Internal.Widgets.Layout (panelWith)
import NanoUI.Internal.Widgets.Node (Response, respClicked, respRect)
-- | Caption button kind and glyph.
data CaptionGlyph
= GlyphMinimize
| GlyphMaximize
| GlyphRestore
-- ^ Shown on the middle button while the window is maximized.
| GlyphClose
deriving (Eq, Show)
-- | Window action triggered by the caption buttons this frame.
data CaptionAction
= CaptionMinimize
| CaptionToggleMaximize
-- ^ Maximize a window that is restored, restore one that is maximized.
| CaptionClose
deriving (Eq, Show)
-- | Dimensions and styling for caption buttons.
data CaptionConfig = CaptionConfig
{ capButtonW :: !Float
-- ^ One button's width.
, capButtonH :: !Float
-- ^ One button's height, which is usually the title bar's.
, capGlyphSize :: !Float
-- ^ The side of the square a glyph is drawn in, centred in the button.
, capCloseColor :: !(Maybe Color)
-- ^ The close button's hover colour; the theme's red when unset.
, capCornerRadius :: !Float
-- ^ Rounding of the close button's top-right corner, which is also the
-- window's corner.
}
deriving (Eq, Show)
-- | 44 by 'captionBarHeight' buttons, a ten-pixel glyph, and an 8-pixel
-- corner matching the desktop's window rounding.
defaultCaptionConfig :: CaptionConfig
defaultCaptionConfig =
CaptionConfig
{ capButtonW = 44
, capButtonH = captionBarHeight
, capGlyphSize = 10
, capCloseColor = Nothing
, capCornerRadius = 8
}
-- | A title bar tall enough that a few pixels along its top can resize the
-- window without the rest becoming hard to aim at.
captionBarHeight :: Float
captionBarHeight = 30
-- | One caption button. Highlights on hover or press: 'GlyphClose' in red
-- (rounded at the top right by 'capCornerRadius'), the others a step up from
-- the window background colour.
captionButton :: CaptionConfig -> CaptionGlyph -> NanoUI Response
captionButton cfg glyph = do
(resp, _) <-
customWidget
defaultCustomWidgetSpec
{ widgetLayout = tight . fixedWH (capButtonW cfg) (capButtonH cfg)
, widgetDraw = \cdc rect -> runCanvasFor cdc $ do
let theme = cdcTheme cdc
lit = cdcHovered cdc || cdcPressed cdc
fg = styleFg (themePanel theme)
when lit $
if glyph == GlyphClose
then cornerRect (capCornerRadius cfg) rect (fromMaybe (themeRed theme) (capCloseColor cfg))
else drawRect rect (lerpColor (themeWindow theme) fg 0.18)
drawGlyph glyph (glyphBox (capGlyphSize cfg) rect) (if lit then fg else themeMuted theme)
}
pure resp
cornerRect :: Float -> Rect -> Color -> CanvasM ()
cornerRect radius r col
| radius <= 0 = drawRect r col
| otherwise = drawPath (P.roundedRectCorners r 0 radius 0 0) col
-- | The three caption buttons in a row: returns the triggered action and the
-- combined button rectangle to exclude from title-bar dragging. Pass whether
-- the window is maximized to select the middle button's glyph.
captionButtons :: Bool -> NanoUI (Maybe CaptionAction, Rect)
captionButtons = captionButtonsConfigured defaultCaptionConfig
-- | 'captionButtons' with custom 'CaptionConfig'.
captionButtonsConfigured :: CaptionConfig -> Bool -> NanoUI (Maybe CaptionAction, Rect)
captionButtonsConfigured cfg maximized = do
mini <- captionButton cfg GlyphMinimize
mid <- captionButton cfg (if maximized then GlyphRestore else GlyphMaximize)
close <- captionButton cfg GlyphClose
let action
| respClicked mini = Just CaptionMinimize
| respClicked mid = Just CaptionToggleMaximize
| respClicked close = Just CaptionClose
| otherwise = Nothing
pure (action, rectUnion (respRect mini) (respRect close))
--------------------------------------------------------------------------------
-- The window's border
--------------------------------------------------------------------------------
-- | Border style for a frameless window.
data WindowFrame = WindowFrame
{ frameWidth :: !Float
-- ^ The line's thickness, and the view's inset. Zero draws no border.
, frameRadius :: !Float
-- ^ Corner rounding, which should match the desktop's for the window.
, frameColor :: !Color
}
deriving (Eq, Show)
-- | A one-pixel border rounded by eight.
defaultWindowFrame :: Color -> WindowFrame
defaultWindowFrame col = WindowFrame {frameWidth = 1, frameRadius = 8, frameColor = col}
-- | Draw a border around the whole window, with the view inside it. A width
-- of zero (for a maximized window) draws none but keeps the container, so
-- the widgets inside keep their ids. The border style stays on the frame's
-- own panel; the view inside uses the surrounding theme.
windowFrame :: WindowFrame -> NanoUI a -> NanoUI a
windowFrame frame body = do
outer <- withContext $ \ctx -> scopeRawTheme ctx =<< getArenaScope (ctxNodeArena ctx)
styled
( panelStyle
( background (themeWindow outer)
. borderColor (frameColor frame)
. borderWidth (frameWidth frame)
. cornerRadius (frameRadius frame)
)
)
$ panelWith (grow . gap 0 . padAll (frameWidth frame) . tight) (themed outer body)
--------------------------------------------------------------------------------
-- Geometry
--------------------------------------------------------------------------------
-- | The parts of a title bar row that drag the window: the row minus the
-- rectangles that take their own clicks, cut horizontally only. Pass the
-- result to the backend as the drag region (@setWindowChrome@ in
-- @nano-ui-sdl@).
dragSpans :: Rect -> [Rect] -> [Rect]
dragSpans (Rect rx ry rw rh) taken =
filter (\r -> rectW r > 0) (go rx (sortOn rectX (filter overlaps taken)))
where
right = rx + rw
overlaps (Rect tx ty tw th) =
tw > 0 && th > 0 && tx < right && tx + tw > rx && ty < ry + rh && ty + th > ry
go x [] = [Rect x ry (right - x) rh]
go x (Rect tx _ tw _ : rest) =
Rect x ry (min right tx - x) rh : go (max x (tx + tw)) rest
--------------------------------------------------------------------------------
-- The glyphs
--------------------------------------------------------------------------------
-- | The square a glyph is drawn in, centred on whole pixels.
glyphBox :: Float -> Rect -> Rect
glyphBox s (Rect x y w h) = Rect (whole (x + (w - s) / 2)) (whole (y + (h - s) / 2)) s s
whole :: Float -> Float
whole v = fromIntegral (round v :: Int)
drawGlyph :: CaptionGlyph -> Rect -> Color -> CanvasM ()
drawGlyph glyph box@(Rect x y w h) col = case glyph of
GlyphMinimize -> drawRect (Rect x (y + whole (h / 2)) w 1) col
GlyphMaximize -> strokeBox box col
-- Two overlapping squares; the back one shows only its top and right edges.
GlyphRestore -> do
drawRect (Rect (x + 2) y (w - 2) 1) col
drawRect (Rect (x + w - 1) y 1 (h - 2)) col
strokeBox (Rect x (y + 2) (w - 2) (h - 2)) col
GlyphClose -> do
drawStrokeAA (V2 (x + 0.5) (y + 0.5)) (V2 (x + w - 0.5) (y + h - 0.5)) 1.2 col
drawStrokeAA (V2 (x + w - 0.5) (y + 0.5)) (V2 (x + 0.5) (y + h - 0.5)) 1.2 col
strokeBox :: Rect -> Color -> CanvasM ()
strokeBox (Rect x y w h) col = do
drawRect (Rect x y w 1) col
drawRect (Rect x (y + h - 1) w 1) col
drawRect (Rect x (y + 1) 1 (h - 2)) col
drawRect (Rect (x + w - 1) (y + 1) 1 (h - 2)) col