Skip to content

Commit

Permalink
Re-introduced fullscreen and added decorated for Gtk4
Browse files Browse the repository at this point in the history
This MR exports two extra symbols to remove decorations (chromeless)
and to provide an easy way to make the window full screen.

A new `SizeHint.FULLSCREEN` can be used to provide such hint
while the `Size` now has a `decorated` value which, if `false`,
would remove all decorations around the UI.
  • Loading branch information
WebReflection committed Oct 4, 2024
1 parent b337344 commit 6853dd3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
Binary file modified build/libwebview-x64.so
Binary file not shown.
8 changes: 5 additions & 3 deletions examples/basic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Webview } from "../src";
import { SizeHint, Webview } from "../src";

const html = `
<html>
Expand All @@ -8,7 +8,9 @@ const html = `
</html>
`;

const webview = new Webview();
const webview = new Webview(false, { hint: SizeHint.FULLSCREEN });
// webview.decorated = false;
// webview.fullscreen();
webview.title = "Bun App";
webview.setHTML(html);
webview.run();
webview.run();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "webview-bun",
"name": "@webreflection/webview-bun",
"description": "Bun bindings for webview, a tiny library for creating web-based desktop GUIs.",
"version": "2.3.0",
"type": "module",
Expand Down
8 changes: 8 additions & 0 deletions src/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export const lib = dlopen(lib_file.default, {
args: [FFIType.ptr],
returns: FFIType.ptr
},
webview_set_fullscreen: {
args: [FFIType.ptr],
returns: FFIType.void
},
webview_set_decorated: {
args: [FFIType.ptr, FFIType.i32],
returns: FFIType.void
},
webview_set_title: {
args: [FFIType.ptr, FFIType.ptr],
returns: FFIType.void
Expand Down
31 changes: 26 additions & 5 deletions src/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { encodeCString, instances, lib } from "./ffi";

/** Window size */
export interface Size {
/** The window has chrome around */
decorated: boolean,
/** The width of the window */
width: number,
/** The height of the window */
Expand All @@ -20,7 +22,9 @@ export const enum SizeHint {
/** Width and height are maximum bounds */
MAX,
/** Window size can not be changed by a user */
FIXED
FIXED,
/** Window is fullscreen */
FULLSCREEN,
};

/** An instance of a webview window.*/
Expand All @@ -47,6 +51,10 @@ export class Webview {
return lib.symbols.webview_get_window(this.#handle);
}

set decorated(value:boolean) {
lib.symbols.webview_set_decorated(this.#handle, +value);
}

/**
* Sets the native window size
*
Expand All @@ -69,8 +77,12 @@ export class Webview {
* ```
*/
set size({ width, height, hint }: Size) {
//@ts-ignore
lib.symbols.webview_set_size(this.#handle, width, height, hint);
if (hint === SizeHint.FULLSCREEN)
this.fullscreen();
else {
//@ts-ignore
lib.symbols.webview_set_size(this.#handle, width, height, hint);
}
}

/**
Expand Down Expand Up @@ -136,13 +148,17 @@ export class Webview {
constructor(debug?: boolean, size?: Size, window?: Pointer | null);
constructor(
debugOrHandle: boolean | Pointer = false,
size: Size | undefined = { width: 1024, height: 768, hint: SizeHint.NONE },
size: Size | undefined = { decorated: true, width: 1024, height: 768, hint: SizeHint.NONE },
window: Pointer | null = null,
) {
this.#handle = typeof debugOrHandle === "bigint" || typeof debugOrHandle === "number"
? debugOrHandle
: lib.symbols.webview_create(Number(debugOrHandle), window);
if (size !== undefined) this.size = size;
if (size !== undefined) {
this.size = size;
if (size.decorated === false)
this.decorated = false;
}
instances.push(this);
}

Expand All @@ -157,6 +173,11 @@ export class Webview {
this.#handle = null;
}

/** Creates a full screen window */
fullscreen() {
lib.symbols.webview_set_fullscreen(this.#handle);
}

/**
* Navigates webview to the given URL. URL may be a data URI, i.e.
* `"data:text/html,<html>...</html>"`. It is often ok not to url-encodeCString it
Expand Down

0 comments on commit 6853dd3

Please sign in to comment.