diff --git a/v2/internal/frontend/desktop/darwin/Application.h b/v2/internal/frontend/desktop/darwin/Application.h index 4239da93b..8be3826de 100644 --- a/v2/internal/frontend/desktop/darwin/Application.h +++ b/v2/internal/frontend/desktop/darwin/Application.h @@ -41,6 +41,7 @@ void ShowApplication(void* ctx); void SetBackgroundColour(void* ctx, int r, int g, int b, int a); void ExecJS(void* ctx, const char*); void Quit(void*); +void WindowPrint(void* ctx); const char* GetSize(void *ctx); const char* GetPosition(void *ctx); diff --git a/v2/internal/frontend/desktop/darwin/Application.m b/v2/internal/frontend/desktop/darwin/Application.m index 2c08002d4..082018f62 100644 --- a/v2/internal/frontend/desktop/darwin/Application.m +++ b/v2/internal/frontend/desktop/darwin/Application.m @@ -10,6 +10,7 @@ #import "WailsContext.h" #import "Application.h" #import "AppDelegate.h" +#import "WindowDelegate.h" #import "WailsMenu.h" #import "WailsMenuItem.h" @@ -384,3 +385,36 @@ void ReleaseContext(void *inctx) { WailsContext *ctx = (__bridge WailsContext*) inctx; [ctx release]; } + +// Credit: https://stackoverflow.com/q/33319295 +void WindowPrint(void *inctx) { + + // Check if macOS 11.0 or newer + if (@available(macOS 11.0, *)) { + ON_MAIN_THREAD( + WailsContext *ctx = (__bridge WailsContext*) inctx; + WKWebView* webView = ctx.webview; + + // I think this should be exposed as a config + // It directly affects the printed output/PDF + NSPrintInfo *pInfo = [NSPrintInfo sharedPrintInfo]; + pInfo.horizontalPagination = NSPrintingPaginationModeAutomatic; + pInfo.verticalPagination = NSPrintingPaginationModeAutomatic; + pInfo.verticallyCentered = YES; + pInfo.horizontallyCentered = YES; + pInfo.orientation = NSPaperOrientationLandscape; + pInfo.leftMargin = 0; + pInfo.rightMargin = 0; + pInfo.topMargin = 0; + pInfo.bottomMargin = 0; + + NSPrintOperation *po = [webView printOperationWithPrintInfo:pInfo]; + po.showsPrintPanel = YES; + po.showsProgressPanel = YES; + + po.view.frame = webView.bounds; + + [po runOperationModalForWindow:ctx.mainWindow delegate:ctx.mainWindow.delegate didRunSelector:nil contextInfo:nil]; + ) + } +} \ No newline at end of file diff --git a/v2/internal/frontend/desktop/darwin/frontend.go b/v2/internal/frontend/desktop/darwin/frontend.go index c0dd27029..bfe470b40 100644 --- a/v2/internal/frontend/desktop/darwin/frontend.go +++ b/v2/internal/frontend/desktop/darwin/frontend.go @@ -286,6 +286,10 @@ func (f *Frontend) Quit() { f.mainWindow.Quit() } +func (f *Frontend) WindowPrint() { + f.mainWindow.Print() +} + type EventNotify struct { Name string `json:"name"` Data []interface{} `json:"data"` diff --git a/v2/internal/frontend/desktop/darwin/window.go b/v2/internal/frontend/desktop/darwin/window.go index c4fb9c0f3..2586b1b48 100644 --- a/v2/internal/frontend/desktop/darwin/window.go +++ b/v2/internal/frontend/desktop/darwin/window.go @@ -263,3 +263,7 @@ func (w *Window) SetApplicationMenu(inMenu *menu.Menu) { func (w *Window) UpdateApplicationMenu() { C.UpdateApplicationMenu(w.context) } + +func (w Window) Print() { + C.WindowPrint(w.context) +} diff --git a/v2/internal/frontend/desktop/linux/frontend.go b/v2/internal/frontend/desktop/linux/frontend.go index 654edae17..7a25df4ac 100644 --- a/v2/internal/frontend/desktop/linux/frontend.go +++ b/v2/internal/frontend/desktop/linux/frontend.go @@ -362,6 +362,10 @@ func (f *Frontend) Quit() { f.mainWindow.Quit() } +func (f *Frontend) WindowPrint() { + f.ExecJS("window.print();") +} + type EventNotify struct { Name string `json:"name"` Data []interface{} `json:"data"` diff --git a/v2/internal/frontend/desktop/windows/frontend.go b/v2/internal/frontend/desktop/windows/frontend.go index 76cecbc93..dabd1e2cd 100644 --- a/v2/internal/frontend/desktop/windows/frontend.go +++ b/v2/internal/frontend/desktop/windows/frontend.go @@ -425,6 +425,10 @@ func (f *Frontend) Quit() { f.mainWindow.Invoke(winc.Exit) } +func (f *Frontend) WindowPrint() { + f.ExecJS("window.print();") +} + func (f *Frontend) setupChromium() { chromium := f.chromium diff --git a/v2/internal/frontend/frontend.go b/v2/internal/frontend/frontend.go index 63807da6b..27eb4a2e7 100644 --- a/v2/internal/frontend/frontend.go +++ b/v2/internal/frontend/frontend.go @@ -121,6 +121,7 @@ type Frontend interface { WindowIsNormal() bool WindowIsFullscreen() bool WindowClose() + WindowPrint() //Screen ScreenGetAll() ([]Screen, error) diff --git a/v2/pkg/runtime/window.go b/v2/pkg/runtime/window.go index ca28e9d30..62345e2e4 100644 --- a/v2/pkg/runtime/window.go +++ b/v2/pkg/runtime/window.go @@ -179,3 +179,8 @@ func WindowSetBackgroundColour(ctx context.Context, R, G, B, A uint8) { } appFrontend.WindowSetBackgroundColour(col) } + +func WindowPrint(ctx context.Context) { + appFrontend := getFrontend(ctx) + appFrontend.WindowPrint() +} diff --git a/website/docs/reference/runtime/window.mdx b/website/docs/reference/runtime/window.mdx index d25725565..69ed1fe2b 100644 --- a/website/docs/reference/runtime/window.mdx +++ b/website/docs/reference/runtime/window.mdx @@ -233,6 +233,13 @@ Any value that is not 0 will be considered 255. Go: `WindowSetBackgroundColour(ctx context.Context, R, G, B, A uint8)`
JS: `WindowSetBackgroundColour(R, G, B, A)` +### WindowPrint + +Opens tha native print dialog. + +Go: `WindowPrint(ctx context.Context)`
+JS: `WindowPrint()` + ## TypeScript Object Definitions ### Position diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index 721d43a8f..755b2d99a 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added new community template wails-sveltekit-ts. Added by [@haukened](https://github.com/haukened) in [PR](https://github.com/wailsapp/wails/pull/2851) - Added support for retrieving the logical and physical screen size in the screen api. Added by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2856) - Added new community template wails-sveltekit-tailwind. Added by [@pylotlight](https://github.com/pylotlight) in [PR](https://github.com/wailsapp/wails/pull/2851) +- Added support for print dialogs. Added by [@aangelisc](https://github.com/aangelisc) in [PR](https://github.com/wailsapp/wails/pull/2822) - Added new `wails dev -nogorebuild` flag to prevent restarts on back end file changes. [@haukened](https://github.com/haukened) in [PR #2870](https://github.com/wailsapp/wails/pull/2870) ### Changed