-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathopen_dialog_windows.go
126 lines (118 loc) · 2.88 KB
/
open_dialog_windows.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2021-2025 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"path/filepath"
"strings"
"github.com/richardwilkes/toolbox/errs"
"github.com/richardwilkes/toolbox/i18n"
"github.com/richardwilkes/unison/internal/w32"
)
type winOpenDialog struct {
fileCommon
}
func platformNewOpenDialog() OpenDialog {
d := &winOpenDialog{}
d.initialize()
return d
}
func (d *winOpenDialog) RunModal() bool {
active := ActiveWindow()
if active != nil {
active.restoreHiddenCursor()
}
defer func() {
if active != nil && active.IsVisible() {
active.ToFront()
}
}()
openDialog := w32.NewOpenDialog()
if openDialog == nil {
errs.Log(errs.New("unable to create open dialog"))
return false
}
if d.initialDir != "" {
openDialog.SetFolder(filepath.Clean(d.initialDir))
}
options := w32.FOSPathMustExist | w32.FOSFileMustExist
if d.canChooseDirs {
options |= w32.FOSPickFolders
}
if d.allowMultipleSelection {
options |= w32.FOSAllowMultiSelect
}
if !d.resolvesAliases {
options |= w32.FOSNoDereferenceLinks
}
openDialog.SetOptions(options)
openDialog.SetFileTypes(d.createFilters())
d.paths = nil
if !openDialog.Show() {
return false
}
d.paths = openDialog.GetResults()
switch len(d.paths) {
case 0:
return false
case 1:
lastWorkingDir = filepath.Dir(d.paths[0])
default:
if d.allowMultipleSelection {
paths := make([]string, len(d.paths)-1)
for i, p := range d.paths {
if i != 0 {
paths[i-1] = filepath.Join(d.paths[0], p)
}
}
d.paths = paths
lastWorkingDir = d.paths[0]
} else {
paths := make([]string, 1)
paths[0] = d.paths[1]
d.paths = paths
lastWorkingDir = filepath.Dir(d.paths[0])
}
}
return true
}
func (d *winOpenDialog) createFilters() []w32.FileFilter {
filters := make([]w32.FileFilter, 0, len(d.extensions)+1)
readable := make([]string, 0, len(d.extensions))
for _, ext := range d.extensions {
if ext != "*" {
readable = append(readable, "*."+ext)
}
}
if len(readable) != 0 {
filters = append(filters, w32.FileFilter{
Name: i18n.Text("All Readable Files"),
Pattern: strings.Join(readable, ";"),
})
}
for _, ext := range d.extensions {
if ext == "*" {
filters = append(filters, w32.FileFilter{
Name: i18n.Text("All Files"),
Pattern: "*.*",
})
} else {
filters = append(filters, w32.FileFilter{
Name: ext + i18n.Text(" Files"),
Pattern: "*." + ext,
})
}
}
if len(d.extensions) == 0 {
filters = append(filters, w32.FileFilter{
Name: i18n.Text("All Files"),
Pattern: "*.*",
})
}
return filters
}