KiCad PCB EDA Suite
Loading...
Searching...
No Matches
notifications_manager.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 Mark Roszko <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <wx/filename.h>
22#include <wx/frame.h>
23#include <wx/hyperlink.h>
24#include <wx/panel.h>
25#include <wx/scrolwin.h>
26#include <wx/sizer.h>
27#include <wx/settings.h>
28#include <wx/stattext.h>
29#include <wx/string.h>
30#include <wx/time.h>
31
32#include <paths.h>
33
35#include <widgets/kistatusbar.h>
36#include <widgets/ui_common.h>
37
38#include <algorithm>
39#include <json_common.h>
40#include <kiplatform/ui.h>
41
42#include <core/wx_stl_compat.h>
44#include <core/kicad_algo.h>
45
46#include <algorithm>
47#include <fstream>
48#include <list>
49#include <map>
50#include <optional>
51#include <tuple>
52#include <vector>
53
54
55static long long g_last_closed_timer = 0;
56
57NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( NOTIFICATION, title, description, href, key, date )
58
59class NOTIFICATION_PANEL : public wxPanel
60{
61public:
62 NOTIFICATION_PANEL( wxWindow* aParent, NOTIFICATIONS_MANAGER* aManager, NOTIFICATION* aNoti ) :
63 wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxSize( -1, 75 ), wxBORDER_SIMPLE ),
64 m_hlDetails( nullptr ),
65 m_notification( aNoti ),
66 m_manager( aManager )
67 {
68 SetSizeHints( wxDefaultSize, wxDefaultSize );
69
70 wxBoxSizer* mainSizer;
71 mainSizer = new wxBoxSizer( wxVERTICAL );
72
73 wxColour fg, bg;
75 SetBackgroundColour( bg );
76 SetForegroundColour( fg );
77
78 m_stTitle = new wxStaticText( this, wxID_ANY, aNoti->title );
79 m_stTitle->Wrap( -1 );
80 m_stTitle->SetFont( KIUI::GetControlFont( this ).Bold() );
81 mainSizer->Add( m_stTitle, 0, wxALL | wxEXPAND, 1 );
82
83 m_stDescription = new wxStaticText( this, wxID_ANY, aNoti->description );
84
85 // Leave room for the borders and a scrollbar that may appear later
86 int margin = FromDIP( 8 ) + wxSystemSettings::GetMetric( wxSYS_VSCROLL_X, aParent );
87
88 m_stDescription->Wrap( std::max( FromDIP( 100 ), aParent->GetClientSize().x - margin ) );
89 mainSizer->Add( m_stDescription, 0, wxALL | wxEXPAND, 1 );
90
91 wxBoxSizer* tailSizer;
92 tailSizer = new wxBoxSizer( wxHORIZONTAL );
93
94 if( !aNoti->href.IsEmpty() )
95 {
96 m_hlDetails = new wxHyperlinkCtrl( this, wxID_ANY, _( "View Details" ), aNoti->href );
97 tailSizer->Add( m_hlDetails, 0, wxALL, 2 );
98 }
99
100 m_hlDismiss = new wxHyperlinkCtrl( this, wxID_ANY, _( "Dismiss" ), aNoti->href );
101 tailSizer->Add( m_hlDismiss, 0, wxALL, 2 );
102
103 mainSizer->Add( tailSizer, 1, wxEXPAND, 5 );
104
105 if( m_hlDetails != nullptr )
106 m_hlDetails->Bind( wxEVT_HYPERLINK, &NOTIFICATION_PANEL::onDetails, this );
107
108 m_hlDismiss->Bind( wxEVT_HYPERLINK, &NOTIFICATION_PANEL::onDismiss, this );
109
110 SetSizer( mainSizer );
111
112 // Grow past the default height when the description wraps
113 SetMinSize( wxSize( -1, std::max( GetMinSize().y, mainSizer->GetMinSize().y ) ) );
114 Layout();
115 }
116
117private:
118 void onDetails( wxHyperlinkEvent& aEvent )
119 {
120 wxString url = aEvent.GetURL();
121
122 if( url.StartsWith( wxS( "kicad://" ) ) )
123 {
124 url.Replace( wxS( "kicad://" ), wxS( "" ) );
125
126 if( url == wxS( "pcm" ) )
127 {
128 // TODO
129 }
130 }
131 else
132 {
133 wxLaunchDefaultBrowser( aEvent.GetURL(), wxBROWSER_NEW_WINDOW );
134 }
135 }
136
137 void onDismiss( wxHyperlinkEvent& aEvent )
138 {
139 CallAfter(
140 [this]()
141 {
142 // This will cause this panel to get deleted
143 m_manager->Remove( m_notification->key );
144 } );
145 }
146
147private:
148 wxStaticText* m_stTitle;
149 wxStaticText* m_stDescription;
150 wxHyperlinkCtrl* m_hlDetails;
151 wxHyperlinkCtrl* m_hlDismiss;
154};
155
156
157class NOTIFICATIONS_LIST : public wxFrame
158{
159public:
160 NOTIFICATIONS_LIST( NOTIFICATIONS_MANAGER* aManager, wxWindow* parent, const wxPoint& pos ) :
161 wxFrame( parent, wxID_ANY, _( "Notifications" ), pos, wxSize( 300, 150 ),
162 wxFRAME_NO_TASKBAR | wxBORDER_SIMPLE ),
163 m_manager( aManager )
164 {
165 SetSizeHints( wxDefaultSize, wxDefaultSize );
166
167 wxBoxSizer* bSizer1;
168 bSizer1 = new wxBoxSizer( wxVERTICAL );
169
170 m_scrolledWindow = new wxScrolledWindow( this, wxID_ANY, wxDefaultPosition,
171 wxSize( -1, -1 ), wxVSCROLL | wxBORDER_SIMPLE );
172 wxColour fg, bg;
174 m_scrolledWindow->SetBackgroundColour( bg );
175 m_scrolledWindow->SetForegroundColour( fg );
176
177 m_scrolledWindow->SetScrollRate( 5, 5 );
178 m_contentSizer = new wxBoxSizer( wxVERTICAL );
179
180 m_scrolledWindow->SetSizer( m_contentSizer );
181 m_scrolledWindow->Layout();
183 bSizer1->Add( m_scrolledWindow, 1, wxEXPAND | wxALL, 0 );
184
185 m_noNotificationsText = new wxStaticText( m_scrolledWindow, wxID_ANY,
186 _( "There are no notifications available" ),
187 wxDefaultPosition, wxDefaultSize,
188 wxALIGN_CENTER_HORIZONTAL );
189 m_noNotificationsText->Wrap( -1 );
190 m_contentSizer->Add( m_noNotificationsText, 1, wxALL | wxEXPAND, 5 );
191
192 Bind( wxEVT_KILL_FOCUS, &NOTIFICATIONS_LIST::onFocusLoss, this );
193 m_scrolledWindow->Bind( wxEVT_KILL_FOCUS, &NOTIFICATIONS_LIST::onFocusLoss, this );
194
195 SetSizer( bSizer1 );
196 Layout();
197
198 SetFocus();
199 }
200
201
203 {
204 // Unbind focus handlers before base class destructors run. GTK fires focus-out signals
205 // during window teardown, which would invoke onFocusLoss through a partially-destroyed
206 // object and crash in the wxWidgets event handler chain.
207 Unbind( wxEVT_KILL_FOCUS, &NOTIFICATIONS_LIST::onFocusLoss, this );
208 m_scrolledWindow->Unbind( wxEVT_KILL_FOCUS, &NOTIFICATIONS_LIST::onFocusLoss, this );
209 }
210
211
212 void onFocusLoss( wxFocusEvent& aEvent )
213 {
214 if( IsDescendant( aEvent.GetWindow() ) )
215 {
216 // Child (such as the hyperlink texts) got focus
217 }
218 else
219 {
220 Close( true );
221 g_last_closed_timer = wxGetLocalTimeMillis().GetValue();
222 }
223
224 aEvent.Skip();
225 }
226
227
228 void Add( NOTIFICATION* aNoti )
229 {
230 m_noNotificationsText->Hide();
231
233 m_contentSizer->Add( panel, 0, wxEXPAND | wxALL, 2 );
234 m_scrolledWindow->Layout();
236
237 // call this at this window otherwise the child panels don't resize width properly
238 Layout();
239
240 m_panelMap[aNoti] = panel;
241 }
242
243
244 void Remove( NOTIFICATION* aNoti )
245 {
246 auto it = m_panelMap.find( aNoti );
247
248 if( it != m_panelMap.end() )
249 {
250 NOTIFICATION_PANEL* panel = m_panelMap[aNoti];
251 m_contentSizer->Detach( panel );
252 panel->Destroy();
253
254 m_panelMap.erase( it );
255
256 // ensure the window contents get shifted as needed
257 m_scrolledWindow->Layout();
258 Layout();
259 }
260
261 if( m_panelMap.size() == 0 )
262 {
263 m_noNotificationsText->Show();
264 }
265 }
266
267private:
268 wxScrolledWindow* m_scrolledWindow;
269
271 wxBoxSizer* m_contentSizer;
272 std::unordered_map<NOTIFICATION*, NOTIFICATION_PANEL*> m_panelMap;
274
278};
279
280
282{
283 m_destFileName = wxFileName( PATHS::GetUserCachePath(), wxT( "notifications.json" ) );
284}
285
286
288{
289 nlohmann::json saved_json;
290
291 std::ifstream saved_json_stream( m_destFileName.GetFullPath().fn_str() );
292
293 try
294 {
295 saved_json_stream >> saved_json;
296
297 m_notifications = saved_json.get<std::list<NOTIFICATION>>();
298 }
299 catch( std::exception& )
300 {
301 // failed to load the json, which is fine, default to no notifications
302 }
303
304 if( wxGetEnv( wxT( "KICAD_TEST_NOTI" ), nullptr ) )
305 {
306 CreateOrUpdate( wxS( "test" ), wxS( "Test Notification" ), wxS( "Test please ignore" ),
307 wxS( "https://kicad.org" ) );
308 }
309}
310
311
313{
314 std::ofstream jsonFileStream( m_destFileName.GetFullPath().fn_str() );
315
316 nlohmann::json saveJson = nlohmann::json( m_notifications );
317 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
318 jsonFileStream.flush();
319 jsonFileStream.close();
320}
321
322
323void NOTIFICATIONS_MANAGER::CreateOrUpdate( const wxString& aKey,
324 const wxString& aTitle,
325 const wxString& aDescription,
326 const wxString& aHref )
327{
328 wxCHECK_RET( !aKey.IsEmpty(), wxS( "Notification key must not be empty" ) );
329
330 auto it = std::find_if( m_notifications.begin(), m_notifications.end(),
331 [&]( const NOTIFICATION& noti )
332 {
333 return noti.key == aKey;
334 } );
335
336 NOTIFICATION* noti = nullptr;
337
338 if( it != m_notifications.end() )
339 {
340 noti = &*it;
341 noti->title = aTitle;
342 noti->description = aDescription;
343 noti->href = aHref;
344 }
345 else
346 {
347 noti = &m_notifications.emplace_back( NOTIFICATION{ aTitle, aDescription, aHref,
348 aKey, wxEmptyString } );
349 }
350
351 // Replace an updated notification's panel rather than add a second one
353 {
354 list->Remove( noti );
355 list->Add( noti );
356 }
357
358 for( KISTATUSBAR* statusBar : m_statusBars )
359 statusBar->SetNotificationCount( m_notifications.size() );
360
361 Save();
362}
363
364
365void NOTIFICATIONS_MANAGER::Remove( const wxString& aKey )
366{
367 auto it = std::find_if( m_notifications.begin(), m_notifications.end(),
368 [&]( const NOTIFICATION& noti )
369 {
370 return noti.key == aKey;
371 } );
372
373 if( it == m_notifications.end() )
374 return;
375
376 if( m_shownDialogs.size() > 0 )
377 {
378 // update dialogs
379
381 list->Remove( &(*it) );
382 }
383
384 m_notifications.erase( it );
385
386 Save();
387
388 for( KISTATUSBAR* statusBar : m_statusBars )
389 statusBar->SetNotificationCount( m_notifications.size() );
390}
391
392
394{
395 NOTIFICATIONS_LIST* evtWindow = dynamic_cast<NOTIFICATIONS_LIST*>( aEvent.GetEventObject() );
396
397 std::erase_if( m_shownDialogs, [&]( NOTIFICATIONS_LIST* dialog )
398 {
399 return dialog == evtWindow;
400 } );
401
402 aEvent.Skip();
403}
404
405
406void NOTIFICATIONS_MANAGER::ShowList( wxWindow* aParent, wxPoint aPos )
407{
408 // Debounce clicking on the icon with a list already showing. The button will get focus
409 // first, which will cause a focus-loss on the list (thereby closing it), and then we'd open
410 // it again without this guard.
411 if( wxGetLocalTimeMillis().GetValue() - g_last_closed_timer < 300 )
412 {
414 return;
415 }
416
417 NOTIFICATIONS_LIST* list = new NOTIFICATIONS_LIST( this, aParent, aPos );
418
419 for( NOTIFICATION& job : m_notifications )
420 list->Add( &job );
421
422 m_shownDialogs.push_back( list );
423
424 list->Bind( wxEVT_CLOSE_WINDOW, &NOTIFICATIONS_MANAGER::onListWindowClosed, this );
425
426 // correct the position
427 wxSize windowSize = list->GetSize();
428 list->SetPosition( aPos - windowSize );
429
430 list->Show();
432}
433
434
436{
437 m_statusBars.push_back( aStatusBar );
438
439 // notifications should already be loaded so set the initial notification count
440 aStatusBar->SetNotificationCount( m_notifications.size() );
441}
442
443
445{
446 std::erase_if( m_statusBars, [&]( KISTATUSBAR* statusBar )
447 {
448 return statusBar == aStatusBar;
449 } );
450}
KISTATUSBAR is a wxStatusBar suitable for Kicad manager.
Definition kistatusbar.h:50
void SetNotificationCount(int aCount)
Set the notification count on the notifications button.
void onFocusLoss(wxFocusEvent &aEvent)
void Add(NOTIFICATION *aNoti)
NOTIFICATIONS_MANAGER * m_manager
wxBoxSizer * m_contentSizer
Inner content of the scrolled window, add panels here.
std::unordered_map< NOTIFICATION *, NOTIFICATION_PANEL * > m_panelMap
void Remove(NOTIFICATION *aNoti)
wxScrolledWindow * m_scrolledWindow
wxStaticText * m_noNotificationsText
Text to be displayed when no notifications are present, this gets a Show/Hide call as needed.
NOTIFICATIONS_LIST(NOTIFICATIONS_MANAGER *aManager, wxWindow *parent, const wxPoint &pos)
std::list< NOTIFICATION > m_notifications
Current stack of notifications. A list, since shown panels hold pointers to its elements.
void Save()
Save notifications to disk.
void onListWindowClosed(wxCloseEvent &aEvent)
Handle removing the shown list window from our list of shown windows.
wxFileName m_destFileName
The cached file path to read/write notifications on disk.
void Load()
Load notifications stored from disk.
void RegisterStatusBar(KISTATUSBAR *aStatusBar)
Add a status bar for handling.
void CreateOrUpdate(const wxString &aKey, const wxString &aTitle, const wxString &aDescription, const wxString &aHref=wxEmptyString)
Create a notification with the given parameters or updates an existing one with the same key.
void Remove(const wxString &aKey)
Remove a notification by key.
std::vector< KISTATUSBAR * > m_statusBars
Status bars registered for updates.
void UnregisterStatusBar(KISTATUSBAR *aStatusBar)
Remove status bar from handling.
void ShowList(wxWindow *aParent, wxPoint aPos)
Show the notification list.
std::vector< NOTIFICATIONS_LIST * > m_shownDialogs
Currently shown notification lists.
wxHyperlinkCtrl * m_hlDetails
void onDismiss(wxHyperlinkEvent &aEvent)
NOTIFICATION_PANEL(wxWindow *aParent, NOTIFICATIONS_MANAGER *aManager, NOTIFICATION *aNoti)
void onDetails(wxHyperlinkEvent &aEvent)
NOTIFICATIONS_MANAGER * m_manager
wxHyperlinkCtrl * m_hlDismiss
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition paths.cpp:422
#define _(s)
void GetInfoBarColours(wxColour &aFGColour, wxColour &aBGColour)
Return the background and foreground colors for info bars in the current scheme.
Definition wxgtk/ui.cpp:76
void ForceFocus(wxWindow *aWindow)
Pass the current focus to the window.
Definition wxgtk/ui.cpp:211
KICOMMON_API wxFont GetControlFont(wxWindow *aWindow)
static long long g_last_closed_timer
wxString description
Additional message displayed under title.
wxString title
Title of the notification.
wxString href
URL if any to link to for details.
Functions to provide common constants and other functions to assist in making a consistent UI.
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(UPDATE_REQUEST, platform, arch, current_version, lang, last_check) struct UPDATE_RESPONSE