Browser: Let the user add/remove bookmarks to the bookmarks bar

This patchset adds a Button to the toolbar (right next to the location field)
with a star icon. The star is white if the currently visited url is not yet
bookmarked and yellow if a bookmark for the url exists.

After adding or removing a bookmark, the bookmark json file is synced to disk.
Therefore, some new pledge/unveil's have been added.
This commit is contained in:
Emanuel Sprung 2020-03-26 20:06:23 +01:00 committed by Andreas Kling
parent 4d50398f02
commit c9059c12dc
Notes: sideshowbarker 2024-07-19 08:06:14 +09:00
5 changed files with 90 additions and 2 deletions

View file

@ -162,3 +162,45 @@ void BookmarksBarWidget::update_content_size()
}
}
}
bool BookmarksBarWidget::contains_bookmark(const String& url)
{
for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
auto item_title = model()->data(model()->index(item_index, 0)).to_string();
auto item_url = model()->data(model()->index(item_index, 1)).to_string();
if (item_url == url) {
return true;
}
}
return false;
}
bool BookmarksBarWidget::remove_bookmark(const String& url)
{
for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
auto item_title = model()->data(model()->index(item_index, 0)).to_string();
auto item_url = model()->data(model()->index(item_index, 1)).to_string();
if (item_url == url) {
auto& json_model = *static_cast<GUI::JsonArrayModel*>(model());
json_model.remove(item_index);
return true;
}
}
return false;
}
bool BookmarksBarWidget::add_bookmark(const String& url, const String& title)
{
Vector<JsonValue> values;
values.append(title);
values.append(url);
auto& json_model = *static_cast<GUI::JsonArrayModel*>(model());
if (json_model.add(move(values))) {
json_model.store();
return true;
}
return false;
}

View file

@ -41,6 +41,10 @@ public:
Function<void(const String&, const String&)> on_bookmark_click;
Function<void(const String&, const String&)> on_bookmark_hover;
bool contains_bookmark(const String& url);
bool remove_bookmark(const String& url);
bool add_bookmark(const String& url, const String& title);
private:
BookmarksBarWidget(const String&, bool enabled);

View file

@ -32,6 +32,7 @@
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MenuBar.h>
#include <LibGUI/StatusBar.h>
@ -42,6 +43,7 @@
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOMTreeModel.h>
#include <LibWeb/Dump.h>
#include <LibWeb/Frame.h>
#include <LibWeb/HtmlView.h>
#include <LibWeb/Layout/LayoutBlock.h>
#include <LibWeb/Layout/LayoutDocument.h>
@ -56,9 +58,11 @@
static const char* home_url = "file:///home/anon/www/welcome.html";
static const char* bookmarks_filename = "/home/anon/bookmarks.json";
static String s_title = "";
int main(int argc, char** argv)
{
if (pledge("stdio shared_buffer accept unix cpath rpath fattr", nullptr) < 0) {
if (pledge("stdio shared_buffer accept unix cpath rpath wpath fattr", nullptr) < 0) {
perror("pledge");
return 1;
}
@ -68,11 +72,23 @@ int main(int argc, char** argv)
// Connect to the ProtocolServer immediately so we can drop the "unix" pledge.
Web::ResourceLoader::the();
if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
if (pledge("stdio shared_buffer accept cpath rpath wpath", nullptr) < 0) {
perror("pledge");
return 1;
}
if (unveil("/home", "rwc") < 0) {
perror("unveil");
return 1;
}
if (unveil("/res", "r") < 0) {
perror("unveil");
return 1;
}
unveil(nullptr, nullptr);
auto window = GUI::Window::construct();
window->set_rect(100, 100, 640, 480);
@ -135,11 +151,36 @@ int main(int argc, char** argv)
html_widget.load(location_box.text());
};
auto& bookmark_button = toolbar.add<GUI::Button>();
bookmark_button.set_button_style(Gfx::ButtonStyle::CoolBar);
bookmark_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-black.png"));
bookmark_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
bookmark_button.set_preferred_size(22, 22);
auto update_bookmark_button = [&](const String& url) {
if (bookmarksbar.contains_bookmark(url)) {
bookmark_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-yellow.png"));
} else {
bookmark_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-contour.png"));
}
};
bookmark_button.on_click = [&] {
auto url = html_widget.main_frame().document()->url().to_string();
if (bookmarksbar.contains_bookmark(url)) {
bookmarksbar.remove_bookmark(url);
} else {
bookmarksbar.add_bookmark(url, s_title);
}
update_bookmark_button(url);
};
html_widget.on_load_start = [&](auto& url) {
location_box.set_text(url.to_string());
if (should_push_loads_to_history)
history.push(url);
update_actions();
update_bookmark_button(url.to_string());
};
html_widget.on_link_click = [&](auto& url) {
@ -151,6 +192,7 @@ int main(int argc, char** argv)
};
html_widget.on_title_change = [&](auto& title) {
s_title = title;
window->set_title(String::format("%s - Browser", title.characters()));
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 612 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 583 B