From 1296b7d210321c0ef14eb11a2c0cd76c0f51278f Mon Sep 17 00:00:00 2001
From: meizhigang <meizhigang@kylinsec.com.cn>
Date: Thu, 20 Jul 2023 17:17:16 +0800
Subject: [PATCH] fix(power):Add battery charging and discharging time
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 - 添加电池充电和放电时间

 Related #9506
---
 plugins/power/tray/CMakeLists.txt |  3 ++-
 plugins/power/tray/po/zh_CN.po    | 34 ++++++++++++++++++++++++++++++-
 plugins/power/tray/power-tray.cpp | 33 +++++++++++++++++++++++++++++-
 plugins/power/tray/power-tray.h   |  3 +++
 4 files changed, 70 insertions(+), 3 deletions(-)

diff --git a/plugins/power/tray/CMakeLists.txt b/plugins/power/tray/CMakeLists.txt
index f74aa08..353065d 100644
--- a/plugins/power/tray/CMakeLists.txt
+++ b/plugins/power/tray/CMakeLists.txt
@@ -9,7 +9,8 @@ add_executable(
   ${TARGET_NAME}
   ${POWER_TRAY_H_FILES} ${POWER_TRAY_CPP_FILES}
   ${PROJECT_SOURCE_DIR}/plugins/power/wrapper/power-upower.cpp
-  ${PROJECT_SOURCE_DIR}/plugins/power/wrapper/power-upower-device.cpp)
+  ${PROJECT_SOURCE_DIR}/plugins/power/wrapper/power-upower-device.cpp
+  ${PROJECT_SOURCE_DIR}/plugins/power/power-utils.cpp)
 
 target_include_directories(${TARGET_NAME} PUBLIC ${GTKMM3_INCLUDE_DIRS})
 
diff --git a/plugins/power/tray/po/zh_CN.po b/plugins/power/tray/po/zh_CN.po
index 436167c..377037d 100644
--- a/plugins/power/tray/po/zh_CN.po
+++ b/plugins/power/tray/po/zh_CN.po
@@ -17,6 +17,38 @@ msgstr ""
 "Content-Type: text/plain; charset=UTF-8\n"
 "Content-Transfer-Encoding: 8bit\n"
 
-#: ../power-tray.cpp:103
+#: ../power-tray.cpp:132
 msgid "Remaining electricty: {0:.1f}%"
 msgstr "剩余电量：{0:.1f}%"
+
+#: ../power-tray.cpp:114
+msgid "Remaining electricty: {0:.1f}%, approximately {1} until charged"
+msgstr "剩余电量：{0:.1f}%，离充满大约{1}"
+
+#: ../power-tray.cpp:123
+msgid "Remaining electricty: {0:.1f}%, approximately provides {1} runtime"
+msgstr "剩余电量：{0:.1f}%，大约可使用{1}"
+
+#: ../plugins/power/power-utils.cpp:26
+msgid "Less than 1 minute"
+msgstr "小于1分钟"
+
+#: ../plugins/power/power-utils.cpp:30
+msgid "{0} minute"
+msgid_plural "{0} minutes"
+msgstr[0] "{0}分钟"
+
+#: ../plugins/power/power-utils.cpp:38
+msgid "{0} hour"
+msgid_plural "{0} hours"
+msgstr[0] "{0}小时"
+
+#: ../plugins/power/power-utils.cpp:44
+msgid "hour"
+msgid_plural "hours"
+msgstr[0] "小时"
+
+#: ../plugins/power/power-utils.cpp:46
+msgid "minute"
+msgid_plural "minutes"
+msgstr[0] "分钟"
\ No newline at end of file
diff --git a/plugins/power/tray/power-tray.cpp b/plugins/power/tray/power-tray.cpp
index 69f9cee..3586f6c 100644
--- a/plugins/power/tray/power-tray.cpp
+++ b/plugins/power/tray/power-tray.cpp
@@ -16,6 +16,7 @@
 
 #include <glib/gi18n.h>
 #include "plugins/power/wrapper/power-upower.h"
+#include "plugins/power/power-utils.h"
 #include "power-i.h"
 
 namespace Kiran
@@ -98,11 +99,41 @@ void PowerTray::update_status_icon()
     }
 
     // 对于电源和UPS设备需要显示电量
-    if (device_for_tray)
+    this->update_status_icon_toolstip(device_for_tray);
+}
+
+void PowerTray::update_status_icon_toolstip(std::shared_ptr<PowerUPowerDevice> device_for_tray)
+{
+    RETURN_IF_FALSE(device_for_tray);
+
+    switch (device_for_tray->get_props().state)
+    {
+    case UP_DEVICE_STATE_CHARGING:
+    {
+        auto time_to_full_text = PowerUtils::get_time_translation(device_for_tray->get_props().time_to_full);
+        auto tooltip_text = fmt::format(_("Remaining electricty: {0:.1f}%, approximately {1} until charged"),
+                                        device_for_tray->get_props().percentage,
+                                        time_to_full_text);
+        gtk_status_icon_set_tooltip_text(this->status_icon_, tooltip_text.c_str());
+        break;
+    }
+    case UP_DEVICE_STATE_DISCHARGING:
+    {
+        auto time_to_empty_text = PowerUtils::get_time_translation(device_for_tray->get_props().time_to_empty);
+        auto tooltip_text = fmt::format(_("Remaining electricty: {0:.1f}%, approximately provides {1} runtime"),
+                                        device_for_tray->get_props().percentage,
+                                        time_to_empty_text);
+        gtk_status_icon_set_tooltip_text(this->status_icon_, tooltip_text.c_str());
+        break;
+    }
+    case UP_DEVICE_STATE_FULLY_CHARGED:
+    default:
     {
         auto tooltip_text = fmt::format(_("Remaining electricty: {0:.1f}%"), device_for_tray->get_props().percentage);
         gtk_status_icon_set_tooltip_text(this->status_icon_, tooltip_text.c_str());
     }
+    break;
+    }
 }
 
 void PowerTray::delay_update_status_icon()
diff --git a/plugins/power/tray/power-tray.h b/plugins/power/tray/power-tray.h
index b496ba9..9a14a47 100644
--- a/plugins/power/tray/power-tray.h
+++ b/plugins/power/tray/power-tray.h
@@ -35,6 +35,9 @@ public:
     // 更新托盘图标
     void update_status_icon();
 
+    // 更新托盘图标提示语
+    void update_status_icon_toolstip(std::shared_ptr<PowerUPowerDevice> device_for_tray);
+
 private:
     void init();
 
-- 
2.27.0

