Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed on auto update frame #51

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ stray = "0.1.3"
glib = "0.17.2"
json = "0.12.4"
gtk = "0.17.0"
regex = "1"

[profile.release]
codegen-units = 1
Expand Down
6 changes: 3 additions & 3 deletions src/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ fn update_cava() -> Continue {
}

if let Ok(has_cava_crashed) = HAS_CAVA_CRASHED.lock() {
glib::Continue(!*has_cava_crashed)
return glib::Continue(!*has_cava_crashed)
} else {
log!(WARN_CAVA_NO_CRASHED_INSTANCE);
glib::Continue(false)
return glib::Continue(false)
}
} else {
log!(WARN_CAVA_NO_BARS_INSTANCE);
glib::Continue(false)
return glib::Continue(false)
}
}
5 changes: 3 additions & 2 deletions src/widgets/button_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ impl HWidget for ButtonWidget {
button.set_tooltip_markup(Some(&new_tooltip));
}

glib::Continue(true)
// INFO: I did the same thing as the label has.
return glib::Continue(true)
};

tick();
// tick();
// NOTE: This does NOT respect update_rate, since it's not meant to update super fast.
glib::timeout_add_local(Duration::from_millis(1000), tick);
}
Expand Down
73 changes: 59 additions & 14 deletions src/widgets/label_widget.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// use regex::Regex;

use crate::{
config,
constants::{
Expand Down Expand Up @@ -95,29 +97,70 @@ fn start_tooltip_loop(label_ref: &mut LabelWidget) {
fn start_label_loop(label_ref: &mut LabelWidget) {
let label = take(&mut label_ref.label);
let command = label_ref.command.to_owned();
if command.is_empty() || label_ref.update_rate <= 3 {
// Not eligible, cancel.
return;
}

// println!("{}", label_ref);;
// TODO: To initiate the update rate, also to modify if the
// update_rate was empty to to set to 500ms automatically.

let mut update_rate: u64 = label_ref.update_rate;
let mut anim_speed: u32 = label_ref.anim_duration;
let text = label_ref.text.to_owned();
let listen = label_ref.listen;
let update_anim = take(&mut label_ref.update_anim).expect(ERR_WRONG_LABEL_RANIM);
let revealer = take(&mut label_ref.revealer);
let anim_speed = label_ref.anim_duration;

if !command.is_empty() && update_rate <= 500 {
update_rate = 500;
}

if command.is_empty() {
// Not eligible, cancel.
return;
}
println!("{}", update_rate);

// INFO: This is just to update the animation speed for less not showing due to animation
// I've experienced some issue that I want to update the result everytime, that's why I added
// this, you may also check my configuration thou.
if update_anim == RevealerTransitionType::Crossfade && update_rate <= 500 && update_rate >= 0 && anim_speed >= 250 {
if update_rate <= u32::MAX as u64 {
anim_speed = 0;
}
}

let tick = move || {
if !listen {
let mut new_text = String::default();
new_text.push_str(&text);
new_text.push_str(&use_aliases(&command));

if !label.text().eq(&new_text) {

// INFO: This will automatically add the %command% for formatting
// If ever that the text is empty.
if new_text.is_empty() && !command.is_empty() {
new_text.push_str("%command%");
}

// INFO: This is to replace the %command% to the executed command
new_text = new_text.replace("%command%", &use_aliases(&command));

// TODO: To use the regex for string formatting

// let pattern = Regex::new(r"%(?P<data>\w+)%").expect("Failed to create regex");
//
// if let Some(capt) = pattern.captures(&new_text) {
// new_text = new_text.replace(format!("%{}%", &capt["data"]), &use_aliases(&label_ref.command.get(capt["data"])))
// }


if !label.text().eq(&new_text) && !new_text.is_empty() {
// NOTE: I'd just used this print function to debug
// I'm still newbie with rust thou.
// println!("Update {}", new_text);
restart_revealer!(
revealer,
|| label.set_text(&new_text),
update_anim,
anim_speed
);
// label.set_text(&new_text);
}
} else {
restart_revealer!(
Expand All @@ -127,12 +170,14 @@ fn start_label_loop(label_ref: &mut LabelWidget) {
anim_speed
);
}

glib::Continue(true)
// NOTE: I don't know what is the reason, but it helps to
// automatic update
return glib::Continue(true);
};

tick();
glib::timeout_add_local(Duration::from_millis(label_ref.update_rate), tick);

// INFO: I've commented this, cause I don't know the reason.
// tick();
glib::timeout_add_local(Duration::from_millis(update_rate), tick);
}

/// Updates the labels content with the string from `BUFFER`.
Expand Down