mirror of
https://github.com/aclist/dztui.git
synced 2026-10-30 14:42:54 +01:00
feat: setup wizard (WIP)
This commit is contained in:
1 parent
f0e51fa758
commit
07ea73ef2e
17 files changed
+839
-61
No files matched your search
@@ -1,8 +1,10 @@
|
||||
from typing import Callable, TYPE_CHECKING
|
||||
from typing import Any, Callable, TYPE_CHECKING
|
||||
|
||||
from dzgui.api.servers import validate_ip
|
||||
from dzgui.const.constants import VIEW_CONCEAL, VIEW_REVEAL
|
||||
from dzgui.util.css import add_class, remove_class
|
||||
from dzgui.util.strings import connect_panel, lan_panel
|
||||
from dzgui.strings.errors import api_popover
|
||||
|
||||
import gi
|
||||
|
||||
@@ -140,3 +142,98 @@ class PortEntry(ValidatedEntry):
|
||||
)
|
||||
self.set_placeholder_text(lan_panel.placeholder)
|
||||
self.set_tooltip_text(lan_panel.entry_tooltip)
|
||||
|
||||
|
||||
# TODO: backport to Options page
|
||||
class APIEntry(Gtk.Box):
|
||||
def __init__(self) -> None:
|
||||
super().__init__(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
|
||||
|
||||
self.func: Callable | None = None
|
||||
|
||||
# TODO: strings
|
||||
self.entry = Gtk.Entry(
|
||||
width_chars=60, hexpand=True, placeholder_text="Enter API key"
|
||||
)
|
||||
self.entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, VIEW_REVEAL)
|
||||
self.entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY, True)
|
||||
self.entry.set_visibility(False)
|
||||
self.entry.connect("changed", self._on_text_changed)
|
||||
self.entry.connect("icon-release", self._on_icon_release)
|
||||
self.entry.connect("activate", self._on_field_activated)
|
||||
|
||||
self.submit = Gtk.Button(label="Submit")
|
||||
self.submit.set_sensitive(False)
|
||||
self.submit.connect("clicked", self._on_submit)
|
||||
|
||||
self.pop = Gtk.Popover()
|
||||
self.pop_label = Gtk.Label(
|
||||
label=api_popover,
|
||||
margin_start=10,
|
||||
margin_end=10,
|
||||
)
|
||||
self.pop.add(self.pop_label)
|
||||
# NOTE: render once to draw text in bubble
|
||||
self.pop.show_all()
|
||||
self.pop.set_margin_start(10)
|
||||
self.pop.set_relative_to(self.entry)
|
||||
self.pop.popdown()
|
||||
|
||||
for el in self.entry, self.submit:
|
||||
self.add(el)
|
||||
|
||||
def get_entry(self) -> None:
|
||||
return self.entry
|
||||
|
||||
def popup(self) -> None:
|
||||
self.pop.popup()
|
||||
|
||||
def get_submit(self) -> Gtk.Button:
|
||||
return self.submit
|
||||
|
||||
def _is_valid_text(self, text: str) -> bool:
|
||||
if text.isspace():
|
||||
return False
|
||||
if len(text) == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
def _on_text_changed(self, entry: Gtk.Entry) -> None:
|
||||
text = entry.get_text()
|
||||
if self._is_valid_text(text):
|
||||
self.submit.set_sensitive(True)
|
||||
else:
|
||||
self.submit.set_sensitive(False)
|
||||
|
||||
def _on_icon_release(
|
||||
self,
|
||||
widget: Gtk.Entry,
|
||||
icon_pos: Gtk.EntryIconPosition,
|
||||
event: Gdk.Event,
|
||||
) -> None:
|
||||
visible = widget.get_visibility()
|
||||
if visible:
|
||||
icon, state = VIEW_REVEAL, False
|
||||
else:
|
||||
icon, state = VIEW_CONCEAL, True
|
||||
widget.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, icon)
|
||||
widget.set_visibility(state)
|
||||
|
||||
def _on_field_activated(self, entry: Gtk.Entry) -> None:
|
||||
self.submit.emit("clicked")
|
||||
|
||||
def _on_submit(self, button: Gtk.Button) -> Any:
|
||||
if self.func is None:
|
||||
return
|
||||
text = self.entry.get_text()
|
||||
res = self.func(text)
|
||||
return res
|
||||
|
||||
def set_validation_func(self, func: Callable | None) -> None:
|
||||
self.func = func
|
||||
|
||||
def disable_button(self) -> None:
|
||||
self.submit.set_sensitive(False)
|
||||
|
||||
def enable_button(self) -> None:
|
||||
self.submit.set_sensitive(True)
|
||||
Reference in new issue
Block a user