Make RadioBox API simpler
This commit is contained in:
		
							parent
							
								
									f4d5f31f2a
								
							
						
					
					
						commit
						a618181120
					
				
					 3 changed files with 23 additions and 43 deletions
				
			
		| 
						 | 
				
			
			@ -23,12 +23,14 @@ RadioBox::RadioBox(
 | 
			
		|||
	QWidget*,
 | 
			
		||||
	const QString &title,
 | 
			
		||||
	int currentValue,
 | 
			
		||||
	const QMap<int, QString> &options,
 | 
			
		||||
	int valueCount,
 | 
			
		||||
	Fn<QString(int)> labelGetter,
 | 
			
		||||
	Fn<void(int)> saveCallback,
 | 
			
		||||
	bool warnRestart)
 | 
			
		||||
: _title(title)
 | 
			
		||||
, _startValue(currentValue)
 | 
			
		||||
, _options(options)
 | 
			
		||||
, _valueCount(valueCount)
 | 
			
		||||
, _labelGetter(labelGetter)
 | 
			
		||||
, _saveCallback(std::move(saveCallback))
 | 
			
		||||
, _warnRestart(warnRestart) {
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -38,13 +40,15 @@ RadioBox::RadioBox(
 | 
			
		|||
	const QString &title,
 | 
			
		||||
	const QString &description,
 | 
			
		||||
	int currentValue,
 | 
			
		||||
	const QMap<int, QString> &options,
 | 
			
		||||
	int valueCount,
 | 
			
		||||
	Fn<QString(int)> labelGetter,
 | 
			
		||||
	Fn<void(int)> saveCallback,
 | 
			
		||||
	bool warnRestart)
 | 
			
		||||
: _title(title)
 | 
			
		||||
, _description(description)
 | 
			
		||||
, _startValue(currentValue)
 | 
			
		||||
, _options(options)
 | 
			
		||||
, _valueCount(valueCount)
 | 
			
		||||
, _labelGetter(labelGetter)
 | 
			
		||||
, _saveCallback(std::move(saveCallback))
 | 
			
		||||
, _warnRestart(warnRestart) {
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -69,13 +73,13 @@ void RadioBox::prepare() {
 | 
			
		|||
 | 
			
		||||
	_group = std::make_shared<Ui::RadiobuttonGroup>(_startValue);
 | 
			
		||||
 | 
			
		||||
	for (auto i = _options.constBegin(); i != _options.constEnd(); ++i) {
 | 
			
		||||
	for (auto i = 0; i != _valueCount; ++i) {
 | 
			
		||||
		content->add(
 | 
			
		||||
			object_ptr<Ui::Radiobutton>(
 | 
			
		||||
				this,
 | 
			
		||||
				_group,
 | 
			
		||||
				i.key(),
 | 
			
		||||
				i.value(),
 | 
			
		||||
				i,
 | 
			
		||||
				_labelGetter(i),
 | 
			
		||||
				st::autolockButton),
 | 
			
		||||
			style::margins(
 | 
			
		||||
				st::boxPadding.left(),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,8 +19,8 @@ namespace Kotato {
 | 
			
		|||
 | 
			
		||||
class RadioBox : public Ui::BoxContent {
 | 
			
		||||
public:
 | 
			
		||||
	RadioBox(QWidget* parent, const QString &title, int currentValue, const QMap<int, QString> &options, Fn<void(int)> saveCallback, bool warnRestart = false);
 | 
			
		||||
	RadioBox(QWidget* parent, const QString &title, const QString &description, int currentValue, const QMap<int, QString> &options, Fn<void(int)> saveCallback, bool warnRestart = false);
 | 
			
		||||
	RadioBox(QWidget* parent, const QString &title, int currentValue, int valueCount, Fn<QString(int)> labelGetter, Fn<void(int)> saveCallback, bool warnRestart = false);
 | 
			
		||||
	RadioBox(QWidget* parent, const QString &title, const QString &description, int currentValue, int valueCount, Fn<QString(int)> labelGetter, Fn<void(int)> saveCallback, bool warnRestart = false);
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
	void prepare() override;
 | 
			
		||||
| 
						 | 
				
			
			@ -31,7 +31,8 @@ private:
 | 
			
		|||
	QString _title;
 | 
			
		||||
	QString _description;
 | 
			
		||||
	int _startValue;
 | 
			
		||||
	QMap<int, QString> _options;
 | 
			
		||||
	int _valueCount;
 | 
			
		||||
	Fn<QString(int)> _labelGetter;
 | 
			
		||||
	Fn<void(int)> _saveCallback;
 | 
			
		||||
	bool _warnRestart = false;
 | 
			
		||||
	std::shared_ptr<Ui::RadiobuttonGroup> _group;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -250,13 +250,6 @@ void SetupKotatoChats(
 | 
			
		|||
		Ui::show(Box<FontsBox>());
 | 
			
		||||
	});
 | 
			
		||||
 | 
			
		||||
	const QMap<int, QString> userpicRoundOptions = {
 | 
			
		||||
		{ 0, UserpicRoundingLabel(0) },
 | 
			
		||||
		{ 1, UserpicRoundingLabel(1) },
 | 
			
		||||
		{ 2, UserpicRoundingLabel(2) },
 | 
			
		||||
		{ 3, UserpicRoundingLabel(3) }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	AddButtonWithLabel(
 | 
			
		||||
		container,
 | 
			
		||||
		tr::ktg_settings_userpic_rounding(),
 | 
			
		||||
| 
						 | 
				
			
			@ -267,7 +260,8 @@ void SetupKotatoChats(
 | 
			
		|||
			tr::ktg_settings_userpic_rounding(tr::now),
 | 
			
		||||
			tr::ktg_settings_userpic_rounding_desc(tr::now),
 | 
			
		||||
			cUserpicCornersType(),
 | 
			
		||||
			userpicRoundOptions,
 | 
			
		||||
			4,
 | 
			
		||||
			UserpicRoundingLabel,
 | 
			
		||||
			[=] (int value) {
 | 
			
		||||
				cSetUserpicCornersType(value);
 | 
			
		||||
				::Kotato::JsonSettings::Write();
 | 
			
		||||
| 
						 | 
				
			
			@ -389,13 +383,6 @@ void SetupKotatoNetwork(not_null<Ui::VerticalLayout*> container) {
 | 
			
		|||
	AddSkip(container);
 | 
			
		||||
	AddSubsectionTitle(container, tr::ktg_settings_network());
 | 
			
		||||
 | 
			
		||||
	const QMap<int, QString> netBoostOptions = {
 | 
			
		||||
		{ 0, NetBoostLabel(0) },
 | 
			
		||||
		{ 1, NetBoostLabel(1) },
 | 
			
		||||
		{ 2, NetBoostLabel(2) },
 | 
			
		||||
		{ 3, NetBoostLabel(3) }
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	AddButtonWithLabel(
 | 
			
		||||
		container,
 | 
			
		||||
		tr::ktg_settings_net_speed_boost(),
 | 
			
		||||
| 
						 | 
				
			
			@ -406,7 +393,8 @@ void SetupKotatoNetwork(not_null<Ui::VerticalLayout*> container) {
 | 
			
		|||
			tr::ktg_net_speed_boost_title(tr::now),
 | 
			
		||||
			tr::ktg_net_speed_boost_desc(tr::now),
 | 
			
		||||
			cNetSpeedBoost(),
 | 
			
		||||
			netBoostOptions,
 | 
			
		||||
			4,
 | 
			
		||||
			NetBoostLabel,
 | 
			
		||||
			[=] (int value) {
 | 
			
		||||
				SetNetworkBoost(value);
 | 
			
		||||
				::Kotato::JsonSettings::Write();
 | 
			
		||||
| 
						 | 
				
			
			@ -530,15 +518,6 @@ void SetupKotatoSystem(
 | 
			
		|||
		}, container->lifetime());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const QMap<int, QString> trayIconOptions = {
 | 
			
		||||
		{ 0, TrayIconLabel(0) },
 | 
			
		||||
		{ 1, TrayIconLabel(1) },
 | 
			
		||||
		{ 2, TrayIconLabel(2) },
 | 
			
		||||
		{ 3, TrayIconLabel(3) },
 | 
			
		||||
		{ 4, TrayIconLabel(4) },
 | 
			
		||||
		{ 5, TrayIconLabel(5) },
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	auto trayIconText = rpl::single(
 | 
			
		||||
		rpl::empty_value()
 | 
			
		||||
	) | rpl::then(
 | 
			
		||||
| 
						 | 
				
			
			@ -557,7 +536,8 @@ void SetupKotatoSystem(
 | 
			
		|||
			tr::ktg_settings_tray_icon(tr::now),
 | 
			
		||||
			tr::ktg_settings_tray_icon_desc(tr::now),
 | 
			
		||||
			cCustomAppIcon(),
 | 
			
		||||
			trayIconOptions,
 | 
			
		||||
			6,
 | 
			
		||||
			TrayIconLabel,
 | 
			
		||||
			[=] (int value) {
 | 
			
		||||
				cSetCustomAppIcon(value);
 | 
			
		||||
				controller->session().data().notifyUnreadBadgeChanged();
 | 
			
		||||
| 
						 | 
				
			
			@ -575,12 +555,6 @@ void SetupKotatoOther(not_null<Ui::VerticalLayout*> container) {
 | 
			
		|||
 | 
			
		||||
	SettingsMenuCSwitch(ktg_settings_show_phone_number, ShowPhoneInDrawer);
 | 
			
		||||
 | 
			
		||||
	const QMap<int, QString> chatIdOptions = {
 | 
			
		||||
		{ 0, ChatIdLabel(0) },
 | 
			
		||||
		{ 1, ChatIdLabel(1) },
 | 
			
		||||
		{ 2, ChatIdLabel(2) },
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	const auto chatIdButton = container->add(
 | 
			
		||||
		object_ptr<Button>(
 | 
			
		||||
			container,
 | 
			
		||||
| 
						 | 
				
			
			@ -603,7 +577,8 @@ void SetupKotatoOther(not_null<Ui::VerticalLayout*> container) {
 | 
			
		|||
			tr::ktg_settings_chat_id(tr::now),
 | 
			
		||||
			tr::ktg_settings_chat_id_desc(tr::now),
 | 
			
		||||
			cShowChatId(),
 | 
			
		||||
			chatIdOptions,
 | 
			
		||||
			3,
 | 
			
		||||
			ChatIdLabel,
 | 
			
		||||
			[=] (int value) {
 | 
			
		||||
				cSetShowChatId(value);
 | 
			
		||||
				::Kotato::JsonSettings::Write();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue