mod keyboard;
mod log_view;
mod panel;
use core::time::Duration;
use glam::Vec2;
use crcbl_core::log::Level;
use crate::text::{FontAtlas, LINE_HEIGHT};
use crate::widget::{NATURAL_FONT_SIZE, Style};
pub use keyboard::{
KEY_ID_BASE, KEY_ID_SPAN, KEYBOARD_HEIGHT_FRACTION, KeyBox, KeyCap, KeyboardLayout, Layer,
TouchKeyboard,
};
pub use log_view::{LogLine, LogView};
pub use panel::{
COMPLETION_ROWS, CONSOLE_HEIGHT_FRACTION, ConsoleInput, ConsoleLayout, ConsolePanel,
MINIMUM_FIELD_COLUMNS, MINIMUM_LOG_ROWS, PROMPT, SEND_ID, SEND_LABEL,
};
pub const CARET_BLINK: Duration = Duration::from_millis(530);
#[must_use]
pub fn caret_shown(elapsed: Duration) -> bool {
let period = CARET_BLINK.as_nanos().max(1);
(elapsed.as_nanos() / period).is_multiple_of(2)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ConsoleStyle {
pub scale: f32,
pub text_size: f32,
pub padding: Vec2,
pub caret_width: f32,
pub panel_color: [f32; 4],
pub border_color: [f32; 4],
pub field_color: [f32; 4],
pub prompt_color: [f32; 4],
pub error_color: [f32; 4],
pub warn_color: [f32; 4],
pub info_color: [f32; 4],
pub debug_color: [f32; 4],
pub trace_color: [f32; 4],
pub completion_color: [f32; 4],
pub candidate_color: [f32; 4],
pub match_color: [f32; 4],
pub button: Style,
}
impl ConsoleStyle {
#[must_use]
pub fn pixel_art(scale: u32) -> Self {
let scale = scale.max(1) as f32;
Self {
scale,
text_size: NATURAL_FONT_SIZE * scale,
padding: Vec2::new(6.0, 4.0) * scale,
caret_width: 2.0 * scale,
panel_color: [0.04, 0.05, 0.08, 0.92],
border_color: [0.42, 0.46, 0.72, 1.0],
field_color: [0.10, 0.11, 0.18, 1.0],
prompt_color: [0.62, 0.64, 0.82, 1.0],
error_color: [1.0, 0.44, 0.40, 1.0],
warn_color: [1.0, 0.78, 0.35, 1.0],
info_color: [0.90, 0.92, 1.0, 1.0],
debug_color: [0.66, 0.70, 0.82, 1.0],
trace_color: [0.50, 0.54, 0.66, 1.0],
completion_color: [0.08, 0.09, 0.14, 0.98],
candidate_color: [0.80, 0.83, 0.95, 1.0],
match_color: [1.0, 0.94, 0.55, 1.0],
button: Style {
text: [0.94, 0.95, 1.0, 1.0],
bg: [0.16, 0.18, 0.30, 1.0],
bg_hover: [0.24, 0.27, 0.44, 1.0],
bg_active: [0.34, 0.38, 0.60, 1.0],
border: [0.42, 0.46, 0.72, 1.0],
},
}
}
#[must_use]
pub fn level_color(&self, level: Level) -> [f32; 4] {
match level {
Level::Error => self.error_color,
Level::Warn => self.warn_color,
Level::Info => self.info_color,
Level::Debug => self.debug_color,
Level::Trace => self.trace_color,
}
}
#[must_use]
pub fn row_height(&self) -> f32 {
LINE_HEIGHT * self.glyph_scale()
}
#[must_use]
pub fn advance(&self, atlas: &FontAtlas) -> f32 {
atlas.text_width("M", self.glyph_scale())
}
fn glyph_scale(&self) -> f32 {
self.text_size / NATURAL_FONT_SIZE
}
}
impl Default for ConsoleStyle {
fn default() -> Self {
Self::pixel_art(1)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_caret_blinks_on_and_off_at_the_declared_interval() {
assert!(caret_shown(Duration::ZERO), "the caret starts shown");
assert!(caret_shown(CARET_BLINK - Duration::from_millis(1)));
assert!(!caret_shown(CARET_BLINK));
assert!(!caret_shown(CARET_BLINK * 2 - Duration::from_millis(1)));
assert!(caret_shown(CARET_BLINK * 2), "the blink did not repeat");
}
#[test]
fn a_bigger_scale_is_a_bigger_panel_and_not_a_different_one() {
let one = ConsoleStyle::pixel_art(1);
let three = ConsoleStyle::pixel_art(3);
assert_eq!(three.text_size, one.text_size * 3.0);
assert_eq!(three.row_height(), one.row_height() * 3.0);
assert_eq!(three.padding, one.padding * 3.0);
assert_eq!(three.caret_width, one.caret_width * 3.0);
assert_eq!(three.panel_color, one.panel_color);
assert_eq!(three.button, one.button);
assert_eq!(
ConsoleStyle::pixel_art(0),
one,
"a zero scale is an invisible panel, not a smaller one",
);
}
#[test]
fn every_level_has_a_colour_of_its_own() {
let style = ConsoleStyle::default();
let mut seen: Vec<[f32; 4]> = Vec::new();
for level in [
Level::Error,
Level::Warn,
Level::Info,
Level::Debug,
Level::Trace,
] {
let color = style.level_color(level);
assert!(
!seen.contains(&color),
"{level} shares its colour with another level",
);
seen.push(color);
}
}
#[test]
fn a_column_is_one_glyph_advance_at_every_scale() {
let atlas = FontAtlas::built_in();
for scale in 1..=4 {
let style = ConsoleStyle::pixel_art(scale);
let advance = style.advance(&atlas);
assert!(
(advance - crate::text::GLYPH_ADVANCE * scale as f32).abs() < 1e-3,
"scale {scale} makes a column {advance} pixels wide",
);
for glyph in ["i", " ", "W"] {
assert!(
(atlas.text_width(glyph, style.text_size / NATURAL_FONT_SIZE) - advance).abs()
< 1e-3,
"{glyph:?} is not one column wide, so the wrap cannot count",
);
}
}
}
}