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. 131
      src/app/browser/main/tab/page/navbar/history.cpp
  3. 48
      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();
} }
} }

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

@ -24,19 +24,51 @@ 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--;
}
return true;
}
catch (std::out_of_range)
{ {
index--; return false;
} }
} }
void History::forward() bool History::forward(
{ Memory & match,
if (has_memory_forward()) bool follow
) {
try
{ {
index++; match = memory.at(
index + 1
);
if (follow)
{
index++;
}
return true;
}
catch (std::out_of_range)
{
return false;
} }
} }
@ -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
); );
} }

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

@ -18,47 +18,43 @@ namespace app::browser::main::tab::page::navbar
class History : public Gtk::Box class History : public Gtk::Box
{ {
// Extras
struct Memory
{
Glib::ustring request;
std::time_t time; // event unix time
bool permanent; // save in database (on application close) @TODO
};
// Define navigation history storage
std::vector<Memory> memory;
int index = 0;
// Private helpers
Memory & get_memory_back();
Memory & get_memory_forward();
// Components // Components
history::Back * historyBack; history::Back * historyBack;
history::Forward * historyForward; history::Forward * historyForward;
int index = 0;
public: public:
// Extras
struct Memory
{
Glib::ustring request;
std::time_t time; // event unix time
bool permanent; // save in database (on application close) @TODO
};
// Define navigation history storage
std::vector<Memory> memory;
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