Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to set the icon of the window #786

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions webview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _create_children(other_windows):

def create_window(title, url=None, html=None, js_api=None, width=800, height=600, x=None, y=None,
resizable=True, fullscreen=False, min_size=(200, 100), hidden=False,
frameless=False, easy_drag=True,
icon=None, frameless=False, easy_drag=True,
minimized=False, on_top=False, confirm_close=False, background_color='#FFFFFF',
transparent=False, text_select=False, localization=None):
"""
Expand All @@ -156,6 +156,7 @@ def create_window(title, url=None, html=None, js_api=None, width=800, height=600
:param fullscreen: True if start in fullscreen mode. Default is False
:param min_size: a (width, height) tuple that specifies a minimum window size. Default is 200x100
:param hidden: Whether the window should be hidden.
:param icon: Window icon name.
:param frameless: Whether the window should have a frame.
:param easy_drag: Easy window drag mode when window is frameless.
:param minimized: Display window minimized
Expand All @@ -176,7 +177,7 @@ def create_window(title, url=None, html=None, js_api=None, width=800, height=600
window = Window(uid, make_unicode(title), url, html,
width, height, x, y, resizable, fullscreen, min_size, hidden,
frameless, easy_drag, minimized, on_top, confirm_close, background_color,
js_api, text_select, transparent, localization)
js_api, text_select, transparent, localization, icon)

windows.append(window)

Expand All @@ -191,4 +192,4 @@ def create_window(title, url=None, html=None, js_api=None, width=800, height=600
def screens():
guilib = initialize()
screens = guilib.get_screens()
return screens
return screens
17 changes: 16 additions & 1 deletion webview/platforms/cef.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import shutil
import sys
import webbrowser
import platform

from ctypes import windll
from ctypes import windll, wintypes
from functools import wraps
from uuid import uuid1
from threading import Event
Expand Down Expand Up @@ -252,6 +253,20 @@ def _create():

instances[window.uid] = browser
window.shown.set()

if window.icon:
icon = window.icon+".ico"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work with png files? Ie. is it necessary to append the .ico suffix?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows require .ico, ways to convert a .png to .ico are a little too complex for me. So I think easier to provide both .ico and .png and so, use a sufixless name of icon

if os.path.isfile(icon):
smallx = windll.user32.GetSystemMetrics(49) #SM_CXSMICON
smally = windll.user32.GetSystemMetrics(50) #SM_CYSMICON
small_icon = windll.user32.LoadImageW(0, icon, 1, smallx, smally, 0x00000010)
windll.user32.SendMessageW(handle, 0x0080, 0, small_icon)

bigx = windll.user32.GetSystemMetrics(11) #SM_CXICON
bigy = windll.user32.GetSystemMetrics(12) #SM_CYICON
big_icon = windll.user32.LoadImageW(0, icon, 1, bigx, bigy, 0x00000010)
windll.user32.SendMessageW(handle, 0x0080, 1, big_icon)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code can be moved into winforms.py. CEF is just a component inside the winform.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sure. I will try to do it this week-end



window_info = cef.WindowInfo()
window_info.SetAsChild(handle)
Expand Down
22 changes: 20 additions & 2 deletions webview/platforms/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
logger.debug('Using Qt %s' % QT_VERSION_STR)

from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QMessageBox, QAction
from PyQt5.QtGui import QColor, QScreen
from PyQt5.QtGui import QColor, QScreen, QIcon

try:
from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView, QWebEnginePage as QWebPage
Expand All @@ -58,6 +58,7 @@ class BrowserView(QMainWindow):

create_window_trigger = QtCore.pyqtSignal(object)
set_title_trigger = QtCore.pyqtSignal(str)
set_icon_trigger = QtCore.pyqtSignal(str)
load_url_trigger = QtCore.pyqtSignal(str)
html_trigger = QtCore.pyqtSignal(str, str)
dialog_trigger = QtCore.pyqtSignal(int, str, bool, str, str)
Expand Down Expand Up @@ -129,7 +130,7 @@ def show_inspector(self):
url = 'http://localhost:{}'.format(BrowserView.inspector_port)
print(url)
window = Window('web_inspector', title, url, '', 700, 500, None, None, True, False,
(300, 200), False, False, False, False, False, False, '#fff', None, False, False, None)
(300, 200), False, False, False, False, False, False, '#fff', None, False, False, '')
window.localization = self.parent().localization

inspector = BrowserView(window)
Expand Down Expand Up @@ -232,6 +233,8 @@ def __init__(self, window):
self.title = window.title
self.setWindowTitle(window.title)

self.setWindowIcon(QIcon(window.icon))

# Set window background color
self.background_color = QColor()
self.background_color.setNamedColor(window.background_color)
Expand Down Expand Up @@ -299,6 +302,7 @@ def __init__(self, window):
self.current_url_trigger.connect(self.on_current_url)
self.evaluate_js_trigger.connect(self.on_evaluate_js)
self.set_title_trigger.connect(self.on_set_title)
self.set_icon_trigger.connect(self.on_set_icon)
self.on_top_trigger.connect(self.on_set_on_top)

if is_webengine and platform.system() != 'OpenBSD':
Expand Down Expand Up @@ -332,6 +336,10 @@ def __init__(self, window):
def on_set_title(self, title):
self.setWindowTitle(title)


def on_set_icon(self, icon):
self.setWindowIcon(QIcon(icon))

def on_file_dialog(self, dialog_type, directory, allow_multiple, save_filename, file_filter):
if dialog_type == FOLDER_DIALOG:
self._file_name = QFileDialog.getExistingDirectory(self, self.localization['linux.openFolder'], options=QFileDialog.ShowDirsOnly)
Expand Down Expand Up @@ -462,6 +470,9 @@ def on_load_finished(self):
def set_title(self, title):
self.set_title_trigger.emit(title)

def set_icon(self, icon):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set_icon support should be added to winforms as well.

self.set_icon_trigger.emit(icon)

def get_current_url(self):
self.loaded.wait()
self.current_url_trigger.emit()
Expand Down Expand Up @@ -631,6 +642,10 @@ def _create():
def set_title(title, uid):
BrowserView.instances[uid].set_title(title)

def set_icon(icon, uid):
BrowserView.instances[uid].set_icon(icon)



def get_current_url(uid):
return BrowserView.instances[uid].get_current_url()
Expand Down Expand Up @@ -701,6 +716,9 @@ def get_position(uid):
def get_size(uid):
window = BrowserView.instances[uid]
return window.width(), window.height()

def get_instance(uid):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems debug

return BrowserView.instances[uid]


def get_screens():
Expand Down
15 changes: 14 additions & 1 deletion webview/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ def _loaded_call(function):
class Window:
def __init__(self, uid, title, url, html, width, height, x, y, resizable, fullscreen,
min_size, hidden, frameless, easy_drag, minimized, on_top, confirm_close,
background_color, js_api, text_select, transparent, localization):
background_color, js_api, text_select, transparent, localization, icon):
self.uid = uid
self.title = make_unicode(title)
self.icon = icon
self.original_url = None if html else url # original URL provided by user
self.real_url = None # transformed URL for internal HTTP server
self.html = html
Expand Down Expand Up @@ -160,6 +161,10 @@ def get_elements(self, selector):

return self.evaluate_js(code)

@_shown_call
def get_instance(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems debug code i have used for test, must be removed

return self.gui.get_instance(self.uid)

@_shown_call
def load_url(self, url):
"""
Expand Down Expand Up @@ -198,6 +203,14 @@ def set_title(self, title):
"""
self.gui.set_title(title, self.uid)

@_shown_call
def set_icon(self, icon):
"""
Set a new title of the window
"""
self.gui.set_icon(icon, self.uid)


@_loaded_call
def get_current_url(self):
"""
Expand Down