-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathopen_dialog_darwin.go
113 lines (93 loc) · 2.7 KB
/
open_dialog_darwin.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
// 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 (
"net/url"
"github.com/richardwilkes/toolbox/errs"
"github.com/richardwilkes/unison/internal/ns"
)
type macOpenDialog struct {
dialog ns.OpenPanel
}
func platformNewOpenDialog() OpenDialog {
return &macOpenDialog{dialog: ns.NewOpenPanel()}
}
func (d *macOpenDialog) InitialDirectory() string {
urlStr := d.dialog.DirectoryURL().AbsoluteString()
u, err := url.Parse(urlStr)
if err != nil {
errs.Log(errs.NewWithCause("unable to parse directory URL", err), "url", urlStr)
return ""
}
return u.Path
}
func (d *macOpenDialog) SetInitialDirectory(dir string) {
dirURL := ns.NewFileURL(dir)
defer dirURL.Release()
d.dialog.SetDirectoryURL(dirURL)
}
func (d *macOpenDialog) AllowedExtensions() []string {
allowed := d.dialog.AllowedFileTypes()
defer allowed.Release()
return allowed.ArrayOfStringToStringSlice()
}
func (d *macOpenDialog) SetAllowedExtensions(types ...string) {
types = SanitizeExtensionList(types)
if len(types) != 0 {
d.dialog.SetAllowedFileTypes(ns.NewArrayFromStringSlice(types))
} else {
d.dialog.SetAllowedFileTypes(0)
}
}
func (d *macOpenDialog) CanChooseFiles() bool {
return d.dialog.CanChooseFiles()
}
func (d *macOpenDialog) SetCanChooseFiles(canChoose bool) {
d.dialog.SetCanChooseFiles(canChoose)
}
func (d *macOpenDialog) CanChooseDirectories() bool {
return d.dialog.CanChooseDirectories()
}
func (d *macOpenDialog) SetCanChooseDirectories(canChoose bool) {
d.dialog.SetCanChooseDirectories(canChoose)
}
func (d *macOpenDialog) ResolvesAliases() bool {
return d.dialog.ResolvesAliases()
}
func (d *macOpenDialog) SetResolvesAliases(resolves bool) {
d.dialog.SetResolvesAliases(resolves)
}
func (d *macOpenDialog) AllowsMultipleSelection() bool {
return d.dialog.AllowsMultipleSelection()
}
func (d *macOpenDialog) SetAllowsMultipleSelection(allow bool) {
d.dialog.SetAllowsMultipleSelection(allow)
}
func (d *macOpenDialog) Path() string {
paths := d.Paths()
if len(paths) == 0 {
return ""
}
return paths[0]
}
func (d *macOpenDialog) Paths() []string {
return d.dialog.URLs().ArrayOfURLToStringSlice()
}
func (d *macOpenDialog) RunModal() bool {
active := ActiveWindow()
if active != nil {
active.restoreHiddenCursor()
}
defer func() {
if active != nil && active.IsVisible() {
active.ToFront()
}
}()
return d.dialog.RunModal()
}