aboutsummaryrefslogtreecommitdiff
path: root/core/src/UI/UI_MainWindow.cpp
blob: 62a195e5f8e07e47b12fe65506884c095e929d6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "UI.hpp"

#include "Model/GlobalStates.hpp"
#include "Model/Project.hpp"
#include "UI/Localization.hpp"
#include "UI/States.hpp"

#include <IconsFontAwesome.h>
#include <imgui.h>
#include <imgui_stdlib.h>
#include <portable-file-dialogs.h>
#include <filesystem>
#include <memory>

namespace fs = std::filesystem;

namespace {
void ProjectTab_Normal()
{
	auto ls = LocaleStrings::Instance.get();
	auto& gs = GlobalStates::GetInstance();
	auto& uis = UIState::GetInstance();

	if (ImGui::Button(ls->CloseActiveProject.Get())) {
		uis.CloseCurrentProject();
		return;
	}
	ImGui::SameLine();
	if (ImGui::Button(ls->OpenActiveProjectInFileSystem.Get())) {
		// TODO
	}

	ImGui::Text("%s%s", ls->ActiveProjectName.Get(), uis.CurrentProject->GetName().c_str());
	ImGui::Text("%s%s", ls->ActiveProjectPath.Get(), uis.CurrentProject->GetPathString().c_str());
}

void ProjectTab_NoProject()
{
	auto ls = LocaleStrings::Instance.get();
	auto& gs = GlobalStates::GetInstance();
	auto& uis = UIState::GetInstance();

	bool openedDummy = true;
	bool openErrorDialog = false;
	static std::string projectName;
	static std::string dirName;
	static fs::path dirPath;
	static bool dirNameIsValid = false;

	auto TrySelectPath = [&](fs::path newPath) {
		if (fs::exists(newPath)) {
			dirNameIsValid = true;
			dirName = newPath.string();
			dirPath = std::move(newPath);
		} else {
			dirNameIsValid = false;
		}
	};

	if (ImGui::Button(ls->NewProject.Get())) {
		ImGui::SetNextWindowCentered();
		ImGui::OpenPopup(ls->NewProjectDialogTitle.Get());
	}

	// Make it so that the modal dialog has a close button
	if (ImGui::BeginPopupModal(ls->NewProjectDialogTitle.Get(), &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) {
		ImGui::InputTextWithHint("##ProjectName", ls->NewProjectNameHint.Get(), &projectName);

		if (ImGui::InputTextWithHint("##ProjectPath", ls->NewProjectPathHint.Get(), &dirName)) {
			// Changed, validate value
			TrySelectPath(fs::path(dirName));
		}
		ImGui::SameLine();
		if (ImGui::Button("...")) {
			auto selection = pfd::select_folder(ls->NewProjectPathDialogTitle.Get()).result();
			if (!selection.empty()) {
				TrySelectPath(fs::path(selection));
			}
		}

		if (projectName.empty()) {
			ImGui::ErrorMessage("%s", ls->NewProjectEmptyNameError.Get());
		}
		if (!dirNameIsValid) {
			ImGui::ErrorMessage("%s", ls->NewProjectInvalidPathError.Get());
		}

		ImGui::Spacing();

		if (ImGui::Button(ls->DialogConfirm.Get(), !dirNameIsValid || projectName.empty())) {
			ImGui::CloseCurrentPopup();

			uis.SetCurrentProject(std::make_unique<Project>(std::move(dirPath), std::move(projectName)));

			// Dialog just got closed, reset states
			projectName = "";
			dirName = "";
			dirPath = fs::path{};
			dirNameIsValid = false;
		}

		ImGui::SameLine();
		if (ImGui::Button(ls->DialogCancel.Get())) {
			ImGui::CloseCurrentPopup();
		}

		ImGui::EndPopup();
	}

	ImGui::SameLine();
	if (ImGui::Button(ls->OpenProject.Get())) {
		auto selection = pfd::open_file(ls->OpenProjectDialogTitle.Get()).result();
		if (!selection.empty()) {
			fs::path path(selection[0]);

			try {
				// Project's constructor wants a path to directory containing cplt_project.json
				uis.SetCurrentProject(std::make_unique<Project>(path.parent_path()));
				openErrorDialog = false;
			} catch (const std::exception& e) {
				openErrorDialog = true;
			}
		}
	}

	// TODO cleanup UI
	// Recent projects

	ImGui::Separator();
	ImGui::TextUnformatted(ls->RecentProjects.Get());

	ImGui::SameLine();
	if (ImGui::Button(ls->ClearRecentProjects.Get())) {
		gs.ClearRecentProjects();
	}

	auto& rp = gs.GetRecentProjects();
	// End of vector is the most recently used, so that appending has less overhead
	size_t toRemoveIdx = rp.size();

	if (rp.empty()) {
		ImGui::TextUnformatted(ls->NoRecentProjectsMessage.Get());
	} else {
		for (auto it = rp.rbegin(); it != rp.rend(); ++it) {
			auto& [path, recent] = *it;
			ImGui::TextUnformatted(recent.c_str());

			size_t idx = std::distance(it, rp.rend()) - 1;
			ImGui::PushID(idx);

			ImGui::SameLine();
			if (ImGui::Button(ICON_FA_FOLDER_OPEN)) {
				try {
					uis.SetCurrentProject(std::make_unique<Project>(path));
					openErrorDialog = false;
				} catch (const std::exception& e) {
					openErrorDialog = true;
				}
			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("%s", ls->OpenRecentProjectTooltip.Get());
			}

			ImGui::SameLine();
			if (ImGui::Button(ICON_FA_TRASH)) {
				toRemoveIdx = idx;
			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("%s", ls->DeleteRecentProjectTooltip.Get());
			}

			ImGui::PopID();
		}
	}

	if (toRemoveIdx != rp.size()) {
		gs.RemoveRecentProject(toRemoveIdx);
	}

	if (openErrorDialog) {
		ImGui::SetNextWindowCentered();
		ImGui::OpenPopup(ls->Error.Get());
	}
	if (ImGui::BeginPopupModal(ls->Error.Get(), &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) {
		ImGui::ErrorMessage("%s", ls->InvalidProjectFormat.Get());
		ImGui::EndPopup();
	}
}
} // namespace

void UI::MainWindow()
{
	auto ls = LocaleStrings::Instance.get();
	auto& uis = UIState::GetInstance();

	auto windowSize = ImGui::GetMainViewport()->Size;
	ImGui::SetNextWindowSize({ windowSize.x, windowSize.y });
	ImGui::SetNextWindowPos({ 0, 0 });
	ImGui::Begin("##MainWindow", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
	if (ImGui::BeginTabBar("##MainWindowTabs")) {
		if (ImGui::BeginTabItem(ls->SettingsTab.Get())) {
			UI::SettingsTab();
			ImGui::EndTabItem();
		}

		if (ImGui::BeginTabItem(ls->ProjectTab.Get(), nullptr)) {
			if (uis.CurrentProject) {
				ProjectTab_Normal();
			} else {
				ProjectTab_NoProject();
			}
			ImGui::EndTabItem();
		}
		if (!uis.CurrentProject) {
			// No project open, simply skip all project specific tabs
			goto endTab;
		}

		if (ImGui::BeginTabItem(ls->DatabaseViewTab.Get())) {
			UI::DatabaseViewTab();
			ImGui::EndTabItem();
		}

		if (ImGui::BeginTabItem(ls->ItemsTab.Get())) {
			UI::ItemsTab();
			ImGui::EndTabItem();
		}

		if (ImGui::BeginTabItem(ls->WorkflowsTab.Get())) {
			UI::WorkflowsTab();
			ImGui::EndTabItem();
		}

	endTab:
		ImGui::EndTabBar();
	}
	ImGui::End();
}