Skip to content

Commit

Permalink
Add Sunrise/Sunset graph
Browse files Browse the repository at this point in the history
  • Loading branch information
lainsce committed Jan 29, 2024
1 parent dc06d64 commit d4429b9
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 22 deletions.
75 changes: 53 additions & 22 deletions data/ui/weatherpage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -118,32 +118,36 @@
<property name="title">Summary</property>
<property name="child">
<object class="GtkBox">
<property name="orientation">vertical</property>
<child>
<object class="GtkScrolledWindow">
<property name="vscrollbar_policy">never</property>
<object class="GtkBox">
<property name="orientation">vertical</property>
<child>
<object class="GtkBox" id="graph">
<property name="vexpand_set">1</property>
<property name="orientation">vertical</property>
<object class="GtkScrolledWindow">
<property name="vscrollbar_policy">never</property>
<child>
<object class="GtkBox" id="graph">
<property name="vexpand_set">1</property>
<property name="orientation">vertical</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="HeDivider">
</object>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="vscrollbar_policy">never</property>
<child>
<object class="GtkBox" id="humidity_timeline">
<property name="orientation">horizontal</property>
<property name="spacing">24</property>
<property name="margin-start">18</property>
<property name="margin-end">18</property>
<property name="vexpand_set">1</property>
<object class="HeDivider">
</object>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="vscrollbar_policy">never</property>
<child>
<object class="GtkBox" id="humidity_timeline">
<property name="orientation">horizontal</property>
<property name="spacing">24</property>
<property name="margin-start">18</property>
<property name="margin-end">18</property>
<property name="vexpand_set">1</property>
</object>
</child>
</object>
</child>
</object>
Expand Down Expand Up @@ -271,7 +275,34 @@
</object>
</child>
<child>

<object class="GtkBox">
<property name="orientation">vertical</property>
<property name="halign">start</property>
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="hexpand">1</property>
<property name="valign">center</property>
<property name="label">Sunrise / Sunset:</property>
<style>
<class name="cb-subtitle"/>
</style>
</object>
</child>
<child>
<object class="GtkBox" id="sunrise_sunset">
<property name="orientation">horizontal</property>
<property name="spacing">24</property>
<property name="halign">end</property>
<property name="margin-start">18</property>
<property name="margin-end">18</property>
<property name="vexpand_set">1</property>
<child>
<object class="GtkDrawingArea" id="da_sun"></object>
</child>
</object>
</child>
</object>
</child>
</object>
</property>
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ asresources = gnome.compile_resources(
sources = [
'src/Application.vala',
'src/MainWindow.vala',
'src/SunGraph.vala',
'src/WeatherPage.vala',
'src/WeatherGraph.vala',
'src/WeatherHumidityRow.vala',
Expand Down
69 changes: 69 additions & 0 deletions src/SunGraph.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
public class Kairos.SunGraph : He.Bin {
private DateTime sunrise { get; set; }
private DateTime sunset { get; set; }
private WeatherPage? weather_page { get; set; }

public SunGraph (WeatherPage weather_page, DateTime sunrise, DateTime sunset) {
this.weather_page = weather_page;
this.sunrise = sunrise;
this.sunset = sunset;

weather_page.da_sun.content_height = 120;
weather_page.da_sun.content_width = 200;
weather_page.da_sun.vexpand = true;
weather_page.da_sun.halign = Gtk.Align.CENTER;
weather_page.da_sun.valign = Gtk.Align.CENTER;
weather_page.da_sun.set_size_request (360, 180);
weather_page.da_sun.set_draw_func (draw_stuff);
}

private void draw_stuff (Gtk.DrawingArea da_sun,
Cairo.Context cr, int x, int y) {
var now = new DateTime.now_local ();

draw (cr, sunrise, sunset, now);
}

private void draw (Cairo.Context cr, DateTime sunrise, DateTime sunset, DateTime current_time) {
int center_x = 140;
int center_y = 100;
int radius = 90;

double start_angle = Math.PI;
double end_angle = 0;
double total_seconds = sunset.to_unix() - sunrise.to_unix();
double elapsed_seconds = current_time.to_unix() - sunrise.to_unix();
double angle = start_angle + (elapsed_seconds / total_seconds) * (end_angle - start_angle);

cr.arc(center_x, center_y, radius, start_angle, end_angle);
cr.set_source_rgba(1, 1, 1, 0.80);
cr.stroke();

if (current_time.get_hour () >= sunrise.get_hour () && current_time.get_hour () <= sunset.get_hour ()) {
double dot_x = center_x + radius * Math.cos(angle);
double dot_y = center_y - radius * Math.sin(angle);

cr.arc(dot_x, dot_y, 10, 0, 2 * Math.PI);
cr.set_source_rgba(1, 1, 1, 0.95);
cr.fill();
} else {
double dot_x = center_x + radius * Math.sin(angle);
double dot_y = center_y - radius * Math.cos(angle);

cr.arc(-dot_x, -dot_y, 10, 0, 2 * Math.PI);
cr.set_source_rgba(1, 1, 1, 0.95);
cr.fill();
}

double label_margin = 5;

cr.move_to(center_x - radius * Math.cos(start_angle) - 3*label_margin, center_y - radius * Math.sin(start_angle) + 3*label_margin);
cr.set_source_rgba(1, 1, 1, 0.95);
cr.set_antialias (Cairo.Antialias.GRAY);
cr.set_font_size(12);
cr.show_text(sunset.format("%H:%M"));

cr.move_to(center_x - radius * Math.cos(end_angle) - 3*label_margin, center_y - radius * Math.sin(end_angle) + 3*label_margin);
cr.show_text(sunrise.format("%H:%M"));
}
}
19 changes: 19 additions & 0 deletions src/WeatherPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public class Kairos.WeatherPage : He.Bin {
[GtkChild]
unowned Gtk.Box graph;
[GtkChild]
unowned Gtk.Box sunrise_sunset;
[GtkChild]
public unowned Gtk.DrawingArea da_sun;
[GtkChild]
unowned He.DisclosureButton refresh_button;

public WeatherPage (MainWindow win, GWeather.Location location) {
Expand Down Expand Up @@ -148,6 +152,10 @@ public class Kairos.WeatherPage : He.Bin {
var entry = new WeatherGraph (hourlyinfo);
graph.prepend (entry);
}
public void add_sun_graph (WeatherPage wp, DateTime sunrise, DateTime sunset) {
var entry = new SunGraph (wp, sunrise, sunset);
sunrise_sunset.prepend (entry);
}
public void update_timeline (GWeather.Info info) {
var forecasts = info.get_forecast_list ().copy ();
var now = new GLib.DateTime.now_local ();
Expand All @@ -166,6 +174,17 @@ public class Kairos.WeatherPage : He.Bin {
}
add_graph (hourlyinfo);
has_forecast_info = true;

// Sunrise & Sunset
ulong sunrise;
info.get_value_sunrise (out sunrise);
var sunrise_time = new GLib.DateTime.from_unix_local (sunrise);
ulong sunset;
info.get_value_sunset (out sunset);
var sunset_time = new GLib.DateTime.from_unix_local (sunset);

add_sun_graph (this, sunrise_time, sunset_time);
has_forecast_info = true;
}

this.hourly_info = hourlyinfo.copy ();
Expand Down

0 comments on commit d4429b9

Please sign in to comment.