Skip to content

Commit

Permalink
fix(widgets): 🐛 input expand to container's size
Browse files Browse the repository at this point in the history
  • Loading branch information
wjian23 committed Nov 18, 2023
1 parent 7d8c435 commit c238395
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
1 change: 1 addition & 0 deletions widgets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lyon_path.workspace = true
ribir_core = {path = "../core", version = "0.0.1-alpha.4" }
ribir_geom = {path = "../geom", version = "0.0.1-alpha.4" }
webbrowser.workspace = true
winit.workspace = true

[dev-dependencies]
paste.workspace = true
Expand Down
77 changes: 64 additions & 13 deletions widgets/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ where
let only_text = text.clone_reader();

let mut stack = @Stack {
fit: StackFit::Loose,
fit: StackFit::Passthrough,
scrollable: scroll_dir,
};

Expand Down Expand Up @@ -334,18 +334,24 @@ where
},
};

let high_light_rect = @OnlySizedByParent {
@SelectedHighLight {
visible: pipe!($stack.has_focus()),
rects: pipe! {
$this.select_text_rect(&$only_text, $text.layout_size())
let high_light_rect = @UnconstrainedBox {
clamp_dim: ClampDim::MIN_SIZE,
@OnlySizedByParent {
@SelectedHighLight {
visible: pipe!($stack.has_focus()),
rects: pipe! {
$this.select_text_rect(&$only_text, $text.layout_size())
}
}
}
};

let caret = @OnlySizedByParent {
@$caret_box {
@Caret { focused: pipe!($stack.has_focus()) }
let caret = @UnconstrainedBox {
clamp_dim: ClampDim::MIN_SIZE,
@OnlySizedByParent {
@$caret_box {
@Caret { focused: pipe!($stack.has_focus()) }
}
}
};

Expand Down Expand Up @@ -373,10 +379,7 @@ impl EditableTextExtraWidget for TextArea {}
impl EditableTextExtraWidget for Input {}

fn size_clamp(style: &TextStyle, rows: Option<f32>, cols: Option<f32>) -> BoxClamp {
let mut clamp: BoxClamp = BoxClamp {
min: Size::new(0., 0.),
max: Size::new(f32::INFINITY, f32::INFINITY),
};
let mut clamp: BoxClamp = BoxClamp::EXPAND_BOTH;
if let Some(cols) = cols {
let width = cols * style.font_size.into_pixel().value();
clamp = clamp.with_fixed_width(width);
Expand Down Expand Up @@ -423,7 +426,9 @@ fn auto_scroll_pos(container: &ScrollableWidget, before: Point, after: Point, si
#[cfg(test)]
mod tests {
use super::{EditableText, Input};
use crate::layout::SizedBox;
use ribir_core::{prelude::*, reset_test_env, test_helper::TestWindow};
use winit::event::{DeviceId, ElementState, MouseButton, WindowEvent};

fn split_value<T: 'static>(v: T) -> (impl StateReader<Value = T>, impl StateWriter<Value = T>) {
let src = Stateful::new(v);
Expand Down Expand Up @@ -453,4 +458,50 @@ mod tests {
wnd.draw_frame();
assert_eq!(*input_value.read(), "hello world");
}

#[test]
fn input_tap_focus() {
reset_test_env!();
let (input_value, input_value_writer) = split_value(String::default());
let w = fn_widget! {
let input = @Input { auto_focus: true };
watch!($input.text().clone())
.subscribe(move |text| {
*input_value_writer.write() = text.to_string();
});

@SizedBox {
size: Size::new(200., 24.),
@ { input }
}
};

let mut wnd = TestWindow::new_with_size(w, Size::new(200., 200.));
wnd.draw_frame();
assert_eq!(*input_value.read(), "");

let device_id = unsafe { DeviceId::dummy() };
#[allow(deprecated)]
wnd.processes_native_event(WindowEvent::CursorMoved {
device_id,
position: (50., 10.).into(),
});
#[allow(deprecated)]
wnd.processes_native_event(WindowEvent::MouseInput {
device_id,
state: ElementState::Pressed,
button: MouseButton::Left,
});
#[allow(deprecated)]
wnd.processes_native_event(WindowEvent::MouseInput {
device_id,
state: ElementState::Released,
button: MouseButton::Left,
});
wnd.draw_frame();

wnd.processes_receive_chars("hello".into());
wnd.draw_frame();
assert_eq!(*input_value.read(), "hello");
}
}

0 comments on commit c238395

Please sign in to comment.