summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/launcher/ui/widgets/WideBar.cpp
blob: 19fc28655e59995c4ed980e0711d00f85f8d48ac (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team
/*
 *  ProjT Launcher - Minecraft Launcher
 *  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, version 3.
 *
 *  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, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include "WideBar.h"

#include <QContextMenuEvent>
#include <QCryptographicHash>
#include <QToolButton>

class ActionButton : public QToolButton
{
	Q_OBJECT
  public:
	ActionButton(QAction* action, QWidget* parent = nullptr, bool use_default_action = false)
		: QToolButton(parent),
		  m_action(action),
		  m_use_default_action(use_default_action)
	{
		setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
		setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
		// workaround for breeze and breeze forks
		setProperty("_kde_toolButton_alignment", Qt::AlignLeft);

		if (m_use_default_action)
		{
			setDefaultAction(action);
		}
		else
		{
			connect(this, &ActionButton::clicked, action, &QAction::trigger);
		}
		connect(action, &QAction::changed, this, &ActionButton::actionChanged);

		actionChanged();
	};
  public slots:
	void actionChanged()
	{
		setEnabled(m_action->isEnabled());
		// better pop up mode
		if (m_action->menu())
		{
			setPopupMode(QToolButton::MenuButtonPopup);
		}
		if (!m_use_default_action)
		{
			setChecked(m_action->isChecked());
			setCheckable(m_action->isCheckable());
			setText(m_action->text());
			setIcon(m_action->icon());
			setToolTip(m_action->toolTip());
			setHidden(!m_action->isVisible());
		}
		setFocusPolicy(Qt::NoFocus);
	}

  private:
	QAction* m_action;
	bool m_use_default_action;
};

WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent)
{
	setFloatable(false);
	setMovable(false);

	setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
	connect(this, &QToolBar::customContextMenuRequested, this, &WideBar::showVisibilityMenu);
}

WideBar::WideBar(QWidget* parent) : QToolBar(parent)
{
	setFloatable(false);
	setMovable(false);

	setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
	connect(this, &QToolBar::customContextMenuRequested, this, &WideBar::showVisibilityMenu);
}

void WideBar::addAction(QAction* action)
{
	BarEntry entry;
	entry.bar_action  = addWidget(new ActionButton(action, this, m_use_default_action));
	entry.menu_action = action;
	entry.type		  = BarEntry::Type::Action;

	m_entries.push_back(entry);

	m_menu_state = MenuState::Dirty;
}

void WideBar::addSeparator()
{
	BarEntry entry;
	entry.bar_action = QToolBar::addSeparator();
	entry.type		 = BarEntry::Type::Separator;

	m_entries.push_back(entry);
}

auto WideBar::getMatching(QAction* act) -> QList<BarEntry>::iterator
{
	auto iter = std::find_if(m_entries.begin(),
							 m_entries.end(),
							 [act](BarEntry const& entry) { return entry.menu_action == act; });

	return iter;
}

void WideBar::insertActionBefore(QAction* before, QAction* action)
{
	auto iter = getMatching(before);
	if (iter == m_entries.end())
		return;

	BarEntry entry;
	entry.bar_action  = insertWidget(iter->bar_action, new ActionButton(action, this, m_use_default_action));
	entry.menu_action = action;
	entry.type		  = BarEntry::Type::Action;

	m_entries.insert(iter, entry);

	m_menu_state = MenuState::Dirty;
}

void WideBar::insertActionAfter(QAction* after, QAction* action)
{
	auto iter = getMatching(after);
	if (iter == m_entries.end())
		return;

	iter++;
	// the action to insert after is present
	// however, the element after it isn't valid
	if (iter == m_entries.end())
	{
		// append the action instead of inserting it
		addAction(action);
		return;
	}

	BarEntry entry;
	entry.bar_action  = insertWidget(iter->bar_action, new ActionButton(action, this, m_use_default_action));
	entry.menu_action = action;
	entry.type		  = BarEntry::Type::Action;

	m_entries.insert(iter, entry);

	m_menu_state = MenuState::Dirty;
}

void WideBar::insertWidgetBefore(QAction* before, QWidget* widget)
{
	auto iter = getMatching(before);
	if (iter == m_entries.end())
		return;

	insertWidget(iter->bar_action, widget);
}

void WideBar::insertSpacer(QAction* action)
{
	auto iter = getMatching(action);
	if (iter == m_entries.end())
		return;

	auto* spacer = new QWidget();
	spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

	BarEntry entry;
	entry.bar_action = insertWidget(iter->bar_action, spacer);
	entry.type		 = BarEntry::Type::Spacer;
	m_entries.insert(iter, entry);
}

void WideBar::insertSeparator(QAction* before)
{
	auto iter = getMatching(before);
	if (iter == m_entries.end())
		return;

	BarEntry entry;
	entry.bar_action = QToolBar::insertSeparator(iter->bar_action);
	entry.type		 = BarEntry::Type::Separator;

	m_entries.insert(iter, entry);
}

QMenu* WideBar::createContextMenu(QWidget* parent, const QString& title)
{
	auto* contextMenu = new QMenu(title, parent);
	for (auto& item : m_entries)
	{
		switch (item.type)
		{
			default:
			case BarEntry::Type::None: break;
			case BarEntry::Type::Separator:
			case BarEntry::Type::Spacer: contextMenu->addSeparator(); break;
			case BarEntry::Type::Action: contextMenu->addAction(item.menu_action); break;
		}
	}
	return contextMenu;
}

static void copyAction(QAction* from, QAction* to)
{
	Q_ASSERT(from);
	Q_ASSERT(to);

	to->setText(from->text());
	to->setIcon(from->icon());
	to->setToolTip(from->toolTip());
}

void WideBar::showVisibilityMenu(QPoint const& position)
{
	if (!m_bar_menu)
	{
		m_bar_menu = std::make_unique<QMenu>(this);
		m_bar_menu->setTearOffEnabled(true);
	}

	if (m_menu_state == MenuState::Dirty)
	{
		for (auto* old_action : m_bar_menu->actions())
			old_action->deleteLater();

		m_bar_menu->clear();

		m_bar_menu->addActions(m_context_menu_actions);

		m_bar_menu->addSeparator()->setText(tr("Customize toolbar actions"));

		for (auto& entry : m_entries)
		{
			if (entry.type != BarEntry::Type::Action)
				continue;

			auto act = new QAction();
			copyAction(entry.menu_action, act);

			act->setCheckable(true);
			act->setChecked(entry.bar_action->isVisible());

			connect(act,
					&QAction::toggled,
					entry.bar_action,
					[this, &entry](bool toggled)
					{
						entry.bar_action->setVisible(toggled);

						// NOTE: This is needed so that disabled actions get reflected on the button when it is made
						// visible.
						static_cast<ActionButton*>(widgetForAction(entry.bar_action))->actionChanged();
					});

			m_bar_menu->addAction(act);
		}

		m_menu_state = MenuState::Fresh;
	}

	m_bar_menu->popup(mapToGlobal(position));
}

void WideBar::addContextMenuAction(QAction* action)
{
	m_context_menu_actions.append(action);
}

QByteArray WideBar::getVisibilityState() const
{
	QByteArray state;

	for (auto const& entry : m_entries)
	{
		if (entry.type != BarEntry::Type::Action)
			continue;

		state.append(entry.bar_action->isVisible() ? '1' : '0');
	}

	state.append(',');
	state.append(getHash());

	return state;
}

void WideBar::setVisibilityState(QByteArray&& state)
{
	auto split = state.split(',');

	auto bits = split.first();
	auto hash = split.last();

	// If the actions changed, we better not try to load the old one to avoid unwanted hiding
	if (!checkHash(hash))
		return;

	qsizetype i = 0;
	for (auto& entry : m_entries)
	{
		if (entry.type != BarEntry::Type::Action)
			continue;
		if (i == bits.size())
			break;

		entry.bar_action->setVisible(bits.at(i++) == '1');

		// NOTE: This is needed so that disabled actions get reflected on the button when it is made visible.
		static_cast<ActionButton*>(widgetForAction(entry.bar_action))->actionChanged();
	}
}

QByteArray WideBar::getHash() const
{
	QCryptographicHash hash(QCryptographicHash::Sha1);
	for (auto const& entry : m_entries)
	{
		if (entry.type != BarEntry::Type::Action)
			continue;
		hash.addData(entry.menu_action->text().toLatin1());
	}

	return hash.result().toBase64();
}

bool WideBar::checkHash(QByteArray const& old_hash) const
{
	return old_hash == getHash();
}

void WideBar::removeAction(QAction* action)
{
	auto iter = getMatching(action);
	if (iter == m_entries.end())
		return;

	iter->bar_action->setVisible(false);
	removeAction(iter->bar_action);
	m_entries.erase(iter);
}

#include "WideBar.moc"