diff --git a/app/ui/transmitter.glade b/app/ui/transmitter.glade index 867efbbf..ed704285 100644 --- a/app/ui/transmitter.glade +++ b/app/ui/transmitter.glade @@ -92,12 +92,14 @@ Author: Dmitriy Yefremov True True - Drag the link here + Drag or paste the link here 2 2 2 2 gtk-paste + + @@ -168,25 +170,29 @@ Author: Dmitriy Yefremov - - demon-editor - True - - - + + True + False + view-restore True False - - gtk-quit + + Show/Hide True False - True - True - + show_image + False + + + demon-editor + True + + + diff --git a/app/ui/transmitter.py b/app/ui/transmitter.py index 0e8e5ea5..9c7a141e 100644 --- a/app/ui/transmitter.py +++ b/app/ui/transmitter.py @@ -1,10 +1,14 @@ +from pathlib import Path from urllib.parse import urlparse +import gi from gi.repository import GLib +from app.commons import log from app.connections import HttpRequestType from app.tools.yt import YouTube -from .uicommons import Gtk, Gdk, UI_RESOURCES_PATH, get_yt_icon +from app.ui.iptv import get_yt_icon +from .uicommons import Gtk, Gdk, UI_RESOURCES_PATH class LinksTransmitter: @@ -17,26 +21,49 @@ class LinksTransmitter: def __init__(self, http_api, app_window): handlers = {"on_popup_menu": self.on_popup_menu, "on_status_icon_activate": self.on_status_icon_activate, - "on_query_tooltip": self.on_query_tooltip, + "on_url_changed": self.on_url_changed, + "on_url_activate": self.on_url_activate, "on_drag_data_received": self.on_drag_data_received, "on_previous": self.on_previous, "on_next": self.on_next, "on_stop": self.on_stop, "on_clear": self.on_clear, - "on_play": self.on_play, - "on_exit": self.on_exit} + "on_play": self.on_play} self._http_api = http_api self._app_window = app_window + self._is_status_icon = True builder = Gtk.Builder() builder.add_from_file(UI_RESOURCES_PATH + "transmitter.glade") builder.connect_signals(handlers) - self._tray = builder.get_object("status_icon") self._main_window = builder.get_object("main_window") self._url_entry = builder.get_object("url_entry") self._tool_bar = builder.get_object("tool_bar") + self._popup_menu = builder.get_object("staus_popup_menu") + self._restore_menu_item = builder.get_object("restore_menu_item") + self._status_active = None + self._status_passive = None + + try: + gi.require_version("AppIndicator3", "0.1") + from gi.repository import AppIndicator3 + except (ImportError, ValueError) as e: + log("{}: Load library error: {}".format(__class__.__name__, e)) + self._tray = builder.get_object("status_icon") + else: + self._is_status_icon = False + self._status_active = AppIndicator3.IndicatorStatus.ACTIVE + self._status_passive = AppIndicator3.IndicatorStatus.PASSIVE + + category = AppIndicator3.IndicatorCategory.APPLICATION_STATUS + path = Path(UI_RESOURCES_PATH + "/icons/hicolor/scalable/apps/demon-editor.svg").resolve() + path = str(path) if path.is_file() else "demon-editor" + self._tray = AppIndicator3.Indicator.new("DemonEditor", path, category) + self._tray.set_status(self._status_active) + self._tray.set_secondary_activate_target(builder.get_object("show_menu_item")) + self._tray.set_menu(self._popup_menu) style_provider = Gtk.CssProvider() style_provider.load_from_path(UI_RESOURCES_PATH + "style.css") @@ -44,7 +71,10 @@ class LinksTransmitter: Gtk.STYLE_PROVIDER_PRIORITY_USER) def show(self, show): - self._tray.set_visible(show) + if self._is_status_icon: + self._tray.set_visible(show) + elif self._status_active: + self._tray.set_status(self._status_active if show else self._status_passive) if not show: self.hide() @@ -59,12 +89,12 @@ class LinksTransmitter: window.hide() if visible else window.show() self._app_window.present() if visible else self._app_window.iconify() - def on_query_tooltip(self, icon, g, x, y, tooltip: Gtk.Tooltip): - if self._main_window.get_visible() or not self._url_entry.get_text(): - return False + def on_url_changed(self, entry): + entry.set_name("GtkEntry" if self.is_url(entry.get_text()) else "digit-entry") - tooltip.set_text(self._url_entry.get_text()) - return True + def on_url_activate(self, entry): + gen = self.activate_url(entry.get_text()) + GLib.idle_add(lambda: next(gen, False), priority=GLib.PRIORITY_LOW) def on_drag_data_received(self, entry, drag_context, x, y, data, info, time): url = data.get_text() @@ -74,9 +104,9 @@ class LinksTransmitter: def activate_url(self, url): self._url_entry.set_name("GtkEntry") - result = urlparse(url) + self._url_entry.set_icon_from_stock(Gtk.EntryIconPosition.SECONDARY, None) - if result.scheme and result.netloc: + if self.is_url(url): self._tool_bar.set_sensitive(False) yt_id = YouTube.get_yt_id(url) yield True @@ -121,6 +151,10 @@ class LinksTransmitter: def clear_playlist(self, res): GLib.idle_add(self._tool_bar.set_sensitive, not res) + if "error_code" in res: + log("Error clearing playlist. There may be no http connection.") + self.on_done(res) + return for ref in res: GLib.idle_add(self._tool_bar.set_sensitive, False) @@ -129,8 +163,11 @@ class LinksTransmitter: self.on_done, self.__STREAM_PREFIX) - def on_exit(self, item=None): - self.show(False) + @staticmethod + def is_url(text): + """ Simple url checking. """ + result = urlparse(text) + return result.scheme and result.netloc if __name__ == "__main__":