Files
tauri-meeting/src/utils/tauri/windows-action.ts

104 lines
2.6 KiB
TypeScript
Raw Normal View History

import { WebviewWindow } from '@tauri-apps/api/webviewWindow';
import { type WindowLabel, windowLabel } from './windows-util';
import { invoke } from '@tauri-apps/api/core';
import { LogicalPosition, LogicalSize, currentMonitor } from '@tauri-apps/api/window';
import { ElMessage as Toast } from 'element-plus';
/**
*
*/
export async function createFloatListWindow(pageType: 'interaction' | 'tutorship', isTeacherMuted = false) {
try {
await invoke('create_float_list_window', { page_type: pageType, isTeacherMuted });
} catch (error: any) {
console.error('创建悬浮球窗口时出错:', error);
Toast.error('悬浮球创建失败');
}
}
/**
*
*/
export async function closeFloatListWindow() {
try {
await invoke('close_float_list_window');
} catch (error: any) {
console.error('关闭悬浮球窗口时出错:', error);
}
}
/**
*
*/
export async function createMainWindow() {
try {
await invoke('create_main_window');
} catch (error: any) {
console.error('创建主窗口时出错:', error);
Toast.error('创建主窗口失败');
}
}
/**
*
*/
export async function moveAndResizeWindow(x: number, y: number, width: number, height: number) {
const win = WebviewWindow.getCurrent();
await win.setSize(new LogicalSize(width, height));
// 获取当前显示器信息
const _currentMonitor = await currentMonitor();
const scaleFactor = await win.scaleFactor();
// 如果有当前显示器信息,则将相对坐标转换为绝对坐标
if (_currentMonitor) {
const monitorPos = _currentMonitor.position.toLogical(scaleFactor);
await win.setPosition(new LogicalPosition(x + monitorPos.x, y + monitorPos.y));
} else {
await win.setPosition(new LogicalPosition(x, y));
}
return Promise.resolve(true);
}
/**
*
*/
export async function closeCurrentWindow() {
const win = WebviewWindow.getCurrent();
if (win.label === windowLabel.main) {
await win.hide();
} else {
await win.close();
}
}
/**
* label
*/
export async function closeWindowByLabel(label: WindowLabel) {
try {
if (!label) {
return;
}
const win = await WebviewWindow.getByLabel(label);
if (!win) {
return;
}
if (win.label === windowLabel.main) {
await win.hide();
} else {
await win.close();
}
} catch (error) {
console.log('关闭错误或已被关闭====', error);
}
}
/**
*
*/
export async function minimizeCurrentWindow() {
const win = WebviewWindow.getCurrent();
await win.minimize();
}