Serenity Operating System
at hosted 152 lines 4.4 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/Badge.h> 28#include <AK/JsonObject.h> 29#include <LibGUI/Layout.h> 30#include <LibGUI/Widget.h> 31 32namespace GUI { 33 34Layout::Layout() 35{ 36} 37 38Layout::~Layout() 39{ 40} 41 42void Layout::notify_adopted(Badge<Widget>, Widget& widget) 43{ 44 if (m_owner == &widget) 45 return; 46 m_owner = widget.make_weak_ptr(); 47} 48 49void Layout::notify_disowned(Badge<Widget>, Widget& widget) 50{ 51 ASSERT(m_owner == &widget); 52 m_owner.clear(); 53} 54 55void Layout::add_entry(Entry&& entry) 56{ 57 m_entries.append(move(entry)); 58 if (m_owner) 59 m_owner->notify_layout_changed({}); 60} 61 62void Layout::add_spacer() 63{ 64 Entry entry; 65 entry.type = Entry::Type::Spacer; 66 add_entry(move(entry)); 67} 68 69void Layout::add_layout(OwnPtr<Layout>&& layout) 70{ 71 Entry entry; 72 entry.type = Entry::Type::Layout; 73 entry.layout = move(layout); 74 add_entry(move(entry)); 75} 76 77void Layout::add_widget(Widget& widget) 78{ 79 Entry entry; 80 entry.type = Entry::Type::Widget; 81 entry.widget = widget.make_weak_ptr(); 82 add_entry(move(entry)); 83} 84 85void Layout::insert_widget_before(Widget& widget, Widget& before_widget) 86{ 87 Entry entry; 88 entry.type = Entry::Type::Widget; 89 entry.widget = widget.make_weak_ptr(); 90 m_entries.insert_before_matching(move(entry), [&](auto& existing_entry) { 91 return existing_entry.type == Entry::Type::Widget && existing_entry.widget.ptr() == &before_widget; 92 }); 93 if (m_owner) 94 m_owner->notify_layout_changed({}); 95} 96 97void Layout::remove_widget(Widget& widget) 98{ 99 m_entries.remove_first_matching([&](auto& entry) { 100 return entry.widget == &widget; 101 }); 102 if (m_owner) 103 m_owner->notify_layout_changed({}); 104} 105 106void Layout::set_spacing(int spacing) 107{ 108 if (m_spacing == spacing) 109 return; 110 m_spacing = spacing; 111 if (m_owner) 112 m_owner->notify_layout_changed({}); 113} 114 115void Layout::set_margins(const Margins& margins) 116{ 117 if (m_margins == margins) 118 return; 119 m_margins = margins; 120 if (m_owner) 121 m_owner->notify_layout_changed({}); 122} 123 124void Layout::save_to(JsonObject& json) 125{ 126 Core::Object::save_to(json); 127 json.set("spacing", m_spacing); 128 129 JsonObject margins_object; 130 margins_object.set("left", m_margins.left()); 131 margins_object.set("right", m_margins.right()); 132 margins_object.set("top", m_margins.top()); 133 margins_object.set("bottom", m_margins.bottom()); 134 json.set("margins", move(margins_object)); 135 136 JsonArray entries_array; 137 for (auto& entry : m_entries) { 138 JsonObject entry_object; 139 if (entry.type == Entry::Type::Widget) { 140 entry_object.set("type", "Widget"); 141 entry_object.set("widget", (FlatPtr)entry.widget.ptr()); 142 } else if (entry.type == Entry::Type::Spacer) { 143 entry_object.set("type", "Spacer"); 144 } else { 145 ASSERT_NOT_REACHED(); 146 } 147 entries_array.append(move(entry_object)); 148 } 149 json.set("entries", move(entries_array)); 150} 151 152}