From 2b0afc5d1b71dae54abb80644b77fb353d94f2df Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 24 Jul 2023 19:37:20 +0800 Subject: [PATCH] =?UTF-8?q?tray=E7=82=B9=E5=87=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 28 ++++++++++++-------------- notes/tray.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 notes/tray.py diff --git a/main.py b/main.py index 6189272..a0e9701 100755 --- a/main.py +++ b/main.py @@ -7,13 +7,6 @@ gi.require_version("WebKit2", "4.1") from gi.repository import Gtk, WebKit2 -# 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon -try: - gi.require_version('AyatanaAppIndicator3', '0.1') - from gi.repository import AyatanaAppIndicator3 as AppIndicator3 -except (ValueError, ImportError): - AppIndicator3 = None - class WebKitWindow(Gtk.Window): def __init__(self): @@ -60,16 +53,21 @@ class WebKitWindow(Gtk.Window): def create_tray(self): - if AppIndicator3 : - indicator = AppIndicator3.Indicator.new( - "youtube", - "youtube", - AppIndicator3.IndicatorCategory.APPLICATION_STATUS - ) - else: - indicator = Gtk.StatusIcon.new_from_icon_name('youtube') + indicator = Gtk.StatusIcon.new_from_icon_name('youtube') + indicator.connect('activate', self.toggle_visible) return indicator + + + def toggle_visible(self, icon): + # trayIcon + print(icon) + + if self.is_active(): + self.hide() + else: + self.present() + def file_path(self, filepath): diff --git a/notes/tray.py b/notes/tray.py new file mode 100644 index 0000000..652b529 --- /dev/null +++ b/notes/tray.py @@ -0,0 +1,55 @@ + + +import gi, platform + +gi.require_version("Gtk", "3.0") + +from gi.repository import Gtk +from gi.repository.GdkPixbuf import Pixbuf + +# 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon +try: + gi.require_version('AyatanaAppIndicator3', '0.1') + from gi.repository import AyatanaAppIndicator3 as AppIndicator3 +except (ValueError, ImportError): + AppIndicator3 = None + +# 是否 widnows +def is_windows(): + return platform.system() in ('Windows', 'Microsoft') + +# 文件要绝对路径 +def get_pixbuf(filename: str, size: int = 0) -> Pixbuf: + + if size: + pixbuf = Pixbuf.new_from_file_at_size(filename, size, size) + else: + pixbuf = Pixbuf.new_from_file(filename) + + return pixbuf + + +# 获取logo图标 +def get_logo(size): + filename = 'youtube.svg' + # windows要用png格式 + if is_windows(): + filename = 'youtube.png' + + return get_pixbuf(filename, size) + + +def create_tray(): + if AppIndicator3 : + indicator = AppIndicator3.Indicator.new( + "youtube", + "youtube", + AppIndicator3.IndicatorCategory.APPLICATION_STATUS + ) + else: + # windows 和 macos 必须传二进制图标, linux可传图标名称(自会去主题中找) + indicator = Gtk.StatusIcon.new_from_pixbuf(get_logo(32)) + # linux + indicator = Gtk.StatusIcon.new_from_icon_name('youtube') + + return indicator \ No newline at end of file