|
|
| from PySide6.QtWidgets import QWidget, QLabel, QVBoxLayout
|
| from PySide6.QtCore import Qt
|
| import win32gui
|
|
|
| class CandidateWindow(QWidget):
|
| def __init__(self):
|
| super().__init__()
|
|
|
|
|
| self.setWindowFlags(
|
| Qt.Tool |
|
| Qt.FramelessWindowHint |
|
| Qt.WindowStaysOnTopHint |
|
| Qt.WindowDoesNotAcceptFocus
|
| )
|
| self.setAttribute(Qt.WA_TranslucentBackground)
|
|
|
| self.layout = QVBoxLayout(self)
|
| self.layout.setContentsMargins(0, 0, 0, 0)
|
|
|
| self.label = QLabel("")
|
| self.label.setStyleSheet("""
|
| QLabel {
|
| background-color: rgba(40, 44, 52, 240);
|
| color: #abb2bf;
|
| font-family: "Microsoft YaHei UI";
|
| font-size: 18px;
|
| padding: 8px 15px;
|
| border: 1px solid #61afef;
|
| border-radius: 6px;
|
| }
|
| """)
|
| self.layout.addWidget(self.label)
|
|
|
| def get_caret_pos(self):
|
| """尝试获取光标位置"""
|
| try:
|
| gui_info = win32gui.GetGUIThreadInfo(0)
|
| hwnd = gui_info['hwndFocus']
|
| rect = gui_info['rcCaret']
|
|
|
| p = win32gui.ClientToScreen(hwnd, (rect[0], rect[1]))
|
| return p[0], p[1]
|
| except:
|
| return None
|
|
|
| def update_candidates(self, candidates):
|
| if not candidates:
|
| self.hide()
|
| return
|
|
|
|
|
| items = [f"<span style='color:#61afef;'>{i+1}.</span>{c}" for i, c in enumerate(candidates)]
|
| self.label.setText(f"<html>{' '.join(items)}</html>")
|
| self.adjustSize()
|
|
|
| pos = self.get_caret_pos()
|
| if pos:
|
| self.move(pos[0], pos[1] + 28)
|
| else:
|
|
|
| pass
|
|
|
| self.show()
|
| self.raise_() |