Skip to content

Commit

Permalink
Replaced time-rs with chrono. Much better user experience with format…
Browse files Browse the repository at this point in the history
…ting.
  • Loading branch information
abuteler committed Sep 8, 2024
1 parent e9a5dec commit 4047a0e
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 101 deletions.
96 changes: 44 additions & 52 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ winit = { version = "0.30", default-features = false }
image = { version = "0.25", default-features = false }
## This greatly improves WGPU's performance due to its heavy use of trace! calls
log = { version = "*", features = ["max_level_debug", "release_max_level_warn"] }
time = { version = "0.3.34", features = ["local-offset"] }
chrono = { version = "0.4.38" }
# for app persistence:
serde = { version = "1", features = ["derive"] }

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "timehold".into(),
title: "Timehold".into(),
name: Some("timehold".into()),
// resolution: WindowResolution::new(850., 450.).with_scale_factor_override(1.0),
resolution: WindowResolution::new(600., 240.).with_scale_factor_override(1.0),
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/timeline/systems/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub fn spawn_header_contents(cmd: &mut Commands, fonts: &Res<Fonts>) -> Entity {

pub fn refresh_heading(
mut q: Query<&mut Text, With<CurrentTimeText>>,
chronos: Res<ChronoSphere>
chrono: Res<ChronoSphere>
) {
let mut text = q.single_mut();
text.sections[0].value = format!("{:?} x {:?}:{:?} {:?}", chronos.weekday, chronos.hh, chronos.mm, chronos.now);
text.sections[0].value = format!("{} | {}:{} ¬ {:?}", chrono.weekday(), chrono.formatted_hh(), chrono.formatted_mm(), chrono.now);
}
4 changes: 2 additions & 2 deletions src/plugins/timeline/systems/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::topbar::spawn_topbar_contents;
use super::header::spawn_header_contents;
use super::timeline::spawn_timeline_body_contents;

pub fn spawn_ui(mut cmd: Commands, asset_server: Res<AssetServer>, fonts: Res<Fonts>, icons: Res<Icons>, chronos: Res<ChronoSphere>) {
pub fn spawn_ui(mut cmd: Commands, asset_server: Res<AssetServer>, fonts: Res<Fonts>, icons: Res<Icons>, chrono: Res<ChronoSphere>) {
// App container
let all_father = cmd.spawn(create_app_grid_bundle()).id();
// Col 1 row 1
Expand All @@ -28,7 +28,7 @@ pub fn spawn_ui(mut cmd: Commands, asset_server: Res<AssetServer>, fonts: Res<Fo
cmd.entity(timeline_header).push_children(&[header_contents]);
// Col 1 row 4
let timeline_body = cmd.spawn(create_timeline_body_grid_area()).id();
let timeline_body_contents = spawn_timeline_body_contents(&mut cmd, asset_server, &fonts, chronos); // todo: decouple system
let timeline_body_contents = spawn_timeline_body_contents(&mut cmd, asset_server, &fonts, chrono); // todo: decouple system
cmd.entity(timeline_body).push_children(&[timeline_body_contents]);

// Col 2 row 1-4
Expand Down
19 changes: 10 additions & 9 deletions src/plugins/timeline/systems/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::timeline_bundles::{
DIAL_WIDTH_PX,
};

pub fn spawn_timeline_body_contents(cmd: &mut Commands, asset_server: Res<AssetServer>, fonts: &Res<Fonts>, chronos: Res<ChronoSphere>) -> Entity {
pub fn spawn_timeline_body_contents(cmd: &mut Commands, asset_server: Res<AssetServer>, fonts: &Res<Fonts>, chrono: Res<ChronoSphere>) -> Entity {
// Spawn a flex container to insert in the grid area, and hold the timeline elements
let body_container = cmd.spawn(create_flex_container_col()).id();

Expand All @@ -37,7 +37,7 @@ pub fn spawn_timeline_body_contents(cmd: &mut Commands, asset_server: Res<AssetS
let background_img = cmd.spawn(background_img_bundle).id();

// START Hour boxes
info!("time is {}", "potential".to_string());
info!("Time is {}", "potential".to_string());
let hours_container = cmd.spawn(create_hours_row_container()).id();
for hour in Hour::VALUES {
let hour_outer = cmd.spawn(create_hour_outer()).id();
Expand All @@ -53,30 +53,31 @@ pub fn spawn_timeline_body_contents(cmd: &mut Commands, asset_server: Res<AssetS
cmd.entity(hours_container).push_children(&[hour_outer]);
}
// Add dial
let position = calc_dial_horizontal_position(chronos.get_chronosphere_hh(), chronos.get_chronosphere_mm());
let position = calc_dial_horizontal_position(chrono.hour(), chrono.minutes());
let dial = cmd.spawn((create_dial(position), DialMarker)).id();

cmd.entity(body_container).push_children(&[background_img, hours_container, dial]);
body_container
}

pub fn refresh_timeline(
chronos: Res<ChronoSphere>,
chrono: Res<ChronoSphere>,
mut dial: Query<&mut Style, With<DialMarker>>,
// mut cmd: Commands,
hours: Query<(Entity, &Hour)>,
) {
// Current time
let new_hh = chronos.get_chronosphere_hh();
let new_mm = chronos.get_chronosphere_mm();
let new_hh = chrono.hour();
let new_mm = chrono.minutes();
let mut style = dial.single_mut();
style.left = Val::Px(calc_dial_horizontal_position(new_hh, new_mm));

// for hour in &hours {
for (_ent, hour) in hours.iter() {
if new_hh.to_string().eq(&hour.to_string()) {
let formatted_new_hh = chrono.formatted_hh();
if formatted_new_hh.eq(&hour.to_string()) {
// cmd.entity(hour).insert(Active);
info!("Now is {}:{}", new_hh.to_string(), new_mm.to_string());
// info!("Now is {}:{}", formatted_new_hh, chrono.formatted_mm());
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/plugins/timeline/systems/topbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub fn spawn_topbar_contents(cmd: &mut Commands, fonts: &Res<Fonts>, icons: &Res

// Right container: icon buttons
let flex_container_right = cmd.spawn(create_flex_container_right()).id();


cmd.entity(flex_container_left).push_children(&[app_icon, app_title]);
cmd.entity(flex_container_right).with_children(|child_builder| {
Expand Down
31 changes: 16 additions & 15 deletions src/resources/chronosphere.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@

use bevy::prelude::*;
use chrono::prelude::*;
use chrono::{DateTime, Local};

use time::{OffsetDateTime, Weekday};
use crate::systems::get_local_now;

#[derive(Resource)]
pub struct ChronoSphere {
pub now: OffsetDateTime,
pub weekday: Weekday,
pub hh: u8,
pub mm: u8,
pub now: DateTime<Local>,
}

impl ChronoSphere {
pub fn new() -> Self {
let now = get_local_now();
let weekday = now.weekday();
Self {
now,
weekday,
hh: now.time().hour(),
mm: now.time().minute(),
}
}
pub fn get_chronosphere_hh(&self) -> f32 {
self.hh as f32
pub fn hour(&self) -> f32 {
self.now.hour() as f32
}
pub fn get_chronosphere_mm(&self) -> f32 {
self.mm as f32
pub fn minutes(&self) -> f32 {
self.now.minute() as f32
}
pub fn get_chronosphere_weekday(&self) -> Weekday {
self.weekday
// For supported formats read: https://docs.rs/chrono/latest/chrono/format/strftime/index.html
pub fn formatted_hh(&self) -> String {
format!("{}", self.now.format("%H"))
}
pub fn formatted_mm(&self) -> String {
format!("{}", self.now.format("%M"))
}
pub fn weekday(&self) -> String {
format!("{}", self.now.format("%A"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/resources/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ impl FromWorld for Icons {
fort,
}
}
}
}
21 changes: 5 additions & 16 deletions src/systems/chronosphere.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
use bevy::prelude::*;
use time::{OffsetDateTime, UtcOffset};
use chrono::{DateTime, Local};

use crate::resources::ChronoSphere;

pub fn get_local_now() -> OffsetDateTime {
match OffsetDateTime::now_local() {
Ok(val) => val,
Err(_) => OffsetDateTime::now_utc().to_offset(
UtcOffset::from_hms(-3, 0, 0).expect(
"IndeterminateOffset for `now_local()`, plus manual setting of offset did not work."
)
),
}
pub fn get_local_now() -> DateTime<Local> {
Local::now()
}

pub fn update_chronosphere(mut chronos: ResMut<ChronoSphere>) {
let now = get_local_now();
chronos.now = now;
chronos.weekday = now.weekday();
chronos.hh = now.time().hour();
chronos.mm = now.time().minute();
pub fn update_chronosphere(mut chrono: ResMut<ChronoSphere>) {
chrono.now = get_local_now();
}
1 change: 0 additions & 1 deletion src/systems/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ pub fn set_window_icon(
pub fn toggle_window_decorations(mut window: Query<&mut Window>) {
window.single_mut().decorations = !window.single_mut().decorations;
}

0 comments on commit 4047a0e

Please sign in to comment.