Compare commits

..

1 Commits

8 changed files with 642 additions and 1048 deletions

152
Cargo.lock generated
View File

@ -32,17 +32,6 @@ version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -102,45 +91,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "clap"
version = "3.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2dbdf4bdacb33466e854ce889eee8dfd5729abf7ccd7664d0a2d60cd384440b"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"lazy_static",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25320346e922cffe59c0bbc5410c8d8784509efb321488971081313cb1e1a33c"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "color_quant"
version = "1.1.0"
@ -269,37 +219,6 @@ dependencies = [
"weezl",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "indexmap"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "jpeg-decoder"
version = "0.2.6"
@ -448,12 +367,6 @@ version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]]
name = "os_str_bytes"
version = "6.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "029d8d0b2f198229de29dca79676f2738ff952edf3fde542eb8bf94d8c21b435"
[[package]]
name = "pico-args"
version = "0.4.2"
@ -478,30 +391,6 @@ dependencies = [
"miniz_oxide",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.36"
@ -673,12 +562,6 @@ dependencies = [
"wayland-protocols",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "suncalc"
version = "0.4.0"
@ -721,21 +604,6 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]]
name = "thiserror"
version = "1.0.31"
@ -865,12 +733,6 @@ dependencies = [
"xmlwriter",
]
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
@ -972,15 +834,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
@ -989,12 +842,10 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "wl-seasonal-hours-clock"
version = "1.1.0"
version = "0.1.0"
dependencies = [
"calloop",
"chrono",
"clap",
"rctree",
"resvg",
"serde",
"smithay-client-toolkit",
@ -1004,7 +855,6 @@ dependencies = [
"toml",
"usvg",
"xdg",
"xmlwriter",
]
[[package]]

View File

@ -1,6 +1,6 @@
[package]
name = "wl-seasonal-hours-clock"
version = "1.1.0"
version = "0.1.0"
edition = "2021"
[dependencies]
@ -15,6 +15,3 @@ toml = "0.5"
serde = { version = "1.0", features = ["derive"] }
xdg = "2.4"
calloop = "0.9"
xmlwriter = "0.1"
rctree = "0.4"
clap = { version = "3.1", features = ["derive"] }

View File

@ -4,7 +4,7 @@ This is a Rust implementation of cinnamons [Seasonal Hours Clock](https://git
## Configuration
Create a file `$XDG_CONFIG_HOME/seasonal-clock.toml` (where `$XDG_CONFIG_HOME` is `~/.config` on Linux systems) with the following content (or use/modify [example-config.toml](example-config.toml)):
Create a file `$XDG_CONFIG_HOME/seasonal-clock.toml` (where `$XDG_CONFIG_HOME` is `~/.config` on Linux systems) with the following content:
```
[seasonal-clock]
@ -16,18 +16,6 @@ longitude = 19.286
If you dont provide a configuration file, the day parts (day/night time and the golden/blue hours) wont be visible on the clock face.
## Command line arguments
### Just print the current hours name
You can pass `-n` or `--now` to the app to only print the current hours name.
This can be used in scripts, or in i3/sway panels, for example.
### Use a different config
Pass `-c FILE` or `--config FILE` to use `FILE` as the configuration file (instead of the default). Im not sure if it can be useful; i only did this to show off my Rust skills ^_^
## Features
### Seasonal hours!

View File

@ -1,4 +0,0 @@
[seasonal-clock]
# This is somewhere arond Hungary
latitude = 47.65
longitude = 19.28

View File

@ -1,116 +0,0 @@
use chrono::{
prelude::{Local, Utc},
TimeZone, Timelike,
};
use suncalc::SunTimes;
use crate::config::Config;
const HOUR_NAMES: [&str; 24] = [
"Candle", "Ice", "Comet", "Thimble", "Root", "Mist", "Sprout", "Rainbow", "Worm", "Bud",
"Blossom", "Ladybug", "Geese", "Dust", "Peach", "Fog", "Acorn", "Gourd", "Soup", "Crow",
"Mushroom", "Thunder", "Frost", "Lantern",
];
pub enum DayPart {
LocalNow,
UtcNow,
UtcNoon,
UtcMidnight,
UtcMorningGoldenEnd,
UtcEveningGoldenStart,
UtcSunrise,
UtcSunset,
UtcDawnStart,
UtcDuskEnd,
}
pub fn get_moon_phase() -> f32 {
let local_timestamp = Local::now();
let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis());
let moon_illumination = suncalc::moon_illumination(unixtime);
moon_illumination.phase as f32 * 28.0
}
pub fn get_seconds_since_midnight(config: &Option<Config>, which: DayPart) -> i32 {
let local_timestamp = Local::now();
let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis());
let sun_times: Option<SunTimes> = if config.is_some() {
Some(suncalc::get_times(
unixtime,
config.unwrap().latitude,
config.unwrap().longitude,
None,
))
} else {
None
};
let seconds = match which {
DayPart::LocalNow => local_timestamp.time().num_seconds_from_midnight(),
DayPart::UtcNow => local_timestamp
.with_timezone(&Utc)
.time()
.num_seconds_from_midnight(),
DayPart::UtcNoon => Utc
.timestamp_millis(sun_times.unwrap().solar_noon.0)
.time()
.num_seconds_from_midnight(),
DayPart::UtcMidnight => Utc
.timestamp_millis(sun_times.unwrap().nadir.0)
.time()
.num_seconds_from_midnight(),
DayPart::UtcMorningGoldenEnd => Utc
.timestamp_millis(sun_times.unwrap().golden_hour_end.0)
.time()
.num_seconds_from_midnight(),
DayPart::UtcEveningGoldenStart => Utc
.timestamp_millis(sun_times.unwrap().golden_hour.0)
.time()
.num_seconds_from_midnight(),
DayPart::UtcSunrise => Utc
.timestamp_millis(sun_times.unwrap().sunrise.0)
.time()
.num_seconds_from_midnight(),
DayPart::UtcSunset => Utc
.timestamp_millis(sun_times.unwrap().sunset.0)
.time()
.num_seconds_from_midnight(),
DayPart::UtcDawnStart => Utc
.timestamp_millis(sun_times.unwrap().dawn.0)
.time()
.num_seconds_from_midnight(),
DayPart::UtcDuskEnd => Utc
.timestamp_millis(sun_times.unwrap().dusk.0)
.time()
.num_seconds_from_midnight(),
};
seconds as i32
}
pub fn get_hms(config: &Option<Config>, which: DayPart) -> (u8, u8, u8) {
let mut seconds = get_seconds_since_midnight(config, which);
let hours = seconds / 3600;
seconds -= hours * 3600;
let minutes = seconds / 60;
seconds -= minutes * 60;
(hours as u8, minutes as u8, seconds as u8)
}
pub fn get_utc_offset() -> i32 {
Local::now().offset().local_minus_utc()
}
pub fn get_utc_hour_name(hour: usize) -> &'static str {
HOUR_NAMES[hour]
}
pub fn get_current_hour_name() -> &'static str {
let (utc_hour, _, _) = get_hms(&None, DayPart::UtcNow);
get_utc_hour_name(utc_hour as usize)
}

View File

@ -1,37 +0,0 @@
use std::fs;
use std::path::PathBuf;
use serde::Deserialize;
#[derive(Deserialize, Copy, Clone)]
pub struct Config {
pub latitude: f64,
pub longitude: f64,
}
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
struct CompleteConfig {
seasonal_clock: Config,
}
pub fn get_config(filename: Option<String>) -> Option<Config> {
let config_path = match filename {
Some(v) => PathBuf::from(v),
None => {
let xdg_dirs = xdg::BaseDirectories::new().unwrap();
xdg_dirs
.place_config_file("seasonal-clock.toml")
.expect("cannot create configuration directory")
}
};
let data: std::io::Result<String> = fs::read_to_string(config_path);
if let Ok(..) = data {
let complete_config: CompleteConfig = toml::from_str(&data.unwrap()).unwrap();
Some(complete_config.seasonal_clock)
} else {
eprintln!("Could not parse config file {:?}", data);
None
}
}

View File

@ -1,48 +1,638 @@
extern crate smithay_client_toolkit as sctk;
use calloop::{timer::Timer, EventLoop};
use clap::Parser;
use std::fmt;
use std::fs;
use calloop::{timer::{Timer, TimerHandle}, EventLoop};
use chrono::prelude::{Local, Utc};
use chrono::TimeZone;
use chrono::Timelike;
use sctk::reexports::client::protocol::{wl_shm, wl_surface};
use sctk::shm::AutoMemPool;
use sctk::window::{Event as WEvent, FallbackFrame};
use serde::Deserialize;
use svg::node::element::path::Data as PathData;
mod clock;
mod config;
mod svg_clock;
use clock::get_current_hour_name;
use config::{get_config, Config};
use svg_clock::{cache_hour_name_paths, gen_svg, svg_to_usvg};
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// Print the current hours name and exit
#[clap(short, long)]
now: bool,
/// Use FILE as the configuration file
#[clap(short, long, value_name = "FILE")]
config: Option<String>,
}
use svg::node::element::{
Circle, Definitions, Group, Line, Path, Rectangle, Style, Text, TextPath,
};
use svg::node::Text as TextNode;
use svg::Document;
sctk::default_environment!(SeasonalClock, desktop);
fn main() {
let args = Args::parse();
const HOUR_NAMES: [&str; 24] = [
"Candle", "Ice", "Comet", "Thimble", "Root", "Mist", "Sprout", "Rainbow", "Worm", "Bud",
"Blossom", "Ladybug", "Geese", "Dust", "Peach", "Fog", "Acorn", "Gourd", "Soup", "Crow",
"Mushroom", "Thunder", "Frost", "Lantern",
];
if args.now {
println!("{}", get_current_hour_name());
std::process::exit(0);
}
let config = get_config(args.config);
run_windowed(&config);
enum Season {
Spring,
Summer,
Autumn,
Winter,
}
fn run_windowed(config: &Option<Config>) {
impl fmt::Display for Season {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Season::Spring => "spring",
Season::Summer => "summer",
Season::Autumn => "autumn",
Season::Winter => "winter",
}
)
}
}
#[derive(Deserialize, Copy, Clone)]
struct Config {
latitude: f64,
longitude: f64,
}
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
struct CompleteConfig {
seasonal_clock: Config,
}
fn seconds_to_degrees(seconds: i32) -> f32 {
seconds as f32 * 360.0 / 86400.0
}
fn time_to_degrees(timestamp: i32, // should be time/timestamp
) -> f32 {
seconds_to_degrees(timestamp)
}
fn hour_name_path(image_width: u32, outer_r: f32, ring_width: f32) -> Path {
let radius = outer_r - ring_width / 2.0;
let delta_x = radius * (15.0_f32.to_radians() / 2.0).sin();
let delta_y = radius * (1.0 - (15.0_f32.to_radians() / 2.0).cos());
let x1 = (image_width as f32) / 2.0 - delta_x;
let y1 = ((image_width as f32) / 2.0 - radius) + delta_y;
let path_data = PathData::new().move_to((x1, y1)).elliptical_arc_by((
radius,
radius,
15,
0,
1,
2.0 * delta_x,
0,
));
Path::new().set("id", "hour-name-path").set("d", path_data)
}
fn hour_marker(
hour: i32,
is_current_hour: bool,
image_width: u32,
outer_r: f32,
ring_width: f32,
hour_name_font_size: f32,
utc_hour_font_size: f32,
) -> Group {
let season = match hour {
0..=5 => Season::Winter,
6..=11 => Season::Spring,
12..=17 => Season::Summer,
18..=23 => Season::Autumn,
_ => panic!("Hour out of range"),
};
let rotation = hour * 15;
let delta_x = outer_r * (15f32.to_radians() / 2.0).sin();
let delta_y = outer_r * (1.0 - (15f32.to_radians() / 2.0).cos());
let s_delta_x = 0.0 - ring_width * (15f32.to_radians() / 2.0).sin();
let s_delta_y = ring_width * (15f32.to_radians() / 2.0).cos();
let i_delta_x = -2.0 * (outer_r - ring_width) * (15f32.to_radians() / 2.0).sin();
let x1 = image_width as f32 / 2.0 - delta_x;
let y1 = (image_width as f32 / 2.0 - outer_r) + delta_y;
let utc_hour_y = image_width as f32 / 2.0 - outer_r + ring_width + utc_hour_font_size;
let path_data = PathData::new()
.move_to((x1, y1))
.elliptical_arc_by((outer_r, outer_r, 15, 0, 1, 2.0 * delta_x, 0))
.line_by((s_delta_x, s_delta_y))
.elliptical_arc_by((
outer_r - ring_width,
outer_r - ring_width,
15,
0,
0,
i_delta_x,
0,
))
.close();
let path = Path::new().set("d", path_data);
let hour_name_text_path = TextPath::new()
.set("xlink:href", "#hour-name-path")
.set("startOffset", "50%")
.add(TextNode::new(HOUR_NAMES[hour as usize]));
let hour_name_text = Text::new()
.set("text-anchor", "middle")
.set("dominant-baseline", "mathematical")
.set("font-size", hour_name_font_size)
.add(hour_name_text_path);
let utc_hour_text = Text::new()
.set("class", "utc")
.set(
"transform",
format!("rotate(-7.5, {}, {})", image_width / 2, image_width / 2),
)
.set("x", image_width / 2)
.set("y", utc_hour_y)
.set("text-anchor", "middle")
.set("dominant-baseline", "mathematical")
.set("font-size", utc_hour_font_size)
.add(TextNode::new(format!("U {:02}", hour)));
Group::new()
.set(
"class",
format!(
"hour {season}{}",
if is_current_hour { " active" } else { "" }
),
)
.set(
"transform",
format!(
"rotate({}, {}, {})",
rotation as f32 - 172.5,
image_width / 2,
image_width / 2
),
)
.add(path)
.add(hour_name_text)
.add(utc_hour_text)
}
fn get_range_path(
image_width: u32,
radius: f32,
range_name: &str,
start_time: i32,
end_time: i32,
) -> Path {
let start_deg = time_to_degrees(start_time);
let end_deg = time_to_degrees(end_time);
let deg_diff = end_deg - start_deg;
let start_delta_x = radius * start_deg.to_radians().sin();
let start_delta_y = radius * (1.0 - start_deg.to_radians().cos());
let end_delta_x = radius * end_deg.to_radians().sin();
let end_delta_y = radius * (1.0 - end_deg.to_radians().cos());
let path_data = PathData::new()
.move_to((image_width / 2, image_width / 2))
.line_to((
image_width as f32 / 2.0 - start_delta_x,
image_width as f32 / 2.0 + radius - start_delta_y,
))
.elliptical_arc_to((
radius,
radius,
deg_diff,
((start_deg < end_deg) ^ (deg_diff.abs() >= 180.0)) as u8,
0,
image_width as f32 / 2.0 - end_delta_x,
image_width as f32 / 2.0 + radius - end_delta_y,
))
.close();
Path::new().set("class", range_name).set("d", path_data)
}
fn get_moon_path(image_width: u32, radius: f32, moon_phase: f64) -> Path {
let handle_x_pos = radius as f64 * 1.34;
let handle_y_pos = radius * 0.88;
let min_x = image_width as f64 / 2.0 - handle_x_pos;
let max_x = min_x + 2.0 * handle_x_pos;
let top_y = image_width as f32 / 2.0 - handle_y_pos;
let bottom_y = image_width as f32 / 2.0 + handle_y_pos;
let h1_x: f64;
let h2_x: f64;
if moon_phase < 14.0 {
h1_x = min_x + 2.0 * handle_x_pos * (1.0 - moon_phase / 14.0);
h2_x = max_x;
} else {
h1_x = min_x;
h2_x = max_x + 2.0 * handle_x_pos * (1.0 - moon_phase / 14.0)
}
let path_data = PathData::new()
.move_to((image_width as f32 / 2.0, image_width as f32 / 2.0 - radius))
.cubic_curve_to((
h1_x,
top_y,
h1_x,
bottom_y,
image_width as f32 / 2.0,
image_width as f32 / 2.0 + radius,
))
.cubic_curve_to((
h2_x,
bottom_y,
h2_x,
top_y,
image_width / 2,
image_width as f32 / 2.0 - radius,
));
Path::new().set("class", "moon").set("d", path_data)
}
fn gen_svg(config: &Option<Config>) -> Document {
let local_timestamp = Local::now();
let utc_hour = local_timestamp.with_timezone(&Utc).time().hour();
let local_hour = local_timestamp.time().hour();
let local_minute = local_timestamp.time().minute();
let local_second = local_timestamp.time().second();
let utc_offset = local_timestamp.offset().local_minus_utc();
let local_time = local_timestamp.time().num_seconds_from_midnight() as i32;
let utc_rotation = seconds_to_degrees(utc_offset);
// TODO: These should be calculated instead of hardcoded
let image_width = 700u32;
// Calculate the Moon phase
let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis());
let moon_illumination = suncalc::moon_illumination(unixtime);
let moon_radius = image_width as f32 * 0.071428571;
let moon_phase = moon_illumination.phase * 28.0;
let local_hour_font_size = image_width as f32 * 0.02357;
let hour_name_font_size = image_width as f32 * 0.019109;
let utc_hour_font_size = image_width as f32 * 0.021462;
let outer_r = (image_width as f32) / 2.0 - 3.0 * hour_name_font_size;
let ring_width = hour_name_font_size * 3.0;
let sun_radius = image_width as f32 * 0.0142871;
let marker_radius = outer_r - ring_width - 2.0 * utc_hour_font_size;
let border = Rectangle::new()
.set("x", 0i32)
.set("y", 0i32)
.set("width", 700i32)
.set("height", 700i32)
.set("id", "border");
let stylesheet = Style::new(
"\
#border {stroke: none; fill: rgb(19, 17, 30); }
.hour path {stroke: rgb(0, 0, 0); stroke-width: 2px;}
.hour text {stroke: none; fill: rgb(238, 187, 85);}
.hour text.utc {stroke: none; fill: rgb(91, 68, 38);}
.winter path {fill: rgb(70, 62, 108);}
.active.winter path {fill: rgb(100, 92, 138);}
.spring path {fill: rgb(55, 87, 55);}
.active.spring path {fill: rgb(85, 117, 85);}
.summer path {fill: rgb(113, 92, 43);}
.active.summer path {fill: rgb(143, 122, 73);}
.autumn path {fill: rgb(108, 68, 44);}
.active.autumn path {fill: rgb(138, 98, 74);}
.local-hour {stroke: none; fill: rgb(238, 187, 85);}
.night-time {stroke: none; fill: rgb(19, 17, 30);}
.blue-hour {stroke: none; fill: rgb(9, 1, 119);}
.golden-hour {stroke: none; fill: rgb(170, 132, 44);}
.day-time {stroke: none; fill: rgb(125, 197, 240);}
.marker {stroke: rgb(19, 17, 30); stroke-width: 2px; fill: none;}
.moon-background {stroke: rgb(170, 170, 170); stroke-width: 2px; fill: rgb(19, 17, 30);}
.moon {stroke: none; fill: rgb(170, 170, 170);}
.sun {stroke: none; fill: rgb(238, 187, 85);}
.mid-marker {stroke: red;}
.dial {stroke-width: 2px; stroke: rgb(238, 187, 85);}
#current-hour rect {stroke: none; fill: rgba(255, 255, 255, 0.5);}
#current-hour-name {font-weight: bold;}",
);
let definitions = Definitions::new().add(hour_name_path(image_width, outer_r, ring_width));
let mut local_clock = Group::new().set("id", "local-clock");
for hour in 0i32..24 {
let hour_str = match hour {
0 => "Midnight".to_string(),
12 => "Noon".to_string(),
_ => hour.to_string(),
};
let rotation = hour * 15;
let hour_name = TextNode::new(hour_str);
let hour_node = Text::new()
.set("class", "local-hour")
.set("transform", format!("rotate({}, 350, 350)", 180 + rotation))
.set("x", (image_width as f32) / 2.0)
.set(
"y",
(image_width as f32) / 2.0 - outer_r - local_hour_font_size / 2.0,
)
.set("text-anchor", "middle")
.set("font-size", local_hour_font_size as f32)
.add(hour_name);
local_clock = local_clock.add(hour_node);
}
let mut seasonal_clock = Group::new().set(
"transform",
format!(
"rotate({}, {}, {})",
utc_rotation,
image_width / 2,
image_width / 2
),
);
for hour in 0i32..24 {
seasonal_clock = seasonal_clock.add(hour_marker(
hour,
hour == utc_hour as i32,
image_width,
outer_r,
ring_width,
hour_name_font_size,
utc_hour_font_size,
));
}
let daytime_circle = Circle::new()
.set("class", "day-time")
.set("cx", image_width / 2)
.set("cy", image_width / 2)
.set("r", marker_radius);
let day_parts_group: Option<Group> = if config.is_some() {
let sun_times = suncalc::get_times(
unixtime,
config.unwrap().latitude,
config.unwrap().longitude,
None,
);
let noon = Utc
.timestamp_millis(sun_times.solar_noon.0)
.time()
.num_seconds_from_midnight() as i32;
let midnight = Utc
.timestamp_millis(sun_times.nadir.0)
.time()
.num_seconds_from_midnight() as i32;
let morning_golden_end = Utc
.timestamp_millis(sun_times.golden_hour_end.0)
.time()
.num_seconds_from_midnight() as i32;
let evening_golden_start = Utc
.timestamp_millis(sun_times.golden_hour.0)
.time()
.num_seconds_from_midnight() as i32;
let sunrise = Utc
.timestamp_millis(sun_times.sunrise.0)
.time()
.num_seconds_from_midnight() as i32;
let sunset = Utc
.timestamp_millis(sun_times.sunset.0)
.time()
.num_seconds_from_midnight() as i32;
let dawn = Utc
.timestamp_millis(sun_times.dawn.0)
.time()
.num_seconds_from_midnight() as i32;
let dusk = Utc
.timestamp_millis(sun_times.dusk.0)
.time()
.num_seconds_from_midnight() as i32;
let golden_hour_path = get_range_path(
image_width,
marker_radius,
"golden-hour",
morning_golden_end,
evening_golden_start,
);
let sun_disc = Circle::new()
.set("class", "sun")
.set("cx", image_width / 2)
.set("cy", image_width as f32 / 2.0 + outer_r / 2.0 + sun_radius)
.set("r", sun_radius)
.set(
"transform",
format!(
"rotate({}, {}, {})",
time_to_degrees(local_time) - utc_rotation,
image_width / 2,
image_width / 2
),
);
let blue_hour_path = get_range_path(
image_width,
marker_radius,
"blue-hour",
sunrise, // morning_blue_end
sunset, // evening_blue_start
);
let nighttime_path = get_range_path(image_width, marker_radius, "night-time", dawn, dusk);
let marker_circle = Circle::new()
.set("class", "marker")
.set("cx", image_width / 2)
.set("cy", image_width / 2)
.set("r", marker_radius);
let noon_marker = Line::new()
.set("class", "mid-marker")
.set(
"transform",
format!(
"rotate({}, {}, {})",
time_to_degrees(noon),
image_width / 2,
image_width / 2
),
)
.set("x1", image_width / 2)
.set(
"y1",
image_width as f32 / 2.0 + marker_radius - sun_radius as f32,
)
.set("x2", image_width / 2)
.set("y2", image_width as f32 / 2.0 + marker_radius);
let midnight_marker = Line::new()
.set("class", "mid-marker")
.set(
"transform",
format!(
"rotate({}, {}, {})",
time_to_degrees(midnight),
image_width / 2,
image_width / 2
),
)
.set("x1", image_width / 2)
.set(
"y1",
image_width as f32 / 2.0 + marker_radius - sun_radius as f32,
)
.set("x2", image_width / 2)
.set("y2", image_width as f32 / 2.0 + marker_radius);
Some(
Group::new()
.set("id", "day-parts")
.set(
"transform",
format!(
"rotate({}, {}, {})",
utc_rotation,
image_width / 2,
image_width / 2
),
)
.add(daytime_circle)
.add(golden_hour_path)
.add(sun_disc)
.add(blue_hour_path)
.add(nighttime_path)
.add(marker_circle)
.add(noon_marker)
.add(midnight_marker),
)
} else {
None
};
let moon_circle = Circle::new()
.set("class", "moon-background")
.set("cx", image_width / 2)
.set("cy", image_width / 2)
.set("r", moon_radius);
let moon_group = Group::new()
.set("id", "moon-container")
.add(moon_circle)
.add(get_moon_path(image_width, moon_radius, moon_phase));
let dial = Line::new()
.set("id", "dial")
.set("class", "dial")
.set(
"transform",
format!(
"rotate({}, {}, {})",
time_to_degrees(local_time),
image_width / 2,
image_width / 2
),
)
.set("x1", image_width / 2)
.set("y1", image_width as f32 / 2.0 + outer_r * 0.5)
.set("x2", image_width / 2)
.set(
"y2",
image_width as f32 / 2.0 + outer_r - ring_width + hour_name_font_size,
);
let current_box_width = (200f32 / 700f32) * image_width as f32;
let current_box_height = (100f32 / 700f32) * image_width as f32;
let current_hour_name_font_size = (40f32 / 700f32) * image_width as f32;
let current_time_font_size = (30f32 / 700f32) * image_width as f32;
let current_hour_rect = Rectangle::new()
.set("x1", 0)
.set("y1", 0)
.set("width", current_box_width)
.set("height", current_box_height);
let current_hour_name = Text::new()
.set("id", "current-hour-name")
.set("font-size", current_hour_name_font_size)
.set("text-anchor", "middle")
.set("dominant-baseline", "mathematical")
.set("x", current_box_width / 2.0)
.set(
"y",
(current_box_height / 5.0) + (current_hour_name_font_size / 2.0),
)
.add(TextNode::new(HOUR_NAMES[utc_hour as usize]));
let current_time_text = Text::new()
.set("font-size", current_time_font_size)
.set("text-anchor", "middle")
.set("dominant-baseline", "mathematical")
.set("x", current_box_width / 2.0)
.set("y", current_box_height - (current_time_font_size / 2.0))
.add(TextNode::new(format!(
"{:02}:{:02}:{:02}",
local_hour, local_minute, local_second
)));
let top_pos = if (6..=18).contains(&local_hour) {
moon_radius * 1.5 // under the moon
} else {
0.0 - moon_radius * 1.5 - current_box_height // above the moon
};
let current_hour_group = Group::new()
.set("id", "current-hour")
.set(
"transform",
format!(
"translate({}, {})",
image_width as f32 / 2.0 - current_box_width / 2.0,
image_width as f32 / 2.0 + top_pos
),
)
.add(current_hour_rect)
.add(current_hour_name)
.add(current_time_text);
let mut document = Document::new()
.set("viewBox", (0i32, 0i32, 700i32, 700i32))
.set("width", 700i32)
.set("height", 700i32)
.set("xmlns:xlink", "http://www.w3.org/1999/xlink")
.add(stylesheet)
.add(definitions)
.add(border)
.add(local_clock);
if let Some(..) = day_parts_group {
document = document.add(day_parts_group.unwrap());
}
document
.add(seasonal_clock)
.add(moon_group)
.add(dial)
.add(current_hour_group)
}
fn main() {
let xdg_dirs = xdg::BaseDirectories::new().unwrap();
let config_path = xdg_dirs
.place_config_file("seasonal-clock.toml")
.expect("cannot create configuration directory");
let data: std::io::Result<String> = fs::read_to_string(config_path);
let config: Option<Config> = if let Ok(..) = data {
let complete_config: CompleteConfig = toml::from_str(&data.unwrap()).unwrap();
Some(complete_config.seasonal_clock)
} else {
None
};
let (env, display, queue) = sctk::new_default_environment!(SeasonalClock, desktop)
.expect("Unable to connect to a Wayland compositor");
@ -84,17 +674,8 @@ fn run_windowed(config: &Option<Config>) {
let mut need_redraw = false;
let mut dimensions = (700, 700);
let hour_name_path_cache = cache_hour_name_paths();
if !env.get_shell().unwrap().needs_configure() {
redraw(
&mut pool,
window.surface(),
dimensions,
config,
&hour_name_path_cache,
)
.expect("Failed to draw");
redraw(&mut pool, window.surface(), dimensions, &config).expect("Failed to draw");
window.refresh()
}
@ -104,32 +685,20 @@ fn run_windowed(config: &Option<Config>) {
let timer_handle = source.handle();
timer_handle.add_timeout(std::time::Duration::from_secs(1), "");
handle
.insert_source(source, |_, timer_handle, event| {
handle.insert_source(source, |_, timer_handle, _| {
println!("Redraw!");
redraw(&mut pool, window.surface(), dimensions).expect("Failed to draw");
window.refresh();
timer_handle.add_timeout(std::time::Duration::from_secs(1), "");
if event.is_none() {
*event = Some(sctk::window::Event::Refresh);
}
})
.unwrap();
});
sctk::WaylandSource::new(queue)
.quick_insert(handle)
.unwrap();
sctk::WaylandSource::new(queue).quick_insert(handle).unwrap();
loop {
// Update every second
match next_action.take() {
Some(WEvent::Close) => break,
Some(WEvent::Refresh) => {
redraw(
&mut pool,
window.surface(),
dimensions,
config,
&hour_name_path_cache,
)
.expect("Failed to draw");
window.refresh();
window.surface().commit();
}
@ -152,14 +721,7 @@ fn run_windowed(config: &Option<Config>) {
if need_redraw {
need_redraw = false;
redraw(
&mut pool,
window.surface(),
dimensions,
config,
&hour_name_path_cache,
)
.expect("Failed to draw")
redraw(&mut pool, window.surface(), dimensions, &config).expect("Failed to draw")
}
if let Err(e) = display.flush() {
@ -177,10 +739,14 @@ fn redraw(
surface: &wl_surface::WlSurface,
(buf_x, buf_y): (u32, u32),
config: &Option<Config>,
hour_name_path_cache: &[(PathData, PathData); 24],
) -> Result<(), ::std::io::Error> {
let document = gen_svg(config, hour_name_path_cache);
let svg_tree = svg_to_usvg(document);
let document = gen_svg(config);
let bytes = document.to_string();
let mut opt = usvg::Options::default();
opt.fontdb.load_system_fonts();
opt.font_family = "Liberation Serif".to_string();
let svg_tree = usvg::Tree::from_str(&bytes, &opt.to_ref()).unwrap();
let (canvas, new_buffer) = pool.buffer(
buf_x as i32,

View File

@ -1,650 +0,0 @@
use std::fmt;
use rctree::Node;
use svg::{
node::{
element::{
path::Data as PathData, Circle, Definitions, Group, Line, Path, Rectangle, Style, Text,
TextPath,
},
Text as TextNode,
},
Document,
};
use usvg::Tree;
use crate::clock::{
get_current_hour_name, get_hms, get_moon_phase, get_seconds_since_midnight, get_utc_hour_name,
get_utc_offset, DayPart,
};
use crate::config::Config;
const IMAGE_WIDTH: u32 = 700;
const HOUR_NAME_FONT_SIZE: f32 = IMAGE_WIDTH as f32 * 0.019109;
const OUTER_R: f32 = (IMAGE_WIDTH as f32) / 2.0 - 3.0 * HOUR_NAME_FONT_SIZE;
const RING_WIDTH: f32 = HOUR_NAME_FONT_SIZE * 3.0;
const UTC_HOUR_FONT_SIZE: f32 = IMAGE_WIDTH as f32 * 0.021462;
enum Season {
Spring,
Summer,
Autumn,
Winter,
}
impl fmt::Display for Season {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Season::Spring => "spring",
Season::Summer => "summer",
Season::Autumn => "autumn",
Season::Winter => "winter",
}
)
}
}
fn seconds_to_degrees(seconds: i32) -> f32 {
seconds as f32 * 360.0 / 86400.0
}
pub fn svg_to_usvg(document: Document) -> Tree {
let doc_str = document.to_string();
let mut opt = usvg::Options::default();
opt.fontdb.load_system_fonts();
opt.font_family = "Liberation Serif".to_string();
Tree::from_str(&doc_str, &opt.to_ref()).unwrap()
}
fn hour_name_path() -> Path {
let radius = OUTER_R - RING_WIDTH / 2.0;
let delta_x = radius * (15.0_f32.to_radians() / 2.0).sin();
let delta_y = radius * (1.0 - (15.0_f32.to_radians() / 2.0).cos());
let x1 = (IMAGE_WIDTH as f32) / 2.0 - delta_x;
let y1 = ((IMAGE_WIDTH as f32) / 2.0 - radius) + delta_y;
let path_data = PathData::new().move_to((x1, y1)).elliptical_arc_by((
radius,
radius,
15,
0,
1,
2.0 * delta_x,
0,
));
Path::new().set("id", "hour-name-path").set("d", path_data)
}
fn create_temp_document(hour: usize) -> Document {
let definitions = Definitions::new().add(hour_name_path());
let utc_hour_y = IMAGE_WIDTH as f32 / 2.0 - OUTER_R + RING_WIDTH + UTC_HOUR_FONT_SIZE;
let hour_name_text_path = TextPath::new()
.set("xlink:href", "#hour-name-path")
.set("startOffset", "50%")
.add(TextNode::new(get_utc_hour_name(hour)));
let hour_name_text = Text::new()
.set("id", "hour-name")
.set("text-anchor", "middle")
.set("dominant-baseline", "mathematical")
.set("font-size", HOUR_NAME_FONT_SIZE)
.add(hour_name_text_path);
let utc_hour_text = Text::new()
.set("id", "utc-hour")
.set(
"transform",
format!("rotate(-7.5, {}, {})", IMAGE_WIDTH / 2, IMAGE_WIDTH / 2),
)
.set("x", IMAGE_WIDTH / 2)
.set("y", utc_hour_y)
.set("text-anchor", "middle")
.set("dominant-baseline", "mathematical")
.set("font-size", UTC_HOUR_FONT_SIZE)
.add(TextNode::new(format!("U {:02}", hour)));
Document::new()
.set("viewBox", (0i32, 0i32, 700i32, 700i32))
.set("width", 700i32)
.set("height", 700i32)
.set("xmlns:xlink", "http://www.w3.org/1999/xlink")
.add(definitions)
.add(hour_name_text)
.add(utc_hour_text)
}
fn node_to_path(node: Node<usvg::NodeKind>) -> PathData {
let mut svg_path_data = PathData::new();
match *node.borrow() {
usvg::NodeKind::Path(ref path) => {
let path_data = &path.data;
for segment in path_data.0.iter() {
match segment {
usvg::PathSegment::MoveTo { x, y } => {
svg_path_data = svg_path_data.move_to((*x, *y));
}
usvg::PathSegment::LineTo { x, y } => {
svg_path_data = svg_path_data.line_to((*x, *y));
}
usvg::PathSegment::CurveTo {
x1,
y1,
x2,
y2,
x,
y,
} => {
svg_path_data = svg_path_data.cubic_curve_to((*x1, *y1, *x2, *y2, *x, *y));
}
usvg::PathSegment::ClosePath => {
svg_path_data = svg_path_data.close();
}
}
}
}
_ => {
unreachable!()
}
}
svg_path_data
}
fn cache_hour_name_path(hour: usize) -> (PathData, PathData) {
let tree = svg_to_usvg(create_temp_document(hour));
let text_node = tree.node_by_id("hour-name").unwrap();
let utc_text_node = tree.node_by_id("utc-hour").unwrap();
(node_to_path(text_node), node_to_path(utc_text_node))
}
pub fn cache_hour_name_paths() -> [(PathData, PathData); 24] {
[
cache_hour_name_path(0),
cache_hour_name_path(1),
cache_hour_name_path(2),
cache_hour_name_path(3),
cache_hour_name_path(4),
cache_hour_name_path(5),
cache_hour_name_path(6),
cache_hour_name_path(7),
cache_hour_name_path(8),
cache_hour_name_path(9),
cache_hour_name_path(10),
cache_hour_name_path(11),
cache_hour_name_path(12),
cache_hour_name_path(13),
cache_hour_name_path(14),
cache_hour_name_path(15),
cache_hour_name_path(16),
cache_hour_name_path(17),
cache_hour_name_path(18),
cache_hour_name_path(19),
cache_hour_name_path(20),
cache_hour_name_path(21),
cache_hour_name_path(22),
cache_hour_name_path(23),
]
}
fn hour_marker(
hour: i32,
hour_name_path_data: &(PathData, PathData),
is_current_hour: bool,
) -> Group {
let season = match hour {
0..=5 => Season::Winter,
6..=11 => Season::Spring,
12..=17 => Season::Summer,
18..=23 => Season::Autumn,
_ => panic!("Hour out of range"),
};
let rotation = hour * 15;
let delta_x = OUTER_R * (15f32.to_radians() / 2.0).sin();
let delta_y = OUTER_R * (1.0 - (15f32.to_radians() / 2.0).cos());
let s_delta_x = 0.0 - RING_WIDTH * (15f32.to_radians() / 2.0).sin();
let s_delta_y = RING_WIDTH * (15f32.to_radians() / 2.0).cos();
let i_delta_x = -2.0 * (OUTER_R - RING_WIDTH) * (15f32.to_radians() / 2.0).sin();
let x1 = IMAGE_WIDTH as f32 / 2.0 - delta_x;
let y1 = (IMAGE_WIDTH as f32 / 2.0 - OUTER_R) + delta_y;
let path_data = PathData::new()
.move_to((x1, y1))
.elliptical_arc_by((OUTER_R, OUTER_R, 15, 0, 1, 2.0 * delta_x, 0))
.line_by((s_delta_x, s_delta_y))
.elliptical_arc_by((
OUTER_R - RING_WIDTH,
OUTER_R - RING_WIDTH,
15,
0,
0,
i_delta_x,
0,
))
.close();
let path = Path::new().set("class", "hour-outline").set("d", path_data);
let hour_name_path = Path::new()
.set("class", "hour-name")
.set("d", hour_name_path_data.0.clone());
let utc_hour_path = Path::new()
.set("class", "utc")
.set("d", hour_name_path_data.1.clone());
Group::new()
.set(
"class",
format!(
"hour {season}{}",
if is_current_hour { " active" } else { "" }
),
)
.set(
"transform",
format!(
"rotate({}, {}, {})",
rotation as f32 - 172.5,
IMAGE_WIDTH / 2,
IMAGE_WIDTH / 2
),
)
.add(path)
.add(hour_name_path)
.add(utc_hour_path)
}
fn get_range_path(radius: f32, range_name: &str, start_time: i32, end_time: i32) -> Path {
let start_deg = seconds_to_degrees(start_time);
let end_deg = seconds_to_degrees(end_time);
let deg_diff = end_deg - start_deg;
let start_delta_x = radius * start_deg.to_radians().sin();
let start_delta_y = radius * (1.0 - start_deg.to_radians().cos());
let end_delta_x = radius * end_deg.to_radians().sin();
let end_delta_y = radius * (1.0 - end_deg.to_radians().cos());
let path_data = PathData::new()
.move_to((IMAGE_WIDTH / 2, IMAGE_WIDTH / 2))
.line_to((
IMAGE_WIDTH as f32 / 2.0 - start_delta_x,
IMAGE_WIDTH as f32 / 2.0 + radius - start_delta_y,
))
.elliptical_arc_to((
radius,
radius,
deg_diff,
((start_deg < end_deg) ^ (deg_diff.abs() >= 180.0)) as u8,
0,
IMAGE_WIDTH as f32 / 2.0 - end_delta_x,
IMAGE_WIDTH as f32 / 2.0 + radius - end_delta_y,
))
.close();
Path::new().set("class", range_name).set("d", path_data)
}
fn get_moon_path(radius: f32, moon_phase: f32) -> Path {
let handle_x_pos = radius * 1.34;
let handle_y_pos = radius * 0.88;
let min_x = IMAGE_WIDTH as f32 / 2.0 - handle_x_pos;
let max_x = min_x + 2.0 * handle_x_pos;
let top_y = IMAGE_WIDTH as f32 / 2.0 - handle_y_pos;
let bottom_y = IMAGE_WIDTH as f32 / 2.0 + handle_y_pos;
let h1_x: f32;
let h2_x: f32;
if moon_phase < 14.0 {
h1_x = min_x + 2.0 * handle_x_pos * (1.0 - moon_phase / 14.0);
h2_x = max_x;
} else {
h1_x = min_x;
h2_x = max_x + 2.0 * handle_x_pos * (1.0 - moon_phase / 14.0)
}
let path_data = PathData::new()
.move_to((IMAGE_WIDTH as f32 / 2.0, IMAGE_WIDTH as f32 / 2.0 - radius))
.cubic_curve_to((
h1_x,
top_y,
h1_x,
bottom_y,
IMAGE_WIDTH as f32 / 2.0,
IMAGE_WIDTH as f32 / 2.0 + radius,
))
.cubic_curve_to((
h2_x,
bottom_y,
h2_x,
top_y,
IMAGE_WIDTH / 2,
IMAGE_WIDTH as f32 / 2.0 - radius,
));
Path::new().set("class", "moon").set("d", path_data)
}
pub fn gen_svg(
config: &Option<Config>,
hour_name_path_cache: &[(PathData, PathData); 24],
) -> Document {
let (utc_hour, _, _) = get_hms(config, DayPart::UtcNow);
let (local_hour, local_minute, local_second) = get_hms(config, DayPart::LocalNow);
let utc_offset = get_utc_offset();
let local_time = get_seconds_since_midnight(config, DayPart::LocalNow);
let utc_rotation = seconds_to_degrees(utc_offset);
let moon_radius = IMAGE_WIDTH as f32 * 0.071428571;
// Calculate the Moon phase
let moon_phase = get_moon_phase();
let local_hour_font_size = IMAGE_WIDTH as f32 * 0.02357;
let sun_radius = IMAGE_WIDTH as f32 * 0.0142871;
let marker_radius = OUTER_R - RING_WIDTH - 2.0 * UTC_HOUR_FONT_SIZE;
let border = Rectangle::new()
.set("x", 0i32)
.set("y", 0i32)
.set("width", 700i32)
.set("height", 700i32)
.set("id", "border");
let stylesheet = Style::new(
"\
#border {stroke: none; fill: rgb(19, 17, 30); }
.hour path.hour-outline {stroke: rgb(0, 0, 0); stroke-width: 2px;}
.hour path.hour-name {stroke: none; fill: rgb(238, 187, 85);}
.hour path.utc {stroke: none; fill: rgb(91, 68, 38);}
.winter path {fill: rgb(70, 62, 108);}
.active.winter path.hour-outline {fill: rgb(100, 92, 138);}
.spring path {fill: rgb(55, 87, 55);}
.active.spring path.hour-outline {fill: rgb(85, 117, 85);}
.summer path {fill: rgb(113, 92, 43);}
.active.summer path.hour-outline {fill: rgb(143, 122, 73);}
.autumn path {fill: rgb(108, 68, 44);}
.active.autumn.hour-outline path {fill: rgb(138, 98, 74);}
.local-hour {stroke: none; fill: rgb(238, 187, 85);}
.night-time {stroke: none; fill: rgb(19, 17, 30);}
.blue-hour {stroke: none; fill: rgb(9, 1, 119);}
.golden-hour {stroke: none; fill: rgb(170, 132, 44);}
.day-time {stroke: none; fill: rgb(125, 197, 240);}
.marker {stroke: rgb(19, 17, 30); stroke-width: 2px; fill: none;}
.moon-background {stroke: rgb(170, 170, 170); stroke-width: 2px; fill: rgb(19, 17, 30);}
.moon {stroke: none; fill: rgb(170, 170, 170);}
.sun {stroke: none; fill: rgb(238, 187, 85);}
.mid-marker {stroke: red;}
.dial {stroke-width: 2px; stroke: rgb(238, 187, 85);}
#current-hour rect {stroke: none; fill: rgba(255, 255, 255, 0.5);}
#current-hour-name {font-weight: bold;}",
);
let mut local_clock = Group::new().set("id", "local-clock");
for hour in 0i32..24 {
let hour_str = match hour {
0 => "Midnight".to_string(),
12 => "Noon".to_string(),
_ => hour.to_string(),
};
let rotation = hour * 15;
let hour_name = TextNode::new(hour_str);
let hour_node = Text::new()
.set("class", "local-hour")
.set("transform", format!("rotate({}, 350, 350)", 180 + rotation))
.set("x", (IMAGE_WIDTH as f32) / 2.0)
.set(
"y",
(IMAGE_WIDTH as f32) / 2.0 - OUTER_R - local_hour_font_size / 2.0,
)
.set("text-anchor", "middle")
.set("font-size", local_hour_font_size as f32)
.add(hour_name);
local_clock = local_clock.add(hour_node);
}
let mut seasonal_clock = Group::new().set(
"transform",
format!(
"rotate({}, {}, {})",
utc_rotation,
IMAGE_WIDTH / 2,
IMAGE_WIDTH / 2
),
);
for hour in 0i32..24 {
seasonal_clock = seasonal_clock.add(hour_marker(
hour,
&hour_name_path_cache[hour as usize],
hour == utc_hour as i32,
));
}
let daytime_circle = Circle::new()
.set("class", "day-time")
.set("cx", IMAGE_WIDTH / 2)
.set("cy", IMAGE_WIDTH / 2)
.set("r", marker_radius);
let day_parts_group: Option<Group> = if config.is_some() {
let noon = get_seconds_since_midnight(config, DayPart::UtcNoon);
let midnight = get_seconds_since_midnight(config, DayPart::UtcMidnight);
let morning_golden_end = get_seconds_since_midnight(config, DayPart::UtcMorningGoldenEnd);
let evening_golden_start =
get_seconds_since_midnight(config, DayPart::UtcEveningGoldenStart);
let sunrise = get_seconds_since_midnight(config, DayPart::UtcSunrise);
let sunset = get_seconds_since_midnight(config, DayPart::UtcSunset);
let dawn = get_seconds_since_midnight(config, DayPart::UtcDawnStart);
let dusk = get_seconds_since_midnight(config, DayPart::UtcDuskEnd);
let golden_hour_path = get_range_path(
marker_radius,
"golden-hour",
morning_golden_end,
evening_golden_start,
);
let sun_disc = Circle::new()
.set("class", "sun")
.set("cx", IMAGE_WIDTH / 2)
.set("cy", IMAGE_WIDTH as f32 / 2.0 + OUTER_R / 2.0 + sun_radius)
.set("r", sun_radius)
.set(
"transform",
format!(
"rotate({}, {}, {})",
seconds_to_degrees(local_time) - utc_rotation,
IMAGE_WIDTH / 2,
IMAGE_WIDTH / 2
),
);
let blue_hour_path = get_range_path(
marker_radius,
"blue-hour",
sunrise, // morning_blue_end
sunset, // evening_blue_start
);
let nighttime_path = get_range_path(marker_radius, "night-time", dawn, dusk);
let marker_circle = Circle::new()
.set("class", "marker")
.set("cx", IMAGE_WIDTH / 2)
.set("cy", IMAGE_WIDTH / 2)
.set("r", marker_radius);
let noon_marker = Line::new()
.set("class", "mid-marker")
.set(
"transform",
format!(
"rotate({}, {}, {})",
seconds_to_degrees(noon),
IMAGE_WIDTH / 2,
IMAGE_WIDTH / 2
),
)
.set("x1", IMAGE_WIDTH / 2)
.set(
"y1",
IMAGE_WIDTH as f32 / 2.0 + marker_radius - sun_radius as f32,
)
.set("x2", IMAGE_WIDTH / 2)
.set("y2", IMAGE_WIDTH as f32 / 2.0 + marker_radius);
let midnight_marker = Line::new()
.set("class", "mid-marker")
.set(
"transform",
format!(
"rotate({}, {}, {})",
seconds_to_degrees(midnight),
IMAGE_WIDTH / 2,
IMAGE_WIDTH / 2
),
)
.set("x1", IMAGE_WIDTH / 2)
.set(
"y1",
IMAGE_WIDTH as f32 / 2.0 + marker_radius - sun_radius as f32,
)
.set("x2", IMAGE_WIDTH / 2)
.set("y2", IMAGE_WIDTH as f32 / 2.0 + marker_radius);
Some(
Group::new()
.set("id", "day-parts")
.set(
"transform",
format!(
"rotate({}, {}, {})",
utc_rotation,
IMAGE_WIDTH / 2,
IMAGE_WIDTH / 2
),
)
.add(daytime_circle)
.add(golden_hour_path)
.add(sun_disc)
.add(blue_hour_path)
.add(nighttime_path)
.add(marker_circle)
.add(noon_marker)
.add(midnight_marker),
)
} else {
None
};
let moon_circle = Circle::new()
.set("class", "moon-background")
.set("cx", IMAGE_WIDTH / 2)
.set("cy", IMAGE_WIDTH / 2)
.set("r", moon_radius);
let moon_group = Group::new()
.set("id", "moon-container")
.add(moon_circle)
.add(get_moon_path(moon_radius, moon_phase));
let dial = Line::new()
.set("id", "dial")
.set("class", "dial")
.set(
"transform",
format!(
"rotate({}, {}, {})",
seconds_to_degrees(local_time),
IMAGE_WIDTH / 2,
IMAGE_WIDTH / 2
),
)
.set("x1", IMAGE_WIDTH / 2)
.set("y1", IMAGE_WIDTH as f32 / 2.0 + OUTER_R * 0.5)
.set("x2", IMAGE_WIDTH / 2)
.set(
"y2",
IMAGE_WIDTH as f32 / 2.0 + OUTER_R - RING_WIDTH + HOUR_NAME_FONT_SIZE,
);
let current_box_width = (200f32 / 700f32) * IMAGE_WIDTH as f32;
let current_box_height = (100f32 / 700f32) * IMAGE_WIDTH as f32;
let current_hour_name_font_size = (40f32 / 700f32) * IMAGE_WIDTH as f32;
let current_time_font_size = (30f32 / 700f32) * IMAGE_WIDTH as f32;
let current_hour_rect = Rectangle::new()
.set("x1", 0)
.set("y1", 0)
.set("width", current_box_width)
.set("height", current_box_height);
let current_hour_name = Text::new()
.set("id", "current-hour-name")
.set("font-size", current_hour_name_font_size)
.set("text-anchor", "middle")
.set("dominant-baseline", "mathematical")
.set("x", current_box_width / 2.0)
.set(
"y",
(current_box_height / 5.0) + (current_hour_name_font_size / 2.0),
)
.add(TextNode::new(get_current_hour_name()));
let current_time_text = Text::new()
.set("font-size", current_time_font_size)
.set("text-anchor", "middle")
.set("dominant-baseline", "mathematical")
.set("x", current_box_width / 2.0)
.set("y", current_box_height - (current_time_font_size / 2.0))
.add(TextNode::new(format!(
"{:02}:{:02}:{:02}",
local_hour, local_minute, local_second
)));
let top_pos = if (6..=18).contains(&local_hour) {
moon_radius * 1.5 // under the moon
} else {
0.0 - moon_radius * 1.5 - current_box_height // above the moon
};
let current_hour_group = Group::new()
.set("id", "current-hour")
.set(
"transform",
format!(
"translate({}, {})",
IMAGE_WIDTH as f32 / 2.0 - current_box_width / 2.0,
IMAGE_WIDTH as f32 / 2.0 + top_pos
),
)
.add(current_hour_rect)
.add(current_hour_name)
.add(current_time_text);
let mut document = Document::new()
.set("viewBox", (0i32, 0i32, 700i32, 700i32))
.set("width", 700i32)
.set("height", 700i32)
.set("xmlns:xlink", "http://www.w3.org/1999/xlink")
.add(stylesheet)
.add(border)
.add(local_clock);
if let Some(..) = day_parts_group {
document = document.add(day_parts_group.unwrap());
}
document
.add(seasonal_clock)
.add(moon_group)
.add(dial)
.add(current_hour_group)
}