Skip to content

Commit

Permalink
countdown
Browse files Browse the repository at this point in the history
  • Loading branch information
hoothin committed May 30, 2023
1 parent 1021df4 commit 33b791c
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 44 deletions.
2 changes: 1 addition & 1 deletion 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 @@ -17,7 +17,7 @@ members = [
name = "rust_clock"
authors = ["Hoothin <rixixi@gmail.com>"]
license = "MIT"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
description = "Clock popup every half hour"
build = "build.rs"
Expand Down
3 changes: 2 additions & 1 deletion conf.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[Config]
time=:30:,:00:
#sound=sound.ogg
#sound=sound.ogg
countdown=::30,::10
175 changes: 134 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ use rodio::{Decoder, OutputStream, Sink};
fn main() -> Result<(), eframe::Error> {
let result = fs::read_to_string("conf.ini");
if let Err(_) = result {
fs::write("conf.ini", "[Config]\ntime=:30:,:00:\n#sound=assets/sound.ogg").unwrap()
fs::write("conf.ini", "[Config]\ntime=:30:,:00:\n#sound=assets/sound.ogg\n#countdown=:20:,::20").unwrap()
}

let i = Ini::load_from_file("conf.ini").unwrap();
let mut time_str = "".to_string();
let mut sound_path = "".to_string();
let mut countdown = "".to_string();
for (sec, prop) in i.iter() {
if let Some(s) = sec {
if s == "Config" {
Expand All @@ -30,6 +31,8 @@ fn main() -> Result<(), eframe::Error> {
time_str = v.to_string();
} else if k == "sound" {
sound_path = v.to_string();
} else if k == "countdown" {
countdown = v.to_string();
}
}
}
Expand All @@ -40,6 +43,7 @@ fn main() -> Result<(), eframe::Error> {

let tray_menu = Menu::new();
let quit_i = MenuItem::new("Quit", true, None);
let countdown_i = MenuItem::new("Countdown", true, None);
tray_menu.append_items(&[
&PredefinedMenuItem::about(
None,
Expand All @@ -50,6 +54,8 @@ fn main() -> Result<(), eframe::Error> {
}),
),
&PredefinedMenuItem::separator(),
&countdown_i,
&PredefinedMenuItem::separator(),
&quit_i,
]);

Expand All @@ -76,18 +82,20 @@ fn main() -> Result<(), eframe::Error> {
"Rust clock", // unused title
options,
Box::new(move |_cc| Box::new(MyApp{
quit: quit_i.id(),
quit_index: quit_i.id(),
visible: true,
time2show: time_str,
sound_path: sound_path,
countdown: countdown,
countdown_index: countdown_i.id(),
..MyApp::default()
})),
)
}

#[derive(Default)]
struct MyApp {
quit: u32,
quit_index: u32,
time: f32,
time2show: String,
tikpop: bool,
Expand All @@ -96,7 +104,11 @@ struct MyApp {
last_pos_y: f32,
last_visible: bool,
sound_path: String,
inited: bool
countdown: String,
countdown_index: u32,
inited: bool,
countdown_start: bool,
countdown_start_time: i64
}

impl eframe::App for MyApp {
Expand All @@ -121,7 +133,89 @@ impl eframe::App for MyApp {
.insert(0, "my_font".to_owned());
ctx.set_fonts(fonts);
}
clock_window_frame(ctx, frame, self);
let mut begin_tik = || {
self.last_visible = self.visible;
if let Some(pos) = frame.get_window_pos() {
self.last_pos_x = pos.x;
self.last_pos_y = pos.y;
}
self.visible = true;
frame.set_visible(true);
self.time = 0.0;
frame.set_window_pos(Pos2::new(init_x, init_y));
if self.sound_path != "" {
let result = fs::File::open(&self.sound_path);
if let Ok(file) = result {
let file = BufReader::new(file);
std::thread::spawn(move || {
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let source = Decoder::new(file).unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
sink.append(source);
sink.sleep_until_end();
});
}
}
ctx.request_repaint_after(std::time::Duration::from_millis(16));
};
let mut custom_clock = "".to_string();
if self.countdown_start == true {
let mut over_time = (Local::now().timestamp() - self.countdown_start_time) as i32;
if self.countdown == "" {
if over_time > 600 {
self.countdown_start_time = Local::now().timestamp();
if self.tikpop == false {
begin_tik();
self.tikpop = true;
}
}
let left_time = 600.0 - over_time as f32;
let minute = (left_time / 60.0) as u32;
let second = (left_time % 60.0) as u32;
custom_clock = format!("00:{:02}:{:02}", minute, second);
} else {
let countdown_arr: Vec<&str> = self.countdown.split(',').collect();
let mut total_time:i32 = 0;
for x in &countdown_arr {
let single_time: Vec<&str> = x.split(':').collect();
let mut cur_time:i32 = 0;
if single_time[0] != "" {
cur_time = cur_time + single_time[0].to_string().parse::<i32>().unwrap() * 3600;
}
if single_time[1] != "" {
cur_time = cur_time + single_time[1].to_string().parse::<i32>().unwrap() * 60;
}
if single_time[2] != "" {
cur_time = cur_time + single_time[2].to_string().parse::<i32>().unwrap();
}
total_time = total_time + cur_time;
if self.tikpop == false && over_time == total_time.into() {
begin_tik();
self.tikpop = true;
} else if over_time < total_time {
let left_time = (total_time - over_time) as f32;
let hour = (left_time / 60.0 / 60.0) as u32;
let minute = (left_time / 60.0) as u32;
let second = (left_time % 60.0) as u32;
custom_clock = format!("{:02}:{:02}:{:02}", hour, minute, second);
break;
}
}
if custom_clock == "" {
over_time = 0;
self.countdown_start_time = Local::now().timestamp();
let left_time = (total_time - over_time) as f32;
let hour = (left_time / 60.0 / 60.0) as u32;
let minute = (left_time / 60.0) as u32;
let second = (left_time % 60.0) as u32;
custom_clock = format!("{:02}:{:02}:{:02}", hour, minute, second);
if self.tikpop == false {
begin_tik();
self.tikpop = true;
}
}
}
}
if self.tikpop == true {
self.time += 2.0;
frame.set_mouse_passthrough(false);
Expand Down Expand Up @@ -153,38 +247,19 @@ impl eframe::App for MyApp {
let time2show_arr: Vec<&str> = self.time2show.split(',').collect();
for x in &time2show_arr {
let single_time: Vec<&str> = x.split(':').collect();
if (single_time[0] == "" || single_time[0] == hour) &&
(single_time[1] == "" || single_time[1] == minute) &&
((single_time[2] == "" && second == "0") || single_time[2] == second) {
self.last_visible = self.visible;
if let Some(pos) = frame.get_window_pos() {
self.last_pos_x = pos.x;
self.last_pos_y = pos.y;
if (single_time[0] == "" || single_time[0] == hour || single_time[0] == "0".to_string() + &hour) &&
(single_time[1] == "" || single_time[1] == minute || single_time[1] == "0".to_string() + &minute) &&
((single_time[2] == "" && (second == "0" || second == "00")) || single_time[2] == second) {
if self.tikpop == false {
begin_tik();
self.tikpop = true;
}
self.visible = true;
self.tikpop = true;
frame.set_visible(true);
self.time = 0.0;
frame.set_window_pos(Pos2::new(init_x, init_y));
if self.sound_path != "" {
let result = fs::File::open(&self.sound_path);
if let Ok(file) = result {
let file = BufReader::new(file);
std::thread::spawn(move || {
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let source = Decoder::new(file).unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
sink.append(source);
sink.sleep_until_end();
});
}
}
ctx.request_repaint_after(std::time::Duration::from_millis(16));
break;
}
}
}
}
clock_window_frame(ctx, frame, self, custom_clock);

if let Ok(TrayEvent {
event: tray_icon::ClickEvent::Left,
Expand All @@ -201,8 +276,15 @@ impl eframe::App for MyApp {
}
}
if let Ok(event) = MenuEvent::receiver().try_recv() {
if event.id == self.quit {
if event.id == self.quit_index {
std::process::exit(0)
} else if event.id == self.countdown_index {
self.countdown_start = !self.countdown_start;
if self.countdown_start == true {
self.visible = true;
frame.set_visible(self.visible);
self.countdown_start_time = Local::now().timestamp();
}
}
}
}
Expand All @@ -211,7 +293,8 @@ impl eframe::App for MyApp {
fn clock_window_frame(
ctx: &egui::Context,
frame: &mut eframe::Frame,
app: &mut MyApp
app: &mut MyApp,
custom_clock: String
) {
use egui::*;
let text_color = ctx.style().visuals.text_color();
Expand Down Expand Up @@ -240,13 +323,23 @@ fn clock_window_frame(
);

// Paint the title:
painter.text(
rect.center_top() + vec2(-41.0, 51.0),
Align2::LEFT_CENTER,
now.format("%H:%M:%S"),
FontId::proportional(50.0),
text_color,
);
if custom_clock == "" {
painter.text(
rect.center_top() + vec2(-41.0, 51.0),
Align2::LEFT_CENTER,
now.format("%H:%M:%S"),
FontId::proportional(50.0),
text_color,
);
} else {
painter.text(
rect.center_top() + vec2(-41.0, 51.0),
Align2::LEFT_CENTER,
custom_clock,
FontId::proportional(50.0),
text_color,
);
}

painter.circle_filled(
Pos2::new(55.0, 50.0),
Expand Down Expand Up @@ -294,7 +387,7 @@ fn clock_window_frame(
if app.tikpop == false {
let close_response = ui.put(
Rect::from_min_size(rect.left_top(), Vec2::splat(28.0)),
Button::new(RichText::new("").size(24.0)).frame(false),
Button::new(RichText::new("").size(16.0)).frame(false),
);
if close_response.clicked() {
frame.set_visible(false);
Expand Down

0 comments on commit 33b791c

Please sign in to comment.