@@ -112,6 +112,58 @@ def generate_rust(menus):
112112 ''' .replace ('{menu_code}' , menu_code )
113113
114114
115+
116+ def typescript_for_item (item ):
117+ typ = item ['type' ]
118+ if typ == 'native' :
119+ nativeItem = item ['nativeMenuItem' ]
120+ return f'<NativeMenuItem item="{ nativeItem } " />'
121+ elif typ == 'separator' :
122+ return '<MenuSeparator />'
123+ elif typ == 'custom' :
124+ item_id = item ['id' ]
125+ title = item ['title' ]
126+ code = f'<MenuItem id="{ item_id } " title="{ title } " '
127+ accel = item .get ('accelerator' )
128+ if accel :
129+ code += f'accelerator="{ accel } " '
130+ code += ' />'
131+ return code
132+ assert ValueError (typ )
133+
134+
135+ def generate_typescript (menus ):
136+ lines = [
137+ '<MenuBar>'
138+ ]
139+ for i , menu in enumerate (menus ):
140+ title = menu ['title' ]
141+ lines += [
142+ f'<Menu title="{ title } ">' ,
143+ * [
144+ typescript_for_item (item )
145+ for item in menu ['items' ]
146+ ],
147+ '</Menu>' ,
148+ ]
149+
150+ lines .append ('</MenuBar>' )
151+ menu_code = '\n ' .join (lines )
152+
153+ return '''// THIS FILE IS GENERATED. Edit menu.json instead.
154+ import React from 'react';
155+
156+ declare const MenuBar: React.ComponentType<any>;
157+ declare const Menu: React.ComponentType<any>;
158+ declare const NativeMenuItem: React.ComponentType<any>;
159+ declare const MenuSeparator: React.ComponentType<any>;
160+ declare const MenuItem: React.ComponentType<any>;
161+
162+ export function AppMenu() {
163+ {menu_code}
164+ }
165+ ''' .replace ('{menu_code}' , menu_code )
166+
115167if __name__ == '__main__' :
116168 menu_json = json .load (open ('menu.json' ))
117169 menus = menu_json ['menus' ]
@@ -120,3 +172,9 @@ def generate_rust(menus):
120172 with open ('src-tauri/src/core/menu.rs' , 'w' ) as out :
121173 out .write (menu_rs )
122174 subprocess .check_output (['rustfmt' , 'src-tauri/src/core/menu.rs' ])
175+
176+ menu_ts = generate_typescript (menus )
177+ with open ('src/components/menu.tsx' , 'w' ) as out :
178+ out .write (menu_ts )
179+ subprocess .check_output (['yarn' , 'prettier' , '--config' , 'package.json' , '--write' , 'src/components/menu.tsx' ])
180+ subprocess .check_output (['yarn' , 'eslint' , '--fix' , '--ext' , '.js,.ts,.tsx' , 'src/components/menu.tsx' ])
0 commit comments