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

Frameless windows (macOS implementation) #177

Merged
merged 15 commits into from
Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions webview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def load_html(content, base_uri=""):

def create_window(title, url=None, js_api=None, width=800, height=600,
resizable=True, fullscreen=False, min_size=(200, 100), strings={}, confirm_quit=False,
background_color='#FFFFFF', debug=False):
background_color='#FFFFFF', debug=False, frameless=False):
"""
Create a web view window using a native GUI. The execution blocks after this function is invoked, so other
program logic must be executed in a separate thread.
Expand All @@ -189,6 +189,7 @@ def create_window(title, url=None, js_api=None, width=800, height=600,
:param strings: a dictionary with localized strings
:param confirm_quit: Display a quit confirmation dialog. Default is False
:param background_color: Background color as a hex string that is displayed before the content of webview is loaded. Default is white.
:param frameless: Whether the window should havea frame.
Copy link
Owner

Choose a reason for hiding this comment

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

havea typo

:return:
"""
valid_color = r'^#(?:[0-9a-fA-F]{3}){1,2}$'
Expand All @@ -199,7 +200,7 @@ def create_window(title, url=None, js_api=None, width=800, height=600,
localization.update(strings)
gui.create_window(_make_unicode(title), _transform_url(url),
width, height, resizable, fullscreen, min_size, confirm_quit,
background_color, debug, js_api, _webview_ready)
background_color, debug, js_api, _webview_ready, frameless)

