2222using System . Linq ;
2323using System . Windows ;
2424using System . Windows . Controls ;
25- using System . Windows . Controls . Primitives ;
2625using System . Windows . Data ;
2726using System . Windows . Input ;
2827
2928using ICSharpCode . ILSpy . Commands ;
29+
3030using ICSharpCode . ILSpy . Docking ;
31- using ICSharpCode . ILSpy . Themes ;
3231using ICSharpCode . ILSpy . ViewModels ;
3332
3433using TomsToolbox . Composition ;
3534using TomsToolbox . ObservableCollections ;
35+ using TomsToolbox . Wpf ;
3636using TomsToolbox . Wpf . Converters ;
3737
38- namespace ICSharpCode . ILSpy . Util
38+ namespace ICSharpCode . ILSpy . Controls
3939{
40+ /// <summary>
41+ /// Interaction logic for MainMenu.xaml
42+ /// </summary>
4043 [ Export ]
41- [ Shared ]
42- public class MenuService ( IExportProvider exportProvider , DockWorkspace dockWorkspace )
44+ [ NonShared ]
45+ public partial class MainMenu
4346 {
44- public void Init ( Menu mainMenu , ToolBar toolBar , InputBindingCollection inputBindings )
47+ public MainMenu ( SettingsService settingsService , IExportProvider exportProvider , DockWorkspace dockWorkspace )
4548 {
46- InitMainMenu ( mainMenu ) ;
47- InitWindowMenu ( mainMenu , inputBindings ) ;
48- InitToolbar ( toolBar ) ;
49+ SessionSettings = settingsService . SessionSettings ;
50+
51+ InitializeComponent ( ) ;
52+
53+ this . BeginInvoke ( ( ) => {
54+ InitMainMenu ( Menu , exportProvider ) ;
55+ InitWindowMenu ( WindowMenuItem , Window . GetWindow ( this ) ! . InputBindings , dockWorkspace ) ;
56+ } ) ;
4957 }
5058
51- void InitMainMenu ( Menu mainMenu )
59+ public SessionSettings SessionSettings { get ; }
60+
61+ static void InitMainMenu ( Menu mainMenu , IExportProvider exportProvider )
5262 {
5363 var mainMenuCommands = exportProvider . GetExports < ICommand , IMainMenuCommandMetadata > ( "MainMenuCommand" ) ;
5464 // Start by constructing the individual flat menus
@@ -106,12 +116,9 @@ void InitMainMenu(Menu mainMenu)
106116 }
107117 }
108118
109- foreach ( var item in parentMenuItems . Values )
119+ foreach ( var item in parentMenuItems . Values . Where ( item => item . Parent == null ) )
110120 {
111- if ( item . Parent == null )
112- {
113- mainMenu . Items . Add ( item ) ;
114- }
121+ mainMenu . Items . Add ( item ) ;
115122 }
116123
117124 MenuItem GetOrAddParentMenuItem ( string menuId , string resourceKey )
@@ -137,59 +144,21 @@ MenuItem GetOrAddParentMenuItem(string menuId, string resourceKey)
137144 }
138145 }
139146
140- void InitWindowMenu ( Menu mainMenu , InputBindingCollection inputBindings )
147+ static void InitWindowMenu ( MenuItem windowMenuItem , InputBindingCollection inputBindings , DockWorkspace dockWorkspace )
141148 {
142- var windowMenuItem = mainMenu . Items . OfType < MenuItem > ( ) . First ( m => ( string ) m . Tag == nameof ( Properties . Resources . _Window ) ) ;
143-
144149 var defaultItems = windowMenuItem . Items . Cast < Control > ( ) . ToArray ( ) ;
145150
146151 windowMenuItem . Items . Clear ( ) ;
147152
148- var toolItems = dockWorkspace . ToolPanes . Select ( toolPane => CreateMenuItem ( toolPane , inputBindings ) ) . ToArray ( ) ;
149- var tabItems = dockWorkspace . TabPages . ObservableSelect ( tabPage => CreateMenuItem ( tabPage ) ) ;
153+ var toolItems = dockWorkspace . ToolPanes . Select ( toolPane => CreateMenuItem ( toolPane , inputBindings , dockWorkspace ) ) . ToArray ( ) ;
154+ var tabItems = dockWorkspace . TabPages . ObservableSelect ( tabPage => CreateMenuItem ( tabPage , dockWorkspace ) ) ;
150155
151156 var allItems = new ObservableCompositeCollection < Control > ( defaultItems , [ new Separator ( ) ] , toolItems , [ new Separator ( ) ] , tabItems ) ;
152157
153158 windowMenuItem . ItemsSource = allItems ;
154159 }
155160
156- void InitToolbar ( ToolBar toolBar )
157- {
158- int navigationPos = 0 ;
159- int openPos = 1 ;
160- var toolbarCommandsByCategory = exportProvider . GetExports < ICommand , IToolbarCommandMetadata > ( "ToolbarCommand" )
161- . OrderBy ( c => c . Metadata ? . ToolbarOrder )
162- . GroupBy ( c => c . Metadata ? . ToolbarCategory ) ;
163-
164- foreach ( var commandCategory in toolbarCommandsByCategory )
165- {
166- if ( commandCategory . Key == nameof ( Properties . Resources . Navigation ) )
167- {
168- foreach ( var command in commandCategory )
169- {
170- toolBar . Items . Insert ( navigationPos ++ , CreateToolbarItem ( command ) ) ;
171- openPos ++ ;
172- }
173- }
174- else if ( commandCategory . Key == nameof ( Properties . Resources . Open ) )
175- {
176- foreach ( var command in commandCategory )
177- {
178- toolBar . Items . Insert ( openPos ++ , CreateToolbarItem ( command ) ) ;
179- }
180- }
181- else
182- {
183- toolBar . Items . Add ( new Separator ( ) ) ;
184- foreach ( var command in commandCategory )
185- {
186- toolBar . Items . Add ( CreateToolbarItem ( command ) ) ;
187- }
188- }
189- }
190- }
191-
192- Control CreateMenuItem ( TabPageModel pane )
161+ static Control CreateMenuItem ( TabPageModel pane , DockWorkspace dockWorkspace )
193162 {
194163 var header = new TextBlock {
195164 MaxWidth = 200 ,
@@ -200,7 +169,7 @@ Control CreateMenuItem(TabPageModel pane)
200169 Source = pane
201170 } ) ;
202171
203- MenuItem menuItem = new ( ) {
172+ var menuItem = new MenuItem {
204173 Command = new TabPageCommand ( pane , dockWorkspace ) ,
205174 Header = header ,
206175 IsCheckable = true
@@ -216,16 +185,16 @@ Control CreateMenuItem(TabPageModel pane)
216185 return menuItem ;
217186 }
218187
219- Control CreateMenuItem ( ToolPaneModel pane , InputBindingCollection inputBindings )
188+ static Control CreateMenuItem ( ToolPaneModel pane , InputBindingCollection inputBindings , DockWorkspace dockWorkspace )
220189 {
221- MenuItem menuItem = new ( ) {
190+ var menuItem = new MenuItem {
222191 Command = pane . AssociatedCommand ?? new ToolPaneCommand ( pane . ContentId , dockWorkspace ) ,
223192 Header = pane . Title
224193 } ;
225194 var shortcutKey = pane . ShortcutKey ;
226195 if ( shortcutKey != null )
227196 {
228- inputBindings . Add ( new ( menuItem . Command , shortcutKey ) ) ;
197+ inputBindings . Add ( new InputBinding ( menuItem . Command , shortcutKey ) ) ;
229198 menuItem . InputGestureText = shortcutKey . GetDisplayStringForCulture ( CultureInfo . CurrentUICulture ) ;
230199 }
231200 if ( ! string . IsNullOrEmpty ( pane . Icon ) )
@@ -239,29 +208,5 @@ Control CreateMenuItem(ToolPaneModel pane, InputBindingCollection inputBindings)
239208
240209 return menuItem ;
241210 }
242-
243- static Button CreateToolbarItem ( IExport < ICommand , IToolbarCommandMetadata > commandExport )
244- {
245- var command = commandExport . Value ;
246-
247- Button toolbarItem = new ( ) {
248- Style = ThemeManager . Current . CreateToolBarButtonStyle ( ) ,
249- Command = CommandWrapper . Unwrap ( command ) ,
250- ToolTip = Properties . Resources . ResourceManager . GetString ( commandExport . Metadata ? . ToolTip ?? string . Empty ) ,
251- Tag = commandExport . Metadata ? . Tag ,
252- Content = new Image {
253- Width = 16 ,
254- Height = 16 ,
255- Source = Images . Load ( command , commandExport . Metadata ? . ToolbarIcon )
256- }
257- } ;
258-
259- if ( command is IProvideParameterBinding parameterBinding )
260- {
261- BindingOperations . SetBinding ( toolbarItem , ButtonBase . CommandParameterProperty , parameterBinding . ParameterBinding ) ;
262- }
263-
264- return toolbarItem ;
265- }
266211 }
267212}
0 commit comments