Gradience/gradience/welcome.py

229 lines
7.2 KiB
Python
Raw Normal View History

2022-08-20 23:17:40 +00:00
# welcome.py
#
# Change the look of Adwaita, with ease
# Copyright (C) 2022 Gradience Team
#
# 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 Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 <https://www.gnu.org/licenses/>.
2022-09-07 15:51:43 +00:00
import sys
2022-08-19 21:50:21 +00:00
import time
2022-09-07 16:00:33 +00:00
from gi.repository import Gtk, Adw, Gio
2022-09-13 16:29:43 +00:00
from .modules.run_async import RunAsync
from .modules.utils import buglog
2022-09-07 15:51:53 +00:00
from .modules.flatpak_overrides import (
create_gtk_user_override,
2022-09-07 16:00:50 +00:00
)
from .constants import rootdir, app_id, version
2022-08-19 22:09:04 +00:00
2022-08-19 21:28:32 +00:00
@Gtk.Template(resource_path=f"{rootdir}/ui/welcome.ui")
class GradienceWelcomeWindow(Adw.Window):
2022-08-19 22:16:25 +00:00
__gtype_name__ = "GradienceWelcomeWindow"
2022-08-19 21:28:32 +00:00
2022-08-19 22:08:44 +00:00
settings = Gtk.Settings.get_default()
2022-08-19 21:28:32 +00:00
carousel = Gtk.Template.Child()
2022-08-19 21:28:32 +00:00
btn_close = Gtk.Template.Child()
btn_back = Gtk.Template.Child()
btn_next = Gtk.Template.Child()
btn_install = Gtk.Template.Child()
2022-09-16 20:34:51 +00:00
btn_agree = Gtk.Template.Child()
switch_system = Gtk.Template.Child()
switch_adw_gtk3 = Gtk.Template.Child()
2022-08-19 21:28:32 +00:00
progressbar = Gtk.Template.Child()
img_welcome = Gtk.Template.Child()
label_skip = Gtk.Template.Child()
images = [
2022-08-19 22:17:22 +00:00
f"{rootdir}/images/welcome.svg",
2022-08-20 15:38:27 +00:00
f"{rootdir}/images/welcome-dark.svg",
2022-08-19 21:28:32 +00:00
]
carousel_pages = [
"welcome", # 0
"release", # 1
"agreement", # 2
"gradience", # 3
"configure", # 4
"download", # 5
"finish", # 6
]
2022-09-22 08:34:42 +00:00
page_welcome = Gtk.Template.Child()
page_release = Gtk.Template.Child()
2022-09-22 08:34:42 +00:00
def __init__(self, window, update=False, **kwargs) -> None:
2022-08-19 22:16:25 +00:00
super().__init__(**kwargs)
2022-08-19 21:28:32 +00:00
self.set_transient_for(window)
self.update = update
2022-08-19 21:28:32 +00:00
# common variables and references
self.window = window
2022-09-03 15:21:22 +00:00
self.gio_settings = Gio.Settings(app_id)
2022-08-19 21:28:32 +00:00
# connect signals
2022-08-19 22:08:44 +00:00
self.connect("close-request", self.quit)
2022-09-07 15:51:53 +00:00
self.carousel.connect("page-changed", self.page_changed)
2022-08-19 22:08:44 +00:00
self.btn_close.connect("clicked", self.close_window)
self.btn_back.connect("clicked", self.previous_page)
self.btn_next.connect("clicked", self.next_page)
self.btn_install.connect("clicked", self.install_runner)
2022-09-16 20:34:51 +00:00
self.btn_agree.connect("clicked", self.agree)
2022-08-19 22:08:44 +00:00
self.settings.connect(
2022-09-07 15:51:53 +00:00
"notify::gtk-application-prefer-dark-theme", self.theme_changed
)
2022-08-19 21:28:32 +00:00
2022-09-22 08:34:42 +00:00
if self.update:
2022-09-23 15:27:11 +00:00
self.page_welcome.set_title(_("Thanks for updating Gradience!"))
2022-09-22 08:34:42 +00:00
self.page_release.set_title(f"Gradience {version}")
2022-08-19 21:28:32 +00:00
self.btn_close.set_sensitive(False)
2022-08-19 22:08:44 +00:00
if self.settings.get_property("gtk-application-prefer-dark-theme"):
2022-08-19 21:28:32 +00:00
self.img_welcome.set_from_resource(self.images[1])
2022-08-19 22:08:44 +00:00
self.page_changed()
2022-08-19 21:28:32 +00:00
2022-08-19 22:08:44 +00:00
def theme_changed(self, settings, key):
2022-08-19 21:28:54 +00:00
self.img_welcome.set_from_resource(
2022-09-07 15:51:53 +00:00
self.images[settings.get_property(
"gtk-application-prefer-dark-theme")]
)
2022-08-19 21:28:32 +00:00
2022-08-19 22:08:44 +00:00
def get_page(self, index):
2022-08-19 21:28:32 +00:00
return self.carousel_pages[index]
2022-08-19 22:08:44 +00:00
def page_changed(self, widget=False, index=0, *_args):
2022-08-19 21:28:32 +00:00
"""
This function is called on first load and when the user require
to change the page. It sets the widgets' status according to
the step of the onboard progress.
"""
2022-08-19 22:08:44 +00:00
page = self.get_page(index)
2022-08-19 21:28:32 +00:00
if page == "finish":
self.btn_back.set_visible(False)
self.btn_next.set_visible(False)
2022-09-16 20:34:51 +00:00
elif page == "agreement":
self.btn_back.set_visible(True)
self.btn_next.set_visible(False)
self.btn_agree.set_visible(True)
2022-08-19 21:28:32 +00:00
elif page == "download":
self.btn_back.set_visible(True)
self.btn_next.set_visible(False)
self.btn_install.set_visible(True)
elif page == "welcome":
self.btn_back.set_visible(False)
self.btn_next.set_visible(True)
else:
self.btn_back.set_visible(True)
self.btn_next.set_visible(True)
2022-09-16 20:34:51 +00:00
def agree(self, widget):
self.btn_back.set_visible(False)
self.btn_next.set_visible(True)
self.btn_install.set_visible(False)
if self.update:
self.window.last_opened_version = self.window.settings.set_string(
"last-opened-version", version
)
self.btn_close.set_sensitive(True)
self.label_skip.set_visible(False)
self.next_page(index=5)
else:
self.next_page()
2022-08-19 21:28:32 +00:00
@staticmethod
2022-08-19 22:08:44 +00:00
def quit(widget=False):
2022-09-07 15:51:43 +00:00
sys.exit()
2022-08-19 21:28:32 +00:00
# TODO: Add adw-gtk3 check
@staticmethod
def check_adw_gtk3():
buglog("check if adw-gtk3 installed")
2022-08-20 16:34:24 +00:00
return True
def adw_gtk3(self):
2022-08-20 16:37:52 +00:00
if not self.check_adw_gtk3(): # install
buglog("install adw-gtk3")
2022-08-20 16:34:24 +00:00
def configure_system(self):
buglog("configure system")
2022-09-03 15:21:22 +00:00
self.allow_flatpak_theming_user_toggled()
def allow_flatpak_theming_user_toggled(self, *args):
create_gtk_user_override(self, self.gio_settings, "gtk4")
2022-08-20 16:34:24 +00:00
2022-08-19 22:08:44 +00:00
def install_runner(self, widget):
2022-08-19 21:28:32 +00:00
def set_completed(result, error=False):
self.label_skip.set_visible(False)
self.btn_close.set_sensitive(True)
2022-08-19 21:54:56 +00:00
self.window.settings.set_boolean("first-run", False)
2022-09-22 05:13:26 +00:00
self.window.last_opened_version = self.window.settings.set_string(
"last-opened-version", version
)
2022-08-19 22:08:44 +00:00
self.next_page()
2022-08-19 21:28:32 +00:00
2022-08-19 22:08:44 +00:00
self.installing = True
2022-08-19 21:28:32 +00:00
self.btn_back.set_visible(False)
self.btn_next.set_visible(False)
self.btn_install.set_visible(False)
self.progressbar.set_visible(True)
self.carousel.set_allow_long_swipes(False)
self.carousel.set_allow_mouse_drag(False)
self.carousel.set_allow_scroll_wheel(False)
self.set_deletable(False)
2022-08-19 22:43:07 +00:00
def install():
2022-08-20 16:34:24 +00:00
if self.switch_adw_gtk3.get_active():
self.adw_gtk3()
2022-08-20 16:37:52 +00:00
2022-08-20 16:37:19 +00:00
if self.switch_system.get_active():
2022-08-20 16:34:24 +00:00
self.configure_system()
2022-08-19 21:28:32 +00:00
2022-08-19 22:43:07 +00:00
RunAsync(self.pulse)
RunAsync(
install,
callback=set_completed,
)
2022-08-19 22:43:33 +00:00
2022-09-22 08:34:42 +00:00
def previous_page(self, widget=False, index=None):
if index is None:
index = int(self.carousel.get_position())
2022-08-19 21:28:32 +00:00
previous_page = self.carousel.get_nth_page(index - 1)
self.carousel.scroll_to(previous_page, True)
def next_page(self, widget=False, index=None):
if index is None:
index = int(self.carousel.get_position())
2022-08-19 21:28:32 +00:00
next_page = self.carousel.get_nth_page(index + 1)
self.carousel.scroll_to(next_page, True)
def pulse(self):
# This function update the progress bar every 1s.
while True:
2022-09-07 15:51:53 +00:00
time.sleep(0.5)
2022-08-19 21:28:32 +00:00
self.progressbar.pulse()
2022-08-19 22:08:44 +00:00
def close_window(self, widget):
2022-08-19 21:28:32 +00:00
self.destroy()
2022-08-19 22:09:04 +00:00
self.window.present()