def set_title(title):
"""
Expand Down
73 changes: 65 additions & 8 deletions webview/cocoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,42 @@
info['NSAppTransportSecurity'] = {'NSAllowsArbitraryLoads': Foundation.YES}


class DragBar(AppKit.NSView):
def mouseDragged_(self, theEvent):
screenFrame = AppKit.NSScreen.mainScreen().frame()
if screenFrame is None:
sys.stderr.write('failed to obtain screen\n')
raise RuntimeError
Copy link
Owner

Choose a reason for hiding this comment

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

It would be better to raise a RuntimeError('Failed to obtain screen'). Same with the rest of RuntimeErrors


window = self.window()
windowFrame = window.frame()
if windowFrame is None:
sys.stderr.write('failed to obtain frame\n')
raise RuntimeError

currentLocation = window.convertBaseToScreen_(window.mouseLocationOutsideOfEventStream())
newOrigin = AppKit.NSMakePoint((currentLocation.x - self.initialLocation.x),
(currentLocation.y - self.initialLocation.y))
if (newOrigin.y + windowFrame.size.height) > \
(screenFrame.origin.y + screenFrame.size.height):
newOrigin.y = screenFrame.origin.y + \
(screenFrame.size.height + windowFrame.size.height)
window.setFrameOrigin_(newOrigin)

def mouseDown_(self, theEvent):
window = self.window()

windowFrame = window.frame()
if windowFrame is None:
sys.stderr.write('failed to obtain frame\n')
raise RuntimeError

self.initialLocation = \
window.convertBaseToScreen_(theEvent.locationInWindow())
self.initialLocation.x -= windowFrame.origin.x
self.initialLocation.y -= windowFrame.origin.y


class BrowserView:
instance = None
app = AppKit.NSApplication.sharedApplication()
Expand Down Expand Up @@ -146,7 +182,7 @@ def printView(frameview):
def webView_decidePolicyForNavigationAction_request_frame_decisionListener_(self, webview, action, request, frame, listener):
# The event that might have triggered the navigation
event = AppKit.NSApp.currentEvent()
action_type = action['WebActionNavigationTypeKey']
action_type = action['WebActionNavigationTypeKey']

""" Disable back navigation on pressing the Delete key: """
# Check if the requested navigation action is Back/Forward
Expand All @@ -166,7 +202,18 @@ def webView_didFinishLoadForFrame_(self, webview, frame):
if not webview.window():
BrowserView.instance.window.setContentView_(webview)
BrowserView.instance.window.makeFirstResponder_(webview)


frame_size = BrowserView.instance.window.frame().size
drag_bar_height = 24

# Flip the webview so our bar is position from the top, not bottom
webview.setFlipped_(True)

rect = AppKit.NSMakeRect(0, 0, frame_size.width, drag_bar_height)
drag_bar = DragBar.alloc().initWithFrame_(rect)
drag_bar.setAutoresizingMask_(AppKit.NSViewWidthSizable)
BrowserView.instance.window.contentView().addSubview_(drag_bar)

Copy link
Collaborator

@shivaprsd shivaprsd Feb 23, 2018

Choose a reason for hiding this comment

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

Is there a reason this code (line 204 to 215) is put here? Do we need to update the drag
bar every time the page loads? If not, the best place for it would be in the __init__()
function itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I managed to move it to init in 841ca88 (new to pyobjc!).

BrowserView.load_event.set()
if BrowserView.instance.js_bridge:
BrowserView.instance._set_js_api()
Expand Down Expand Up @@ -224,7 +271,7 @@ def performKeyEquivalent_(self, theEvent):

return handled

def __init__(self, title, url, width, height, resizable, fullscreen, min_size, background_color, debug, js_api, webview_ready):
def __init__(self, title, url, width, height, resizable, fullscreen, min_size, background_color, debug, js_api, webview_ready, frameless):
Copy link
Owner

Choose a reason for hiding this comment

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

Please place frameless before webview_ready

BrowserView.instance = self
BrowserView.debug = debug

Expand All @@ -244,15 +291,25 @@ def __init__(self, title, url, width, height, resizable, fullscreen, min_size, b
if resizable:
window_mask = window_mask | AppKit.NSResizableWindowMask

if frameless is True:
Copy link
Owner

Choose a reason for hiding this comment

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

if frameless

window_mask = window_mask | AppKit.NSFullSizeContentViewWindowMask | AppKit.NSTexturedBackgroundWindowMask
Copy link
Collaborator

@shivaprsd shivaprsd Feb 23, 2018

Choose a reason for hiding this comment

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

NSFullSizeContentViewWindowMask is now deprecated. Doesn't work in High Sierra. If
we are to use this, we would need an OS API version check. Also a way to do it in 10.13+

Edit: NSWindowStyleMaskFullSizeContentView works in High Sierra.


self.window = AppKit.NSWindow.alloc().\
initWithContentRect_styleMask_backing_defer_(rect, window_mask, AppKit.NSBackingStoreBuffered, False)
self.window.setTitle_(title)
self.window.setBackgroundColor_(BrowserView.nscolor_from_hex(background_color))
self.window.setMinSize_(AppKit.NSSize(min_size[0], min_size[1]))
# Set the titlebar color (so that it does not change with the window color)
self.window.contentView().superview().subviews().lastObject().setBackgroundColor_(AppKit.NSColor.windowBackgroundColor())

self.webkit = BrowserView.WebKitHost.alloc().initWithFrame_(rect)
if frameless is True:
Copy link
Owner

Choose a reason for hiding this comment

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

if frameless

self.window.setTitlebarAppearsTransparent_(True)
self.window.setTitleVisibility_(AppKit.NSWindowTitleHidden)
webkit_rect = AppKit.NSMakeRect(0, -12, width, height - 12)
else:
# Set the titlebar color (so that it does not change with the window color)
self.window.contentView().superview().subviews().lastObject().setBackgroundColor_(AppKit.NSColor.windowBackgroundColor())
webkit_rect = rect

self.webkit = BrowserView.WebKitHost.alloc().initWithFrame_(webkit_rect)

self._browserDelegate = BrowserView.BrowserDelegate.alloc().init()
self._windowDelegate = BrowserView.WindowDelegate.alloc().init()
Expand Down Expand Up @@ -535,11 +592,11 @@ def _set_debugging():


def create_window(title, url, width, height, resizable, fullscreen, min_size,
confirm_quit, background_color, debug, js_api, webview_ready):
confirm_quit, background_color, debug, js_api, webview_ready, frameless):
global _confirm_quit
_confirm_quit = confirm_quit

browser = BrowserView(title, url, width, height, resizable, fullscreen, min_size, background_color, debug, js_api, webview_ready)
browser = BrowserView(title, url, width, height, resizable, fullscreen, min_size, background_color, debug, js_api, webview_ready, frameless)
browser.show()


Expand Down