Skip to content

Commit

Permalink
Reduced code duplication and magic numbers, improved code style consi…
Browse files Browse the repository at this point in the history
…stency
  • Loading branch information
nixpare committed Sep 21, 2024
1 parent 95710b7 commit c142752
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 400 deletions.
37 changes: 15 additions & 22 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,38 +527,31 @@ func (a *App) error(message string, args ...any) {

func (a *App) NewWebviewWindowWithOptions(windowOptions WebviewWindowOptions) *WebviewWindow {
newWindow := NewWindow(windowOptions)
id := newWindow.ID()

a.windowsLock.Lock()
a.windows[id] = newWindow
a.windowsLock.Unlock()

// Call hooks
for _, hook := range a.windowCreatedCallbacks {
hook(newWindow)
}

a.runOrDeferToAppRun(newWindow)
a.addNewWindow(newWindow)

return newWindow
}

func (a *App) NewWebviewPanelWithOptions(panelOptions WebviewPanelOptions) *WebviewPanel {
newPanel := NewPanel(panelOptions)
id := newPanel.ID()
a.addNewWindow(newPanel)

a.windowsLock.Lock()
a.windows[id] = newPanel
a.windowsLock.Unlock()
return newPanel
}

// Call hooks
for _, hook := range a.windowCreatedCallbacks {
hook(newPanel)
}
func (a *App) addNewWindow(newWindow Window) {
id := newWindow.ID()

a.runOrDeferToAppRun(newPanel)
a.windowsLock.Lock()
a.windows[id] = newWindow
a.windowsLock.Unlock()

return newPanel
// Call hooks
for _, hook := range a.windowCreatedCallbacks {
hook(newWindow)
}

a.runOrDeferToAppRun(newWindow)
}

func (a *App) NewSystemTray() *SystemTray {
Expand Down
46 changes: 7 additions & 39 deletions v3/pkg/application/webview_panel.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package application

import "github.com/wailsapp/wails/v3/pkg/events"

type webviewPanelImpl interface {
webviewWindowImpl
getWebviewWindowImpl() webviewWindowImpl
setFloating(floating bool)
}

type WebviewPanel struct {
WebviewWindow
*WebviewWindow

options WebviewPanelOptions
impl webviewPanelImpl
Expand All @@ -19,45 +17,15 @@ type WebviewPanel struct {

// NewPanel creates a new panel with the given options
func NewPanel(options WebviewPanelOptions) *WebviewPanel {
if options.Width == 0 {
options.Width = 800
}
if options.Height == 0 {
options.Height = 600
}
if options.URL == "" {
options.URL = "/"
}
window := NewWindow(options.WebviewWindowOptions)
options.WebviewWindowOptions = window.options

result := &WebviewPanel{
WebviewWindow: WebviewWindow{
id: getWindowID(),
options: options.WebviewWindowOptions,
eventListeners: make(map[uint][]*WindowEventListener),
contextMenus: make(map[string]*Menu),
eventHooks: make(map[uint][]*WindowEventListener),
menuBindings: make(map[string]*MenuItem),
},
options: options,
WebviewWindow: window,
options: options,
}

result.setupEventMapping()

// 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)
if result.impl != nil {
InvokeSync(result.impl.close)
}
}
})

// Process keybindings
// Process keybindings specific to the WebviewPanel
if result.options.KeyBindings != nil || result.options.WebviewWindowOptions.KeyBindings != nil {
result.keyBindings = processKeyBindingOptionsForPanel(result.options.KeyBindings, result.options.WebviewWindowOptions.KeyBindings)
}
Expand Down Expand Up @@ -118,5 +86,5 @@ func (p *WebviewPanel) processKeyBinding(acceleratorString string) bool {
}
}

return globalApplication.processKeyBinding(acceleratorString, &p.WebviewWindow)
return globalApplication.processKeyBinding(acceleratorString, p.WebviewWindow)
}
14 changes: 3 additions & 11 deletions v3/pkg/application/webview_panel_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,25 @@ void panelSetFloating(void* nsPanel, bool floating) {
import "C"
import (
"unsafe"

"github.com/wailsapp/wails/v3/internal/runtime"
"github.com/wailsapp/wails/v3/pkg/events"
)

type macosWebviewPanel struct {
macosWebviewWindow
*macosWebviewWindow

nsPanel unsafe.Pointer
parent *WebviewPanel
}

func newPanelImpl(parent *WebviewPanel) *macosWebviewPanel {
result := &macosWebviewPanel{
macosWebviewWindow: macosWebviewWindow{
parent: &parent.WebviewWindow,
},
macosWebviewWindow: newWindowImpl(parent.WebviewWindow),
parent: parent,
}
result.parent.RegisterHook(events.Mac.WebViewDidFinishNavigation, func(event *WindowEvent) {
result.execJS(runtime.Core())
})
return result
}

func (p *macosWebviewPanel) getWebviewWindowImpl() webviewWindowImpl {
return &p.macosWebviewWindow
return p.macosWebviewWindow
}

func (p *macosWebviewPanel) run() {
Expand Down
2 changes: 1 addition & 1 deletion v3/pkg/application/webview_panel_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func processKeyBindingOptionsForPanel(keyBindings map[string]func(panel *Webview
continue
}
result[acc.String()] = func(panel *WebviewPanel) {
callback(&panel.WebviewWindow)
callback(panel.WebviewWindow)
}
globalApplication.debug("Added Keybinding", "accelerator", acc.String())
}
Expand Down
1 change: 1 addition & 0 deletions v3/pkg/application/webview_responder_darwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define WEBVIEW_RESPONDER_DARWIN

#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>

@interface WebviewResponder : NSResponder
@property(assign) NSWindow *w;
Expand Down
152 changes: 76 additions & 76 deletions v3/pkg/application/webview_responder_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,86 +44,86 @@ - (NSString *)keyStringFromEvent:(NSEvent *)event {
// Get the pressed key
switch ([event keyCode]) {
// Function keys
case 122: return @"f1";
case 120: return @"f2";
case 99: return @"f3";
case 118: return @"f4";
case 96: return @"f5";
case 97: return @"f6";
case 98: return @"f7";
case 100: return @"f8";
case 101: return @"f9";
case 109: return @"f10";
case 103: return @"f11";
case 111: return @"f12";
case 105: return @"f13";
case 107: return @"f14";
case 113: return @"f15";
case 106: return @"f16";
case 64: return @"f17";
case 79: return @"f18";
case 80: return @"f19";
case 90: return @"f20";
case kVK_F1: return @"f1";
case kVK_F2: return @"f2";
case kVK_F3: return @"f3";
case kVK_F4: return @"f4";
case kVK_F5: return @"f5";
case kVK_F6: return @"f6";
case kVK_F7: return @"f7";
case kVK_F8: return @"f8";
case kVK_F9: return @"f9";
case kVK_F10: return @"f10";
case kVK_F11: return @"f11";
case kVK_F12: return @"f12";
case kVK_F13: return @"f13";
case kVK_F14: return @"f14";
case kVK_F15: return @"f15";
case kVK_F16: return @"f16";
case kVK_F17: return @"f17";
case kVK_F18: return @"f18";
case kVK_F19: return @"f19";
case kVK_F20: return @"f20";
// Letter keys
case 0: return @"a";
case 11: return @"b";
case 8: return @"c";
case 2: return @"d";
case 14: return @"e";
case 3: return @"f";
case 5: return @"g";
case 4: return @"h";
case 34: return @"i";
case 38: return @"j";
case 40: return @"k";
case 37: return @"l";
case 46: return @"m";
case 45: return @"n";
case 31: return @"o";
case 35: return @"p";
case 12: return @"q";
case 15: return @"r";
case 1: return @"s";
case 17: return @"t";
case 32: return @"u";
case 9: return @"v";
case 13: return @"w";
case 7: return @"x";
case 16: return @"y";
case 6: return @"z";
case kVK_ANSI_A: return @"a";
case kVK_ANSI_B: return @"b";
case kVK_ANSI_C: return @"c";
case kVK_ANSI_D: return @"d";
case kVK_ANSI_E: return @"e";
case kVK_ANSI_F: return @"f";
case kVK_ANSI_G: return @"g";
case kVK_ANSI_H: return @"h";
case kVK_ANSI_I: return @"i";
case kVK_ANSI_J: return @"j";
case kVK_ANSI_K: return @"k";
case kVK_ANSI_L: return @"l";
case kVK_ANSI_M: return @"m";
case kVK_ANSI_N: return @"n";
case kVK_ANSI_O: return @"o";
case kVK_ANSI_P: return @"p";
case kVK_ANSI_Q: return @"q";
case kVK_ANSI_R: return @"r";
case kVK_ANSI_S: return @"s";
case kVK_ANSI_T: return @"t";
case kVK_ANSI_U: return @"u";
case kVK_ANSI_V: return @"v";
case kVK_ANSI_W: return @"w";
case kVK_ANSI_X: return @"x";
case kVK_ANSI_Y: return @"y";
case kVK_ANSI_Z: return @"z";
// Number keys
case 29: return @"0";
case 18: return @"1";
case 19: return @"2";
case 20: return @"3";
case 21: return @"4";
case 23: return @"5";
case 22: return @"6";
case 26: return @"7";
case 28: return @"8";
case 25: return @"9";
case kVK_ANSI_0: return @"0";
case kVK_ANSI_1: return @"1";
case kVK_ANSI_2: return @"2";
case kVK_ANSI_3: return @"3";
case kVK_ANSI_4: return @"4";
case kVK_ANSI_5: return @"5";
case kVK_ANSI_6: return @"6";
case kVK_ANSI_7: return @"7";
case kVK_ANSI_8: return @"8";
case kVK_ANSI_9: return @"9";
// Other special keys
case 51: return @"delete";
case 117: return @"forward delete";
case 123: return @"left";
case 124: return @"right";
case 126: return @"up";
case 125: return @"down";
case 48: return @"tab";
case 53: return @"escape";
case 49: return @"space";
case kVK_Delete: return @"delete";
case kVK_ForwardDelete: return @"forward delete";
case kVK_LeftArrow: return @"left";
case kVK_RightArrow: return @"right";
case kVK_UpArrow: return @"up";
case kVK_DownArrow: return @"down";
case kVK_Tab: return @"tab";
case kVK_Escape: return @"escape";
case kVK_Space: return @"space";
// Punctuation and other keys (for a standard US layout)
case 33: return @"[";
case 30: return @"]";
case 43: return @",";
case 27: return @"-";
case 39: return @"'";
case 44: return @"/";
case 47: return @".";
case 41: return @";";
case 24: return @"=";
case 50: return @"`";
case 42: return @"\\";
case kVK_ANSI_LeftBracket: return @"[";
case kVK_ANSI_RightBracket: return @"]";
case kVK_ANSI_Comma: return @",";
case kVK_ANSI_Minus: return @"-";
case kVK_ANSI_Quote: return @"'";
case kVK_ANSI_Slash: return @"/";
case kVK_ANSI_Period: return @".";
case kVK_ANSI_Semicolon: return @";";
case kVK_ANSI_Equal: return @"=";
case kVK_ANSI_Grave: return @"`";
case kVK_ANSI_Backslash: return @"\\";
default: return [self specialKeyStringFromEvent:event];
}
}
Expand Down
6 changes: 4 additions & 2 deletions v3/pkg/application/webview_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ func (w *WebviewWindow) setupEventMapping() {

// NewWindow creates a new window with the given options
func NewWindow(options WebviewWindowOptions) *WebviewWindow {
id := getWindowID()

if options.Width == 0 {
options.Width = 800
}
Expand All @@ -229,11 +231,11 @@ func NewWindow(options WebviewWindowOptions) *WebviewWindow {
}

if options.Name == "" {
options.Name = fmt.Sprintf("window-%d", getWindowID())
options.Name = fmt.Sprintf("Window %d", id)
}

result := &WebviewWindow{
id: getWindowID(),
id: id,
options: options,
eventListeners: make(map[uint][]*WindowEventListener),
contextMenus: make(map[string]*Menu),
Expand Down
Loading

0 comments on commit c142752

Please sign in to comment.