Browse Source

simplify history navigation api

CPP-GTK4
yggverse 3 months ago
parent
commit
43e89716ae
  1. 16
      src/app/browser/main/tab/page/navbar.cpp
  2. 127
      src/app/browser/main/tab/page/navbar/history.cpp
  3. 38
      src/app/browser/main/tab/page/navbar/history.hpp

16
src/app/browser/main/tab/page/navbar.cpp

@ -89,28 +89,28 @@ Navbar::Navbar(
// Actions // Actions
void Navbar::back() void Navbar::back()
{ {
if (navbarHistory->has_memory_back()) navbar::History::Memory match;
if (navbarHistory->back(match))
{ {
navbarRequest->set_text( navbarRequest->set_text(
navbarHistory->make_memory_back_request() match.request
); );
navbarHistory->back(); // --
navbarUpdate->activate(); navbarUpdate->activate();
} }
} }
void Navbar::forward() void Navbar::forward()
{ {
if (navbarHistory->has_memory_forward()) navbar::History::Memory match;
if (navbarHistory->forward(match))
{ {
navbarRequest->set_text( navbarRequest->set_text(
navbarHistory->make_memory_forward_request() match.request
); );
navbarHistory->forward(); // ++
navbarUpdate->activate(); navbarUpdate->activate();
} }
} }

127
src/app/browser/main/tab/page/navbar/history.cpp

@ -24,20 +24,52 @@ History::History()
} }
// Actions // Actions
void History::back() bool History::back(
{ Memory & match,
if (has_memory_back()) bool follow
) {
try
{
match = memory.at(
index - 1
);
if (follow)
{ {
index--; index--;
} }
return true;
}
catch (std::out_of_range)
{
return false;
}
} }
void History::forward() bool History::forward(
{ Memory & match,
if (has_memory_forward()) bool follow
) {
try
{
match = memory.at(
index + 1
);
if (follow)
{ {
index++; index++;
} }
return true;
}
catch (std::out_of_range)
{
return false;
}
} }
void History::push( void History::push(
@ -61,82 +93,19 @@ void History::push(
void History::refresh() void History::refresh()
{ {
Memory match;
historyBack->set_sensitive( historyBack->set_sensitive(
has_memory_back() back(
match,
false
)
); );
historyForward->set_sensitive( historyForward->set_sensitive(
has_memory_forward() forward(
); match,
} false
)
// Getters
bool History::has_memory_back() // @TODO & MEMORY
{
try
{
const History::Memory & MEMORY = get_memory_back();
return true;
}
catch (const std::out_of_range & EXCEPTION)
{
return false;
}
}
bool History::has_memory_forward() // @TODO & MEMORY
{
try
{
const History::Memory & MEMORY = get_memory_forward();
return true;
}
catch (const std::out_of_range & EXCEPTION)
{
return false;
}
}
// Copying getters
Glib::ustring History::make_memory_back_request()
{
Glib::ustring request;
if (has_memory_back())
{
request = get_memory_back().request;
}
return request;
}
Glib::ustring History::make_memory_forward_request()
{
Glib::ustring request;
if (has_memory_forward())
{
request = get_memory_forward().request;
}
return request;
}
// Private helpers
History::Memory & History::get_memory_back()
{
return memory.at(
index - 1
);
}
History::Memory & History::get_memory_forward()
{
return memory.at(
index + 1
); );
} }

38
src/app/browser/main/tab/page/navbar/history.hpp

@ -18,6 +18,14 @@ namespace app::browser::main::tab::page::navbar
class History : public Gtk::Box class History : public Gtk::Box
{ {
// Components
history::Back * historyBack;
history::Forward * historyForward;
int index = 0;
public:
// Extras // Extras
struct Memory struct Memory
{ {
@ -28,37 +36,25 @@ namespace app::browser::main::tab::page::navbar
// Define navigation history storage // Define navigation history storage
std::vector<Memory> memory; std::vector<Memory> memory;
int index = 0;
// Private helpers
Memory & get_memory_back();
Memory & get_memory_forward();
// Components
history::Back * historyBack;
history::Forward * historyForward;
public:
History(); History();
// Actions // Actions
void back(); bool back(
void forward(); Memory & match,
bool follow = true
);
bool forward(
Memory & match,
bool follow = true
);
void push( void push(
const Glib::ustring & REQUEST const Glib::ustring & REQUEST
); );
void refresh(); void refresh();
// Getters
bool has_memory_back();
bool has_memory_forward();
// Copying getters (to keep private members encapsulation)
Glib::ustring make_memory_back_request();
Glib::ustring make_memory_forward_request();
}; };
} }

Loading…
Cancel
Save