Skip to content

Commit

Permalink
Feature: Preserving Folder Structure (#395)
Browse files Browse the repository at this point in the history
* Modify function to conform to review feedback

* Rename "Autosaves" Pane to "File Handling" to make it more versatile.

* Fixed inconsistent display of "Zoom Mode" configuration

* Refactor problematic code

* Allow user configuration to import preserve folder structure

* Update based on review feedback

* Refactor incMessageBox() with enum args and return value

* refector incImportKeepFolderStructPop() with enum return value

* rename incConvertToTinyfdButton() to incDialogButtonToTinyfd()

* reuse DialogButton enum in dialog.d

* Implement class DialogHandler and remove incMessageBox() to avoid environmental issues

* Revert "Modify function to conform to review feedback"

This reverts commit 8b6adfb.

* Revert "Fixed inconsistent display of "Zoom Mode" configuration"

This reverts commit 1188835.

* Clone `kra-d` git to fix kra import issue

Note: run `dub upgrade` before build

* Duplicate Dialog return value avoids mistakes reuse reference

* Update kra-d version

* Update inmath allow other 1.0.x series version
  • Loading branch information
r888800009 authored Sep 11, 2024
1 parent 3cd1cf9 commit ca5c4f0
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 58 deletions.
4 changes: 2 additions & 2 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ authors "Inochi2D Project"
copyright "Copyright © 2020, Inochi2D Project"
license "BSD 2-clause"
dependency "psd-d" version="~>0.6.1"
dependency "inmath" version="~>1.0.5"
dependency "kra-d" version="~>0.5.5"
dependency "inmath" version="~>1.0.6"
dependency "kra-d" version="~>0.5.6"
dependency "tinyfiledialogs" version="~>0.10.1"
dependency "facetrack-d" version="~>0.7.6"
dependency "bindbc-sdl" version="~>1.1.2"
Expand Down
3 changes: 2 additions & 1 deletion source/creator/core/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import std.exception;
import bindbc.sdl;
import bindbc.opengl;
import inochi2d;
import tinyfiledialogs;
import std.string;
import std.stdio;
import std.conv;
Expand Down Expand Up @@ -629,6 +628,8 @@ void incBeginLoopNoEv() {
incRenderDialogs();
}
incStatusUpdate();

incHandleDialogHandlers();
}

