|
| 1 | +// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. |
| 2 | +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md |
| 3 | + |
| 4 | +#include "NotificationsBox.hpp" |
| 5 | + |
| 6 | +#include "application-desktop/data/Style.hpp" |
| 7 | + |
| 8 | +#include "TextFixedSize.hpp" |
| 9 | +#include "RichTextParser.hpp" |
| 10 | +#include "FontManager.hpp" |
| 11 | + |
| 12 | +using namespace gui; |
| 13 | +using namespace style::desktop; |
| 14 | + |
| 15 | +NotificationsBox::NotificationsBox(Item *parent, uint32_t x, uint32_t y, uint32_t w, uint32_t h) |
| 16 | + : VBox(parent, x, y, w, h) |
| 17 | +{ |
| 18 | + this->setAlignment(Alignment(gui::Alignment::Horizontal::Center)); |
| 19 | + this->setPenWidth(style::window::default_border_no_focus_w); |
| 20 | + this->setPenFocusWidth(style::window::default_border_no_focus_w); |
| 21 | + |
| 22 | + auto getNextNotification = [this]() -> Item * { |
| 23 | + auto focusedItem = getFocusItem(); |
| 24 | + if (focusedItem == nullptr) { |
| 25 | + return nullptr; |
| 26 | + } |
| 27 | + auto nextItem = focusedItem->getNavigationItem(NavigationDirection::UP); |
| 28 | + |
| 29 | + if (nextItem == nullptr) { |
| 30 | + nextItem = focusedItem->getNavigationItem(NavigationDirection::DOWN); |
| 31 | + } |
| 32 | + return nextItem; |
| 33 | + }; |
| 34 | + |
| 35 | + auto setNextFocusedItemAfterErase = [this](Item *item) -> bool { |
| 36 | + if (item == nullptr) { |
| 37 | + setFocus(false); |
| 38 | + } |
| 39 | + else { |
| 40 | + item->clearNavigationItem(NavigationDirection::DOWN); |
| 41 | + item->clearNavigationItem(NavigationDirection::UP); |
| 42 | + setNavigation(); |
| 43 | + setFocusItem(item); |
| 44 | + } |
| 45 | + return true; |
| 46 | + }; |
| 47 | + |
| 48 | + inputCallback = [this, setNextFocusedItemAfterErase, getNextNotification](Item &, const InputEvent &event) -> bool { |
| 49 | + if (event.isShortPress() && event.is(KeyCode::KEY_RF)) { |
| 50 | + LOG_DEBUG("Removing single notification"); |
| 51 | + auto ptr = getFocusItem(); |
| 52 | + if (ptr == nullptr || focusItem == this) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + auto nextItem = getNextNotification(); |
| 56 | + ptr->inputCallback(*this, event); |
| 57 | + erase(ptr); |
| 58 | + return setNextFocusedItemAfterErase(nextItem); |
| 59 | + } |
| 60 | + return false; |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +namespace |
| 65 | +{ |
| 66 | + auto buildImageInactive(const UTF8 &img) -> gui::Image * |
| 67 | + { |
| 68 | + auto thumbnail = new gui::Image(img); |
| 69 | + thumbnail->activeItem = false; |
| 70 | + return thumbnail; |
| 71 | + } |
| 72 | + |
| 73 | + auto buildNotificationIcon(UTF8 icon) -> gui::Image * |
| 74 | + { |
| 75 | + auto thumbnail = buildImageInactive(icon); |
| 76 | + thumbnail->setMinimumWidth(notifications::IconWidth); |
| 77 | + thumbnail->setAlignment(Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Center)); |
| 78 | + thumbnail->setMargins( |
| 79 | + gui::Margins(style::window::default_left_margin, 0, style::window::default_right_margin, 0)); |
| 80 | + return thumbnail; |
| 81 | + } |
| 82 | + |
| 83 | + auto buildNotificationNameLabel(const UTF8 &name, uint32_t width) -> gui::TextFixedSize * |
| 84 | + { |
| 85 | + auto text = new gui::TextFixedSize( |
| 86 | + nullptr, 0, 0, style::desktop::notifications::TextMaxWidth, style::window::label::default_h); |
| 87 | + text->setMaximumSize(width, Axis::X); |
| 88 | + |
| 89 | + TextFormat format(FontManager::getInstance().getFont(style::window::font::medium)); |
| 90 | + text::RichTextParser rtParser; |
| 91 | + auto parsedText = rtParser.parse(name, &format); |
| 92 | + |
| 93 | + text->setText(std::move(parsedText)); |
| 94 | + text->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center)); |
| 95 | + text->setPenWidth(style::window::default_border_no_focus_w); |
| 96 | + text->setUnderline(false); |
| 97 | + text->activeItem = false; |
| 98 | + return text; |
| 99 | + } |
| 100 | + |
| 101 | + constexpr auto maxNotificationValue = "99+"; |
| 102 | + |
| 103 | + auto buildNotificationCountText(const UTF8 &indicator) -> gui::Text * |
| 104 | + { |
| 105 | + auto number = new gui::Text(); |
| 106 | + if (indicator.length() > 2) { |
| 107 | + number->setText(maxNotificationValue); |
| 108 | + number->setMinimumWidth(strlen(maxNotificationValue) * notifications::DigitSize); |
| 109 | + } |
| 110 | + else { |
| 111 | + number->setText(indicator); |
| 112 | + number->setMinimumWidth(indicator.length() * notifications::DigitSize); |
| 113 | + } |
| 114 | + number->setMinimumWidth(indicator.length() * notifications::DigitSize); |
| 115 | + number->setFont(style::window::font::mediumbold); |
| 116 | + number->setPenWidth(style::window::default_border_no_focus_w); |
| 117 | + number->setMargins(gui::Margins(0, 0, style::window::default_right_margin, 0)); |
| 118 | + number->setAlignment(Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Center)); |
| 119 | + number->activeItem = false; |
| 120 | + return number; |
| 121 | + } |
| 122 | + |
| 123 | +} // namespace |
| 124 | + |
| 125 | +auto NotificationsBox::addNotification(UTF8 icon, |
| 126 | + UTF8 name, |
| 127 | + UTF8 indicator, |
| 128 | + std::function<bool()> showCallback, |
| 129 | + std::function<bool()> clearCallback, |
| 130 | + std::function<void(bool)> onFocus) -> bool |
| 131 | +{ |
| 132 | + // 1. create hbox for all elements |
| 133 | + auto el = new gui::HBox(nullptr, 0, 0, style::window::default_body_width, style::window::label::default_h); |
| 134 | + |
| 135 | + // 2. Add all elements to hbox layout |
| 136 | + el->addWidget(buildNotificationIcon(icon)); |
| 137 | + |
| 138 | + el->addWidget(buildNotificationNameLabel(name, el->area().w)); |
| 139 | + el->addWidget(buildImageInactive("dot_12px_hard_alpha_W_G")); |
| 140 | + el->addWidget(buildNotificationCountText(indicator)); |
| 141 | + |
| 142 | + // 3. Set hbox layout properties |
| 143 | + el->setAlignment(Alignment(gui::Alignment::Vertical::Center)); |
| 144 | + el->setMargins(gui::Margins(0, 0, 0, 10)); |
| 145 | + el->setPenWidth(style::window::default_border_no_focus_w); |
| 146 | + el->setPenFocusWidth(style::window::default_border_focus_w); |
| 147 | + el->setEdges(RectangleEdge::Bottom | RectangleEdge::Top); |
| 148 | + |
| 149 | + el->focusChangedCallback = [el, onFocus](Item &) -> bool { |
| 150 | + onFocus(el->focus); |
| 151 | + return true; |
| 152 | + }; |
| 153 | + el->activatedCallback = [showCallback](Item &) { return showCallback(); }; |
| 154 | + el->inputCallback = [showCallback, clearCallback, this](Item &, const InputEvent &event) -> bool { |
| 155 | + if (event.isShortPress() && event.is(KeyCode::KEY_RF) && clearCallback) { |
| 156 | + return clearCallback(); |
| 157 | + } |
| 158 | + return false; |
| 159 | + }; |
| 160 | + |
| 161 | + this->addWidget(el); |
| 162 | + return el->visible; |
| 163 | +} |
| 164 | + |
| 165 | +bool NotificationsBox::onInput(const InputEvent &inputEvent) |
| 166 | +{ |
| 167 | + |
| 168 | + if (inputEvent.isShortPress() && (inputEvent.is(KeyCode::KEY_UP) || inputEvent.is(KeyCode::KEY_DOWN))) { |
| 169 | + auto handled = handleNavigation(inputEvent); |
| 170 | + if (!handled) { |
| 171 | + setFocus(false); |
| 172 | + } |
| 173 | + return true; |
| 174 | + } |
| 175 | + return VBox::onInput(inputEvent); |
| 176 | +} |
| 177 | + |
| 178 | +bool NotificationsBox::clearAll(const InputEvent &event) |
| 179 | +{ |
| 180 | + while (!children.empty()) { |
| 181 | + inputCallback(*this, event); |
| 182 | + } |
| 183 | + return true; |
| 184 | +} |
| 185 | + |
| 186 | +void NotificationsBox::navigateToBottom() |
| 187 | +{ |
| 188 | + setFocusItem(children.back()); |
| 189 | +} |
0 commit comments