Skip to content

Commit

Permalink
Implement 'Open AI' submenu in the NVDA root menu for documentation, …
Browse files Browse the repository at this point in the history
…main dialog and various web shortcuts
  • Loading branch information
AAClause committed Dec 2, 2023
1 parent 481dfd5 commit 7aa6a67
Showing 1 changed file with 83 additions and 5 deletions.
88 changes: 83 additions & 5 deletions addon/globalPlugins/openai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@

addonHandler.initTranslation()

ROOT_ADDON_DIR = "\\".join(ADDON_DIR.split(os.sep)[:-2])
ADDON_INFO = addonHandler.Addon(
ROOT_ADDON_DIR
).manifest

confSpecs = {
"use_org": "boolean(default=False)",
"model": f"string(default={DEFAULT_MODEL.name})",
Expand Down Expand Up @@ -202,9 +207,79 @@ def __init__(self):
APIKey = api_key_manager.get_api_key()
gui.settingsDialogs.NVDASettingsDialog.categoryClasses.append(SettingsDlg)
self.client = None
self.createMenu()

def createMenu(self):
self.submenu = wx.Menu()
item = self.submenu.Append(
wx.ID_ANY,
_("Docu&mentation"),
_("Open the documentation of this addon")
)
gui.mainFrame.sysTrayIcon.Bind(wx.EVT_MENU, self.onDocumentation, item)
item = self.submenu.Append(
wx.ID_ANY,
_("Main d&ialog..."),
_("Show the Open AI dialog")
)
gui.mainFrame.sysTrayIcon.Bind(wx.EVT_MENU, self.onShowMainDialog, item)
item = self.submenu.Append(
wx.ID_ANY,
_("API &keys"),
_("Manage the API keys")
)
gui.mainFrame.sysTrayIcon.Bind(wx.EVT_MENU, self.onAPIKeys, item)
item = self.submenu.Append(
wx.ID_ANY,
_("API &usage"),
_("Open the API usage webpage")
)
gui.mainFrame.sysTrayIcon.Bind(wx.EVT_MENU, self.onAPIUsage, item)
item = self.submenu.Append(
wx.ID_ANY,
_("Git&Hub repository"),
_("Open the GitHub repository of this addon")
)
gui.mainFrame.sysTrayIcon.Bind(wx.EVT_MENU, self.onGitRepo, item)

addon_name = ADDON_INFO["name"]
addon_version = ADDON_INFO["version"]
self.submenu_item = gui.mainFrame.sysTrayIcon.menu.InsertMenu(
2,
wx.ID_ANY,
_("Open A&I {addon_version}".format(
addon_version=addon_version)
),
self.submenu
)

def onAPIKeys(self, evt):
url = "https://platform.openai.com/api-keys"
os.startfile(url)

def onAPIUsage(self, evt):
url = "https://platform.openai.com/usage"
os.startfile(url)

def onGitRepo(self, evt):
url = "https://github.com/aaclause/nvda-OpenAI/"
os.startfile(url)

def onDocumentation(self, evt):
import languageHandler
languages = ["en"]
language = languageHandler.getLanguage()
if '_' in language:
languages.insert(0, language.split('_')[0])
languages.insert(0, language)
for lang in languages:
fp = os.path.join(ROOT_ADDON_DIR, "doc", lang, "readme.html")
if os.path.exists(fp):
os.startfile(fp)

def terminate(self):
gui.settingsDialogs.NVDASettingsDialog.categoryClasses.remove(SettingsDlg)
gui.mainFrame.sysTrayIcon.menu.DestroyItem(self.submenu_item)
super().terminate()

def getClient(self):
Expand All @@ -228,11 +303,7 @@ def getClient(self):
self.client = OpenAI(api_key=api_key)
return self.client

@script(
gesture="kb:nvda+g",
description=_("Show Open AI dialog")
)
def script_showMainDialog(self, gesture):
def onShowMainDialog(self, evt):
if not self.getClient():
return ui.message(NO_AUTHENTICATION_KEY_PROVIDED_MSG)
from . import maindialog
Expand All @@ -242,6 +313,13 @@ def script_showMainDialog(self, gesture):
conf=conf
)

@script(
gesture="kb:nvda+g",
description=_("Show Open AI dialog")
)
def script_showMainDialog(self, gesture):
self.onShowMainDialog(None)

@script(
gesture="kb:nvda+e",
description=_("Take a screenshot and describe it")
Expand Down

0 comments on commit 7aa6a67

Please sign in to comment.