void incSetDefaultLayout() {
Expand Down
7 changes: 6 additions & 1 deletion source/creator/core/settings.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ string incSettingsPath() {
void incSettingsLoad() {
if (exists(incSettingsPath())) {
settings = parseJSON(readText(incSettingsPath()));

// File Handling
// Always ask the user whether to preserve the folder structure during import
// also see incGetKeepLayerFolder()
settings["KeepLayerFolder"] = "Ask";
}
}

Expand Down Expand Up @@ -94,4 +99,4 @@ T incSettingsGet(T)(string name, T default_) {
*/
bool incSettingsCanGet(string name) {
return (name in settings) !is null;
}
}
44 changes: 38 additions & 6 deletions source/creator/io/kra.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ private {
bool incImportShowKRADialog() {
TFD_Filter[] filters = [{ ["*.kra"], "Krita Document (*.kra)" }];
string file = incShowImportDialog(filters, _("Import..."));

if (file) {
incImportKRA(file, IncKRAImportSettings(false));
return true;
}
return false;
return incAskImportKRA(file);
}

class IncKRALayer {
Expand Down Expand Up @@ -142,8 +137,45 @@ struct IncKRAImportSettings {
bool keepStructure = false;
}

/**
Imports a KRA file with user prompt.
also see incAskImportPSD()
*/
bool incAskImportKRA(string file) {
if (!file) return false;

KRALoadHandler handler = new KRALoadHandler(file);
return incKeepStructDialog(handler);
}

class KRALoadHandler : ImportKeepHandler {
private string file;

this(string file) {
super();
this.file = file;
}

override
bool load(AskKeepLayerFolder select) {
switch (select) {
case AskKeepLayerFolder.NotPreserve:
incImportKRA(file, IncKRAImportSettings(false));
return true;
case AskKeepLayerFolder.Preserve:
incImportKRA(file, IncKRAImportSettings(true));
return true;
case AskKeepLayerFolder.Cancel:
return false;
default:
throw new Exception("Invalid selection");
}
}
}

/**
Imports a KRA file.
Note: You should invoke incAskImportKRA for UI interaction.
*/
void incImportKRA(string file, IncKRAImportSettings settings = IncKRAImportSettings.init) {
incNewProject();
Expand Down
122 changes: 86 additions & 36 deletions source/creator/io/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public import creator.io.kra;
public import creator.io.inpexport;
public import creator.io.videoexport;
public import creator.io.imageexport;
import creator.widgets: DialogButtons;
import creator.widgets.dialog;

import tinyfiledialogs;
public import tinyfiledialogs : TFD_Filter;
Expand Down Expand Up @@ -73,6 +75,13 @@ private {
}
}

string incToDString(c_str cstr1) {
if (cstr1 !is null) {
return cast(string) cstr1.fromStringz;
}
return null;
}

string incShowImportDialog(const(TFD_Filter)[] filters, string title, bool multiple = false) {
version (linux) {
try {
Expand All @@ -86,19 +95,11 @@ string incShowImportDialog(const(TFD_Filter)[] filters, string title, bool multi

// FALLBACK: If xdg-desktop-portal is not available then try tinyfiledialogs.
c_str filename = tinyfd_openFileDialog(title.toStringz, "", filters, multiple);
if (filename !is null) {
string file = cast(string) filename.fromStringz;
return file.dup;
}
return null;
return incToDString(filename).dup;
}
} else {
c_str filename = tinyfd_openFileDialog(title.toStringz, "", filters, multiple);
if (filename !is null) {
string file = cast(string) filename.fromStringz;
return file.dup;
}
return null;
return incToDString(filename).dup;
}
}

Expand All @@ -114,15 +115,11 @@ string incShowOpenFolderDialog(string title = "Open...") {

// FALLBACK: If xdg-desktop-portal is not available then try tinyfiledialogs.
c_str filename = tinyfd_selectFolderDialog(title.toStringz, null);
if (filename !is null)
return cast(string) filename.fromStringz.dup;
return null;
return incToDString(filename).dup;
}
} else {
c_str filename = tinyfd_selectFolderDialog(title.toStringz, null);
if (filename !is null)
return cast(string) filename.fromStringz.dup;
return null;
return incToDString(filename).dup;
}
}

Expand All @@ -138,19 +135,11 @@ string incShowOpenDialog(const(TFD_Filter)[] filters, string title = "Open...")

// 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.dup;
}
return null;
return incToDString(filename).dup;
}
} else {
c_str filename = tinyfd_openFileDialog(title.toStringz, "", filters, false);
if (filename !is null) {
string file = cast(string) filename.fromStringz;
return file.dup;
}
return null;
return incToDString(filename).dup;
}
}

Expand All @@ -166,19 +155,11 @@ string incShowSaveDialog(const(TFD_Filter)[] filters, string fname, string title

// FALLBACK: If xdg-desktop-portal is not available then try tinyfiledialogs.
c_str filename = tinyfd_saveFileDialog(title.toStringz, fname.toStringz, filters);
if (filename !is null) {
string file = cast(string) filename.fromStringz;
return file.dup;
}
return null;
return incToDString(filename).dup;
}
} else {
c_str filename = tinyfd_saveFileDialog(title.toStringz, fname.toStringz, filters);
if (filename !is null) {
string file = cast(string) filename.fromStringz;
return file.dup;
}
return null;
return incToDString(filename).dup;
}
}

Expand Down Expand Up @@ -221,3 +202,72 @@ void incCreatePartsFromFiles(string[] files) {
}
}
}

string incGetKeepLayerFolder() {
if (incSettingsCanGet("KeepLayerFolder"))
return incSettingsGet!string("KeepLayerFolder");
else
// also see incSettingsLoad()
// Preserve the original behavior for existing users
return "NotPreserve";
}

bool incSetKeepLayerFolder(string select) {
incSettingsSet("KeepLayerFolder", select);
return true;
}

enum AskKeepLayerFolder {
Preserve, NotPreserve, Cancel
}

const(char)* INC_KEEP_STRUCT_DIALOG_NAME = "ImportKeepFolderStructPopup";

/**
Function for importing pop-up dialog
*/
bool incKeepStructDialog(ImportKeepHandler handler) {
if (incGetKeepLayerFolder() == "Preserve") {
handler.load(AskKeepLayerFolder.Preserve);
} else if (incGetKeepLayerFolder() == "NotPreserve") {
handler.load(AskKeepLayerFolder.NotPreserve);
} else {
incRegisterDialogHandler(handler);

// Show dialog
incDialog(
INC_KEEP_STRUCT_DIALOG_NAME,
__("Import File"),
_("Do you want to preserve the folder structure of the imported file? You can change this in the settings."),
DialogLevel.Warning,
DialogButtons.Yes | DialogButtons.No | DialogButtons.Cancel
);
}

return true;
}

class ImportKeepHandler : DialogHandler {
this () {
super(INC_KEEP_STRUCT_DIALOG_NAME);
}

override
bool onClick(DialogButtons button) {
switch (button) {
case DialogButtons.Cancel:
return this.load(AskKeepLayerFolder.Cancel);
case DialogButtons.Yes:
return this.load(AskKeepLayerFolder.Preserve);
case DialogButtons.No:
return this.load(AskKeepLayerFolder.NotPreserve);
default:
throw new Exception("Invalid button");
}
}

bool load(AskKeepLayerFolder select) {
// override this
return false;
}
}
44 changes: 38 additions & 6 deletions source/creator/io/psd.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ private {
bool incImportShowPSDDialog() {
TFD_Filter[] filters = [{ ["*.psd"], "Photoshop Document (*.psd)" }];
string file = incShowImportDialog(filters, _("Import..."));

if (file) {
incImportPSD(file, IncPSDImportSettings(false));
return true;
}
return false;
return incAskImportPSD(file);
}

class IncPSDLayer {
Expand Down Expand Up @@ -142,8 +137,45 @@ struct IncPSDImportSettings {
bool keepStructure = false;
}

/**
Imports a PSD file with user prompt.
also see incAskImportKRA()
*/
bool incAskImportPSD(string file) {
if (!file) return false;

PSDLoadHandler handler = new PSDLoadHandler(file);
return incKeepStructDialog(handler);
}

class PSDLoadHandler : ImportKeepHandler {
private string file;

this(string file) {
super();
this.file = file;
}

override
bool load(AskKeepLayerFolder select) {
switch (select) {
case AskKeepLayerFolder.NotPreserve:
incImportPSD(file, IncPSDImportSettings(false));
return true;
case AskKeepLayerFolder.Preserve:
incImportPSD(file, IncPSDImportSettings(true));
return true;
case AskKeepLayerFolder.Cancel:
return false;
default:
throw new Exception("Invalid selection");
}
}
}

/**
Imports a PSD file.
Note: You should invoke incAskImportPSD for UI interaction.
*/
void incImportPSD(string file, IncPSDImportSettings settings = IncPSDImportSettings.init) {
incNewProject();
Expand Down
7 changes: 6 additions & 1 deletion source/creator/panels/viewport.d
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,12 @@ protected:

// Allow dragging PSD in to main window
case ".psd":
incImportPSD(file);
incAskImportPSD(file);
break mainLoop;

// Allow dragging KRA in to main window
case ".kra":
incAskImportKRA(file);
break mainLoop;

default:
Expand Down
1 change: 1 addition & 0 deletions source/creator/viewport/vertex/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ void incViewportVertexConfirmBar() {
}

// In case of a warning popup preventing application.
// TODO: if incDialogButtonSelected does not work, we may implement a DialogHandler for this.
if (incDialogButtonSelected("CONFIRM_VERTEX_APPLY") == DialogButtons.Yes) {
incMeshEditApply();
}
Expand Down
Loading

0 comments on commit ca5c4f0

Please sign in to comment.