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 @@
<?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;
use \Yggverse\Yoda\Entity\Browser\Container\Page\Response; use \Yggverse\Yoda\Entity\Browser\Container\Page\Response;
use \Yggverse\Yoda\Model\Connection; use \Yggverse\Yoda\Model\Connection;
use \Yggverse\Yoda\Model\Filesystem;
class Page class Page
{ {
@ -200,57 +199,10 @@ class Page
); );
// Update content // Update content
switch ($connection->getMime()) $this->content->set(
{ $connection->getData(),
case Filesystem::MIME_TEXT_GEMINI: $connection->getMime()
);
$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')
);
}
// Hide progressbar // Hide progressbar
$this->progressbar->hide(); $this->progressbar->hide();
@ -271,7 +223,7 @@ class Page
); );
// Update content // Update content
$this->content->setGemtext( $this->content->set(
_('Response time reached') _('Response time reached')
); );

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

@ -4,29 +4,37 @@ declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Container\Page; 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\Entity\Browser\Container\Page\Content\Viewport;
use \Yggverse\Yoda\Model\Filesystem;
class Content class Content
{ {
public \GtkScrolledWindow $gtk; public \GtkScrolledWindow $gtk;
// Dependencies // Dependencies
public \Yggverse\Yoda\Entity\Browser\Container\Page $page; public Page $page;
// Requirements // Requirements
public \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Data $data; public Viewport $viewport;
public \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Viewport $viewport;
// Defaults // Defaults
private int $_margin = 8; private int $_margin = 8;
// Extras
private ?string $_source = null;
public function __construct( public function __construct(
\Yggverse\Yoda\Entity\Browser\Container\Page $page Page $page
) { ) {
$this->page = $page; $this->page = $page;
// Init container // Init scrolled window container
$this->gtk = new \GtkScrolledWindow; $this->gtk = new \GtkScrolledWindow;
$this->gtk->set_margin_start( $this->gtk->set_margin_start(
@ -41,21 +49,11 @@ class Content
$this->_margin $this->_margin
); );
// Init viewport // Init scrolled window viewport
// to integrate scrolled window features for data label
$this->viewport = new Viewport( $this->viewport = new Viewport(
$this $this
); );
// Init data label
$this->data = new Data(
$this
);
$this->viewport->gtk->add(
$this->data->gtk
);
$this->gtk->add( $this->gtk->add(
$this->viewport->gtk $this->viewport->gtk
); );
@ -64,28 +62,73 @@ class Content
$this->gtk->show(); $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( public function getSource(): ?string
?string $data = null,
?string &$title = null
): void
{ {
$this->data->setGemtext( return $this->_source;
$data,
$title
);
} }
public function setPlain( public function refresh()
?string $data = null
): void
{ {
$this->data->setPlain( // @TODO
$data
);
} }
} }

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;
use \Yggverse\Gemtext\Document; use \Yggverse\Gemtext\Document;
use \Yggverse\Net\Address; use \Yggverse\Net\Address;
class Data class Gemtext extends \Yggverse\Yoda\Abstract\Entity\Browser\Container\Page\Content\Markup
{ {
public \GtkLabel $gtk; public function setSource(
// 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(
string $value, string $value,
string | null &$title = null, string | null &$title = null,
bool $preformatted = false bool $preformatted = false
): void ): void
{ {
$this->_source = $value;
$document = new Document( $document = new Document(
$value $value
); );
@ -330,8 +231,39 @@ class Data
$line $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( private function _wrap(

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

@ -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
{ {
file_put_contents( file_put_contents(
$dialog->get_filename(), $dialog->get_filename(),
$page->content->data->raw $page->content->getSource()
); );
} }
} }

Loading…
Cancel
Save