1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:57:43 +00:00

Browser: Add context menu with history for back/forward button

Right clicking on back or forward will now show a context menu with
URLs to navigate to. Also added an optional argument for the number of
steps in go_back() and go_forward().
This commit is contained in:
Marcus Nilsson 2021-05-24 00:58:00 +02:00 committed by GitHub
parent 0688e02339
commit 6b85c7647c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 20 deletions

View file

@ -34,16 +34,16 @@ URL History::current() const
return m_items[m_current];
}
void History::go_back()
void History::go_back(int steps)
{
VERIFY(can_go_back());
m_current--;
VERIFY(can_go_back(steps));
m_current -= steps;
}
void History::go_forward()
void History::go_forward(int steps)
{
VERIFY(can_go_forward());
m_current++;
VERIFY(can_go_forward(steps));
m_current += steps;
}
void History::clear()
@ -52,4 +52,22 @@ void History::clear()
m_current = -1;
}
const Vector<URL> History::get_back_history()
{
Vector<URL> back_history;
for (int i = m_current - 1; i >= 0; i--) {
back_history.append(m_items[i]);
}
return back_history;
}
const Vector<URL> History::get_forward_history()
{
Vector<URL> forward_history;
for (int i = m_current + 1; i < static_cast<int>(m_items.size()); i++) {
forward_history.append(m_items[i]);
}
return forward_history;
}
}

View file

@ -17,13 +17,14 @@ public:
void push(const URL&);
URL current() const;
const Vector<URL> get_back_history();
const Vector<URL> get_forward_history();
void go_back();
void go_forward();
bool can_go_back() { return m_current > 0; }
bool can_go_forward() { return m_current + 1 < static_cast<int>(m_items.size()); }
void go_back(int steps = 1);
void go_forward(int steps = 1);
bool can_go_back(int steps = 1) { return (m_current - steps) >= 0; }
bool can_go_forward(int steps = 1) { return (m_current + steps) < static_cast<int>(m_items.size()); }
void clear();
private:

View file

@ -92,8 +92,36 @@ Tab::Tab(BrowserWindow& window, Type type)
else
m_web_content_view = webview_container.add<Web::OutOfProcessWebView>();
toolbar.add_action(window.go_back_action());
toolbar.add_action(window.go_forward_action());
auto& go_back_button = toolbar.add_action(window.go_back_action());
go_back_button.on_context_menu_request = [this](auto& context_menu_event) {
if (!m_history.can_go_back())
return;
int i = 0;
m_go_back_context_menu = GUI::Menu::construct();
for (auto& url : m_history.get_back_history()) {
i++;
m_go_back_context_menu->add_action(GUI::Action::create(url.to_string(),
Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"),
[this, i](auto&) { go_back(i); }));
}
m_go_back_context_menu->popup(context_menu_event.screen_position());
};
auto& go_forward_button = toolbar.add_action(window.go_forward_action());
go_forward_button.on_context_menu_request = [this](auto& context_menu_event) {
if (!m_history.can_go_forward())
return;
int i = 0;
m_go_forward_context_menu = GUI::Menu::construct();
for (auto& url : m_history.get_forward_history()) {
i++;
m_go_forward_context_menu->add_action(GUI::Action::create(url.to_string(),
Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"),
[this, i](auto&) { go_forward(i); }));
}
m_go_forward_context_menu->popup(context_menu_event.screen_position());
};
toolbar.add_action(window.go_home_action());
toolbar.add_action(window.reload_action());
@ -315,16 +343,16 @@ void Tab::reload()
load(url());
}
void Tab::go_back()
void Tab::go_back(int steps)
{
m_history.go_back();
m_history.go_back(steps);
update_actions();
load(m_history.current(), LoadType::HistoryNavigation);
}
void Tab::go_forward()
void Tab::go_forward(int steps)
{
m_history.go_forward();
m_history.go_forward(steps);
update_actions();
load(m_history.current(), LoadType::HistoryNavigation);
}

View file

@ -46,8 +46,8 @@ public:
void load(const URL&, LoadType = LoadType::Normal);
void reload();
void go_back();
void go_forward();
void go_back(int steps = 1);
void go_forward(int steps = 1);
void did_become_active();
void context_menu_requested(const Gfx::IntPoint& screen_position);
@ -104,7 +104,8 @@ private:
RefPtr<GUI::Menu> m_tab_context_menu;
RefPtr<GUI::Menu> m_page_context_menu;
RefPtr<GUI::Menu> m_go_back_context_menu;
RefPtr<GUI::Menu> m_go_forward_context_menu;
String m_title;
RefPtr<const Gfx::Bitmap> m_icon;