Skip to content

Commit

Permalink
Restore examples and text API to use (potentially flawed) Into<String…
Browse files Browse the repository at this point in the history
…> generic
  • Loading branch information
LPGhatguy committed Nov 20, 2024
1 parent a8a4daa commit 147cb5e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 30 deletions.
4 changes: 2 additions & 2 deletions crates/yakui-widgets/src/shorthand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ pub fn label<S: Into<Cow<'static, str>>>(text: S) -> Response<TextResponse> {
}

/// See [TextBox].
pub fn textbox(text: &str) -> Response<TextBoxResponse> {
TextBox::new(text.to_owned()).show()
pub fn textbox<S: Into<String>>(text: S) -> Response<TextBoxResponse> {
TextBox::new(text.into()).show()
}

/// See [Flexible].
Expand Down
2 changes: 1 addition & 1 deletion crates/yakui-widgets/src/widgets/render_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions crates/yakui-widgets/src/widgets/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ pub struct TextBox {
}

impl TextBox {
pub fn new(text: String) -> Self {
pub fn new<S: Into<String>>(text: S) -> Self {
let mut style = TextStyle::label();
style.align = TextAlignment::Start;

Self {
text,
text: text.into(),

style,
padding: Pad::all(8.0),
Expand Down
4 changes: 2 additions & 2 deletions crates/yakui/examples/panels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
36 changes: 13 additions & 23 deletions crates/yakui/examples/textbox.rs
Original file line number Diff line number Diff line change
@@ -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());
}
});
}

Expand Down

0 comments on commit 147cb5e

Please sign in to comment.