ltk/widget/text_edit/
metrics.rsuse crate::render::Canvas;
use crate::types::{ Length, Rect };
use super::theme;
pub( crate ) fn single_line_scale( rect: Rect ) -> f32
{
rect.height / theme::HEIGHT
}
pub( crate ) fn single_line_pad_h( rect: Rect, pad_h: Option<f32> ) -> f32
{
pad_h.unwrap_or_else( || theme::PAD_H * single_line_scale( rect ) )
}
#[ derive( Clone, Copy, Debug ) ]
pub( crate ) struct MultilineMetrics
{
pub font_size: f32,
pub glyph_h: f32,
pub line_h: f32,
pub pad_h: f32,
pub pad_v: f32,
pub caret_w: f32,
pub caret_h: f32,
}
impl MultilineMetrics
{
pub( crate ) fn resolve( canvas: &Canvas, font_size: Option<Length> ) -> Self
{
let font_size = super::resolve_font_size( canvas, font_size );
let dpi = canvas.dpi_scale().max( 1.0 );
let glyph_h = font_size * dpi;
Self
{
font_size,
glyph_h,
line_h: glyph_h * theme::LINE_H_MULT,
pad_h: canvas.geom_px( theme::PAD_H ),
pad_v: canvas.geom_px( theme::PAD_V_MULTI ),
caret_w: ( theme::CARET_W * dpi ).round(),
caret_h: glyph_h + theme::CARET_OVERHANG * dpi,
}
}
pub( crate ) fn height_for_rows( &self, rows: u32 ) -> f32
{
rows as f32 * self.line_h + self.pad_v * 2.0
}
pub( crate ) fn inner_width( &self, rect: Rect ) -> f32
{
( rect.width - self.pad_h * 2.0 ).max( self.glyph_h )
}
pub( crate ) fn visible_lines( &self, rect: Rect ) -> usize
{
let inner_h = ( rect.height - self.pad_v * 2.0 ).max( self.line_h );
( inner_h / self.line_h ).floor().max( 1.0 ) as usize
}
}