Skip to content

Commit

Permalink
✨ JSBridge分辨率适配
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed Aug 11, 2024
1 parent 82ba88a commit 885fa3b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
22 changes: 15 additions & 7 deletions src-tauri/src/client/menu.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! @file src/client/menu.rs
//! @desc 客户端菜单模块,负责操作米游社客户端菜单
//! @since Beta v0.5.0
//! @since Beta v0.5.2
use crate::client::utils;
use tauri::menu::{Menu, MenuBuilder, MenuEvent, MenuItemBuilder, Submenu, SubmenuBuilder};
use tauri::{AppHandle, LogicalSize, Manager, Size, Window, Wry};
use tauri_utils::config::WebviewUrl;
Expand All @@ -21,7 +22,7 @@ pub fn get_mhy_client_url(func: String) -> WebviewUrl {
.parse()
.unwrap();
}
return WebviewUrl::External(url_res);
WebviewUrl::External(url_res)
}

// 创建子菜单-工具
Expand All @@ -39,7 +40,7 @@ fn create_utils_menu(app: AppHandle) -> Submenu<Wry> {
.item(&rotate_window_submenu)
.build()
.expect("failed to create utils_menu");
return utils_menu;
utils_menu
}

// 创建米游社客户端菜单
Expand All @@ -48,13 +49,13 @@ pub fn create_mhy_menu(app: AppHandle) -> Menu<Wry> {
let cancel_top_menu = MenuItemBuilder::with_id("cancel_top", "取消置顶").build(&app).unwrap();
let open_post_menu = MenuItemBuilder::with_id("open_post", "打开帖子").build(&app).unwrap();
let utils_menu = create_utils_menu(app.clone());
return MenuBuilder::new(&app)
MenuBuilder::new(&app)
.item(&top_menu)
.item(&cancel_top_menu)
.item(&open_post_menu)
.item(&utils_menu)
.build()
.expect("failed to create mhy_menu");
.expect("failed to create mhy_menu")
}

// 菜单栏事件处理
Expand Down Expand Up @@ -181,10 +182,17 @@ fn handle_menu_rotate_window(app_handle: &Window) {
let window = window_get.unwrap();
// 获取窗口宽高比
let cur_size = window.inner_size().ok().unwrap();
let monitor = window.primary_monitor().ok().unwrap().unwrap();
if cur_size.width > cur_size.height {
window.set_size(Size::Logical(LogicalSize { width: 400.0, height: 800.0 })).unwrap();
let trans_size = utils::get_window_size2(monitor, 400.0, 800.0);
window
.set_size(Size::Logical(LogicalSize { width: trans_size.0, height: trans_size.1 }))
.unwrap();
} else {
window.set_size(Size::Logical(LogicalSize { width: 1280.0, height: 720.0 })).unwrap();
let trans_size = utils::get_window_size2(monitor, 1280.0, 720.0);
window
.set_size(Size::Logical(LogicalSize { width: trans_size.0, height: trans_size.1 }))
.unwrap();
}
window.center().unwrap();
window.set_focus().unwrap();
Expand Down
7 changes: 5 additions & 2 deletions src-tauri/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! @file src/client/mod.rs
//! @desc 客户端模块,负责操作米游社客户端
//! @since Beta v0.5.0
//! @since Beta v0.5.2
mod menu;
mod utils;

use tauri::{AppHandle, Manager, WebviewWindowBuilder};
use tauri_utils::config::WebviewUrl;

Expand All @@ -29,8 +31,9 @@ pub async fn create_mhy_client(handle: AppHandle, func: String, url: String) {
window_find.unwrap().destroy().unwrap();
return;
}
let trans_size = utils::get_window_size(handle.clone(), win_width, win_height);
WebviewWindowBuilder::new(&handle, "mhy_client", url_parse)
.inner_size(win_width, win_height)
.inner_size(trans_size.0, trans_size.1)
.title("米游社")
.center()
.user_agent(win_ua)
Expand Down
22 changes: 22 additions & 0 deletions src-tauri/src/client/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! @file src/client/utils.rs
//! @desc 结合屏幕分辨率获取窗口大小
//! @since Beta v0.5.2
use tauri::{AppHandle, Manager, Monitor};

pub fn get_window_size(app: AppHandle, width: f64, height: f64) -> (f64, f64) {
let mhy_window = app.get_webview_window("TeyvatGuide").unwrap();
let monitor = mhy_window.primary_monitor().unwrap().expect("failed to get primary monitor");
get_window_size2(monitor, width, height)
}

pub fn get_window_size2(monitor: Monitor, width: f64, height: f64) -> (f64, f64) {
let monitor_size = monitor.size();
let monitor_width = monitor_size.width as f64;
let monitor_height = monitor_size.height as f64;
let width_scale = monitor_width / 1920.0;
let height_scale = monitor_height / 1080.0;
let get_width = (width * width_scale).round();
let get_height = (height * height_scale).round();
(get_width, get_height)
}

0 comments on commit 885fa3b

Please sign in to comment.