Browse Source

implement bookmarks browser

PHP-GTK3
yggverse 4 months ago
parent
commit
49ec6ee23d
  1. 20
      src/Abstract/Entity/Browser/Bookmark/Container/Navbar/Button.php
  2. 20
      src/Abstract/Entity/Browser/Bookmark/Container/Navbar/Entry.php
  3. 68
      src/Entity/Browser/Bookmark.php
  4. 65
      src/Entity/Browser/Bookmark/Container.php
  5. 111
      src/Entity/Browser/Bookmark/Container/Content.php
  6. 148
      src/Entity/Browser/Bookmark/Container/Content/Table.php
  7. 65
      src/Entity/Browser/Bookmark/Container/Content/Table/Data.php
  8. 31
      src/Entity/Browser/Bookmark/Container/Content/Viewport.php
  9. 99
      src/Entity/Browser/Bookmark/Container/Navbar.php
  10. 39
      src/Entity/Browser/Bookmark/Container/Navbar/Delete.php
  11. 45
      src/Entity/Browser/Bookmark/Container/Navbar/Filter.php
  12. 34
      src/Entity/Browser/Bookmark/Container/Navbar/Open.php
  13. 35
      src/Entity/Browser/Bookmark/Header.php
  14. 10
      src/Entity/Browser/Menu.php
  15. 49
      src/Entity/Browser/Menu/Bookmark.php
  16. 2
      src/Entity/Browser/Menu/History.php

20
src/Abstract/Entity/Browser/Bookmark/Container/Navbar/Button.php

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Abstract\Entity\Browser\Bookmark\Container\Navbar;
use \Yggverse\Yoda\Entity\Browser\Bookmark\Container\Navbar;
abstract class Button extends \Yggverse\Yoda\Abstract\Entity\Button
{
public Navbar $navbar;
public function __construct(
Navbar $navbar
) {
parent::__construct();
$this->navbar = $navbar;
}
}

20
src/Abstract/Entity/Browser/Bookmark/Container/Navbar/Entry.php

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Abstract\Entity\Browser\Bookmark\Container\Navbar;
use \Yggverse\Yoda\Entity\Browser\Bookmark\Container\Navbar;
abstract class Entry extends \Yggverse\Yoda\Abstract\Entity\Entry
{
public Navbar $navbar;
public function __construct(
Navbar $navbar
) {
parent::__construct();
$this->navbar = $navbar;
}
}

68
src/Entity/Browser/Bookmark.php

@ -0,0 +1,68 @@ @@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser;
use \GtkWindow;
use \Yggverse\Yoda\Entity\Browser;
class Bookmark
{
// GTK
public \GtkWindow $gtk;
// Dependencies
public Browser $browser;
// Requirements
public Bookmark\Header $header;
public Bookmark\Container $container;
// Defaults
public const WIDTH = 640;
public const HEIGHT = 640;
public const MAXIMIZE = false;
public function __construct(
Browser $browser
) {
// Init dependencies
$this->browser = $browser;
// Init window
$this->gtk = new GtkWindow;
$this->gtk->set_size_request(
$this::WIDTH,
$this::HEIGHT
);
if ($this::MAXIMIZE)
{
$this->gtk->maximize();
}
// Init header
$this->header = new Bookmark\Header(
$this
);
$this->gtk->set_titlebar(
$this->header->gtk
);
// Init container
$this->container = new Bookmark\Container(
$this
);
$this->gtk->add(
$this->container->gtk
);
// Render
$this->gtk->show();
}
}

65
src/Entity/Browser/Bookmark/Container.php

@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Bookmark;
use \GtkBox;
use \GtkOrientation;
use \Yggverse\Yoda\Entity\Browser\Bookmark;
class Container
{
// GTK
public GtkBox $gtk;
// Dependencies
public Bookmark $bookmark;
// Requirements
public Container\Navbar $navbar;
public Container\Content $content;
public function __construct(
Bookmark $bookmark
) {
// Init dependency
$this->bookmark = $bookmark;
// Init container
$this->gtk = new GtkBox(
GtkOrientation::VERTICAL
);
// Init navbar
$this->navbar = new Container\Navbar(
$this
);
$this->gtk->add(
$this->navbar->gtk
);
// Init content
$this->content = new Container\Content(
$this
);
$this->gtk->pack_start(
$this->content->gtk,
true,
true,
0
);
// Render
$this->gtk->show();
}
public function refresh()
{
$this->navbar->refresh();
$this->content->refresh();
}
}

111
src/Entity/Browser/Bookmark/Container/Content.php

