Implement font method for ComboBox

This commit is contained in:
Héctor Ramón Jiménez 2020-07-08 07:04:20 +02:00
parent 1c12bad866
commit 69ac47f463
4 changed files with 39 additions and 37 deletions

View File

@ -43,8 +43,9 @@ where
cursor_position: Point,
options: &[T],
hovered_option: Option<usize>,
text_size: u16,
padding: u16,
text_size: u16,
font: Font,
style: &Style,
) -> Self::Output {
use std::f32;
@ -83,7 +84,7 @@ where
..bounds
},
size: f32::from(text_size),
font: Font::Default,
font,
color: if is_selected {
style.selected_text_color
} else {

View File

@ -29,8 +29,9 @@ where
bounds: Rectangle,
cursor_position: Point,
selected: Option<String>,
text_size: u16,
padding: u16,
text_size: u16,
font: Font,
style: &Box<dyn StyleSheet>,
) -> Self::Output {
let is_mouse_over = bounds.contains(cursor_position);
@ -69,7 +70,7 @@ where
let label = Primitive::Text {
content: label,
size: f32::from(text_size),
font: Font::Default,
font,
color: style.text_color,
bounds: Rectangle {
x: bounds.x + f32::from(padding),

View File

@ -11,6 +11,7 @@ pub struct Menu<'a, T, Message, Renderer: self::Renderer> {
width: u16,
padding: u16,
text_size: Option<u16>,
font: Renderer::Font,
style: <Renderer as self::Renderer>::Style,
}
@ -32,6 +33,7 @@ where
width: 0,
padding: 0,
text_size: None,
font: Default::default(),
style: Default::default(),
}
}
@ -51,6 +53,11 @@ where
self
}
pub fn font(mut self, font: Renderer::Font) -> Self {
self.font = font;
self
}
pub fn style(
mut self,
style: impl Into<<Renderer as self::Renderer>::Style>,
@ -115,21 +122,22 @@ where
on_selected,
width,
padding,
font,
text_size,
style,
} = menu;
let container = Container::new(
Scrollable::new(&mut state.scrollable).push(List::new(
let container =
Container::new(Scrollable::new(&mut state.scrollable).push(List {
options,
&mut state.hovered_option,
hovered_option: &mut state.hovered_option,
on_selected,
font,
text_size,
padding,
style.clone(),
)),
)
.padding(1);
style: style.clone(),
}))
.padding(1);
Self {
container,
@ -246,31 +254,12 @@ struct List<'a, T, Message, Renderer: self::Renderer> {
options: &'a [T],
hovered_option: &'a mut Option<usize>,
on_selected: &'a dyn Fn(T) -> Message,
text_size: Option<u16>,
padding: u16,
text_size: Option<u16>,
font: Renderer::Font,
style: <Renderer as self::Renderer>::Style,
}
impl<'a, T, Message, Renderer: self::Renderer> List<'a, T, Message, Renderer> {
pub fn new(
options: &'a [T],
hovered_option: &'a mut Option<usize>,
on_selected: &'a dyn Fn(T) -> Message,
text_size: Option<u16>,
padding: u16,
style: <Renderer as self::Renderer>::Style,
) -> Self {
List {
options,
hovered_option,
on_selected,
text_size,
padding,
style,
}
}
}
impl<'a, T, Message, Renderer: self::Renderer> Widget<'a, Message, Renderer>
for List<'a, T, Message, Renderer>
where
@ -370,8 +359,9 @@ where
cursor_position,
self.options,
*self.hovered_option,
self.text_size.unwrap_or(renderer.default_size()),
self.padding,
self.text_size.unwrap_or(renderer.default_size()),
self.font,
&self.style,
)
}
@ -396,8 +386,9 @@ pub trait Renderer:
cursor_position: Point,
options: &[T],
hovered_option: Option<usize>,
text_size: u16,
padding: u16,
text_size: u16,
font: Self::Font,
style: &<Self as Renderer>::Style,
) -> Self::Output;
}

View File

@ -17,6 +17,7 @@ where
width: Length,
padding: u16,
text_size: Option<u16>,
font: Renderer::Font,
style: <Renderer as self::Renderer>::Style,
}
@ -45,7 +46,8 @@ where
width: Length::Shrink,
text_size: None,
padding: Renderer::DEFAULT_PADDING,
style: <Renderer as self::Renderer>::Style::default(),
font: Default::default(),
style: Default::default(),
}
}
@ -70,6 +72,11 @@ where
self
}
pub fn font(mut self, font: Renderer::Font) -> Self {
self.font = font;
self
}
/// Sets the style of the [`ComboBox`].
///
/// [`ComboBox`]: struct.ComboBox.html
@ -200,8 +207,9 @@ where
layout.bounds(),
cursor_position,
self.selected.as_ref().map(ToString::to_string),
self.text_size.unwrap_or(renderer.default_size()),
self.padding,
self.text_size.unwrap_or(renderer.default_size()),
self.font,
&self.style,
)
}
@ -244,8 +252,9 @@ pub trait Renderer: text::Renderer + menu::Renderer {
bounds: Rectangle,
cursor_position: Point,
selected: Option<String>,
text_size: u16,
padding: u16,
text_size: u16,
font: Self::Font,
style: &<Self as Renderer>::Style,
) -> Self::Output;
}