Skip to content

Commit

Permalink
feat: add more apis
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Oct 3, 2024
1 parent 080a64f commit 111031a
Show file tree
Hide file tree
Showing 6 changed files with 600 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ version = "0.1.0"
crate-type = ["cdylib"]

[dependencies]
napi = "2"
napi = { version = "2", default-features = true, features = ["napi9"] }
napi-derive = "2"
tao = "0.30.2"
wry = { version = "0.45.0", features = ["devtools"] }
wry = { version = "0.45.0", features = ["devtools", "fullscreen"] }

[build-dependencies]
napi-build = "2"
Expand Down
16 changes: 14 additions & 2 deletions examples/html.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const requireScript = require('node:module').createRequire(__filename);
const { Application } = requireScript('../index.js');
// const requireScript = require('node:module').createRequire(__filename);
// const { Application } = requireScript('../index.js');
const { Application } = require('../index.js');

const app = new Application();

app.onIpcMessage((data) => {
console.log({ data });
});

const window = app.createBrowserWindow({
html: `<!DOCTYPE html>
<html>
Expand All @@ -10,6 +16,12 @@ const window = app.createBrowserWindow({
</head>
<body>
<h1>Hello world!</h1>
<button id="btn">Click me!</button>
<script>
btn.onclick = function send() {
window.ipc.postMessage('Hello from webview!');
}
</script>
</body>
</html>
`,
Expand Down
102 changes: 102 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,44 @@

/* auto-generated by NAPI-RS */

export const enum FullscreenType {
/** Exclusive fullscreen. */
Exclusive = 0,
/** Borderless fullscreen. */
Borderless = 1
}
export interface Dimensions {
/** The width of the size. */
width: number
/** The height of the size. */
height: number
}
export interface Position {
/** The x position. */
x: number
/** The y position. */
y: number
}
export interface VideoMode {
/** The size of the video mode. */
size: Dimensions
/** The bit depth of the video mode. */
bitDepth: number
/** The refresh rate of the video mode. */
refreshRate: number
}
export interface Monitor {
/** The name of the monitor. */
name?: string
/** The scale factor of the monitor. */
scaleFactor: number
/** The size of the monitor. */
size: Dimensions
/** The position of the monitor. */
position: Position
/** The video modes of the monitor. */
videoModes: Array<VideoMode>
}
export const enum JsProgressBarState {
None = 0,
Normal = 1,
Expand Down Expand Up @@ -55,6 +93,32 @@ export interface BrowserWindowOptions {
userAgent?: string
/** The default theme. */
theme?: Theme
/** The preload script */
preload?: string
/** Whether the window is zoomable via hotkeys or gestures. */
hotkeysZoom?: boolean
/** Whether the clipboard access is enabled. */
clipboard?: boolean
/** Whether the autoplay policy is enabled. */
autoplay?: boolean
/** Indicates whether horizontal swipe gestures trigger backward and forward page navigation. */
backForwardNavigationGestures?: boolean
}
export interface HeaderData {
key: string
value?: string
}
export interface IpcMessage {
/** The unique identifier of the window that sent the message. */
windowId: number
/** The body of the message. */
body: Array<number>
/** The HTTP method of the message. */
method: string
/** The headers of the message. */
headers: Array<HeaderData>
/** The URI of the message. */
uri: string
}
/** Returns the version of the webview. */
export declare function getWebviewVersion(): string
Expand All @@ -79,6 +143,14 @@ export interface ApplicationOptions {
exitCode?: number
}
export declare class BrowserWindow {
/** The unique identifier of this window. */
id(): number
/** Launch a print modal for this window's contents. */
print(): void
/** Set webview zoom level. */
zoom(scaleFacotr: number): void
/** Hides or shows the webview. */
setWebviewVisibility(visible: boolean): void
/** Whether the devtools is opened. */
isDevtoolsOpen(): boolean
/** Opens the devtools. */
Expand Down Expand Up @@ -125,6 +197,8 @@ export declare class BrowserWindow {
setTheme(theme: Theme): void
/** Evaluates the given JavaScript code. */
evaluateScript(js: string): void
/** Evaluates the given JavaScript code with a callback. */
evaluateScriptWithCallback(js: string, callback: (...args: any[]) => any): void
/** Sets the window icon. */
setWindowIcon(icon: Array<number> | string, width: number, height: number): void
/** Removes the window icon. */
Expand All @@ -136,11 +210,39 @@ export declare class BrowserWindow {
setVisible(visible: boolean): void
/** Modifies the window's progress bar. */
setProgressBar(state: JsProgressBar): void
/** Maximizes the window. */
setMaximized(value: boolean): void
/** Minimizes the window. */
setMinimized(value: boolean): void
/** Bring the window to front and focus. */
focus(): void
/** Get available monitors. */
getAvailableMonitors(): Array<Monitor>
/** Get the current monitor. */
getCurrentMonitor(): Monitor | null
/** Get the primary monitor. */
getPrimaryMonitor(): Monitor | null
/** Get the monitor from the given point. */
getMonitorFromPoint(x: number, y: number): Monitor | null
/** Prevents the window contents from being captured by other apps. */
setContentProtection(enabled: boolean): void
/** Sets the window always on top. */
setAlwaysOnTop(enabled: boolean): void
/** Sets always on bottom. */
setAlwaysOnBottom(enabled: boolean): void
/** Turn window decorations on or off. */
setDecorations(enabled: boolean): void
/** Gets the window's current fullscreen state. */
get fullscreen(): FullscreenType | null
/** Sets the window to fullscreen or back. */
setFullscreen(fullscreenType?: FullscreenType | undefined | null): void
}
/** Represents an application. */
export declare class Application {
/** Creates a new application. */
constructor(options?: ApplicationOptions | undefined | null)
/** Sets the IPC handler callback. */
onIpcMessage(handler?: (...args: any[]) => any | undefined | null): void
/** Creates a new browser window. */
createBrowserWindow(options?: BrowserWindowOptions | undefined | null): BrowserWindow
/** Creates a new browser window as a child window. */
Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

/* auto-generated by NAPI-RS */

const { existsSync, readFileSync } = require('node:fs')
const { join } = require('node:path')
const { existsSync, readFileSync } = require('fs')
const { join } = require('path')

const { platform, arch } = process

Expand Down Expand Up @@ -116,7 +116,7 @@ switch (platform) {
nativeBinding = require('@webviewjs/webview-darwin-universal')
}
break
} catch { }
} catch {}
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, 'webview.darwin-x64.node'))
Expand Down Expand Up @@ -310,8 +310,9 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { JsProgressBarState, Theme, BrowserWindow, getWebviewVersion, ControlFlow, Application } = nativeBinding
const { FullscreenType, JsProgressBarState, Theme, BrowserWindow, getWebviewVersion, ControlFlow, Application } = nativeBinding

module.exports.FullscreenType = FullscreenType
module.exports.JsProgressBarState = JsProgressBarState
module.exports.Theme = Theme
module.exports.BrowserWindow = BrowserWindow
Expand Down
Loading

0 comments on commit 111031a

Please sign in to comment.