| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-09-03 11:14:37 +01:00
										 |  |  |  * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 16:27:10 +01:00
										 |  |  | #include "StylePropertiesModel.h"
 | 
					
						
							| 
									
										
										
										
											2020-06-13 19:22:30 +02:00
										 |  |  | #include <AK/QuickSort.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-04 01:15:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-30 11:15:31 +02:00
										 |  |  | namespace WebView { | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 16:27:10 +01:00
										 |  |  | StylePropertiesModel::StylePropertiesModel(JsonObject properties) | 
					
						
							|  |  |  |     : m_properties(move(properties)) | 
					
						
							| 
									
										
										
										
											2020-01-04 01:15:42 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-08-24 16:27:10 +01:00
										 |  |  |     m_properties.for_each_member([&](auto& property_name, auto& property_value) { | 
					
						
							| 
									
										
										
										
											2020-05-28 14:20:12 +02:00
										 |  |  |         Value value; | 
					
						
							| 
									
										
										
										
											2021-08-24 16:27:10 +01:00
										 |  |  |         value.name = property_name; | 
					
						
							| 
									
										
										
										
											2020-05-28 14:20:12 +02:00
										 |  |  |         value.value = property_value.to_string(); | 
					
						
							|  |  |  |         m_values.append(value); | 
					
						
							| 
									
										
										
										
											2020-01-04 01:15:42 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-06-13 19:22:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     quick_sort(m_values, [](auto& a, auto& b) { return a.name < b.name; }); | 
					
						
							| 
									
										
										
										
											2020-01-04 01:15:42 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-14 13:21:51 -06:00
										 |  |  | StylePropertiesModel::~StylePropertiesModel() = default; | 
					
						
							| 
									
										
										
										
											2021-08-24 16:27:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | int StylePropertiesModel::row_count(GUI::ModelIndex const&) const | 
					
						
							| 
									
										
										
										
											2020-01-04 01:15:42 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return m_values.size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String StylePropertiesModel::column_name(int column_index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (column_index) { | 
					
						
							|  |  |  |     case Column::PropertyName: | 
					
						
							|  |  |  |         return "Name"; | 
					
						
							|  |  |  |     case Column::PropertyValue: | 
					
						
							|  |  |  |         return "Value"; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-01-04 01:15:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-08-24 16:27:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | GUI::Variant StylePropertiesModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const | 
					
						
							| 
									
										
										
										
											2020-01-04 01:15:42 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& value = m_values[index.row()]; | 
					
						
							| 
									
										
										
										
											2020-08-16 16:00:07 +02:00
										 |  |  |     if (role == GUI::ModelRole::Display) { | 
					
						
							| 
									
										
										
										
											2020-01-04 01:15:42 +01:00
										 |  |  |         if (index.column() == Column::PropertyName) | 
					
						
							|  |  |  |             return value.name; | 
					
						
							|  |  |  |         if (index.column() == Column::PropertyValue) | 
					
						
							|  |  |  |             return value.value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 18:20:20 +01:00
										 |  |  | Vector<GUI::ModelIndex> StylePropertiesModel::matches(StringView searching, unsigned flags, GUI::ModelIndex const& parent) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_values.is_empty()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     Vector<GUI::ModelIndex> found_indices; | 
					
						
							|  |  |  |     for (auto it = m_values.begin(); !it.is_end(); ++it) { | 
					
						
							|  |  |  |         GUI::ModelIndex index = this->index(it.index(), Column::PropertyName, parent); | 
					
						
							|  |  |  |         if (!string_matches(data(index, GUI::ModelRole::Display).as_string(), searching, flags)) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         found_indices.append(index); | 
					
						
							|  |  |  |         if (flags & FirstMatchOnly) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return found_indices; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |