使用在线图标,以及增加点击菜单后的图标切换效果
This commit is contained in:
parent
dc3da3af34
commit
bd1b3a9612
@ -82,11 +82,23 @@ body {
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
padding: 12px 20px;
|
padding: 12px 20px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
transition: background-color 0.3s;
|
transition: all 0.3s ease;
|
||||||
|
border-left: 3px solid transparent;
|
||||||
}
|
}
|
||||||
|
|
||||||
.menu-item:hover {
|
.menu-item:hover {
|
||||||
background-color: #f5f5f5;
|
background-color: #f5f5f5;
|
||||||
|
border-left-color: #3498db;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item.active {
|
||||||
|
background-color: #e3f2fd;
|
||||||
|
border-left-color: #2196f3;
|
||||||
|
color: #1976d2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item.active .menu-icon {
|
||||||
|
filter: brightness(0.8);
|
||||||
}
|
}
|
||||||
|
|
||||||
.menu-icon {
|
.menu-icon {
|
||||||
|
48
src/index.ts
48
src/index.ts
@ -1,13 +1,25 @@
|
|||||||
import { WebviewTag } from "electron";
|
import { WebviewTag } from "electron";
|
||||||
|
|
||||||
// 菜单项接口
|
// 菜单项
|
||||||
interface MenuItem {
|
interface MenuItem {
|
||||||
ShowName: string;
|
ShowName: string;
|
||||||
Url: string;
|
Url: string;
|
||||||
Icon: string;
|
IconConfig: IconConfig;
|
||||||
Children: MenuItem[] | null;
|
Children: MenuItem[] | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 图标配置
|
||||||
|
interface IconConfig {
|
||||||
|
_1x: {
|
||||||
|
Default: string;
|
||||||
|
Selected: string;
|
||||||
|
};
|
||||||
|
_2x: {
|
||||||
|
Default: string;
|
||||||
|
Selected: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// API响应接口
|
// API响应接口
|
||||||
interface ApiResponse<T> {
|
interface ApiResponse<T> {
|
||||||
status: number;
|
status: number;
|
||||||
@ -57,13 +69,12 @@ async function getMenuList(): Promise<MenuItem[]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 创建菜单项
|
// 创建菜单项
|
||||||
function createMenuItem(item: MenuItem): HTMLLIElement {
|
function createMenuItem(item: MenuItem, menuList: MenuItem[]): HTMLLIElement {
|
||||||
const li = document.createElement('li');
|
const li = document.createElement('li');
|
||||||
li.className = 'menu-item';
|
li.className = 'menu-item';
|
||||||
|
|
||||||
const icon = document.createElement('img');
|
const icon = document.createElement('img');
|
||||||
//icon.src = `./assets/${item.Icon}`;
|
icon.src = item.IconConfig._1x.Default;
|
||||||
icon.src = `./assets/list.png`;
|
|
||||||
icon.alt = item.ShowName;
|
icon.alt = item.ShowName;
|
||||||
icon.className = 'menu-icon';
|
icon.className = 'menu-icon';
|
||||||
|
|
||||||
@ -75,17 +86,24 @@ function createMenuItem(item: MenuItem): HTMLLIElement {
|
|||||||
|
|
||||||
if (item.Url) {
|
if (item.Url) {
|
||||||
li.addEventListener('click', async () => {
|
li.addEventListener('click', async () => {
|
||||||
//(document.querySelector("webview") as WebviewElement).src = "https://www.baidu.com";
|
// 移除其他菜单项的选中状态和图标
|
||||||
//(document.querySelector("webview") as WebviewTag).src = item.Url;
|
document.querySelectorAll('.menu-item').forEach(menuItem => {
|
||||||
|
menuItem.classList.remove('active');
|
||||||
|
const menuIcon = menuItem.querySelector('.menu-icon') as HTMLImageElement;
|
||||||
|
if (menuIcon) {
|
||||||
|
const menuItemData = menuList.find((m: MenuItem) => m.ShowName === menuItem.querySelector('span')?.textContent);
|
||||||
|
if (menuItemData) {
|
||||||
|
menuIcon.src = menuItemData.IconConfig._1x.Default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 添加当前菜单项的选中状态和图标
|
||||||
|
li.classList.add('active');
|
||||||
|
icon.src = item.IconConfig._1x.Selected;
|
||||||
|
|
||||||
// 需要明确url是否指定http协议或https协议,如果没有指定,则默认使用http协议。因为没有指定协议会导致webview无法加载。
|
|
||||||
const url: string = item.Url.startsWith("http") ? item.Url : `http://${item.Url}`;
|
const url: string = item.Url.startsWith("http") ? item.Url : `http://${item.Url}`;
|
||||||
|
|
||||||
/**
|
|
||||||
* 无法访问的错误有很多种,但总的来说可以分为两大类:
|
|
||||||
* 1. 网络错误:DNS解析错误、连接超时、SSL证书错误等。
|
|
||||||
* 2. 服务器错误:服务器拒绝连接、服务器无响应、404、500等。
|
|
||||||
*/
|
|
||||||
const result = await window.electronAPI.checkUrl(url);
|
const result = await window.electronAPI.checkUrl(url);
|
||||||
if (result.ok && result.status >= 200 && result.status < 400) {
|
if (result.ok && result.status >= 200 && result.status < 400) {
|
||||||
console.log('✅ URL 可访问:', result.status);
|
console.log('✅ URL 可访问:', result.status);
|
||||||
@ -106,14 +124,14 @@ function renderMenu(menuList: MenuItem[]) {
|
|||||||
if (!menuContainer) return;
|
if (!menuContainer) return;
|
||||||
|
|
||||||
menuList.forEach(item => {
|
menuList.forEach(item => {
|
||||||
const menuItem = createMenuItem(item);
|
const menuItem = createMenuItem(item, menuList);
|
||||||
menuContainer.appendChild(menuItem);
|
menuContainer.appendChild(menuItem);
|
||||||
|
|
||||||
if (item.Children) {
|
if (item.Children) {
|
||||||
const subMenu = document.createElement('ul');
|
const subMenu = document.createElement('ul');
|
||||||
subMenu.className = 'submenu';
|
subMenu.className = 'submenu';
|
||||||
item.Children.forEach(child => {
|
item.Children.forEach(child => {
|
||||||
const childItem = createMenuItem(child);
|
const childItem = createMenuItem(child, menuList);
|
||||||
subMenu.appendChild(childItem);
|
subMenu.appendChild(childItem);
|
||||||
});
|
});
|
||||||
menuContainer.appendChild(subMenu);
|
menuContainer.appendChild(subMenu);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user