/usr/lib/python2.7/dist-packages/gtweak/widgets.py is in gnome-tweak-tool 3.18.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | # This file is part of gnome-tweak-tool.
#
# Copyright (c) 2011 John Stowers
#
# gnome-tweak-tool 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.
#
# gnome-tweak-tool 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 gnome-tweak-tool. If not, see <http://www.gnu.org/licenses/>.
import logging
import os.path
from gi.repository import GLib, Gtk, Gdk, Gio, Pango
from gtweak.tweakmodel import Tweak, TweakGroup
from gtweak.gsettings import GSettingsSetting, GSettingsFakeSetting, GSettingsMissingError
from gtweak.gtksettings import GtkSettingsManager
from gtweak.gshellwrapper import GnomeShellFactory
UI_BOX_SPACING = 4
_shell = GnomeShellFactory().get_shell()
def build_label_beside_widget(txt, *widget, **kwargs):
"""
Builds a HBox containing widgets.
Optional Kwargs:
hbox: Use an existing HBox, not a new one
info: Informational text to be shown after the label
warning: Warning text to be shown after the label
"""
def make_image(icon, tip):
image = Gtk.Image.new_from_icon_name(icon, Gtk.IconSize.MENU)
image.set_tooltip_text(tip)
return image
def show_tooltip_when_ellipsized(label, x, y, keyboard_mode, tooltip):
layout = label.get_layout()
if layout.is_ellipsized():
tooltip.set_text(label.get_text())
return True
else:
return False
if kwargs.get("hbox"):
hbox = kwargs.get("hbox")
else:
hbox = Gtk.HBox()
hbox.props.spacing = UI_BOX_SPACING
lbl = Gtk.Label(label=txt)
lbl.props.ellipsize = Pango.EllipsizeMode.END
lbl.props.xalign = 0.0
lbl.set_has_tooltip(True)
lbl.connect("query-tooltip", show_tooltip_when_ellipsized)
hbox.pack_start(lbl, True, True, 0)
if kwargs.get("info"):
hbox.pack_start(
make_image("dialog-information-symbolic", kwargs.get("info")),
False, False, 0)
if kwargs.get("warning"):
hbox.pack_start(
make_image("dialog-warning-symbolic", kwargs.get("warning")),
False, False, 0)
for w in widget:
hbox.pack_start(w, False, False, 0)
#For Atk, indicate that the rightmost widget, usually the switch relates to the
#label. By convention this is true in the great majority of cases. Settings that
#construct their own widgets will need to set this themselves
lbl.set_mnemonic_widget(widget[-1])
return hbox
def build_combo_box_text(selected, *values):
"""
builds a GtkComboBox and model containing the supplied values.
@values: a list of 2-tuples (value, name)
"""
store = Gtk.ListStore(str, str)
store.set_sort_column_id(0, Gtk.SortType.ASCENDING)
selected_iter = None
for (val, name) in values:
_iter = store.append( (val, name) )
if val == selected:
selected_iter = _iter
combo = Gtk.ComboBox(model=store)
renderer = Gtk.CellRendererText()
combo.pack_start(renderer, True)
combo.add_attribute(renderer, 'markup', 1)
if selected_iter:
combo.set_active_iter(selected_iter)
return combo
def build_horizontal_sizegroup():
sg = Gtk.SizeGroup(mode=Gtk.SizeGroupMode.HORIZONTAL)
sg.props.ignore_hidden = True
return sg
def build_tight_button(stock_id):
button = Gtk.Button()
button.set_relief(Gtk.ReliefStyle.NONE)
button.set_focus_on_click(False)
button.add(Gtk.Image.new_from_stock(stock_id, Gtk.IconSize.MENU))
data = ".button {\n" \
"-GtkButton-default-border : 0px;\n" \
"-GtkButton-default-outside-border : 0px;\n" \
"-GtkButton-inner-border: 0px;\n" \
"-GtkWidget-focus-line-width : 0px;\n" \
"-GtkWidget-focus-padding : 0px;\n" \
"padding: 0px;\n" \
"}"
provider = Gtk.CssProvider()
provider.load_from_data(data)
# 600 = GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
button.get_style_context().add_provider(provider, 600)
return button
def adjust_schema_for_overrides(originalSchema, key, options):
if (_shell is None):
return originalSchema
if (_shell.mode == 'classic'):
overridesSchema = "org.gnome.shell.extensions.classic-overrides"
overridesFile = None
else:
overridesSchema = "org.gnome.shell.overrides"
overridesFile = "org.gnome.shell.gschema.xml"
try:
if (key in GSettingsSetting(overridesSchema, schema_filename=overridesFile).list_keys()):
options['schema_filename'] = overridesFile
return overridesSchema
except GSettingsMissingError, e:
logging.info("GSetting missing %s" % (e.message))
return originalSchema
class _GSettingsTweak(Tweak):
def __init__(self, name, schema_name, key_name, **options):
schema_name = adjust_schema_for_overrides(schema_name, key_name, options)
self.schema_name = schema_name
self.key_name = key_name
self._extra_info = None
if 'uid' not in options:
options['uid'] = key_name
try:
self.settings = GSettingsSetting(schema_name, **options)
Tweak.__init__(self,
name,
options.get("description",self.settings.schema_get_description(key_name)),
**options)
except GSettingsMissingError, e:
self.settings = GSettingsFakeSetting()
Tweak.__init__(self,"","")
self.loaded = False
logging.info("GSetting missing %s" % (e.message))
except KeyError:
self.settings = GSettingsFakeSetting()
Tweak.__init__(self,"","")
self.loaded = False
logging.info("GSettings missing key %s (key %s)" % (schema_name, key_name))
if options.get("logout_required") and self.loaded:
self.settings.connect("changed::%s" % key_name, self._on_changed_notify_logout)
def _on_changed_notify_logout(self, settings, key_name):
self.notify_logout()
@property
def extra_info(self):
if self._extra_info is None:
self._extra_info = self.settings.schema_get_summary(self.key_name)
return self._extra_info
class _DependableMixin(object):
def add_dependency_on_tweak(self, depends, depends_how):
if isinstance(depends, Tweak):
self._depends = depends
if depends_how is None:
depends_how = lambda x,kn: x.get_boolean(kn)
self._depends_how = depends_how
sensitive = self._depends_how(
depends.settings,
depends.key_name,
)
self.set_sensitive(sensitive)
depends.settings.connect("changed::%s" % depends.key_name, self._on_changed_depend)
def _on_changed_depend(self, settings, key_name):
sensitive = self._depends_how(settings,key_name)
self.set_sensitive(sensitive)
class ListBoxTweakGroup(Gtk.ListBox, TweakGroup):
def __init__(self, name, *tweaks, **options):
if 'uid' not in options:
options['uid'] = self.__class__.__name__
Gtk.ListBox.__init__(self,
selection_mode=Gtk.SelectionMode.NONE,
name=options['uid'])
self.get_style_context().add_class(
options.get('css_class','tweak-group'))
self.props.vexpand = False
self.props.valign = Gtk.Align.START
self._sg = Gtk.SizeGroup(mode=Gtk.SizeGroupMode.HORIZONTAL)
self._sg.props.ignore_hidden = True
TweakGroup.__init__(self, name, **options)
for t in tweaks:
self.add_tweak_row(t)
#FIXME: need to add remove_tweak_row and remove_tweak (which clears
#the search cache etc)
def add_tweak_row(self, t, position=None):
if self.add_tweak(t):
if isinstance(t, Gtk.ListBoxRow):
row = t
else:
row = Gtk.ListBoxRow(name=t.uid)
row.get_style_context().add_class("tweak")
if isinstance(t, Title):
row.get_style_context().add_class("title")
row.add(t)
if position is None:
self.add(row)
else:
self.insert(row, position)
if t.widget_for_size_group:
self._sg.add_widget(t.widget_for_size_group)
return row
class GSettingsCheckTweak(Gtk.Box, _GSettingsTweak, _DependableMixin):
def __init__(self, name, schema_name, key_name, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
_GSettingsTweak.__init__(self, name, schema_name, key_name, **options)
widget = Gtk.CheckButton.new_with_label(name)
self.settings.bind(
key_name,
widget,
"active", Gio.SettingsBindFlags.DEFAULT)
self.add(widget)
self.widget_for_size_group = None
self.add_dependency_on_tweak(
options.get("depends_on"),
options.get("depends_how")
)
class GSettingsSwitchTweak(Gtk.Box, _GSettingsTweak, _DependableMixin):
def __init__(self, name, schema_name, key_name, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
_GSettingsTweak.__init__(self, name, schema_name, key_name, **options)
w = Gtk.Switch()
self.settings.bind(key_name, w, "active", Gio.SettingsBindFlags.DEFAULT)
build_label_beside_widget(name, w, hbox=self)
self.widget_for_size_group = None
self.add_dependency_on_tweak(
options.get("depends_on"),
options.get("depends_how")
)
class GSettingsFontButtonTweak(Gtk.Box, _GSettingsTweak, _DependableMixin):
def __init__(self, name, schema_name, key_name, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
_GSettingsTweak.__init__(self, name, schema_name, key_name, **options)
w = Gtk.FontButton()
self.settings.bind(key_name, w, "font-name", Gio.SettingsBindFlags.DEFAULT)
build_label_beside_widget(name, w, hbox=self)
self.widget_for_size_group = w
class GSettingsRangeTweak(Gtk.Box, _GSettingsTweak, _DependableMixin):
def __init__(self, name, schema_name, key_name, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
_GSettingsTweak.__init__(self, name, schema_name, key_name, **options)
#returned variant is range:(min, max)
_min, _max = self.settings.get_range(key_name)[1]
w = Gtk.HScale.new_with_range(_min, _max, options.get('adjustment_step', 1))
self.settings.bind(key_name, w.get_adjustment(), "value", Gio.SettingsBindFlags.DEFAULT)
build_label_beside_widget(self.name, w, hbox=self)
self.widget_for_size_group = w
class GSettingsSpinButtonTweak(Gtk.Box, _GSettingsTweak, _DependableMixin):
def __init__(self, name, schema_name, key_name, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
_GSettingsTweak.__init__(self, name, schema_name, key_name, **options)
#returned variant is range:(min, max)
_min, _max = self.settings.get_range(key_name)[1]
adjustment = Gtk.Adjustment(value=0, lower=_min, upper=_max, step_increment=options.get('adjustment_step', 1))
w = Gtk.SpinButton()
w.set_adjustment(adjustment)
w.set_digits(options.get('digits', 0))
self.settings.bind(key_name, adjustment, "value", Gio.SettingsBindFlags.DEFAULT)
build_label_beside_widget(name, w, hbox=self)
self.widget_for_size_group = w
self.add_dependency_on_tweak(
options.get("depends_on"),
options.get("depends_how")
)
class GSettingsComboEnumTweak(Gtk.Box, _GSettingsTweak, _DependableMixin):
def __init__(self, name, schema_name, key_name, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
_GSettingsTweak.__init__(self, name, schema_name, key_name, **options)
_type, values = self.settings.get_range(key_name)
value = self.settings.get_string(key_name)
self.settings.connect('changed::'+self.key_name, self._on_setting_changed)
w = build_combo_box_text(value, *[(v,v.replace("-"," ").title()) for v in values])
w.connect('changed', self._on_combo_changed)
self.combo = w
build_label_beside_widget(name, w, hbox=self)
self.widget_for_size_group = w
def _values_are_different(self):
#to stop bouncing back and forth between changed signals. I suspect there must be a nicer
#Gio.settings_bind way to fix this
return self.settings.get_string(self.key_name) != \
self.combo.get_model().get_value(self.combo.get_active_iter(), 0)
def _on_setting_changed(self, setting, key):
assert key == self.key_name
val = self.settings.get_string(key)
model = self.combo.get_model()
for row in model:
if val == row[0]:
self.combo.set_active_iter(row.iter)
break
def _on_combo_changed(self, combo):
val = self.combo.get_model().get_value(self.combo.get_active_iter(), 0)
if self._values_are_different():
self.settings.set_string(self.key_name, val)
class GSettingsComboTweak(Gtk.Box, _GSettingsTweak, _DependableMixin):
def __init__(self, name, schema_name, key_name, key_options, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
_GSettingsTweak.__init__(self, name, schema_name, key_name, **options)
#check key_options is iterable
#and if supplied, check it is a list of 2-tuples
assert len(key_options) >= 0
if len(key_options):
assert len(key_options[0]) == 2
self._key_options = key_options
self.combo = build_combo_box_text(
self.settings.get_string(self.key_name),
*key_options)
self.combo.connect('changed', self._on_combo_changed)
self.settings.connect('changed::'+self.key_name, self._on_setting_changed)
build_label_beside_widget(name, self.combo,hbox=self)
self.widget_for_size_group = self.combo
def _on_setting_changed(self, setting, key):
assert key == self.key_name
val = self.settings.get_string(key)
model = self.combo.get_model()
for row in model:
if val == row[0]:
self.combo.set_active_iter(row.iter)
return
self.combo.set_active(-1)
def _on_combo_changed(self, combo):
_iter = combo.get_active_iter()
if _iter:
value = combo.get_model().get_value(_iter, 0)
self.settings.set_string(self.key_name, value)
@property
def extra_info(self):
if self._extra_info is None:
self._extra_info = self.settings.schema_get_summary(self.key_name)
self._extra_info += " " + " ".join(op[0] for op in self._key_options)
return self._extra_info
class FileChooserButton(Gtk.FileChooserButton):
def __init__(self, title, local_only, mimetypes):
Gtk.FileChooserButton.__init__(self, title=title)
if mimetypes:
f = Gtk.FileFilter()
for m in mimetypes:
f.add_mime_type(m)
self.set_filter(f)
#self.set_width_chars(15)
self.set_local_only(local_only)
self.set_action(Gtk.FileChooserAction.OPEN)
class GSettingsFileChooserButtonTweak(Gtk.Box, _GSettingsTweak, _DependableMixin):
def __init__(self, name, schema_name, key_name, local_only, mimetypes, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
_GSettingsTweak.__init__(self, name, schema_name, key_name, **options)
self.settings.connect('changed::'+self.key_name, self._on_setting_changed)
self.filechooser = FileChooserButton(name,local_only,mimetypes)
self.filechooser.set_uri(self.settings.get_string(self.key_name))
self.filechooser.connect("file-set", self._on_file_set)
build_label_beside_widget(name, self.filechooser, hbox=self)
self.widget_for_size_group = self.filechooser
def _values_are_different(self):
return self.settings.get_string(self.key_name) != self.filechooser.get_uri()
def _on_setting_changed(self, setting, key):
self.filechooser.set_uri(self.settings.get_string(key))
def _on_file_set(self, chooser):
uri = self.filechooser.get_uri()
if uri and self._values_are_different():
self.settings.set_string(self.key_name, uri)
class GetterSetterSwitchTweak(Gtk.Box, Tweak):
def __init__(self, name, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
Tweak.__init__(self, name, options.get("description",""), **options)
sw = Gtk.Switch()
sw.set_active(self.get_active())
sw.connect("notify::active", self._on_toggled)
build_label_beside_widget(name, sw, hbox=self)
def _on_toggled(self, sw, pspec):
self.set_active(sw.get_active())
def get_active(self):
raise NotImplementedError()
def set_active(self, v):
raise NotImplementedError()
class DarkThemeSwitcher(Gtk.Box, Tweak):
def __init__(self, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.VERTICAL)
Tweak.__init__(self, _("Enable dark theme for all applications"),
_("Enable the dark theme hint for all the applications in the session"),
**options)
self._gtksettings = GtkSettingsManager()
w = Gtk.Switch()
w.set_active(self._gtksettings.get_integer("gtk-application-prefer-dark-theme"))
title = _("Global Dark Theme")
description = _("Applications need to be restarted for change to take effect")
w.connect("notify::active", self._on_switch_changed)
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
hbox.props.spacing = UI_BOX_SPACING
lbl = Gtk.Label(label=title)
lbl.props.ellipsize = Pango.EllipsizeMode.END
lbl.props.xalign = 0.0
hbox.pack_start(lbl, True, True, 0)
hbox.pack_start(w, False, False, 0)
lbl_des = Gtk.Label()
lbl_des.props.xalign = 0.0
lbl_des.set_markup("<span size='x-small'>"+GLib.markup_escape_text(description)+"</span>")
self.pack_start(hbox, False, False, 0)
self.pack_start(lbl_des, False, False,0)
self.widget_for_size_group = None
def _on_switch_changed(self, switch, param):
active = switch.get_active()
try:
self._gtksettings.set_integer("gtk-application-prefer-dark-theme",
active)
except:
self.notify_information(_("Error writing setting"))
class Title(Gtk.Box, Tweak):
def __init__(self, name, desc, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
Tweak.__init__(self, name, desc, **options)
widget = Gtk.Label()
widget.set_markup("<b>"+GLib.markup_escape_text(name)+"</b>")
widget.props.xalign = 0.0
self.add(widget)
class GSettingsSwitchTweakValue(Gtk.Box, _GSettingsTweak):
def __init__(self, name, schema_name, key_name, **options):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
_GSettingsTweak.__init__(self, name, schema_name, key_name, **options)
sw = Gtk.Switch()
sw.set_active(self.get_active())
sw.connect("notify::active", self._on_toggled)
build_label_beside_widget(name, sw, hbox=self)
def _on_toggled(self, sw, pspec):
self.set_active(sw.get_active())
def set_active(self, v):
raise NotImplementedError()
def get_active(self):
raise NotImplementedError()
|