refactor(web): consolidate WASM object constructors as create (#776)

This commit is contained in:
Benoît Cortier
2025-04-23 20:56:11 +03:00
committed by GitHub
parent aef4b924aa
commit 24e64d7589
25 changed files with 174 additions and 192 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ use wasm_bindgen::JsValue;
pub trait ClipboardData {
type Item: ClipboardItem;
fn init() -> Self;
fn create() -> Self;
fn add_text(&mut self, mime_type: &str, text: &str);
fn add_binary(&mut self, mime_type: &str, binary: &[u8]);
fn items(&self) -> &[Self::Item];
@@ -9,7 +9,8 @@ pub struct DesktopSize {
#[wasm_bindgen]
impl DesktopSize {
pub fn init(width: u16, height: u16) -> Self {
#[wasm_bindgen(constructor)]
pub fn create(width: u16, height: u16) -> Self {
DesktopSize { width, height }
}
}
+1 -1
View File
@@ -48,7 +48,7 @@ pub struct Extension {
#[wasm_bindgen]
impl Extension {
#[wasm_bindgen(constructor)]
pub fn new(ident: String, value: JsValue) -> Self {
pub fn create(ident: String, value: JsValue) -> Self {
Self { ident, value }
}
}
+1 -1
View File
@@ -12,6 +12,6 @@ pub trait DeviceEvent {
pub trait InputTransaction {
type DeviceEvent: DeviceEvent;
fn init() -> Self;
fn create() -> Self;
fn add_event(&mut self, event: Self::DeviceEvent);
}
+39 -6
View File
@@ -129,18 +129,22 @@ macro_rules! make_bridge {
self.0.run().await.map(SessionTerminationInfo).map_err(IronError)
}
#[wasm_bindgen(js_name = desktopSize)]
pub fn desktop_size(&self) -> $crate::DesktopSize {
self.0.desktop_size()
}
#[wasm_bindgen(js_name = applyInputs)]
pub fn apply_inputs(&self, transaction: InputTransaction) -> Result<(), IronError> {
self.0.apply_inputs(transaction.0).map_err(IronError)
}
#[wasm_bindgen(js_name = releaseAllInputs)]
pub fn release_all_inputs(&self) -> Result<(), IronError> {
self.0.release_all_inputs().map_err(IronError)
}
#[wasm_bindgen(js_name = synchronizeLockKeys)]
pub fn synchronize_lock_keys(
&self,
scroll_lock: bool,
@@ -157,6 +161,7 @@ macro_rules! make_bridge {
self.0.shutdown().map_err(IronError)
}
#[wasm_bindgen(js_name = onClipboardPaste)]
pub async fn on_clipboard_paste(&self, content: ClipboardData) -> Result<(), IronError> {
self.0.on_clipboard_paste(content.0).await.map_err(IronError)
}
@@ -173,10 +178,12 @@ macro_rules! make_bridge {
.resize(width, height, scale_factor, physical_width, physical_height);
}
#[wasm_bindgen(js_name = supportsUnicodeKeyboardShortcuts)]
pub fn supports_unicode_keyboard_shortcuts(&self) -> bool {
self.0.supports_unicode_keyboard_shortcuts()
}
#[wasm_bindgen(js_name = extensionCall)]
pub fn extension_call(
ext: $crate::Extension,
) -> Result<$crate::internal::wasm_bindgen::JsValue, IronError> {
@@ -187,8 +194,9 @@ macro_rules! make_bridge {
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
#[doc(hidden)]
impl SessionBuilder {
pub fn init() -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::SessionBuilder>::init())
#[wasm_bindgen(constructor)]
pub fn create() -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::SessionBuilder>::create())
}
pub fn username(&self, username: String) -> Self {
@@ -199,6 +207,7 @@ macro_rules! make_bridge {
Self(self.0.destination(destination))
}
#[wasm_bindgen(js_name = serverDomain)]
pub fn server_domain(&self, server_domain: String) -> Self {
Self(self.0.server_domain(server_domain))
}
@@ -207,30 +216,37 @@ macro_rules! make_bridge {
Self(self.0.password(password))
}
#[wasm_bindgen(js_name = proxyAddress)]
pub fn proxy_address(&self, address: String) -> Self {
Self(self.0.proxy_address(address))
}
#[wasm_bindgen(js_name = authToken)]
pub fn auth_token(&self, token: String) -> Self {
Self(self.0.auth_token(token))
}
#[wasm_bindgen(js_name = desktopSize)]
pub fn desktop_size(&self, desktop_size: $crate::DesktopSize) -> Self {
Self(self.0.desktop_size(desktop_size))
}
#[wasm_bindgen(js_name = renderCanvas)]
pub fn render_canvas(&self, canvas: $crate::internal::web_sys::HtmlCanvasElement) -> Self {
Self(self.0.render_canvas(canvas))
}
#[wasm_bindgen(js_name = setCursorStyleCallback)]
pub fn set_cursor_style_callback(&self, callback: $crate::internal::web_sys::js_sys::Function) -> Self {
Self(self.0.set_cursor_style_callback(callback))
}
#[wasm_bindgen(js_name = setCursorStyleCallbackContext)]
pub fn set_cursor_style_callback_context(&self, context: $crate::internal::wasm_bindgen::JsValue) -> Self {
Self(self.0.set_cursor_style_callback_context(context))
}
#[wasm_bindgen(js_name = remoteClipboardChangedCallback)]
pub fn remote_clipboard_changed_callback(
&self,
callback: $crate::internal::web_sys::js_sys::Function,
@@ -238,6 +254,7 @@ macro_rules! make_bridge {
Self(self.0.remote_clipboard_changed_callback(callback))
}
#[wasm_bindgen(js_name = remoteReceivedFormatListCallback)]
pub fn remote_received_format_list_callback(
&self,
callback: $crate::internal::web_sys::js_sys::Function,
@@ -245,6 +262,7 @@ macro_rules! make_bridge {
Self(self.0.remote_received_format_list_callback(callback))
}
#[wasm_bindgen(js_name = forceClipboardUpdateCallback)]
pub fn force_clipboard_update_callback(
&self,
callback: $crate::internal::web_sys::js_sys::Function,
@@ -272,40 +290,48 @@ macro_rules! make_bridge {
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
#[doc(hidden)]
impl DeviceEvent {
#[wasm_bindgen(js_name = mouseButtonPressed)]
pub fn mouse_button_pressed(button: u8) -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::mouse_button_pressed(button))
}
#[wasm_bindgen(js_name = mouseButtonReleased)]
pub fn mouse_button_released(button: u8) -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::mouse_button_released(button))
}
#[wasm_bindgen(js_name = mouseMove)]
pub fn mouse_move(x: u16, y: u16) -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::mouse_move(
x, y,
))
}
#[wasm_bindgen(js_name = wheelRotations)]
pub fn wheel_rotations(vertical: bool, rotation_units: i16) -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::wheel_rotations(vertical, rotation_units))
}
#[wasm_bindgen(js_name = keyPressed)]
pub fn key_pressed(scancode: u16) -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::key_pressed(
scancode,
))
}
#[wasm_bindgen(js_name = keyReleased)]
pub fn key_released(scancode: u16) -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::key_released(
scancode,
))
}
#[wasm_bindgen(js_name = unicodePressed)]
pub fn unicode_pressed(unicode: char) -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::unicode_pressed(unicode))
}
#[wasm_bindgen(js_name = unicodeReleased)]
pub fn unicode_released(unicode: char) -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::unicode_released(unicode))
}
@@ -314,10 +340,12 @@ macro_rules! make_bridge {
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
#[doc(hidden)]
impl InputTransaction {
pub fn init() -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::InputTransaction>::init())
#[wasm_bindgen(constructor)]
pub fn create() -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::InputTransaction>::create())
}
#[wasm_bindgen(js_name = addEvent)]
pub fn add_event(&mut self, event: DeviceEvent) {
self.0.add_event(event.0);
}
@@ -326,14 +354,17 @@ macro_rules! make_bridge {
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
#[doc(hidden)]
impl ClipboardData {
pub fn init() -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::ClipboardData>::init())
#[wasm_bindgen(constructor)]
pub fn create() -> Self {
Self(<<$api as $crate::RemoteDesktopApi>::ClipboardData>::create())
}
#[wasm_bindgen(js_name = addText)]
pub fn add_text(&mut self, mime_type: &str, text: &str) {
self.0.add_text(mime_type, text);
}
#[wasm_bindgen(js_name = addBinary)]
pub fn add_binary(&mut self, mime_type: &str, binary: &[u8]) {
self.0.add_binary(mime_type, binary);
}
@@ -342,6 +373,7 @@ macro_rules! make_bridge {
self.0.items().into_iter().cloned().map(ClipboardItem).collect()
}
#[wasm_bindgen(js_name = isEmpty)]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
@@ -350,6 +382,7 @@ macro_rules! make_bridge {
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
#[doc(hidden)]
impl ClipboardItem {
#[wasm_bindgen(js_name = mimeType)]
pub fn mime_type(&self) -> String {
self.0.mime_type().to_owned()
}
+1 -1
View File
@@ -10,7 +10,7 @@ pub trait SessionBuilder {
type Session: Session;
type Error: IronError;
fn init() -> Self;
fn create() -> Self;
#[must_use]
fn username(&self, username: String) -> Self;
#[must_use]
+1 -1
View File
@@ -613,7 +613,7 @@ impl ClipboardData {
impl iron_remote_desktop::ClipboardData for ClipboardData {
type Item = ClipboardItem;
fn init() -> Self {
fn create() -> Self {
Self::new()
}
+1 -1
View File
@@ -58,7 +58,7 @@ pub(crate) struct InputTransaction(pub(crate) SmallVec<[Operation; 3]>);
impl iron_remote_desktop::InputTransaction for InputTransaction {
type DeviceEvent = DeviceEvent;
fn init() -> Self {
fn create() -> Self {
Self(SmallVec::new())
}
+1 -1
View File
@@ -102,7 +102,7 @@ impl iron_remote_desktop::SessionBuilder for SessionBuilder {
type Session = Session;
type Error = IronError;
fn init() -> Self {
fn create() -> Self {
Self(Rc::new(RefCell::new(SessionBuilderInner::default())))
}
+5 -12
View File
@@ -14,18 +14,11 @@ export async function init(log_level: string) {
}
export const Backend = {
createDesktopSize: DesktopSize.init,
createMouseButtonPressed: DeviceEvent.mouse_button_pressed,
createMouseButtonReleased: DeviceEvent.mouse_button_released,
createMouseMove: DeviceEvent.mouse_move,
createWheelRotations: DeviceEvent.wheel_rotations,
createKeyPressed: DeviceEvent.key_pressed,
createKeyReleased: DeviceEvent.key_released,
createUnicodePressed: DeviceEvent.unicode_pressed,
createUnicodeReleased: DeviceEvent.unicode_released,
createInputTransaction: InputTransaction.init,
createSessionBuilder: SessionBuilder.init,
createClipboardData: ClipboardData.init,
DesktopSize: DesktopSize,
InputTransaction: InputTransaction,
SessionBuilder: SessionBuilder,
ClipboardData: ClipboardData,
DeviceEvent: DeviceEvent,
};
export function preConnectionBlob(pcb: string): Extension {
@@ -1,8 +1,8 @@
import type { ClipboardItem } from './ClipboardItem';
export interface ClipboardData {
add_text(mime_type: string, text: string): void;
add_binary(mime_type: string, binary: Uint8Array): void;
addText(mimeType: string, text: string): void;
addBinary(mimeType: string, binary: Uint8Array): void;
items(): ClipboardItem[];
is_empty(): boolean;
isEmpty(): boolean;
}
@@ -1,4 +1,4 @@
export interface ClipboardItem {
mime_type(): string;
mimeType(): string;
value(): string | Uint8Array;
}
@@ -1,6 +1,5 @@
import type { DeviceEvent } from './DeviceEvent';
export interface InputTransaction {
init(): InputTransaction;
add_event(event: DeviceEvent): void;
addEvent(event: DeviceEvent): void;
}
@@ -1,7 +1,7 @@
import type { DesktopSize } from './DesktopSize';
export interface NewSessionInfo {
session_id: number;
websocket_port: number;
initial_desktop_size: DesktopSize;
sessionId: number;
websocketPort: number;
initialDesktopSize: DesktopSize;
}
@@ -5,16 +5,18 @@ import type { SessionBuilder } from './SessionBuilder';
import type { ClipboardData } from './ClipboardData';
export interface RemoteDesktopModule {
createDesktopSize(width: number, height: number): DesktopSize;
createMouseButtonPressed(button: number): DeviceEvent;
createMouseButtonReleased(button: number): DeviceEvent;
createMouseMove(x: number, y: number): DeviceEvent;
createWheelRotations(vertical: boolean, rotation_units: number): DeviceEvent;
createKeyPressed(scancode: number): DeviceEvent;
createKeyReleased(scancode: number): DeviceEvent;
createUnicodePressed(unicode: string): DeviceEvent;
createUnicodeReleased(unicode: string): DeviceEvent;
createInputTransaction(): InputTransaction;
createSessionBuilder(): SessionBuilder;
createClipboardData(): ClipboardData;
DesktopSize: { new (width: number, height: number): DesktopSize };
InputTransaction: { new (): InputTransaction };
SessionBuilder: { new (): SessionBuilder };
ClipboardData: { new (): ClipboardData };
DeviceEvent: {
mouseButtonPressed(button: number): DeviceEvent;
mouseButtonReleased(button: number): DeviceEvent;
mouseMove(x: number, y: number): DeviceEvent;
wheelRotations(vertical: boolean, rotationUnits: number): DeviceEvent;
keyPressed(scancode: number): DeviceEvent;
keyReleased(scancode: number): DeviceEvent;
unicodePressed(unicode: string): DeviceEvent;
unicodeReleased(unicode: string): DeviceEvent;
};
}
@@ -1,6 +1,6 @@
import type { DesktopSize } from './DesktopSize';
export interface ResizeEvent {
session_id: number;
desktop_size: DesktopSize;
sessionId: number;
desktopSize: DesktopSize;
}
@@ -1,8 +0,0 @@
export interface ServerRect {
bottom: number;
left: number;
right: number;
top: number;
clone_buffer(): Uint8Array;
}
@@ -5,19 +5,19 @@ import type { ClipboardData } from './ClipboardData';
export interface Session {
run(): Promise<SessionTerminationInfo>;
desktop_size(): DesktopSize;
apply_inputs(transaction: InputTransaction): void;
release_all_inputs(): void;
synchronize_lock_keys(scroll_lock: boolean, num_lock: boolean, caps_lock: boolean, kana_lock: boolean): void;
extension_call(value: unknown): unknown;
desktopSize(): DesktopSize;
applyInputs(transaction: InputTransaction): void;
releaseAllInputs(): void;
synchronizeLockKeys(scrollLock: boolean, numLock: boolean, capsLock: boolean, kanaLock: boolean): void;
extensionCall(value: unknown): unknown;
shutdown(): void;
on_clipboard_paste(data: ClipboardData): Promise<void>;
onClipboardPaste(data: ClipboardData): Promise<void>;
resize(
width: number,
height: number,
scale_factor?: number | null,
physical_width?: number | null,
physical_height?: number | null,
scaleFactor?: number | null,
physicalWidth?: number | null,
physicalHeight?: number | null,
): void;
supports_unicode_keyboard_shortcuts(): boolean;
supportsUnicodeKeyboardShortcuts(): boolean;
}
@@ -14,7 +14,7 @@ export interface SessionBuilder {
/**
* Optional
*/
server_domain(server_domain: string): SessionBuilder;
serverDomain(serverDomain: string): SessionBuilder;
/**
* Required
*/
@@ -22,19 +22,19 @@ export interface SessionBuilder {
/**
* Required
*/
proxy_address(address: string): SessionBuilder;
proxyAddress(address: string): SessionBuilder;
/**
* Required
*/
auth_token(token: string): SessionBuilder;
authToken(token: string): SessionBuilder;
/**
* Optional
*/
desktop_size(desktop_size: DesktopSize): SessionBuilder;
desktopSize(desktopSize: DesktopSize): SessionBuilder;
/**
* Optional
*/
render_canvas(canvas: HTMLCanvasElement): SessionBuilder;
renderCanvas(canvas: HTMLCanvasElement): SessionBuilder;
/**
* Required.
*
@@ -44,33 +44,33 @@ export interface SessionBuilder {
* - `url` (custom cursor data URL); `cursor_data` contains the data URL with Base64-encoded
* cursor bitmap; `hotspot_x` and `hotspot_y` are set to the cursor hotspot coordinates.
*/
set_cursor_style_callback(callback: SetCursorStyleCallback): SessionBuilder;
setCursorStyleCallback(callback: SetCursorStyleCallback): SessionBuilder;
/**
* Required.
*/
set_cursor_style_callback_context(context: unknown): SessionBuilder;
setCursorStyleCallbackContext(context: unknown): SessionBuilder;
/**
* Optional
*/
remote_clipboard_changed_callback(callback: RemoteClipboardChangedCallback): SessionBuilder;
remoteClipboardChangedCallback(callback: RemoteClipboardChangedCallback): SessionBuilder;
/**
* Optional
*/
remote_received_format_list_callback(callback: RemoteReceiveForwardListCallback): SessionBuilder;
remoteReceivedFormatListCallback(callback: RemoteReceiveForwardListCallback): SessionBuilder;
/**
* Optional
*/
force_clipboard_update_callback(callback: ForceClipboardUpdateCallback): SessionBuilder;
forceClipboardUpdateCallback(callback: ForceClipboardUpdateCallback): SessionBuilder;
extension(value: unknown): SessionBuilder;
connect(): Promise<Session>;
}
interface SetCursorStyleCallback {
(
cursor_kind: string,
cursor_data: string | undefined,
hotspot_x: number | undefined,
hotspot_y: number | undefined,
cursorKind: string,
cursorData: string | undefined,
hotspotX: number | undefined,
hotspotY: number | undefined,
): void;
}
@@ -13,7 +13,7 @@ export interface UserInteraction {
connect(config: Config): Promise<NewSessionInfo>;
setKeyboardUnicodeMode(use_unicode: boolean): void;
setKeyboardUnicodeMode(useUnicode: boolean): void;
ctrlAltDel(): void;

Some files were not shown because too many files have changed in this diff Show More