feat(web): add dynamic_resizing_supported callback

This commit is contained in:
Alex Yusiuk 2025-06-19 18:49:12 +03:00 committed by Benoît CORTIER
parent 4d9cf56e68
commit a8815ec09e
No known key found for this signature in database
GPG key ID: FE943BF70ABEA672
11 changed files with 68 additions and 10 deletions

2
Cargo.lock generated
View file

@ -2313,7 +2313,7 @@ dependencies = [
[[package]]
name = "iron-remote-desktop"
version = "0.3.0"
version = "0.4.0"
dependencies = [
"console_error_panic_hook",
"tracing",

View file

@ -1,6 +1,6 @@
[package]
name = "iron-remote-desktop"
version = "0.3.0"
version = "0.4.0"
readme = "README.md"
description = "Helper crate for building WASM modules compatible with iron-remote-desktop WebComponent"
edition.workspace = true

View file

@ -287,6 +287,16 @@ macro_rules! make_bridge {
))
}
#[wasm_bindgen(js_name = dynamicResizingSupportedCallback)]
pub fn dynamic_resizing_supported_callback(
&self,
callback: $crate::internal::web_sys::js_sys::Function,
) -> Self {
Self($crate::SessionBuilder::dynamic_resizing_supported_callback(
&self.0, callback,
))
}
pub fn extension(&self, ext: $crate::Extension) -> Self {
Self($crate::SessionBuilder::extension(&self.0, ext))
}

View file

