目录

子窗口无法拖动

问题现象

通过createSubWindow创建了一个子窗口来实现悬浮窗,设置了拖动事件,但是不生效。

背景知识

问题定位

设置子窗口背景发现子窗口大小是整个屏幕,使用resize也没用,发现创建子窗口后setFollowParentWindowLayoutEnabled为true,导致子窗口的布局信息与主窗口完全一致并保持(主窗口的大小为整个屏幕)。当子窗口调用该接口后,再调用moveTo、resize等修改布局信息的接口将不生效,当再次调用setFollowParentWindowLayoutEnabled接口并传入false时,效果将不再持续。

分析结论

setFollowParentWindowLayoutEnabled为true,导致子窗口的布局信息与主窗口完全一致并保持,所以无法单独拖动子窗口,除非拖动主窗口使子窗口跟随移动。

修改建议

  1. 不使用setFollowParentWindowLayoutEnabled接口时,通过滑动手势PanGesture能正常拖动子窗口。
    this.windowStage.createSubWindow('SubWindow', async (err: BusinessError, data: window.Window | null) => {
      if (err.code !== 0 || !data) {
        console.error(`Failed to create the subwindow. Code: ${err.code}, message: ${err.message}`);
        return;
      }
      const win = data;
      try {
        // 1. 设置大小
        await win.resize(this.uiContext.vp2px(100), this.uiContext.vp2px(100));
        await win.setUIContent('pages/SubWindow');
        // 2. 设置透明背景,避免遮挡
        await win.setWindowBackgroundColor('#50FF00');
        // 3. 显示窗口
        await win.showWindow();
        await win.setWindowTouchable(true);
        await win.setWindowFocusable(false);
      } catch (subErr) {
        console.error('子窗口操作失败: ' + subErr.message);
      }
    });
  2. 设置setFollowParentWindowLayoutEnabled接口为true时,无法通过滑动手势PanGesture单独拖动子窗口,但可以拖动主窗口使子窗口跟随移动。
    this.windowStage.createSubWindow('SubWindowTwo', async (err: BusinessError, data: window.Window | null) => {
      if (err.code !== 0 || !data) {
        console.error(`Failed to create the subwindow. Code: ${err.code}, message: ${err.message}`);
        return;
      }
      const win = data;
      await win.setFollowParentWindowLayoutEnabled(true);
      try {
        // 1. 设置大小
        await win.resize(this.uiContext.vp2px(100), this.uiContext.vp2px(100));
        await win.setUIContent('pages/SubWindowTwo');
        // 2. 设置透明背景,
        await win.setWindowBackgroundColor('#8050ff00');
        // 3. 显示窗口
        await win.showWindow();
        // 4. 设置不可触摸,防止子窗口阻挡主窗口的拖动的手势
        await win.setWindowTouchable(false);
        await win.setWindowFocusable(false);
      } catch (subErr) {
        console.error('子窗口操作失败: ' + subErr.message);
      }
    });

完整示例代码如下:

常见FAQ

Q:createSubWindow创建子窗口时,是否可以添加进入动画?

A:子窗口显示隐藏的方法不支持动画效果。