@ -0,0 +1,111 @@ @@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Bookmark\Container;
use \GtkScrolledWindow;
use \Yggverse\Yoda\Entity\Browser\Bookmark\Container;
class Content
{
// GTK
public GtkScrolledWindow $gtk;
// Dependencies
public Container $container;
// Requirements
public Content\Viewport $viewport;
public Content\Table $table;
// Defaults
public const MARGIN = 8;
public function __construct(
Container $container
) {
// Init dependency
$this->container = $container;
// Init container
$this->gtk = new GtkScrolledWindow;
$this->gtk->set_margin_start(
$this::MARGIN
);
$this->gtk->set_margin_end(
$this::MARGIN
);
$this->gtk->set_margin_bottom(
$this::MARGIN
);
// Init bookmark records table
$this->table = new Content\Table(
$this
);
// Init viewport to integrate scrolled window features
$this->viewport = new Content\Viewport(
$this
);
$this->viewport->gtk->add(
$this->table->gtk
);
$this->gtk->add(
$this->viewport->gtk
);
// Render
$this->gtk->show();
// Do initial search
$this->search();
}
// Do records search in database
public function search(
string $filter = ''
): void
{
$this->table->data->clear();
if ($records = $this->container->bookmark->browser->database->findBookmark($filter))
{
foreach ($records as $record)
{
$this->table->data->append(
$record->id,
$record->time,
$record->request,
$record->title
);
}
}
else
{
$this->container->navbar->open->gtk->set_sensitive(
false
);
$this->container->navbar->delete->gtk->set_sensitive(
false
);
}
}
// Refresh rows using current filter value in the navbar
public function refresh(): void
{
$this->search(
$this->container->navbar->filter->getValue()
);
}
}

148
src/Entity/Browser/Bookmark/Container/Content/Table.php

@ -0,0 +1,148 @@ @@ -0,0 +1,148 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Bookmark\Container\Content;
use \GtkCellRendererText;
use \GtkTreeView;
use \GtkTreeViewColumn;
use \Yggverse\Yoda\Entity\Browser\Bookmark\Container\Content;
class Table
{
public GtkTreeView $gtk;
// Dependencies
public Content $content;
// Requirements
public Table\Data $data;
// Defaults
public const TIME = 'Time';
public const TITLE = 'Title';
public const REQUEST = 'Request';
public function __construct(
Content $content
) {
// Init dependencies
$this->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::REQUEST,
new GtkCellRendererText(),
'text',
2
)
);
$this->gtk->append_column(
new GtkTreeViewColumn(
$this::TITLE,
new GtkCellRendererText(),
'text',
3
)
);
// Init data model
$this->data = new Table\Data(
$this
);
$this->gtk->set_model(
$this->data->gtk
);
// Render
$this->gtk->show();
// Init events
$this->gtk->connect(
'row-activated',
function()
{
// Focus on browser
// $this->content->container->bookmark->browser->gtk->present();
$this->content->container->bookmark->browser->container->tab->append(
$this->getSelectedRequest()
);
}
);
$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 getSelectedRequest(): ?string
{
if ($request = $this->_getSelected(2))
{
return $request;
}
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;
}
}

65
src/Entity/Browser/Bookmark/Container/Content/Table/Data.php

@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Bookmark\Container\Content\Table;
use \GObject;
use \GtkListStore;
use \Yggverse\Yoda\Entity\Browser\Bookmark\Container\Content\Table;
class Data
{
public GtkListStore $gtk;
// Dependencies
public Table $table;
// Defaults
public const TIME = 'c';
public function __construct(
Table $table
) {
// Init dependencies
$this->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 $request,
?string $title
): void
{
$this->gtk->append(
[
$id,
date(
$this::TIME,
$time
),
$request,
strval(
$title
)
]
);
}
// Remove rows from list
public function clear(): void
{
$this->gtk->clear();
}
}

31
src/Entity/Browser/Bookmark/Container/Content/Viewport.php

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Bookmark\Container\Content;
use \GtkViewport;
use \Yggverse\Yoda\Entity\Browser\Bookmark\Container\Content;
class Viewport
{
// GTK
public GtkViewport $gtk;
// Dependencies
public Content $content;
public function __construct(
Content $content
) {
// Init dependencies
$this->content = $content;
// Init viewport
$this->gtk = new GtkViewport;
// Render
$this->gtk->show();
}
}

99
src/Entity/Browser/Bookmark/Container/Navbar.php

@ -0,0 +1,99 @@ @@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Bookmark\Container;
use \GtkBox;
use \GtkOrientation;
use \Yggverse\Yoda\Entity\Browser\Bookmark\Container;
class Navbar
{
public GtkBox $gtk;
// Dependencies
public Container $container;
// Requirements
public Navbar\Delete $delete;
public Navbar\Filter $filter;
public Navbar\Open $open;
public Navbar\Search $search;
// Defaults
public const MARGIN = 8;
public const SPACING = 8;
public function __construct(
Container $container
) {
// Init dependency
$this->container = $container;
// Init container
$this->gtk = new GtkBox(
GtkOrientation::HORIZONTAL
);
$this->gtk->set_margin_top(
$this::MARGIN
);
$this->gtk->set_margin_bottom(
$this::MARGIN
);
$this->gtk->set_margin_start(
$this::MARGIN
);
$this->gtk->set_margin_end(
$this::MARGIN
);
$this->gtk->set_spacing(
$this::SPACING
);
// Init open button
$this->open = new Navbar\Open(
$this
);
$this->gtk->add(
$this->open->gtk
);
// Init delete button
$this->delete = new Navbar\Delete(
$this
);
$this->gtk->add(
$this->delete->gtk
);
// Init filter entry
$this->filter = new Navbar\Filter(
$this
);
$this->gtk->pack_start(
$this->filter->gtk,
true,
true,
0
);
// Render
$this->gtk->show();
}
public function refresh(): void
{
$this->delete->refresh();
$this->open->refresh();
}
}

