freya_terminal/
component.rs

1use std::borrow::Cow;
2
3use freya_core::prelude::*;
4
5use crate::{
6    element::TerminalElement,
7    handle::TerminalHandle,
8};
9
10/// User-facing Terminal component
11#[derive(Clone, PartialEq)]
12pub struct Terminal {
13    handle: TerminalHandle,
14    font_family: Cow<'static, str>,
15    font_size: f32,
16    foreground: Color,
17    background: Color,
18    layout: LayoutData,
19    key: DiffKey,
20}
21
22impl Terminal {
23    /// Create a terminal with a handle for interactive PTY
24    pub fn new(handle: TerminalHandle) -> Self {
25        Self {
26            handle,
27            font_family: Cow::Borrowed("Cascadia Code"),
28            font_size: 14.,
29            foreground: (220, 220, 220).into(),
30            background: (10, 10, 10).into(),
31            layout: LayoutData::default(),
32            key: DiffKey::default(),
33        }
34    }
35
36    /// Set the font family for the terminal
37    pub fn font_family(mut self, font_family: impl Into<Cow<'static, str>>) -> Self {
38        self.font_family = font_family.into();
39        self
40    }
41
42    /// Set the font size for the terminal
43    pub fn font_size(mut self, font_size: f32) -> Self {
44        self.font_size = font_size;
45        self
46    }
47
48    /// Set the foreground color
49    pub fn foreground(mut self, foreground: impl Into<Color>) -> Self {
50        self.foreground = foreground.into();
51        self
52    }
53
54    /// Set the background color
55    pub fn background(mut self, background: impl Into<Color>) -> Self {
56        self.background = background.into();
57        self
58    }
59}
60
61impl Component for Terminal {
62    fn render(&self) -> impl IntoElement {
63        TerminalElement::new(self.handle.clone())
64            .font_family(self.font_family.clone())
65            .font_size(self.font_size)
66            .foreground(self.foreground)
67            .background(self.background)
68    }
69}
70
71impl KeyExt for Terminal {
72    fn write_key(&mut self) -> &mut DiffKey {
73        &mut self.key
74    }
75}
76
77impl LayoutExt for Terminal {
78    fn get_layout(&mut self) -> &mut LayoutData {
79        &mut self.layout
80    }
81}
82impl ContainerExt for Terminal {}