Beispiel #1
0
        public UIActionHandlerFunc TryEditCurrentLanguage(MdiTabControl dockHostControl) => perform =>
        {
            // Only enable in developer mode.
            if (!GetSetting(DeveloperMode))
            {
                return(UIActionVisibility.Hidden);
            }

            // Cannot edit built-in localizer.
            if (!(CurrentLocalizer is FileLocalizer fileLocalizer))
            {
                return(UIActionVisibility.Hidden);
            }

            if (perform)
            {
                OpenOrActivateSettingsEditor(
                    dockHostControl,
                    languageEditorBoxes.GetOrAdd(fileLocalizer.LanguageFile.AbsoluteFilePath, key => new Box <SettingsEditor>(null)),
                    () =>
                {
                    // Generate translations into language file if empty.
                    string initialTextGenerator()
                    {
                        var settingCopy = new SettingCopy(fileLocalizer.LanguageFile.Settings.Schema);

                        // Fill with built-in default dictionary, or if not provided, an empty dictionary.
                        settingCopy.AddOrReplace(
                            Localizers.Translations,
                            defaultLocalizerDictionary ?? new Dictionary <LocalizedStringKey, string>());

                        // And overwrite the existing language file with this.
                        // This doesn't preserve trivia such as comments, whitespace, or even the order in which properties are given.
                        return(fileLocalizer.LanguageFile.GenerateJson(
                                   settingCopy.Commit(),
                                   SettingWriterOptions.SuppressSettingComments));
                    }

                    return(CreateSettingsEditor(
                               SyntaxEditorCodeAccessOption.FixedFile,
                               fileLocalizer.LanguageFile,
                               initialTextGenerator,
                               null));
                });
            }

            return(UIActionVisibility.Enabled);
        };
Beispiel #2
0
        public UIActionHandlerFunc TryShowDefaultSettingsFile(MdiTabControl dockHostControl) => perform =>
        {
            if (perform)
            {
                OpenOrActivateSettingsEditor(
                    dockHostControl,
                    defaultSettingsEditorBox,
                    () => CreateSettingsEditor(
                        GetSetting(DeveloperMode) ? SyntaxEditorCodeAccessOption.FixedFile : SyntaxEditorCodeAccessOption.ReadOnly,
                        DefaultSettings,
                        () => DefaultSettings.GenerateJson(DefaultSettings.Settings, SettingWriterOptions.Default),
                        SharedSettings.DefaultSettingsAutoSave));
            }

            return(UIActionVisibility.Enabled);
        };
Beispiel #3
0
        internal void OpenOrActivateSettingsEditor(MdiTabControl dockHostControl, Box <SettingsEditor> settingsEditor, Func <SettingsEditor> settingsEditorConstructor)
        {
            if (settingsEditor.Value == null)
            {
                // Rely on exception handler in call stack, so no try-catch here.
                settingsEditor.Value = settingsEditorConstructor();

                if (settingsEditor.Value != null)
                {
                    dockHostControl.TabPages.Add(new MdiTabPage <SettingsEditor>(settingsEditor.Value));
                    settingsEditor.Value.Disposed += (_, __) => settingsEditor.Value = null;
                }
            }

            if (settingsEditor.Value != null)
            {
                settingsEditor.Value.EnsureActivated();
            }
        }
Beispiel #4
0
        public UIActionHandlerFunc TryEditPreferencesFile(MdiTabControl dockHostControl) => perform =>
        {
            if (perform)
            {
                OpenOrActivateSettingsEditor(
                    dockHostControl,
                    localSettingsEditorBox,
                    () =>
                {
                    // If the file doesn't exist yet, generate a local settings file with a commented out copy
                    // of the default settings to serve as an example, and to show which settings are available.
                    string initialTextGenerator()
                    {
                        SettingCopy localSettingsExample = new SettingCopy(LocalSettings.Settings.Schema);

                        var defaultSettingsObject = DefaultSettings.Settings;
                        foreach (var property in localSettingsExample.Schema.AllProperties)
                        {
                            if (defaultSettingsObject.Schema.TryGetProperty(property.Name, out SettingProperty defaultSettingProperty) &&
                                defaultSettingsObject.TryGetRawValue(defaultSettingProperty, out PValue sourceValue))
                            {
                                localSettingsExample.AddOrReplaceRaw(property, sourceValue);
                            }
                        }

                        return(LocalSettings.GenerateJson(
                                   localSettingsExample.Commit(),
                                   SettingWriterOptions.CommentOutProperties));
                    }

                    return(CreateSettingsEditor(
                               SyntaxEditorCodeAccessOption.FixedFile,
                               LocalSettings,
                               initialTextGenerator,
                               SharedSettings.PreferencesAutoSave));
                });
            }

            return(UIActionVisibility.Enabled);
        };