39
src/Entity/Browser/Bookmark/Container/Navbar/Delete.php

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Bookmark\Container\Navbar;
use \GtkButton;
use \Yggverse\Yoda\Abstract\Entity\Browser\Bookmark\Container\Navbar\Button;
class Delete extends Button
{
public const IMAGE = 'edit-delete-symbolic';
public const LABEL = 'Delete';
public const TOOLTIP = 'Delete';
protected function _onCLick(
GtkButton $entity
): void
{
if ($id = $this->navbar->container->content->table->getSelectedId())
{
$this->navbar->container->bookmark->browser->database->deleteBookmark(
$id
);
}
$this->navbar->container->refresh();
}
public function refresh(): void
{
$this->gtk->set_sensitive(
boolval(
$this->navbar->container->content->table->getSelectedId()
)
);
}
}

45
src/Entity/Browser/Bookmark/Container/Navbar/Filter.php

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Bookmark\Container\Navbar;
use \GdkEvent;
use \GtkEntry;
use \Yggverse\Yoda\Abstract\Entity\Browser\Bookmark\Container\Navbar\Entry;
class Filter extends Entry
{
public const PLACEHOLDER = 'Search in bookmarks...';
protected function _onActivate(
GtkEntry $entry
): void
{
$this->navbar->container->content->search(
$entry->get_text()
);
}
protected function _onKeyRelease(
GtkEntry $entry,
GdkEvent $event
): void
{
$this->navbar->container->content->search(
$entry->get_text()
);
}
protected function _onChanged(
GtkEntry $entry
): void
{}
protected function _onFocusOut(
GtkEntry $entry,
GdkEvent $event
): void
{}
}

34
src/Entity/Browser/Bookmark/Container/Navbar/Open.php

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Bookmark\Container\Navbar;
use \GtkButton;
use \Yggverse\Yoda\Abstract\Entity\Browser\Bookmark\Container\Navbar\Button;
class Open extends Button
{
public const IMAGE = null; // list-add-symbolic | tab-new-symbolic
public const LABEL = 'Open';
public const TOOLTIP = 'Open';
protected function _onCLick(
GtkButton $entity
): void
{
$this->navbar->container->bookmark->browser->container->tab->append(
$this->navbar->container->content->table->getSelectedRequest()
);
}
public function refresh(): void
{
$this->gtk->set_sensitive(
boolval(
$this->navbar->container->content->table->getSelectedId()
)
);
}
}

35
src/Entity/Browser/Bookmark/Header.php

@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Bookmark;
use \GtkHeaderBar;
class Header
{
public GtkHeaderBar $gtk;
public const ACTIONS = true;
public const TITLE = 'Bookmark - Yoda';
public const SUBTITLE = '';
public function __construct()
{
$this->gtk = new GtkHeaderBar;
$this->gtk->set_show_close_button(
$this::ACTIONS
);
$this->gtk->set_title(
_($this::TITLE)
);
$this->gtk->set_subtitle(
_($this::SUBTITLE)
);
$this->gtk->show();
}
}

10
src/Entity/Browser/Menu.php

@ -19,6 +19,7 @@ class Menu @@ -19,6 +19,7 @@ class Menu
// Requirements
public Menu\File $file;
public Menu\Tab $tab;
public Menu\Bookmark $bookmark;
public Menu\History $history;
public Menu\Quit $quit;
@ -49,6 +50,15 @@ class Menu @@ -49,6 +50,15 @@ class Menu
$this->tab->gtk
);
// Init bookmark menu item
$this->bookmark = new Menu\Bookmark(
$this
);
$this->gtk->append(
$this->bookmark->gtk
);
// Init history menu item
$this->history = new Menu\History(
$this

49
src/Entity/Browser/Menu/Bookmark.php

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Menu;
use \GtkMenuItem;
use \Yggverse\Yoda\Entity\Browser\Menu;
use \Yggverse\Yoda\Entity\Browser\Bookmark as Window;
class Bookmark
{
// GTK
public GtkMenuItem $gtk;
// Dependencies
public Menu $menu;
// Defaults
public const LABEL = 'Bookmarks';
public function __construct(
Menu $menu
) {
// Init dependencies
$this->menu = $menu;
// Init menu item
$this->gtk = GtkMenuItem::new_with_label(
$this::LABEL
);
// Render
$this->gtk->show();
// Int events
$this->gtk->connect(
'activate',
function()
{
new Window(
$this->menu->browser
);
}
);
}
}

2
src/Entity/Browser/Menu/History.php

@ -40,7 +40,7 @@ class History @@ -40,7 +40,7 @@ class History
'activate',
function()
{
$history = new Window(
new Window(
$this->menu->browser
);
}

Loading…
Cancel
Save