summaryrefslogtreecommitdiff
path: root/meshmc/launcher/ui/pages/global/AppearancePage.cpp
blob: 2ea6717cca21affe54498ef1ad00bccc48c5f48b (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
239
240
241
242
243
244
245
246
/* SPDX-FileCopyrightText: 2026 Project Tick
 * SPDX-FileContributor: Project Tick
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 *   MeshMC - A Custom Launcher for Minecraft
 *   Copyright (C) 2026 Project Tick
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "AppearancePage.h"
#include "ui_AppearancePage.h"

#include "Application.h"
#include "ui/themes/ITheme.h"
#include "ui/themes/ThemeManager.h"
#include "ui/themes/CatPack.h"

#include <QGraphicsOpacityEffect>

static const QStringList previewIconNames = {
	"new",	"centralmods", "viewfolder", "launch",
	"copy", "about",	   "settings",	 "accounts"};

AppearancePage::AppearancePage(QWidget* parent)
	: QWidget(parent), ui(new Ui::AppearancePage)
{
	ui->setupUi(this);

	ui->catPreview->setGraphicsEffect(new QGraphicsOpacityEffect(this));

	connect(ui->widgetStyleComboBox,
			QOverload<int>::of(&QComboBox::currentIndexChanged), this,
			&AppearancePage::applyWidgetTheme);
	connect(ui->iconsComboBox,
			QOverload<int>::of(&QComboBox::currentIndexChanged), this,
			&AppearancePage::applyIconTheme);
	connect(ui->catPackComboBox,
			QOverload<int>::of(&QComboBox::currentIndexChanged), this,
			&AppearancePage::applyCatTheme);

	loadSettings();
}

AppearancePage::~AppearancePage()
{
	delete ui;
}

bool AppearancePage::apply()
{
	applySettings();
	return true;
}

void AppearancePage::applyWidgetTheme(int index)
{
	auto settings = APPLICATION->settings();
	auto originalTheme = settings->get("ApplicationTheme").toString();
	auto newTheme = ui->widgetStyleComboBox->itemData(index).toString();
	if (originalTheme != newTheme) {
		settings->set("ApplicationTheme", newTheme);
		APPLICATION->themeManager()->applyCurrentlySelectedTheme();

		// Sync icon combo to the auto-resolved icon theme
		auto resolvedIcon = settings->get("IconTheme").toString();
		auto iconThemes = APPLICATION->themeManager()->iconThemes();
		QString resolvedFamily;
		for (const auto& entry : iconThemes) {
			if (entry.id == resolvedIcon) {
				resolvedFamily = entry.family;
				break;
			}
		}
		ui->iconsComboBox->blockSignals(true);
		for (int i = 0; i < ui->iconsComboBox->count(); i++) {
			if (ui->iconsComboBox->itemData(i).toString() == resolvedFamily) {
				ui->iconsComboBox->setCurrentIndex(i);
				break;
			}
		}
		ui->iconsComboBox->blockSignals(false);
	}
	updateIconPreview();
}

void AppearancePage::applyIconTheme(int index)
{
	auto settings = APPLICATION->settings();
	auto tm = APPLICATION->themeManager();
	auto family = ui->iconsComboBox->itemData(index).toString();
	auto resolved = tm->resolveIconTheme(family);
	if (resolved.isEmpty())
		return;
	auto originalIconTheme = settings->get("IconTheme").toString();
	if (originalIconTheme != resolved) {
		settings->set("IconTheme", resolved);
		tm->applyCurrentlySelectedTheme();
	}
	updateIconPreview();
}

void AppearancePage::applySettings()
{
	// Theme and icon changes are already persisted live via
	// applyWidgetTheme/applyIconTheme. This is intentionally minimal — settings
	// are saved on combo change.
}

void AppearancePage::loadSettings()
{
	auto settings = APPLICATION->settings();
	auto tm = APPLICATION->themeManager();

	// Block signals during population
	ui->widgetStyleComboBox->blockSignals(true);
	ui->iconsComboBox->blockSignals(true);
	ui->catPackComboBox->blockSignals(true);

	// --- Widget themes (flat list) ---
	ui->widgetStyleComboBox->clear();
	auto currentThemeId = settings->get("ApplicationTheme").toString();
	auto themes = tm->allThemes();
	int themeIdx = 0;

	for (size_t i = 0; i < themes.size(); i++) {
		auto* theme = themes[i];
		ui->widgetStyleComboBox->addItem(theme->name(), theme->id());
		if (!theme->tooltip().isEmpty()) {
			ui->widgetStyleComboBox->setItemData(
				static_cast<int>(i), theme->tooltip(), Qt::ToolTipRole);
		}
		if (theme->id() == currentThemeId) {
			themeIdx = static_cast<int>(i);
		}
	}

	ui->widgetStyleComboBox->setCurrentIndex(themeIdx);

	// --- Icon themes (one entry per family, auto-resolves dark/light) ---
	ui->iconsComboBox->clear();
	auto currentIconTheme = settings->get("IconTheme").toString();
	auto iconThemeList = tm->iconThemes();
	int iconIdx = 0;

	// Find the family of the currently active icon theme
	QString currentFamily;
	for (const auto& entry : iconThemeList) {
		if (entry.id == currentIconTheme) {
			currentFamily = entry.family;
			break;
		}
	}

	// Populate combo with one entry per family
	QSet<QString> seenFamilies;
	int comboIdx = 0;
	for (const auto& entry : iconThemeList) {
		if (seenFamilies.contains(entry.family))
			continue;
		seenFamilies.insert(entry.family);

		QString displayName =
			entry.variant.isEmpty() ? entry.name : entry.family;
		ui->iconsComboBox->addItem(displayName, entry.family);

		if (entry.family == currentFamily) {
			iconIdx = comboIdx;
		}
		comboIdx++;
	}

	ui->iconsComboBox->setCurrentIndex(iconIdx);

	// --- Cat Packs ---
	ui->catPackComboBox->clear();
	auto currentCat = settings->get("BackgroundCat").toString();
	auto cats = tm->getValidCatPacks();
	int catIdx = 0;

	for (int i = 0; i < cats.size(); i++) {
		auto* cat = cats[i];
		QIcon catIcon(cat->path());
		ui->catPackComboBox->addItem(catIcon, cat->name(), cat->id());
		if (cat->id() == currentCat) {
			catIdx = i;
		}
	}

	ui->catPackComboBox->setCurrentIndex(catIdx);

	// Unblock signals
	ui->widgetStyleComboBox->blockSignals(false);
	ui->iconsComboBox->blockSignals(false);
	ui->catPackComboBox->blockSignals(false);

	// Initial previews
	updateIconPreview();
	updateCatPreview();
}

void AppearancePage::updateIconPreview()
{
	QList<QToolButton*> previewButtons = {ui->icon1, ui->icon2, ui->icon3,
										  ui->icon4, ui->icon5, ui->icon6,
										  ui->icon7, ui->icon8};

	for (int i = 0; i < previewButtons.size() && i < previewIconNames.size();
		 i++) {
		previewButtons[i]->setIcon(
			APPLICATION->getThemedIcon(previewIconNames[i]));
	}
}

void AppearancePage::applyCatTheme(int index)
{
	auto settings = APPLICATION->settings();
	auto originalCat = settings->get("BackgroundCat").toString();
	auto newCat = ui->catPackComboBox->itemData(index).toString();
	if (originalCat != newCat) {
		settings->set("BackgroundCat", newCat);
	}
	updateCatPreview();
}

void AppearancePage::updateCatPreview()
{
	QIcon catPackIcon(APPLICATION->themeManager()->getCatPack());
	ui->catPreview->setIcon(catPackIcon);

	auto effect =
		dynamic_cast<QGraphicsOpacityEffect*>(ui->catPreview->graphicsEffect());
	if (effect)
		effect->setOpacity(1.0);
}