bug(macos): fix bug on macos

This commit is contained in:
AkiChase 2024-04-15 14:00:25 +08:00
parent b9814a9aed
commit 32bf097f27
6 changed files with 95 additions and 48 deletions

View File

@ -10,21 +10,22 @@
"tauri": "tauri" "tauri": "tauri"
}, },
"dependencies": { "dependencies": {
"@tauri-apps/api": ">=2.0.0-beta.0",
"@tauri-apps/plugin-os": "2.0.0-beta.2",
"@tauri-apps/plugin-shell": ">=2.0.0-beta.0",
"pinia": "^2.1.7", "pinia": "^2.1.7",
"vue": "^3.3.4", "vue": "^3.3.4",
"vue-router": "4", "vue-router": "4"
"@tauri-apps/api": ">=2.0.0-beta.0",
"@tauri-apps/plugin-shell": ">=2.0.0-beta.0"
}, },
"devDependencies": { "devDependencies": {
"@vitejs/plugin-vue": "^5.0.4",
"typescript": "^5.0.2",
"vite": "^5.0.0",
"vue-tsc": "^1.8.5",
"@tauri-apps/cli": ">=2.0.0-beta.0", "@tauri-apps/cli": ">=2.0.0-beta.0",
"@vicons/fluent": "^0.12.0", "@vicons/fluent": "^0.12.0",
"@vicons/ionicons5": "^0.12.0", "@vicons/ionicons5": "^0.12.0",
"@vitejs/plugin-vue": "^5.0.4",
"naive-ui": "^2.38.1", "naive-ui": "^2.38.1",
"sass": "^1.71.1" "sass": "^1.71.1",
"typescript": "^5.0.2",
"vite": "^5.0.0",
"vue-tsc": "^1.8.5"
} }
} }

View File

@ -12,7 +12,7 @@ tauri-build = { version = "2.0.0-beta", features = [] }
[dependencies] [dependencies]
tauri = { version = "2.0.0-beta", features = ["macos-private-api"] } tauri = { version = "2.0.0-beta", features = ["macos-private-api"] }
tauri-plugin-shell = "2.0.0-beta" tauri-plugin-os = "2.0.0-beta"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
anyhow = "1.0" anyhow = "1.0"

View File

@ -13,6 +13,8 @@
"window:allow-close", "window:allow-close",
"window:allow-is-maximizable", "window:allow-is-maximizable",
"window:allow-start-dragging", "window:allow-start-dragging",
"window:allow-unmaximize" "window:allow-unmaximize",
"os:default",
"os:allow-platform"
] ]
} }

View File

@ -1,8 +1,13 @@
use crate::resource::{ResHelper, ResourceName}; use crate::resource::{ResHelper, ResourceName};
use std::{ use std::{
io::BufRead, os::windows::process::CommandExt, path::PathBuf, process::{Child, Command, Stdio} io::BufRead,
path::PathBuf,
process::{Child, Command, Stdio},
}; };
#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
use anyhow::{Context, Ok, Result}; use anyhow::{Context, Ok, Result};
#[derive(Clone, Debug, serde::Serialize)] #[derive(Clone, Debug, serde::Serialize)]
@ -87,12 +92,14 @@ pub struct Adb;
/// But some output of command won't be output, like adb service startup information. /// But some output of command won't be output, like adb service startup information.
impl Adb { impl Adb {
fn cmd_base(res_dir: &PathBuf) -> Command { fn cmd_base(res_dir: &PathBuf) -> Command {
let mut cmd = Command::new(ResHelper::get_file_path(res_dir, ResourceName::Adb)); #[cfg(target_os = "windows")]{
let mut cmd = Command::new(ResHelper::get_file_path(res_dir, ResourceName::Adb));
#[cfg(target_os = "windows")] cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW
cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW cmd
}
cmd #[cfg(not(target_os = "windows"))]{
Command::new(ResHelper::get_file_path(res_dir, ResourceName::Adb))
}
} }
/// execute "adb devices" and return devices list /// execute "adb devices" and return devices list

View File

@ -132,33 +132,42 @@ async fn main() {
.unwrap(); .unwrap();
let main_window = app.get_webview_window("main").unwrap(); let main_window = app.get_webview_window("main").unwrap();
let scale_factor = main_window.scale_factor().unwrap();
main_window
.set_size(tauri::Size::Physical(tauri::PhysicalSize {
width: 1350,
height: 750,
}))
.unwrap();
let zoomfactor = 1.0 / scale_factor; #[cfg(windows)]
main_window {
.with_webview(move |webview| { let scale_factor = main_window.scale_factor().unwrap();
#[cfg(target_os = "windows")] main_window
unsafe { .set_size(tauri::Size::Physical(tauri::PhysicalSize {
// see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html width: 1350,
webview.controller().SetZoomFactor(zoomfactor).unwrap(); height: 750,
} }))
.unwrap();
// #[cfg(target_os = "macos")] main_window
// unsafe { .with_webview(move |webview| {
// let () = msg_send![webview.inner(), setPageZoom: 4.]; unsafe {
// } // see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html
}) webview
.unwrap(); .controller()
.SetZoomFactor(1.0 / scale_factor)
.unwrap();
}
})
.unwrap();
}
#[cfg(target_os = "macos")]
{
main_window
.set_size(tauri::Size::Logical(tauri::LogicalSize {
width: 1350.,
height: 750.,
}))
.unwrap();
}
Ok(()) Ok(())
}) })
.plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_os::init())
.invoke_handler(tauri::generate_handler![ .invoke_handler(tauri::generate_handler![
adb_devices, adb_devices,
get_screen_size, get_screen_size,

View File

@ -19,24 +19,42 @@ import {
PhysicalSize, PhysicalSize,
getCurrent, getCurrent,
} from "@tauri-apps/api/window"; } from "@tauri-apps/api/window";
import { platform } from "@tauri-apps/plugin-os";
import { SettingsOutline } from "@vicons/ionicons5"; import { SettingsOutline } from "@vicons/ionicons5";
import { UnlistenFn } from "@tauri-apps/api/event"; import { UnlistenFn } from "@tauri-apps/api/event";
let unlistenResize: UnlistenFn = () => {}; let unlistenResize: UnlistenFn = () => {};
let unlistenMove: UnlistenFn = () => {}; let unlistenMove: UnlistenFn = () => {};
let factor = 1;
let platformName = "";
// macos: use logical position and size to refresh the area model
// others: use pyhsical position and size to refresh the area model
async function refreshAreaModel(size?: PhysicalSize, pos?: PhysicalPosition) { async function refreshAreaModel(size?: PhysicalSize, pos?: PhysicalPosition) {
// header size and sidebar size // header size and sidebar size
const mt = 30; const mt = 30;
const ml = 70; const ml = 70;
if (size !== undefined) { if (platformName === "macos") {
areaModel.value.sizeW = Math.floor(size.width - ml); // use logical position and size
areaModel.value.sizeH = Math.floor(size.height - mt); if (size !== undefined) {
} areaModel.value.sizeW = Math.floor((size.width - ml) / factor);
if (pos !== undefined) { areaModel.value.sizeH = Math.floor((size.height - mt) / factor);
areaModel.value.posX = Math.floor(pos.x + ml); }
areaModel.value.posY = Math.floor(pos.y + mt); if (pos !== undefined) {
areaModel.value.posX = Math.floor((pos.x + ml) / factor);
areaModel.value.posY = Math.floor((pos.y + mt) / factor);
}
} else {
if (size !== undefined) {
areaModel.value.sizeW = Math.floor(size.width - ml);
areaModel.value.sizeH = Math.floor(size.height - mt);
}
if (pos !== undefined) {
areaModel.value.posX = Math.floor(pos.x + ml);
areaModel.value.posY = Math.floor(pos.y + mt);
}
} }
} }
@ -110,12 +128,22 @@ async function adjustMaskArea() {
areaModel.value.sizeW + ml, areaModel.value.sizeW + ml,
areaModel.value.sizeH + mt areaModel.value.sizeH + mt
); );
await appWindow.setPosition(pos);
await appWindow.setSize(size); if (platformName === "macos") {
// use logical position and size
await appWindow.setPosition(pos.toLogical(factor));
await appWindow.setSize(size.toLogical(factor));
} else {
await appWindow.setPosition(pos);
await appWindow.setSize(size);
}
} }
onMounted(async () => { onMounted(async () => {
const appWindow = getCurrent(); const appWindow = getCurrent();
factor = await appWindow.scaleFactor();
platformName = await platform();
unlistenResize = await appWindow.onResized(({ payload: size }) => { unlistenResize = await appWindow.onResized(({ payload: size }) => {
refreshAreaModel(size, undefined); refreshAreaModel(size, undefined);
}); });