Skip to content

Commit 8b65864

Browse files
committed
LibGUI: Add DynamicWidgetContainer config_key prop
Previously the section_label prop was used as config key. But because that string contains a visual text it contains sometimes spaces. Spaces in config keys are quite ugly and not following the .ini spec I think. With the new require config_key prop you can define a custom config key.
1 parent 19550fe commit 8b65864

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

Base/usr/share/man/man5/GML/Widget/DynamicWidgetContainer.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ Nested containers with persistence:
4040
@GUI::DynamicWidgetContainer {
4141
section_label: "Parent Section"
4242
config_domain: "abc"
43+
config_key: "ParentSection"
4344
with_individual_order: true
4445
detached_size: [200, 640]
4546

4647
@GUI::DynamicWidgetContainer {
4748
section_label: "Section 1"
4849
config_domain: "abc"
50+
config_key: "Section1"
4951

5052
@GUI::Widget {
5153
}
@@ -57,6 +59,7 @@ Nested containers with persistence:
5759
@GUI::DynamicWidgetContainer {
5860
section_label: "Section 2"
5961
config_domain: "abc"
62+
config_key: "Section2"
6063

6164
@GUI::Widget {
6265
}

Userland/Applications/HexEditor/HexEditorWidget.gml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
visible: false
2626
section_label: "Panels"
2727
config_domain: "HexEditor"
28+
config_key: "Panels"
2829
preferred_height: "grow"
2930
show_controls: false
3031

3132
@GUI::DynamicWidgetContainer {
3233
name: "search_results_container"
3334
section_label: "Search Results"
3435
config_domain: "HexEditor"
36+
config_key: "SearchResults"
3537
preferred_height: "grow"
3638
visible: false
3739

@@ -44,6 +46,7 @@
4446
name: "value_inspector_container"
4547
section_label: "Value Inspector"
4648
config_domain: "HexEditor"
49+
config_key: "ValueInspector"
4750
preferred_height: "grow"
4851
visible: false
4952

@@ -75,6 +78,7 @@
7578
name: "annotations_container"
7679
section_label: "Annotations"
7780
config_domain: "HexEditor"
81+
config_key: "Annotations"
7882
preferred_height: "grow"
7983
visible: false
8084

Userland/Applications/PixelPaint/PixelPaintWindow.gml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
@GUI::DynamicWidgetContainer {
4747
section_label: "Editor Panels"
4848
config_domain: "PixelPaint"
49+
config_key: "EditorPanels"
4950
with_individual_order: true
5051
fixed_width: 200
5152
preferred_height: "grow"
@@ -54,6 +55,7 @@
5455
@GUI::DynamicWidgetContainer {
5556
section_label: "Layers"
5657
config_domain: "PixelPaint"
58+
config_key: "Layers"
5759
preferred_height: "grow"
5860
detached_size: [250, 600]
5961

@@ -76,6 +78,7 @@
7678
@GUI::DynamicWidgetContainer {
7779
section_label: "Color Visualizations"
7880
config_domain: "PixelPaint"
81+
config_key: "ColorVisualizations"
7982

8083
@GUI::Widget {
8184
layout: @GUI::VerticalBoxLayout {}
@@ -110,6 +113,7 @@
110113
@GUI::DynamicWidgetContainer {
111114
section_label: "Tool Properties"
112115
config_domain: "PixelPaint"
116+
config_key: "ToolProperties"
113117
detached_size: [200, 200]
114118

115119
@PixelPaint::ToolPropertiesWidget {

Userland/Libraries/LibGUI/DynamicWidgetContainer.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ DynamicWidgetContainer::DynamicWidgetContainer(Gfx::Orientation orientation)
2525
VERIFY(orientation == Gfx::Orientation::Vertical);
2626
REGISTER_STRING_PROPERTY("section_label", section_label, set_section_label);
2727
REGISTER_STRING_PROPERTY("config_domain", config_domain, set_config_domain);
28+
REGISTER_STRING_PROPERTY("config_key", config_key, set_config_key);
2829
REGISTER_SIZE_PROPERTY("detached_size", detached_size, set_detached_size);
2930
REGISTER_BOOL_PROPERTY("with_individual_order", is_container_with_individual_order, set_container_with_individual_order);
3031
REGISTER_BOOL_PROPERTY("show_controls", show_controls, set_show_controls);
@@ -123,7 +124,7 @@ void DynamicWidgetContainer::set_view_state(ViewState state)
123124
(void)detach_widgets();
124125

125126
if (persist_state())
126-
Config::write_i32(config_domain(), "DynamicWidgetContainers"sv, section_label(), to_underlying(state));
127+
Config::write_i32(config_domain(), "DynamicWidgetContainers"sv, config_key(), to_underlying(state));
127128
}
128129

129130
void DynamicWidgetContainer::restore_view_state()
@@ -133,9 +134,9 @@ void DynamicWidgetContainer::restore_view_state()
133134

134135
deferred_invoke([&]() {
135136
if (is_container_with_individual_order()) {
136-
auto order_or_error = JsonValue::from_string(Config::read_string(config_domain(), "DynamicWidgetContainers"sv, section_label()));
137+
auto order_or_error = JsonValue::from_string(Config::read_string(config_domain(), "DynamicWidgetContainers"sv, config_key()));
137138
if (order_or_error.is_error() || !order_or_error.value().is_array()) {
138-
Config::remove_key(config_domain(), "DynamicWidgetContainers"sv, section_label());
139+
Config::remove_key(config_domain(), "DynamicWidgetContainers"sv, config_key());
139140
return;
140141
}
141142

@@ -166,7 +167,7 @@ void DynamicWidgetContainer::restore_view_state()
166167
for (auto const& child : new_child_order)
167168
add_child(*child);
168169
} else {
169-
int persisted_state = Config::read_i32(config_domain(), "DynamicWidgetContainers"sv, section_label(), to_underlying(ViewState::Expanded));
170+
int persisted_state = Config::read_i32(config_domain(), "DynamicWidgetContainers"sv, config_key(), to_underlying(ViewState::Expanded));
170171
set_view_state(static_cast<ViewState>(persisted_state));
171172
}
172173
update();
@@ -187,6 +188,14 @@ void DynamicWidgetContainer::set_config_domain(String domain)
187188
restore_view_state();
188189
}
189190

191+
void DynamicWidgetContainer::set_config_key(String key)
192+
{
193+
m_config_key = move(key);
194+
// FIXME: A much better solution would be to call the restore_view_state within a dedicated "initialization finished" method that is called by the gml runtime after that widget is ready.
195+
// We do not have such a method yet.
196+
restore_view_state();
197+
}
198+
190199
void DynamicWidgetContainer::set_detached_size(Gfx::IntSize const size)
191200
{
192201
m_detached_size = { size };
@@ -446,7 +455,7 @@ void DynamicWidgetContainer::swap_widget_positions(NonnullRefPtr<Core::EventRece
446455
for (auto& child : child_containers())
447456
new_widget_order.must_append(child.section_label());
448457

449-
Config::write_string(config_domain(), "DynamicWidgetContainers"sv, section_label(), new_widget_order.serialized<StringBuilder>());
458+
Config::write_string(config_domain(), "DynamicWidgetContainers"sv, config_key(), new_widget_order.serialized<StringBuilder>());
450459
}
451460

452461
void DynamicWidgetContainer::update_control_button_visibility()

Userland/Libraries/LibGUI/DynamicWidgetContainer.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class DynamicWidgetContainer : public Frame {
3434
void set_section_label(String);
3535
StringView config_domain() const& { return m_config_domain; }
3636
void set_config_domain(String);
37-
bool persist_state() const { return !m_config_domain.is_empty(); }
37+
StringView config_key() const& { return m_config_key; }
38+
void set_config_key(String);
39+
bool persist_state() const { return !m_config_domain.is_empty() && !m_config_key.is_empty(); }
3840
void set_detached_size(Gfx::IntSize const);
3941
Gfx::IntSize detached_size() const { return m_detached_size.value(); }
4042
bool has_detached_size() { return m_detached_size.has_value(); }
@@ -50,7 +52,7 @@ class DynamicWidgetContainer : public Frame {
5052

5153
protected:
5254
explicit DynamicWidgetContainer(Gfx::Orientation = Gfx::Orientation::Vertical);
53-
virtual void paint_event(PaintEvent&) override {};
55+
virtual void paint_event(PaintEvent&) override { }
5456
virtual void second_paint_event(PaintEvent&) override;
5557
virtual void resize_event(ResizeEvent&) override;
5658
virtual void child_event(Core::ChildEvent&) override;
@@ -67,6 +69,7 @@ class DynamicWidgetContainer : public Frame {
6769
ViewState m_view_state = ViewState::Expanded;
6870
String m_section_label;
6971
String m_config_domain;
72+
String m_config_key;
7073
bool m_is_container_with_individual_order { false };
7174
bool m_persist_state { false };
7275
bool m_is_dragging { false };

0 commit comments

Comments
 (0)