[OH] Provide correct geometry offset and fix available screen dimensions (#36915)

This commit corrects the geometry details provided under the OH platform
by getting the offset from the OS. OH port provides correct offset and
available space.

Fixes: #36466

---------

Signed-off-by: Astraea Quinn Skoutelli <astraea.quinn.skoutelli@huawei.com>
Signed-off-by: Jonathan Schwender <55576758+jschwe@users.noreply.github.com>
Co-authored-by: Jonathan Schwender <55576758+jschwe@users.noreply.github.com>
This commit is contained in:
Astraea Quinn S 2025-05-09 17:07:49 +02:00 committed by GitHub
parent 2aaf9695df
commit 366515f256
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 6 deletions

View file

@ -38,6 +38,14 @@ impl Coordinates {
viewport: Rect::new(Point2D::new(x, y), Size2D::new(width, height)),
}
}
pub fn origin(&self) -> Point2D<i32, DevicePixel> {
self.viewport.origin
}
pub fn size(&self) -> Size2D<i32, DevicePixel> {
self.viewport.size
}
}
pub(super) struct ServoWindowCallbacks {
@ -115,11 +123,13 @@ impl ServoDelegate for ServoShellServoDelegate {
impl WebViewDelegate for RunningAppState {
fn screen_geometry(&self, _webview: WebView) -> Option<ScreenGeometry> {
let coord = self.callbacks.coordinates.borrow();
let screen_size = DeviceIntSize::new(coord.viewport.size.width, coord.viewport.size.height);
let offset = coord.origin();
let available_size = coord.size();
let screen_size = coord.size();
Some(ScreenGeometry {
size: screen_size,
available_size: screen_size,
offset: Point2D::zero(),
available_size,
offset,
})
}

View file

@ -28,8 +28,9 @@ use servo::{
use xcomponent_sys::{
OH_NativeXComponent, OH_NativeXComponent_Callback, OH_NativeXComponent_GetKeyEvent,
OH_NativeXComponent_GetKeyEventAction, OH_NativeXComponent_GetKeyEventCode,
OH_NativeXComponent_GetTouchEvent, OH_NativeXComponent_GetXComponentSize,
OH_NativeXComponent_KeyAction, OH_NativeXComponent_KeyCode, OH_NativeXComponent_KeyEvent,
OH_NativeXComponent_GetTouchEvent, OH_NativeXComponent_GetXComponentOffset,
OH_NativeXComponent_GetXComponentSize, OH_NativeXComponent_KeyAction,
OH_NativeXComponent_KeyCode, OH_NativeXComponent_KeyEvent,
OH_NativeXComponent_RegisterCallback, OH_NativeXComponent_RegisterKeyEventCallback,
OH_NativeXComponent_TouchEvent, OH_NativeXComponent_TouchEventType,
};
@ -267,6 +268,33 @@ extern "C" fn on_surface_created_cb(xcomponent: *mut OH_NativeXComponent, window
info!("Returning from on_surface_created_cb");
}
/// Returns the offset of the surface relative to its parent's top left corner
///
/// # Safety
///
/// `xcomponent` and `native_window` must be valid, non-null and aligned pointers to a
/// live xcomponent and associated native window surface.
unsafe fn get_xcomponent_offset(
xcomponent: *mut OH_NativeXComponent,
native_window: *mut c_void,
) -> Result<(i32, i32), i32> {
let mut x: f64 = 0.0;
let mut y: f64 = 0.0;
let result = unsafe {
OH_NativeXComponent_GetXComponentOffset(xcomponent, native_window, &raw mut x, &raw mut y)
};
if result != 0 {
error!("OH_NativeXComponent_GetXComponentOffset failed with {result}");
return Err(result);
}
Ok((
(x.round() as i64).try_into().expect("X offset too large"),
(y.round() as i64).try_into().expect("Y offset too large"),
))
}
/// Returns the size of the surface
///
/// # Safety

View file

@ -97,7 +97,10 @@ pub fn init(
let Ok(window_size) = (unsafe { super::get_xcomponent_size(xcomponent, native_window) }) else {
return Err("Failed to get xcomponent size");
};
let coordinates = Coordinates::new(0, 0, window_size.width, window_size.height);
let Ok((x, y)) = (unsafe { super::get_xcomponent_offset(xcomponent, native_window) }) else {
return Err("Failed to get xcomponent offset");
};
let coordinates = Coordinates::new(x, y, window_size.width, window_size.height);
let display_handle = RawDisplayHandle::Ohos(OhosDisplayHandle::new());
let display_handle = unsafe { DisplayHandle::borrow_raw(display_handle) };