Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #24

Merged
merged 11 commits into from
Sep 25, 2024
Merged

Dev #24

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions DesktopTools/app/msg_systray.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ def showMenu(self):
设计托盘的菜单
"""
self.menuMain = QMenu()
self.subMenu = QMenu()
self.showAction = QAction("测试休息提醒消息", self, triggered=self.showYouNeedRest)
self.quitAction = QAction("退出", self, triggered=self.quit)

self.subMenu.addAction(self.showAction)
self.action_test = QAction("测试休息提醒消息", self, triggered=self.showYouNeedRest)
self.subMenu = QMenu()
self.subMenu.setTitle("测试菜单")
self.subMenu.addAction(self.action_test)
self.menuMain.addMenu(
self.subMenu,
)

self.menuMain.addAction(self.quitAction)
self.subMenu.setTitle("测试菜单")
self.action_quit = QAction("退出", self, triggered=self.quit)
self.action_search_bar = QAction("搜索", self, triggered=self.show_search_bar)
self.menuMain.addAction(self.action_search_bar)
self.menuMain.addAction(self.action_quit)

self.setContextMenu(self.menuMain)

def initConnect(self):
Expand Down Expand Up @@ -86,7 +89,10 @@ def showYouNeedRest(self, msg="主动点击测试", level=0):
}
vlevel = dict_msg[level]["value"]
txt = dict_msg[level]["desc"]
self.showMessage(txt, msg, self.MessageIcon(vlevel))
self.showMessage(txt, msg or "", self.MessageIcon(vlevel))

def show_search_bar(self, msg="主动点击测试", level=0):
self.parent().open_search_bar()

def quit(self):
# TODO 完整的退出 不过注释掉几行也能退出,蛮奇怪,先这么吧.
Expand Down
13 changes: 11 additions & 2 deletions DesktopTools/feather_hotkey/clip_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import random
import re
import shutil
from zoneinfo import ZoneInfo

# from PySide6.QtCore import Qt
from PySide6.QtGui import QClipboard
Expand Down Expand Up @@ -87,7 +88,7 @@ def extract_float(string):
return numbers


def unixtime_to_datetime_str(unixtime_str):
def unixtime_to_datetime_str(unixtime_str, tz=ZoneInfo("Asia/Shanghai")):
list_float = extract_float(unixtime_str)
logger.debug(f"unixtime_str: {unixtime_str} - list_float: {list_float}")
unixtime = 0
Expand All @@ -100,7 +101,7 @@ def unixtime_to_datetime_str(unixtime_str):
unixtime = int(unixtime_str)
dt_str = ""
try:
dt = datetime.datetime.fromtimestamp(unixtime)
dt = datetime.datetime.fromtimestamp(unixtime, tz=tz)
dt_str = dt.strftime("%Y-%m-%d %H:%M:%S")
except Exception:
pass
Expand Down Expand Up @@ -132,6 +133,7 @@ def __init__(self, clipboard: QClipboard) -> None:
FuncClass(desc="连续空格替换为指定符号", func=self.replace_spaces),
FuncClass(desc="data转为JSON字符串", func=self.data2json),
FuncClass(desc="unixtime转datetime", func=self.unixtime_to_datetime_str),
FuncClass(desc="unixtime转datetime(UTC)", func=self.unixtime_to_datetime_utc_str),
FuncClass(desc="路径转python导入import语句", func=self.modify_import_path),
]

Expand All @@ -149,6 +151,13 @@ def list_all_result(self, text: str) -> list[FuncClass]:
def set_clipboard(self, content):
self.clipboard.setText(content)

def unixtime_to_datetime_utc_str(self, text):
content = unixtime_to_datetime_str(text or 0, tz=ZoneInfo("UTC"))
if not content:
return False, "非unix时间"
# self.set_clipboard(content)
return True, content

def unixtime_to_datetime_str(self, text):
content = unixtime_to_datetime_str(text or 0)
if not content:
Expand Down
26 changes: 26 additions & 0 deletions DesktopTools/feather_hotkey/custom_list_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from PySide6.QtWidgets import QStyledItemDelegate
from PySide6.QtGui import QColor, QFont


class CustomItemDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super().__init__(parent)

def paint(self, painter, option, index):
# 绘制默认项
super().paint(painter, option, index)

# 获取当前项的文本
item = self.parent().item(index.row())
text = item.text()

# 设置半透明的颜色
painter.setPen(QColor(150, 150, 150, 150)) # 半透明的灰色
painter.setFont(QFont("Helvetica Neue", 12))

# 计算并绘制快捷键提示,确保其右对齐
shortcut_text = f" (Ctrl+{index.row() + 1})"
text_width = painter.fontMetrics().horizontalAdvance(shortcut_text)

# 绘制文本
painter.drawText(option.rect.right() - text_width - 10, option.rect.top() + 15, shortcut_text)
36 changes: 19 additions & 17 deletions DesktopTools/feather_hotkey/searchbar.ui
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<string>小命令工具</string>
</property>
<property name="styleSheet">
<string notr="true"> background-color: #F9F9F9;
<string notr="true">background-color: #F0F0F0;
color: #333333;
font-size: 16px;
font-family: Arial, sans-serif;</string>
font-family: 'Helvetica Neue', Arial, sans-serif;</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
Expand All @@ -30,12 +30,12 @@
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="styleSheet">
<string notr="true"> background-color: #FFFFFF;
border-radius: 40px;
border: 2px solid #CCCCCC;
padding: 20px;
<string notr="true">background-color: #FFFFFF;
border-radius: 20px;
border: 1px solid #CCCCCC;
padding: 10px;
font-size: 18px;
font-family: Arial, sans-serif;</string>
font-family: 'Helvetica Neue', Arial, sans-serif;</string>
</property>
<property name="placeholderText">
<string>开始输入命令...</string>
Expand All @@ -45,12 +45,12 @@
<item>
<widget class="QPushButton" name="pushButton">
<property name="styleSheet">
<string notr="true"> background-color: #008CBA;
border-radius: 40px;
<string notr="true">background-color: #007BFF;
border-radius: 20px;
border: none;
padding: 20px 40px;
padding: 10px 20px;
font-size: 18px;
font-family: Arial, sans-serif;
font-family: 'Helvetica Neue', Arial, sans-serif;
color: #FFFFFF;</string>
</property>
<property name="text">
Expand All @@ -67,14 +67,16 @@
</property>
<property name="styleSheet">
<string notr="true">background-color: #FFFFFF;
border-radius: 16px;
border: 2px solid #CCCCCC;
padding: 20px;
selection-background-color: #FFCA72;
border-radius: 10px;
border: 1px solid #CCCCCC;
padding: 10px;
font-size: 20px;
font-family: Arial, sans-serif;
line-height: 2.5;
font-family: 'Helvetica Neue', Arial, sans-serif;
line-height: 1.5;
margin-bottom: 10px;
margin: 1px; </string>
margin: 1px;
background: rgba(255, 255, 255, 0.9);</string>
</property>
<property name="currentRow">
<number>0</number>
Expand Down
57 changes: 48 additions & 9 deletions DesktopTools/feather_hotkey/thread.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,60 @@
import sys
from pynput import keyboard
from PySide6.QtCore import QThread, Signal
from PySide6.QtCore import QByteArray, QThread, Signal, QAbstractNativeEventFilter
from ..logger import logger

if sys.platform == "win32":
import ctypes
import win32con
user32 = ctypes.windll.user32

class NativeEvent(QAbstractNativeEventFilter):
def __init__(self, hotkey) -> None:
super().__init__()
self.hotkey = hotkey

def nativeEventFilter(self, eventType: QByteArray | bytes, message: int) -> object:
if sys.platform == "win32":
from ctypes import wintypes
import win32con
msg = wintypes.MSG.from_address(message.__init__())
if eventType == "windows_generic_MSG" and msg.message == win32con.WM_HOTKEY:
self.hotkey.on_activate()
return super().nativeEventFilter(eventType, message)


class SignalHotKey(QThread):
_signal = Signal()

def __init__(self):
super().__init__()
self.hotkey_id = 1
self.hotkey_vk = 0x31
# win: https://learn.microsoft.com/zh-cn/windows/win32/inputdev/virtual-key-codes

def register(self):
if sys.platform == "win32":
sign = user32.RegisterHotKey(None, self.hotkey_id, win32con.MOD_ALT, self.hotkey_vk)
# https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-registerhotkey
logger.info(f"Hotkey Registration {sign}")
self.parent().installEventFilter(NativeEvent(hotkey=self))
else:
# sys.platform in ("linux", "darwin"):
# 还没原生的方法,只能用`pynput`啦
hotkey = keyboard.GlobalHotKeys(
{
"<ctrl>+<32>": self.on_activate,
# "<ctrl>+<alt>+g": lambda: print("Goodbye"),
}
)
hotkey.start()

def listen(self):
hotkey = keyboard.GlobalHotKeys(
{
"<alt>+<shift>+c": self.on_activate,
# "<ctrl>+<alt>+g": lambda: print("Goodbye"),
}
)
hotkey.start()
def unregister(self):
if sys.platform == "win32" and self.hotkey_id:
sign = user32.UnregisterHotKey(None, self.hotkey_id)
logger.info(f"Hotkey unregistration {sign}")
else:
pass

def on_activate(self):
self._signal.emit()
Loading
Loading