55
66public class MainWindow : Gtk .ApplicationWindow {
77 private Gtk . Stack stack;
8- private GWeather . Location location;
8+ private Gtk . Spinner spinner;
9+ private Gtk . Grid grid;
10+ private Granite . Placeholder placeholder;
11+ private GWeather . Location ? location = null ;
912 private GWeather . Info weather_info;
1013
1114 construct {
12- get_location. begin ();
13-
1415 weather_info = new GWeather .Info (location) {
1516 contact_info
= " [email protected] " 1617 };
@@ -36,30 +37,34 @@ public class MainWindow : Gtk.ApplicationWindow {
3637 valign = Gtk . Align . START
3738 };
3839
39- var grid = new Gtk .Grid () {
40+ grid = new Gtk .Grid () {
4041 column_spacing = 12
4142 };
4243 grid. attach (weather_icon, 0 , 0 , 1 , 2 );
4344 grid. attach (temp_label, 1 , 0 , 1 , 2 );
4445 grid. attach (weather_label, 2 , 0 );
4546 grid. attach (location_label, 2 , 1 );
47+ grid. add_css_class (" weather" );
4648
47- var spinner = new Gtk .Spinner () {
49+ spinner = new Gtk .Spinner () {
4850 halign = Gtk . Align . CENTER ,
4951 vexpand = true ,
5052 spinning = true
5153 };
5254
53- var alert_label = new Gtk .Label (_(" Unable to Get Location" ));
55+ placeholder = new Granite .Placeholder (_(" Unable to Get Location" )) {
56+ icon = new ThemedIcon (" location-disabled-symbolic" ),
57+ description = _ ("Make sure location access is turned on in <a href ='settings ://privacy /location '>System Settings → Security & ; Privacy</ a >")
58+ };
5459
5560 stack = new Gtk .Stack () {
5661 transition_type = Gtk . StackTransitionType . CROSSFADE ,
5762 valign = Gtk . Align . CENTER ,
58- vhomogeneous = true
63+ vhomogeneous = false
5964 };
6065 stack.add_child (spinner );
61- stack. add_named (grid, " weather " );
62- stack. add_named (alert_label, " alert " );
66+ stack.add_child (grid );
67+ stack.add_child ( placeholder );
6368
6469 var window_handle = new Gtk .WindowHandle () {
6570 child = stack
@@ -71,8 +76,18 @@ public class MainWindow : Gtk.ApplicationWindow {
7176 titlebar = new Gtk .Grid () { visible = false };
7277 title = _ ("Nimbus ");
7378
79+ get_location ();
80+
7481 notify["is-active"].connect (() => {
75- weather_info. update ();
82+ if (stack. visible_child == spinner || ! is_active) {
83+ return ;
84+ }
85+
86+ if (location == null ) {
87+ get_location ();
88+ } else {
89+ weather_info. update ();
90+ }
7691 });
7792
7893 weather_info.updated.connect (() => {
@@ -107,29 +122,40 @@ public class MainWindow : Gtk.ApplicationWindow {
107122 });
108123 }
109124
110- public async void get_location () {
111- try {
112- var simple = yield new GClue .Simple (Application . get_default (). application_id, GClue . AccuracyLevel . CITY , null );
125+ private void get_location () {
126+ stack. visible_child = spinner;
127+
128+ get_gclue_simple. begin ((obj, res) = > {
129+ var simple = get_gclue_simple. end (res);
130+ if (simple != null ) {
131+ simple. notify[" location" ]. connect (() = > {
132+ on_location_updated (simple. location. latitude, simple. location. longitude);
133+ });
113134
114- simple. notify[" location" ]. connect (() = > {
115135 on_location_updated (simple. location. latitude, simple. location. longitude);
116- });
136+ } else {
137+ stack. visible_child = placeholder;
138+ }
139+ });
140+ }
117141
118- on_location_updated (simple. location. latitude, simple. location. longitude);
142+ private async GClue . Simple ? get_gclue_simple () {
143+ try {
144+ var simple = yield new GClue .Simple (Application . get_default (). application_id, GClue . AccuracyLevel . CITY , null );
145+ return simple;
119146 } catch (Error e) {
120147 warning (" Failed to connect to GeoClue2 service: %s " , e. message);
121- stack. visible_child_name = " alert" ;
122- return ;
148+ return null ;
123149 }
124150 }
125151
126- public void on_location_updated (double latitude , double longitude ) {
152+ private void on_location_updated (double latitude, double longitude) {
127153 location = GWeather . Location . get_world ();
128154 location = location. find_nearest_city (latitude, longitude);
129155 if (location != null ) {
130156 weather_info. location = location;
131157 weather_info. update ();
132- stack. visible_child_name = " weather " ;
158+ stack. visible_child = grid ;
133159 }
134160 }
135161}
0 commit comments