Skip to content

Commit

Permalink
Implement open dialog system from inochi-creator (#33)
Browse files Browse the repository at this point in the history
* Implement open dialog system from creator

* Barebone builds require linux deps too
  • Loading branch information
grillo-delmal authored May 11, 2023
1 parent 032ff82 commit a462922
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
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();
}
}

0 comments on commit a462922

Please sign in to comment.