[MIRROR] https://codeberg.org/naomi/nanel

Configurable format for DateTime module

+53 -85
+2 -3
examples/default.ron
··· 22 22 ), 23 23 DateTime( 24 24 calendar: true, 25 - mm_dd_yyyy: false, 26 - military: true, 27 - seconds: false, 25 + upper_format: "%H:%M", // https://docs.rs/chrono/latest/chrono/format/strftime/index.html 26 + lower_format: "%d/%m/%Y", 28 27 ), 29 28 NotificationTray, 30 29 ViewDesktop(show_on_hover: true, thin: true)
+34 -75
src/modules.rs
··· 11 11 msg::{Message, NanelWindowType}, 12 12 panel::label, 13 13 styles::{solid, transparent}, 14 - widgets::{icon, small_icon, taskbar_button, taskbar_button_thin, taskbar_button_with_info}, 14 + widgets::{ 15 + button_with_position::ButtonInfo, icon, small_icon, taskbar_button, taskbar_button_thin, 16 + taskbar_button_with_info, 17 + }, 15 18 windows::{ 16 19 calendar_menu, directory_menu, notification_menu, quick_settings_menu, start_menu, 17 20 systray_menu, ··· 38 41 }, 39 42 DateTime { 40 43 calendar: bool, 41 - mm_dd_yyyy: bool, 42 - military: bool, 43 - seconds: bool, 44 + upper_format: String, 45 + lower_format: String, 44 46 }, 45 47 NotificationTray, 46 48 ViewDesktop { ··· 178 180 .into() 179 181 } 180 182 181 - fn get_time( 182 - time: DateTime<Local>, 183 - mm_dd_yyyy: &bool, 184 - military: &bool, 185 - seconds: &bool, 186 - ) -> (String, String) { 187 - let dd = time.day(); 188 - let dd = match dd { 189 - x if x <= 9 => format!("0{}", x), 190 - x => format!("{}", x), 191 - }; 192 - let mm = time.month(); 193 - let mm = match mm { 194 - x if x <= 9 => format!("0{}", x), 195 - x => format!("{}", x), 196 - }; 197 - let yyyy = time.year(); 198 - 199 - let date = match mm_dd_yyyy { 200 - true => format!(" {}/{}/{} ", mm, dd, yyyy), 201 - false => format!(" {}/{}/{} ", dd, mm, yyyy), 202 - }; 203 - 204 - let h = time.hour(); 205 - let h = match military { 206 - true => h, 207 - false => { 208 - if h > 12 { 209 - h - 12 210 - } else { 211 - h 212 - } 213 - } 214 - }; 215 - let h = match h { 216 - x if x <= 9 => format!("0{}", x), 217 - x => format!("{}", x), 218 - }; 219 - let m = time.minute(); 220 - let m = match m { 221 - x if x <= 9 => format!("0{}", x), 222 - x => format!("{}", x), 223 - }; 224 - let s = time.second(); 225 - let s = match s { 226 - x if x <= 9 => format!("0{}", x), 227 - x => format!("{}", x), 228 - }; 229 - 230 - let time = match seconds { 231 - true => format!(" {}:{}:{} ", h, m, s), 232 - false => format!(" {}:{} ", h, m), 233 - }; 234 - 235 - (date, time) 236 - } 237 - 238 183 pub fn date_time<'a>( 239 184 datetime: DateTime<Local>, 240 - mm_dd_yyyy: &bool, 241 - military: &bool, 242 - seconds: &bool, 185 + calendar: &bool, 186 + upper_format: &String, 187 + lower_format: &String, 243 188 vertical: &bool, 244 189 ) -> Element<'a, Message> { 245 - let (date, time) = get_time(datetime, mm_dd_yyyy, military, seconds); 246 - 247 190 taskbar_button_with_info( 248 191 match vertical { 249 192 true => Into::<Element<Message>>::into( 250 193 row![text("TODO")].clip(false).align_y(Alignment::Center), 251 194 ), 252 195 false => Into::<Element<Message>>::into( 253 - column![label(time.as_str()), label(date.as_str())] 254 - .clip(false) 255 - .align_x(Alignment::Center), 196 + column![ 197 + label( 198 + (String::from(" ") 199 + + datetime.format(upper_format.as_str()).to_string().as_str() 200 + + " ") 201 + .as_str() 202 + ), 203 + label( 204 + (String::from(" ") 205 + + datetime.format(lower_format.as_str()).to_string().as_str() 206 + + " ") 207 + .as_str() 208 + ), 209 + ] 210 + .clip(false) 211 + .align_x(Alignment::Center), 256 212 ), 257 213 }, 258 214 false, 259 215 vertical, 260 216 transparent, 261 217 ) 262 - .on_press_with(|info| Message::UserToggledWindow { 263 - x: info.position.x as i32, 264 - y: info.position.y as i32, 265 - viewport: (info.viewport.0 as i32, info.viewport.1 as i32), 266 - window_type: NanelWindowType::Calendar, 267 - container_id: calendar_menu::CALENDAR_MENU.clone(), 218 + .on_press_with_maybe(match calendar { 219 + true => Some(|info: ButtonInfo| Message::UserToggledWindow { 220 + x: info.position.x as i32, 221 + y: info.position.y as i32, 222 + viewport: (info.viewport.0 as i32, info.viewport.1 as i32), 223 + window_type: NanelWindowType::Calendar, 224 + container_id: calendar_menu::CALENDAR_MENU.clone(), 225 + }), 226 + false => None, 268 227 }) 269 228 .padding(1) 270 229 .into()
+6 -7
src/panel.rs
··· 142 142 &self.is_vertical(), 143 143 ), 144 144 Module::DateTime { 145 - calendar: _, 146 - mm_dd_yyyy, 147 - military, 148 - seconds, 145 + calendar, 146 + upper_format, 147 + lower_format, 149 148 } => date_time( 150 149 self.datetime, 151 - mm_dd_yyyy, 152 - military, 153 - seconds, 150 + calendar, 151 + upper_format, 152 + lower_format, 154 153 &self.is_vertical(), 155 154 ), 156 155 Module::ViewDesktop {
+11
src/widgets/button_with_position.rs
··· 101 101 self 102 102 } 103 103 104 + pub fn on_press_with_maybe( 105 + mut self, 106 + on_press: Option<impl Fn(ButtonInfo) -> Message + 'a>, 107 + ) -> Self { 108 + self.on_press = match on_press { 109 + Some(on_press) => Some(OnPress::Closure(Box::new(on_press))), 110 + None => None, 111 + }; 112 + self 113 + } 114 + 104 115 /// Sets whether the contents of the [`ButtonWithPosition`] should be clipped on 105 116 /// overflow. 106 117 pub fn clip(mut self, clip: bool) -> Self {