Beispiel #1
0
        private void App_OnStartup(object sender, StartupEventArgs e)
        {
            try
            {
                Log.Info("Boot strapping the services and UI.");

                // We manually close this because automatic closure steals focus from the
                // dynamic splash screen.
                splashScreen.Close(TimeSpan.FromSeconds(0.5f));

                //Apply theme
                applyTheme();

                //Define MainViewModel before services so I can setup a delegate to call into the MainViewModel
                //This is to work around the fact that the MainViewModel is created after the services.
                MainViewModel     mainViewModel         = null;
                Action <KeyValue> fireKeySelectionEvent = kv =>
                {
                    if (mainViewModel != null) //Access to modified closure is a good thing here, for once!
                    {
                        mainViewModel.FireKeySelectionEvent(kv);
                    }
                };

                CleanupAndPrepareCommuniKateInitialState();

                ValidateDynamicKeyboardLocation();

                // Handle plugins. Validate if directory exists and is accessible and pre-load all plugins, building a in-memory list of available ones.
                ValidatePluginsLocation();
                if (Settings.Default.EnablePlugins)
                {
                    PluginEngine.LoadAvailablePlugins();
                }

                var presageInstallationProblem = PresageInstallationProblemsDetected();

                //Create services
                var           errorNotifyingServices = new List <INotifyErrors>();
                IAudioService audioService           = new AudioService();

                IDictionaryService           dictionaryService           = new DictionaryService(Settings.Default.SuggestionMethod);
                IPublishService              publishService              = new PublishService();
                ISuggestionStateService      suggestionService           = new SuggestionStateService();
                ICalibrationService          calibrationService          = CreateCalibrationService();
                ICapturingStateManager       capturingStateManager       = new CapturingStateManager(audioService);
                ILastMouseActionStateManager lastMouseActionStateManager = new LastMouseActionStateManager();
                IKeyStateService             keyStateService             = new KeyStateService(suggestionService, capturingStateManager, lastMouseActionStateManager, calibrationService, fireKeySelectionEvent);
                IInputService inputService = CreateInputService(keyStateService, dictionaryService, audioService,
                                                                calibrationService, capturingStateManager, errorNotifyingServices);
                IKeyboardOutputService keyboardOutputService = new KeyboardOutputService(keyStateService, suggestionService, publishService, dictionaryService, fireKeySelectionEvent);
                IMouseOutputService    mouseOutputService    = new MouseOutputService(publishService);
                errorNotifyingServices.Add(audioService);
                errorNotifyingServices.Add(dictionaryService);
                errorNotifyingServices.Add(publishService);
                errorNotifyingServices.Add(inputService);

                ReleaseKeysOnApplicationExit(keyStateService, publishService);

                //Compose UI
                var mainWindow = new MainWindow(audioService, dictionaryService, inputService, keyStateService);
                IWindowManipulationService mainWindowManipulationService = CreateMainWindowManipulationService(mainWindow);
                errorNotifyingServices.Add(mainWindowManipulationService);
                mainWindow.WindowManipulationService = mainWindowManipulationService;

                //Subscribing to the on closing events.
                mainWindow.Closing += dictionaryService.OnAppClosing;

                mainViewModel = new MainViewModel(
                    audioService, calibrationService, dictionaryService, keyStateService,
                    suggestionService, capturingStateManager, lastMouseActionStateManager,
                    inputService, keyboardOutputService, mouseOutputService, mainWindowManipulationService,
                    errorNotifyingServices);

                mainWindow.SetMainViewModel(mainViewModel);

                //Setup actions to take once main view is loaded (i.e. the view is ready, so hook up the services which kicks everything off)
                Action postMainViewLoaded = () =>
                {
                    mainViewModel.AttachErrorNotifyingServiceHandlers();
                    mainViewModel.AttachInputServiceEventHandlers();
                };

                mainWindow.AddOnMainViewLoadedAction(postMainViewLoaded);

                //Show the main window
                mainWindow.Show();

                if (Settings.Default.LookToScrollEnabled && Settings.Default.LookToScrollShowOverlayWindow)
                {
                    // Create the overlay window, but don't show it yet. It'll make itself visible when the conditions are right.
                    new LookToScrollOverlayWindow(mainViewModel);
                }

                //Display splash screen and check for updates (and display message) after the window has been sized and positioned for the 1st time
                EventHandler sizeAndPositionInitialised = null;
                sizeAndPositionInitialised = async(_, __) =>
                {
                    mainWindowManipulationService.SizeAndPositionInitialised -= sizeAndPositionInitialised; //Ensure this handler only triggers once
                    await ShowSplashScreen(inputService, audioService, mainViewModel, OptiKey.Properties.Resources.OPTIKEY_CHAT_DESCRIPTION);

                    await mainViewModel.RaiseAnyPendingErrorToastNotifications();
                    await AttemptToStartMaryTTSService(inputService, audioService, mainViewModel);
                    await AlertIfPresageBitnessOrBootstrapOrVersionFailure(presageInstallationProblem, inputService, audioService, mainViewModel);

                    inputService.RequestResume(); //Start the input service

                    await CheckForUpdates(inputService, audioService, mainViewModel);
                };

                if (mainWindowManipulationService.SizeAndPositionIsInitialised)
                {
                    sizeAndPositionInitialised(null, null);
                }
                else
                {
                    mainWindowManipulationService.SizeAndPositionInitialised += sizeAndPositionInitialised;
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error starting up application", ex);
                throw;
            }
        }
Beispiel #2
0
        private async void App_OnStartup(object sender, StartupEventArgs e)
        {
            try
            {
                Log.Info("Boot strapping the services and UI.");

                //Apply theme
                applyTheme();

                //Define MainViewModel before services so I can setup a delegate to call into the MainViewModel
                //This is to work around the fact that the MainViewModel is created after the services.
                MainViewModel     mainViewModel         = null;
                Action <KeyValue> fireKeySelectionEvent = kv =>
                {
                    if (mainViewModel != null) //Access to modified closure is a good thing here, for once!
                    {
                        mainViewModel.FireKeySelectionEvent(kv);
                    }
                };

                //Create services
                var                          errorNotifyingServices      = new List <INotifyErrors>();
                IAudioService                audioService                = new AudioService();
                IDictionaryService           dictionaryService           = new DictionaryService();
                IPublishService              publishService              = new PublishService();
                ISuggestionStateService      suggestionService           = new SuggestionStateService();
                ICalibrationService          calibrationService          = CreateCalibrationService();
                ICapturingStateManager       capturingStateManager       = new CapturingStateManager(audioService);
                ILastMouseActionStateManager lastMouseActionStateManager = new LastMouseActionStateManager();
                IKeyStateService             keyStateService             = new KeyStateService(suggestionService, capturingStateManager, lastMouseActionStateManager, calibrationService, fireKeySelectionEvent);
                IInputService                inputService                = CreateInputService(keyStateService, dictionaryService, audioService, calibrationService, capturingStateManager, errorNotifyingServices);
                IKeyboardOutputService       keyboardOutputService       = new KeyboardOutputService(keyStateService, suggestionService, publishService, dictionaryService, fireKeySelectionEvent);
                IMouseOutputService          mouseOutputService          = new MouseOutputService(publishService);
                errorNotifyingServices.Add(audioService);
                errorNotifyingServices.Add(dictionaryService);
                errorNotifyingServices.Add(publishService);
                errorNotifyingServices.Add(inputService);

                //Release keys on application exit
                ReleaseKeysOnApplicationExit(keyStateService, publishService);

                //Compose UI
                var mainWindow = new MainWindow(audioService, dictionaryService, inputService);

                IWindowManipulationService mainWindowManipulationService = new WindowManipulationService(
                    mainWindow,
                    () => Settings.Default.MainWindowOpacity,
                    () => Settings.Default.MainWindowState,
                    () => Settings.Default.MainWindowPreviousState,
                    () => Settings.Default.MainWindowFloatingSizeAndPosition,
                    () => Settings.Default.MainWindowDockPosition,
                    () => Settings.Default.MainWindowDockSize,
                    () => Settings.Default.MainWindowFullDockThicknessAsPercentageOfScreen,
                    () => Settings.Default.MainWindowCollapsedDockThicknessAsPercentageOfFullDockThickness,
                    () => Settings.Default.MainWindowMinimisedPosition,
                    o => Settings.Default.MainWindowOpacity                               = o,
                    state => Settings.Default.MainWindowState                             = state,
                    state => Settings.Default.MainWindowPreviousState                     = state,
                    rect => Settings.Default.MainWindowFloatingSizeAndPosition            = rect,
                    pos => Settings.Default.MainWindowDockPosition                        = pos,
                    size => Settings.Default.MainWindowDockSize                           = size,
                    t => Settings.Default.MainWindowFullDockThicknessAsPercentageOfScreen = t,
                    t => Settings.Default.MainWindowCollapsedDockThicknessAsPercentageOfFullDockThickness = t);

                errorNotifyingServices.Add(mainWindowManipulationService);

                mainViewModel = new MainViewModel(
                    audioService, calibrationService, dictionaryService, keyStateService,
                    suggestionService, capturingStateManager, lastMouseActionStateManager,
                    inputService, keyboardOutputService, mouseOutputService, mainWindowManipulationService, errorNotifyingServices);

                mainWindow.MainView.DataContext = mainViewModel;

                //Setup actions to take once main view is loaded (i.e. the view is ready, so hook up the services which kicks everything off)
                Action postMainViewLoaded = mainViewModel.AttachServiceEventHandlers;
                if (mainWindow.MainView.IsLoaded)
                {
                    postMainViewLoaded();
                }
                else
                {
                    RoutedEventHandler loadedHandler = null;
                    loadedHandler = (s, a) =>
                    {
                        postMainViewLoaded();
                        mainWindow.MainView.Loaded -= loadedHandler; //Ensure this handler only triggers once
                    };
                    mainWindow.MainView.Loaded += loadedHandler;
                }

                //Show the main window
                mainWindow.Show();

                //Display splash screen and check for updates (and display message) after the window has been sized and positioned for the 1st time
                EventHandler sizeAndPositionInitialised = null;
                sizeAndPositionInitialised = async(_, __) =>
                {
                    mainWindowManipulationService.SizeAndPositionInitialised -= sizeAndPositionInitialised; //Ensure this handler only triggers once
                    await ShowSplashScreen(inputService, audioService, mainViewModel);

                    inputService.RequestResume(); //Start the input service
                    await CheckForUpdates(inputService, audioService, mainViewModel);
                };
                if (mainWindowManipulationService.SizeAndPositionIsInitialised)
                {
                    sizeAndPositionInitialised(null, null);
                }
                else
                {
                    mainWindowManipulationService.SizeAndPositionInitialised += sizeAndPositionInitialised;
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error starting up application", ex);
                throw;
            }
        }
Beispiel #3
0
        public MainWindow CreateKeyboard()
        {
            ////Apply resource language (and listen for changes)
            Action <OptiKey.Enums.Languages> applyResourceLanguage = language => OptiKey.Properties.Resources.Culture = language.ToCultureInfo();

            Settings.Default.OnPropertyChanges(s => s.UiLanguage).Subscribe(applyResourceLanguage);
            applyResourceLanguage(Settings.Default.UiLanguage);

            MainWindow mainWindow;

            //Define MainViewModel before services so I can setup a delegate to call into the MainViewModel
            //This is to work around the fact that the MainViewModel is created after the services.
            OptiKey.UI.ViewModels.MainViewModel mainViewModel = null;
            Action <KeyValue> fireKeySelectionEvent           = kv =>
            {
                if (mainViewModel != null) //Access to modified closure is a good thing here, for once!
                {
                    mainViewModel.FireKeySelectionEvent(kv);
                }
            };

            //Create services
            var                          errorNotifyingServices      = new List <INotifyErrors>();
            IAudioService                audioService                = new AudioService();
            IDictionaryService           dictionaryService           = new DictionaryService(OptiKey.Enums.AutoCompleteMethods.NGram);
            IPublishService              publishService              = new PublishService();
            ISuggestionStateService      suggestionService           = new SuggestionStateService();
            ICalibrationService          calibrationService          = OptiKey.App.CreateCalibrationService();
            ICapturingStateManager       capturingStateManager       = new CapturingStateManager(audioService);
            ILastMouseActionStateManager lastMouseActionStateManager = new LastMouseActionStateManager();
            IKeyStateService             keyStateService             = new KeyStateService(suggestionService, capturingStateManager, lastMouseActionStateManager, calibrationService, fireKeySelectionEvent);
            IInputService                inputService                = CreateInputService(keyStateService, dictionaryService, audioService, calibrationService, capturingStateManager, errorNotifyingServices);
            IKeyboardOutputService       keyboardOutputService       = new KeyboardOutputService(keyStateService, suggestionService, publishService, dictionaryService, fireKeySelectionEvent);
            IMouseOutputService          mouseOutputService          = new MouseOutputService(publishService);

            errorNotifyingServices.Add(audioService);
            errorNotifyingServices.Add(dictionaryService);
            errorNotifyingServices.Add(publishService);
            errorNotifyingServices.Add(inputService);

            //Release keys on application exit
            //ReleaseKeysOnApplicationExit(keyStateService, publishService);

            //Compose UI
            mainWindow = new MainWindow(audioService, dictionaryService, inputService, keyStateService);

            //for testing just hard code the size and position of the mainWindow

            ////get the size of the screen
            Rectangle screenSize   = Screen.PrimaryScreen.Bounds;
            double    windowWidth  = screenSize.Width - toolBarOffset;
            double    windowHeight = screenSize.Height * 0.40;

            Rect windowDimensions = new Rect(0, (screenSize.Height - windowHeight) - taskBarOffset, windowWidth, windowHeight);

            //Settings.Default.MainWindowFloatingSizeAndPosition = windowDimensions;

            IWindowManipulationService mainWindowManipulationService = new WindowManipulationService(
                mainWindow,
                () => Settings.Default.MainWindowOpacity,
                () => Settings.Default.MainWindowState,
                () => Settings.Default.MainWindowPreviousState,
                //() => Settings.Default.MainWindowFloatingSizeAndPosition,
                () => windowDimensions,
                () => Settings.Default.MainWindowDockPosition,
                () => Settings.Default.MainWindowDockSize,
                () => Settings.Default.MainWindowFullDockThicknessAsPercentageOfScreen,
                () => Settings.Default.MainWindowCollapsedDockThicknessAsPercentageOfFullDockThickness,
                () => Settings.Default.MainWindowMinimisedPosition,
                o => Settings.Default.MainWindowOpacity                               = o,
                state => Settings.Default.MainWindowState                             = state,
                state => Settings.Default.MainWindowPreviousState                     = state,
                rect => Settings.Default.MainWindowFloatingSizeAndPosition            = rect,
                pos => Settings.Default.MainWindowDockPosition                        = pos,
                size => Settings.Default.MainWindowDockSize                           = size,
                t => Settings.Default.MainWindowFullDockThicknessAsPercentageOfScreen = t,
                t => Settings.Default.MainWindowCollapsedDockThicknessAsPercentageOfFullDockThickness = t);

            errorNotifyingServices.Add(mainWindowManipulationService);
            mainWindow.WindowManipulationService = mainWindowManipulationService;

            mainViewModel = new MainViewModel(
                audioService, calibrationService, dictionaryService, keyStateService,
                suggestionService, capturingStateManager, lastMouseActionStateManager,
                inputService, keyboardOutputService, mouseOutputService, mainWindowManipulationService, errorNotifyingServices);

            mainWindow.MainView.DataContext = mainViewModel;

            //Setup actions to take once main view is loaded (i.e. the view is ready, so hook up the services which kicks everything off)
            Action postMainViewLoaded = mainViewModel.AttachServiceEventHandlers;

            if (mainWindow.MainView.IsLoaded)
            {
                postMainViewLoaded();
            }
            else
            {
                RoutedEventHandler loadedHandler = null;
                loadedHandler = (s, a) =>
                {
                    postMainViewLoaded();
                    mainWindow.MainView.Loaded -= loadedHandler; //Ensure this handler only triggers once
                };
                mainWindow.MainView.Loaded += loadedHandler;
            }

            //Show the main window
            ResourceDictionary resourses = new ResourceDictionary();

            mainWindow.Resources = resourses;
            ThemeResourceDictionary themeDictionary = new ThemeResourceDictionary();

            resourses.MergedDictionaries.Add(themeDictionary);

            //setSize

            //Display splash screen and check for updates (and display message) after the window has been sized and positioned for the 1st time
            EventHandler sizeAndPositionInitialised = null;

            sizeAndPositionInitialised = async(_, __) =>
            {
                mainWindowManipulationService.SizeAndPositionInitialised -= sizeAndPositionInitialised; //Ensure this handler only triggers once
                inputService.RequestResume();                                                           //Start the input service
            };
            if (mainWindowManipulationService.SizeAndPositionIsInitialised)
            {
                sizeAndPositionInitialised(null, null);
            }
            else
            {
                mainWindowManipulationService.SizeAndPositionInitialised += sizeAndPositionInitialised;
            }


            mainWindow.Closed           += (_, __) => ReleaseKeysOnApplicationExit(keyStateService, publishService);
            mainWindow.IsVisibleChanged += (_, __) => ReleaseKeysOnApplicationExit(keyStateService, publishService);

            return(mainWindow);
        }