Skip to content

Commit

Permalink
Fixed some issues pointed out in the PR by rabbit
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpare committed Sep 21, 2024
1 parent 76558b9 commit 2ede8e2
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 68 deletions.
6 changes: 3 additions & 3 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ func (a *App) handleDragAndDropMessage(event *dragAndDropMessage) {
window, ok := a.windows[event.windowId]
a.windowsLock.Unlock()
if !ok {
a.error("DragAndDropMessage: WebviewWindow #%d not found", event.windowId)
a.error("DragAndDropMessage: Window #%d not found", event.windowId)
return
}
// Get callback from window
Expand All @@ -714,7 +714,7 @@ func (a *App) handleWindowMessage(event *windowMessage) {
window, ok := a.windows[event.windowId]
a.windowsLock.RUnlock()
if !ok {
a.error("WindowMessage: WebviewWindow #%d not found", event.windowId)
a.error("WindowMessage: Window #%d not found", event.windowId)
return
}
// Check if the message starts with "wails:"
Expand Down Expand Up @@ -1000,7 +1000,7 @@ func (a *App) handleWindowKeyEvent(event *windowKeyEvent) {
window, ok := a.windows[event.windowId]
a.windowsLock.RUnlock()
if !ok {
a.error("WindowKeyEvent: WebviewWindow #%d not found", event.windowId)
a.error("WindowKeyEvent: Window #%d not found", event.windowId)
return
}
// Get callback from window
Expand Down
5 changes: 4 additions & 1 deletion v3/pkg/application/application_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ type Options struct {
// DisableDefaultSignalHandler disables the default signal handler
DisableDefaultSignalHandler bool

// KeyBindings is a map of key bindings to functions
// KeyBindings is a map of key bindings to functions. The function provides the [Window]
// interface, so if you need more granular control over the window (e.g. [*WebviewWindow]), it's possible
// to declare the key binding directly in the specific window options or switch over
// the types that implement the [Window] interface
KeyBindings map[string]func(window Window)

// OnShutdown is called when the application is about to terminate.
Expand Down
8 changes: 5 additions & 3 deletions v3/pkg/application/webview_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ func NewPanel(options WebviewPanelOptions) *WebviewPanel {

result.setupEventMapping()

// Listen for window closing events and de
// Listen for window closing events and delete it
result.OnWindowEvent(events.Common.WindowClosing, func(event *WindowEvent) {
shouldClose := true
if result.options.ShouldClose != nil {
shouldClose = result.options.ShouldClose(result)
}
if shouldClose {
globalApplication.deleteWindowByID(result.id)
InvokeSync(result.impl.close)
if result.impl != nil {
InvokeSync(result.impl.close)
}
}
})

Expand Down Expand Up @@ -86,7 +88,7 @@ func (p *WebviewPanel) SetFloating(b bool) Window {
}

func (p *WebviewPanel) HandleKeyEvent(acceleratorString string) {
if p.impl == nil && !p.isDestroyed() {
if p.impl == nil || p.isDestroyed() {
return
}
InvokeSync(func() {
Expand Down
4 changes: 4 additions & 0 deletions v3/pkg/application/webview_panel_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ package application
#include "webview_window_bindings_darwin.h"
void *panelNew(unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop, struct WebviewPreferences preferences) {
return createWindow(WindowTypePanel, id, width, height, fraudulentWebsiteWarningEnabled, frameless, enableDragAndDrop, preferences);
}
// Set NSPanel floating
void panelSetFloating(void* nsPanel, bool floating) {
// Set panel floating on main thread
Expand Down
5 changes: 4 additions & 1 deletion v3/pkg/application/webview_panel_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ type WebviewPanelOptions struct {
// Return true to allow the panel to close, or false to prevent it from closing.
ShouldClose func(panel *WebviewPanel) bool

// KeyBindings is a map of key bindings to functions
// KeyBindings is a map of key bindings to functions. Other key bindings provided from
// the embedded field [WebviewWindowOptions] are still valid and the two maps are merged
// together, with the [WebviewPanelOptions] values overriding the others if they have the
// same key.
KeyBindings map[string]func(panel *WebviewPanel)
}

Expand Down
2 changes: 1 addition & 1 deletion v3/pkg/application/webview_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ func (w *WebviewWindow) processKeyBinding(acceleratorString string) bool {
}

func (w *WebviewWindow) HandleKeyEvent(acceleratorString string) {
if w.impl == nil && !w.isDestroyed() {
if w.impl == nil || w.isDestroyed() {
return
}
InvokeSync(func() {
Expand Down
15 changes: 8 additions & 7 deletions v3/pkg/application/webview_window_bindings_darwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ struct WebviewPreferences {
bool *FullscreenEnabled;
};

typedef enum {
WindowTypeWindow,
WindowTypePanel
} WindowType;

extern void registerListener(unsigned int event);

void *windowNew(unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop, struct WebviewPreferences preferences);
void *panelNew(unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop, struct WebviewPreferences preferences);
void *windowOrPanelNew(bool isWindow, unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop, struct WebviewPreferences preferences);
void *createWindow(WindowType windowType, unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop, struct WebviewPreferences preferences);
void printWindowStyle(void *window);
void setInvisibleTitleBarHeight(void *window, unsigned int height);
void windowSetTransparent(void *nsWindow);
Expand Down Expand Up @@ -51,7 +54,7 @@ void webviewSetTransparent(void *nsWindow);
void webviewSetBackgroundColour(void *nsWindow, int r, int g, int b, int alpha);
void windowSetBackgroundColour(void *nsWindow, int r, int g, int b, int alpha);
bool windowIsMaximised(void *nsWindow);
bool windowIsFullscreen(void *nsWindow);
bool windowIsFullScreen (void *nsWindow);
bool windowIsMinimised(void *nsWindow);
bool windowIsFocused(void *nsWindow);
void windowFullscreen(void *nsWindow);
Expand Down Expand Up @@ -82,8 +85,7 @@ void windowRenderHTML(void *window, const char *html);
void windowInjectCSS(void *window, const char *css);
void windowMinimise(void *window);
void windowMaximise(void *window);
bool isFullScreen(void *window);
bool isVisible(void *window);
bool windowIsVisible(void *window);
void windowSetFullScreen(void *window, bool fullscreen);
void windowUnminimise(void *window);
void windowUnmaximise(void *window);
Expand All @@ -98,7 +100,6 @@ void windowShowMenu(void *window, void *menu, int x, int y);
void windowSetFrameless(void *window, bool frameless);
void startDrag(void *window);
void windowPrint(void *window);
void setWindowEnabled(void *window, bool enabled);
void windowSetEnabled(void *window, bool enabled);
void windowFocus(void *window);
bool isIgnoreMouseEvents(void *nsWindow);
Expand Down
60 changes: 23 additions & 37 deletions v3/pkg/application/webview_window_bindings_darwin.m
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
#include "webview_window_bindings_darwin.h"

// Create a new Window
void* windowNew(unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop, struct WebviewPreferences preferences) {
return windowOrPanelNew(true, id, width, height, fraudulentWebsiteWarningEnabled, frameless, enableDragAndDrop, preferences);
}

// Create a new Panel
void* panelNew(unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop, struct WebviewPreferences preferences) {
return windowOrPanelNew(false, id, width, height, fraudulentWebsiteWarningEnabled, frameless, enableDragAndDrop, preferences);
}

void* windowOrPanelNew(bool isWindow, unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop, struct WebviewPreferences preferences) {
void *createWindow(WindowType windowType, unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop, struct WebviewPreferences preferences) {
NSWindowStyleMask styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
if (frameless) {
styleMask = NSWindowStyleMaskBorderless | NSWindowStyleMaskResizable;
}

WebviewWindow* webviewWindow;
if (isWindow) {
webviewWindow = [[WebviewWindow alloc] initAsWindow:NSMakeRect(0, 0, width-1, height-1)
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:NO];
} else {
webviewWindow = [[WebviewWindow alloc] initAsPanel:NSMakeRect(0, 0, width-1, height-1)
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:NO];
switch (windowType) {
case WindowTypeWindow:
webviewWindow = [[WebviewWindow alloc] initAsWindow:NSMakeRect(0, 0, width-1, height-1)
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:NO];
break;
case WindowTypePanel:
webviewWindow = [[WebviewWindow alloc] initAsPanel:NSMakeRect(0, 0, width-1, height-1)
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:NO];
break;
default:
NSLog(@"Invalid WindowType");
return nil;
}

NSWindow *window = webviewWindow.w;
Expand Down Expand Up @@ -365,7 +361,7 @@ bool windowIsMaximised(void* nsWindow) {
return [((WebviewWindow*)nsWindow).w isZoomed];
}

bool windowIsFullscreen(void* nsWindow) {
bool windowIsFullScreen (void* nsWindow) {
return [((WebviewWindow*)nsWindow).w styleMask] & NSWindowStyleMaskFullScreen;
}

Expand All @@ -379,15 +375,15 @@ bool windowIsFocused(void* nsWindow) {

// Set Window fullscreen
void windowFullscreen(void* nsWindow) {
if( windowIsFullscreen(nsWindow) ) {
if( windowIsFullScreen (nsWindow) ) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[((WebviewWindow*)nsWindow).w toggleFullScreen:nil];
});}

void windowUnFullscreen(void* nsWindow) {
if( !windowIsFullscreen(nsWindow) ) {
if( !windowIsFullScreen (nsWindow) ) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -608,20 +604,14 @@ void windowMaximise(void *window) {
[((WebviewWindow*)window).w zoom:nil];
}

bool isFullScreen(void *window) {
NSWindow* nsWindow = ((WebviewWindow *)window).w;
long mask = [nsWindow styleMask];
return (mask & NSWindowStyleMaskFullScreen) == NSWindowStyleMaskFullScreen;
}

bool isVisible(void *window) {
bool windowIsVisible(void *window) {
NSWindow* nsWindow = ((WebviewWindow *)window).w;
return (nsWindow.occlusionState & NSWindowOcclusionStateVisible) == NSWindowOcclusionStateVisible;
}

// windowSetFullScreen
void windowSetFullScreen(void *window, bool fullscreen) {
if (isFullScreen(window)) {
if (windowIsFullScreen (window)) {
return;
}
NSWindow* nsWindow = ((WebviewWindow *)window).w;
Expand Down Expand Up @@ -755,15 +745,11 @@ void windowPrint(void *window) {
#endif
}

void setWindowEnabled(void *window, bool enabled) {
void windowSetEnabled(void *window, bool enabled) {
NSWindow* nsWindow = ((WebviewWindow*)window).w;
[nsWindow setIgnoresMouseEvents:!enabled];
}

void windowSetEnabled(void *window, bool enabled) {
// TODO: Implement
}

void windowFocus(void *window) {
NSWindow* nsWindow = ((WebviewWindow *)window).w;
// If the current application is not active, activate it
Expand Down
8 changes: 6 additions & 2 deletions v3/pkg/application/webview_window_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ package application
#cgo LDFLAGS: -framework Cocoa -framework WebKit
#include "webview_window_bindings_darwin.h"
void *windowNew(unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop, struct WebviewPreferences preferences) {
return createWindow(WindowTypeWindow, id, width, height, fraudulentWebsiteWarningEnabled, frameless, enableDragAndDrop, preferences);
}
*/
import "C"
import (
Expand Down Expand Up @@ -194,7 +198,7 @@ func (w *macosWebviewWindow) isMaximised() bool {

func (w *macosWebviewWindow) isFullscreen() bool {
return w.syncMainThreadReturningBool(func() bool {
return bool(C.windowIsFullscreen(w.nsWindow))
return bool(C.windowIsFullScreen(w.nsWindow))
})
}

Expand All @@ -203,7 +207,7 @@ func (w *macosWebviewWindow) isNormal() bool {
}

func (w *macosWebviewWindow) isVisible() bool {
return bool(C.isVisible(w.nsWindow))
return bool(C.windowIsVisible(w.nsWindow))
}

func (w *macosWebviewWindow) syncMainThreadReturningBool(fn func() bool) bool {
Expand Down
21 changes: 8 additions & 13 deletions v3/pkg/application/webview_window_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,29 @@
extern bool hasListeners(unsigned int);

@implementation WebviewWindow
- (WebviewWindow *) initAsWindow:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation;
{
- (WebviewWindow *) initAsWindow:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation {
self = [super init];

self.w = [[NSWindow alloc] initWithContentRect:contentRect styleMask:windowStyle backing:bufferingType defer:deferCreation];
[self.w setAlphaValue:1.0];
[self.w setBackgroundColor:[NSColor clearColor]];
[self.w setOpaque:NO];
[self.w setMovableByWindowBackground:YES];

self.responder = [[WebviewResponder alloc] initAttachToWindow:self.w];
[self commonInitialization];

return self;
}
- (WebviewWindow *) initAsPanel:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation;
{
- (WebviewWindow *) initAsPanel:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation {
self = [super init];

self.w = (NSWindow *) [[NSPanel alloc] initWithContentRect:contentRect styleMask:windowStyle backing:bufferingType defer:deferCreation];
[self commonInitialization];

return self;
}
- (void) commonInitialization {
[self.w setAlphaValue:1.0];
[self.w setBackgroundColor:[NSColor clearColor]];
[self.w setOpaque:NO];
[self.w setMovableByWindowBackground:YES];

self.responder = [[WebviewResponder alloc] initAttachToWindow:self.w];

return self;
}
- (void) dealloc {
// Remove the script handler, otherwise WebviewWindowDelegate won't get deallocated
Expand Down

0 comments on commit 2ede8e2

Please sign in to comment.