11use commons:: prelude:: * ;
2+ use futures:: { StreamExt , channel:: mpsc} ;
23use gpui:: * ;
34use status_bar:: prelude:: * ;
5+ use status_bar:: services:: * ;
6+ use std:: time:: Duration ;
7+ use upower:: interfaces:: device:: BatteryState ;
48
59fn main ( ) {
610 let application = gpui:: Application :: new ( ) . with_assets ( Assets { } ) ;
@@ -13,7 +17,98 @@ fn main() {
1317 window_bounds : Some ( window_bounds) ,
1418 ..Default :: default ( )
1519 } ,
16- |_window, cx| cx. new ( |_cx| StatusBar :: new ( ) ) ,
20+ |_window, cx| {
21+ let ( app_channel_tx, mut app_channel_rx) = mpsc:: channel :: < AppEvents > ( 120 ) ;
22+ let executor = cx. background_executor ( ) ;
23+
24+ executor
25+ . spawn ( sync_network_status ( app_channel_tx. clone ( ) ) )
26+ . detach ( ) ;
27+ executor
28+ . spawn ( sync_network_strength ( app_channel_tx. clone ( ) ) )
29+ . detach ( ) ;
30+
31+ executor
32+ . spawn ( sync_bluetooth_status ( app_channel_tx. clone ( ) ) )
33+ . detach ( ) ;
34+ executor
35+ . spawn ( sync_bluetooth_connected_status ( app_channel_tx. clone ( ) ) )
36+ . detach ( ) ;
37+
38+ executor
39+ . spawn ( sync_battery_state ( app_channel_tx. clone ( ) ) )
40+ . detach ( ) ;
41+ executor
42+ . spawn ( sync_battery_level ( app_channel_tx. clone ( ) ) )
43+ . detach ( ) ;
44+
45+ cx. new ( |cx| {
46+ cx. spawn ( async move |app, cx| {
47+ while let Some ( event) = app_channel_rx. next ( ) . await {
48+ match event {
49+ AppEvents :: WirelessStatusChanged { enabled } => {
50+ let _ = app. update ( cx, |this : & mut StatusBar , cx| {
51+ this. wireless_enabled = enabled;
52+ cx. notify ( ) ;
53+ } ) ;
54+ }
55+ AppEvents :: WirelessStrength { strength } => {
56+ let _ = app. update ( cx, |this : & mut StatusBar , cx| {
57+ this. wireless_strength = strength;
58+ cx. notify ( ) ;
59+ } ) ;
60+ }
61+ AppEvents :: BluetoothEnabled { enabled } => {
62+ let _ = app. update ( cx, |this : & mut StatusBar , cx| {
63+ this. bluetooth_enabled = enabled;
64+ cx. notify ( ) ;
65+ } ) ;
66+ }
67+ AppEvents :: BluetoothConnectionStatus { connected } => {
68+ let _ = app. update ( cx, |this : & mut StatusBar , cx| {
69+ this. bluetooth_connected = connected;
70+ cx. notify ( ) ;
71+ } ) ;
72+ }
73+ AppEvents :: BatteryStateChanged { state } => {
74+ let _ = app. update ( cx, |this : & mut StatusBar , cx| {
75+ this. battery_state = match state {
76+ BatteryState :: Charging => BatteryState :: Charging ,
77+ BatteryState :: Discharging => BatteryState :: Discharging ,
78+ _ => BatteryState :: Unknown ,
79+ } ;
80+ cx. notify ( ) ;
81+ } ) ;
82+ }
83+ AppEvents :: BatteryLevelChanged { level } => {
84+ let _ = app. update ( cx, |this : & mut StatusBar , cx| {
85+ this. battery_level = level;
86+ cx. notify ( ) ;
87+ } ) ;
88+ }
89+ AppEvents :: TimeUpdated => {
90+ let _ = app. update ( cx, |this : & mut StatusBar , cx| {
91+ this. update_time ( cx) ;
92+ } ) ;
93+ }
94+ }
95+ }
96+ } )
97+ . detach ( ) ;
98+
99+ cx. spawn ( async move |app, cx| {
100+ loop {
101+ cx. background_executor ( ) . timer ( Duration :: from_secs ( 1 ) ) . await ;
102+ let _ = app. update ( cx, |this : & mut StatusBar , cx| {
103+ this. update_time ( cx) ;
104+ } ) ;
105+ }
106+ } )
107+ . detach ( ) ;
108+
109+ StatusBar :: new ( )
110+ } )
111+ } ,
17112 )
18113 . unwrap ( ) ;
19114 cx. activate ( true ) ;
0 commit comments