night-light: replace blocking sync calls with async

parent 4a898d03
...@@ -2,6 +2,7 @@ namespace XimperShellNotificationCenter.Widgets { ...@@ -2,6 +2,7 @@ namespace XimperShellNotificationCenter.Widgets {
public class NightLightTile : QuickSettingsTile { public class NightLightTile : QuickSettingsTile {
private const int NEUTRAL_TEMP = 6000; private const int NEUTRAL_TEMP = 6000;
private int temperature = 4500; private int temperature = 4500;
private bool _is_available = false;
public NightLightTile (Json.Object ?cfg) { public NightLightTile (Json.Object ?cfg) {
base ("night-light-disabled-symbolic", base ("night-light-disabled-symbolic",
...@@ -14,40 +15,31 @@ namespace XimperShellNotificationCenter.Widgets { ...@@ -14,40 +15,31 @@ namespace XimperShellNotificationCenter.Widgets {
temperature = temp; temperature = temp;
} }
} }
_is_available =
Environment.find_program_in_path (
"hyprsunset") != null;
} }
public override bool is_available () { public override bool is_available () {
try { return _is_available;
string output;
Process.spawn_command_line_sync (
"pgrep -x hyprsunset",
out output, null, null);
return output.strip ().length > 0;
} catch (SpawnError e) {
return false;
}
} }
public override void on_cc_visibility_change ( public override void on_cc_visibility_change (
bool visible) { bool visible) {
if (visible && is_available ()) { if (visible && _is_available) {
sync_state (); sync_state.begin ();
} }
} }
private void sync_state () { private async void sync_state () {
try { string msg;
string output; yield Functions.execute_command (
Process.spawn_command_line_sync ( "hyprctl hyprsunset temperature",
"hyprctl hyprsunset temperature", {}, out msg);
out output, null, null); int current = int.parse (msg.strip ());
int current = int.parse (output.strip ()); active = (current < NEUTRAL_TEMP);
active = (current < NEUTRAL_TEMP); update_icon ();
update_icon ();
} catch (SpawnError e) {
debug ("Failed to get temperature: %s",
e.message);
}
} }
public override void on_toggle () { public override void on_toggle () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment