[refactor] simplify Widget trait return type to InteractInfo

This commit is contained in:
Emil Ernerfeldt 2020-05-23 12:43:08 +02:00
parent 5c966bdc76
commit c22156cd0f
5 changed files with 32 additions and 20 deletions

View file

@ -110,6 +110,17 @@ impl GuiResponse {
} }
} }
impl Into<InteractInfo> for GuiResponse {
fn into(self) -> InteractInfo {
InteractInfo {
hovered: self.hovered,
clicked: self.clicked,
active: self.active,
rect: self.rect,
}
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/// What sort of interaction is a widget sensitive to? /// What sort of interaction is a widget sensitive to?

View file

@ -490,7 +490,8 @@ impl Ui {
// Addding Widgets // Addding Widgets
pub fn add(&mut self, widget: impl Widget) -> GuiResponse { pub fn add(&mut self, widget: impl Widget) -> GuiResponse {
widget.ui(self) let interact = widget.ui(self);
self.response(interact)
} }
// Convenience functions: // Convenience functions:

View file

@ -1,6 +1,6 @@
#![allow(clippy::new_without_default)] #![allow(clippy::new_without_default)]
use crate::{layout::Direction, GuiResponse, *}; use crate::{layout::Direction, *};
mod slider; mod slider;
pub mod text_edit; pub mod text_edit;
@ -11,7 +11,7 @@ pub use {paint::*, slider::*, text_edit::*};
/// Anything implementing Widget can be added to a Ui with `Ui::add` /// Anything implementing Widget can be added to a Ui with `Ui::add`
pub trait Widget { pub trait Widget {
fn ui(self, ui: &mut Ui) -> GuiResponse; fn ui(self, ui: &mut Ui) -> InteractInfo;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -106,11 +106,11 @@ macro_rules! label {
} }
impl Widget for Label { impl Widget for Label {
fn ui(self, ui: &mut Ui) -> GuiResponse { fn ui(self, ui: &mut Ui) -> InteractInfo {
let galley = self.layout(ui); let galley = self.layout(ui);
let rect = ui.allocate_space(galley.size); let rect = ui.allocate_space(galley.size);
self.paint_galley(ui, rect.min, galley); self.paint_galley(ui, rect.min, galley);
ui.response(ui.interact_hover(rect)) ui.interact_hover(rect)
} }
} }
@ -150,7 +150,7 @@ impl Hyperlink {
} }
impl Widget for Hyperlink { impl Widget for Hyperlink {
fn ui(self, ui: &mut Ui) -> GuiResponse { fn ui(self, ui: &mut Ui) -> InteractInfo {
let Hyperlink { url, text } = self; let Hyperlink { url, text } = self;
let color = color::LIGHT_BLUE; let color = color::LIGHT_BLUE;
@ -185,7 +185,7 @@ impl Widget for Hyperlink {
ui.add_galley(interact.rect.min, galley, text_style, Some(color)); ui.add_galley(interact.rect.min, galley, text_style, Some(color));
ui.response(interact) interact
} }
} }
@ -226,7 +226,7 @@ impl Button {
} }
impl Widget for Button { impl Widget for Button {
fn ui(self, ui: &mut Ui) -> GuiResponse { fn ui(self, ui: &mut Ui) -> InteractInfo {
let Button { let Button {
text, text,
text_color, text_color,
@ -253,7 +253,7 @@ impl Widget for Button {
let stroke_color = ui.style().interact(&interact).stroke_color; let stroke_color = ui.style().interact(&interact).stroke_color;
let text_color = text_color.unwrap_or(stroke_color); let text_color = text_color.unwrap_or(stroke_color);
ui.add_galley(text_cursor, galley, text_style, Some(text_color)); ui.add_galley(text_cursor, galley, text_style, Some(text_color));
ui.response(interact) interact
} }
} }
@ -282,7 +282,7 @@ impl<'a> Checkbox<'a> {
} }
impl<'a> Widget for Checkbox<'a> { impl<'a> Widget for Checkbox<'a> {
fn ui(self, ui: &mut Ui) -> GuiResponse { fn ui(self, ui: &mut Ui) -> InteractInfo {
let Checkbox { let Checkbox {
checked, checked,
text, text,
@ -328,7 +328,7 @@ impl<'a> Widget for Checkbox<'a> {
let text_color = text_color.unwrap_or(stroke_color); let text_color = text_color.unwrap_or(stroke_color);
ui.add_galley(text_cursor, galley, text_style, Some(text_color)); ui.add_galley(text_cursor, galley, text_style, Some(text_color));
ui.response(interact) interact
} }
} }
@ -361,7 +361,7 @@ pub fn radio(checked: bool, text: impl Into<String>) -> RadioButton {
} }
impl Widget for RadioButton { impl Widget for RadioButton {
fn ui(self, ui: &mut Ui) -> GuiResponse { fn ui(self, ui: &mut Ui) -> InteractInfo {
let RadioButton { let RadioButton {
checked, checked,
text, text,
@ -403,7 +403,7 @@ impl Widget for RadioButton {
let text_color = text_color.unwrap_or(stroke_color); let text_color = text_color.unwrap_or(stroke_color);
ui.add_galley(text_cursor, galley, text_style, Some(text_color)); ui.add_galley(text_cursor, galley, text_style, Some(text_color));
ui.response(interact) interact
} }
} }
@ -449,7 +449,7 @@ impl Separator {
} }
impl Widget for Separator { impl Widget for Separator {
fn ui(self, ui: &mut Ui) -> GuiResponse { fn ui(self, ui: &mut Ui) -> InteractInfo {
let Separator { let Separator {
line_width, line_width,
min_spacing, min_spacing,
@ -488,6 +488,6 @@ impl Widget for Separator {
color: color, color: color,
width: line_width, width: line_width,
}); });
ui.response(ui.interact_hover(rect)) ui.interact_hover(rect)
} }
} }

View file

@ -99,7 +99,7 @@ impl<'a> Slider<'a> {
} }
impl<'a> Widget for Slider<'a> { impl<'a> Widget for Slider<'a> {
fn ui(mut self, ui: &mut Ui) -> GuiResponse { fn ui(mut self, ui: &mut Ui) -> InteractInfo {
let text_style = TextStyle::Button; let text_style = TextStyle::Button;
let font = &ui.fonts()[text_style]; let font = &ui.fonts()[text_style];
@ -131,7 +131,7 @@ impl<'a> Widget for Slider<'a> {
ui.add(Label::new(full_text).multiline(false)); ui.add(Label::new(full_text).multiline(false));
}); });
slider_response slider_response.into()
}) })
} }
} else { } else {
@ -189,7 +189,7 @@ impl<'a> Widget for Slider<'a> {
}); });
} }
ui.response(interact) interact
} }
} }
} }

View file

@ -49,7 +49,7 @@ impl<'t> TextEdit<'t> {
} }
impl<'t> Widget for TextEdit<'t> { impl<'t> Widget for TextEdit<'t> {
fn ui(self, ui: &mut Ui) -> GuiResponse { fn ui(self, ui: &mut Ui) -> InteractInfo {
let TextEdit { let TextEdit {
text, text,
id, id,
@ -145,7 +145,7 @@ impl<'t> Widget for TextEdit<'t> {
ui.add_galley(interact.rect.min, galley, text_style, text_color); ui.add_galley(interact.rect.min, galley, text_style, text_color);
ui.memory().text_edit.insert(id, state); ui.memory().text_edit.insert(id, state);
ui.response(interact) interact
} }
} }