-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
166 lines (143 loc) · 4.6 KB
/
Copy pathrenderer.js
File metadata and controls
166 lines (143 loc) · 4.6 KB
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
// --- 操作するHTML要素を取得 ---
const tabsContainer = document.getElementById('tabs');
const addBtn = document.getElementById('add-btn');
const tabBar = document.getElementById('tab-bar');
const serviceSelect = document.getElementById('service-select');
const themeBtn = document.getElementById('theme-btn');
// --- モーダル関連の要素を取得 ---
const modal = document.getElementById('modal');
const accountNameInput = document.getElementById('account-name-input');
const okBtn = document.getElementById('ok-btn');
const cancelBtn = document.getElementById('cancel-btn');
// 起動時にmainプロセスへ準備完了を通知
window.api.rendererReady();
// BrowserViewの表示領域をタブバーの実寸に追従させる
function reportUiMetrics() {
if (!tabBar) return;
const rect = tabBar.getBoundingClientRect();
const height = Math.ceil(rect.height);
window.api.setUiTopInset(height);
}
reportUiMetrics();
// テーマ切替(system -> dark -> light -> system)
const THEME_KEY = 'gdesk.theme';
function applyTheme(theme) {
const root = document.documentElement;
if (!theme || theme === 'system') {
root.removeAttribute('data-theme');
return;
}
if (theme === 'dark' || theme === 'light') {
root.setAttribute('data-theme', theme);
}
}
function getSavedTheme() {
try {
return localStorage.getItem(THEME_KEY) || 'system';
} catch {
return 'system';
}
}
function saveTheme(theme) {
try {
localStorage.setItem(THEME_KEY, theme);
} catch {
// ignore
}
}
function cycleTheme() {
const current = getSavedTheme();
const next = current === 'system' ? 'dark' : current === 'dark' ? 'light' : 'system';
saveTheme(next);
applyTheme(next);
}
applyTheme(getSavedTheme());
if (themeBtn) {
themeBtn.addEventListener('click', () => {
cycleTheme();
reportUiMetrics();
});
}
if (tabBar && 'ResizeObserver' in window) {
const ro = new ResizeObserver(() => reportUiMetrics());
ro.observe(tabBar);
}
window.addEventListener('resize', () => {
reportUiMetrics();
});
// サービス切替(アクティブなアカウントに対して適用)
if (serviceSelect) {
serviceSelect.addEventListener('change', () => {
const key = serviceSelect.value;
window.api.switchService(key);
});
}
// 「+」ボタンがクリックされたらモーダルを表示
addBtn.addEventListener('click', () => {
window.api.hideView();
modal.classList.remove('hidden');
accountNameInput.focus();
reportUiMetrics();
});
// モーダルの「OK」ボタン
okBtn.addEventListener('click', () => {
const accountName = accountNameInput.value;
if (accountName) {
window.api.addAccount(accountName);
modal.classList.add('hidden');
accountNameInput.value = '';
} else {
window.api.showView();
}
reportUiMetrics();
});
// モーダルの「キャンセル」ボタン
cancelBtn.addEventListener('click', () => {
modal.classList.add('hidden');
accountNameInput.value = '';
window.api.showView();
reportUiMetrics();
});
// main.jsからアカウント追加・復元の通知を受け取る
window.api.onAccountAdded(({ id, name }) => {
// 既に同じタブがあれば作らない
if (document.querySelector(`.tab[data-id="${id}"]`)) {
return;
}
const tab = document.createElement('div');
tab.className = 'tab';
tab.textContent = name;
tab.dataset.id = id;
tabsContainer.appendChild(tab);
tab.addEventListener('click', () => {
window.api.switchAccount(id);
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
tab.classList.add('active');
reportUiMetrics();
});
});
// 新規アカウント追加完了後、そのタブをアクティブにする
window.api.onAccountAddedComplete(({ id }) => {
const newTab = document.querySelector(`.tab[data-id="${id}"]`);
if (newTab) {
newTab.click();
}
window.api.showView();
reportUiMetrics();
});
// 復元時にアクティブタブを設定する
window.api.onSetActiveTab((id) => {
const tabToActivate = document.querySelector(`.tab[data-id="${id}"]`);
if (tabToActivate) {
tabToActivate.click();
}
reportUiMetrics();
});
// main側が「今アクティブなアカウントのサービス」を通知してくる
window.api.onActiveService(({ serviceKey }) => {
if (!serviceSelect) return;
if (!serviceKey) return;
if (serviceSelect.value !== serviceKey) {
serviceSelect.value = serviceKey;
}
});