From 147cb5ec70cf20ead000ba293b5fa81e2de79fc8 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Wed, 20 Nov 2024 16:43:02 -0500 Subject: [PATCH] Restore examples and text API to use (potentially flawed) Into generic --- crates/yakui-widgets/src/shorthand.rs | 4 +-- .../yakui-widgets/src/widgets/render_text.rs | 2 +- crates/yakui-widgets/src/widgets/textbox.rs | 4 +-- crates/yakui/examples/panels.rs | 4 +-- crates/yakui/examples/textbox.rs | 36 +++++++------------ 5 files changed, 20 insertions(+), 30 deletions(-) diff --git a/crates/yakui-widgets/src/shorthand.rs b/crates/yakui-widgets/src/shorthand.rs index 3a0c5484..bfcbb68f 100644 --- a/crates/yakui-widgets/src/shorthand.rs +++ b/crates/yakui-widgets/src/shorthand.rs @@ -100,8 +100,8 @@ pub fn label>>(text: S) -> Response { } /// See [TextBox]. -pub fn textbox(text: &str) -> Response { - TextBox::new(text.to_owned()).show() +pub fn textbox>(text: S) -> Response { + TextBox::new(text.into()).show() } /// See [Flexible]. diff --git a/crates/yakui-widgets/src/widgets/render_text.rs b/crates/yakui-widgets/src/widgets/render_text.rs index 9b1b50a6..ef240580 100644 --- a/crates/yakui-widgets/src/widgets/render_text.rs +++ b/crates/yakui-widgets/src/widgets/render_text.rs @@ -14,7 +14,7 @@ use crate::util::widget; Renders text. You probably want to use [Text][super::Text] instead, which supports features like padding. -Responds with [RenderTextBoxResponse]. +Responds with [RenderTextResponse]. */ #[derive(Debug, Clone, Default)] #[non_exhaustive] diff --git a/crates/yakui-widgets/src/widgets/textbox.rs b/crates/yakui-widgets/src/widgets/textbox.rs index 3618e277..c5838a9d 100644 --- a/crates/yakui-widgets/src/widgets/textbox.rs +++ b/crates/yakui-widgets/src/widgets/textbox.rs @@ -46,12 +46,12 @@ pub struct TextBox { } impl TextBox { - pub fn new(text: String) -> Self { + pub fn new>(text: S) -> Self { let mut style = TextStyle::label(); style.align = TextAlignment::Start; Self { - text, + text: text.into(), style, padding: Pad::all(8.0), diff --git a/crates/yakui/examples/panels.rs b/crates/yakui/examples/panels.rs index 036b2fd3..8d37f440 100644 --- a/crates/yakui/examples/panels.rs +++ b/crates/yakui/examples/panels.rs @@ -32,9 +32,9 @@ pub fn run() { row(|| { label("Input"); expanded(|| { - let name = use_state(|| String::new()); + let name = use_state(|| String::from("Hello")); - let res = textbox("Hello"); + let res = textbox(name.borrow().clone()); if let Some(new_text) = res.into_inner().text { name.set(new_text); } diff --git a/crates/yakui/examples/textbox.rs b/crates/yakui/examples/textbox.rs index f0177b02..665f7e19 100644 --- a/crates/yakui/examples/textbox.rs +++ b/crates/yakui/examples/textbox.rs @@ -1,32 +1,22 @@ use yakui::widgets::{Pad, TextBox}; -use yakui::{button, center, expanded, row, use_state}; +use yakui::{center, use_state}; pub fn run() { - let text = use_state(|| "Hello!".to_owned()); - - // println!("Text: {}", text.borrow()); + let text = use_state(|| "".to_owned()); center(|| { - row(|| { - expanded(|| { - let mut my_box = TextBox::new(text.borrow().to_owned()); - // my_box.style.font_size = 60.0; - // my_box.padding = Pad::all(20.0); - my_box.placeholder = "placeholder".into(); - - let response = my_box.show().into_inner(); - if let Some(new_text) = response.text { - text.set(new_text); - } - if response.activated { - println!("{}", text.borrow()); - } - }); + 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 button("Reset Text").clicked { - text.set("Hello!".to_owned()); - } - }); + let response = my_box.show().into_inner(); + if let Some(new_text) = response.text { + text.set(new_text); + } + if response.activated { + println!("{}", text.borrow()); + } }); }