Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
lib: Improve imUI text view
Erik Kundt committed 1 year ago
commit f84f676fc61ed9b1654ac8bb1ca5e3fb12ab1069
parent 9f97181d5a1643345cbb5a09020479771fa90b0c
2 files changed +20 -16
modified src/ui/im.rs
@@ -519,11 +519,11 @@ where
        widget::Bar::new(columns, borders).ui(self, frame)
    }

-
    pub fn text_view(
+
    pub fn text_view<'a>(
        &mut self,
        frame: &mut Frame,
-
        text: String,
-
        scroll: &mut (usize, usize),
+
        text: impl Into<Text<'a>>,
+
        scroll: &'a mut (usize, usize),
        borders: Option<Borders>,
    ) -> Response {
        widget::TextView::new(text, scroll, borders).ui(self, frame)
modified src/ui/im/widget.rs
@@ -605,20 +605,20 @@ impl<'a> Widget for Bar<'a> {
}

#[derive(Clone, Debug)]
-
pub struct TextViewState {
-
    text: String,
+
pub struct TextViewState<'a> {
+
    text: Text<'a>,
    cursor: (usize, usize),
}

-
impl TextViewState {
-
    pub fn new(text: impl Into<String>, cursor: (usize, usize)) -> Self {
+
impl<'a> TextViewState<'a> {
+
    pub fn new(text: impl Into<Text<'a>>, cursor: (usize, usize)) -> Self {
        Self {
            text: text.into(),
            cursor,
        }
    }

-
    pub fn text(&self) -> &String {
+
    pub fn text(&self) -> &Text<'a> {
        &self.text
    }

@@ -627,7 +627,7 @@ impl TextViewState {
    }
}

-
impl TextViewState {
+
impl<'a> TextViewState<'a> {
    fn scroll_up(&mut self) {
        self.cursor.0 = self.cursor.0.saturating_sub(1);
    }
@@ -668,19 +668,19 @@ impl TextViewState {
}

pub struct TextView<'a> {
-
    text: String,
+
    text: Text<'a>,
    borders: Option<Borders>,
    cursor: &'a mut (usize, usize),
}

impl<'a> TextView<'a> {
    pub fn new(
-
        text: impl ToString,
+
        text: impl Into<Text<'a>>,
        cursor: &'a mut (usize, usize),
        borders: Option<Borders>,
    ) -> Self {
        Self {
-
            text: text.to_string(),
+
            text: text.into(),
            borders,
            cursor,
        }
@@ -702,7 +702,7 @@ impl<'a> Widget for TextView<'a> {
        } else {
            ui.theme.border_style
        };
-
        let length = self.text.lines().count();
+
        let length = self.text.lines.len();
        // let virtual_length = length * ((length as f64).log2() as usize) / 100;
        // let content_length = area.height as usize + virtual_length;
        // let content_length = length;
@@ -749,9 +749,13 @@ impl<'a> Widget for TextView<'a> {
        let mut state = TextViewState::new(self.text.clone(), *self.cursor);

        if let Some(key) = ui.input_with_key(|_| true) {
-
            let lines = self.text.lines().clone();
-
            let len = lines.clone().count();
-
            let max_line_len = lines.map(|l| l.chars().count()).max().unwrap_or_default();
+
            let lines = self.text.lines.clone();
+
            let len = lines.clone().len();
+
            let max_line_len = lines
+
                .into_iter()
+
                .map(|l| l.to_string().chars().count())
+
                .max()
+
                .unwrap_or_default();
            let page_size = area.height as usize;

            match key {