Skip to content

Commit

Permalink
Add visibility controls, changelog, and better release process (#43)
Browse files Browse the repository at this point in the history
* Add some more comments

* Add a fuller example

* Explicitly export webview options

* Add setVisibility, isVisible

* Update publish process to tag release

* Add changelog

* Bump to 0.0.7/0.1.6
  • Loading branch information
zephraph authored Sep 23, 2024
1 parent 2447aea commit 7d05cc8
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 8 deletions.
28 changes: 27 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,37 @@ jobs:
runs-on: ubuntu-latest

permissions:
contents: read
contents: write
id-token: write

steps:
- uses: actions/checkout@v4

- name: Get current version
id: current_version
run: echo "version=$(grep '"version"' deno.json | awk '{ print $2 }' | sed 's/[",]//g')" >> $GITHUB_OUTPUT

- name: Get published version
id: published_version
run: echo "version=$(npx jsr show @justbe/webview | head -n 2 | xargs | awk '{print $4}')" >> $GITHUB_OUTPUT

- name: Publish package
if: ${{ steps.current_version.outputs.version != steps.published_version.outputs.version }}
run: npx jsr publish
env:
LATEST_VERSION: ${{ env.latest_version }}

- name: Get latest version
if: ${{ steps.current_version.outputs.version != steps.published_version.outputs.version }}
id: latest_version
run: echo "version=$(npx jsr show @justbe/webview | head -n 2 | xargs | awk '{print $4}')" >> $GITHUB_OUTPUT

- name: Tag and push if versions differ
if: ${{ steps.current_version.outputs.version != steps.published_version.outputs.version && steps.latest_version.outputs.version != steps.published_version.outputs.version }}
run: |
git config user.name github-actions
git config user.email [email protected]
git tag -a v${{ steps.latest_version.outputs.version }} -m "Release ${{ steps.latest_version.outputs.version }}"
git push origin v${{ steps.latest_version.outputs.version }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog

## 0.0.7 (binary 0.1.6) -- 2024-09-23

- Added this changelog
- Add the ability to show and hide the webview window via `.setVisibility(true/false)`
- Added the ability to check if the webview window is visible via `.isVisible()`

## 0.0.6 (binary 0.1.5) -- 2024-09-23

- Fixed a bug where `.on` and `.once` weren't firing for the webview's lifecycle events

## 0.0.5 (binary 0.1.5) -- 2024-09-23

- Improved type generation to avoid publishing slow types to JSR

## 0.0.4 (binary 0.1.5) -- 2024-09-22

- Added examples, doc comments to code
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "deno-webview"
version = "0.1.5"
version = "0.1.6"
edition = "2021"

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@justbe/webview",
"exports": "./src/lib.ts",
"version": "0.0.6",
"version": "0.0.7",
"tasks": {
"dev": "deno run --watch main.ts",
"gen": "deno task gen:rust && deno task gen:deno",
Expand Down
18 changes: 18 additions & 0 deletions schemas/WebViewMessage.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions schemas/WebViewRequest.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions schemas/WebViewResponse.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 54 additions & 4 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
* ```ts
* import { createWebView } from "jsr:@justbe/webview";
*
* using webview = await createWebView({ title: "My Webview" });
* using webview = await createWebView({
* title: "Example",
* html: "<h1>Hello, World!</h1>",
* devtools: true
* });
*
* webview.on("started", async () => {
* await webview.openDevTools();
* await webview.eval("console.log('This is printed from eval!')");
* });
*
* await webview.waitUntilClosed();
* ```
Expand All @@ -25,9 +34,11 @@ import type { Except } from "npm:type-fest";
import { join } from "jsr:@std/path";
import { ensureDir, exists } from "jsr:@std/fs";

export type { WebViewOptions } from "./schemas.ts";

// Should match the cargo package version
/** The version of the webview binary that's expected */
export const BIN_VERSION = "0.1.5";
export const BIN_VERSION = "0.1.6";

type JSON =
| string
Expand All @@ -51,6 +62,10 @@ type ResultKinds = Pick<ResultType, "$type">["$type"];
* @param result - The result of the webview request.
* @param expectedType - The format of the expected result.
*/
function returnResult(
result: WebViewResponse,
expectedType: "boolean",
): boolean;
function returnResult(
result: WebViewResponse,
expectedType: "string",
Expand All @@ -62,7 +77,7 @@ function returnResult(
function returnResult(
result: WebViewResponse,
expectedType?: ResultKinds,
): string | JSON {
): string | JSON | boolean {
switch (result.$type) {
case "result": {
if (expectedType && result.result.$type !== expectedType) {
Expand All @@ -74,6 +89,8 @@ function returnResult(
return res.value;
case "json":
return JSON.parse(res.value) as JSON;
case "boolean":
return res.value;
}
break;
}
Expand Down Expand Up @@ -295,6 +312,9 @@ export class WebView implements Disposable {
this.#externalEvent.once(event, callback);
}

/**
* Sets the title of the webview window.
*/
async setTitle(title: string): Promise<void> {
const result = await this.#send({
$type: "setTitle",
Expand All @@ -304,13 +324,26 @@ export class WebView implements Disposable {
}

/**
* Gets the title of the webview.
* Gets the title of the webview window.
*/
async getTitle(): Promise<string> {
const result = await this.#send({ $type: "getTitle" });
return returnResult(result, "string");
}

/**
* Sets the visibility of the webview window.
*/
async setVisibility(visible: boolean): Promise<void> {
const result = await this.#send({ $type: "setVisibility", visible });
return returnAck(result);
}

async isVisible(): Promise<boolean> {
const result = await this.#send({ $type: "isVisible" });
return returnResult(result, "boolean");
}

/**
* Evaluates JavaScript code in the webview.
*/
Expand Down Expand Up @@ -343,6 +376,23 @@ export class WebView implements Disposable {
this[Symbol.dispose]();
}

/**
* Part of the explicit resource management feature added in TS 5.2
*
* When a reference to the webview is stored with `using` this method
* will be called automatically when the webview goes out of scope.
*
* @example
*
* ```ts
* {
* using webview = await createWebView({ title: "My Webview" });
* } // Webview will be cleaned up here
*
* ```
*
* @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management
*/
[Symbol.dispose](): void {
this.#internalEvent.removeAllListeners();
this.#stdin.releaseLock();
Expand Down
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ enum Request {
Eval { id: String, js: String },
SetTitle { id: String, title: String },
GetTitle { id: String },
SetVisibility { id: String, visible: bool },
IsVisible { id: String },
OpenDevTools { id: String },
}

Expand All @@ -108,6 +110,7 @@ enum Response {
enum ResultType {
String(String),
Json(String),
Boolean(bool),
}

impl From<String> for ResultType {
Expand All @@ -116,6 +119,12 @@ impl From<String> for ResultType {
}
}

impl From<bool> for ResultType {
fn from(value: bool) -> Self {
ResultType::Boolean(value)
}
}

fn main() -> wry::Result<()> {
let args: Vec<String> = env::args().collect();
let webview_options: WebViewOptions = serde_json::from_str(&args[1]).unwrap();
Expand Down Expand Up @@ -254,6 +263,14 @@ fn main() -> wry::Result<()> {
});
}
}
Request::SetVisibility { id, visible } => {
window.set_visible(visible);
res(Response::Ack { id });
}
Request::IsVisible { id } => res(Response::Result {
id,
result: window.is_visible().into(),
}),
}
}
}
Expand Down
Loading

0 comments on commit 7d05cc8

Please sign in to comment.