| 1 | // Aseprite | 
|---|---|
| 2 | // Copyright (C) 2020-2022 Igara Studio S.A. | 
| 3 | // Copyright (C) 2001-2017 David Capello | 
| 4 | // | 
| 5 | // This program is distributed under the terms of | 
| 6 | // the End-User License Agreement for Aseprite. | 
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H | 
| 9 | #include "config.h" | 
| 10 | #endif | 
| 11 | |
| 12 | #include "app/ui/notifications.h" | 
| 13 | |
| 14 | #include "app/notification_delegate.h" | 
| 15 | #include "app/ui/skin/skin_theme.h" | 
| 16 | #include "base/launcher.h" | 
| 17 | #include "ui/menu.h" | 
| 18 | #include "ui/paint_event.h" | 
| 19 | #include "ui/size_hint_event.h" | 
| 20 | |
| 21 | namespace app { | 
| 22 | |
| 23 | using namespace ui; | 
| 24 | |
| 25 | class NotificationItem : public MenuItem { | 
| 26 | public: | 
| 27 | NotificationItem(INotificationDelegate* del) | 
| 28 | : MenuItem(del->notificationText()), | 
| 29 | m_delegate(del) { | 
| 30 | } | 
| 31 | |
| 32 | protected: | 
| 33 | void onClick() override { | 
| 34 | MenuItem::onClick(); | 
| 35 | m_delegate->notificationClick(); | 
| 36 | } | 
| 37 | |
| 38 | private: | 
| 39 | INotificationDelegate* m_delegate; | 
| 40 | }; | 
| 41 | |
| 42 | Notifications::Notifications() | 
| 43 | : Button( "") | 
| 44 | , m_flagStyle(skin::SkinTheme::get(this)->styles.flag()) | 
| 45 | , m_red(false) | 
| 46 | { | 
| 47 | } | 
| 48 | |
| 49 | void Notifications::addLink(INotificationDelegate* del) | 
| 50 | { | 
| 51 | m_popup.addChild(new NotificationItem(del)); | 
| 52 | m_red = true; | 
| 53 | } | 
| 54 | |
| 55 | void Notifications::onSizeHint(SizeHintEvent& ev) | 
| 56 | { | 
| 57 | ev.setSizeHint(gfx::Size(16, 10)*guiscale()); // TODO hard-coded flag size | 
| 58 | } | 
| 59 | |
| 60 | void Notifications::onPaint(PaintEvent& ev) | 
| 61 | { | 
| 62 | Graphics* g = ev.graphics(); | 
| 63 | |
| 64 | PaintWidgetPartInfo info; | 
| 65 | if (hasMouseOver()) info.styleFlags |= ui::Style::Layer::kMouse; | 
| 66 | if (m_red) info.styleFlags |= ui::Style::Layer::kFocus; | 
| 67 | if (isSelected()) info.styleFlags |= ui::Style::Layer::kSelected; | 
| 68 | |
| 69 | theme()->paintWidgetPart( | 
| 70 | g, m_flagStyle, clientBounds(), info); | 
| 71 | } | 
| 72 | |
| 73 | void Notifications::onClick(ui::Event& ev) | 
| 74 | { | 
| 75 | m_red = false; | 
| 76 | invalidate(); | 
| 77 | |
| 78 | gfx::Rect bounds = this->bounds(); | 
| 79 | m_popup.showPopup( | 
| 80 | gfx::Point( | 
| 81 | bounds.x - m_popup.sizeHint().w, | 
| 82 | bounds.y2()), | 
| 83 | display()); | 
| 84 | } | 
| 85 | |
| 86 | } // namespace app | 
| 87 | 
