Giter VIP home page Giter VIP logo

Comments (10)

sqweek avatar sqweek commented on September 16, 2024 1

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.

HACKERALERT avatar HACKERALERT commented on September 16, 2024 1

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.

redraskal avatar redraskal commented on September 16, 2024 1

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.

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.

PixelOmen avatar PixelOmen commented on September 16, 2024

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.

sqweek avatar sqweek commented on September 16, 2024

from dialog.

redraskal avatar redraskal commented on September 16, 2024

The issue impacts the openfile call (both load & save)- pushed a fix. Windows is weird.

from dialog.

HACKERALERT avatar HACKERALERT commented on September 16, 2024

@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.

HACKERALERT avatar HACKERALERT commented on September 16, 2024

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.

HACKERALERT avatar HACKERALERT commented on September 16, 2024

https://stackoverflow.com/questions/4117222/on-win7-getopenfilename-function-ignores-the-lpstrinitialdir-arguament

from dialog.

sqweek avatar sqweek commented on September 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.