Skip to content
This repository was archived by the owner on Jun 16, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ impl epi::App for ExampleApp {
.weekend_days(|date| date.day() % 2 == 0),
);
ui.end_row();

ui.label("Translation (French)");
ui.add(
DatePicker::new("translationfrench", &mut self.date)
.translation(&translation::TRANSLATION_FRENCH),
);
ui.end_row();
});
});
}
Expand Down
32 changes: 27 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
//!
//! [ex]: ./examples/simple.rs

pub mod translation;
use translation::{DateTranslation, TRANSLATION_ENGLISH};

use std::{fmt::Display, hash::Hash};

pub use chrono::{
Expand All @@ -39,7 +42,6 @@ use eframe::{
egui,
egui::{Area, Color32, DragValue, Frame, Id, Key, Order, Response, RichText, Ui, Widget},
};
use num_traits::FromPrimitive;

/// Default values of fields are:
/// - sunday_first: `false`
Expand All @@ -59,6 +61,7 @@ where
weekend_color: Color32,
weekend_func: fn(&Date<Tz>) -> bool,
highlight_weekend: bool,
translation: &'static DateTranslation,
}

impl<'a, Tz> DatePicker<'a, Tz>
Expand All @@ -77,6 +80,7 @@ where
weekend_color: Color32::from_rgb(196, 0, 0),
weekend_func: |date| date.weekday() == Weekday::Sat || date.weekday() == Weekday::Sun,
highlight_weekend: true,
translation: &TRANSLATION_ENGLISH,
}
}

Expand Down Expand Up @@ -120,11 +124,19 @@ where
}

/// Set function, which will decide if date is a weekend day or not.
#[must_use]
pub fn weekend_days(mut self, is_weekend: fn(&Date<Tz>) -> bool) -> Self {
self.weekend_func = is_weekend;
self
}

/// Set the translation of this date picker.
#[must_use]
pub fn translation(mut self, translation: &'static DateTranslation) -> Self {
self.translation = translation;
self
}

/// Draw names of week days as 7 columns of grid without calling `Ui::end_row`
fn show_grid_header(&mut self, ui: &mut Ui) {
let day_indexes = if self.sunday_first {
Expand All @@ -133,8 +145,13 @@ where
[0, 1, 2, 3, 4, 5, 6]
};
for i in day_indexes {
let b = Weekday::from_u8(i).unwrap();
ui.label(b.to_string());
let weekday_string = self
.translation
.weekday_short
.get(i)
.copied()
.unwrap_or("Unknown");
ui.label(weekday_string);
}
}

Expand Down Expand Up @@ -199,7 +216,7 @@ where
ui.horizontal(|ui| {
self.show_month_control(ui);
self.show_year_control(ui);
if ui.button("Today").clicked() {
if ui.button(self.translation.today).clicked() {
*self.date = Utc::now().with_timezone(&self.date.timezone()).date();
}
});
Expand Down Expand Up @@ -228,7 +245,12 @@ where
/// to current date.
fn show_month_control(&mut self, ui: &mut Ui) {
self.date_step_button(ui, "<", Duration::days(-30));
let month_string = chrono::Month::from_u32(self.date.month()).unwrap().name();
let month_string = self
.translation
.month
.get(self.date.month0() as usize)
.copied()
.unwrap_or("Unknown");
// TODO: When https://github.com/emilk/egui/pull/543 is merged try to change label to combo box.
ui.add(egui::Label::new(
RichText::new(format!("{: <9}", month_string)).text_style(egui::TextStyle::Monospace),
Expand Down
47 changes: 47 additions & 0 deletions src/translation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/// A translation for strings used by egui-datepicker
pub struct DateTranslation {
/// The full-lenght twelve month of a year (starting at January)
pub month: [&'static str; 12],
/// A shortened version of the 7 days of the week (starting at Sunday)
pub weekday_short: [&'static str; 7],
/// Translation of the "today" button, that set the date to the current date when clicked
pub today: &'static str,
}

pub const TRANSLATION_ENGLISH: DateTranslation = DateTranslation {
month: [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
],
weekday_short: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
today: "Today",
};

pub const TRANSLATION_FRENCH: DateTranslation = DateTranslation {
month: [
"janvier",
"février",
"mars",
"avril",
"mai",
"juin",
"juillet",
"août",
"septembre",
"octobre",
"novembre",
"décembre",
],
weekday_short: ["dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam."],
today: "Aujourd'hui",
};