diff --git a/exp/pkg/application/menuitem.go b/exp/pkg/application/menuitem.go index 8be6d9ea0..0fe6409a7 100644 --- a/exp/pkg/application/menuitem.go +++ b/exp/pkg/application/menuitem.go @@ -158,12 +158,12 @@ func newRole(role Role) *MenuItem { return newToggleFullscreenMenuItem() case ToggleDevTools: return newToggleDevToolsMenuItem() - //case ResetZoom: - // return newResetZoomMenuItem() - //case ZoomIn: - // return newZoomInMenuItem() - //case ZoomOut: - // return newZoomOutMenuItem() + case ResetZoom: + return newResetZoomMenuItem() + case ZoomIn: + return newZoomInMenuItem() + case ZoomOut: + return newZoomOutMenuItem() default: println("No support for role:", role) diff --git a/exp/pkg/application/menuitem_darwin.go b/exp/pkg/application/menuitem_darwin.go index d4fa46296..b554da718 100644 --- a/exp/pkg/application/menuitem_darwin.go +++ b/exp/pkg/application/menuitem_darwin.go @@ -601,3 +601,37 @@ func newToggleDevToolsMenuItem() *MenuItem { } }) } + +func newResetZoomMenuItem() *MenuItem { + // reset zoom menu item + return newMenuItem("Actual Size"). + SetAccelerator("CmdOrCtrl+0"). + OnClick(func(ctx *Context) { + currentWindow := globalApplication.GetCurrentWindow() + if currentWindow != nil { + currentWindow.ResetZoom() + } + }) +} + +func newZoomInMenuItem() *MenuItem { + return newMenuItem("Zoom In"). + SetAccelerator("CmdOrCtrl+plus"). + OnClick(func(ctx *Context) { + currentWindow := globalApplication.GetCurrentWindow() + if currentWindow != nil { + currentWindow.ZoomIn() + } + }) +} + +func newZoomOutMenuItem() *MenuItem { + return newMenuItem("Zoom Out"). + SetAccelerator("CmdOrCtrl+-"). + OnClick(func(ctx *Context) { + currentWindow := globalApplication.GetCurrentWindow() + if currentWindow != nil { + currentWindow.ZoomOut() + } + }) +} diff --git a/exp/pkg/application/roles.go b/exp/pkg/application/roles.go index 03ee7c958..c36a8c45d 100644 --- a/exp/pkg/application/roles.go +++ b/exp/pkg/application/roles.go @@ -86,9 +86,9 @@ func newViewMenu() *MenuItem { viewMenu.AddRole(ForceReload) viewMenu.AddRole(ToggleDevTools) viewMenu.AddSeparator() - //viewMenu.AddRole(ResetZoom) - //viewMenu.AddRole(ZoomIn) - //viewMenu.AddRole(ZoomOut) + viewMenu.AddRole(ResetZoom) + viewMenu.AddRole(ZoomIn) + viewMenu.AddRole(ZoomOut) viewMenu.AddSeparator() viewMenu.AddRole(ToggleFullscreen) subMenu := newSubMenuItem("View") diff --git a/exp/pkg/application/window.go b/exp/pkg/application/window.go index 9280e312f..169c18892 100644 --- a/exp/pkg/application/window.go +++ b/exp/pkg/application/window.go @@ -37,6 +37,9 @@ type windowImpl interface { forceReload() toggleFullscreen() toggleDevTools() + resetZoom() + zoomIn() + zoomOut() } type Window struct { @@ -323,3 +326,24 @@ func (w *Window) ToggleDevTools() { } w.impl.toggleDevTools() } + +func (w *Window) ResetZoom() { + if w.impl == nil { + return + } + w.impl.resetZoom() +} + +func (w *Window) ZoomIn() { + if w.impl == nil { + return + } + w.impl.zoomIn() +} + +func (w *Window) ZoomOut() { + if w.impl == nil { + return + } + w.impl.zoomOut() +} diff --git a/exp/pkg/application/window_darwin.go b/exp/pkg/application/window_darwin.go index aaa7e874f..bf33fcf2b 100644 --- a/exp/pkg/application/window_darwin.go +++ b/exp/pkg/application/window_darwin.go @@ -184,6 +184,43 @@ void windowEnableDevTools(void* nsWindow) { }); } +// windowResetZoom +void windowResetZoom(void* nsWindow) { + // Reset zoom on main thread + dispatch_async(dispatch_get_main_queue(), ^{ + // Get window delegate + WindowDelegate* delegate = (WindowDelegate*)[(NSWindow*)nsWindow delegate]; + // Reset zoom + [delegate.webView setMagnification:1.0]; + }); +} + +// windowZoomIn +void windowZoomIn(void* nsWindow) { + // Zoom in on main thread + dispatch_async(dispatch_get_main_queue(), ^{ + // Get window delegate + WindowDelegate* delegate = (WindowDelegate*)[(NSWindow*)nsWindow delegate]; + // Zoom in + [delegate.webView setMagnification:delegate.webView.magnification + 0.05]; + }); +} + +// windowZoomOut +void windowZoomOut(void* nsWindow) { + // Zoom out on main thread + dispatch_async(dispatch_get_main_queue(), ^{ + // Get window delegate + WindowDelegate* delegate = (WindowDelegate*)[(NSWindow*)nsWindow delegate]; + // Zoom out + if( delegate.webView.magnification > 1.05 ) { + [delegate.webView setMagnification:delegate.webView.magnification - 0.05]; + } else { + [delegate.webView setMagnification:1.0]; + } + }); +} + // Execute JS in NSWindow void windowExecJS(void* nsWindow, char* js) { // Execute JS on main thread @@ -489,6 +526,18 @@ type macosWindow struct { // devtools } +func (w *macosWindow) zoomIn() { + C.windowZoomIn(w.nsWindow) +} + +func (w *macosWindow) zoomOut() { + C.windowZoomOut(w.nsWindow) +} + +func (w *macosWindow) resetZoom() { + C.windowResetZoom(w.nsWindow) +} + func (w *macosWindow) toggleDevTools() { showDevTools(w.nsWindow) }