Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement open dialog system from inochi-creator #33

Merged
merged 2 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ stringImportPaths "res"
configuration "barebones" {
platforms "linux"
targetType "executable"

dependency "dportals" version="~>0.1.0"
}


Expand Down
86 changes: 86 additions & 0 deletions source/session/io/package.d
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;
}
}

24 changes: 24 additions & 0 deletions source/session/windows/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import session.scene;
import session.log;
import session.framesend;
import session.plugins;
import session.io;
import inui;
import inui.widgets;
import inui.toolwindow;
Expand All @@ -22,6 +23,8 @@ import inui.utils.link;
import std.format;
import session.ver;

version(linux) import dportals;

private {
struct InochiWindowSettings {
int width;
Expand Down Expand Up @@ -78,6 +81,23 @@ protected:

if (uiImBeginMenu(__("File"))) {

if (uiImMenuItem(__("Open"))) {
const TFD_Filter[] filters = [
{ ["*.inp"], "Inochi2d Puppet (*.inp)" }
];

string parentWindow = "";
version(linux) {
static if (is(typeof(&getWindowHandle))) {
parentWindow = getWindowHandle();
}
}
string file = insShowOpenDialog(filters, _("Open..."), parentWindow);
if (file) loadModels([file]);
}

uiImSeperator();

if (uiImMenuItem(__("Exit"))) {
this.close();
}
Expand Down Expand Up @@ -170,6 +190,8 @@ protected:
}
uiImEndMainMenuBar();
}

version(linux) dpUpdate();
}

override
Expand Down Expand Up @@ -212,5 +234,7 @@ public:
version (InBranding) {
logo = new Texture(ShallowTexture(cast(ubyte[])import("tex/logo.png")));
}

version(linux) dpInit();
}
}