From 427082b52f1ec2cf91bcdf1e976e2433e90a072a Mon Sep 17 00:00:00 2001 From: huangjiawen Date: Wed, 29 Mar 2023 14:01:24 +0800 Subject: [PATCH 2/2] feature(menu): Add UI for modal power options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加模态电源选项功能界面 Related #67101 Signed-off-by: huangjiawen --- config.h.in | 1 + icons/kiran-power-hibernate.svg | 42 ++++++ icons/kiran-power-lock_screen.svg | 42 ++++++ icons/kiran-power-logout.svg | 42 ++++++ icons/kiran-power-reboot.svg | 42 ++++++ icons/kiran-power-shutdown.svg | 42 ++++++ icons/kiran-power-suspend.svg | 42 ++++++ icons/kiran-power-switch_user.svg | 42 ++++++ src/menu/menu-applet-window.cpp | 21 ++- src/menu/menu-applet-window.h | 2 + src/menu/menu-power-button.cpp | 19 +++ src/menu/menu-power-button.h | 6 + src/menu/menu-power-dialog.cpp | 216 ++++++++++++++++++++++++++++++ src/menu/menu-power-dialog.h | 43 ++++++ 14 files changed, 601 insertions(+), 1 deletion(-) create mode 100644 icons/kiran-power-hibernate.svg create mode 100644 icons/kiran-power-lock_screen.svg create mode 100644 icons/kiran-power-logout.svg create mode 100644 icons/kiran-power-reboot.svg create mode 100644 icons/kiran-power-shutdown.svg create mode 100644 icons/kiran-power-suspend.svg create mode 100644 icons/kiran-power-switch_user.svg create mode 100644 src/menu/menu-power-dialog.cpp create mode 100644 src/menu/menu-power-dialog.h diff --git a/config.h.in b/config.h.in index 33b04f2..7369f6f 100644 --- a/config.h.in +++ b/config.h.in @@ -5,4 +5,5 @@ #define PACKAGE_VERSION "@PROJECT_VERSION@" #define GETTEXT_PACKAGE PACKAGE_NAME #define COPYRIGHT "Copyright ©2020 KylinSec. All rights reserved." +#cmakedefine POWER_DIALOG diff --git a/icons/kiran-power-hibernate.svg b/icons/kiran-power-hibernate.svg new file mode 100644 index 0000000..146b603 --- /dev/null +++ b/icons/kiran-power-hibernate.svg @@ -0,0 +1,42 @@ + + + + + + Adobe Photoshop CC 2017 (Windows) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/icons/kiran-power-lock_screen.svg b/icons/kiran-power-lock_screen.svg new file mode 100644 index 0000000..0dea8ec --- /dev/null +++ b/icons/kiran-power-lock_screen.svg @@ -0,0 +1,42 @@ + + + + + + Adobe Photoshop CC 2017 (Windows) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/icons/kiran-power-logout.svg b/icons/kiran-power-logout.svg new file mode 100644 index 0000000..b7a1645 --- /dev/null +++ b/icons/kiran-power-logout.svg @@ -0,0 +1,42 @@ + + + + + + Adobe Photoshop CC 2017 (Windows) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/icons/kiran-power-reboot.svg b/icons/kiran-power-reboot.svg new file mode 100644 index 0000000..98de3b5 --- /dev/null +++ b/icons/kiran-power-reboot.svg @@ -0,0 +1,42 @@ + + + + + + Adobe Photoshop CC 2017 (Windows) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/icons/kiran-power-shutdown.svg b/icons/kiran-power-shutdown.svg new file mode 100644 index 0000000..e36fa4a --- /dev/null +++ b/icons/kiran-power-shutdown.svg @@ -0,0 +1,42 @@ + + + + + + Adobe Photoshop CC 2017 (Windows) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/icons/kiran-power-suspend.svg b/icons/kiran-power-suspend.svg new file mode 100644 index 0000000..c6ea79b --- /dev/null +++ b/icons/kiran-power-suspend.svg @@ -0,0 +1,42 @@ + + + + + + Adobe Photoshop CC 2017 (Windows) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/icons/kiran-power-switch_user.svg b/icons/kiran-power-switch_user.svg new file mode 100644 index 0000000..42d584a --- /dev/null +++ b/icons/kiran-power-switch_user.svg @@ -0,0 +1,42 @@ + + + + + + Adobe Photoshop CC 2017 (Windows) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/menu/menu-applet-window.cpp b/src/menu/menu-applet-window.cpp index 81397a4..2637eca 100644 --- a/src/menu/menu-applet-window.cpp +++ b/src/menu/menu-applet-window.cpp @@ -21,14 +21,16 @@ #include "lib/base.h" #include "menu-app-launcher-button.h" #include "menu-power-button.h" +#include "menu-power-dialog.h" #include "window-manager.h" - +#include "global.h" #include #include #include #include "global.h" #include "menu-apps-container.h" #include "recent-files-widget.h" +#include "config.h" #define NEW_APPS_MAX_SIZE 3 @@ -643,6 +645,18 @@ Gtk::Button *MenuAppletWindow::create_launcher_button(const std::string &icon_na return button; } +Gtk::Button *MenuAppletWindow::create_power_dialog_button() +{ + MenuPowerButton *button; + button = Gtk::make_managed(); + + set_transient_for(*this); + + button->signal_menu_hide().connect(sigc::mem_fun(*this, &Gtk::Widget::hide)); + + return button; +} + void MenuAppletWindow::add_sidebar_buttons() { Gtk::Separator *separator; @@ -685,7 +699,12 @@ void MenuAppletWindow::add_sidebar_buttons() "mate-system-monitor"); side_box->add(*launcher_btn); + +#ifdef POWER_DIALOG + auto power_btn = create_power_dialog_button(); +#else auto power_btn = Gtk::make_managed(); +#endif side_box->add(*power_btn); side_box->show_all(); diff --git a/src/menu/menu-applet-window.h b/src/menu/menu-applet-window.h index 7a4d66d..d34cf5d 100644 --- a/src/menu/menu-applet-window.h +++ b/src/menu/menu-applet-window.h @@ -242,4 +242,6 @@ private: Gtk::Button *create_page_button(const char *icon_resource, const char *tooltip, int page_index); + + Gtk::Button *create_power_dialog_button(); }; diff --git a/src/menu/menu-power-button.cpp b/src/menu/menu-power-button.cpp index 0237153..622481b 100644 --- a/src/menu/menu-power-button.cpp +++ b/src/menu/menu-power-button.cpp @@ -13,7 +13,10 @@ */ #include "menu-power-button.h" +#include "menu-power-dialog.h" #include +#include + #include "kiran-helper.h" MenuPowerButton::MenuPowerButton() : menu(nullptr) @@ -32,7 +35,12 @@ MenuPowerButton::MenuPowerButton() : menu(nullptr) MenuPowerButton::~MenuPowerButton() { +#ifdef POWER_DIALOG + delete dialog_; +#else delete menu; +#endif + } void MenuPowerButton::on_clicked() @@ -42,10 +50,21 @@ void MenuPowerButton::on_clicked() if (menu) delete menu; +#ifdef POWER_DIALOG + dialog_ = new MenuPowerDialog(); + dialog_->show_all(); +#else menu = new MenuPowerMenu(); menu->attach_to_widget(*this); menu->show_all(); menu->popup_at_widget(this, Gdk::GRAVITY_SOUTH_EAST, Gdk::GRAVITY_SOUTH_WEST, event); +#endif gdk_event_free(event); } + + +sigc::signal MenuPowerButton::signal_menu_hide() +{ + return m_signal_menu_hide; +} diff --git a/src/menu/menu-power-button.h b/src/menu/menu-power-button.h index fb94225..82151b0 100644 --- a/src/menu/menu-power-button.h +++ b/src/menu/menu-power-button.h @@ -16,6 +16,7 @@ #define MENU_POWER_BUTTON_H #include +#include "config.h" #include "menu-power-menu.h" class MenuPowerButton : public Gtk::Button @@ -23,13 +24,18 @@ class MenuPowerButton : public Gtk::Button public: MenuPowerButton(); ~MenuPowerButton(); + sigc::signal signal_menu_hide(); protected: virtual void on_clicked() override; + sigc::signal m_signal_menu_hide; private: Gtk::Image icon; Gtk::Menu *menu; +#ifdef POWER_DIALOG + Gtk::Window *dialog_; +#endif }; #endif // MENU_POWER_BUTTON_H diff --git a/src/menu/menu-power-dialog.cpp b/src/menu/menu-power-dialog.cpp new file mode 100644 index 0000000..1d75cf1 --- /dev/null +++ b/src/menu/menu-power-dialog.cpp @@ -0,0 +1,216 @@ +/** + * Copyright (c) 2020 ~ 2021 KylinSec Co., Ltd. + * kiran-cc-daemon is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + * + * Author: huangjiawen + */ + +#include "menu-power-dialog.h" +#include +#include +#include +#include +#include "global.h" +#include "kiran-helper.h" +#include "kiran-power.h" + +MenuPowerDialog::MenuPowerDialog() +{ + Gtk::HeaderBar *dialog_header_bar; + Gtk::Frame *dialog_frame; + Gtk::Box *dialog_vbox; + Gtk::Box *dialog_hbox; + Gtk::Grid *dialog_grid; + + dialog_header_bar = Gtk::make_managed(); + dialog_header_bar->set_decoration_layout("menu,icon:close"); + dialog_header_bar->set_show_close_button(); + set_titlebar(*dialog_header_bar); + + set_modal(); + set_title(_("Shutdown")); + set_border_width(5); + set_icon_name("kiran-power-shutdown"); + set_position(Gtk::WIN_POS_CENTER); + + dialog_frame = Gtk::make_managed(); + dialog_frame->set_shadow_type(Gtk::SHADOW_OUT); + add(*dialog_frame); + + Gdk::RGBA color; + color.set_rgba(0.20, 0.20, 0.20, 1); + dialog_header_bar->override_background_color(color, Gtk::STATE_FLAG_NORMAL); + dialog_frame->override_background_color(color, Gtk::STATE_FLAG_NORMAL); + + dialog_vbox = Gtk::make_managed(Gtk::ORIENTATION_VERTICAL); + dialog_vbox->set_border_width(20); + dialog_frame->add(*dialog_vbox); + + dialog_hbox = Gtk::make_managed(Gtk::ORIENTATION_HORIZONTAL); + dialog_hbox->set_border_width(35); + dialog_vbox->pack_start(*dialog_hbox, Gtk::PACK_EXPAND_PADDING); + + dialog_grid = Gtk::make_managed(); + dialog_grid->set_row_spacing(5); + dialog_grid->set_column_spacing(10); + dialog_hbox->pack_start(*dialog_grid, Gtk::PACK_EXPAND_PADDING); + + add_power_button(dialog_grid); + + get_style_context()->add_class("power-menu-dialog"); +} + +void MenuPowerDialog::add_power_button(Gtk::Grid *dialog_grid) +{ + gint space = 0; + auto power = KiranPower::get_default(); + + if (power->can_shutdown()) + { + space += 1; + add_button("kiran-power-shutdown", "Shutdown", dialog_grid, space, &MenuPowerDialog::do_shutdown); + } + + if (power->can_reboot()) + { + space += 1; + add_button("kiran-power-reboot", "Reboot", dialog_grid, space, &MenuPowerDialog::do_reboot); + } + + if (power->can_logout()) + { + space += 1; + add_button("kiran-power-logout", "Logout", dialog_grid, space, &MenuPowerDialog::do_logout); + } + + if (power->can_hibernate()) + { + space += 1; + add_button("kiran-power-hibernate", "Hibernate", dialog_grid, space, &MenuPowerDialog::do_hibernate); + } + + if (power->can_lock_screen()) + { + space += 1; + add_button("kiran-power-lock_screen", "Lock screen", dialog_grid, space, &MenuPowerDialog::do_lock_screen); + } + + if (power->can_switch_user()) + { + space += 1; + add_button("kiran-power-switch_user", "Switch user", dialog_grid, space, &MenuPowerDialog::do_switch_user); + } + + if (power->can_suspend()) + { + space += 1; + add_button("kiran-power-suspend", "Suspend", dialog_grid, space, &MenuPowerDialog::do_suspend); + } +} + +MenuPowerDialog::~MenuPowerDialog() +{ +} + +void MenuPowerDialog::add_button(const char *button_icon_name, + const char *button_name, + Gtk::Grid *dialog_grid, + int space, + void (MenuPowerDialog::*power_func)()) +{ + Gtk::Button *power_button; + Gtk::Label *power_label; + + power_button = Gtk::make_managed(); + power_button->set_size_request(60, 40); + power_button->set_image_from_icon_name(button_icon_name, Gtk::ICON_SIZE_DND); + power_button->set_relief(Gtk::RELIEF_NONE); + power_button->signal_clicked().connect(sigc::mem_fun(*this, power_func)); + + dialog_grid->attach(*power_button, space, 0, 1, 1); + + power_label = Gtk::make_managed(_(button_name)); + dialog_grid->attach(*power_label, space, 1, 1, 1); +} + +void MenuPowerDialog::do_reboot() +{ + auto power = KiranPower::get_default(); + + power->reboot(); + + return; +} + +void MenuPowerDialog::do_logout() +{ + auto power = KiranPower::get_default(); + + power->logout(LOGOUT_MODE_NOW); + + return; +} + +void MenuPowerDialog::do_shutdown() +{ + auto power = KiranPower::get_default(); + + power->shutdown(); + + return; +} + +void MenuPowerDialog::do_hibernate() +{ + auto power = KiranPower::get_default(); + + power->hibernate(); + + return; +} +void MenuPowerDialog::do_suspend() +{ + auto power = KiranPower::get_default(); + + power->suspend(); + + return; +} + +void MenuPowerDialog::do_switch_user() +{ + auto power = KiranPower::get_default(); + + if (power->get_graphical_ntvs() >= power->get_ntvs_total()) + { + KLOG_DEBUG("Total ntvs: %d, graphical ntvs: %d.", power->get_ntvs_total(), power->get_graphical_ntvs()); + + Gtk::MessageDialog dialog(_("The logined users reach the maximum limit at the same time, " + "you cannot switch to the greeter interface, Please log off some logined users first."), + true, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true); + dialog.set_title(_("System Warning")); + dialog.run(); + } + else + { + power->switch_user(); + } + + return; +} + +void MenuPowerDialog::do_lock_screen() +{ + auto power = KiranPower::get_default(); + + power->lock_screen(); + + return; +} diff --git a/src/menu/menu-power-dialog.h b/src/menu/menu-power-dialog.h new file mode 100644 index 0000000..471ab42 --- /dev/null +++ b/src/menu/menu-power-dialog.h @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2020 ~ 2021 KylinSec Co., Ltd. + * kiran-cc-daemon is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + * + * Author: huangjiawen + */ + +#ifndef MENU_POWER_DIALOG_H +#define MENU_POWER_DIALOG_H + +#include +#include "kiran-opacity-menu.h" + +class MenuPowerDialog : public Gtk::Window +{ +public: + MenuPowerDialog(); + ~MenuPowerDialog() override; + void do_shutdown(); + void do_reboot(); + void do_logout(); + void do_lock(); + void do_hibernate(); + void do_suspend(); + void do_switch_user(); + void do_lock_screen(); + + void add_power_button(Gtk::Grid *dialog_grid); + void add_button(const char *button_icon_name, + const char *button_name, + Gtk::Grid *dialog_grid, + int space, + void (MenuPowerDialog::*power_func)()); +}; + +#endif // MENU_POWER_DIALOG_H -- 2.33.0