Files
TimeTracker/src/time_tracker/platform/windows.py
2026-03-10 01:40:10 +03:00

34 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import win32gui
import win32process
import psutil
from .base import PlatformBase, WindowInfo
class WindowsPlatform(PlatformBase):
def get_active_window(self) -> WindowInfo:
try:
hwnd = win32gui.GetForegroundWindow()
if not hwnd:
return WindowInfo(title="", process_name="")
# Получаем заголовок окна
title = win32gui.GetWindowText(hwnd).strip()
# Получаем PID процесса
_, pid = win32process.GetWindowThreadProcessId(hwnd)
if pid == 0:
return WindowInfo(title=title, process_name="")
# Получаем имя исполняемого файла процесса
try:
process = psutil.Process(pid)
process_name = process.name()
except (psutil.NoSuchProcess, psutil.AccessDenied):
process_name = ""
return WindowInfo(title=title, process_name=process_name)
except Exception:
# В случае ошибки (например, недоступно окно) — возвращаем пустые данные
return WindowInfo(title="", process_name="")