ltk/widget/separator/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>
use crate::types::{ Color, Rect };
use crate::render::Canvas;
use super::Element;
mod theme;
/// A horizontal divider line.
///
/// Renders a 1 px (default) line across the full width of its layout rect,
/// with vertical padding above and below. Use to break a column into
/// visual sections — between settings groups, list categories or content
/// blocks. The line takes the divider colour from the active theme by
/// default; override with [`Self::color`] for custom palettes.
///
/// ```rust,no_run
/// # use ltk::{ column, separator, text, Element };
/// # #[ derive( Clone ) ] enum Msg {}
/// # fn _ex() -> Element<Msg> {
/// // In view():
/// column()
/// .push( text( "General" ) )
/// .push( separator() )
/// .push( text( "Network" ) )
/// .into()
/// # }
/// ```
pub struct Separator
{
/// Stroke colour. Defaults to the theme's `divider` palette slot.
pub color: Color,
/// Line thickness in logical pixels.
pub thickness: f32,
/// Vertical padding above and below the line, baked into
/// `preferred_size` so the divider visually centres in the row a
/// parent column allocates.
pub pad_v: f32,
}
impl Separator
{
/// Create a separator with the theme's default divider colour and
/// 1 px thickness.
pub fn new() -> Self
{
Self
{
color: theme::color(),
thickness: theme::THICKNESS,
pad_v: theme::PAD_V,
}
}
/// Override the stroke colour. Useful for emphasised dividers between
/// destructive actions.
pub fn color( mut self, color: Color ) -> Self
{
self.color = color;
self
}
/// Override the line thickness. Defaults to 1 logical pixel.
pub fn thickness( mut self, t: f32 ) -> Self
{
self.thickness = t;
self
}
/// Return the preferred `(width, height)`. Width is `max_width`;
/// height is `thickness + 2 × pad_v`.
pub fn preferred_size( &self, max_width: f32 ) -> (f32, f32)
{
( max_width, self.thickness + self.pad_v * 2.0 )
}
/// Draw the divider line into `canvas` at `rect`.
pub fn draw( &self, canvas: &mut Canvas, rect: Rect )
{
let y = rect.y + rect.height / 2.0;
canvas.draw_line( rect.x, y, rect.x + rect.width, y, self.color, self.thickness );
}
}
/// Create a default [`Separator`] (theme divider colour, 1 px thickness).
///
/// ```rust,no_run
/// # use ltk::{ column, separator, text, Element };
/// # #[ derive( Clone ) ] enum Msg {}
/// # fn _ex() -> Element<Msg> {
/// column()
/// .push( text( "Section A" ) )
/// .push( separator() )
/// .push( text( "Section B" ) )
/// .into()
/// # }
/// ```
pub fn separator() -> Separator
{
Separator::new()
}
impl<Msg: Clone> From<Separator> for Element<Msg>
{
fn from( s: Separator ) -> Self
{
Element::Separator( s )
}
}
#[cfg(test)]
mod tests;