Skip to content

Commit

Permalink
make textboxes only respond with a text field when text is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
msparkles committed Jul 11, 2024
1 parent 31b549c commit 5521b7f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 31 deletions.
67 changes: 41 additions & 26 deletions crates/yakui-widgets/src/widgets/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ pub struct TextBoxWidget {
drag: DragState,
cosmic_editor: RefCell<Option<cosmic_text::Editor<'static>>>,
max_size: Cell<Option<(Option<f32>, Option<f32>)>>,
text_changed: Cell<bool>,
scale_factor: Cell<Option<f32>>,
}

pub struct TextBoxResponse {
pub render_text: RenderText,
pub scroll: Option<cosmic_text::Scroll>,
pub text: String,
pub text: Option<String>,
/// Whether the user pressed "Enter" in this box, only makes sense in inline
pub activated: bool,
/// Whether the box lost focus
Expand All @@ -147,6 +148,7 @@ impl Widget for TextBoxWidget {
drag: DragState::None,
cosmic_editor: RefCell::new(None),
max_size: Cell::default(),
text_changed: Cell::default(),
scale_factor: Cell::default(),
}
}
Expand All @@ -157,37 +159,43 @@ impl Widget for TextBoxWidget {
let mut style = self.props.style.clone();
let mut scroll = None;

let text = self.cosmic_editor.borrow().as_ref().and_then(|editor| {
let mut is_empty = false;

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

if buffer.lines.iter().all(|v| v.text().is_empty()) {
// Dim towards background
style.color = style
.color
.lerp(&self.props.fill.unwrap_or(Color::CLEAR), 0.75);

None
} else {
Some(
buffer
.lines
.iter()
.map(|v| v.text())
.collect::<Vec<_>>()
.join("\n"),
)
}
is_empty = buffer.lines.iter().all(|v| v.text().is_empty());

buffer
.lines
.iter()
.map(|v| v.text())
.collect::<Vec<_>>()
.join("\n")
})
});

if is_empty {
// Dim towards background
style.color = style
.color
.lerp(&self.props.fill.unwrap_or(Color::CLEAR), 0.75);
}

Self::Response {
scroll,
text: if self.text_changed.take() {
text.clone()
} else {
None
},
render_text: RenderText {
text: text.clone().unwrap_or(self.props.placeholder.clone()),
text: (!is_empty)
.then_some(text)
.flatten()
.unwrap_or(self.props.placeholder.clone()),
style,
},
scroll,
text: text.unwrap_or_default(),
activated: mem::take(&mut self.activated),
lost_focus: mem::take(&mut self.lost_focus),
}
Expand Down Expand Up @@ -232,6 +240,8 @@ impl Widget for TextBoxWidget {
}

if let Some(new_text) = &self.props.update_text {
self.text_changed.set(true);

editor.with_buffer_mut(|buffer| {
buffer.set_text(
font_system,
Expand Down Expand Up @@ -498,7 +508,6 @@ impl Widget for TextBoxWidget {
cosmic_text::Action::Motion(cosmic_text::Motion::PageUp),
);
}

EventResponse::Sink
}

Expand All @@ -509,20 +518,21 @@ impl Widget for TextBoxWidget {
cosmic_text::Action::Motion(cosmic_text::Motion::PageDown),
);
}

EventResponse::Sink
}

KeyCode::Backspace => {
if *down {
editor.action(font_system, cosmic_text::Action::Backspace);
self.text_changed.set(true);
}
EventResponse::Sink
}

KeyCode::Delete => {
if *down {
editor.action(font_system, cosmic_text::Action::Delete);
self.text_changed.set(true);
}
EventResponse::Sink
}
Expand Down Expand Up @@ -552,12 +562,14 @@ impl Widget for TextBoxWidget {
if self.props.inline_edit {
if self.props.multiline && modifiers.shift() {
editor.action(font_system, cosmic_text::Action::Enter);
self.text_changed.set(true);
} else {
self.activated = true;
ctx.input.set_selection(None);
}
} else {
editor.action(font_system, cosmic_text::Action::Enter);
self.text_changed.set(true);
}
}
EventResponse::Sink
Expand All @@ -566,7 +578,9 @@ impl Widget for TextBoxWidget {
KeyCode::Escape => {
if *down {
editor.action(font_system, cosmic_text::Action::Escape);
ctx.input.set_selection(None);
if self.props.inline_edit {
ctx.input.set_selection(None);
}
}
EventResponse::Sink
}
Expand All @@ -591,6 +605,7 @@ impl Widget for TextBoxWidget {
}
} else {
editor.action(font_system, cosmic_text::Action::Insert(*c));
self.text_changed.set(true);
}
}
});
Expand Down
4 changes: 3 additions & 1 deletion crates/yakui/examples/autofocus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ pub fn run() {
response.request_focus();
}

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

Expand Down
4 changes: 3 additions & 1 deletion crates/yakui/examples/clear_textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ fn run() {

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

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

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

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

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

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

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

0 comments on commit 5521b7f

Please sign in to comment.