5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 17:22:01 +08:00

Add print functionality to v2 (#2822)

* Add print functionality to v2

* Update changelog

* Update runtime docs

---------

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
This commit is contained in:
Andreas Christou 2023-09-05 22:55:36 +01:00 committed by GitHub
parent d599fd80cb
commit 886bcc7b47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 65 additions and 0 deletions

View File

@ -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);

View File

@ -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];
)
}
}

View File

@ -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"`

View File

@ -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)
}

View File

@ -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"`

View File

@ -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

View File

@ -121,6 +121,7 @@ type Frontend interface {
WindowIsNormal() bool
WindowIsFullscreen() bool
WindowClose()
WindowPrint()
//Screen
ScreenGetAll() ([]Screen, error)

View File

@ -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()
}

View File

@ -233,6 +233,13 @@ Any value that is not 0 will be considered 255.
Go: `WindowSetBackgroundColour(ctx context.Context, R, G, B, A uint8)`<br/>
JS: `WindowSetBackgroundColour(R, G, B, A)`
### WindowPrint
Opens tha native print dialog.
Go: `WindowPrint(ctx context.Context)`<br/>
JS: `WindowPrint()`
## TypeScript Object Definitions
### Position

View File

@ -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