File size: 9,804 Bytes
f1b4581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
class UIManager {
    constructor() {
        // 延迟初始化,确保DOM已加载
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', () => this.init());
        } else {
            // 如果DOM已经加载完成,则立即初始化
            this.init();
        }
    }
    
    init() {
        console.log('初始化UI管理器...');
        // UI elements
        this.settingsPanel = document.getElementById('settingsPanel');
        this.settingsToggle = document.getElementById('settingsToggle');
        this.closeSettings = document.getElementById('closeSettings');
        this.themeToggle = document.getElementById('themeToggle');
        this.toastContainer = document.getElementById('toastContainer');
        
        // 验证关键元素是否存在
        if (!this.themeToggle) {
            console.error('主题切换按钮未找到!');
            return;
        }
        
        if (!this.toastContainer) {
            console.error('Toast容器未找到!');
            // 尝试创建Toast容器
            this.toastContainer = this.createToastContainer();
        }
        
        // Check for preferred color scheme
        this.checkPreferredColorScheme();
        
        // Initialize event listeners
        this.setupEventListeners();
        
        console.log('UI管理器初始化完成');
    }
    
    createToastContainer() {
        console.log('创建Toast容器');
        const container = document.createElement('div');
        container.id = 'toastContainer';
        container.className = 'toast-container';
        document.body.appendChild(container);
        return container;
    }
    
    checkPreferredColorScheme() {
        const savedTheme = localStorage.getItem('theme');
        const prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
        
        if (savedTheme) {
            this.setTheme(savedTheme === 'dark');
        } else {
            this.setTheme(prefersDark.matches);
        }
        
        prefersDark.addEventListener('change', (e) => this.setTheme(e.matches));
    }
    
    setTheme(isDark) {
        try {
            document.documentElement.setAttribute('data-theme', isDark ? 'dark' : 'light');
            if (this.themeToggle) {
                this.themeToggle.innerHTML = `<i class="fas fa-${isDark ? 'sun' : 'moon'}"></i>`;
            }
            localStorage.setItem('theme', isDark ? 'dark' : 'light');
            console.log(`主题已切换为: ${isDark ? '深色' : '浅色'}`);
        } catch (error) {
            console.error('设置主题时出错:', error);
        }
    }
    
    /**
     * 显示一个Toast消息
     * @param {string} message 显示的消息内容
     * @param {string} type 消息类型,可以是'success', 'error', 'info', 'warning'
     * @param {number} displayTime 显示的时间(毫秒),如果为-1则持续显示直到手动关闭
     * @returns {HTMLElement} 返回创建的Toast元素,可用于后续移除
     */
    showToast(message, type = 'success', displayTime) {
        try {
            if (!message) {
                console.warn('尝试显示空消息');
                message = '';
            }
            
            if (!this.toastContainer) {
                console.error('Toast容器不存在,正在创建新容器');
                this.toastContainer = this.createToastContainer();
                if (!this.toastContainer) {
                    console.error('无法创建Toast容器,放弃显示消息');
                    return null;
                }
            }
            
            // 检查是否已经存在相同内容的提示
            try {
                const existingToasts = this.toastContainer.querySelectorAll('.toast');
                for (const existingToast of existingToasts) {
                    try {
                        const spanElement = existingToast.querySelector('span');
                        if (spanElement && spanElement.textContent === message) {
                            // 已经存在相同的提示,不再创建新的
                            return existingToast;
                        }
                    } catch (e) {
                        console.warn('检查现有toast时出错:', e);
                        // 继续检查其他toast元素
                    }
                }
            } catch (e) {
                console.warn('查询现有toast时出错:', e);
                // 继续创建新的toast
            }
            
            const toast = document.createElement('div');
            toast.className = `toast ${type}`;
            
            // 根据类型设置图标
            let icon = 'check-circle';
            if (type === 'error') icon = 'exclamation-circle';
            else if (type === 'warning') icon = 'exclamation-triangle';
            else if (type === 'info') icon = 'info-circle';
            
            toast.innerHTML = `
                <i class="fas fa-${icon}"></i>
                <span>${message}</span>
            `;
            
            // 如果是持续显示的Toast,添加关闭按钮
            if (displayTime === -1) {
                const closeButton = document.createElement('button');
                closeButton.className = 'toast-close';
                closeButton.innerHTML = '<i class="fas fa-times"></i>';
                closeButton.addEventListener('click', (e) => {
                    this.hideToast(toast);
                });
                toast.appendChild(closeButton);
                toast.classList.add('persistent');
            }
            
            this.toastContainer.appendChild(toast);
            
            // 为不同类型的提示设置不同的显示时间
            if (displayTime !== -1) {
                // 如果没有指定时间,则根据消息类型和内容长度设置默认时间
                if (displayTime === undefined) {
                    displayTime = message === '截图成功' ? 1500 : 
                                 type === 'error' ? 5000 : 
                                 message.length > 50 ? 4000 : 3000;
                }
                
                setTimeout(() => {
                    this.hideToast(toast);
                }, displayTime);
            }
            
            return toast;
        } catch (error) {
            console.error('显示Toast消息时出错:', error);
            return null;
        }
    }
    
    /**
     * 隐藏一个Toast消息
     * @param {HTMLElement} toast 要隐藏的Toast元素
     */
    hideToast(toast) {
        if (!toast || !toast.parentNode) return;
        
        toast.style.opacity = '0';
        setTimeout(() => {
            if (toast.parentNode) {
                toast.remove();
            }
        }, 300);
    }
    
    closeAllPanels() {
        if (this.settingsPanel) {
            this.settingsPanel.classList.remove('active');
        }
    }
    
    hideSettingsPanel() {
        if (this.settingsPanel) {
            this.settingsPanel.classList.remove('active');
        }
    }
    
    toggleSettingsPanel() {
        if (this.settingsPanel) {
            this.settingsPanel.classList.toggle('active');
        }
    }
    
    closeSettingsPanel() {
        if (this.settingsPanel) {
            this.settingsPanel.classList.remove('active');
        }
    }
    
    // 检查点击事件,如果点击了设置面板外部,则关闭设置面板
    checkClickOutsideSettings(e) {
        if (this.settingsPanel &&
            !this.settingsPanel.contains(e.target) &&
            !e.target.closest('#settingsToggle')) {
            this.settingsPanel.classList.remove('active');
        }
    }
    
    setupEventListeners() {
        // 确保所有元素都存在
        if (!this.settingsToggle || !this.closeSettings || !this.themeToggle) {
            console.error('无法设置事件监听器:一些UI元素未找到');
            return;
        }
        
        // Settings panel
        this.settingsToggle.addEventListener('click', () => {
            this.closeAllPanels();
            this.settingsPanel.classList.toggle('active');
        });
        
        this.closeSettings.addEventListener('click', () => {
            this.settingsPanel.classList.remove('active');
        });
        
        // Theme toggle
        this.themeToggle.addEventListener('click', () => {
            try {
                const currentTheme = document.documentElement.getAttribute('data-theme');
                console.log('当前主题:', currentTheme);
                this.setTheme(currentTheme !== 'dark');
            } catch (error) {
                console.error('切换主题时出错:', error);
            }
        });
        
        // Close panels when clicking outside
        document.addEventListener('click', (e) => {
            this.checkClickOutsideSettings(e);
        });
    }
}

// 创建全局实例
window.UIManager = UIManager;

// 确保在DOM加载完毕后才创建UIManager实例
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', () => {
        window.uiManager = new UIManager();
    });
} else {
    window.uiManager = new UIManager();
}

// 导出全局辅助函数
window.showToast = (message, type) => {
    if (window.uiManager) {
        return window.uiManager.showToast(message, type);
    } else {
        console.error('UI管理器未初始化,无法显示Toast');
        return null;
    }
};

window.closeAllPanels = () => {
    if (window.uiManager) {
        window.uiManager.closeAllPanels();
    } else {
        console.error('UI管理器未初始化,无法关闭面板');
    }
};