2022-05-16 13:15:34 +00:00
|
|
|
|
extern crate smithay_client_toolkit as sctk;
|
|
|
|
|
|
2022-05-21 04:29:39 +00:00
|
|
|
|
use std::fs;
|
2022-05-17 14:45:01 +00:00
|
|
|
|
|
2022-05-19 11:52:31 +00:00
|
|
|
|
use calloop::{timer::Timer, EventLoop};
|
2022-05-19 06:14:01 +00:00
|
|
|
|
use chrono::prelude::{Local, Utc};
|
2022-05-18 03:23:44 +00:00
|
|
|
|
use chrono::TimeZone;
|
2022-05-17 14:56:26 +00:00
|
|
|
|
use chrono::Timelike;
|
2022-05-16 13:15:34 +00:00
|
|
|
|
use sctk::reexports::client::protocol::{wl_shm, wl_surface};
|
|
|
|
|
use sctk::shm::AutoMemPool;
|
|
|
|
|
use sctk::window::{Event as WEvent, FallbackFrame};
|
2022-05-21 04:29:39 +00:00
|
|
|
|
use serde::Deserialize;
|
2022-05-17 15:23:26 +00:00
|
|
|
|
use svg::node::element::path::Data as PathData;
|
2022-05-23 03:01:25 +00:00
|
|
|
|
use svg::node::element::{Circle, Group, Line, Path, Rectangle, Style, Text};
|
2022-05-17 14:56:26 +00:00
|
|
|
|
use svg::node::Text as TextNode;
|
|
|
|
|
use svg::Document;
|
2022-05-16 13:15:34 +00:00
|
|
|
|
|
2022-05-22 16:58:27 +00:00
|
|
|
|
mod svg_clock;
|
|
|
|
|
|
2022-05-23 02:41:09 +00:00
|
|
|
|
use svg_clock::{
|
2022-05-23 15:46:20 +00:00
|
|
|
|
cache_hour_name_paths, hour_marker, seconds_to_degrees, svg_to_usvg, HOUR_NAMES,
|
|
|
|
|
HOUR_NAME_FONT_SIZE, IMAGE_WIDTH, OUTER_R, RING_WIDTH, UTC_HOUR_FONT_SIZE,
|
2022-05-23 02:41:09 +00:00
|
|
|
|
};
|
2022-05-16 13:15:34 +00:00
|
|
|
|
|
2022-05-22 16:58:27 +00:00
|
|
|
|
sctk::default_environment!(SeasonalClock, desktop);
|
2022-05-18 16:15:16 +00:00
|
|
|
|
|
2022-05-21 04:57:20 +00:00
|
|
|
|
#[derive(Deserialize, Copy, Clone)]
|
2022-05-21 04:29:39 +00:00
|
|
|
|
struct Config {
|
|
|
|
|
latitude: f64,
|
|
|
|
|
longitude: f64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
|
struct CompleteConfig {
|
|
|
|
|
seasonal_clock: Config,
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 03:59:49 +00:00
|
|
|
|
fn get_range_path(radius: f32, range_name: &str, start_time: i32, end_time: i32) -> Path {
|
2022-05-23 15:48:45 +00:00
|
|
|
|
let start_deg = seconds_to_degrees(start_time);
|
|
|
|
|
let end_deg = seconds_to_degrees(end_time);
|
2022-05-18 09:18:41 +00:00
|
|
|
|
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()
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.move_to((IMAGE_WIDTH / 2, IMAGE_WIDTH / 2))
|
2022-05-18 09:18:41 +00:00
|
|
|
|
.line_to((
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH as f32 / 2.0 - start_delta_x,
|
|
|
|
|
IMAGE_WIDTH as f32 / 2.0 + radius - start_delta_y,
|
2022-05-18 09:18:41 +00:00
|
|
|
|
))
|
|
|
|
|
.elliptical_arc_to((
|
|
|
|
|
radius,
|
|
|
|
|
radius,
|
|
|
|
|
deg_diff,
|
2022-05-19 07:19:57 +00:00
|
|
|
|
((start_deg < end_deg) ^ (deg_diff.abs() >= 180.0)) as u8,
|
2022-05-18 09:18:41 +00:00
|
|
|
|
0,
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH as f32 / 2.0 - end_delta_x,
|
|
|
|
|
IMAGE_WIDTH as f32 / 2.0 + radius - end_delta_y,
|
2022-05-18 09:18:41 +00:00
|
|
|
|
))
|
|
|
|
|
.close();
|
|
|
|
|
|
|
|
|
|
Path::new().set("class", range_name).set("d", path_data)
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 03:59:49 +00:00
|
|
|
|
fn get_moon_path(radius: f32, moon_phase: f64) -> Path {
|
2022-05-17 15:23:26 +00:00
|
|
|
|
let handle_x_pos = radius as f64 * 1.34;
|
|
|
|
|
let handle_y_pos = radius * 0.88;
|
2022-05-22 03:59:49 +00:00
|
|
|
|
let min_x = IMAGE_WIDTH as f64 / 2.0 - handle_x_pos;
|
2022-05-17 15:23:26 +00:00
|
|
|
|
let max_x = min_x + 2.0 * handle_x_pos;
|
2022-05-22 03:59:49 +00:00
|
|
|
|
let top_y = IMAGE_WIDTH as f32 / 2.0 - handle_y_pos;
|
|
|
|
|
let bottom_y = IMAGE_WIDTH as f32 / 2.0 + handle_y_pos;
|
2022-05-17 15:23:26 +00:00
|
|
|
|
|
|
|
|
|
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()
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.move_to((IMAGE_WIDTH as f32 / 2.0, IMAGE_WIDTH as f32 / 2.0 - radius))
|
2022-05-17 15:23:26 +00:00
|
|
|
|
.cubic_curve_to((
|
|
|
|
|
h1_x,
|
|
|
|
|
top_y,
|
|
|
|
|
h1_x,
|
|
|
|
|
bottom_y,
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH as f32 / 2.0,
|
|
|
|
|
IMAGE_WIDTH as f32 / 2.0 + radius,
|
2022-05-17 15:23:26 +00:00
|
|
|
|
))
|
|
|
|
|
.cubic_curve_to((
|
|
|
|
|
h2_x,
|
|
|
|
|
bottom_y,
|
|
|
|
|
h2_x,
|
|
|
|
|
top_y,
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH / 2,
|
|
|
|
|
IMAGE_WIDTH as f32 / 2.0 - radius,
|
2022-05-17 15:23:26 +00:00
|
|
|
|
));
|
|
|
|
|
Path::new().set("class", "moon").set("d", path_data)
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 11:45:14 +00:00
|
|
|
|
fn gen_svg(config: &Option<Config>, hour_name_path_cache: &[(PathData, PathData); 24]) -> Document {
|
2022-05-17 14:45:01 +00:00
|
|
|
|
let local_timestamp = Local::now();
|
2022-05-19 06:33:29 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2022-05-18 03:44:07 +00:00
|
|
|
|
let utc_offset = local_timestamp.offset().local_minus_utc();
|
2022-05-17 14:45:01 +00:00
|
|
|
|
let local_time = local_timestamp.time().num_seconds_from_midnight() as i32;
|
2022-05-19 06:14:01 +00:00
|
|
|
|
let utc_rotation = seconds_to_degrees(utc_offset);
|
2022-05-17 14:45:01 +00:00
|
|
|
|
|
2022-05-17 15:23:26 +00:00
|
|
|
|
// Calculate the Moon phase
|
|
|
|
|
let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis());
|
|
|
|
|
let moon_illumination = suncalc::moon_illumination(unixtime);
|
2022-05-22 03:59:49 +00:00
|
|
|
|
let moon_radius = IMAGE_WIDTH as f32 * 0.071428571;
|
2022-05-17 15:23:26 +00:00
|
|
|
|
let moon_phase = moon_illumination.phase * 28.0;
|
|
|
|
|
|
2022-05-22 03:59:49 +00:00
|
|
|
|
let local_hour_font_size = IMAGE_WIDTH as f32 * 0.02357;
|
|
|
|
|
let sun_radius = IMAGE_WIDTH as f32 * 0.0142871;
|
2022-05-23 11:48:45 +00:00
|
|
|
|
let marker_radius = OUTER_R - RING_WIDTH - 2.0 * UTC_HOUR_FONT_SIZE;
|
2022-05-18 09:18:41 +00:00
|
|
|
|
|
2022-05-17 14:45:01 +00:00
|
|
|
|
let border = Rectangle::new()
|
|
|
|
|
.set("x", 0i32)
|
|
|
|
|
.set("y", 0i32)
|
|
|
|
|
.set("width", 700i32)
|
|
|
|
|
.set("height", 700i32)
|
|
|
|
|
.set("id", "border");
|
2022-05-17 14:56:26 +00:00
|
|
|
|
let stylesheet = Style::new(
|
|
|
|
|
"\
|
2022-05-17 14:45:01 +00:00
|
|
|
|
#border {stroke: none; fill: rgb(19, 17, 30); }
|
2022-05-23 03:01:25 +00:00
|
|
|
|
.hour path.hour-outline {stroke: rgb(0, 0, 0); stroke-width: 2px;}
|
|
|
|
|
.hour path.hour-name {stroke: none; fill: rgb(238, 187, 85);}
|
2022-05-23 11:45:14 +00:00
|
|
|
|
.hour path.utc {stroke: none; fill: rgb(91, 68, 38);}
|
2022-05-19 03:04:48 +00:00
|
|
|
|
.winter path {fill: rgb(70, 62, 108);}
|
2022-05-23 03:01:25 +00:00
|
|
|
|
.active.winter path.hour-outline {fill: rgb(100, 92, 138);}
|
2022-05-19 03:04:48 +00:00
|
|
|
|
.spring path {fill: rgb(55, 87, 55);}
|
2022-05-23 03:01:25 +00:00
|
|
|
|
.active.spring path.hour-outline {fill: rgb(85, 117, 85);}
|
2022-05-19 03:04:48 +00:00
|
|
|
|
.summer path {fill: rgb(113, 92, 43);}
|
2022-05-23 03:01:25 +00:00
|
|
|
|
.active.summer path.hour-outline {fill: rgb(143, 122, 73);}
|
2022-05-19 03:04:48 +00:00
|
|
|
|
.autumn path {fill: rgb(108, 68, 44);}
|
2022-05-23 03:01:25 +00:00
|
|
|
|
.active.autumn.hour-outline path {fill: rgb(138, 98, 74);}
|
2022-05-17 14:45:01 +00:00
|
|
|
|
.local-hour {stroke: none; fill: rgb(238, 187, 85);}
|
2022-05-18 15:13:43 +00:00
|
|
|
|
.night-time {stroke: none; fill: rgb(19, 17, 30);}
|
2022-05-18 09:18:41 +00:00
|
|
|
|
.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);}
|
2022-05-18 15:19:49 +00:00
|
|
|
|
.marker {stroke: rgb(19, 17, 30); stroke-width: 2px; fill: none;}
|
2022-05-17 15:23:26 +00:00
|
|
|
|
.moon-background {stroke: rgb(170, 170, 170); stroke-width: 2px; fill: rgb(19, 17, 30);}
|
|
|
|
|
.moon {stroke: none; fill: rgb(170, 170, 170);}
|
2022-05-18 15:10:55 +00:00
|
|
|
|
.sun {stroke: none; fill: rgb(238, 187, 85);}
|
2022-05-18 03:23:44 +00:00
|
|
|
|
.mid-marker {stroke: red;}
|
2022-05-19 04:19:38 +00:00
|
|
|
|
.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;}",
|
2022-05-17 14:56:26 +00:00
|
|
|
|
);
|
2022-05-18 16:48:19 +00:00
|
|
|
|
|
2022-05-17 14:56:26 +00:00
|
|
|
|
let mut local_clock = Group::new().set("id", "local-clock");
|
2022-05-17 14:45:01 +00:00
|
|
|
|
|
|
|
|
|
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))
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.set("x", (IMAGE_WIDTH as f32) / 2.0)
|
2022-05-17 14:56:26 +00:00
|
|
|
|
.set(
|
|
|
|
|
"y",
|
2022-05-22 04:04:32 +00:00
|
|
|
|
(IMAGE_WIDTH as f32) / 2.0 - OUTER_R - local_hour_font_size / 2.0,
|
2022-05-17 14:56:26 +00:00
|
|
|
|
)
|
2022-05-17 14:45:01 +00:00
|
|
|
|
.set("text-anchor", "middle")
|
|
|
|
|
.set("font-size", local_hour_font_size as f32)
|
|
|
|
|
.add(hour_name);
|
|
|
|
|
local_clock = local_clock.add(hour_node);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 03:44:07 +00:00
|
|
|
|
let mut seasonal_clock = Group::new().set(
|
|
|
|
|
"transform",
|
|
|
|
|
format!(
|
|
|
|
|
"rotate({}, {}, {})",
|
2022-05-19 06:14:01 +00:00
|
|
|
|
utc_rotation,
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH / 2,
|
|
|
|
|
IMAGE_WIDTH / 2
|
2022-05-18 03:44:07 +00:00
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for hour in 0i32..24 {
|
|
|
|
|
seasonal_clock = seasonal_clock.add(hour_marker(
|
|
|
|
|
hour,
|
2022-05-23 03:01:25 +00:00
|
|
|
|
&hour_name_path_cache[hour as usize],
|
2022-05-19 03:33:19 +00:00
|
|
|
|
hour == utc_hour as i32,
|
2022-05-18 03:44:07 +00:00
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 09:18:41 +00:00
|
|
|
|
let daytime_circle = Circle::new()
|
|
|
|
|
.set("class", "day-time")
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.set("cx", IMAGE_WIDTH / 2)
|
|
|
|
|
.set("cy", IMAGE_WIDTH / 2)
|
2022-05-18 09:18:41 +00:00
|
|
|
|
.set("r", marker_radius);
|
|
|
|
|
|
2022-05-21 04:57:20 +00:00
|
|
|
|
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(
|
|
|
|
|
marker_radius,
|
|
|
|
|
"golden-hour",
|
|
|
|
|
morning_golden_end,
|
|
|
|
|
evening_golden_start,
|
2022-05-18 15:10:55 +00:00
|
|
|
|
);
|
|
|
|
|
|
2022-05-21 04:57:20 +00:00
|
|
|
|
let sun_disc = Circle::new()
|
|
|
|
|
.set("class", "sun")
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.set("cx", IMAGE_WIDTH / 2)
|
2022-05-22 04:04:32 +00:00
|
|
|
|
.set("cy", IMAGE_WIDTH as f32 / 2.0 + OUTER_R / 2.0 + sun_radius)
|
2022-05-21 04:57:20 +00:00
|
|
|
|
.set("r", sun_radius)
|
|
|
|
|
.set(
|
|
|
|
|
"transform",
|
|
|
|
|
format!(
|
|
|
|
|
"rotate({}, {}, {})",
|
2022-05-23 15:48:45 +00:00
|
|
|
|
seconds_to_degrees(local_time) - utc_rotation,
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH / 2,
|
|
|
|
|
IMAGE_WIDTH / 2
|
2022-05-21 04:57:20 +00:00
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let blue_hour_path = get_range_path(
|
|
|
|
|
marker_radius,
|
|
|
|
|
"blue-hour",
|
|
|
|
|
sunrise, // morning_blue_end
|
|
|
|
|
sunset, // evening_blue_start
|
|
|
|
|
);
|
2022-05-18 09:18:41 +00:00
|
|
|
|
|
2022-05-22 03:59:49 +00:00
|
|
|
|
let nighttime_path = get_range_path(marker_radius, "night-time", dawn, dusk);
|
2022-05-18 15:15:27 +00:00
|
|
|
|
|
2022-05-21 04:57:20 +00:00
|
|
|
|
let marker_circle = Circle::new()
|
|
|
|
|
.set("class", "marker")
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.set("cx", IMAGE_WIDTH / 2)
|
|
|
|
|
.set("cy", IMAGE_WIDTH / 2)
|
2022-05-21 04:57:20 +00:00
|
|
|
|
.set("r", marker_radius);
|
2022-05-19 06:14:01 +00:00
|
|
|
|
|
2022-05-21 04:57:20 +00:00
|
|
|
|
let noon_marker = Line::new()
|
|
|
|
|
.set("class", "mid-marker")
|
|
|
|
|
.set(
|
|
|
|
|
"transform",
|
|
|
|
|
format!(
|
|
|
|
|
"rotate({}, {}, {})",
|
2022-05-23 15:48:45 +00:00
|
|
|
|
seconds_to_degrees(noon),
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH / 2,
|
|
|
|
|
IMAGE_WIDTH / 2
|
2022-05-21 04:57:20 +00:00
|
|
|
|
),
|
|
|
|
|
)
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.set("x1", IMAGE_WIDTH / 2)
|
2022-05-21 04:57:20 +00:00
|
|
|
|
.set(
|
|
|
|
|
"y1",
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH as f32 / 2.0 + marker_radius - sun_radius as f32,
|
2022-05-21 04:57:20 +00:00
|
|
|
|
)
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.set("x2", IMAGE_WIDTH / 2)
|
|
|
|
|
.set("y2", IMAGE_WIDTH as f32 / 2.0 + marker_radius);
|
2022-05-19 06:14:01 +00:00
|
|
|
|
|
2022-05-21 04:57:20 +00:00
|
|
|
|
let midnight_marker = Line::new()
|
|
|
|
|
.set("class", "mid-marker")
|
|
|
|
|
.set(
|
|
|
|
|
"transform",
|
|
|
|
|
format!(
|
|
|
|
|
"rotate({}, {}, {})",
|
2022-05-23 15:48:45 +00:00
|
|
|
|
seconds_to_degrees(midnight),
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH / 2,
|
|
|
|
|
IMAGE_WIDTH / 2
|
2022-05-21 04:57:20 +00:00
|
|
|
|
),
|
|
|
|
|
)
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.set("x1", IMAGE_WIDTH / 2)
|
2022-05-21 04:57:20 +00:00
|
|
|
|
.set(
|
|
|
|
|
"y1",
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH as f32 / 2.0 + marker_radius - sun_radius as f32,
|
2022-05-21 04:57:20 +00:00
|
|
|
|
)
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.set("x2", IMAGE_WIDTH / 2)
|
|
|
|
|
.set("y2", IMAGE_WIDTH as f32 / 2.0 + marker_radius);
|
2022-05-21 04:57:20 +00:00
|
|
|
|
|
|
|
|
|
Some(
|
|
|
|
|
Group::new()
|
|
|
|
|
.set("id", "day-parts")
|
|
|
|
|
.set(
|
|
|
|
|
"transform",
|
|
|
|
|
format!(
|
|
|
|
|
"rotate({}, {}, {})",
|
|
|
|
|
utc_rotation,
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH / 2,
|
|
|
|
|
IMAGE_WIDTH / 2
|
2022-05-21 04:57:20 +00:00
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.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),
|
2022-05-19 06:14:01 +00:00
|
|
|
|
)
|
2022-05-21 04:57:20 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2022-05-19 06:14:01 +00:00
|
|
|
|
|
|
|
|
|
let moon_circle = Circle::new()
|
|
|
|
|
.set("class", "moon-background")
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.set("cx", IMAGE_WIDTH / 2)
|
|
|
|
|
.set("cy", IMAGE_WIDTH / 2)
|
2022-05-19 06:14:01 +00:00
|
|
|
|
.set("r", moon_radius);
|
|
|
|
|
|
|
|
|
|
let moon_group = Group::new()
|
|
|
|
|
.set("id", "moon-container")
|
|
|
|
|
.add(moon_circle)
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.add(get_moon_path(moon_radius, moon_phase));
|
2022-05-19 06:14:01 +00:00
|
|
|
|
|
2022-05-17 14:45:01 +00:00
|
|
|
|
let dial = Line::new()
|
|
|
|
|
.set("id", "dial")
|
|
|
|
|
.set("class", "dial")
|
2022-05-17 14:56:26 +00:00
|
|
|
|
.set(
|
|
|
|
|
"transform",
|
|
|
|
|
format!(
|
|
|
|
|
"rotate({}, {}, {})",
|
2022-05-23 15:48:45 +00:00
|
|
|
|
seconds_to_degrees(local_time),
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH / 2,
|
|
|
|
|
IMAGE_WIDTH / 2
|
2022-05-17 14:56:26 +00:00
|
|
|
|
),
|
|
|
|
|
)
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.set("x1", IMAGE_WIDTH / 2)
|
2022-05-22 04:04:32 +00:00
|
|
|
|
.set("y1", IMAGE_WIDTH as f32 / 2.0 + OUTER_R * 0.5)
|
2022-05-22 03:59:49 +00:00
|
|
|
|
.set("x2", IMAGE_WIDTH / 2)
|
2022-05-17 14:56:26 +00:00
|
|
|
|
.set(
|
|
|
|
|
"y2",
|
2022-05-22 04:06:36 +00:00
|
|
|
|
IMAGE_WIDTH as f32 / 2.0 + OUTER_R - RING_WIDTH + HOUR_NAME_FONT_SIZE,
|
2022-05-17 14:56:26 +00:00
|
|
|
|
);
|
2022-05-17 14:45:01 +00:00
|
|
|
|
|
2022-05-22 03:59:49 +00:00
|
|
|
|
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;
|
2022-05-19 04:19:38 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
)));
|
|
|
|
|
|
2022-05-21 04:58:28 +00:00
|
|
|
|
let top_pos = if (6..=18).contains(&local_hour) {
|
2022-05-19 04:19:38 +00:00
|
|
|
|
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({}, {})",
|
2022-05-22 03:59:49 +00:00
|
|
|
|
IMAGE_WIDTH as f32 / 2.0 - current_box_width / 2.0,
|
|
|
|
|
IMAGE_WIDTH as f32 / 2.0 + top_pos
|
2022-05-19 04:19:38 +00:00
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.add(current_hour_rect)
|
|
|
|
|
.add(current_hour_name)
|
|
|
|
|
.add(current_time_text);
|
|
|
|
|
|
2022-05-21 04:57:20 +00:00
|
|
|
|
let mut document = Document::new()
|
2022-05-17 14:45:01 +00:00
|
|
|
|
.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)
|
2022-05-21 04:57:20 +00:00
|
|
|
|
.add(local_clock);
|
|
|
|
|
|
|
|
|
|
if let Some(..) = day_parts_group {
|
|
|
|
|
document = document.add(day_parts_group.unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document
|
2022-05-18 03:44:07 +00:00
|
|
|
|
.add(seasonal_clock)
|
2022-05-17 15:23:26 +00:00
|
|
|
|
.add(moon_group)
|
2022-05-17 14:45:01 +00:00
|
|
|
|
.add(dial)
|
2022-05-19 04:19:38 +00:00
|
|
|
|
.add(current_hour_group)
|
2022-05-17 14:45:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-16 13:15:34 +00:00
|
|
|
|
fn main() {
|
2022-05-21 04:29:39 +00:00
|
|
|
|
let xdg_dirs = xdg::BaseDirectories::new().unwrap();
|
|
|
|
|
let config_path = xdg_dirs
|
|
|
|
|
.place_config_file("seasonal-clock.toml")
|
|
|
|
|
.expect("cannot create configuration directory");
|
2022-05-21 04:57:20 +00:00
|
|
|
|
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
|
|
|
|
|
};
|
2022-05-21 04:29:39 +00:00
|
|
|
|
|
2022-05-19 11:52:31 +00:00
|
|
|
|
let (env, display, queue) = sctk::new_default_environment!(SeasonalClock, desktop)
|
2022-05-16 13:15:34 +00:00
|
|
|
|
.expect("Unable to connect to a Wayland compositor");
|
|
|
|
|
|
|
|
|
|
let surface = env
|
|
|
|
|
.create_surface_with_scale_callback(|dpi, _surface, _dispatch_data| {
|
|
|
|
|
println!("dpi changed to {}", dpi);
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
|
|
|
|
|
let mut next_action = None::<WEvent>;
|
|
|
|
|
|
|
|
|
|
let mut window = env
|
|
|
|
|
.create_window::<FallbackFrame, _>(
|
|
|
|
|
surface,
|
|
|
|
|
None,
|
|
|
|
|
(100, 100),
|
|
|
|
|
move |evt, mut dispatch_data| {
|
|
|
|
|
let next_action = dispatch_data.get::<Option<WEvent>>().unwrap();
|
|
|
|
|
let replace = matches!(
|
|
|
|
|
(&evt, &*next_action),
|
|
|
|
|
(_, &None)
|
|
|
|
|
| (_, &Some(WEvent::Refresh))
|
|
|
|
|
| (&WEvent::Configure { .. }, &Some(WEvent::Configure { .. }))
|
|
|
|
|
| (&WEvent::Close, _)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if replace {
|
|
|
|
|
*next_action = Some(evt);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.expect("Failed to create a window !");
|
|
|
|
|
|
|
|
|
|
window.set_title("Seasonal Hours Clock".to_string());
|
|
|
|
|
|
2022-05-17 14:56:26 +00:00
|
|
|
|
let mut pool = env
|
|
|
|
|
.create_auto_pool()
|
|
|
|
|
.expect("Failed to create the memory pool.");
|
2022-05-16 13:15:34 +00:00
|
|
|
|
let mut need_redraw = false;
|
2022-05-17 14:45:01 +00:00
|
|
|
|
let mut dimensions = (700, 700);
|
2022-05-16 13:15:34 +00:00
|
|
|
|
|
2022-05-23 03:01:25 +00:00
|
|
|
|
let hour_name_path_cache = cache_hour_name_paths();
|
|
|
|
|
|
2022-05-16 13:15:34 +00:00
|
|
|
|
if !env.get_shell().unwrap().needs_configure() {
|
2022-05-23 03:01:25 +00:00
|
|
|
|
redraw(
|
|
|
|
|
&mut pool,
|
|
|
|
|
window.surface(),
|
|
|
|
|
dimensions,
|
|
|
|
|
&config,
|
|
|
|
|
&hour_name_path_cache,
|
|
|
|
|
)
|
|
|
|
|
.expect("Failed to draw");
|
2022-05-16 13:15:34 +00:00
|
|
|
|
window.refresh()
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 11:52:31 +00:00
|
|
|
|
let mut event_loop = EventLoop::<Option<WEvent>>::try_new().unwrap();
|
|
|
|
|
let handle = event_loop.handle();
|
|
|
|
|
let source = Timer::new().expect("Failed to create timer event source!");
|
|
|
|
|
let timer_handle = source.handle();
|
|
|
|
|
timer_handle.add_timeout(std::time::Duration::from_secs(1), "");
|
|
|
|
|
|
|
|
|
|
handle
|
|
|
|
|
.insert_source(source, |_, timer_handle, event| {
|
|
|
|
|
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();
|
2022-05-17 14:45:01 +00:00
|
|
|
|
|
2022-05-16 13:15:34 +00:00
|
|
|
|
loop {
|
2022-05-17 14:45:01 +00:00
|
|
|
|
// Update every second
|
2022-05-16 13:15:34 +00:00
|
|
|
|
match next_action.take() {
|
|
|
|
|
Some(WEvent::Close) => break,
|
|
|
|
|
Some(WEvent::Refresh) => {
|
2022-05-23 03:01:25 +00:00
|
|
|
|
redraw(
|
|
|
|
|
&mut pool,
|
|
|
|
|
window.surface(),
|
|
|
|
|
dimensions,
|
|
|
|
|
&config,
|
|
|
|
|
&hour_name_path_cache,
|
|
|
|
|
)
|
|
|
|
|
.expect("Failed to draw");
|
2022-05-16 13:15:34 +00:00
|
|
|
|
window.refresh();
|
|
|
|
|
window.surface().commit();
|
|
|
|
|
}
|
2022-05-17 14:56:26 +00:00
|
|
|
|
Some(WEvent::Configure {
|
|
|
|
|
new_size,
|
|
|
|
|
states: _,
|
|
|
|
|
}) => {
|
2022-05-16 13:15:34 +00:00
|
|
|
|
if let Some((w, h)) = new_size {
|
|
|
|
|
if dimensions != (w, h) {
|
|
|
|
|
dimensions = (w, h);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
window.resize(dimensions.0, dimensions.1);
|
|
|
|
|
window.refresh();
|
|
|
|
|
need_redraw = true;
|
|
|
|
|
}
|
|
|
|
|
None => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if need_redraw {
|
|
|
|
|
need_redraw = false;
|
|
|
|
|
|
2022-05-23 03:01:25 +00:00
|
|
|
|
redraw(
|
|
|
|
|
&mut pool,
|
|
|
|
|
window.surface(),
|
|
|
|
|
dimensions,
|
|
|
|
|
&config,
|
|
|
|
|
&hour_name_path_cache,
|
|
|
|
|
)
|
|
|
|
|
.expect("Failed to draw")
|
2022-05-16 13:15:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-17 14:45:01 +00:00
|
|
|
|
if let Err(e) = display.flush() {
|
|
|
|
|
if e.kind() != ::std::io::ErrorKind::WouldBlock {
|
|
|
|
|
eprintln!("Error while trying to flush the wayland socket: {:?}", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 11:52:31 +00:00
|
|
|
|
event_loop.dispatch(None, &mut next_action).unwrap();
|
2022-05-16 13:15:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn redraw(
|
|
|
|
|
pool: &mut AutoMemPool,
|
|
|
|
|
surface: &wl_surface::WlSurface,
|
|
|
|
|
(buf_x, buf_y): (u32, u32),
|
2022-05-21 04:57:20 +00:00
|
|
|
|
config: &Option<Config>,
|
2022-05-23 11:45:14 +00:00
|
|
|
|
hour_name_path_cache: &[(PathData, PathData); 24],
|
2022-05-16 13:15:34 +00:00
|
|
|
|
) -> Result<(), ::std::io::Error> {
|
2022-05-23 03:01:25 +00:00
|
|
|
|
let document = gen_svg(config, hour_name_path_cache);
|
2022-05-22 17:01:48 +00:00
|
|
|
|
let svg_tree = svg_to_usvg(document);
|
2022-05-17 14:45:01 +00:00
|
|
|
|
|
2022-05-16 13:15:34 +00:00
|
|
|
|
let (canvas, new_buffer) = pool.buffer(
|
|
|
|
|
buf_x as i32,
|
|
|
|
|
buf_y as i32,
|
|
|
|
|
4 * buf_x as i32,
|
|
|
|
|
wl_shm::Format::Argb8888,
|
|
|
|
|
)?;
|
|
|
|
|
|
2022-05-17 14:45:01 +00:00
|
|
|
|
let image_size = ::std::cmp::min(buf_x, buf_y);
|
|
|
|
|
let move_x = (buf_x - image_size) as f32 / 2.0;
|
|
|
|
|
let move_y = (buf_y - image_size) as f32 / 2.0;
|
|
|
|
|
let mut pixmap = tiny_skia::Pixmap::new(buf_x, buf_y).unwrap();
|
2022-05-18 15:18:30 +00:00
|
|
|
|
// pre-fill the pixmap with our background color so we don’t have a white strip at the edges
|
|
|
|
|
pixmap.fill(
|
|
|
|
|
tiny_skia::Color::from_rgba(19f32 / 255f32, 17f32 / 255f32, 30f32 / 255f32, 1f32).unwrap(),
|
|
|
|
|
);
|
2022-05-17 14:56:26 +00:00
|
|
|
|
resvg::render(
|
|
|
|
|
&svg_tree,
|
|
|
|
|
usvg::FitTo::Size(image_size, image_size),
|
|
|
|
|
tiny_skia::Transform::from_translate(move_x, move_y),
|
|
|
|
|
pixmap.as_mut(),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2022-05-16 13:15:34 +00:00
|
|
|
|
// We do not have anything to draw yet, so draw an empty surface
|
2022-05-17 14:45:01 +00:00
|
|
|
|
for (dst_pixel, src_pixel) in canvas.chunks_exact_mut(4).zip(pixmap.pixels()) {
|
|
|
|
|
let r = src_pixel.red() as u32;
|
|
|
|
|
let g = src_pixel.green() as u32;
|
|
|
|
|
let b = src_pixel.blue() as u32;
|
|
|
|
|
let a = src_pixel.alpha() as u32;
|
|
|
|
|
|
|
|
|
|
let r = ::std::cmp::min(0xFF, (0xFF * (0xFF - a) + a * r) / 0xFF);
|
|
|
|
|
let g = ::std::cmp::min(0xFF, (0xFF * (0xFF - a) + a * g) / 0xFF);
|
|
|
|
|
let b = ::std::cmp::min(0xFF, (0xFF * (0xFF - a) + a * b) / 0xFF);
|
|
|
|
|
|
|
|
|
|
let pixel: [u8; 4] = ((0xFF << 24) + (r << 16) + (g << 8) + b).to_ne_bytes();
|
|
|
|
|
|
|
|
|
|
dst_pixel[0] = pixel[0];
|
|
|
|
|
dst_pixel[1] = pixel[1];
|
|
|
|
|
dst_pixel[2] = pixel[2];
|
|
|
|
|
dst_pixel[3] = pixel[3];
|
2022-05-16 13:15:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
surface.attach(Some(&new_buffer), 0, 0);
|
|
|
|
|
|
|
|
|
|
if surface.as_ref().version() >= 4 {
|
|
|
|
|
surface.damage_buffer(0, 0, buf_x as i32, buf_y as i32);
|
|
|
|
|
} else {
|
|
|
|
|
surface.damage(0, 0, buf_x as i32, buf_y as i32);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
surface.commit();
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|