#pragma once #include #include #include #include "services/textureCache.hpp" #include "widgets/weather.hpp" #include "gtkmm/box.h" #include "gtkmm/enums.h" #include "gtkmm/image.h" std::string formatTemperature(double temp) { return std::to_string(static_cast(std::round(temp))) + "°C"; } class WeatherInfo : public Gtk::Box { private: Gtk::Image getImageFromWeatherCode(int weather_code, bool is_daytime = true) { Gtk::Image pic; auto file = std::ifstream("resources/weatherCodes.json"); if (!file.is_open()) { auto homeDir = std::getenv("HOME"); std::cout << "Failed to open local weatherCodes.json, trying " << std::string(homeDir) + "/.config/bar/weatherCodes.json" << std::endl; file = std::ifstream(std::string(homeDir) + "/.config/bar/weatherCodes.json"); if (!file.is_open()) { std::cerr << "Failed to open weatherCodes.json" << std::endl; return pic; } } auto json = nlohmann::json::parse(file); file.close(); std::string image_path = json[std::to_string(weather_code)][is_daytime ? "day" : "night"]["image"].get(); auto texture = TextureCacheService::getInstance()->getTexture(image_path); if (texture) { pic.set(texture); } pic.set_pixel_size(64); return pic; } public: WeatherInfo(WeatherWidget::CurrentWeather current_weather) { set_orientation(Gtk::Orientation::HORIZONTAL); this->set_valign(Gtk::Align::CENTER); this->set_halign(Gtk::Align::FILL); auto image = getImageFromWeatherCode(current_weather.weather_code); auto temp = Gtk::Label(); temp.set_markup("" + formatTemperature(current_weather.temperature) + "" + " (" + formatTemperature(current_weather.apparent_temperature) + ")"); temp.set_halign(Gtk::Align::START); auto temp_box = Gtk::Box(Gtk::Orientation::VERTICAL); temp_box.set_valign(Gtk::Align::CENTER); temp_box.set_halign(Gtk::Align::START); temp_box.append(temp); append(image); append(temp_box); } WeatherInfo(WeatherWidget::HourlyWeather hourly_weather) { set_orientation(Gtk::Orientation::HORIZONTAL); this->set_valign(Gtk::Align::CENTER); add_css_class("weather-info"); auto image = getImageFromWeatherCode(hourly_weather.weather_code); auto temp = Gtk::Label(); temp.set_markup("" + formatTemperature(hourly_weather.temperature_2m) + "" + " (" + formatTemperature(hourly_weather.apparent_temperature) + ")"); temp.set_halign(Gtk::Align::START); Gtk::Box temp_box(Gtk::Orientation::VERTICAL); temp_box.set_halign(Gtk::Align::START); temp_box.set_valign(Gtk::Align::CENTER); temp_box.append(temp); auto timeLabel = Gtk::Label(); timeLabel.set_markup("" + hourly_weather.time + ""); timeLabel.set_halign(Gtk::Align::START); temp_box.append(timeLabel); append(image); append(temp_box); } WeatherInfo(WeatherWidget::DailyWeather daily_weather) { set_orientation(Gtk::Orientation::HORIZONTAL); this->set_valign(Gtk::Align::CENTER); add_css_class("weather-info"); auto image = getImageFromWeatherCode(daily_weather.weather_code); auto max_label = Gtk::Label(); max_label.set_markup("H: " + formatTemperature(daily_weather.temperature_2m_max) + "" + " (" + formatTemperature(daily_weather.apparent_temperature_max) + ")"); max_label.set_halign(Gtk::Align::START); auto min_label = Gtk::Label(); min_label.set_markup("L: " + formatTemperature(daily_weather.temperature_2m_min) + "" + " (" + formatTemperature(daily_weather.apparent_temperature_min) + ")"); min_label.set_halign(Gtk::Align::START); Gtk::Box temp_box(Gtk::Orientation::VERTICAL); temp_box.set_halign(Gtk::Align::START); temp_box.set_valign(Gtk::Align::CENTER); temp_box.append(max_label); temp_box.append(min_label); auto timeLabel = Gtk::Label(); timeLabel.set_markup("" + daily_weather.time + ""); timeLabel.set_halign(Gtk::Align::START); temp_box.append(timeLabel); append(image); append(temp_box); } };