Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[上游问题解决方案] 当多台显示器的缩放设置不同时,窗口无法在显示器之间正确切换 #2343

Open
5 of 6 tasks
lazhenyi opened this issue Jan 5, 2025 · 1 comment
Labels
C: Backend T: Bug Something isn't working

Comments

@lazhenyi
Copy link

lazhenyi commented Jan 5, 2025

复现步骤 / Step to reproduce

当程序在不同显示器之间切换的时候,会发生大小持续变化

预期行为 / Expected behavior

不变

实际行为 / Actual behavior

应用日志 / App logs

No response

备注 / Addition details

pub fn move_window_to_other_monitor(window: Window, i: usize) -> tauri::Result<()> {
    let monitors = match get_available_monitors(window.clone()) {
        Ok(monitors) => monitors,
        Err(e) => {
            eprintln!("无法获取可用显示器列表: {}", e);
            // 提供默认配置或提示用户检查系统设置
            return Ok(());
        }
    };
    let (_index, monitor) = monitors.get(i).ok_or(tauri::Error::CreateWindow)?;

    let pos = monitor.position();

    window.set_position(Position::Physical(tauri::PhysicalPosition {
        x: pos.x,
        y: pos.y,
    }))?;

    Ok(())
}

fn resize_window(window: &Window, screen_share: f64) -> tauri::Result<()> {
    let monitor = window.current_monitor().unwrap().unwrap();
    let monitor_size = monitor.size();
    let scaled_size: PhysicalSize<u32> = PhysicalSize {
        width: ((monitor_size.width) as f64 * screen_share).round() as u32,
        height: ((monitor_size.height) as f64 * screen_share).round() as u32,
    };
    window.set_size(Size::Physical(scaled_size))?;
    Ok(())
}

#[cfg(windows)]
pub fn center_window(window: &Window) -> tauri::Result<()> {
    use winapi::shared::windef::RECT;
    use winapi::um::winuser::{SystemParametersInfoW, SPI_GETWORKAREA};

    // 获取工作区
    let mut work_area = RECT {
        left: 0,
        top: 0,
        right: 0,
        bottom: 0,
    };
    unsafe {
        SystemParametersInfoW(SPI_GETWORKAREA, 0, &mut work_area as *mut _ as *mut _, 0);
    }

    let work_area_width = (work_area.right - work_area.left) as u32;
    let work_area_height = (work_area.bottom - work_area.top) as u32;
    let work_area_x = work_area.left as i32;
    let work_area_y = work_area.top as i32;

    let window_size = window.outer_size().unwrap();

    let new_x = work_area_x + (work_area_width as i32 - window_size.width as i32) / 2;
    let new_y = work_area_y + (work_area_height as i32 - window_size.height as i32) / 2;

    window.set_position(Position::Physical(tauri::PhysicalPosition {
        x: new_x,
        y: new_y,
    }))?;
    Ok(())
}

#[cfg(target_os = "macos")]
pub fn center_window(window: &Window) -> tauri::Result<()> {
    window.center();
    Ok(())
}
#[tauri::command]
pub fn get_available_monitors(
    window: tauri::Window,
) -> tauri::Result<Vec<(usize, tauri::Monitor)>> {
    let mut monitors = window.available_monitors()?;
    monitors.sort_by(|a, b| {
        let a_pos = a.position();
        let b_pos = b.position();
        let a_size = a.size();
        let b_size = b.size();
        let a_val =
            (a_pos.y + 200) * 10 / a_size.height as i32 + (a_pos.x + 300) / a_size.width as i32;
        let b_val =
            (b_pos.y + 200) * 10 / b_size.height as i32 + (b_pos.x + 300) / b_size.width as i32;

        a_val.cmp(&b_val)
    });

    monitors
        .iter()
        .enumerate()
        .map(|(i, m)| Ok((i, m.clone())))
        .collect()
}

环境信息 / Environment information

1.6.1

自查步骤 / Verify steps

  • 如果您有足够的时间和能力,并愿意为此提交 PR,请勾上此复选框 / Pull request is welcome. Check this if you want to start a pull request
  • 您已知悉如果没有提供正确的系统信息,以及日志,您的 Issue 会直接被关闭 / You have known that if you don't provide correct system information and logs, your issue will be closed directly
  • 您已仔细查看并知情 Q&AFAQ 中的内容 / You have read and understood the contents of Q&A and FAQ
  • 您已搜索过 Issue Tracker,没有找到类似内容 / I have searched on Issue Tracker, No duplicate or related open issue has been found
  • 您确保这个 Issue 只提及一个问题。如果您有多个问题报告,烦请发起多个 Issue / Ensure there is only one bug report in this issue. Please make multiply issue for multiply bugs
  • 您确保已使用最新 Pre-release 版本测试,并且该问题在最新 Pre-release 版本中并未解决 / This bug have not solved in latest Pre-release version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: Backend T: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants