aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/Options/QtOptionsPage.cs
blob: aed7b82a78f7d910193c666c543e0dee53bc2f2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using EnvDTE;
using Microsoft.Build.Framework;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace QtVsTools.Core.Options
{
    using Common;
    using Core;
    using VisualStudio;

    using static Common.Converters;
    using static QtVsTools.Common.EnumExt;

    public partial class QtOptionsPage : DialogPage
    {
        public enum QtMsBuild
        {
            [String("QtMsBuild_Path")] Path
        }

        public enum QmlDebug
        {
            [String("QMLDebug_Enable")] Enable,
            [String("QMLDebug_Timeout")] Timeout
        }

        public enum Help
        {
            [String("Help_Preference")] Preference,
            [String("Help_TryOnF1Pressed")] TryOnF1Pressed
        }

        public enum Designer
        {
            [String("Designer_Detached")] Detached
        }

        public enum Linguist
        {
            [String("Linguist_Detached")] Detached
        }

        public enum ResEditor
        {
            [String("ResourceEditor_Detached")] Detached
        }

        public enum BkgBuild
        {
            [String("BkgBuild_ProjectTracking")] ProjectTracking,
            [String("BkgBuild_RunQtTools")] RunQtTools,
            [String("BkgBuild_DebugInfo")] DebugInfo,
            [String("BkgBuild_LoggerVerbosity")] LoggerVerbosity
        }


        public enum Natvis
        {
            [String("LinkNatvis")] Link
        }

        public enum QmlLanguageServer
        {
            [String("QmlLsp_Enable")] Enable,
            [String("QmlLsp_Path")] Path,
            [String("QmlLsp_Log")] Log,
            [String("QmlLsp_LogSize")] LogSize
        }

        public enum Style
        {
            [String("Style_ColorTheme")] ColorTheme,
            [String("Style_CustomStylesheetPath")] StylesheetPath
        }

        public enum Telemetry
        {
            [String("Telemetry_Enable")] Enable
        }

        public enum DevelopmentReleases
        {
            [String("SearchDevRelease")] SearchDevRelease,
            [String("SearchDevReleaseTimeout")] SearchDevReleaseTimeout
        }

        public enum Timeout : uint { Disabled = 0 }

        private class TimeoutConverter : EnumConverter
        {
            public TimeoutConverter(Type t) : base(t)
            { }

            public override bool GetStandardValuesSupported(ITypeDescriptorContext c)
                => true;

            public override bool GetStandardValuesExclusive(ITypeDescriptorContext c)
                => false;

            public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext c)
                => new(new[] { Timeout.Disabled });

            public override object ConvertFrom(
                ITypeDescriptorContext context,
                CultureInfo culture,
                object value)
            {
                uint n = 0;
                try {
                    n = Convert.ToUInt32(value);
                } catch (Exception e) {
                    e.Log();
                }
                return (Timeout)n;
            }

            public override object ConvertTo(
                ITypeDescriptorContext context,
                CultureInfo culture,
                object value,
                Type destinationType)
            {
                if (destinationType == typeof(string))
                    return value?.ToString();
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }

        private class QmlLanguageServerPathEditor : FileNameEditor
        {
            protected override void InitializeDialog(OpenFileDialog openFileDialog)
            {
                openFileDialog.Title = "Select QML Language Server";
                openFileDialog.Filter = "QML Language Server (qmlls)|qmlls;qmlls.exe";
            }
        }

        [Category("Qt/MSBuild")]
        [DisplayName("Path to Qt/MSBuild files")]
        [Description("Corresponds to the QTMSBUILD environment variable")]
        public string QtMsBuildPath { get; set; }

        [Category("QML Debugging")]
        [DisplayName("Process debug events")]
        [Description("Set to false to turn off processing of all debug events by the QML debug "
            + "engine, effectively excluding it from the debugging environment. Disabling the "
            + "QML debug engine will skip debugging of QML code for all projects.")]
        public bool QmlDebuggerEnabledOption
        {
            get => QmlDebuggerEnabled;
            set => QtOptionsPageSettings.Instance.SetValue(() => QmlDebuggerEnabled, value);
        }

        [Settings(QmlDebug.Enable, true)]
        public static bool QmlDebuggerEnabled =>
            QtOptionsPageSettings.Instance.GetValue(() => QmlDebuggerEnabled);

        [Category("QML Debugging")]
        [DisplayName("Runtime connection timeout (ms)")]
        [TypeConverter(typeof(TimeoutConverter))]
        public Timeout QmlDebuggerTimeoutOption
        {
            get => QmlDebuggerTimeout;
            set => QtOptionsPageSettings.Instance.SetValue(() => QmlDebuggerTimeout, value);
        }

        [Settings(QmlDebug.Timeout, (Timeout)60000)]
        public static Timeout QmlDebuggerTimeout =>
            QtOptionsPageSettings.Instance.GetValue(() => QmlDebuggerTimeout);

        [Category("Help")]
        [DisplayName("Keyboard shortcut")]
        [Description("To change keyboard mapping, go to: Tools > Options > Keyboard")]
        [ReadOnly(true)]
        private string QtHelpKeyBinding { get; set; }

        public enum SourcePreference { Online, Offline }

        [Category("Help")]
        [DisplayName("Preferred source")]
        public SourcePreference HelpPreferenceOption
        {
            get => HelpPreference;
            set => QtOptionsPageSettings.Instance.SetValue(() => HelpPreference, value);
        }

        [Settings(Help.Preference, SourcePreference.Online)]
        public static SourcePreference HelpPreference =>
            QtOptionsPageSettings.Instance.GetValue(() => HelpPreference);

        [Category("Help")]
        [DisplayName("Try Qt documentation when F1 is pressed")]
        public bool TryQtHelpOnF1PressedOption
        {
            get => TryQtHelpOnF1Pressed;
            set => QtOptionsPageSettings.Instance.SetValue(() => TryQtHelpOnF1Pressed, value);
        }

        [Settings(Help.TryOnF1Pressed, true)]
        public static bool TryQtHelpOnF1Pressed =>
            QtOptionsPageSettings.Instance.GetValue(() => TryQtHelpOnF1Pressed);

        [Category("Qt Designer")]
        [DisplayName("Run in detached window")]
        public bool DesignerDetachedOption
        {
            get => DesignerDetached;
            set => QtOptionsPageSettings.Instance.SetValue(() => DesignerDetached, value);
        }

        [Settings(Designer.Detached, false)]
        public static bool DesignerDetached =>
            QtOptionsPageSettings.Instance.GetValue(() => DesignerDetached);

        [Category("Qt Linguist")]
        [DisplayName("Run in detached window")]
        public bool LinguistDetachedOption
        {
            get => LinguistDetached;
            set => QtOptionsPageSettings.Instance.SetValue(() => LinguistDetached, value);
        }

        [Settings(Linguist.Detached, false)]
        public static bool LinguistDetached =>
            QtOptionsPageSettings.Instance.GetValue(() => LinguistDetached);

        [Category("Qt Resource Editor")]
        [DisplayName("Run in detached window")]
        public bool ResourceEditorDetachedOption
        {
            get => ResourceEditorDetached;
            set => QtOptionsPageSettings.Instance.SetValue(() => ResourceEditorDetached, value);
        }

        [Settings(ResEditor.Detached, false)]
        public static bool ResourceEditorDetached =>
            QtOptionsPageSettings.Instance.GetValue(() => ResourceEditorDetached);

        [Category("IntelliSense")]
        [DisplayName("Auto project tracking")]
        [Description(
            "Enable this option to automatically keep track of project changes and trigger a"
            + " background build of Qt targets if required to keep IntelliSense updated.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool ProjectTrackingOption
        {
            get => ProjectTracking;
            set => QtOptionsPageSettings.Instance.SetValue(() => ProjectTracking, value);
        }

        [Settings(BkgBuild.ProjectTracking, true)]
        public static bool ProjectTracking =>
            QtOptionsPageSettings.Instance.GetValue(() => ProjectTracking);

        [Category("IntelliSense")]
        [DisplayName("Run Qt tools in background build")]
        [Description(
            "Enable this option to allow all Qt tools (e.g. moc, uic) to be invoked during a"
            + " background update of IntelliSense information. If disabled, only qmake will be"
            + " invoked during background builds, to update a minimal set of Qt build properties.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool BuildRunQtToolsOption
        {
            get => BuildRunQtTools;
            set => QtOptionsPageSettings.Instance.SetValue(() => BuildRunQtTools, value);
        }

        [Settings(BkgBuild.RunQtTools, true)]
        public static bool BuildRunQtTools =>
            QtOptionsPageSettings.Instance.GetValue(() => BuildRunQtTools);

        [Category("IntelliSense")]
        [DisplayName("Show debug information")]
        [Description("Enable this option to display debug information about IntelliSense updates.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool BuildDebugInformationOption
        {
            get => BuildDebugInformation;
            set => QtOptionsPageSettings.Instance.SetValue(() => BuildDebugInformation, value);
        }

        [Settings(BkgBuild.DebugInfo, false)]
        public static bool BuildDebugInformation =>
            QtOptionsPageSettings.Instance.GetValue(() => BuildDebugInformation);

        [Category("IntelliSense")]
        [DisplayName("Verbosity of background build log")]
        [Description("Configure verbosity level of background build log.")]
        public LoggerVerbosity BuildLoggerVerbosityOption
        {
            get => BuildLoggerVerbosity;
            set => QtOptionsPageSettings.Instance.SetValue(() => BuildLoggerVerbosity, value);
        }

        [Settings(BkgBuild.LoggerVerbosity, LoggerVerbosity.Quiet)]
        public static LoggerVerbosity BuildLoggerVerbosity
            => QtOptionsPageSettings.Instance.GetValue(() => BuildLoggerVerbosity);

        [Category("Natvis")]
        [DisplayName("Embed .natvis file into PDB")]
        [Description("Embeds the debugger visualizations (.natvis file) into the PDB file "
            + "generated by LINK. While setting this option, the embedded Natvis file will "
            + "take precedence over user-specific Natvis files(for example the files "
            + @"located in %USERPROFILE%\Documents\Visual Studio 2022\Visualizers).")]
        [TypeConverter(typeof(EnableDisableConverter))]
        [Settings(Natvis.Link, true)]
        public bool LinkNatvis
        {
            get => QtOptionsPageSettings.Instance.GetValue(() => LinkNatvis);
            set => QtOptionsPageSettings.Instance.SetValue(() => LinkNatvis, value);
        }

        [Category("QML Language Server")]
        [DisplayName("Enable")]
        [Description("Connect to a QML Language Server for enhanced code editing experience. "
            + "Restarting Visual Studio might be required after enabling the QML Language Server.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool QmlLanguageServerEnableOption
        {
            get => QmlLanguageServerEnable;
            set => QtOptionsPageSettings.Instance.SetValue(() => QmlLanguageServerEnable, value);
        }

        [Settings(QmlLanguageServer.Enable, false)]
        public static bool QmlLanguageServerEnable
             => QtOptionsPageSettings.Instance.GetValue(() => QmlLanguageServerEnable);

        [Category("QML Language Server")]
        [DisplayName("QML Language Server path")]
        [Description("Select the QML Language Server to use. Leave the path empty to use default "
            + "provided one by the Qt VS Tools extension, which includes the latest features and "
            + "fixes.")]
        [Editor(typeof(QmlLanguageServerPathEditor), typeof(UITypeEditor))]
        public string QmlLanguageServerPathOption
        {
            get => QmlLanguageServerPath;
            set => QtOptionsPageSettings.Instance.SetValue(() => QmlLanguageServerPath, value);
        }

        [Settings(QmlLanguageServer.Path, "")]
        public static string QmlLanguageServerPath
            => QtOptionsPageSettings.Instance.GetValue(() => QmlLanguageServerPath);

        [Category("QML Language Server")]
        [DisplayName("Log")]
        [Description("Write exchanged LSP messages to log file in %TEMP%.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool QmlLanguageServerLogOption
        {
            get => QmlLanguageServerLog;
            set => QtOptionsPageSettings.Instance.SetValue(() => QmlLanguageServerLog, value);
        }

        [Settings(QmlLanguageServer.Log, false)]
        public static bool QmlLanguageServerLog
            => QtOptionsPageSettings.Instance.GetValue(() => QmlLanguageServerLog);

        [Category("QML Language Server")]
        [DisplayName("Log Size")]
        [Description("Maximum size (in KB) of QML LSP log file.")]
        public int QmlLanguageServerLogSizeOption
        {
            get => QmlLanguageServerLogSize;
            set => QtOptionsPageSettings.Instance.SetValue(() => QmlLanguageServerLogSize, value);
        }

        [Settings(QmlLanguageServer.LogSize, 2500)]
        public static int QmlLanguageServerLogSize
            => QtOptionsPageSettings.Instance.GetValue(() => QmlLanguageServerLogSize);

        public enum EditorColorTheme
        {
            Consistent,
            Dark,
            Light
        }

        [Category("Style")]
        [DisplayName("Color theme")]
        [Description("Color theme used in editors (Qt Designer, Qt Linguist, Qt Resource Editor). "
            + "By default consistent with the Visual Studio color theme.")]
        public EditorColorTheme ColorThemeOption
        {
            get => QtOptionsPageSettings.Instance.GetValue(() => ColorTheme);
            set => QtOptionsPageSettings.Instance.SetValue(() => ColorTheme, value);
        }

        [Settings(Style.ColorTheme, EditorColorTheme.Consistent)]
        public static EditorColorTheme ColorTheme =>
            QtOptionsPageSettings.Instance.GetValue(() => ColorTheme);

        [Category("Style")]
        [DisplayName("Path to stylesheet")]
        [Description("Path to stylesheet used in editors (Qt Designer, Qt Linguist, Qt Resource "
            + "Editor).")]
        public string StylesheetPathOption
        {
            get => StylesheetPath;
            set => QtOptionsPageSettings.Instance.SetValue(() => StylesheetPath, value);
        }

        [Settings(Style.StylesheetPath, "")]
        public static string StylesheetPath =>
            QtOptionsPageSettings.Instance.GetValue(() => StylesheetPath);

        [Category("Telemetry")]
        [DisplayName("Enable")]
        [Description("Enable telemetry.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool TelemetryEnableOption
        {
            get => TelemetryEnable;
            set => QtOptionsPageSettings.Instance.SetValue(() => TelemetryEnable, value);
        }

        [Settings(Telemetry.Enable, true)]
        public static bool TelemetryEnable
            => QtOptionsPageSettings.Instance.GetValue(() => TelemetryEnable);

        [Category("Development releases")]
        [DisplayName("Search automatically")]
        [Description("If enabled, runs once every 24 hours after Visual Studio has been idle for "
            + "60 seconds.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool SearchDevReleaseOption
        {
            get => SearchDevRelease;
            set => SearchDevRelease = value;
        }

        [Settings(DevelopmentReleases.SearchDevRelease, false)]
        public static bool SearchDevRelease
        {
            get => QtOptionsPageSettings.Instance.GetValue(() => SearchDevRelease);
            set => QtOptionsPageSettings.Instance.SetValue(() => SearchDevRelease, value);
        }

        [Category("Development releases")]
        [DisplayName("Search timeout in seconds")]
        [Description("Sets the time in seconds to wait before the search request for development "
            + "releases times out.")]
        public int SearchDevReleaseTimeoutOption
        {
            get => SearchDevReleaseTimeout;
            set => QtOptionsPageSettings.Instance.SetValue(() => SearchDevReleaseTimeout, value);
        }

        [Settings(DevelopmentReleases.SearchDevReleaseTimeout, 3)]
        public static int SearchDevReleaseTimeout =>
            QtOptionsPageSettings.Instance.GetValue(() => SearchDevReleaseTimeout);

        public override void LoadSettingsFromStorage()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            try {
                ////////
                // Get Qt Help keyboard shortcut
                //
                var dte = VsServiceProvider.GetService<SDTE, DTE>();
                var f1QtHelpBindings = dte.Commands.Item("QtVSTools.F1QtHelp")?.Bindings as Array;
                var binding = f1QtHelpBindings?.Cast<string>()
                    .Select(x => x.Split(new[] { "::" }, StringSplitOptions.None))
                    .Select(x => new { Scope = x.FirstOrDefault(), Shortcut = x.LastOrDefault() })
                    .FirstOrDefault();
                QtHelpKeyBinding = binding != null ? $"[{binding.Scope}] {binding.Shortcut}" : "";
                QtMsBuildPath = Environment.GetEnvironmentVariable("QTMSBUILD");
            } catch (Exception exception) {
                exception.Log();
            }
        }

        public override void SaveSettingsToStorage()
        {
            try {
                if (!string.IsNullOrEmpty(QtMsBuildPath)
                    && QtMsBuildPath != Environment.GetEnvironmentVariable("QTMSBUILD")) {
                    Environment.SetEnvironmentVariable(
                        "QTMSBUILD", QtMsBuildPath, EnvironmentVariableTarget.User);
                    Environment.SetEnvironmentVariable(
                        "QTMSBUILD", QtMsBuildPath, EnvironmentVariableTarget.Process);
                }

                if (QmlLanguageServerLogSizeOption < 100)
                    QmlLanguageServerLogSizeOption = 100;
                SaveSettingsToStorageStatic();
            } catch (Exception exception) {
                exception.Log();
            }
        }

        public static void SaveSettingsToStorageStatic()
        {
            QtOptionsPageSettings.Instance.SaveSettings();
        }
    }
}