Skip to content

Instantly share code, notes, and snippets.

@mirkobrombin
Last active March 2, 2024 16:10
Show Gist options
  • Save mirkobrombin/31008cdf17bc5918438a24f5360a004a to your computer and use it in GitHub Desktop.
Save mirkobrombin/31008cdf17bc5918438a24f5360a004a to your computer and use it in GitHub Desktop.
# keyboard.py
#
# Copyright 2024 mirkobrombin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundationat version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import unicodedata
from gi.repository import Adw, Gtk
from gettext import gettext as _
from vanilla_installer.core.timezones import (
all_timezones,
get_location,
get_timezone_preview,
)
from vanilla_installer.utils.run_async import RunAsync
@Gtk.Template(resource_path="/org/vanillaos/Installer/gtk/widget-timezone.ui")
class TimezoneRow(Adw.ActionRow):
__gtype_name__ = "TimezoneRow"
select_button = Gtk.Template.Child()
country_label = Gtk.Template.Child()
def __init__(self, title, subtitle, tz_name, parent, **kwargs):
super().__init__(**kwargs)
self.title = title
self.subtitle = subtitle
self.parent = parent
self.tz_name = tz_name
tz_time, tz_date = get_timezone_preview(tz_name)
self.set_title(title)
self.set_subtitle(f"{tz_time} • {tz_date}")
self.country_label.set_label(tz_name)
self.select_button.connect("toggled", self.__on_check_button_toggled)
def __on_check_button_toggled(self, widget):
tz_split = self.tz_name.split("/", 1)
self.parent.select_timezone(tz_split[0], tz_split[1])
@Gtk.Template(resource_path="/org/vanillaos/Installer/gtk/default-timezone.ui")
class VanillaDefaultTimezone(Adw.Bin):
__gtype_name__ = "VanillaDefaultTimezone"
btn_next = Gtk.Template.Child()
entry_search_timezone = Gtk.Template.Child()
all_timezones_group = Gtk.Template.Child()
current_tz_label = Gtk.Template.Child()
current_location_label = Gtk.Template.Child()
def __init__(self, window, distro_info, key, step, **kwargs):
super().__init__(**kwargs)
self.window = window
self.distro_info = distro_info
self.key = key
self.step = step
self.selected_timezone = {"region": "Europe", "zone": None}
self.entry_search_timezone.connect("search-changed", self.__on_search_changed)
self.btn_next.connect("clicked", self.window.next)
self.window.carousel.connect("page-changed", self.timezone_verify)
self.__generate_timezone_list_widgets()
RunAsync(get_location, self.__set_located_timezone)
def select_timezone(self, region, zone):
self.selected_timezone["region"] = region
self.selected_timezone["zone"] = zone
self.current_tz_label.set_label(f"{region}/{zone}")
self.timezone_verify()
def timezone_verify(self, *args):
valid = (
self.selected_timezone["region"]
and self.selected_timezone["zone"] is not None
)
self.btn_next.set_sensitive(valid)
def __on_search_changed(self, entry):
search_text = (
unicodedata.normalize("NFD", entry.get_text().lower())
.encode("ascii", "ignore")
.decode("utf-8")
)
for child in self.all_timezones_group.get_children():
child.set_visible(
any(
search_text
in unicodedata.normalize("NFD", row.title.lower())
.encode("ascii", "ignore")
.decode("utf-8")
or search_text
in unicodedata.normalize("NFD", row.subtitle.lower())
.encode("ascii", "ignore")
.decode("utf-8")
for row in child.get_rows()
)
)
def __generate_timezone_list_widgets(self):
for region, countries in all_timezones.items():
for country, cities in countries.items():
country_tz_expander_row = Adw.ExpanderRow(
title=country, subtitle=region
)
self.all_timezones_group.add(country_tz_expander_row)
for city, tzname in cities.items():
timezone_row = TimezoneRow(city, country, tzname, self)
country_tz_expander_row.add_row(timezone_row)
def __set_located_timezone(self, result, error):
if result:
current_city = result.get_city_name()
current_country = result.get_country_name()
for child in self.all_timezones_group.get_children():
for row in child.get_rows():
if current_city == row.title and current_country == row.subtitle:
self.select_timezone(current_country, current_city)
row.select_button.set_active(True)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment