[Xfce4-commits] [panel-plugins/xfce4-whiskermenu-plugin] 03/04: Add confirmation dialog to command buttons.
noreply at xfce.org
noreply at xfce.org
Sat Sep 8 17:50:44 CEST 2018
This is an automated email from the git hooks/post-receive script.
g o t t c o d e p u s h e d a c o m m i t t o b r a n c h m a s t e r
in repository panel-plugins/xfce4-whiskermenu-plugin.
commit 6ff7364a1304418d10c0dc06af799d07c33c10bc
Author: Graeme Gott <graeme at gottcode.org>
Date: Fri Aug 31 19:29:52 2018 -0400
Add confirmation dialog to command buttons.
---
panel-plugin/command.cpp | 108 ++++++++++++++++++++++++++++------
panel-plugin/command.h | 20 ++++++-
panel-plugin/configuration-dialog.cpp | 20 +++++++
panel-plugin/configuration-dialog.h | 2 +
panel-plugin/settings.cpp | 26 ++++++--
panel-plugin/settings.h | 1 +
6 files changed, 152 insertions(+), 25 deletions(-)
diff --git a/panel-plugin/command.cpp b/panel-plugin/command.cpp
index 18d6ba6..a4cb17e 100644
--- a/panel-plugin/command.cpp
+++ b/panel-plugin/command.cpp
@@ -37,16 +37,29 @@ enum
//-----------------------------------------------------------------------------
-Command::Command(const gchar* icon, const gchar* text, const gchar* command, const gchar* error_text) :
+Command::Command(const gchar* icon, const gchar* text, const gchar* command, const gchar* error_text, const gchar* confirm_question, const gchar* confirm_status) :
m_button(NULL),
m_menuitem(NULL),
m_icon(g_strdup(icon)),
- m_text(g_strdup(text)),
+ m_mnemonic(g_strdup(text)),
m_command(g_strdup(command)),
m_error_text(g_strdup(error_text)),
m_status(WHISKERMENU_COMMAND_UNCHECKED),
- m_shown(true)
+ m_shown(true),
+ m_timeout_details({NULL, g_strdup(confirm_question), g_strdup(confirm_status), 0})
{
+ std::string mnemonic(text ? text : "");
+ for (std::string::size_type i = 0, length = mnemonic.length(); i < length; ++i)
+ {
+ if (mnemonic[i] == '_')
+ {
+ mnemonic.erase(i, 1);
+ --length;
+ --i;
+ }
+ }
+ m_text = g_strdup(mnemonic.c_str());
+
check();
}
@@ -66,9 +79,12 @@ Command::~Command()
}
g_free(m_icon);
+ g_free(m_mnemonic);
g_free(m_text);
g_free(m_command);
g_free(m_error_text);
+ g_free(m_timeout_details.question);
+ g_free(m_timeout_details.status);
}
//-----------------------------------------------------------------------------
@@ -80,21 +96,10 @@ GtkWidget* Command::get_button()
return m_button;
}
- std::string tooltip(m_text ? m_text : "");
- for (std::string::size_type i = 0, length = tooltip.length(); i < length; ++i)
- {
- if (tooltip[i] == '_')
- {
- tooltip.erase(i, 1);
- --length;
- --i;
- }
- }
-
m_button = gtk_button_new();
gtk_button_set_relief(GTK_BUTTON(m_button), GTK_RELIEF_NONE);
- gtk_widget_set_tooltip_text(m_button, tooltip.c_str());
- g_signal_connect_slot<GtkButton*>(m_button, "clicked", &Command::activate, this);
+ gtk_widget_set_tooltip_text(m_button, m_text);
+ g_signal_connect_slot<GtkButton*>(m_button, "clicked", &Command::activate, this, true);
GtkWidget* image = gtk_image_new_from_icon_name(m_icon, GTK_ICON_SIZE_LARGE_TOOLBAR);
gtk_container_add(GTK_CONTAINER(m_button), GTK_WIDGET(image));
@@ -117,7 +122,7 @@ GtkWidget* Command::get_menuitem()
}
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
- m_menuitem = gtk_image_menu_item_new_with_mnemonic(m_text);
+ m_menuitem = gtk_image_menu_item_new_with_mnemonic(m_mnemonic);
GtkWidget* image = gtk_image_new_from_icon_name(m_icon, GTK_ICON_SIZE_MENU);
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(m_menuitem), image);
g_signal_connect_slot<GtkMenuItem*>(m_menuitem, "activate", &Command::activate, this);
@@ -204,6 +209,14 @@ void Command::check()
void Command::activate()
{
+ if (wm_settings->confirm_session_command
+ && m_timeout_details.question
+ && m_timeout_details.status
+ && !confirm())
+ {
+ return;
+ }
+
GError* error = NULL;
if (g_spawn_command_line_async(m_command, &error) == false)
{
@@ -213,3 +226,64 @@ void Command::activate()
}
//-----------------------------------------------------------------------------
+
+// Adapted from https://git.xfce.org/xfce/xfce4-panel/tree/plugins/actions/actions.c
+bool Command::confirm()
+{
+ // Create dialog
+ m_timeout_details.dialog = gtk_message_dialog_new(NULL, GtkDialogFlags(0),
+ GTK_MESSAGE_QUESTION, GTK_BUTTONS_CANCEL,
+ "%s", m_timeout_details.question);
+ GtkDialog* dialog = GTK_DIALOG(m_timeout_details.dialog);
+
+ GtkWindow* window = GTK_WINDOW(m_timeout_details.dialog);
+ gtk_window_set_keep_above(window, true);
+ gtk_window_stick(window);
+ gtk_window_set_skip_taskbar_hint(window, true);
+ gtk_window_set_title(window, m_text);
+
+ // Add icon
+G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+ GtkWidget* image = gtk_image_new_from_icon_name(m_icon, GTK_ICON_SIZE_DIALOG);
+ gtk_widget_show(image);
+ gtk_message_dialog_set_image(GTK_MESSAGE_DIALOG(dialog), image);
+G_GNUC_END_IGNORE_DEPRECATIONS
+
+ // Create accept button
+ gtk_dialog_add_button(dialog, m_mnemonic, GTK_RESPONSE_ACCEPT);
+ gtk_dialog_set_default_response(dialog, GTK_RESPONSE_ACCEPT);
+
+ // Run dialog
+ m_timeout_details.time_left = 60;
+ guint timeout_id = g_timeout_add(1000, &Command::confirm_countdown, &m_timeout_details);
+ confirm_countdown(&m_timeout_details);
+
+ gint result = gtk_dialog_run(dialog);
+
+ g_source_remove(timeout_id);
+ gtk_widget_destroy(m_timeout_details.dialog);
+ m_timeout_details.dialog = NULL;
+
+ return result == GTK_RESPONSE_ACCEPT;
+}
+
+//-----------------------------------------------------------------------------
+
+gboolean Command::confirm_countdown(gpointer data)
+{
+ TimeoutDetails* details = static_cast<TimeoutDetails*>(data);
+
+ if (details->time_left == 0)
+ {
+ gtk_dialog_response(GTK_DIALOG(details->dialog), GTK_RESPONSE_ACCEPT);
+ }
+ else
+ {
+ gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(details->dialog),
+ details->status, details->time_left);
+ }
+
+ return --details->time_left >= 0;
+}
+
+//-----------------------------------------------------------------------------
diff --git a/panel-plugin/command.h b/panel-plugin/command.h
index 53753ed..36c3304 100644
--- a/panel-plugin/command.h
+++ b/panel-plugin/command.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013 Graeme Gott <graeme at gottcode.org>
+ * Copyright (C) 2013, 2018 Graeme Gott <graeme at gottcode.org>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -26,7 +26,7 @@ namespace WhiskerMenu
class Command
{
public:
- Command(const gchar* icon, const gchar* text, const gchar* command, const gchar* error_text);
+ Command(const gchar* icon, const gchar* text, const gchar* command, const gchar* error_text, const gchar* confirm_question = NULL, const gchar* confirm_status = NULL);
~Command();
GtkWidget* get_button();
@@ -44,7 +44,7 @@ public:
const gchar* get_text() const
{
- return m_text;
+ return m_mnemonic;
}
void set(const gchar* command);
@@ -56,14 +56,28 @@ public:
void activate();
private:
+ bool confirm();
+ static gboolean confirm_countdown(gpointer data);
+
+private:
GtkWidget* m_button;
GtkWidget* m_menuitem;
gchar* m_icon;
+ gchar* m_mnemonic;
gchar* m_text;
gchar* m_command;
gchar* m_error_text;
int m_status;
bool m_shown;
+
+ struct TimeoutDetails
+ {
+ GtkWidget* dialog;
+ gchar* question;
+ gchar* status;
+ gint time_left;
+ }
+ m_timeout_details;
};
}
diff --git a/panel-plugin/configuration-dialog.cpp b/panel-plugin/configuration-dialog.cpp
index 81f733b..37445b5 100644
--- a/panel-plugin/configuration-dialog.cpp
+++ b/panel-plugin/configuration-dialog.cpp
@@ -284,6 +284,14 @@ void ConfigurationDialog::toggle_stay_on_focus_out(GtkToggleButton* button)
//-----------------------------------------------------------------------------
+void ConfigurationDialog::toggle_confirm_session_command(GtkToggleButton* button)
+{
+ wm_settings->confirm_session_command = gtk_toggle_button_get_active(button);
+ wm_settings->set_modified();
+}
+
+//-----------------------------------------------------------------------------
+
void ConfigurationDialog::recent_items_max_changed(GtkSpinButton* button)
{
wm_settings->recent_items_max = gtk_spin_button_get_value_as_int(button);
@@ -763,6 +771,18 @@ GtkWidget* ConfigurationDialog::init_behavior_tab()
gtk_widget_set_sensitive(GTK_WIDGET(m_display_recent), wm_settings->recent_items_max);
g_signal_connect_slot(m_display_recent, "toggled", &ConfigurationDialog::toggle_display_recent, this);
+
+ // Create command buttons section
+ GtkBox* command_vbox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 6));
+ GtkWidget* command_frame = make_aligned_frame(_("Session Commands"), GTK_WIDGET(command_vbox));
+ gtk_box_pack_start(page, command_frame, false, false, 0);
+
+ // Add option to show confirmation dialogs
+ m_confirm_session_command = gtk_check_button_new_with_mnemonic(_("Show c_onfirmation dialog"));
+ gtk_box_pack_start(command_vbox, m_confirm_session_command, true, true, 0);
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_confirm_session_command), wm_settings->confirm_session_command);
+ g_signal_connect_slot(m_confirm_session_command, "toggled", &ConfigurationDialog::toggle_confirm_session_command, this);
+
return GTK_WIDGET(page);
}
diff --git a/panel-plugin/configuration-dialog.h b/panel-plugin/configuration-dialog.h
index 0ce5655..afafa7d 100644
--- a/panel-plugin/configuration-dialog.h
+++ b/panel-plugin/configuration-dialog.h
@@ -50,6 +50,7 @@ private:
void toggle_position_commands_alternate(GtkToggleButton* button);
void toggle_position_categories_alternate(GtkToggleButton* button);
void toggle_stay_on_focus_out(GtkToggleButton* button);
+ void toggle_confirm_session_command(GtkToggleButton* button);
void category_icon_size_changed(GtkComboBox* combo);
void item_icon_size_changed(GtkComboBox* combo);
@@ -95,6 +96,7 @@ private:
GtkWidget* m_position_commands_alternate;
GtkWidget* m_position_categories_alternate;
GtkWidget* m_stay_on_focus_out;
+ GtkWidget* m_confirm_session_command;
GtkWidget* m_category_icon_size;
GtkWidget* m_item_icon_size;
GtkWidget* m_background_opacity;
diff --git a/panel-plugin/settings.cpp b/panel-plugin/settings.cpp
index 097457d..c332bb5 100644
--- a/panel-plugin/settings.cpp
+++ b/panel-plugin/settings.cpp
@@ -113,6 +113,8 @@ Settings::Settings() :
position_categories_alternate(false),
stay_on_focus_out(false),
+ confirm_session_command(true),
+
menu_width(400),
menu_height(500),
menu_opacity(100)
@@ -137,23 +139,33 @@ Settings::Settings() :
command[CommandLogOutUser] = new Command("system-log-out",
_("Log _Out"),
"xfce4-session-logout --logout --fast",
- _("Failed to log out."));
+ _("Failed to log out."),
+ _("Are you sure you want to log out?"),
+ _("Logging out in %d seconds."));
command[CommandRestart] = new Command("system-reboot",
_("_Restart"),
"xfce4-session-logout --reboot --fast",
- _("Failed to restart."));
+ _("Failed to restart."),
+ _("Are you sure you want to restart?"),
+ _("Restarting computer in %d seconds."));
command[CommandShutDown] = new Command("system-shutdown",
_("Shut _Down"),
"xfce4-session-logout --halt --fast",
- _("Failed to shut down."));
+ _("Failed to shut down."),
+ _("Are you sure you want to shut down?"),
+ _("Turning off computer in %d seconds."));
command[CommandSuspend] = new Command("system-suspend",
_("Suspe_nd"),
"xfce4-session-logout --suspend",
- _("Failed to suspend."));
+ _("Failed to suspend."),
+ _("Do you want to suspend to RAM?"),
+ _("Suspending computer in %d seconds."));
command[CommandHibernate] = new Command("system-hibernate",
_("_Hibernate"),
"xfce4-session-logout --hibernate",
- _("Failed to hibernate."));
+ _("Failed to hibernate."),
+ _("Do you want to suspend to disk?"),
+ _("Hibernating computer in %d seconds."));
command[CommandLogOut] = new Command("system-log-out",
_("Log Ou_t..."),
"xfce4-session-logout",
@@ -237,6 +249,8 @@ void Settings::load(char* file)
position_categories_alternate = xfce_rc_read_bool_entry(rc, "position-categories-alternate", position_categories_alternate);
stay_on_focus_out = xfce_rc_read_bool_entry(rc, "stay-on-focus-out", stay_on_focus_out);
+ confirm_session_command = xfce_rc_read_bool_entry(rc, "confirm-session-command", confirm_session_command);
+
menu_width = std::max(10, xfce_rc_read_int_entry(rc, "menu-width", menu_width));
menu_height = std::max(10, xfce_rc_read_int_entry(rc, "menu-height", menu_height));
menu_opacity = std::min(100, std::max(0, xfce_rc_read_int_entry(rc, "menu-opacity", menu_height)));
@@ -336,6 +350,8 @@ void Settings::save(char* file)
xfce_rc_write_bool_entry(rc, "position-categories-alternate", position_categories_alternate);
xfce_rc_write_bool_entry(rc, "stay-on-focus-out", stay_on_focus_out);
+ xfce_rc_write_bool_entry(rc, "confirm-session-command", confirm_session_command);
+
xfce_rc_write_int_entry(rc, "menu-width", menu_width);
xfce_rc_write_int_entry(rc, "menu-height", menu_height);
xfce_rc_write_int_entry(rc, "menu-opacity", menu_opacity);
diff --git a/panel-plugin/settings.h b/panel-plugin/settings.h
index 90b04d5..678f642 100644
--- a/panel-plugin/settings.h
+++ b/panel-plugin/settings.h
@@ -101,6 +101,7 @@ public:
CountCommands
};
Command* command[CountCommands];
+ bool confirm_session_command;
std::vector<SearchAction*> search_actions;
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the Xfce4-commits
mailing list