Skip to content

Commit

Permalink
Merge pull request #216 from Strosel/main
Browse files Browse the repository at this point in the history
Add font size to date picker
  • Loading branch information
genusistimelord authored Mar 4, 2024
2 parents 36a9b5d + 0033058 commit 1c4765d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 72 deletions.
21 changes: 8 additions & 13 deletions src/core/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,10 @@ const fn num_days_of_month(year: i32, month: u32) -> u32 {
}
}

/// Gets the string representation of the year of the given date.
#[must_use]
pub fn year_as_string(date: NaiveDate) -> String {
date.format("%Y").to_string()
}

/// Gets the string representation of the month of the given date.
/// Gets the string representation of the date of the given month and date.
#[must_use]
pub fn month_as_string(date: NaiveDate) -> String {
date.format("%B").to_string()
pub fn date_as_string(date: NaiveDate) -> String {
date.format("%Y %B").to_string()
}

/// Gets the length of the longest month name.
Expand All @@ -237,8 +229,11 @@ pub static MAX_MONTH_STR_LEN: Lazy<usize> = Lazy::new(|| {

let max = months
.iter()
.map(|m| month_as_string(*m))
.map(|s| s.len())
.map(|m| {
let d = date_as_string(*m);
let (_, s) = d.split_once(' ').expect("Date must contain space");
s.len()
})
.max()
.expect("There should be a maximum element");

Expand Down
13 changes: 13 additions & 0 deletions src/native/date_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use iced::{
advanced::{
layout::{Limits, Node},
renderer,
text::Renderer as _,
widget::{
self,
tree::{Tag, Tree},
Expand All @@ -21,6 +22,7 @@ use iced::{
Element,
Event,
Length,
Pixels,
Point,
Rectangle,
Renderer, // the actual type
Expand Down Expand Up @@ -78,6 +80,8 @@ where
/// The buttons of the overlay.
overlay_state: Element<'a, Message, Theme, Renderer>,
//button_style: <Renderer as button::Renderer>::Style, // clone not satisfied
/// The font and icon size of the [`DatePickerOverlay`] or `None` for the default
font_size: Option<Pixels>,
}

impl<'a, Message, Theme> DatePicker<'a, Message, Theme>
Expand Down Expand Up @@ -120,6 +124,7 @@ where
style: <Theme as StyleSheet>::Style::default(),
overlay_state: DatePickerOverlayButtons::default().into(),
//button_style: <Renderer as button::Renderer>::Style::default(),
font_size: None,
}
}

Expand All @@ -130,6 +135,13 @@ where
//self.button_style = style.into();
self
}

/// Sets the font and icon size of the [`DatePicker`].
#[must_use]
pub fn font_size<P: Into<Pixels>>(mut self, size: P) -> Self {
self.font_size = Some(size.into());
self
}
}

/// The state of the [`DatePicker`] / [`DatePickerOverlay`].
Expand Down Expand Up @@ -286,6 +298,7 @@ where
position,
self.style.clone(),
&mut state.children[1],
self.font_size.unwrap_or_else(|| renderer.default_size()),
)
.overlay(),
)
Expand Down
Loading

0 comments on commit 1c4765d

Please sign in to comment.