mirror of
https://seed.radicle.dev/rad:z39mP9rQAaGmERfUMPULfPUi473tY.git
synced 2026-11-01 09:20:33 +08:00
lib: Improve imUI text view
This commit is contained in:
+3
-3
@@ -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)
|
||||
|
||||
+17
-13
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user