Comments (10)
This issue is half-fixed -- thanks for doing the legwork @redraskal! The example code in the first comment here will now work, but unfortunately I don't see a way to prevent the underlying WIN32 calls from changing the process's working directory which means concurrent goroutines may still see surprises. I used this code to test that scenario:
package main
import (
"fmt"
"time"
"runtime"
"os"
"github.com/sqweek/dialog"
)
func main() {
runtime.GOMAXPROCS(2)
path, _ := os.Getwd()
fmt.Println("starting dir:", path)
go func() {
ticker := time.NewTicker(1 * time.Second)
for _ = range ticker.C {
p, _ := os.Getwd()
fmt.Println("current dir:", p)
}
}()
file, err := dialog.File().Title("Open").Filter("All Files", "*").Load()
fmt.Println("Selected file/Error:", file, err)
file, err = dialog.File().Title("Save").Filter("All Files", "*").Save()
fmt.Println("Selected file/Error:", file, err)
path, _ = os.Getwd()
fmt.Println("final dir:", path)
}
I've added a note to the readme about this behaviour.
from dialog.
Solved it. In dlgs_windows.go, I changed
d.opf.InitialDir, _ = syscall.UTF16PtrFromString(b.StartDir)
to
d.opf.File, _ = syscall.UTF16PtrFromString(b.StartDir+"\\file")
Now it always opens in the directory I choose with "file" prefilled in the filename box. Thanks for the great library!
Edit: The code above I just realized doesn't work because it sets a new pointer that dialog will not be able to retrieve the chosen path as. See the link below for the simple solution.
if b.StartDir != "" {
tmp,_ := syscall.UTF16FromString(b.StartDir+"\\"+b.InitFilename)
copy(d.buf,tmp)
}
works fine.
https://github.com/HACKERALERT/dialog/blob/master/dlgs_windows.go#L101 for reference.
from dialog.
Solved it. In dlgs_windows.go, I changed
d.opf.InitialDir, _ = syscall.UTF16PtrFromString(b.StartDir)
tod.opf.File, _ = syscall.UTF16PtrFromString(b.StartDir+"\\file")
Now it always opens in the directory I choose with "file" prefilled in the filename box. Thanks for the great library!Edit: The code above I just realized doesn't work because it sets a new pointer that dialog will not be able to retrieve the chosen path as. See the link below for the simple solution.
if b.StartDir != "" { tmp,_ := syscall.UTF16FromString(b.StartDir+"\\"+b.InitFilename) copy(d.buf,tmp) }works fine.
https://github.com/HACKERALERT/dialog/blob/master/dlgs_windows.go#L101 for reference.
I appreciate the work! I haven't had time to look into this issue & would have likely run into it as well in the future.
from dialog.
I found the problem. It seems like dialog
and/or w32
is changing the working directory of the Go app to the parent directory of whatever file is selected via the dialog window.
Since I'm not using an absolute path to "test.exe", it becomes "pathToSelectedFile/test.exe". I also happened to have a "text.exe" in my PATH env variable, so testapp.Start()
doesn't return any errors unless I call it with a relative path like "./test.exe".
Simply changing the working directory back (via os.Chdir) is a simple and straight forward workaround, but I assume this isn't intended behavior?
from dialog.
from dialog.
The issue impacts the openfile call (both load & save)- pushed a fix. Windows is weird.
from dialog.
@sqweek @redraskal I'm still having this issue.
f := dialog.File()
f.Title("Save as:")
f.SetStartDir(filepath.Dir(onlyFiles[0])}
file, _ := f.Save()
The first time the block above runs, all is fine and the start directory is correct. But when I choose to save the file in my Downloads, the next time the block above runs again, it goes to my Downloads instead of the directory I set. Any ideas? Thanks.
from dialog.
Found the issue: https://docs.microsoft.com/en-ca/windows/win32/api/commdlg/ns-commdlg-openfilenamea?redirectedfrom=MSDN, Ctrl+F "The initial directory. The algorithm for selecting the initial directory varies on different platforms.":
Windows 7:
If lpstrInitialDir has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory.
So if I SetStartDir to the same path, the last location where the user chose to save a file will be used instead of what I set.
from dialog.
from dialog.
Ah damn, I almost pointed you in that direction suspecting it was the standard windows behaviour, but then I was like "ah but surely that doesn't apply when you explicitly set the start directory!" Thanks W32API, love being confounded xD
That said, I can understand the reasoning behind it because as a user if I had to open a dialog a bunch of times in a row for files in the same directory, it would be pretty annoying if it reset to some application defined default directory every time?
from dialog.
Related Issues (20)
- GTK3 segmentation fault (fedora) HOT 26
- macOS Big Sur. Loader won't show up. Causes program to hang. HOT 4
- GTK/Pango affecting Gitlab workflow HOT 2
- Message can not to show HOT 2
- [Linux] 'BadGC (invalid GC parameter)' on dialog.File().Load()-call HOT 1
- Package gtk+-3.0 was not found... HOT 1
- linux: wrong positioning messages windows
- Unexpected error including `github.com/AllenDang/giu` HOT 9
- Updated Windows Folder Picker HOT 2
- Pipeline is Failing HOT 4
- MacOS deprecation warnings: setAllowedFileTypes HOT 4
- Application crashes with `GTK+ 2.x symbols detected` when using gio HOT 5
- Macos build errors HOT 2
- MacOS: deprecated warnings at runtime: setAllowedFileTypes HOT 1
- Text Input Available? HOT 3
- setAllowedFileTypes: is deprecated:
- Windows 11: FileOpen filter extensions not working? HOT 2
- Windows: no way to populate "Save as type" for FileSave dialogs? HOT 1
- [OSX] Hangs when showing Message Dialog HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dialog.