Coverage for aipyapp/gui/statusbar.py: 0%

1 import os

2 import sys

3 import subprocess

4 import wx

5

6 from .. import T

7

8 class CStatusBar(wx.StatusBar):

9 def __init__(self, parent):

10 super().__init__(parent, style=wx.STB_DEFAULT_STYLE)

11 self.parent = parent

12 self.SetFieldsCount(3)

13 self.SetStatusWidths([-1, 30, 80])

14

15 self.tm = parent.tm

16 self.current_llm = self.tm.client_manager.names['default']

17 self.enabled_llm = list(self.tm.client_manager.names['enabled'])

18 self.menu_items = self.enabled_llm

19 self.radio_group = []

20

21 self.folder_button = wx.StaticBitmap(self, -1, wx.ArtProvider.GetBitmap(wx.ART_FOLDER_OPEN, wx.ART_MENU))

22 self.folder_button.Bind(wx.EVT_LEFT_DOWN, self.on_open_work_dir)

23 self.Bind(wx.EVT_SIZE, self.on_size)

24

25 self.SetStatusText(f"{self.current_llm} ▾", 2)

26 self.Bind(wx.EVT_LEFT_DOWN, self.on_click)

27

28 def on_size(self, event):

29 rect = self.GetFieldRect(1)

30 self.folder_button.SetPosition((rect.x + 5, rect.y + 2))

31 event.Skip()

32

33 def on_click(self, event):

34 rect = self.GetFieldRect(2)

35 if rect.Contains(event.GetPosition()):

36 self.show_menu()

37

38 def show_menu(self):

39 self.current_menu = wx.Menu()

40 self.radio_group = []

41 for label in self.menu_items:

42 item = wx.MenuItem(self.current_menu, wx.ID_ANY, label, kind=wx.ITEM_RADIO)

43 self.current_menu.Append(item)

44 self.radio_group.append(item)

45 self.Bind(wx.EVT_MENU, self.on_menu_select, item)

46 if label == self.current_llm:

47 item.Check()

48 rect = self.GetFieldRect(2)

49 pos = self.ClientToScreen(rect.GetBottomLeft())

50 self.PopupMenu(self.current_menu, self.ScreenToClient(pos))

51

52 def on_menu_select(self, event):

53 item = self.current_menu.FindItemById(event.GetId())

54 label = item.GetItemLabel()

55 if self.tm.use(label):

56 self.current_llm = label

57 self.SetStatusText(f"{label} ▾", 2)

58 else:

59 wx.MessageBox(T("LLM {} is not available").format(label), T("Warning"), wx.OK|wx.ICON_WARNING)

60

61 def on_open_work_dir(self, event):

62 """打开工作目录"""

63 work_dir = self.tm.workdir

64 if os.path.exists(work_dir):

65 if sys.platform == 'win32':

66 os.startfile(work_dir)

67 elif sys.platform == 'darwin':

68 subprocess.call(['open', work_dir])

69 else:

70 subprocess.call(['xdg-open', work_dir])

71 else:

72 wx.MessageBox(T("Work directory does not exist"), T("Error"), wx.OK | wx.ICON_ERROR)