-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.py
More file actions
266 lines (233 loc) · 8.71 KB
/
buffer.py
File metadata and controls
266 lines (233 loc) · 8.71 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Andy Stewart
#
# Author: Andy Stewart <lazycat.manatee@gmail.com>
# Maintainer: Andy Stewart <lazycat.manatee@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from core.webengine import BrowserBuffer
from core.utils import *
import os
from PyQt6 import QtCore
from PyQt6.QtCore import QPointF, Qt
from PyQt6.QtGui import QMouseEvent
class AppBuffer(BrowserBuffer):
def __init__(self, buffer_id, url, arguments):
BrowserBuffer.__init__(self, buffer_id, url, arguments, False)
self.load_index_html(__file__)
def init_app(self):
self.init_file()
self.init_colors()
def init_colors(self):
# Get various colors from Emacs
(text_color,
function_color,
keyword_color,
builtin_color,
comment_color,
string_color,
negation_color,
variable_color,
type_color,
warning_color) = get_emacs_func_result(
"get-emacs-face-foregrounds",
["default",
"font-lock-function-name-face",
"font-lock-keyword-face",
"font-lock-builtin-face",
"font-lock-comment-face",
"font-lock-string-face",
"font-lock-negation-char-face",
"font-lock-variable-name-face",
"font-lock-type-face",
"font-lock-warning-face"])
# Get background color
background_color = self.theme_background_color
# Get user-defined rainbow color schemes
rainbow_colors = get_emacs_func_result("eaf-mind-elixir-get-rainbow-colors", [])
rainbow_colors_light = rainbow_colors[0] # Rainbow color string for light theme
rainbow_colors_dark = rainbow_colors[1] # Rainbow color string for dark theme
# Pass all colors to JavaScript
self.buffer_widget.eval_js_function(
'initColors',
background_color,
text_color,
{
"main": text_color,
"second": function_color,
"third": keyword_color,
"fourth": builtin_color,
"fifth": comment_color,
"sixth": string_color,
"seventh": negation_color,
"eighth": variable_color,
"ninth": type_color,
"tenth": warning_color
},
{
"light": rainbow_colors_light,
"dark": rainbow_colors_dark
})
@interactive
def update_theme(self):
super().update_theme()
# Get various colors from Emacs, same as in init_colors
(text_color,
function_color,
keyword_color,
builtin_color,
comment_color,
string_color,
negation_color,
variable_color,
type_color,
warning_color) = get_emacs_func_result(
"get-emacs-face-foregrounds",
["default",
"font-lock-function-name-face",
"font-lock-keyword-face",
"font-lock-builtin-face",
"font-lock-comment-face",
"font-lock-string-face",
"font-lock-negation-char-face",
"font-lock-variable-name-face",
"font-lock-type-face",
"font-lock-warning-face"])
# Get background color
background_color = self.theme_background_color
# Get user-defined rainbow color schemes
rainbow_colors = get_emacs_func_result("eaf-mind-elixir-get-rainbow-colors", [])
rainbow_colors_light = rainbow_colors[0] # Rainbow color string for light theme
rainbow_colors_dark = rainbow_colors[1] # Rainbow color string for dark theme
# Pass all colors to JavaScript
self.buffer_widget.eval_js_function(
'updateTheme',
background_color,
text_color,
{
"main": text_color,
"second": function_color,
"third": keyword_color,
"fourth": builtin_color,
"fifth": comment_color,
"sixth": string_color,
"seventh": negation_color,
"eighth": variable_color,
"ninth": type_color,
"tenth": warning_color
},
{
"light": rainbow_colors_light,
"dark": rainbow_colors_dark
})
@interactive()
def focus_root_node(self):
'''
Focus on the root node using Mind Elixir API and simulate mouse click.
'''
# 首先使用JavaScript API选中根节点
self.buffer_widget.eval_js_function("focusRootNode")
# 然后模拟鼠标点击以激活键盘导航
# 获取buffer_widget的位置和大小
rect = self.buffer_widget.geometry()
# 计算中心点位置
center_x = rect.x() + rect.width() // 2
center_y = rect.y() + rect.height() // 2
# 创建点击位置
click_pos = QPointF(center_x, center_y)
# 创建按下事件
press_event = QMouseEvent(
QMouseEvent.Type.MouseButtonPress,
click_pos,
Qt.MouseButton.LeftButton,
Qt.MouseButton.LeftButton,
Qt.KeyboardModifier.NoModifier
)
# 发送按下事件
for widget in self.get_key_event_widgets():
post_event(widget, press_event)
# 创建释放事件
release_event = QMouseEvent(
QMouseEvent.Type.MouseButtonRelease,
click_pos,
Qt.MouseButton.LeftButton,
Qt.MouseButton.LeftButton,
Qt.KeyboardModifier.NoModifier
)
# 发送释放事件
for widget in self.get_key_event_widgets():
post_event(widget, release_event)
def init_file(self):
self.url = os.path.expanduser(self.url)
if os.path.exists(self.url):
with open(self.url, "r") as f:
# 读取.eme文件内容
data = f.read()
self.buffer_widget.eval_js_function("openFile", string_to_base64(data))
else:
# 如果是新文件,初始化一个空的思维导图
self.buffer_widget.eval_js_function("initRootNode")
@QtCore.pyqtSlot(str)
def _save_file(self, data):
if data is None:
message_to_emacs("Error: Could not get mind map data")
return
with open(self.url, "w") as f:
f.write(data)
@interactive(insert_or_do=True)
def save_file(self):
'''
Save mind map to .eme file.
'''
# 获取mind map数据
data = self.buffer_widget.execute_js("saveFile();")
self._save_file(data)
message_to_emacs("Save file: " + self.url)
@interactive()
def paste_to_node_topic(self):
'''
Paste Emacs clipboard content to current node topic.
'''
# 获取Emacs剪贴板内容
clipboard_text = self.get_clipboard_text()
if clipboard_text:
# 调用JavaScript方法设置节点标题
self.buffer_widget.eval_js_function("setNodeTopic", clipboard_text)
message_to_emacs(f"Pasted: {clipboard_text}")
else:
message_to_emacs("Clipboard is empty")
@interactive()
def copy_node_topic(self):
'''
Copy current node topic to Emacs clipboard.
'''
# 获取当前节点标题
node_topic = self.buffer_widget.execute_js("getNodeTopic();")
# 添加调试信息
current_node = self.buffer_widget.execute_js("this.mindElixir && this.mindElixir.currentNode ? 'yes' : 'no';")
message_to_emacs(f"Current node exists: {current_node}")
if node_topic:
# 复制到Emacs剪贴板
eval_in_emacs('kill-new', [node_topic])
message_to_emacs(f"Copied: {node_topic}")
else:
message_to_emacs("No node selected")
@interactive()
def debug_node_info(self):
'''
Debug node information.
'''
debug_info = self.buffer_widget.execute_js("debugNodeInfo();")
message_to_emacs(f"Debug info: {debug_info}")