-
Notifications
You must be signed in to change notification settings - Fork 892
/
progressindicator.go
103 lines (85 loc) · 2.86 KB
/
progressindicator.go
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
// Copyright 2012 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"unsafe"
)
import (
"github.com/lxn/win"
"syscall"
)
type ProgressIndicator struct {
hwnd win.HWND
taskbarList3 *win.ITaskbarList3
completed uint32
total uint32
state PIState
overlayIcon *Icon
overlayIconDescription string
}
type PIState int
const (
PINoProgress PIState = win.TBPF_NOPROGRESS
PIIndeterminate PIState = win.TBPF_INDETERMINATE
PINormal PIState = win.TBPF_NORMAL
PIError PIState = win.TBPF_ERROR
PIPaused PIState = win.TBPF_PAUSED
)
//newTaskbarList3 precondition: Windows version is at least 6.1 (yes, Win 7 is version 6.1).
func newTaskbarList3(hwnd win.HWND) (*ProgressIndicator, error) {
var classFactoryPtr unsafe.Pointer
if hr := win.CoGetClassObject(&win.CLSID_TaskbarList, win.CLSCTX_ALL, nil, &win.IID_IClassFactory, &classFactoryPtr); win.FAILED(hr) {
return nil, errorFromHRESULT("CoGetClassObject", hr)
}
var taskbarList3ObjectPtr unsafe.Pointer
classFactory := (*win.IClassFactory)(classFactoryPtr)
defer classFactory.Release()
if hr := classFactory.CreateInstance(nil, &win.IID_ITaskbarList3, &taskbarList3ObjectPtr); win.FAILED(hr) {
return nil, errorFromHRESULT("IClassFactory.CreateInstance", hr)
}
return &ProgressIndicator{taskbarList3: (*win.ITaskbarList3)(taskbarList3ObjectPtr), hwnd: hwnd}, nil
}
func (pi *ProgressIndicator) SetState(state PIState) error {
if hr := pi.taskbarList3.SetProgressState(pi.hwnd, (int)(state)); win.FAILED(hr) {
return errorFromHRESULT("ITaskbarList3.setprogressState", hr)
}
pi.state = state
return nil
}
func (pi *ProgressIndicator) State() PIState {
return pi.state
}
func (pi *ProgressIndicator) SetTotal(total uint32) {
pi.total = total
}
func (pi *ProgressIndicator) Total() uint32 {
return pi.total
}
func (pi *ProgressIndicator) SetCompleted(completed uint32) error {
if hr := pi.taskbarList3.SetProgressValue(pi.hwnd, completed, pi.total); win.FAILED(hr) {
return errorFromHRESULT("ITaskbarList3.SetProgressValue", hr)
}
pi.completed = completed
return nil
}
func (pi *ProgressIndicator) Completed() uint32 {
return pi.completed
}
func (pi *ProgressIndicator) SetOverlayIcon(icon *Icon, description string) error {
handle := win.HICON(0)
if icon != nil {
handle = icon.handleForDPI(int(win.GetDpiForWindow(pi.hwnd)))
}
description16, err := syscall.UTF16PtrFromString(description)
if err != nil {
description16 = &[]uint16{0}[0]
}
if hr := pi.taskbarList3.SetOverlayIcon(pi.hwnd, handle, description16); win.FAILED(hr) {
return errorFromHRESULT("ITaskbarList3.SetOverlayIcon", hr)
}
pi.overlayIcon = icon
pi.overlayIconDescription = description
return nil
}