Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement placeholder text in text boxes #105

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/yakui-core/src/geometry/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ impl Color {
.into_components()
.into()
}

/// Blend with `other` in linear space
pub fn lerp(&self, other: &Color, ratio: f32) -> Self {
Self::from_linear(self.to_linear().lerp(other.to_linear(), ratio))
}
}

macro_rules! builtin_colors {
Expand Down
21 changes: 19 additions & 2 deletions crates/yakui-widgets/src/widgets/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct TextBox {
pub style: TextStyle,
pub padding: Pad,
pub fill: Option<Color>,
/// Drawn when no text has been set
pub placeholder: String,
}

impl TextBox {
Expand All @@ -38,6 +40,7 @@ impl TextBox {
style: TextStyle::label(),
padding: Pad::all(8.0),
fill: Some(colors::BACKGROUND_3),
placeholder: String::new(),
}
}

Expand Down Expand Up @@ -76,12 +79,26 @@ impl Widget for TextBoxWidget {
fn update(&mut self, props: Self::Props) -> Self::Response {
self.props = props;

let text = self.updated_text.as_ref().unwrap_or(&self.props.text);
let mut text = self.updated_text.as_ref().unwrap_or(&self.props.text);
let use_placeholder = text.is_empty();
if use_placeholder {
text = &self.props.placeholder;
}

let mut render = RenderTextBox::new(text.clone());
render.style = self.props.style.clone();
render.selected = self.selected;
render.cursor = self.cursor;
if !use_placeholder {
render.cursor = self.cursor;
}
if use_placeholder {
// Dim towards background
render.style.color = self
.props
.style
.color
.lerp(&self.props.fill.unwrap_or(Color::CLEAR), 0.75);
}

pad(self.props.padding, || {
let res = render.show();
Expand Down
3 changes: 2 additions & 1 deletion crates/yakui/examples/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use yakui::widgets::{Pad, TextBox};
use yakui::{center, use_state};

pub fn run() {
let text = use_state(|| "Hello\nWorld\nBar".to_owned());
let text = use_state(|| "".to_owned());

center(|| {
let mut my_box = TextBox::new(text.borrow().as_str());
my_box.style.font_size = 60.0;
my_box.padding = Pad::all(50.0);
my_box.placeholder = "placeholder".into();

if let Some(new_text) = my_box.show().into_inner().text {
text.set(new_text);
Expand Down