Coverage for collectra/gui/native_menu.py: 75.00%

24 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-13 04:26 +0000

1"""Native menu entries delegate to the same actions as the viewer toolbar.""" 

2 

3import json 

4import platform 

5 

6from webview.menu import Menu, MenuAction, MenuSeparator 

7 

8 

9def put_file_menu_first(menu): 

10 """Keep the app menu at index zero, then File before Cocoa's Edit/View.""" 

11 if menu is None: 

12 return 

13 for index in range(1, menu.numberOfItems()): 

14 item = menu.itemAtIndex_(index) 

15 submenu = item.submenu() 

16 if submenu is not None and submenu.title() == "File": 

17 if index != 1: 

18 menu.removeItemAtIndex_(index) 

19 menu.insertItem_atIndex_(item, 1) 

20 return 

21 

22 

23def arrange_native_menu(): 

24 if platform.system() != "Darwin": 

25 return 

26 from AppKit import NSApplication 

27 from PyObjCTools import AppHelper 

28 

29 # pywebview's startup callback runs on a worker; AppKit requires the UI thread. 

30 AppHelper.callAfter( 

31 lambda: put_file_menu_first(NSApplication.sharedApplication().mainMenu()) 

32 ) 

33 

34 

35def build_native_menu(window): 

36 def action(title, name): 

37 return MenuAction( 

38 title, 

39 lambda: window.evaluate_js(f"runNativeMenuAction({json.dumps(name)})"), 

40 ) 

41 

42 return [ 

43 Menu( 

44 "File", 

45 [ 

46 action("Open Results…", "open"), 

47 action("Show Current Results in File Manager…", "reveal"), 

48 MenuSeparator(), 

49 action("Close Current Tab", "close-tab"), 

50 ], 

51 ), 

52 Menu( 

53 "Navigate", 

54 [ 

55 action("Previous File", "previous-file"), 

56 action("Next File", "next-file"), 

57 MenuSeparator(), 

58 action("Find File…", "find-file"), 

59 action("Find Artefacts…", "find-artefacts"), 

60 action("Previous Search Match", "previous-match"), 

61 action("Next Search Match", "next-match"), 

62 ], 

63 ), 

64 Menu( 

65 "Panels", 

66 [ 

67 action("Toggle Sidebar", "sidebar"), 

68 action("Open Pipeline Graph", "graph"), 

69 action("Show Artefacts", "artefacts"), 

70 action("Show Tasks", "tasks"), 

71 MenuSeparator(), 

72 action("Fit Pipeline Graph", "fit-graph"), 

73 action("Fit Image", "fit-image"), 

74 MenuSeparator(), 

75 Menu( 

76 "Split Panel", 

77 [ 

78 action(f"Split {direction.title()}", f"split-{direction}") 

79 for direction in ("left", "right", "above", "below") 

80 ], 

81 ), 

82 action("Toggle Current Panel", "collapse-panel"), 

83 action("Remove Current Panel", "remove-panel"), 

84 ], 

85 ), 

86 Menu( 

87 "Layouts", 

88 [ 

89 action("Choose Layout…", "choose-layout"), 

90 action("Save Current Layout…", "save-layout"), 

91 ], 

92 ), 

93 Menu( 

94 "Tools", 

95 [ 

96 action("Check for Updates…", "check-updates"), 

97 MenuSeparator(), 

98 action("History and Changes…", "source-control"), 

99 action("Settings…", "settings"), 

100 action("Statistics…", "statistics"), 

101 action("Load Details…", "load-details"), 

102 ], 

103 ), 

104 ]