Exemple #1
0
        private static void OnStartup(string[] args)
        {
            // TODO free console for UI execution

            // Bootstrap container
            container = new SimpleInjector.Container();

            container.RegisterSingleton<IPlatformFacade, WindowsPlatform>();
            container.RegisterSingleton<IWindowManagerService, WindowManagerService>();
            container.RegisterSingleton<IKeyMapService, KeyMapService>();
            container.RegisterSingleton<INotificationService, NotificationService>();
            container.RegisterSingleton<IMainWindow, MainWindow>();

            // Register default command set
            var commands =
                AppDomain.CurrentDomain.GetAssemblies()
                         .SelectMany(s => s.GetTypes())
                         .Where(p => p != typeof(ICommand) && typeof(ICommand).IsAssignableFrom(p))
                         .ToList();
            container.RegisterCollection<ICommand>(commands);
            container.Register(
                typeof(ICommandHandler<>),
                container.GetTypesToRegister(typeof(ICommandHandler<>), new[] { Assembly.GetExecutingAssembly() })
                    .Where(t => t.IsPublic));

            // Register handlers
            var commandHandlers =
                commands.Select(command => container.GetInstance(typeof(ICommandHandler<>).MakeGenericType(command)));
            container.RegisterSingleton<ICommandService>(
                () => new CommandService(container.GetAllInstances<ICommand>(), commandHandlers));

            container.Verify();

            mainForm = container.GetInstance<IMainWindow>() as Form;

            // Parse arguments and run the app
            var getopt = new GetOpt(Options);

            char returnChar = ' ';
            int optionIndex = 0;
            string optionArg = null;
            bool startWindowManager = true;

            try
            {
                do
                {
                    // parse the args
                    switch (returnChar)
                    {
                        case 'h':
                            Console.Write(getopt.Description);
                            return;
                        case 'v':
                            Console.WriteLine(Application.ProductVersion);
                            return;
                        case 'f': // parse alternate rc file
                            configFile = optionArg;
                            break;
                        case 'c': // send a command
                            startWindowManager = false;
                            break;
                        case 'r': // reset all windows to decorated regular state, in case things screw up :(
                            startWindowManager = false;
                            WindowManagerService.Reset();
                            break;
                    }

                    returnChar = getopt.Parse(args, ref optionIndex, out optionArg);
                }
                while (returnChar != ' ');
            }
            catch (GetOpt.InvalidOptionException e)
            {
                Console.WriteLine(Properties.Resources.Option_Help_Error);
                Console.WriteLine(e);
                return;
            }

            if (startWindowManager)
            {
                // Start the services
                ((ServiceBase)container.GetInstance<IKeyMapService>()).Start();
                ((ServiceBase)container.GetInstance<IWindowManagerService>()).Start();
                ((ServiceBase)container.GetInstance<INotificationService>()).Start();

                // set the internal variables
                // set the data structures
                // read rc file
                ////CommandManager.Execute((int)CommandManager.OtherCommands.source, new string[] { _configFile });
                var keyMapService = container.GetInstance<IKeyMapService>();

                // Top keymap does not require the window on focus, they are global hot keys
                // Prefix key must reside here
                keyMapService.AddKeyMap("top");
                keyMapService.SetTopKey("top", Keys.Control | Keys.B);

                // Root keymap is the default keymap invoked via Prefix key with readkey command
                // All other default shortcuts reside here
                keyMapService.AddKeyMap("root");

                var commandService = container.GetInstance<ICommandService>();
                commandService.Run("definekey top T readkey root");

                // Set default variables
                // commandService.Run()
                ////keyMapService.SetTopKey("root", Keys.Control | Keys.T);
                Application.Run(mainForm);
            }
            else
            {
                // check if an instance already runs
                // send the commands
                // throw output to console or to the bar
            }
        }