diff --git a/src/Entity/Browser/Container/Page/Auth.php b/src/Entity/Browser/Container/Page/Auth.php index f124e3b5..59626a4f 100644 --- a/src/Entity/Browser/Container/Page/Auth.php +++ b/src/Entity/Browser/Container/Page/Auth.php @@ -7,7 +7,6 @@ namespace Yggverse\Yoda\Entity\Browser\Container\Page; use \Exception; use \GtkButtonsType; use \GtkDialogFlags; -use \GtkEntry; use \GtkLabel; use \GtkMessageDialog; use \GtkMessageType; @@ -33,47 +32,17 @@ class Auth public const DIALOG_FORMAT_SECONDARY_TEXT = 'Select identity'; public const DIALOG_MESSAGE_FORMAT = 'Authorization'; public const DIALOG_CONTENT_OPTION_LABEL_CREATE = 'Create new for this resource'; - public const DIALOG_CONTENT_OPTION_LABEL_IDENTITY_NAME = '#%d (%s)'; - public const DIALOG_CONTENT_OPTION_LABEL_IDENTITY_NAME_DEFAULT = '#%d (no name)'; public const DIALOG_CONTENT_OPTION_MARGIN = 8; public const DIALOG_CONTENT_SPACING = 1; - public const DIALOG_CONTENT_OPTION_NAME_ALIGNMENT = 0.5; - public const DIALOG_CONTENT_OPTION_NAME_MARGIN = 12; - public const DIALOG_CONTENT_OPTION_NAME_PLACEHOLDER = 'Local name (optional)'; - // Extras private array $_options = []; // GtkRadioButton - private GtkEntry $_name; public function __construct( Page $page ) { // Init dependencies $this->page = $page; - - // Init extras - $this->_name = new GtkEntry; - - $this->_name->set_alignment( - $this::DIALOG_CONTENT_OPTION_NAME_ALIGNMENT - ); - - $this->_name->set_placeholder_text( - _($this::DIALOG_CONTENT_OPTION_NAME_PLACEHOLDER) - ); - - $this->_name->set_margin_start( - $this::DIALOG_CONTENT_OPTION_NAME_MARGIN - ); - - $this->_name->set_margin_end( - $this::DIALOG_CONTENT_OPTION_NAME_MARGIN - ); - - $this->_name->set_margin_bottom( - $this::DIALOG_CONTENT_OPTION_NAME_MARGIN - ); } public function dialog(): bool @@ -103,14 +72,18 @@ class Auth ); // Init new certificate option - $this->_options[0] = $this->_option( - _($this::DIALOG_CONTENT_OPTION_LABEL_CREATE) + $this->_options[0] = new Auth\Option\Identity( + $this ); - $this->_options[0]->join_group( + $this->_options[0]->setGroup( $this->_options[0] ); + $this->_options[0]->setLabel( + 0, _($this::DIALOG_CONTENT_OPTION_LABEL_CREATE) + ); + // Search database for auth records foreach ($this->page->container->browser->database->auth->like( sprintf( @@ -122,20 +95,18 @@ class Auth // Get related identity records if ($identity = $this->page->container->browser->database->identity->get($auth->identity)) { - $this->_options[$identity->id] = $this->_option( - $identity->name ? sprintf( - _($this::DIALOG_CONTENT_OPTION_LABEL_IDENTITY_NAME), - $identity->id, - $identity->name - ) : sprintf( - _($this::DIALOG_CONTENT_OPTION_LABEL_IDENTITY_NAME_DEFAULT), - $identity->id - ) + $this->_options[$identity->id] = new Auth\Option\Identity( + $this ); - $this->_options[$identity->id]->join_group( + $this->_options[$identity->id]->setGroup( $this->_options[0] ); + + $this->_options[$identity->id]->setLabel( + $identity->id, + $identity->name + ); } } @@ -151,15 +122,15 @@ class Auth { // Append option $content->add( - $option + $option->gtk ); - // Is new - if (!$id) + // Is new and option has name entity + if (!$id && !is_null($option->name)) { // Append name entry after new identity option $content->add( - $this->_name, + $option->name, true, true, 0 @@ -173,7 +144,7 @@ class Auth ); // Set margin - $option->set_margin_bottom( + $option->gtk->set_margin_bottom( self::DIALOG_CONTENT_OPTION_MARGIN ); } @@ -193,7 +164,7 @@ class Auth // Find active option foreach ($this->_options as $id => $option) { - if ($option->get_active()) + if ($option->gtk->get_active()) { // Auth if ($id) @@ -249,50 +220,21 @@ class Auth return false; } - private function _option( - string $label, - int $margin = self::DIALOG_CONTENT_OPTION_MARGIN - ): GtkRadioButton + public function refresh(): void { - $option = new GtkRadioButton; - - $option->set_label( - $label - ); - - $option->set_margin_top( - $margin - ); - - $option->set_margin_start( - $margin - ); - - $option->set_margin_end( - $margin - ); - - $option->connect( - 'toggled', - function(): void + // Detect active option + foreach ($this->_options as $id => $option) + { + // Is new and option has name entity + if (!$id && !is_null($option->name)) { - // Detect active option - foreach ($this->_options as $id => $option) - { - // Is new - if (!$id) - { - // Update sensibility - $this->_name->set_sensitive( - $option->get_active() - ); + // Update sensibility + $option->name->gtk->set_sensitive( + $option->gtk->get_active() + ); - break; - } - } + break; } - ); - - return $option; + } } } \ No newline at end of file diff --git a/src/Entity/Browser/Container/Page/Auth/Option/Identity.php b/src/Entity/Browser/Container/Page/Auth/Option/Identity.php new file mode 100644 index 00000000..12cf72bd --- /dev/null +++ b/src/Entity/Browser/Container/Page/Auth/Option/Identity.php @@ -0,0 +1,96 @@ +auth = $auth; + + // Init GTK + $this->gtk = new GtkRadioButton; + + $this->gtk->set_margin_top( + $this::MARGIN + ); + + $this->gtk->set_margin_start( + $this::MARGIN + ); + + $this->gtk->set_margin_end( + $this::MARGIN + ); + + $this->gtk->show(); + + // Connect events + $this->gtk->connect( + 'toggled', + function(): void + { + $this->auth->refresh(); + } + ); + } + + public function setGroup( + Identity $identity + ): void + { + $this->gtk->join_group( + $identity->gtk + ); + } + + public function setLabel( + int $id, + ?string $label = null + ): void + { + $this->gtk->set_label( + $label ? sprintf( + $this::LABEL_DEFAULT, + $id, + $label + ) : sprintf( + $this::LABEL_NO_NAME, + $id + ) + ); + } + + public function setName( + string $label + ): void + { + $this->name = new Identity\Name( + $this + ); + } +} \ No newline at end of file diff --git a/src/Entity/Browser/Container/Page/Auth/Option/Identity/Name.php b/src/Entity/Browser/Container/Page/Auth/Option/Identity/Name.php new file mode 100644 index 00000000..7eeb6d32 --- /dev/null +++ b/src/Entity/Browser/Container/Page/Auth/Option/Identity/Name.php @@ -0,0 +1,53 @@ +identity = $identity; + + // Init GTK + $this->gtk = new GtkEntry; + + $this->_name->set_alignment( + $this::ALIGNMENT + ); + + $this->_name->set_placeholder_text( + _($this::PLACEHOLDER) + ); + + $this->_name->set_margin_start( + $this::MARGIN + ); + + $this->_name->set_margin_end( + $this::MARGIN + ); + + $this->_name->set_margin_bottom( + $this::MARGIN + ); + + $this->gtk->show(); + } +} \ No newline at end of file