fix: add fallbacks for old libadwaita widgets
This commit is contained in:
+86
-6
@@ -184,6 +184,86 @@ class CompatBanner(Gtk.Revealer):
|
|||||||
self.set_reveal_child(revealed)
|
self.set_reveal_child(revealed)
|
||||||
|
|
||||||
|
|
||||||
|
class CompatToolbarView(Gtk.Box):
|
||||||
|
"""Fallback for `Adw.ToolbarView` (available since libadwaita 1.4)."""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
super().__init__(orientation=Gtk.Orientation.VERTICAL)
|
||||||
|
self._content: Gtk.Widget | None = None
|
||||||
|
|
||||||
|
def add_top_bar(self, widget: Gtk.Widget) -> None:
|
||||||
|
self.append(widget)
|
||||||
|
|
||||||
|
def set_content(self, widget: Gtk.Widget) -> None:
|
||||||
|
if self._content is not None:
|
||||||
|
self.remove(self._content)
|
||||||
|
self._content = widget
|
||||||
|
widget.set_vexpand(True)
|
||||||
|
self.append(widget)
|
||||||
|
|
||||||
|
|
||||||
|
class CompatEntryRow(Adw.ActionRow):
|
||||||
|
"""Fallback for `Adw.EntryRow` (available since libadwaita 1.2)."""
|
||||||
|
|
||||||
|
def __init__(self, title: str = ""):
|
||||||
|
super().__init__(title=title)
|
||||||
|
self._entry = Gtk.Entry()
|
||||||
|
self._entry.set_hexpand(True)
|
||||||
|
self.add_suffix(self._entry)
|
||||||
|
self.set_activatable_widget(self._entry)
|
||||||
|
|
||||||
|
def get_text(self) -> str:
|
||||||
|
return self._entry.get_text()
|
||||||
|
|
||||||
|
def set_text(self, text: str) -> None:
|
||||||
|
self._entry.set_text(text)
|
||||||
|
|
||||||
|
|
||||||
|
class CompatSwitchRow(Adw.ActionRow):
|
||||||
|
"""Fallback for `Adw.SwitchRow` (available since libadwaita 1.4)."""
|
||||||
|
|
||||||
|
def __init__(self, title: str = ""):
|
||||||
|
super().__init__(title=title)
|
||||||
|
self._switch = Gtk.Switch()
|
||||||
|
self._switch.set_valign(Gtk.Align.CENTER)
|
||||||
|
self.add_suffix(self._switch)
|
||||||
|
self.set_activatable_widget(self._switch)
|
||||||
|
|
||||||
|
def get_active(self) -> bool:
|
||||||
|
return self._switch.get_active()
|
||||||
|
|
||||||
|
def set_active(self, active: bool) -> None:
|
||||||
|
self._switch.set_active(active)
|
||||||
|
|
||||||
|
def connect_active_notify(self, callback: Callable[..., None]) -> None:
|
||||||
|
self._switch.connect("notify::active", callback)
|
||||||
|
|
||||||
|
|
||||||
|
def make_toolbar_view() -> Gtk.Widget:
|
||||||
|
if hasattr(Adw, "ToolbarView"):
|
||||||
|
return Adw.ToolbarView()
|
||||||
|
return CompatToolbarView()
|
||||||
|
|
||||||
|
|
||||||
|
def make_entry_row(title: str) -> Gtk.Widget:
|
||||||
|
if hasattr(Adw, "EntryRow"):
|
||||||
|
return Adw.EntryRow(title=title)
|
||||||
|
return CompatEntryRow(title=title)
|
||||||
|
|
||||||
|
|
||||||
|
def make_switch_row(title: str) -> Gtk.Widget:
|
||||||
|
if hasattr(Adw, "SwitchRow"):
|
||||||
|
return Adw.SwitchRow(title=title)
|
||||||
|
return CompatSwitchRow(title=title)
|
||||||
|
|
||||||
|
|
||||||
|
def connect_switch_row_active(row: Gtk.Widget, callback: Callable[..., None]) -> None:
|
||||||
|
if hasattr(row, "connect_active_notify"):
|
||||||
|
row.connect_active_notify(callback) # type: ignore[attr-defined]
|
||||||
|
else:
|
||||||
|
row.connect("notify::active", callback)
|
||||||
|
|
||||||
|
|
||||||
# ── Settings Dialog ─────────────────────────────────────────────
|
# ── Settings Dialog ─────────────────────────────────────────────
|
||||||
class SettingsWindow(Adw.Window):
|
class SettingsWindow(Adw.Window):
|
||||||
"""Preferences dialog using libadwaita widgets."""
|
"""Preferences dialog using libadwaita widgets."""
|
||||||
@@ -201,7 +281,7 @@ class SettingsWindow(Adw.Window):
|
|||||||
self.set_modal(True)
|
self.set_modal(True)
|
||||||
self.set_default_size(480, -1)
|
self.set_default_size(480, -1)
|
||||||
|
|
||||||
toolbar_view = Adw.ToolbarView()
|
toolbar_view = make_toolbar_view()
|
||||||
|
|
||||||
header = Adw.HeaderBar()
|
header = Adw.HeaderBar()
|
||||||
cancel_btn = Gtk.Button(label="Annulla")
|
cancel_btn = Gtk.Button(label="Annulla")
|
||||||
@@ -222,11 +302,11 @@ class SettingsWindow(Adw.Window):
|
|||||||
"locale di Bottles usata per l'installazione."
|
"locale di Bottles usata per l'installazione."
|
||||||
)
|
)
|
||||||
|
|
||||||
self.server_row = Adw.EntryRow(title="Indirizzo server")
|
self.server_row = make_entry_row("Indirizzo server")
|
||||||
self.server_row.set_text(config.server_url)
|
self.server_row.set_text(config.server_url)
|
||||||
group.add(self.server_row)
|
group.add(self.server_row)
|
||||||
|
|
||||||
self.bottles_row = Adw.EntryRow(title="Cartella Bottles")
|
self.bottles_row = make_entry_row("Cartella Bottles")
|
||||||
self.bottles_row.set_text(config.bottles_dir)
|
self.bottles_row.set_text(config.bottles_dir)
|
||||||
group.add(self.bottles_row)
|
group.add(self.bottles_row)
|
||||||
|
|
||||||
@@ -444,9 +524,9 @@ class CellarWindow(Adw.ApplicationWindow):
|
|||||||
self.bottles_info_row.add_css_class("property")
|
self.bottles_info_row.add_css_class("property")
|
||||||
info_group.add(self.bottles_info_row)
|
info_group.add(self.bottles_info_row)
|
||||||
|
|
||||||
self.replace_row = Adw.SwitchRow(title="Sostituisci installazioni esistenti")
|
self.replace_row = make_switch_row("Sostituisci installazioni esistenti")
|
||||||
self.replace_row.set_active(self.config.replace_existing)
|
self.replace_row.set_active(self.config.replace_existing)
|
||||||
self.replace_row.connect("notify::active", self._store_replace_preference)
|
connect_switch_row_active(self.replace_row, self._store_replace_preference)
|
||||||
info_group.add(self.replace_row)
|
info_group.add(self.replace_row)
|
||||||
|
|
||||||
content_box.append(info_group)
|
content_box.append(info_group)
|
||||||
@@ -492,7 +572,7 @@ class CellarWindow(Adw.ApplicationWindow):
|
|||||||
self.toast_overlay.set_child(outer_box)
|
self.toast_overlay.set_child(outer_box)
|
||||||
|
|
||||||
# ── ToolbarView ──
|
# ── ToolbarView ──
|
||||||
toolbar_view = Adw.ToolbarView()
|
toolbar_view = make_toolbar_view()
|
||||||
toolbar_view.add_top_bar(header)
|
toolbar_view.add_top_bar(header)
|
||||||
toolbar_view.set_content(self.toast_overlay)
|
toolbar_view.set_content(self.toast_overlay)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user