2022-10-10 20:34:50 +00:00
|
|
|
# option_row.py
|
2022-07-17 11:46:05 +00:00
|
|
|
#
|
2022-08-11 08:16:44 +00:00
|
|
|
# Change the look of Adwaita, with ease
|
2023-05-04 13:24:18 +00:00
|
|
|
# Copyright (C) 2022-2023, Gradience Team
|
2022-07-17 11:46:05 +00:00
|
|
|
#
|
2022-08-11 08:16:44 +00:00
|
|
|
# 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.
|
2022-08-11 16:39:52 +00:00
|
|
|
#
|
2022-08-11 08:16:44 +00:00
|
|
|
# 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.
|
2022-08-11 16:39:52 +00:00
|
|
|
#
|
2022-08-11 08:16:44 +00:00
|
|
|
# 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-07-17 11:46:05 +00:00
|
|
|
|
2022-07-24 18:43:32 +00:00
|
|
|
from gi.repository import Gtk, Gdk, Adw
|
2022-07-17 11:46:05 +00:00
|
|
|
|
2023-01-05 20:15:01 +00:00
|
|
|
from gradience.backend.utils.colors import rgb_to_hash
|
2022-12-02 21:52:47 +00:00
|
|
|
from gradience.backend.constants import rootdir
|
2022-08-11 16:39:52 +00:00
|
|
|
|
2022-08-13 10:24:16 +00:00
|
|
|
|
2022-10-10 20:34:50 +00:00
|
|
|
@Gtk.Template(resource_path=f"{rootdir}/ui/option_row.ui")
|
|
|
|
class GradienceOptionRow(Adw.ActionRow):
|
|
|
|
__gtype_name__ = "GradienceOptionRow"
|
2022-07-17 11:46:05 +00:00
|
|
|
|
|
|
|
color_value = Gtk.Template.Child("color-value")
|
|
|
|
text_value = Gtk.Template.Child("text-value")
|
|
|
|
value_stack = Gtk.Template.Child("value-stack")
|
|
|
|
text_value_toggle = Gtk.Template.Child("text-value-toggle")
|
2022-08-10 19:16:44 +00:00
|
|
|
warning_button = Gtk.Template.Child("warning-button")
|
|
|
|
warning_label = Gtk.Template.Child("warning-label")
|
2022-07-17 11:46:05 +00:00
|
|
|
explanation_button = Gtk.Template.Child("explanation-button")
|
|
|
|
explanation_label = Gtk.Template.Child("explanation-label")
|
|
|
|
|
2023-05-04 13:24:18 +00:00
|
|
|
def __init__(self, name, title, explanation=None, adw_gtk3_support=None, update_var=None, **kwargs):
|
2022-07-17 11:46:05 +00:00
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
2023-01-05 20:15:01 +00:00
|
|
|
self.app = Gtk.Application.get_default()
|
|
|
|
|
2022-07-17 15:02:29 +00:00
|
|
|
self.set_name(name)
|
2022-07-17 11:46:05 +00:00
|
|
|
self.set_title(title)
|
2022-07-25 11:08:41 +00:00
|
|
|
self.set_subtitle("@" + name)
|
2022-08-10 19:16:44 +00:00
|
|
|
|
2023-05-04 13:24:18 +00:00
|
|
|
if adw_gtk3_support == "yes" or not adw_gtk3_support:
|
2022-08-10 19:16:44 +00:00
|
|
|
self.warning_button.set_visible(False)
|
|
|
|
elif adw_gtk3_support == "partial":
|
|
|
|
self.warning_button.add_css_class("warning")
|
2022-08-11 16:39:52 +00:00
|
|
|
self.warning_label.set_label(
|
2022-12-27 20:28:58 +00:00
|
|
|
_("This option is only partially supported by the adw-gtk3 theme.")
|
2022-09-07 15:51:36 +00:00
|
|
|
)
|
2022-08-10 19:16:44 +00:00
|
|
|
elif adw_gtk3_support == "no":
|
|
|
|
self.warning_button.add_css_class("error")
|
2022-08-11 16:39:52 +00:00
|
|
|
self.warning_label.set_label(
|
|
|
|
_("This option is not supported by the adw-gtk3 theme.")
|
|
|
|
)
|
2022-08-10 19:16:44 +00:00
|
|
|
|
2022-07-17 11:46:05 +00:00
|
|
|
self.explanation_label.set_label(explanation or "")
|
2023-05-04 13:24:18 +00:00
|
|
|
|
|
|
|
if not explanation:
|
2022-07-17 11:46:05 +00:00
|
|
|
self.explanation_button.set_visible(False)
|
|
|
|
|
2023-05-04 13:24:18 +00:00
|
|
|
self.update_var = update_var
|
|
|
|
|
|
|
|
def connect_signals(self, update_vars):
|
|
|
|
self.color_value.connect("color-set", self.on_color_value_changed, update_vars)
|
|
|
|
self.text_value.connect("changed", self.on_text_value_changed, update_vars)
|
|
|
|
|
|
|
|
def on_color_value_changed(self, _unused, update_vars, *_args):
|
2023-01-05 20:15:01 +00:00
|
|
|
color_value = self.color_value.get_rgba().to_string()
|
|
|
|
|
|
|
|
if color_value.startswith("rgb") or color_value.startswith("rgba"):
|
|
|
|
color_hex, alpha = rgb_to_hash(color_value)
|
|
|
|
if not alpha:
|
|
|
|
color_value = color_hex
|
|
|
|
|
2022-08-11 16:39:52 +00:00
|
|
|
self.update_value(
|
2023-05-04 13:24:18 +00:00
|
|
|
color_value, update_from="color_value", update_vars=update_vars
|
2022-08-11 16:39:52 +00:00
|
|
|
)
|
2022-07-17 12:25:36 +00:00
|
|
|
|
2023-05-04 13:24:18 +00:00
|
|
|
def on_text_value_changed(self, _unused, update_vars, *_args):
|
2023-01-05 20:15:01 +00:00
|
|
|
color_value = self.text_value.get_text()
|
2023-05-04 13:24:18 +00:00
|
|
|
|
2022-12-27 20:28:58 +00:00
|
|
|
self.update_value(
|
2023-05-04 13:24:18 +00:00
|
|
|
color_value, update_from="text_value", update_vars=update_vars
|
2022-12-27 20:28:58 +00:00
|
|
|
)
|
2022-07-17 12:25:36 +00:00
|
|
|
|
2022-07-17 11:46:05 +00:00
|
|
|
@Gtk.Template.Callback()
|
2022-07-25 14:35:40 +00:00
|
|
|
def on_text_value_toggled(self, *_args):
|
2022-07-24 18:43:32 +00:00
|
|
|
if self.text_value_toggle.get_active():
|
2022-07-17 11:46:05 +00:00
|
|
|
self.value_stack.set_visible_child(self.text_value)
|
|
|
|
else:
|
|
|
|
self.value_stack.set_visible_child(self.color_value)
|
|
|
|
|
2023-05-04 13:24:18 +00:00
|
|
|
def update_value(self, new_value, update_vars=False, **kwargs):
|
2022-07-30 05:10:31 +00:00
|
|
|
rgba = Gdk.RGBA()
|
2023-01-05 20:15:01 +00:00
|
|
|
is_app_ready = self.app.is_ready
|
|
|
|
|
2022-07-17 12:25:36 +00:00
|
|
|
if kwargs.get("update_from") != "text_value":
|
2022-07-30 05:10:31 +00:00
|
|
|
if rgba.parse(new_value):
|
2023-01-05 20:15:01 +00:00
|
|
|
self.text_value.set_text(new_value)
|
|
|
|
self.text_value_toggle.set_active(False)
|
2022-07-30 05:10:31 +00:00
|
|
|
else:
|
|
|
|
self.text_value.set_text(new_value)
|
2023-01-05 20:15:01 +00:00
|
|
|
self.text_value_toggle.set_active(True)
|
|
|
|
|
2022-07-17 12:25:36 +00:00
|
|
|
if kwargs.get("update_from") != "color_value":
|
|
|
|
if rgba.parse(new_value):
|
|
|
|
self.color_value.set_rgba(rgba)
|
2022-07-24 17:46:37 +00:00
|
|
|
self.color_value.set_tooltip_text(new_value)
|
2022-07-17 12:25:36 +00:00
|
|
|
else:
|
2022-07-30 05:10:31 +00:00
|
|
|
rgba.parse("rgba(0,0,0,0)")
|
2022-07-17 12:25:36 +00:00
|
|
|
self.color_value.set_rgba(rgba)
|
2023-01-05 20:15:01 +00:00
|
|
|
self.color_value.set_tooltip_text(_("Not a color, see text value"))
|
|
|
|
|
2023-05-04 13:24:18 +00:00
|
|
|
if update_vars == True:
|
|
|
|
if is_app_ready and kwargs.get("update_from") == "text_value" and new_value != "":
|
|
|
|
if self.update_var:
|
|
|
|
self.update_var[self.get_name()] = new_value
|
|
|
|
else:
|
|
|
|
self.app.variables[self.get_name()] = new_value
|
|
|
|
self.app.mark_as_dirty()
|
|
|
|
self.app.reload_variables()
|