use std::env; use chrono::{DateTime, Datelike, Local, Locale}; use color_eyre::Result; use gtk::prelude::*; use gtk::{Align, Button, Calendar, Label, Orientation}; use serde::Deserialize; use tokio::sync::mpsc; use tokio::time::sleep; use crate::channels::{AsyncSenderExt, BroadcastReceiverExt}; use crate::config::{CommonConfig, LayoutConfig}; use crate::gtk_helpers::IronbarGtkExt; use crate::modules::{ Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, PopupButton, WidgetContext, }; use crate::{module_impl, spawn}; #[derive(Debug, Deserialize, Clone)] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct ClockModule { /// The format string to use for the date/time shown on the bar. /// Pango markup is supported. /// /// Detail on available tokens can be found here: /// /// /// **Default**: `%d/%m/%Y %H:%M` #[serde(default = "default_format")] format: String, /// The format string to use for the date/time shown in the popup header. /// Pango markup is supported. /// /// Detail on available tokens can be found here: /// /// /// **Default**: `%H:%M:%S` #[serde(default = "default_popup_format")] format_popup: String, /// The locale to use when formatting dates. /// /// Note this will not control the calendar - /// for that you must set `LC_TIME`. /// /// **Valid options**: See [here](https://docs.rs/pure-rust-locales/0.8.1/pure_rust_locales/enum.Locale.html#variants) ///
/// **Default**: `$LC_TIME` or `$LANG` or `'POSIX'` #[serde(default = "default_locale")] locale: String, /// See [layout options](module-level-options#layout) #[serde(default, flatten)] layout: LayoutConfig, /// See [common options](module-level-options#common-options). #[serde(flatten)] pub common: Option, } impl Default for ClockModule { fn default() -> Self { ClockModule { format: default_format(), format_popup: default_popup_format(), locale: default_locale(), layout: LayoutConfig::default(), common: Some(CommonConfig::default()), } } } fn default_format() -> String { String::from("%d/%m/%Y %H:%M") } fn default_popup_format() -> String { String::from("%H:%M:%S") } fn default_locale() -> String { env::var("LC_TIME") .or_else(|_| env::var("LANG")) .map_or_else(|_| "POSIX".to_string(), strip_tail) } fn strip_tail(string: String) -> String { string .split_once('.') .map(|(head, _)| head.to_string()) .unwrap_or(string) } impl Module