@ -51,6 +51,9 @@ pub trait SessionBuilder {
#[must_use]
fn force_clipboard_update_callback(&self, callback: js_sys::Function) -> Self;
#[must_use]
fn dynamic_resizing_supported_callback(&self, callback: js_sys::Function) -> Self;
#[must_use]
fn extension(&self, ext: Extension) -> Self;

View file

@ -66,6 +66,7 @@ struct SessionBuilderInner {
remote_clipboard_changed_callback: Option<js_sys::Function>,
remote_received_format_list_callback: Option<js_sys::Function>,
force_clipboard_update_callback: Option<js_sys::Function>,
dynamic_resizing_supported_callback: Option<js_sys::Function>,
use_display_control: bool,
}
@ -93,6 +94,7 @@ impl Default for SessionBuilderInner {
remote_clipboard_changed_callback: None,
remote_received_format_list_callback: None,
force_clipboard_update_callback: None,
dynamic_resizing_supported_callback: None,
use_display_control: false,
}
@ -205,6 +207,12 @@ impl iron_remote_desktop::SessionBuilder for SessionBuilder {
self.clone()
}
/// Optional
fn dynamic_resizing_supported_callback(&self, callback: js_sys::Function) -> Self {
self.0.borrow_mut().dynamic_resizing_supported_callback = Some(callback);
self.clone()
}
fn extension(&self, ext: Extension) -> Self {
iron_remote_desktop::extension_match! {
match ext;
@ -310,6 +318,12 @@ impl iron_remote_desktop::SessionBuilder for SessionBuilder {
let use_display_control = self.0.borrow().use_display_control;
if use_display_control {
if let Some(callback) = self.0.borrow().dynamic_resizing_supported_callback.clone() {
callback.call0(&JsValue::NULL).expect("failed to call JS callback");
};
}
let (connection_result, ws) = connect(ConnectParams {
ws,
config,

View file

@ -10,7 +10,7 @@
"Alexandr Yusuk"
],
"description": "Backend-agnostic Web Component for remote desktop protocols",
"version": "0.5.1",
"version": "0.5.2",
"main": "iron-remote-desktop.js",
"types": "index.d.ts",
"files": [

View file

@ -61,6 +61,10 @@ export interface SessionBuilder {
* Optional
*/
forceClipboardUpdateCallback(callback: ForceClipboardUpdateCallback): SessionBuilder;
/**
* Optional
*/
dynamicResizingSupportedCallback(callback: DynamicResizingSupportedCallback): SessionBuilder;
extension(value: unknown): SessionBuilder;
connect(): Promise<Session>;
}
@ -85,3 +89,7 @@ interface RemoteReceiveForwardListCallback {
interface ForceClipboardUpdateCallback {
(): void;
}
interface DynamicResizingSupportedCallback {
(): void;
}

View file

@ -478,7 +478,7 @@
remoteDesktopService.dynamicResizeObservable.subscribe((evt) => {
loggingService.info(`Dynamic resize!, width: ${evt.width}, height: ${evt.height}`);
setViewerStyle(evt.height.toString(), evt.width.toString(), true);
setViewerStyle(evt.height.toString() + 'px', evt.width.toString() + 'px', true);
});
remoteDesktopService.changeVisibilityObservable.subscribe((val) => {
@ -523,19 +523,18 @@
function fullResize() {
const windowSize = getWindowSize();
const wrapperBoundingBox = wrapper.getBoundingClientRect();
const containerWidth = windowSize.x - wrapperBoundingBox.x;
const containerHeight = windowSize.y - wrapperBoundingBox.y;
const containerWidth = windowSize.x;
const containerHeight = windowSize.y;
let width = canvas.width;
let height = canvas.height;
const ratio = Math.max(containerWidth / canvas.width, containerHeight / canvas.height);
const ratio = Math.min(containerWidth / canvas.width, containerHeight / canvas.height);
width = width * ratio;
height = height * ratio;
setWrapperStyle(`${containerHeight}px`, `${containerWidth}px`, 'auto');
setWrapperStyle(`${containerHeight}px`, `${containerWidth}px`, 'hidden');
width = width > 0 ? width : 0;
height = height > 0 ? height : 0;

View file

@ -10,6 +10,7 @@ export class Config {
readonly authToken: string;
readonly desktopSize?: DesktopSize;
readonly extensions: Extension[];
readonly dynamicResizeSupportedCallback?: () => void;
constructor(
userData: { username: string; password: string },
@ -20,6 +21,9 @@ export class Config {
extensions: Extension[];
desktopSize?: DesktopSize;
},
callbacks: {
dynamicResizeSupportedCallback?: () => void;
},
) {
this.username = userData.username;
this.password = userData.password;
@ -29,5 +33,6 @@ export class Config {
this.serverDomain = configOptions.serverDomain;
this.extensions = configOptions.extensions;
this.desktopSize = configOptions.desktopSize;
this.dynamicResizeSupportedCallback = callbacks.dynamicResizeSupportedCallback;
}
}

View file

@ -25,6 +25,7 @@ export class ConfigBuilder {
private authToken: string = '';
private desktopSize?: DesktopSize;
private extensions: Extension[] = [];
private dynamicResizeSupportedCallback?: () => void;
/**
* Creates a new ConfigBuilder instance.
@ -119,6 +120,16 @@ export class ConfigBuilder {
return this;
}
/**
* Optional
* @param callback - The callback function
* @returns The builder instance for method chaining
*/
withDynamicResizeSupportedCallback(callback: () => void): ConfigBuilder {
this.dynamicResizeSupportedCallback = callback;
return this;
}
/**
* Builds a new Config instance.
*
@ -145,6 +156,10 @@ export class ConfigBuilder {
desktopSize: this.desktopSize,
};
return new Config(userData, proxyData, configOptions);
const callbacks = {
dynamicResizeSupportedCallback: this.dynamicResizeSupportedCallback,
};
return new Config(userData, proxyData, configOptions, callbacks);
}
}

View file

@ -136,6 +136,10 @@ export class RemoteDesktopService {
sessionBuilder.forceClipboardUpdateCallback(this.onForceClipboardUpdate);
}
if (config.dynamicResizeSupportedCallback != null) {
sessionBuilder.dynamicResizingSupportedCallback(config.dynamicResizeSupportedCallback);
}
if (config.desktopSize != null) {
sessionBuilder.desktopSize(
new this.module.DesktopSize(config.desktopSize.width, config.desktopSize.height),