From 7b38e00089b06651e1ef88eae0b3ead4a68a4419 Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 11 Apr 2024 18:53:20 +0300 Subject: [PATCH] implement history navigation --- src/Model/History.php | 71 +++++++++++++++++++++++++++++++++++++++++++ src/Tab/Page.php | 65 +++++++++++++++++++++++++++++++++++---- 2 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 src/Model/History.php diff --git a/src/Model/History.php b/src/Model/History.php new file mode 100644 index 00000000..585b88c5 --- /dev/null +++ b/src/Model/History.php @@ -0,0 +1,71 @@ +_position = -1; + + $this->_record = []; + } + + public function add( + string $record + ): int + { + $this->_record[] = $record; + + $this->_position = array_key_last( + $this->_record + ); + + return $this->_position; + } + + public function get( + int $position + ): ?string + { + return isset($this->_record[$position]) ? $this->_record[$position] : null; + } + + public function getBack(): ?string + { + return $this->get( + $this->_position - 1 + ); + } + + public function getForward(): ?string + { + return $this->get( + $this->_position + 1 + ); + } + + public function goBack(): ?string + { + $this->_position--; + + return $this->get( + $this->_position + ); + } + + public function goForward(): ?string + { + $this->_position++; + + return $this->get( + $this->_position + ); + } +} \ No newline at end of file diff --git a/src/Tab/Page.php b/src/Tab/Page.php index 2d89701e..b6b87000 100644 --- a/src/Tab/Page.php +++ b/src/Tab/Page.php @@ -6,8 +6,8 @@ namespace Yggverse\Yoda\Tab; class Page { - public \Yggverse\Yoda\Model\Memory $dns; - public \Yggverse\Yoda\Model\Memory $history; + public \Yggverse\Yoda\Model\Memory $dns; + public \Yggverse\Yoda\Model\History $history; public \GtkBox $box, $header, @@ -34,9 +34,11 @@ class Page // Init config $this->config = \Yggverse\Yoda\Model\File::getConfig()->window->tab->page; - // Init memory + // Init DNS memory $this->dns = new \Yggverse\Yoda\Model\Memory(); - $this->history = new \Yggverse\Yoda\Model\Memory(); + + // Init history + $this->history = new \Yggverse\Yoda\Model\History(); // Compose header $this->header = new \GtkBox( @@ -76,6 +78,8 @@ class Page 'released', function ($entry) { + $this->history->reset(); + $this->open( $this->config->header->button->home->url ); @@ -94,11 +98,41 @@ class Page $this->config->header->button->back->label ); + $this->back->set_sensitive( + false + ); + + $this->back->connect( + 'released', + function ($entry) + { + $this->open( + $this->history->goBack(), + false + ); + } + ); + // Forward button $this->forward = \GtkButton::new_with_label( $this->config->header->button->forward->label ); + $this->forward->set_sensitive( + false + ); + + $this->forward->connect( + 'released', + function ($entry) + { + $this->open( + $this->history->goForward(), + false + ); + } + ); + /// Group buttons if ($this->config->header->button->back->visible || $this->config->header->button->forward->visible) { @@ -284,7 +318,8 @@ class Page } public function open( - string $url + string $url, + bool $history = true ): void { // Update address field by requested @@ -292,11 +327,29 @@ class Page $url ); - // Update home button sensitivity on match requested + // Update history pool + if ($history) + { + $this->history->add( + $url + ); + } + + // Update home button sensibility on match requested $this->home->set_sensitive( !($url == $this->config->header->button->home->url) ); + // Update back button sensibility + $this->back->set_sensitive( + (bool) $this->history->getBack() + ); + + // Update forward button sensibility + $this->forward->set_sensitive( + (bool) $this->history->getForward() + ); + // Open current address switch (true) {