Make Button sizing strategy adaptive
This commit is contained in:
parent
fa53d9adbb
commit
03c901d49b
3 changed files with 20 additions and 13 deletions
|
|
@ -42,6 +42,18 @@ impl Length {
|
||||||
pub fn is_fill(&self) -> bool {
|
pub fn is_fill(&self) -> bool {
|
||||||
self.fill_factor() != 0
|
self.fill_factor() != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the "fluid" variant of the [`Length`].
|
||||||
|
///
|
||||||
|
/// Specifically:
|
||||||
|
/// - [`Length::Shrink`] if [`Length::Shrink`] or [`Length::Fixed`].
|
||||||
|
/// - [`Length::Fill`] otherwise.
|
||||||
|
pub fn fluid(&self) -> Length {
|
||||||
|
match self {
|
||||||
|
Length::Fill | Length::FillPortion(_) => Length::Fill,
|
||||||
|
Length::Shrink | Length::Fixed(_) => Length::Shrink,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Pixels> for Length {
|
impl From<Pixels> for Length {
|
||||||
|
|
|
||||||
|
|
@ -71,11 +71,14 @@ where
|
||||||
{
|
{
|
||||||
/// Creates a new [`Button`] with the given content.
|
/// Creates a new [`Button`] with the given content.
|
||||||
pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self {
|
pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self {
|
||||||
|
let content = content.into();
|
||||||
|
let size = content.as_widget().size_hint();
|
||||||
|
|
||||||
Button {
|
Button {
|
||||||
content: content.into(),
|
content,
|
||||||
on_press: None,
|
on_press: None,
|
||||||
width: Length::Shrink,
|
width: size.width.fluid(),
|
||||||
height: Length::Shrink,
|
height: size.height.fluid(),
|
||||||
padding: Padding::new(5.0),
|
padding: Padding::new(5.0),
|
||||||
style: <Renderer::Theme as StyleSheet>::Style::default(),
|
style: <Renderer::Theme as StyleSheet>::Style::default(),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,16 +52,8 @@ where
|
||||||
Container {
|
Container {
|
||||||
id: None,
|
id: None,
|
||||||
padding: Padding::ZERO,
|
padding: Padding::ZERO,
|
||||||
width: if size.width.is_fill() {
|
width: size.width.fluid(),
|
||||||
Length::Fill
|
height: size.height.fluid(),
|
||||||
} else {
|
|
||||||
Length::Shrink
|
|
||||||
},
|
|
||||||
height: if size.height.is_fill() {
|
|
||||||
Length::Fill
|
|
||||||
} else {
|
|
||||||
Length::Shrink
|
|
||||||
},
|
|
||||||
max_width: f32::INFINITY,
|
max_width: f32::INFINITY,
|
||||||
max_height: f32::INFINITY,
|
max_height: f32::INFINITY,
|
||||||
horizontal_alignment: alignment::Horizontal::Left,
|
horizontal_alignment: alignment::Horizontal::Left,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue