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

Support gui_definition.tuw as a default JSON path #76

Merged
merged 3 commits into from
Feb 2, 2025
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: 1 addition & 1 deletion examples/get_start/minimal/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Minimal GUI

Tuw will read `./gui_definition.json` (or `./gui_definition.jsonc`) when launching the executable. You can define a GUI in the JSON file.
Tuw reads `./gui_definition.json` (, `.jsonc`, or `.tuw`) when launching the executable. You can define a GUI in the JSON file.

The following JSON is for a minimal GUI that you can create.
It has only a button to echo `Hello!`.
Expand Down
5 changes: 4 additions & 1 deletion include/main_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct MenuData {

#define EMPTY_DOCUMENT rapidjson::Document(rapidjson::kObjectType)

// Get "gui_definition.*"
noex::string GetDefaultJsonPath();

// Main window
class MainFrame {
private:
Expand Down Expand Up @@ -61,7 +64,7 @@ class MainFrame {

void Initialize(const rapidjson::Document& definition,
const rapidjson::Document& config,
const char* json_path) noexcept;
noex::string json_path) noexcept;

void UpdatePanel(unsigned definition_id) noexcept;
void OpenURL(int id) noexcept;
Expand Down
7 changes: 1 addition & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ int main(int argc, char* argv[]) noexcept {

if (json_path.empty()) {
if (cmd_int == CMD_MERGE)
json_path = "gui_definition.json";
json_path = GetDefaultJsonPath();
else
json_path = "new.json";
}
Expand All @@ -299,11 +299,6 @@ int main(int argc, char* argv[]) noexcept {

switch (cmd_int) {
case CMD_MERGE:
if (!envuFileExists(json_path.c_str()) &&
envuFileExists((json_path + "c").c_str())) {
// Not found .json but found .jsonc
json_path.push_back('c');
}
result = Merge(exe_path, json_path, new_exe_path, force);
break;
case CMD_SPLIT:
Expand Down
34 changes: 18 additions & 16 deletions src/main_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
#include "string_utils.h"
#include "tuw_constants.h"

constexpr char DEF_JSON[] = "gui_definition.json";
constexpr char DEF_JSONC[] = "gui_definition.jsonc";
noex::string GetDefaultJsonPath() {
noex::string def_name = "gui_definition.";
for (const char* ext : { "jsonc", "tuw" }) {
noex::string json_path = def_name + ext;
if (envuFileExists(json_path.c_str()))
return json_path;
}
return def_name + "json";
}

// Main window
void MainFrame::Initialize(const rapidjson::Document& definition,
const rapidjson::Document& config,
const char* json_path) noexcept {
noex::string json_path) noexcept {
PrintFmt("%s v%s by %s\n", tuw_constants::TOOL_NAME,
tuw_constants::VERSION, tuw_constants::AUTHOR);
PrintFmt(tuw_constants::LOGO);
Expand All @@ -27,21 +34,16 @@ void MainFrame::Initialize(const rapidjson::Document& definition,

bool exists_external_json = true;
noex::string workdir;
if (json_path) {
char* full_json_path = envuGetFullPath(json_path);
if (!json_path.empty()) {
char* full_json_path = envuGetFullPath(json_path.c_str());
workdir = envuStr(envuGetDirectory(full_json_path));
envuFree(full_json_path);
} else {
workdir = envuStr(envuGetDirectory(exe_path.c_str()));
// Find gui_definition.json
json_path = DEF_JSON;
if (!envuFileExists(json_path)) {
// Find gui_definition.jsonc
if (envuFileExists(DEF_JSONC))
json_path = DEF_JSONC;
}
json_path = GetDefaultJsonPath();
}
exists_external_json = envuFileExists(json_path);
exists_external_json = envuFileExists(json_path.c_str());

json_utils::JsonResult result = JSON_RESULT_OK;
if (!m_definition.IsObject() || m_definition.ObjectEmpty()) {
Expand All @@ -63,12 +65,12 @@ void MainFrame::Initialize(const rapidjson::Document& definition,

if (!result.ok) {
if (exists_external_json) {
PrintFmt("[LoadDefinition] Loaded %s\n", json_path);
PrintFmt("[LoadDefinition] Loaded %s\n", json_path.c_str());
result = json_utils::LoadJson(json_path, m_definition);
if (!result.ok)
m_definition.SetObject();
} else {
result = { false, noex::string(json_path) + " not found." };
result = { false, json_path + " not found." };
}
}
}
Expand Down Expand Up @@ -114,8 +116,8 @@ void MainFrame::Initialize(const rapidjson::Document& definition,
}

if (ignore_external_json) {
noex::string msg = noex::string("WARNING: Using embedded JSON. ") +
json_path + " was ignored.\n";
noex::string msg = "WARNING: Using embedded JSON. " +
json_path + " was ignored.\n";
PrintFmt("[LoadDefinition] %s", msg.c_str());
ShowSuccessDialog(msg, "Warning");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/string_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ TEST(StringTest, GetLastLine) {
expect_tuwstr("last", GetLastLine("last"));
expect_tuwstr("last", GetLastLine("last\n"));
expect_tuwstr("last", GetLastLine("\nlast"));
expect_tuwstr("last", GetLastLine("firs\nsecond\nlast\r\n\r\n"));
expect_tuwstr("last", GetLastLine("first\nsecond\nlast\r\n\r\n"));
}

TEST(StringTest, GetLastLineOneChar) {
Expand Down