Browse Source

refactor page mime routing to init multimedia support

PHP-GTK3
yggverse 5 months ago
parent
commit
81595f0b79
  1. 77
      src/Abstract/Entity/Browser/Container/Page/Content/Markup.php
  2. 58
      src/Entity/Browser/Container/Page.php
  3. 109
      src/Entity/Browser/Container/Page/Content.php
  4. 140
      src/Entity/Browser/Container/Page/Content/Gemtext.php
  5. 24
      src/Entity/Browser/Container/Page/Content/Plain.php
  6. 2
      src/Entity/Browser/Menu/File/Save.php

77
src/Abstract/Entity/Browser/Container/Page/Content/Markup.php

@ -0,0 +1,77 @@ @@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Abstract\Entity\Browser\Container\Page\Content;
use \Yggverse\Yoda\Entity\Browser\Container\Page\Content;
abstract class Markup
{
public \GtkLabel $gtk;
// Dependencies
public Content $content;
// Defaults
protected int $_wrap = 140;
public function __construct(
Content $content
) {
// Init dependency
$this->content = $content;
// Init markup label
$this->gtk = new \GtkLabel;
$this->gtk->set_use_markup(
true
);
$this->gtk->set_selectable(
true
);
$this->gtk->set_can_focus(
false
);
$this->gtk->set_track_visited_links(
true
);
$this->gtk->set_xalign(
0
);
$this->gtk->set_yalign(
0
);
$this->gtk->show();
// Init events
$this->gtk->connect(
'activate-link',
function(
\GtkLabel $label,
string $href
) {
$this->_onActivateLink(
$label,
$href
);
}
);
}
abstract protected function _onActivateLink(
\GtkLabel $label,
string $href
);
abstract public function setSource(
string $value
): void;
}

58
src/Entity/Browser/Container/Page.php

@ -11,7 +11,6 @@ use \Yggverse\Yoda\Entity\Browser\Container\Page\Content; @@ -11,7 +11,6 @@ use \Yggverse\Yoda\Entity\Browser\Container\Page\Content;
use \Yggverse\Yoda\Entity\Browser\Container\Page\Response;
use \Yggverse\Yoda\Model\Connection;
use \Yggverse\Yoda\Model\Filesystem;
class Page
{
@ -200,57 +199,10 @@ class Page @@ -200,57 +199,10 @@ class Page
);
// Update content
switch ($connection->getMime())
{
case Filesystem::MIME_TEXT_GEMINI:
$title = null;
$this->content->setGemtext(
(string) $connection->getData(),
$title
);
if ($title)
{
$this->title->setValue(
$title
);
}
break;
case Filesystem::MIME_TEXT_PLAIN:
$this->content->setPlain(
(string) $connection->getData()
);
break;
/* @TODO
case 'image/gif':
case 'image/jpeg':
case 'image/png':
case 'image/webp':
$this->content->setImage(
(string) $connection->getData()
);
break;
*/
default:
$this->title->setValue(
_('Oops!')
);
$this->content->setPlain(
_('MIME type not supported')
);
}
$this->content->set(
$connection->getData(),
$connection->getMime()
);
// Hide progressbar
$this->progressbar->hide();
@ -271,7 +223,7 @@ class Page @@ -271,7 +223,7 @@ class Page
);
// Update content
$this->content->setGemtext(
$this->content->set(
_('Response time reached')
);

109
src/Entity/Browser/Container/Page/Content.php

@ -4,29 +4,37 @@ declare(strict_types=1); @@ -4,29 +4,37 @@ declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Container\Page;
use \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Data;
use \Yggverse\Yoda\Entity\Browser\Container\Page;
use \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Gemtext;
use \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Image;
use \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Plain;
use \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Viewport;
use \Yggverse\Yoda\Model\Filesystem;
class Content
{
public \GtkScrolledWindow $gtk;
// Dependencies
public \Yggverse\Yoda\Entity\Browser\Container\Page $page;
public Page $page;
// Requirements
public \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Data $data;
public \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Viewport $viewport;
public Viewport $viewport;
// Defaults
private int $_margin = 8;
// Extras
private ?string $_source = null;
public function __construct(
\Yggverse\Yoda\Entity\Browser\Container\Page $page
Page $page
) {
$this->page = $page;
// Init container
// Init scrolled window container
$this->gtk = new \GtkScrolledWindow;
$this->gtk->set_margin_start(
@ -41,21 +49,11 @@ class Content @@ -41,21 +49,11 @@ class Content
$this->_margin
);
// Init viewport
// to integrate scrolled window features for data label
// Init scrolled window viewport
$this->viewport = new Viewport(
$this
);
// Init data label
$this->data = new Data(
$this
);
$this->viewport->gtk->add(
$this->data->gtk
);
$this->gtk->add(
$this->viewport->gtk
);
@ -64,28 +62,73 @@ class Content @@ -64,28 +62,73 @@ class Content
$this->gtk->show();
}
public function refresh()
public function set(
?string $data,
?string $mime
): void
{
// @TODO
$this->_source = $data;
switch ($mime)
{
case Filesystem::MIME_TEXT_GEMINI:
$document = new Gemtext(
$this
);
$document->setSource(
$data
);
break;
case Filesystem::MIME_TEXT_PLAIN:
$document = new Plain(
$this
);
$document->setSource(
$data
);
break;
/* @TODO
case 'image/gif':
case 'image/jpeg':
case 'image/png':
case 'image/webp':
break;
*/
default:
$document = new Plain(
$this
);
$document->setSource(
_('MIME type not supported')
);
}
$this->viewport->gtk->add(
$document->gtk
);
//$this->gtk->show_all();
}
public function setGemtext(
?string $data = null,
?string &$title = null
): void
public function getSource(): ?string
{
$this->data->setGemtext(
$data,
$title
);
return $this->_source;
}
public function setPlain(
?string $data = null
): void
public function refresh()
{
$this->data->setPlain(
$data
);
// @TODO
}
}

140
src/Entity/Browser/Container/Page/Content/Data.php → src/Entity/Browser/Container/Page/Content/Gemtext.php

@ -7,115 +7,16 @@ namespace Yggverse\Yoda\Entity\Browser\Container\Page\Content; @@ -7,115 +7,16 @@ namespace Yggverse\Yoda\Entity\Browser\Container\Page\Content;
use \Yggverse\Gemtext\Document;
use \Yggverse\Net\Address;
class Data
class Gemtext extends \Yggverse\Yoda\Abstract\Entity\Browser\Container\Page\Content\Markup
{
public \GtkLabel $gtk;
// Extras
public ?string $raw = null;
// Dependencies
public \Yggverse\Yoda\Entity\Browser\Container\Page\Content $content;
// Defaults
private int $_wrap = 140;
public function __construct(
\Yggverse\Yoda\Entity\Browser\Container\Page\Content $content
) {
// Init dependency
$this->content = $content;
// Init markup label
$this->gtk = new \GtkLabel;
$this->gtk->set_use_markup(
true
);
$this->gtk->set_selectable(
true
);
$this->gtk->set_can_focus(
false
);
$this->gtk->set_track_visited_links(
true
);
$this->gtk->set_xalign(
0
);
$this->gtk->set_yalign(
0
);
// Render
$this->gtk->show();
// Init events
$this->gtk->connect(
'activate-link',
function(
\GtkLabel $label,
string $href
) {
// Format URL
$url = $this->_url(
$href
);
// Update request entry
$this->content->page->navbar->request->setValue(
$this->_url(
$href
)
);
// Update page
$this->content->page->update();
// Prevent propagation for supported protocols
if (in_array(
parse_url(
$url,
PHP_URL_SCHEME
),
[
'nex',
'gemini',
'file'
])
) return true;
}
);
}
public function setPlain(
string $value
): void
{
$this->gtk->set_markup(
sprintf(
'<tt>%s</tt>',
htmlspecialchars(
$value
)
)
);
$this->raw = $value;
}
public function setGemtext(
public function setSource(
string $value,
string | null &$title = null,
bool $preformatted = false
): void
{
$this->_source = $value;
$document = new Document(
$value
);
@ -330,8 +231,39 @@ class Data @@ -330,8 +231,39 @@ class Data
$line
)
);
}
protected function _onActivateLink(
\GtkLabel $label,
string $href
) {
// Format URL
$url = $this->_url(
$href
);
// Update request entry
$this->content->page->navbar->request->setValue(
$this->_url(
$href
)
);
$this->raw = $value;
// Update page
$this->content->page->update();
// Prevent propagation for supported protocols
if (in_array(
parse_url(
$url,
PHP_URL_SCHEME
),
[
'nex',
'gemini',
'file'
])
) return true;
}
private function _wrap(

24
src/Entity/Browser/Container/Page/Content/Plain.php

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Container\Page\Content;
class Plain extends \Yggverse\Yoda\Abstract\Entity\Browser\Container\Page\Content\Markup
{
public function setSource(
string $value
): void
{
$this->_source = $value;
$this->gtk->set_markup(
sprintf(
'<tt>%s</tt>',
htmlspecialchars(
$value
)
)
);
}
}

2
src/Entity/Browser/Menu/File/Save.php

@ -62,7 +62,7 @@ class Save @@ -62,7 +62,7 @@ class Save
{
file_put_contents(
$dialog->get_filename(),
$page->content->data->raw
$page->content->getSource()
);
}
}

Loading…
Cancel
Save