@heybox/hb-sdk API Reference - v0.9.5
    正在准备搜索索引...
    • 获取当前小程序窗口信息。

      返回 Promise<MiniProgramWindowInfoResult>

      调用时经过 Runtime 归一化的设备像素比、屏幕与窗口尺寸、安全区域及顶部偏移快照。

      HbMiniProgramSDKError 当前 Host 未开放窗口测量能力时以 METHOD_FORBIDDEN 拒绝;握手失败、Runtime 已卸载、SDK 已销毁、请求超时或 Host 测量失败时抛出对应 bridge / Runtime 错误。

      该方法无入参,不触发用户授权,也无需在 package.json#heybox.permissions 中声明。 每次调用都会重新读取当前窗口并返回一次性快照;它不订阅尺寸变化。PC 可缩放窗口 可能在用户拖拽、Host 恢复窗口或系统缩放变化后得到新尺寸,需要最新值时应重新调用。

      package.json#heybox.window 的 defaultWidth、defaultHeight、minWidth、 minHeight 与 resizable 只控制开窗初值和用户拖拽约束,不能替代本 API 的运行时 查询。该方法只读取几何信息;导航栏前景色由 MiniProgramViewportModule.setNavigationBarStyle 单独设置。

      import hbSDK from '@heybox/hb-sdk'

      const info = await hbSDK.viewport.getWindowInfo()
      const safeInsets = {
      top: info.safeArea.top,
      right: info.windowWidth - info.safeArea.right,
      bottom: info.windowHeight - info.safeArea.bottom,
      left: info.safeArea.left,
      }

      const root = document.documentElement
      for (const [edge, value] of Object.entries(safeInsets)) {
      root.style.setProperty(`--safe-area-${edge}`, `${value}px`)
      }
      // 窗口尺寸变化后重新调用 getWindowInfo(),不要长期缓存这次结果。

      0.3.1

      android

      ios

      ohos

      windows

      macos

      android none

      ios none

      ohos none

      windows none

      macos none