Skip to content

Commit

Permalink
Make sure widget don't handle key pressed while a modal is opened
Browse files Browse the repository at this point in the history
Refs: #77
  • Loading branch information
orontee committed Nov 28, 2023
1 parent 5f49b73 commit 665d059
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
8 changes: 1 addition & 7 deletions src/alerts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,13 @@ void AlertViewer::display_next_alert_maybe() {
}
}

bool AlertViewer::handle_key_press(int key) {
return (key == IV_KEY_PREV) or (key == IV_KEY_NEXT);
}

bool AlertViewer::handle_key_release(int key) {
if (key == IV_KEY_PREV) {
this->display_previous_alert_maybe();
return true;
} else if (key == IV_KEY_NEXT) {
this->display_next_alert_maybe();
return true;
}
return false;
return true;
}

int AlertViewer::handle_pointer_event(int event_type, int pointer_pos_x,
Expand Down
2 changes: 0 additions & 2 deletions src/alerts.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class AlertViewer : public ModalWidget {

void do_paint() override;

bool handle_key_press(int key) override;

bool handle_key_release(int key) override;

int handle_pointer_event(int event_type, int pointer_pos_x,
Expand Down
9 changes: 9 additions & 0 deletions src/ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Ui::Ui(std::shared_ptr<Config> config, std::shared_ptr<Model> model)
this->children.push_back(this->forecast_stack);
this->children.push_back(status_bar);

this->register_key_event_consumer(this->alert_viewer);
this->register_key_event_consumer(menu_button);
this->register_key_event_consumer(this->forecast_stack);
}
Expand Down Expand Up @@ -113,6 +114,14 @@ int Ui::handle_pointer_event(int event_type, int pointer_pos_x,
return 0;
}

bool Ui::is_key_event_consumer_active(
std::shared_ptr<KeyEventConsumer> consumer) {
if (this->visible_modal) {
return consumer == this->visible_modal;
}
return KeyEventDispatcher::is_key_event_consumer_active(consumer);
}

void Ui::switch_forecast_widget() { this->forecast_stack->switch_forecast(); }

void Ui::open_location_list(const std::vector<Location> &locations) {
Expand Down
3 changes: 3 additions & 0 deletions src/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Ui : public KeyEventDispatcher {
int handle_pointer_event(int event_type, int pointer_pos_x,
int pointer_pos_y);

bool is_key_event_consumer_active(
std::shared_ptr<KeyEventConsumer> consumer) override;

void display_alert() { this->alert_viewer->open(); }

void switch_forecast_widget();
Expand Down
7 changes: 5 additions & 2 deletions src/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ struct Widget : public KeyEventConsumer {

virtual bool is_modal() const { return false; }
// a modal widget takes control of the screen, and receives all
// keys and pointer events
// keys and pointer events when visible

virtual bool is_visible() const { return true; }
// invisible widgets are painted white
// invisible widgets are painted white, note that nothing ensures
// that the returned value change when a modal is opened!

virtual bool is_enabled() const { return true; }
// disabled widgets don't receive keys and pointer events
Expand Down Expand Up @@ -83,6 +84,8 @@ struct Widget : public KeyEventConsumer {
return this->is_visible() and this->is_enabled();
}

bool handle_key_press(int key) override { return this->is_modal(); }

protected:
irect bounding_box;

Expand Down

0 comments on commit 665d059

Please sign in to comment.