Skip to content

Commit

Permalink
Merge branch 'window-icon'
Browse files Browse the repository at this point in the history
  • Loading branch information
r0x0r committed Oct 7, 2024
2 parents fc9c4bc + 378a0b7 commit e45de4f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
8 changes: 8 additions & 0 deletions examples/icon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Set window icon using `webview.start(icon=<file_path>). This is supported only on GTK and QT. For other
platforms, icon is set during freezing."""

import webview

if __name__ == '__main__':
window = webview.create_window('Set window icon', 'https://pywebview.flowrl.com/hello')
webview.start(icon='../logo/logo.png')
9 changes: 7 additions & 2 deletions webview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from webview.localization import original_localization
from webview.menu import Menu
from webview.screen import Screen
from webview.util import (_TOKEN, base_uri, escape_line_breaks, escape_string,
from webview.util import (_TOKEN, abspath, base_uri, escape_line_breaks, escape_string,
is_app, is_local_url, parse_file_type)
from webview.window import Window

Expand Down Expand Up @@ -108,6 +108,7 @@ def start(
server: type[http.ServerType] = http.BottleServer,
server_args: dict[Any, Any] = {},
ssl: bool = False,
icon: str | None = None,
):
"""
Start a GUI loop and display previously created windows. This function must
Expand All @@ -125,14 +126,15 @@ def start(
will be served using a local HTTP server on a random port. For each
window, a separate HTTP server is spawned. This option is ignored for
non-local URLs.
:param user_agent: Change user agent string. Not supported in EdgeHTML.
:param user_agent: Change user agent string.
:param private_mode: Enable private mode. In private mode, cookies and local storage are not preserved.
Default is True.
:param storage_path: Custom location for cookies and other website data
:param menu: List of menus to be included in the app menu
:param server: Server class. Defaults to BottleServer
:param server_args: Dictionary of arguments to pass through to the server instantiation
:param ssl: Enable SSL for local HTTP server. Default is False.
:param icon: Path to the icon file. Supported only on GTK/QT.
"""
global guilib

Expand All @@ -148,6 +150,9 @@ def _create_children(other_windows):
_settings['http_server'] = http_server
_settings['private_mode'] = private_mode

if icon:
_settings['icon'] = abspath(icon)

if storage_path:
__set_storage_path(storage_path)

Expand Down
5 changes: 4 additions & 1 deletion webview/platforms/gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from gi.repository import Gdk, Gio
from gi.repository import GLib as glib
from gi.repository import Gtk as gtk
from gi.repository import Soup
from gi.repository import WebKit2 as webkit

renderer = 'gtkwebkit2'
Expand Down Expand Up @@ -211,6 +210,10 @@ def __init__(self, window: Window) -> None:
if window.fullscreen:
self.toggle_fullscreen()

if _settings['icon']:
self.window.set_icon_from_file(_settings['icon'])
self.window.set_default_icon_from_file(_settings['icon'])

def close_window(self, *data):
should_cancel = self.pywebview_window.events.closing.set()

Expand Down
6 changes: 5 additions & 1 deletion webview/platforms/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from qtpy import PYQT6, PYSIDE6
from qtpy.QtCore import QJsonValue
from qtpy.QtGui import QColor, QScreen
from qtpy.QtGui import QColor, QIcon, QScreen
from qtpy.QtWidgets import QAction, QApplication, QFileDialog, QMainWindow, QMenuBar, QMessageBox

try:
Expand Down Expand Up @@ -443,6 +443,10 @@ def __init__(self, window):
self.activateWindow()
self.raise_()

if _settings['icon']:
icon = QIcon(_settings['icon'])
self.setWindowIcon(icon)

self.shown.set()

def on_set_title(self, title):
Expand Down

0 comments on commit e45de4f

Please sign in to comment.