-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement open dialog system from inochi-creator (#33)
* Implement open dialog system from creator * Barebone builds require linux deps too
- Loading branch information
1 parent
032ff82
commit a462922
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright © 2020-2023, Inochi2D Project | ||
Distributed under the 2-Clause BSD License, see LICENSE file. | ||
Authors: Luna Nielsen | ||
*/ | ||
module session.io; | ||
|
||
import tinyfiledialogs; | ||
public import tinyfiledialogs : TFD_Filter; | ||
import std.string; | ||
import i18n; | ||
|
||
version (linux) { | ||
import dportals.filechooser; | ||
import dportals.promise; | ||
} | ||
|
||
private { | ||
version (linux) { | ||
string uriFromPromise(Promise promise) { | ||
if (promise.success) { | ||
import std.array : replace; | ||
|
||
string uri = promise.value["uris"].data.array[0].str; | ||
uri = uri.replace("%20", " "); | ||
return uri[7 .. $]; | ||
} | ||
return null; | ||
} | ||
|
||
FileFilter[] tfdToFileFilter(const(TFD_Filter)[] filters) { | ||
FileFilter[] out_; | ||
|
||
foreach (filter; filters) { | ||
auto of = FileFilter( | ||
cast(string) filter.description.fromStringz, | ||
[] | ||
); | ||
|
||
foreach (i, pattern; filter.patterns) { | ||
of.items ~= FileFilterItem( | ||
cast(uint) i, | ||
cast(string) pattern.fromStringz | ||
); | ||
} | ||
|
||
out_ ~= of; | ||
} | ||
|
||
return out_; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
Call a file dialog to open a file. | ||
*/ | ||
string insShowOpenDialog(const(TFD_Filter)[] filters, string title = "Open...", string parentWindow = "") { | ||
version (linux) { | ||
try { | ||
FileOpenOptions op; | ||
op.filters = tfdToFileFilter(filters); | ||
auto promise = dpFileChooserOpenFile(parentWindow, title, op); | ||
promise.await(); | ||
return promise.uriFromPromise(); | ||
} catch (Throwable ex) { | ||
|
||
// FALLBACK: If xdg-desktop-portal is not available then try tinyfiledialogs. | ||
c_str filename = tinyfd_openFileDialog(title.toStringz, "", filters, false); | ||
if (filename !is null) { | ||
string file = cast(string) filename.fromStringz; | ||
return file; | ||
} | ||
return null; | ||
} | ||
} else { | ||
c_str filename = tinyfd_openFileDialog(title.toStringz, "", filters, false); | ||
if (filename !is null) { | ||
string file = cast(string) filename.fromStringz; | ||
return file; | ||
} | ||
return null; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters