Skip to content

Commit

Permalink
cleanup textbox
Browse files Browse the repository at this point in the history
  • Loading branch information
msparkles committed Jul 9, 2024
1 parent 675ac05 commit 31b549c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 39 deletions.
34 changes: 11 additions & 23 deletions crates/yakui-widgets/src/widgets/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ 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>)>>,
Expand All @@ -128,7 +127,7 @@ pub struct TextBoxWidget {
pub struct TextBoxResponse {
pub render_text: RenderText,
pub scroll: Option<cosmic_text::Scroll>,
pub text: Option<String>,
pub text: String,
/// Whether the user pressed "Enter" in this box, only makes sense in inline
pub activated: bool,
/// Whether the box lost focus
Expand All @@ -145,7 +144,6 @@ impl Widget for TextBoxWidget {
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 Down Expand Up @@ -183,17 +181,13 @@ 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()),
style,
},
scroll,
text,
text: text.unwrap_or_default(),
activated: mem::take(&mut self.activated),
lost_focus: mem::take(&mut self.lost_focus),
}
Expand Down Expand Up @@ -231,29 +225,23 @@ impl Widget for TextBoxWidget {
);

buffer.set_size(font_system, max_width, max_height);

buffer.shape_until_scroll(font_system, false);
});

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,
);
});

editor.set_cursor(cosmic_text::Cursor::new(0, 0));
editor.with_buffer_mut(|buffer| {
buffer.set_text(
font_system,
new_text,
self.props.style.attrs.as_attrs(),
cosmic_text::Shaping::Advanced,
);
});

self.text_up_to_date.set(true);
}
editor.set_cursor(cosmic_text::Cursor::new(0, 0));
}
}
});
Expand Down
4 changes: 1 addition & 3 deletions crates/yakui/examples/autofocus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub fn run() {
response.request_focus();
}

if let Some(new_text) = response.into_inner().text {
text.set(new_text);
}
text.set(response.into_inner().text);
});
}

Expand Down
5 changes: 1 addition & 4 deletions crates/yakui/examples/clear_textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ fn run() {

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

if let Some(new_text) = res.into_inner().text {
text.set(new_text);
}
text.set(res.into_inner().text);

if button("Clear").clicked {
clear.set(true);
Expand Down
4 changes: 1 addition & 3 deletions crates/yakui/examples/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ pub fn run() {
checked.set(res.checked);

let res = textbox("Hello", None);
if let Some(new_name) = res.text.as_ref() {
name.set(new_name.clone());
}
name.set(res.into_inner().text);

row(|| {
if let Some(new_step_size) = slider(step_size.get(), 0.0, 1.0).value {
Expand Down
4 changes: 1 addition & 3 deletions crates/yakui/examples/panels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ pub fn run() {
let name = use_state(|| String::new());

let res = textbox("Hello", None);
if let Some(new_name) = res.into_inner().text {
name.set(new_name);
}
name.set(res.into_inner().text);
});
});

Expand Down
4 changes: 1 addition & 3 deletions crates/yakui/examples/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ pub fn run() {
my_box.placeholder = "placeholder".into();

let response = my_box.show().into_inner();
if let Some(new_text) = response.text {
text.set(new_text);
}
text.set(response.text);
if response.activated {
println!("{}", text.borrow());
}
Expand Down

0 comments on commit 31b549c

Please sign in to comment.