Browse Source

implement history navigation

PHP-GTK3
yggverse 7 months ago
parent
commit
7b38e00089
  1. 71
      src/Model/History.php
  2. 63
      src/Tab/Page.php

71
src/Model/History.php

@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Model;
class History
{
private int $_position = -1;
private array $_record = [];
public function reset(): void
{
$this->_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
);
}
}

63
src/Tab/Page.php

@ -7,7 +7,7 @@ namespace Yggverse\Yoda\Tab;
class Page class Page
{ {
public \Yggverse\Yoda\Model\Memory $dns; public \Yggverse\Yoda\Model\Memory $dns;
public \Yggverse\Yoda\Model\Memory $history; public \Yggverse\Yoda\Model\History $history;
public \GtkBox $box, public \GtkBox $box,
$header, $header,
@ -34,9 +34,11 @@ class Page
// Init config // Init config
$this->config = \Yggverse\Yoda\Model\File::getConfig()->window->tab->page; $this->config = \Yggverse\Yoda\Model\File::getConfig()->window->tab->page;
// Init memory // Init DNS memory
$this->dns = new \Yggverse\Yoda\Model\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 // Compose header
$this->header = new \GtkBox( $this->header = new \GtkBox(
@ -76,6 +78,8 @@ class Page
'released', 'released',
function ($entry) function ($entry)
{ {
$this->history->reset();
$this->open( $this->open(
$this->config->header->button->home->url $this->config->header->button->home->url
); );
@ -94,11 +98,41 @@ class Page
$this->config->header->button->back->label $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 // Forward button
$this->forward = \GtkButton::new_with_label( $this->forward = \GtkButton::new_with_label(
$this->config->header->button->forward->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 /// Group buttons
if ($this->config->header->button->back->visible || $this->config->header->button->forward->visible) if ($this->config->header->button->back->visible || $this->config->header->button->forward->visible)
{ {
@ -284,7 +318,8 @@ class Page
} }
public function open( public function open(
string $url string $url,
bool $history = true
): void ): void
{ {
// Update address field by requested // Update address field by requested
@ -292,11 +327,29 @@ class Page
$url $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( $this->home->set_sensitive(
!($url == $this->config->header->button->home->url) !($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 // Open current address
switch (true) switch (true)
{ {

Loading…
Cancel
Save