From d4bf1f6c56877bbb77af4562ee77f44cd9aded67 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 7 Jul 2024 05:29:31 +0300 Subject: [PATCH] separate entities, use viewport container for history listing --- .../Browser/History/Container/Content.php | 185 +++--------------- .../History/Container/Content/Table.php | 141 +++++++++++++ .../History/Container/Content/Table/Data.php | 60 ++++++ .../History/Container/Content/Viewport.php | 23 +++ .../History/Container/Navbar/Delete.php | 4 +- .../Browser/History/Container/Navbar/Open.php | 4 +- 6 files changed, 257 insertions(+), 160 deletions(-) create mode 100644 src/Entity/Browser/History/Container/Content/Table.php create mode 100644 src/Entity/Browser/History/Container/Content/Table/Data.php create mode 100644 src/Entity/Browser/History/Container/Content/Viewport.php diff --git a/src/Entity/Browser/History/Container/Content.php b/src/Entity/Browser/History/Container/Content.php index b729a492..8dfc36a7 100644 --- a/src/Entity/Browser/History/Container/Content.php +++ b/src/Entity/Browser/History/Container/Content.php @@ -4,23 +4,22 @@ declare(strict_types=1); namespace Yggverse\Yoda\Entity\Browser\History\Container; +use \Yggverse\Yoda\Entity\Browser\History\Container\Content\Viewport; +use \Yggverse\Yoda\Entity\Browser\History\Container\Content\Table; + class Content { public \GtkScrolledWindow $gtk; - // Requirements @TODO entity - public \GtkTreeView $treeview; - public \GtkListStore $list; - // Dependencies public \Yggverse\Yoda\Entity\Browser\History\Container $container; + // Requirements + public \Yggverse\Yoda\Entity\Browser\History\Container\Content\Viewport $viewport; + public \Yggverse\Yoda\Entity\Browser\History\Container\Content\Table $table; + // Defaults - private string $_time = 'Time'; - private string $_title = 'Title'; - private string $_url = 'URL'; - private string $_format = 'c'; - private int $_margin = 8; + private int $_margin = 8; public function __construct( \Yggverse\Yoda\Entity\Browser\History\Container $container @@ -31,146 +30,52 @@ class Content // Init container $this->gtk = new \GtkScrolledWindow; - // Init records listing - $this->treeview = new \GtkTreeView; - - $this->treeview->set_margin_start( + $this->gtk->set_margin_start( $this->_margin ); - $this->treeview->set_margin_end( + $this->gtk->set_margin_end( $this->_margin ); - $this->treeview->append_column( - new \GtkTreeViewColumn( - $this->_time, - new \GtkCellRendererText(), - 'text', - 1 - ) - ); - - $this->treeview->append_column( - new \GtkTreeViewColumn( - $this->_url, - new \GtkCellRendererText(), - 'text', - 2 - ) + $this->gtk->set_margin_bottom( + $this->_margin ); - $this->treeview->append_column( - new \GtkTreeViewColumn( - $this->_title, - new \GtkCellRendererText(), - 'text', - 3 - ) + // Init history records table + $this->table = new Table( + $this ); - $this->list = new \GtkListStore( - \GObject::TYPE_INT, - \GObject::TYPE_STRING, - \GObject::TYPE_STRING, - \GObject::TYPE_STRING + // Init viewport to integrate scrolled window features + $this->viewport = new Viewport( + $this ); - $this->treeview->set_model( - $this->list + $this->viewport->gtk->add( + $this->table->gtk ); $this->gtk->add( - $this->treeview + $this->viewport->gtk ); - // Connect events - $this->treeview->connect( - 'row-activated', - function( - \GtkTreeView $treeview - ) { - // Focus on browser - // $this->container->history->browser->gtk->present(); - - $this->container->history->browser->container->tab->append( - $this->getSelectedUrl() - ); - } - ); - - $this->treeview->connect( - 'cursor-changed', - function( - \GtkTreeView $treeview - ) { - $this->container->navbar->open->gtk->set_sensitive( - boolval( - $this->getSelectedId() - ) - ); - - $this->container->navbar->delete->gtk->set_sensitive( - boolval( - $this->getSelectedId() - ) - ); - } - ); - - // Make initial search + // Do initial search $this->search(); } - // Append new row - public function append( - int $id, - int $time, - string $url, - ?string $title - ): void - { - $this->list->append( - [ - $id, - date( - $this->_format, - $time - ), - $url, - strval( - $title - ) - ] - ); - } - - // Remove rows from list - public function clear(): void - { - $this->list->clear(); - } - - // Refresh rows using current navbar value - public function refresh(): void - { - $this->search( - $this->container->navbar->filter->gtk->get_text() - ); - } - // Do records search in database public function search( string $filter = '' ): void { - $this->clear(); + $this->table->data->clear(); if ($records = $this->container->history->browser->database->findHistory($filter)) { foreach ($records as $record) { - $this->append( + $this->table->data->append( $record->id, $record->time, $record->url, @@ -191,43 +96,11 @@ class Content } } - public function getSelectedId(): ?int - { - if ($id = $this->_getSelected(0)) - { - return $id; - } - - return null; - } - - public function getSelectedUrl(): ?string - { - if ($url = $this->_getSelected(2)) - { - return $url; - } - - return null; - } - - private function _getSelected( - int $column - ): null|int|string + // Refresh rows using current filter value in the navbar + public function refresh(): void { - list( - $list, - $row - ) = $this->treeview->get_selection()->get_selected(); - - if ($list && $row) - { - if ($value = $list->get_value($row, $column)) - { - return $value; - } - } - - return null; + $this->search( + $this->container->navbar->filter->gtk->get_text() + ); } } \ No newline at end of file diff --git a/src/Entity/Browser/History/Container/Content/Table.php b/src/Entity/Browser/History/Container/Content/Table.php new file mode 100644 index 00000000..4b3ee733 --- /dev/null +++ b/src/Entity/Browser/History/Container/Content/Table.php @@ -0,0 +1,141 @@ +content = $content; + + // Init tree view + $this->gtk = new \GtkTreeView; + + $this->gtk->append_column( + new \GtkTreeViewColumn( + $this->_time, + new \GtkCellRendererText(), + 'text', + 1 + ) + ); + + $this->gtk->append_column( + new \GtkTreeViewColumn( + $this->_url, + new \GtkCellRendererText(), + 'text', + 2 + ) + ); + + $this->gtk->append_column( + new \GtkTreeViewColumn( + $this->_title, + new \GtkCellRendererText(), + 'text', + 3 + ) + ); + + // Init data model + $this->data = new Data( + $this + ); + + $this->gtk->set_model( + $this->data->gtk + ); + + // Connect events + $this->gtk->connect( + 'row-activated', + function() + { + // Focus on browser + // $this->content->container->history->browser->gtk->present(); + + $this->content->container->history->browser->container->tab->append( + $this->getSelectedUrl() + ); + } + ); + + $this->gtk->connect( + 'cursor-changed', + function() + { + $this->content->container->navbar->open->gtk->set_sensitive( + boolval( + $this->getSelectedId() + ) + ); + + $this->content->container->navbar->delete->gtk->set_sensitive( + boolval( + $this->getSelectedId() + ) + ); + } + ); + } + + public function getSelectedId(): ?int + { + if ($id = $this->_getSelected(0)) + { + return $id; + } + + return null; + } + + public function getSelectedUrl(): ?string + { + if ($url = $this->_getSelected(2)) + { + return $url; + } + + return null; + } + + private function _getSelected( + int $column + ): null|int|string + { + list( + $list, + $row + ) = $this->gtk->get_selection()->get_selected(); + + if ($list && $row) + { + if ($value = $list->get_value($row, $column)) + { + return $value; + } + } + + return null; + } +} \ No newline at end of file diff --git a/src/Entity/Browser/History/Container/Content/Table/Data.php b/src/Entity/Browser/History/Container/Content/Table/Data.php new file mode 100644 index 00000000..56ecaa7b --- /dev/null +++ b/src/Entity/Browser/History/Container/Content/Table/Data.php @@ -0,0 +1,60 @@ +table = $table; + + // Init tree view + $this->gtk = new \GtkListStore( + \GObject::TYPE_INT, + \GObject::TYPE_STRING, + \GObject::TYPE_STRING, + \GObject::TYPE_STRING + ); + } + + // Append new row + public function append( + int $id, + int $time, + string $url, + ?string $title + ): void + { + $this->gtk->append( + [ + $id, + date( + $this->_time, + $time + ), + $url, + strval( + $title + ) + ] + ); + } + + // Remove rows from list + public function clear(): void + { + $this->gtk->clear(); + } +} \ No newline at end of file diff --git a/src/Entity/Browser/History/Container/Content/Viewport.php b/src/Entity/Browser/History/Container/Content/Viewport.php new file mode 100644 index 00000000..8660a5e9 --- /dev/null +++ b/src/Entity/Browser/History/Container/Content/Viewport.php @@ -0,0 +1,23 @@ +content = $content; + + // Init viewport + $this->gtk = new \GtkViewport; + } +} \ No newline at end of file diff --git a/src/Entity/Browser/History/Container/Navbar/Delete.php b/src/Entity/Browser/History/Container/Navbar/Delete.php index d58f9215..15fefcd0 100644 --- a/src/Entity/Browser/History/Container/Navbar/Delete.php +++ b/src/Entity/Browser/History/Container/Navbar/Delete.php @@ -12,7 +12,7 @@ class Delete extends \Yggverse\Yoda\Abstract\Entity\Browser\History\Container\Na \GtkButton $entity ): void { - if ($id = $this->navbar->container->content->getSelectedId()) + if ($id = $this->navbar->container->content->table->getSelectedId()) { $this->navbar->container->history->browser->database->deleteHistory( $id @@ -26,7 +26,7 @@ class Delete extends \Yggverse\Yoda\Abstract\Entity\Browser\History\Container\Na { $this->gtk->set_sensitive( boolval( - $this->navbar->container->content->getSelectedId() + $this->navbar->container->content->table->getSelectedId() ) ); } diff --git a/src/Entity/Browser/History/Container/Navbar/Open.php b/src/Entity/Browser/History/Container/Navbar/Open.php index 0824fda7..29099ec9 100644 --- a/src/Entity/Browser/History/Container/Navbar/Open.php +++ b/src/Entity/Browser/History/Container/Navbar/Open.php @@ -13,7 +13,7 @@ class Open extends \Yggverse\Yoda\Abstract\Entity\Browser\History\Container\Navb ): void { $this->navbar->container->history->browser->container->tab->append( - $this->navbar->container->content->getSelectedUrl() + $this->navbar->container->content->table->getSelectedUrl() ); } @@ -21,7 +21,7 @@ class Open extends \Yggverse\Yoda\Abstract\Entity\Browser\History\Container\Navb { $this->gtk->set_sensitive( boolval( - $this->navbar->container->content->getSelectedId() + $this->navbar->container->content->table->getSelectedId() ) ); }