user_interface: terminate bot asynchronously

This commit is contained in:
Syping 2026-04-16 22:52:34 +02:00
parent 12bcfe2f9b
commit 2d3f3cc365
2 changed files with 24 additions and 7 deletions

View file

@ -100,7 +100,7 @@ user_interface::user_interface()
m_stop_button = Gtk::make_managed<Gtk::Button>("Stop"); m_stop_button = Gtk::make_managed<Gtk::Button>("Stop");
m_stop_button->set_size_request(80, -1); m_stop_button->set_size_request(80, -1);
m_stop_button->set_sensitive(false); m_stop_button->set_sensitive(false);
m_stop_button->signal_clicked().connect(sigc::mem_fun(*this, &user_interface::terminate)); m_stop_button->signal_clicked().connect(sigc::mem_fun(*this, &user_interface::terminate_async));
button_box->append(*m_stop_button); button_box->append(*m_stop_button);
const std::string token = m_user_config.get_token(); const std::string token = m_user_config.get_token();
@ -117,6 +117,7 @@ user_interface::user_interface()
m_log_callback = std::bind(&user_interface::log_append, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); m_log_callback = std::bind(&user_interface::log_append, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
m_bot.log_callback_add(m_log_callback); m_bot.log_callback_add(m_log_callback);
m_log_dispatcher.connect(sigc::mem_fun(*this, &user_interface::on_log_dispatched)); m_log_dispatcher.connect(sigc::mem_fun(*this, &user_interface::on_log_dispatched));
m_terminate_dispatcher.connect(sigc::mem_fun(*this, &user_interface::on_terminate_dispatched));
set_child(*vertical_box); set_child(*vertical_box);
} }
@ -164,13 +165,12 @@ void user_interface::run() {
void user_interface::terminate() { void user_interface::terminate() {
log_append("Stopping bot...", "Launch"); log_append("Stopping bot...", "Launch");
m_bot.terminate(); m_bot.terminate();
bool token_valid = bot::regex::verify_discord_bot_token(m_token_entry->get_text()); m_terminate_dispatcher.emit();
m_start_button->set_sensitive(token_valid); }
void user_interface::terminate_async() {
m_stop_button->set_sensitive(false); m_stop_button->set_sensitive(false);
m_token_entry->set_sensitive(true); m_terminate_thread = std::make_unique<std::thread>(&user_interface::terminate, this);
bool translator_configureable = m_translator_dropdown->get_selected() > 0;
m_translator_configure_button->set_sensitive(translator_configureable);
m_translator_dropdown->set_sensitive(true);
} }
void user_interface::on_log_dispatched() { void user_interface::on_log_dispatched() {
@ -180,6 +180,19 @@ void user_interface::on_log_dispatched() {
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &user_interface::log_scroll_down)); Glib::signal_idle().connect_once(sigc::mem_fun(*this, &user_interface::log_scroll_down));
} }
void user_interface::on_terminate_dispatched() {
bool token_valid = bot::regex::verify_discord_bot_token(m_token_entry->get_text());
m_start_button->set_sensitive(token_valid);
m_token_entry->set_sensitive(true);
bool translator_configureable = m_translator_dropdown->get_selected() > 0;
m_translator_configure_button->set_sensitive(translator_configureable);
m_translator_dropdown->set_sensitive(true);
if (std::thread *terminate_thread = m_terminate_thread.get()) {
terminate_thread->join();
m_terminate_thread.reset();
}
}
void user_interface::on_token_entry_changed() { void user_interface::on_token_entry_changed() {
bool token_valid = bot::regex::verify_discord_bot_token(m_token_entry->get_text()); bool token_valid = bot::regex::verify_discord_bot_token(m_token_entry->get_text());
m_start_button->set_sensitive(!m_bot.is_running() ? token_valid : false); m_start_button->set_sensitive(!m_bot.is_running() ? token_valid : false);

View file

@ -35,7 +35,9 @@ namespace bot {
void log_scroll_down(); void log_scroll_down();
void run(); void run();
void terminate(); void terminate();
void terminate_async();
void on_log_dispatched(); void on_log_dispatched();
void on_terminate_dispatched();
void on_token_entry_changed(); void on_token_entry_changed();
void on_translator_configure_pressed(); void on_translator_configure_pressed();
void on_translator_dropdown_changed(); void on_translator_dropdown_changed();
@ -50,6 +52,8 @@ namespace bot {
Gtk::TextView* m_log_textview; Gtk::TextView* m_log_textview;
Gtk::Button* m_start_button; Gtk::Button* m_start_button;
Gtk::Button* m_stop_button; Gtk::Button* m_stop_button;
Glib::Dispatcher m_terminate_dispatcher;
std::unique_ptr<std::thread> m_terminate_thread;
Gtk::PasswordEntry* m_token_entry; Gtk::PasswordEntry* m_token_entry;
Gtk::Button* m_translator_configure_button; Gtk::Button* m_translator_configure_button;
Gtk::DropDown* m_translator_dropdown; Gtk::DropDown* m_translator_dropdown;