From 3012de944b091ce2857909cadd59cf0add76c94f Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 22 Aug 2022 20:22:25 +0200 Subject: [PATCH] Fix hidden editor rendering --- src/ui/widgets/editor.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/ui/widgets/editor.rs b/src/ui/widgets/editor.rs index a97e055..ff4a183 100644 --- a/src/ui/widgets/editor.rs +++ b/src/ui/widgets/editor.rs @@ -11,6 +11,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::ui::util; +use super::text::Text; use super::Widget; /// Like [`Frame::wrap`] but includes a final break index if the text ends with @@ -424,7 +425,7 @@ pub struct Editor { text: Styled, idx: usize, focus: bool, - hidden: Option, + hidden: Option>, } impl Editor { @@ -448,7 +449,7 @@ impl Editor { } pub fn hidden_with_placeholder>(mut self, placeholder: S) -> Self { - self.hidden = Some(placeholder.into()); + self.hidden = Some(Box::new(Text::new(placeholder))); self } @@ -479,7 +480,21 @@ impl Editor { #[async_trait] impl Widget for Editor { - fn size(&self, frame: &mut Frame, max_width: Option, _max_height: Option) -> Size { + fn size(&self, frame: &mut Frame, max_width: Option, max_height: Option) -> Size { + if let Some(placeholder) = &self.hidden { + let mut size = if self.text.text().is_empty() { + Size::new(1, 1) + } else { + placeholder.size(frame, max_width, max_height) + }; + + // Cursor needs to fit regardless of focus + size.width = size.width.max(1); + size.height = size.height.max(1); + + return size; + } + let max_width = max_width.map(|w| w as usize).unwrap_or(usize::MAX).max(1); let max_text_width = max_width - 1; let indices = wrap(frame, self.text.text(), max_text_width); @@ -497,7 +512,9 @@ impl Widget for Editor { async fn render(self: Box, frame: &mut Frame) { if let Some(placeholder) = self.hidden { - frame.write(Pos::ZERO, placeholder); + if !self.text.text().is_empty() { + placeholder.render(frame).await; + } if self.focus { frame.set_cursor(Some(Pos::ZERO)); }