Add option to remove the frame from a TextEdit

This commit is contained in:
Emil Ernerfeldt 2021-01-13 23:14:46 +01:00
parent fb5bd801b8
commit 99726decb6

View file

@ -127,6 +127,7 @@ pub struct TextEdit<'t> {
id_source: Option<Id>, id_source: Option<Id>,
text_style: Option<TextStyle>, text_style: Option<TextStyle>,
text_color: Option<Color32>, text_color: Option<Color32>,
frame: bool,
multiline: bool, multiline: bool,
enabled: bool, enabled: bool,
desired_width: Option<f32>, desired_width: Option<f32>,
@ -147,6 +148,7 @@ impl<'t> TextEdit<'t> {
id_source: None, id_source: None,
text_style: None, text_style: None,
text_color: None, text_color: None,
frame: true,
multiline: false, multiline: false,
enabled: true, enabled: true,
desired_width: None, desired_width: None,
@ -161,6 +163,7 @@ impl<'t> TextEdit<'t> {
id: None, id: None,
id_source: None, id_source: None,
text_style: None, text_style: None,
frame: true,
text_color: None, text_color: None,
multiline: true, multiline: true,
enabled: true, enabled: true,
@ -201,6 +204,12 @@ impl<'t> TextEdit<'t> {
self self
} }
/// Default is `true`. If set to `false` there will be no frame showing that this is editable text!
pub fn frame(mut self, frame: bool) -> Self {
self.frame = frame;
self
}
/// Set to 0.0 to keep as small as possible /// Set to 0.0 to keep as small as possible
pub fn desired_width(mut self, desired_width: f32) -> Self { pub fn desired_width(mut self, desired_width: f32) -> Self {
self.desired_width = Some(desired_width); self.desired_width = Some(desired_width);
@ -218,6 +227,7 @@ impl<'t> TextEdit<'t> {
impl<'t> Widget for TextEdit<'t> { impl<'t> Widget for TextEdit<'t> {
fn ui(self, ui: &mut Ui) -> Response { fn ui(self, ui: &mut Ui) -> Response {
let frame = self.frame;
let margin = Vec2::splat(2.0); let margin = Vec2::splat(2.0);
let frame_rect = ui.available_rect_before_wrap(); let frame_rect = ui.available_rect_before_wrap();
let content_rect = frame_rect.shrink2(margin); let content_rect = frame_rect.shrink2(margin);
@ -227,17 +237,19 @@ impl<'t> Widget for TextEdit<'t> {
let frame_rect = Rect::from_min_max(frame_rect.min, content_ui.min_rect().max + margin); let frame_rect = Rect::from_min_max(frame_rect.min, content_ui.min_rect().max + margin);
let response = response | ui.allocate_response(frame_rect.size(), Sense::click()); let response = response | ui.allocate_response(frame_rect.size(), Sense::click());
let visuals = ui.style().interact(&response); if frame {
let frame_rect = response.rect.expand(visuals.expansion); let visuals = ui.style().interact(&response);
ui.painter().set( let frame_rect = response.rect.expand(visuals.expansion);
where_to_put_background, ui.painter().set(
Shape::Rect { where_to_put_background,
rect: frame_rect, Shape::Rect {
corner_radius: visuals.corner_radius, rect: frame_rect,
fill: ui.style().visuals.dark_bg_color, corner_radius: visuals.corner_radius,
stroke: visuals.bg_stroke, fill: ui.style().visuals.dark_bg_color,
}, stroke: visuals.bg_stroke,
); },
);
}
response response
} }
@ -251,6 +263,7 @@ impl<'t> TextEdit<'t> {
id_source, id_source,
text_style, text_style,
text_color, text_color,
frame: _,
multiline, multiline,
enabled, enabled,
desired_width, desired_width,