diff --git a/NEWS.md b/NEWS.md index d476e0d..768594d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,9 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Added +- Add close button to alert dialogs + [#99](https://github.com/orontee/taranis/issues/99) + - Add icons to sunrise and sunset lines [#95](https://github.com/orontee/taranis/issues/95) diff --git a/src/alerts.cc b/src/alerts.cc index 4a0cffe..1950ff2 100644 --- a/src/alerts.cc +++ b/src/alerts.cc @@ -15,8 +15,9 @@ namespace taranis { void AlertsButton::on_clicked() { this->viewer->open(); } AlertViewer::AlertViewer(std::shared_ptr model, + std::shared_ptr icons, std::shared_ptr fonts) - : ModalWidget{}, model{model}, fonts{fonts}, + : ModalWidget{}, model{model}, icons{icons}, fonts{fonts}, content_width{this->bounding_box.w - 2 * AlertViewer::horizontal_padding}, title_height{2 * this->fonts->get_small_font()->height}, alert_title_start_y{this->title_height + AlertViewer::vertical_padding} {} @@ -61,6 +62,20 @@ void AlertViewer::do_paint() { this->content_width, this->title_height, this->title_text.c_str(), ALIGN_CENTER); + if (!this->close_button) { + const auto close_button_icon_size = + default_font->height + AlertViewer::vertical_padding; + + this->close_button = + std::make_shared(close_button_icon_size, this->icons); + this->close_button->set_click_handler(std::bind(&AlertViewer::hide, this)); + this->close_button->set_pos_x(this->get_width() - close_button_icon_size - + AlertViewer::horizontal_padding); + this->close_button->set_pos_y(this->get_pos_y() + + AlertViewer::vertical_padding / 4); + } + this->close_button->do_paint(); + DrawHorizontalSeparator(0, this->title_height, ScreenWidth(), HORIZONTAL_SEPARATOR_SOLID); @@ -210,6 +225,12 @@ int AlertViewer::handle_pointer_event(int event_type, int pointer_pos_x, return 1; } } + if (this->close_button and + this->close_button->is_in_bouding_box(pointer_pos_x, pointer_pos_y) and + this->close_button->is_enabled()) { + return this->close_button->handle_pointer_event(event_type, pointer_pos_x, + pointer_pos_y); + } return 0; } diff --git a/src/alerts.h b/src/alerts.h index 7c9f8cc..569b101 100644 --- a/src/alerts.h +++ b/src/alerts.h @@ -5,6 +5,7 @@ #include #include "button.h" +#include "closebutton.h" #include "fonts.h" #include "icons.h" #include "model.h" @@ -35,7 +36,8 @@ class AlertsButton : public Button { class AlertViewer : public ModalWidget { public: - AlertViewer(std::shared_ptr model, std::shared_ptr fonts); + AlertViewer(std::shared_ptr model, std::shared_ptr icons, + std::shared_ptr fonts); void open(); @@ -53,8 +55,11 @@ class AlertViewer : public ModalWidget { static constexpr int vertical_padding{25}; std::shared_ptr model; + std::shared_ptr icons; std::shared_ptr fonts; + std::shared_ptr close_button; + size_t alert_index{0}; const int content_width; diff --git a/src/ui.cc b/src/ui.cc index 182ce0c..2efd844 100644 --- a/src/ui.cc +++ b/src/ui.cc @@ -31,7 +31,8 @@ Ui::Ui(std::shared_ptr config, std::shared_ptr model) menu_button->set_pos_y(Ui::button_margin); menu_button->set_menu_handler(&handle_menu_item_selected); - this->alert_viewer = std::make_shared(model, this->fonts); + this->alert_viewer = + std::make_shared(model, this->icons, this->fonts); this->daily_forecast_viewer = std::make_shared(model, this->icons, this->fonts);