Source code for ahkpy.menu

import dataclasses as dc
import functools
import uuid

from .flow import ahk_call, global_ahk_lock, _wrap_callback
from .settings import COORD_MODES, _set_coord_mode
from .unset import UNSET


__all__ = [
    "Menu",
    "TrayMenu",
    "tray_menu",
]





def _bare_menu_item_handler(callback, *_):
    callback()


def _menu_item_handler(callback, item_name, item_pos, menu_name):
    callback(item_name=item_name, item_pos=item_pos-1, menu=Menu(menu_name))


[docs]class TrayMenu(Menu): """The tray menu object. Can be used to change items in the tray menu and the appearence of the application in the notification area. Example usage:: ahkpy.tray_menu.hide_tray_icon() ahkpy.tray_menu.show_tray_icon() ahkpy.tray_menu.set_clicks(1) ahkpy.hotkey("F1", ahkpy.tray_menu.toggle_tray_icon) ahkpy.tray_menu.delete_all_items() ahkpy.tray_menu.add("E&xit", sys.exit, default=True) """ __slots__ = ("name",) def __init__(self): super().__init__("tray") @property def tray_icon_file(self): """The current tray icon file name. Returns ``None`` if the icon hasn't been changed. Setting a new file name resets the icon number to 0. :type: str :variable: `A_IconFile <https://www.autohotkey.com/docs/Variables.htm#IconFile>`_ :command: `Menu, Tray, Icon, FileName, 0 <https://www.autohotkey.com/docs/commands/Menu.htm#TrayIcon>`_ """ return ahk_call("GetVar", "A_IconFile") or None @tray_icon_file.setter def tray_icon_file(self, filename): self.set_tray_icon(filename) @property def tray_icon_number(self): """The current tray icon number in an icon group. Returns ``None`` if the icon hasn't been changed. :type: int :variable: `A_IconNumber <https://www.autohotkey.com/docs/Variables.htm#IconNumber>`_ :command: `Menu, Tray, Icon, %A_IconFile%, Number <https://www.autohotkey.com/docs/commands/Menu.htm#TrayIcon>`_ """ number = ahk_call("GetVar", "A_IconNumber") if isinstance(number, int): return number - 1 # Default icon, no number. return None @tray_icon_number.setter def tray_icon_number(self, number): filename = self.tray_icon_file if filename: self.set_tray_icon(filename, number=number)
[docs] def set_tray_icon(self, filename=UNSET, *, number=None, affected_by_suspend=None): """Change the script's tray icon. The *filename* argument can be either an icon file (ICO, CUR, ANI, EXE, DLL, CPL, SCR) or any image in a format supported by AutoHotkey. To restore the default tray icon, pass ``None`` to *filename*. The optional *number* argument sets the icon group to use. It defaults to 0, which is the first group in the file. If *number* is negative, its absolute value is assumed to be the resource ID of an icon within an executable file. If the optional *affected_by_suspend* argument is true (default), enabling Suspended mode via :func:`suspend` or via the tray menu changes the icon. :command: `Menu, Tray, Icon, FileName, IconNumber <https://www.autohotkey.com/docs/commands/Menu.htm#TrayIcon>`_ """ if filename is None: # Set default icon. self.set_tray_icon("*") return if isinstance(number, int): number += 1 if affected_by_suspend: freeze = "0" elif affected_by_suspend is not None: freeze = "1" else: freeze = None self._call("Icon", filename or "", number, freeze)
@property def is_tray_icon_visible(self): """Whether the icon is visible in the notification area. :type: bool :variable: `A_IconHidden <https://www.autohotkey.com/docs/Variables.htm#IconHidden>`_ :command: `Menu, Tray, Icon <https://www.autohotkey.com/docs/Variables.htm#Icon>`_ """ return not ahk_call("GetVar", "A_IconHidden") @is_tray_icon_visible.setter def is_tray_icon_visible(self, is_tray_icon_visible): if is_tray_icon_visible: self.show_tray_icon() else: self.hide_tray_icon()
[docs] def toggle_tray_icon(self): """Toggle the icon visibility in the notification area.""" self.is_tray_icon_visible = not self.is_tray_icon_visible
[docs] def show_tray_icon(self): """Show the icon in the notification area.""" self._call("Icon")
[docs] def hide_tray_icon(self): """Hide the icon from the notification area.""" self._call("NoIcon")
@property def tip(self): """The tray icon's tooltip text. To restore the default text, set the property to ``None``. :variable: `A_IconTip <https://www.autohotkey.com/docs/Variables.htm#IconTip>`_ :command: `Menu, Tray, Tip <https://www.autohotkey.com/docs/Variables.htm#Icon>`_ """ return ahk_call("GetVar", "A_IconTip") or None @tip.setter def tip(self, text): self._call("Tip", text)
[docs] def set_clicks(self, number): """Set the number of clicks to activate the tray menu's default menu item. Defaults to 2 clicks. """ self._call("Click", number)
tray_menu = TrayMenu()