MeshLib C++ Docs
Loading...
Searching...
No Matches
MRRibbonNotification.h
Go to the documentation of this file.
1#pragma once
2#include "MRViewerFwd.h"
4#include "MRAsyncTimer.h"
6#include "MRMesh/MRBox.h"
7#include <functional>
8#include <chrono>
9
10namespace MR
11{
12
14{
15 enum Tag : unsigned
16 {
17 None = 0b0000,
18 Report = 0b0001,
21 Important = 0b1000,
24 };
25};
27
28using NotificationTagMask = unsigned;
29
36
38{
39 // Callback for notification
40 // if it is not null, a button will be drawn, and callback will be invoked on button click
41 using OnButtonClick = std::function<void()>;
43
44 // Name of button that will be drawn if callback is enabled
45 std::string buttonName = "OK";
46 // Header of notification
47 std::string header;
48 // Text of notification
49 std::string text;
50 // Type of notification
52 // Time that notification stays visible
53 // negative value means to use default one
54 float lifeTimeSec = -1.0f;
55 // it ANDs with RibbonNotifier allowed tags to see if notification should be displayed
57 // if notifications are equal to last one added, it just increment counter
58 // note that if there is present `onButtonClick` this function always returns false
59 bool operator==( const RibbonNotification& other ) const;
60};
61
62// class to hold and operate with notifications
63class MRVIEWER_CLASS RibbonNotifier
64{
65public:
66 // adds new notification for drawing
67 MRVIEWER_API void pushNotification( const RibbonNotification& notification );
68
69 // main draw function. draw actual notification or history, and history button
70 // limitFramebuffer - available framebuffer space (usually same as `Viewer::getViewportsBounds()`)
71 MRVIEWER_API void draw( float scaling, const Box2i& limitFramebuffer );
72
73 // set maximum time while history button will be present on screen
74 // negative value means that history button will never be hidden
75 MRVIEWER_API void setHitoryButtonMaxLifeTime( float histBtnMaxLifeTime );
76
77 // this value is used as notification `lifeTimeSec` if negative values passed
78 float defaultNotificationLifeTimeSeconds = 5.0f;
79
80 // this mask is used to control allowed notifications by filtering with tags
81 NotificationTagMask allowedTagMask = NotificationTags::Default;
82
83 // position of notifications on screen
84 RibbonNotificationCorner cornerPosition = RibbonNotificationCorner::LowerLeft;
85private:
86 struct NotificationWithTimer
87 {
88 RibbonNotification notification;
89 float timer{ 0.0f };
90 int sameCounter = 1;
91 };
92 std::vector<NotificationWithTimer> notifications_;
93 std::vector<NotificationWithTimer> notificationsHistory_;
94 bool requestRedraw_ = false;
95 bool historyMode_ = false;
96
97 float showHistoryBtnMaxTime_{ -1.0f }; // negative value here means that there is no need to hide history button
98 float currentHistoryBtnTimer_{ -1.0f }; // update to validly hide the button
99#ifndef __EMSCRIPTEN__
100 Time requestedTime_{ Time::max() };
101 AsyncRequest asyncRequest_;
102#endif
103
104 // draw button to show last notifications
105 void drawHistoryButton_( float scaling, const Box2i& limitFramebuffer );
106 // draw notification history
107 void drawHistory_( float scaling, const Box2i& limitFramebuffer );
108 // draw floating notifications
109 void drawFloating_( float scaling, const Box2i& limitFramebuffer );
110
111 // set this true on open history and on new notification added
112 bool scrollDownNeeded_ = false;
113 float prevHistoryScrollMax_ = 0.0f;
114 struct DrawNotificationSettings
115 {
116 int index{ 0 };
117 float scalig{ 1.0f };
118 float width{ 0.0f };
119 bool historyMode{ false };
120 Vector2f* currentPos{ nullptr };
121 };
122 // draws one notification
123 // returns false if need to close
124 bool drawNotification_( const DrawNotificationSettings& settings );
125 void addNotification_( std::vector<NotificationWithTimer>& store, const RibbonNotification& notification );
126 void filterInvalid_( int numInvalid = -1 );
127 void requestClosestRedraw_();
128};
129
130}
#define MR_MAKE_FLAG_OPERATORS(T)
Definition MRFlagOperators.h:6
Definition MRRibbonNotification.h:64
MRVIEWER_API void setHitoryButtonMaxLifeTime(float histBtnMaxLifeTime)
MRVIEWER_API void pushNotification(const RibbonNotification &notification)
MRVIEWER_API void draw(float scaling, const Box2i &limitFramebuffer)
auto width(const Box< V > &box)
returns size along x axis
Definition MRMesh/MRBox.h:247
std::chrono::time_point< std::chrono::system_clock > Time
Definition MRAsyncTimer.h:17
NotificationType
Definition MRNotificationType.h:7
unsigned NotificationTagMask
Definition MRRibbonNotification.h:28
RibbonNotificationCorner
corner where notifications will appear
Definition MRRibbonNotification.h:32
Definition MRRibbonNotification.h:14
Tag
Definition MRRibbonNotification.h:16
@ ImplicitChanges
Definition MRRibbonNotification.h:20
@ Report
Definition MRRibbonNotification.h:18
@ Recommendation
Definition MRRibbonNotification.h:19
@ All
Definition MRRibbonNotification.h:23
@ Important
Definition MRRibbonNotification.h:21
@ Default
Definition MRRibbonNotification.h:22
@ None
Definition MRRibbonNotification.h:17
Definition MRRibbonNotification.h:38
std::string buttonName
Definition MRRibbonNotification.h:45
NotificationTagMask tags
Definition MRRibbonNotification.h:56
std::function< void()> OnButtonClick
Definition MRRibbonNotification.h:41
OnButtonClick onButtonClick
Definition MRRibbonNotification.h:42
std::string text
Definition MRRibbonNotification.h:49
NotificationType type
Definition MRRibbonNotification.h:51
float lifeTimeSec
Definition MRRibbonNotification.h:54
bool operator==(const RibbonNotification &other) const
std::string header
Definition MRRibbonNotification.h:47