mirror of
https://github.com/PurpleI2P/i2pd
synced 2025-10-05 23:52:50 +02:00
update main window by timer
This commit is contained in:
@@ -28,18 +28,10 @@
|
||||
|
||||
constexpr int M_GRACEFUL_SHUTDOWN = 1;
|
||||
constexpr int C_GRACEFUL_SHUTDOWN_UPDATE = 2;
|
||||
constexpr bigtime_t GRACEFUL_SHUTDOWN_UPDATE_INTERVAL = 1000*1000; // in microseconds
|
||||
constexpr int C_MAIN_VIEW_UPDATE = 3;
|
||||
constexpr bigtime_t GRACEFUL_SHUTDOWN_UPDATE_INTERVAL = 1000*1100; // in microseconds, ~ 1 sec
|
||||
constexpr int GRACEFUL_SHUTDOWN_UPDATE_COUNT = 600; // 10 minutes
|
||||
|
||||
class MainWindowView: public BStringView
|
||||
{
|
||||
public:
|
||||
MainWindowView (BRect r);
|
||||
|
||||
private:
|
||||
void Draw (BRect updateRect) override;
|
||||
|
||||
};
|
||||
constexpr bigtime_t MAIN_VIEW_UPDATE_INTERVAL = 5000*1000; // in miscroseconds, 5 secs
|
||||
|
||||
class MainWindow: public BWindow
|
||||
{
|
||||
@@ -49,9 +41,12 @@ class MainWindow: public BWindow
|
||||
private:
|
||||
void MessageReceived (BMessage * msg) override;
|
||||
|
||||
void UpdateMainView ();
|
||||
|
||||
private:
|
||||
BMessenger m_Messenger;
|
||||
std::unique_ptr<BMessageRunner> m_GracefulShutdownTimer;
|
||||
BStringView * m_MainView;
|
||||
std::unique_ptr<BMessageRunner> m_MainViewUpdateTimer, m_GracefulShutdownTimer;
|
||||
};
|
||||
|
||||
class I2PApp: public BApplication
|
||||
@@ -60,24 +55,6 @@ class I2PApp: public BApplication
|
||||
I2PApp ();
|
||||
};
|
||||
|
||||
MainWindowView::MainWindowView (BRect r):
|
||||
BStringView (r, nullptr, nullptr, B_FOLLOW_ALL, B_WILL_DRAW)
|
||||
{
|
||||
SetViewColor (255, 255, 255);
|
||||
SetHighColor (0xD4, 0x3B, 0x69);
|
||||
BFont font = *be_plain_font;
|
||||
font.SetSize (12);
|
||||
SetFont (&font);
|
||||
}
|
||||
|
||||
void MainWindowView::Draw (BRect updateRect)
|
||||
{
|
||||
std::stringstream s;
|
||||
i2p::util::PrintMainWindowText (s);
|
||||
SetText (s.str ().c_str ());
|
||||
BStringView::Draw (updateRect);
|
||||
}
|
||||
|
||||
MainWindow::MainWindow ():
|
||||
BWindow (BRect(100, 100, 500, 400), "i2pd " VERSION, B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE),
|
||||
m_Messenger (nullptr, this)
|
||||
@@ -89,9 +66,22 @@ MainWindow::MainWindow ():
|
||||
runMenu->AddItem (new BMenuItem ("Graceful shutdown", new BMessage (M_GRACEFUL_SHUTDOWN), 'G'));
|
||||
runMenu->AddItem (new BMenuItem ("Quit", new BMessage (B_QUIT_REQUESTED), 'Q'));
|
||||
menuBar->AddItem (runMenu);
|
||||
r = Bounds (); r.left = 20; r.top = 21;
|
||||
auto view = new MainWindowView (r);
|
||||
AddChild (view);
|
||||
m_MainView = new BStringView (BRect (20, 21, 300, 250), nullptr, "Starting...", B_FOLLOW_ALL, B_WILL_DRAW);
|
||||
m_MainView->SetViewColor (255, 255, 255);
|
||||
m_MainView->SetHighColor (0xD4, 0x3B, 0x69);
|
||||
BFont font = *be_plain_font;
|
||||
font.SetSize (12);
|
||||
m_MainView->SetFont (&font);
|
||||
AddChild (m_MainView);
|
||||
m_MainViewUpdateTimer = std::make_unique<BMessageRunner>(m_Messenger,
|
||||
BMessage (C_MAIN_VIEW_UPDATE), MAIN_VIEW_UPDATE_INTERVAL);
|
||||
}
|
||||
|
||||
void MainWindow::UpdateMainView ()
|
||||
{
|
||||
std::stringstream s;
|
||||
i2p::util::PrintMainWindowText (s);
|
||||
m_MainView->SetText (s.str ().c_str ());
|
||||
}
|
||||
|
||||
void MainWindow::MessageReceived (BMessage * msg)
|
||||
@@ -99,23 +89,36 @@ void MainWindow::MessageReceived (BMessage * msg)
|
||||
if (!msg) return;
|
||||
switch (msg->what)
|
||||
{
|
||||
case C_MAIN_VIEW_UPDATE:
|
||||
UpdateMainView ();
|
||||
break;
|
||||
case M_GRACEFUL_SHUTDOWN:
|
||||
if (!m_GracefulShutdownTimer)
|
||||
{
|
||||
i2p::context.SetAcceptsTunnels (false);
|
||||
Daemon.gracefulShutdownInterval = GRACEFUL_SHUTDOWN_UPDATE_COUNT;
|
||||
m_MainViewUpdateTimer = nullptr;
|
||||
m_GracefulShutdownTimer = std::make_unique<BMessageRunner>(m_Messenger,
|
||||
BMessage (C_GRACEFUL_SHUTDOWN_UPDATE), GRACEFUL_SHUTDOWN_UPDATE_INTERVAL);
|
||||
}
|
||||
break;
|
||||
case C_GRACEFUL_SHUTDOWN_UPDATE:
|
||||
if (Daemon.gracefulShutdownInterval > 0) Daemon.gracefulShutdownInterval--;
|
||||
if (Daemon.gracefulShutdownInterval > 0)
|
||||
{
|
||||
UpdateMainView ();
|
||||
Daemon.gracefulShutdownInterval--;
|
||||
}
|
||||
if (!Daemon.gracefulShutdownInterval || i2p::tunnel::tunnels.CountTransitTunnels () <= 0)
|
||||
{
|
||||
m_GracefulShutdownTimer = nullptr;
|
||||
Daemon.gracefulShutdownInterval = 0;
|
||||
m_Messenger.SendMessage (B_QUIT_REQUESTED);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case B_QUIT_REQUESTED:
|
||||
m_MainViewUpdateTimer = nullptr;
|
||||
m_GracefulShutdownTimer = nullptr;
|
||||
BWindow::MessageReceived (msg);
|
||||
break;
|
||||
default:
|
||||
BWindow::MessageReceived (msg);
|
||||
|
Reference in New Issue
Block a user