Skip to content

Commit

Permalink
reintroduce a way to update textbox
Browse files Browse the repository at this point in the history
  • Loading branch information
msparkles committed Jul 2, 2024
1 parent c6a5866 commit 6601d7e
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 27 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<S: Into<String>>(text: S) -> Response<TextBoxResponse> {
TextBox::new(text).show()
pub fn textbox(initial_text: &str, updated_text: Option<&str>) -> Response<TextBoxResponse> {
TextBox::with_text(initial_text, updated_text).show()
}

/// See [Flexible].
Expand Down
61 changes: 50 additions & 11 deletions crates/yakui-widgets/src/widgets/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Responds with [TextBoxResponse].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TextBox {
pub initial_text: String,
pub update_text: Option<String>,

pub style: TextStyle,
pub padding: Pad,
pub fill: Option<Color>,
Expand All @@ -42,9 +43,10 @@ pub struct TextBox {
}

impl TextBox {
pub fn new<S: Into<String>>(initial_text: S) -> Self {
pub fn new(update_text: Option<String>) -> Self {
Self {
initial_text: initial_text.into(),
update_text,

style: TextStyle::label(),
padding: Pad::all(8.0),
fill: Some(colors::BACKGROUND_3),
Expand All @@ -59,6 +61,18 @@ impl TextBox {
}
}

pub fn with_text(initial_text: &str, updated_text: Option<&str>) -> TextBox {
let first_time = use_state(|| true);

if first_time.get() {
first_time.set(false);

TextBox::new(Some(initial_text.into()))
} else {
TextBox::new(updated_text.map(Into::into))
}
}

pub fn show(self) -> Response<TextBoxResponse> {
let render_text = use_state(|| None as Option<(RenderText, Option<cosmic_text::Scroll>)>);

Expand Down Expand Up @@ -101,15 +115,16 @@ pub struct TextBoxWidget {
active: bool,
activated: bool,
lost_focus: bool,
text_up_to_date: Cell<bool>,
drag: DragState,
cosmic_editor: RefCell<Option<cosmic_text::Editor<'static>>>,
max_size: Cell<Option<(Option<f32>, Option<f32>)>>,
scale_factor: Cell<Option<f32>>,
}

pub struct TextBoxResponse {
render_text: RenderText,
scroll: Option<cosmic_text::Scroll>,
pub render_text: RenderText,
pub scroll: Option<cosmic_text::Scroll>,
pub text: Option<String>,
/// Whether the user pressed "Enter" in this box, only makes sense in inline
pub activated: bool,
Expand All @@ -123,10 +138,11 @@ impl Widget for TextBoxWidget {

fn new() -> Self {
Self {
props: TextBox::new(""),
props: TextBox::new(None),
active: false,
activated: false,
lost_focus: false,
text_up_to_date: Cell::new(true),
drag: DragState::None,
cosmic_editor: RefCell::new(None),
max_size: Cell::default(),
Expand All @@ -140,7 +156,7 @@ impl Widget for TextBoxWidget {
let mut style = self.props.style.clone();
let mut scroll = None;

let text = self.cosmic_editor.get_mut().as_mut().and_then(|editor| {
let text = self.cosmic_editor.borrow().as_ref().and_then(|editor| {
editor.with_buffer(|buffer| {
scroll = Some(buffer.scroll());

Expand All @@ -164,6 +180,10 @@ impl Widget for TextBoxWidget {
})
});

if self.props.update_text.is_some() {
self.text_up_to_date.set(false);
}

Self::Response {
render_text: RenderText {
text: text.clone().unwrap_or(self.props.placeholder.clone()),
Expand Down Expand Up @@ -197,10 +217,10 @@ impl Widget for TextBoxWidget {
)));
}

if self.scale_factor.get() != Some(ctx.layout.scale_factor())
|| self.max_size.get() != Some(max_size)
{
if let Some(editor) = self.cosmic_editor.borrow_mut().as_mut() {
if let Some(editor) = self.cosmic_editor.borrow_mut().as_mut() {
if self.scale_factor.get() != Some(ctx.layout.scale_factor())
|| self.max_size.get() != Some(max_size)
{
editor.with_buffer_mut(|buffer| {
buffer.set_metrics(
font_system,
Expand All @@ -215,6 +235,25 @@ impl Widget for TextBoxWidget {
self.scale_factor.set(Some(ctx.layout.scale_factor()));
self.max_size.replace(Some(max_size));
}

if let Some(new_text) = &self.props.update_text {
if !self.text_up_to_date.get() {
editor.with_buffer_mut(|buffer| {
buffer.set_text(
font_system,
&new_text,
self.props.style.attrs.as_attrs(),
cosmic_text::Shaping::Advanced,
);

println!("hi");
});

editor.set_cursor(cosmic_text::Cursor::new(0, 0));

self.text_up_to_date.set(true);
}
}
}
});

Expand Down
4 changes: 2 additions & 2 deletions crates/yakui/examples/autofocus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use yakui::widgets::{Pad, TextBox};
use yakui::{center, use_state};

pub fn run() {
let text = use_state(|| "".to_owned());
let text = use_state(|| String::new());
let autofocus = use_state(|| false);

center(|| {
let mut box1 = TextBox::new(text.borrow().as_str());
let mut box1 = TextBox::with_text("", None);
box1.style.font_size = 60.0;
box1.padding = Pad::all(50.0);
box1.placeholder = "placeholder".into();
Expand Down
11 changes: 7 additions & 4 deletions crates/yakui/examples/clear_textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ use yakui::{button, column, textbox, use_state};

fn run() {
column(|| {
let text = use_state(|| "Hello".to_string());
let text = use_state(|| String::new());
let clear = use_state(|| false);

let res = textbox("Hello", if clear.get() { Some("") } else { None });
clear.set(false);

let res = textbox(text.borrow().clone());
if let Some(new_text) = res.into_inner().text {
*text.borrow_mut() = new_text;
text.set(new_text);
}

if button("Clear").clicked {
text.borrow_mut().clear();
clear.set(true);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions crates/yakui/examples/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use yakui::{button, checkbox, label, pad, row, slider, textbox, use_state};

pub fn run() {
let checked = use_state(|| false);
let name = use_state(|| String::from("Hello"));
let name = use_state(|| String::new());
let step_size = use_state(|| 0.0);
let sliding = use_state(|| 50.0);

Expand All @@ -18,7 +18,7 @@ pub fn run() {
let res = checkbox(checked.get());
checked.set(res.checked);

let res = textbox(name.borrow().clone());
let res = textbox("Hello", None);
if let Some(new_name) = res.text.as_ref() {
name.set(new_name.clone());
}
Expand Down
8 changes: 4 additions & 4 deletions crates/yakui/examples/panels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ pub fn run() {
row(|| {
label("Input");
expanded(|| {
let name = use_state(|| String::from("Hello"));
let name = use_state(|| String::new());

let res = textbox(name.borrow().clone());
if let Some(new_name) = res.text.as_ref() {
name.set(new_name.clone());
let res = textbox("Hello", None);
if let Some(new_name) = res.into_inner().text {
name.set(new_name);
}
});
});
Expand Down
4 changes: 2 additions & 2 deletions crates/yakui/examples/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use yakui::widgets::{Pad, TextBox};
use yakui::{center, use_state};

pub fn run() {
let text = use_state(|| "".to_owned());
let text = use_state(|| String::new());

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

0 comments on commit 6601d7e

Please sign in to comment.