-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard_layout_monitor.py
More file actions
70 lines (56 loc) · 2.59 KB
/
Copy pathkeyboard_layout_monitor.py
File metadata and controls
70 lines (56 loc) · 2.59 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
# Copyright (C) 2024 Oleksii Sylichenko (a.silichenko@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
# (at your option) 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 <https://www.gnu.org/licenses/>.
import logging
import threading
import time
import keyboard_layout_controller
from onekey_layout_switcher.models import LayoutInfo
from onekey_layout_switcher.utils.layout_info_util import LayoutInfoUtil
from tray_icon import TrayIcon
CHECK_INTERVAL: float = 0.5
"""Check keyboard layout every `value` seconds.
Is used when window is changed and language also changed but not by our hotkey"""
logger = logging.getLogger(__name__)
class KeyboardLayoutMonitor:
"""Starts tray icon with flag of the keyboard language.
Checks keyboard layout and update flag icon if layout is changed."""
def __init__(
self,
tray_icon: TrayIcon,
layout_info_util: LayoutInfoUtil,
check_interval: float = None
) -> None:
self._is_active: bool = False
self._check_interval: float = check_interval if check_interval is not None else CHECK_INTERVAL
self._tray_icon: TrayIcon = tray_icon
self._layout_info_util: LayoutInfoUtil = layout_info_util
def _monitoring(self) -> None:
prev_active_hkl: int = 0
while self._is_active:
active_hkl: int = keyboard_layout_controller.get_active_hkl()
if active_hkl != prev_active_hkl:
prev_active_hkl = active_hkl
layout: LayoutInfo = self._layout_info_util.from_hkl(active_hkl)
country_code: str = layout.country_code or ''
if not country_code:
logger.error(f'Failed to obtain country code: {str(layout)}')
self._tray_icon.update_layout(country_code)
time.sleep(self._check_interval)
def start(self) -> None:
"""Starts monitoring thread and runs tray icon, which is blocking."""
self._is_active = True
threading.Thread(target=self._monitoring, daemon=True).start()
self._tray_icon.run() # blocking
self._is_active = False