aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/VisualStudio/StatusBar.cs
blob: 652cf86799874caf3b9f1a1e19fcc37b2e99f88f (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
// 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.Diagnostics;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

using Tasks = System.Threading.Tasks;

namespace QtVsTools.VisualStudio
{
    using Common;

    public static class StatusBar
    {
        private static LazyFactory Lazy { get; } = new();

        private static IVsStatusbar Self => Lazy.Get(() => Self, () =>
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            return VsServiceProvider.GetService<SVsStatusbar, IVsStatusbar>();
        });

        private static uint Cookie { get; set; }

        public static void SetText(string text)
        {
            ThreadHelper.JoinableTaskFactory.Run(async () => await SetTextAsync(text));
        }

        public static async Tasks.Task SetTextAsync(string text)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            Self.IsFrozen(out var isFrozen);
            Self.FreezeOutput(0);
            Self.SetText(text);
            Self.FreezeOutput(isFrozen);
        }

        public static string GetText()
        {
            return ThreadHelper.JoinableTaskFactory.Run(async () => await GetTextAsync());
        }

        public static async Tasks.Task<string> GetTextAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            var status = Self.GetText(out var value);
            Debug.Assert(status == VSConstants.S_OK);
            return value;
        }

        public static void Clear()
        {
            ThreadHelper.JoinableTaskFactory.Run(async () => await ClearAsync());
        }

        public static async Tasks.Task ClearAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            Self.IsFrozen(out var isFrozen);
            Self.FreezeOutput(0);
            var status = Self.Clear();
            Debug.Assert(status == VSConstants.S_OK);
            Self.FreezeOutput(isFrozen);
        }

        public static void Progress(string text, int totalSteps, int currentStep = 0)
        {
            Progress(text, (uint)totalSteps, (uint)currentStep);
        }

        public static void Progress(string text, uint totalSteps, uint currentStep = 0)
        {
            ThreadHelper.JoinableTaskFactory.Run(async () => await ProgressAsync(text, totalSteps,
                currentStep));
        }

        public static async Tasks.Task ProgressAsync(string text, uint totalSteps, uint currentStep = 0)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            if (Self is null)
                return;
            if (Cookie == 0)
                Self.FreezeOutput(0);
            var cookie = Cookie;
            Self.Progress(ref cookie, 1, text, currentStep, totalSteps);
            Cookie = cookie;
        }

        public static void ResetProgress()
        {
            ThreadHelper.JoinableTaskFactory.Run(async () => await ResetProgressAsync());
        }

        public static async Tasks.Task ResetProgressAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            if (Self is null)
                return;
            if (Cookie == 0)
                return;
            var cookie = Cookie;
            Self?.Progress(ref cookie, 0, string.Empty, 0, 0);
            Cookie = 0;
        }
    }
}