6
from gettext import gettext as _
7
from gi.repository import Gio, Gtk, Gdk
8
from optparse import OptionParser
16
class Direction(object):
18
def __init__(self, name):
21
self.value = getattr(Gdk.ScrollDirection, name.upper())
24
class GtkScroller(object):
26
exit_code = EXIT_WITH_FAILURE
28
def __init__(self, directions, edge_scroll=False):
29
self.directions = directions
30
self.edge_scroll = edge_scroll
31
self.touchpad_key = "org.gnome.settings-daemon.peripherals.touchpad"
32
self.horiz_scroll_key = True
33
source = Gio.SettingsSchemaSource.get_default()
34
if not source.lookup(self.touchpad_key, True):
35
self.touchpad_key = "org.gnome.desktop.peripherals.touchpad"
36
self.horiz_scroll_key = False
37
self.touchpad_settings = Gio.Settings.new(self.touchpad_key)
39
# Initialize GTK constants
40
self.ICON_SIZE = Gtk.IconSize.BUTTON
41
self.ICON_TESTED = Gtk.STOCK_YES
42
self.ICON_UNTESTED = Gtk.STOCK_INDEX
43
self.ICON_NOT_REQUIRED = Gtk.STOCK_REMOVE
45
self.button_factory = Gtk.Button
46
self.hbox_factory = Gtk.HBox
47
self.image_factory = Gtk.Image
48
self.label_factory = Gtk.Label
49
self.vbox_factory = Gtk.VBox
53
window.set_type_hint(Gdk.WindowType.TOPLEVEL)
55
Gdk.EventMask.SCROLL_MASK | Gdk.EventMask.SMOOTH_SCROLL_MASK)
56
window.set_size_request(200, 100)
57
window.set_resizable(False)
58
window.set_title(_("Type Text"))
59
window.connect("delete-event", lambda w, e: self.quit())
60
window.connect("scroll-event", self.on_scroll)
63
# Add common widgets to the window.
64
vbox = self._add_vbox(window)
65
self.label = self._add_label(vbox)
66
button_hbox = self._add_hbox(vbox)
67
validation_hbox = self._add_hbox(vbox)
68
self.status = self._add_label(vbox)
69
self.exit_button = self._add_button(vbox, Gtk.STOCK_CLOSE)
70
self.exit_button.connect("clicked", lambda w: self.quit())
72
# Add widgets for each direction.
74
for direction in self.directions:
75
self._add_label(button_hbox, direction.name)
76
self.icons[direction] = self._add_image(
77
validation_hbox, Gtk.STOCK_INDEX)
80
_("Please move the mouse cursor to this window.") +
82
_("Then scroll in each direction on your touchpad."))
84
def _add_button(self, context, stock):
85
button = self.button_factory(stock=stock)
90
def _add_hbox(self, context, spacing=4):
91
hbox = self.hbox_factory()
97
def _add_image(self, context, stock):
98
image = self.image_factory(stock=stock, icon_size=self.ICON_SIZE)
103
def _add_label(self, context, text=None):
104
label = self.label_factory()
106
label.set_size_request(0, 0)
107
label.set_line_wrap(True)
113
def _add_vbox(self, context):
114
vbox = self.vbox_factory()
115
vbox.set_homogeneous(False)
122
# Save touchpad settings.
123
if self.horiz_scroll_key:
124
self.saved_horiz_scroll_enabled = \
125
self.touchpad_settings.get_boolean("horiz-scroll-enabled")
126
self.saved_scroll_method = self.touchpad_settings.get_string(
129
# Set touchpad settings.
130
if self.horiz_scroll_key:
131
self.touchpad_settings.set_boolean("horiz-scroll-enabled", True)
133
self.touchpad_settings.set_string(
134
"scroll-method", "edge-scrolling")
139
# Reset touchpad settings.
140
if self.horiz_scroll_key:
141
self.touchpad_settings.set_boolean(
142
"horiz-scroll-enabled", self.saved_horiz_scroll_enabled)
143
self.touchpad_settings.set_string(
144
"scroll-method", self.saved_scroll_method)
148
def show_text(self, text, widget=None):
151
widget.set_text(text)
153
def found_direction(self, direction):
154
direction.tested = True
155
self.icons[direction].set_from_stock(
156
self.ICON_TESTED, size=self.ICON_SIZE)
157
self.check_directions()
159
def check_directions(self):
160
if all([direction.tested for direction in self.directions]):
162
_("All required directions have been tested!"), self.status)
163
self.exit_code = EXIT_WITH_SUCCESS
164
self.exit_button.grab_focus()
166
def on_scroll(self, window, event):
167
for direction in self.directions:
168
if direction.value == event.direction:
169
self.found_direction(direction)
176
gettext.textdomain("checkbox")
178
usage = """Usage: %prog DIRECTION... [--edge-scroll]"""
179
parser = OptionParser(usage=usage)
180
parser.add_option("--edge-scroll", action="store_true", default=False,
181
help="Force touchpad to use edge scrolling only")
182
(options, args) = parser.parse_args(args)
185
parser.error("Must specify directions to test.")
190
direction = Direction(arg)
191
except AttributeError:
192
parser.error("Unsupported direction: %s" % arg)
193
directions.append(direction)
195
scroller = GtkScroller(directions, edge_scroll=options.edge_scroll)
198
except KeyboardInterrupt:
199
scroller.show_text(_("Test interrupted"), scroller.status)
202
return scroller.exit_code
204
if __name__ == "__main__":
205
sys.exit(main(sys.argv[1:]))