1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:07:36 +00:00

Browser: Add window to inspect history

This commit is contained in:
Rafał Babiarz 2022-12-10 16:50:47 +01:00 committed by Andrew Kaster
parent ebe925b7c0
commit 3454891d38
11 changed files with 259 additions and 0 deletions

View file

@ -0,0 +1,88 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "HistoryModel.h"
#include <AK/FuzzyMatch.h>
namespace Browser {
void HistoryModel::set_items(AK::Vector<History::URLTitlePair> items)
{
begin_insert_rows({}, m_entries.size(), m_entries.size());
m_entries = items;
end_insert_rows();
did_update(DontInvalidateIndices);
}
void HistoryModel::clear_items()
{
begin_insert_rows({}, m_entries.size(), m_entries.size());
m_entries.clear();
end_insert_rows();
did_update(DontInvalidateIndices);
}
int HistoryModel::row_count(GUI::ModelIndex const& index) const
{
if (!index.is_valid())
return m_entries.size();
return 0;
}
DeprecatedString HistoryModel::column_name(int column) const
{
switch (column) {
case Column::Title:
return "Title";
case Column::URL:
return "URL";
default:
VERIFY_NOT_REACHED();
}
return {};
}
GUI::ModelIndex HistoryModel::index(int row, int column, GUI::ModelIndex const&) const
{
if (static_cast<size_t>(row) < m_entries.size())
return create_index(row, column, &m_entries.at(row));
return {};
}
GUI::Variant HistoryModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
{
if (role != GUI::ModelRole::Display)
return {};
auto const& history_entry = m_entries[index.row()];
switch (index.column()) {
case Column::Title:
return history_entry.title;
case Column::URL:
return history_entry.url.serialize();
}
VERIFY_NOT_REACHED();
}
TriState HistoryModel::data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const
{
auto needle = term.as_string();
if (needle.is_empty())
return TriState::True;
auto const& history_entry = m_entries[index.row()];
auto haystack = DeprecatedString::formatted("{} {}", history_entry.title, history_entry.url.serialize());
if (fuzzy_match(needle, haystack).score > 0)
return TriState::True;
return TriState::False;
}
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include "Applications/Browser/History.h"
#include <AK/Vector.h>
#include <LibGUI/Model.h>
#include <LibGUI/Widget.h>
namespace Browser {
class HistoryModel final : public GUI::Model {
public:
enum Column {
Title,
URL,
__Count,
};
void set_items(AK::Vector<History::URLTitlePair> items);
void clear_items();
virtual int row_count(GUI::ModelIndex const&) const override;
virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return Column::__Count; }
virtual DeprecatedString column_name(int column) const override;
virtual GUI::ModelIndex index(int row, int column = 0, GUI::ModelIndex const& = GUI::ModelIndex()) const override;
virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role = GUI::ModelRole::Display) const override;
virtual TriState data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override;
private:
AK::Vector<History::URLTitlePair> m_entries;
};
}

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "HistoryWidget.h"
#include <Applications/Browser/HistoryWidgetGML.h>
#include <LibGUI/TableView.h>
namespace Browser {
HistoryWidget::HistoryWidget()
{
load_from_gml(history_widget_gml);
m_table_view = find_descendant_of_type_named<GUI::TableView>("history_tableview");
m_textbox = find_descendant_of_type_named<GUI::TextBox>("history_filter_textbox");
m_model = adopt_ref(*new HistoryModel());
m_filtering_model = MUST(GUI::FilteringProxyModel::create(*m_model));
m_filtering_model->set_filter_term(""sv);
m_textbox->on_change = [this] {
m_filtering_model->set_filter_term(m_textbox->text());
if (m_filtering_model->row_count() != 0)
m_table_view->set_cursor(m_filtering_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
};
m_table_view->set_model(m_filtering_model);
m_table_view->set_alternating_row_colors(true);
}
void HistoryWidget::set_history_entries(Vector<History::URLTitlePair> entries)
{
m_model->set_items(entries);
}
void HistoryWidget::clear_history_entries()
{
m_model->clear_items();
}
}

View file

@ -0,0 +1,15 @@
@GUI::Widget {
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
margins: [4]
}
@GUI::TextBox {
name: "history_filter_textbox"
placeholder: "Filter"
}
@GUI::TableView {
name: "history_tableview"
}
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include "Applications/Browser/History.h"
#include "HistoryModel.h"
#include <LibGUI/FilteringProxyModel.h>
#include <LibGUI/TextBox.h>
#include <LibGUI/Widget.h>
namespace Browser {
class HistoryWidget final : public GUI::Widget {
C_OBJECT(HistoryWidget);
public:
virtual ~HistoryWidget() override = default;
void set_history_entries(Vector<History::URLTitlePair> entries);
void clear_history_entries();
private:
HistoryWidget();
RefPtr<GUI::TableView> m_table_view;
RefPtr<GUI::TextBox> m_textbox;
RefPtr<HistoryModel> m_model;
RefPtr<GUI::FilteringProxyModel> m_filtering_model;
};
}