|
0 |
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
|
|
1 |
# LightDM GTK Greeter Settings
|
|
2 |
# Copyright (C) 2014 Andrew P. <pan.pav.7c5@gmail.com>
|
|
3 |
#
|
|
4 |
# This program is free software: you can redistribute it and/or modify it
|
|
5 |
# under the terms of the GNU General Public License version 3, as published
|
|
6 |
# by the Free Software Foundation.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful, but
|
|
9 |
# WITHOUT ANY WARRANTY; without even the implied warranties of
|
|
10 |
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
|
|
11 |
# PURPOSE. See the GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License along
|
|
14 |
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
15 |
|
|
16 |
|
|
17 |
from gi.repository import Gtk
|
|
18 |
from itertools import product
|
|
19 |
from lightdm_gtk_greeter_settings.OptionEntry import BaseEntry
|
|
20 |
from lightdm_gtk_greeter_settings.helpers import WidgetsWrapper
|
|
21 |
|
|
22 |
|
|
23 |
__all__ = ['PositionEntry']
|
|
24 |
|
|
25 |
|
|
26 |
class PositionEntry(BaseEntry):
|
|
27 |
|
|
28 |
class Dimension:
|
|
29 |
def __init__(self, widgets, on_changed):
|
|
30 |
self._entry = widgets['entry']
|
|
31 |
self._percents = widgets['percents']
|
|
32 |
self._mirror = widgets['mirror']
|
|
33 |
self._adjustment = widgets['adjustment']
|
|
34 |
self._on_changed = on_changed
|
|
35 |
|
|
36 |
self._anchor = None
|
|
37 |
|
|
38 |
self._percents.connect('toggled', self._on_percents_toggled)
|
|
39 |
self._mirror.connect('toggled', self._on_mirror_toggled)
|
|
40 |
self._on_value_changed_id = self._adjustment.connect('value-changed',
|
|
41 |
self._on_value_changed)
|
|
42 |
|
|
43 |
@property
|
|
44 |
def value(self):
|
|
45 |
return '%s%d%s,%s' % ('-' if self._mirror.props.active else '',
|
|
46 |
int(self._entry.props.value),
|
|
47 |
'%' if self._percents.props.active else '',
|
|
48 |
self._anchor)
|
|
49 |
|
|
50 |
@value.setter
|
|
51 |
def value(self, s):
|
|
52 |
value, _, anchor = s.partition(',')
|
|
53 |
|
|
54 |
percents = value and value[-1] == '%'
|
|
55 |
if percents:
|
|
56 |
value = value[:-1]
|
|
57 |
|
|
58 |
try:
|
|
59 |
p = int(value)
|
|
60 |
except ValueError:
|
|
61 |
p = 0
|
|
62 |
|
|
63 |
negative = (p < 0) or (p == 0 and value and value[0] == '-')
|
|
64 |
|
|
65 |
if not anchor or anchor not in ('start', 'center', 'end'):
|
|
66 |
if negative:
|
|
67 |
anchor = 'end'
|
|
68 |
else:
|
|
69 |
anchor = 'start'
|
|
70 |
self._anchor = anchor
|
|
71 |
|
|
72 |
self._percents.props.active = percents
|
|
73 |
self._adjustment.props.upper = 100 if self._percents.props.active else 10000
|
|
74 |
self._mirror.props.active = negative
|
|
75 |
with self._adjustment.handler_block(self._on_value_changed_id):
|
|
76 |
self._adjustment.props.value = -p if negative else p
|
|
77 |
|
|
78 |
@property
|
|
79 |
def anchor(self):
|
|
80 |
return self._anchor
|
|
81 |
|
|
82 |
@anchor.setter
|
|
83 |
def anchor(self, value):
|
|
84 |
self._anchor = value
|
|
85 |
|
|
86 |
def get_value_for_screen(self, screen: int):
|
|
87 |
p = int(self._adjustment.props.value)
|
|
88 |
|
|
89 |
if self._percents.props.active:
|
|
90 |
p = screen * p / 100
|
|
91 |
if self._mirror.props.active:
|
|
92 |
p = screen - p
|
|
93 |
|
|
94 |
return int(p)
|
|
95 |
|
|
96 |
def _on_percents_toggled(self, toggle):
|
|
97 |
self._adjustment.props.upper = 100 if toggle.props.active \
|
|
98 |
else 10000
|
|
99 |
self._on_changed(self)
|
|
100 |
|
|
101 |
def _on_mirror_toggled(self, toggle):
|
|
102 |
self._on_changed(self)
|
|
103 |
|
|
104 |
def _on_value_changed(self, widget):
|
|
105 |
self._on_changed(self)
|
|
106 |
|
|
107 |
|
|
108 |
ASSUMED_WINDOW_SIZE = 430, 240
|
|
109 |
|
|
110 |
def __init__(self, widgets):
|
|
111 |
super().__init__(widgets)
|
|
112 |
|
|
113 |
self._screen_size = None
|
|
114 |
self._last_overlay_size = None
|
|
115 |
self._last_window_allocation = None
|
|
116 |
|
|
117 |
self._screen_frame = widgets['screen_frame']
|
|
118 |
self._screen_overlay = widgets['screen_overlay']
|
|
119 |
self._window_frame = widgets['window_frame']
|
|
120 |
self._grid = widgets['window_grid']
|
|
121 |
|
|
122 |
# Creating points grid
|
|
123 |
anchors_align = (Gtk.Align.START, Gtk.Align.CENTER, Gtk.Align.END)
|
|
124 |
anchors = [(x, y, Gtk.RadioButton())
|
|
125 |
for x, y in product(enumerate(('start', 'center', 'end')), repeat=2)]
|
|
126 |
|
|
127 |
self._anchors = {}
|
|
128 |
for (left, x_anchor), (top, y_anchor), w in anchors:
|
|
129 |
w.props.halign = anchors_align[left]
|
|
130 |
w.props.valign = anchors_align[top]
|
|
131 |
w.props.group = anchors[0][-1]
|
|
132 |
w.connect('toggled', self._on_anchor_toggled, x_anchor, y_anchor)
|
|
133 |
self._grid.attach(w, left, top, 1, 1)
|
|
134 |
self._anchors[x_anchor, y_anchor] = w
|
|
135 |
|
|
136 |
self._grid.show_all()
|
|
137 |
|
|
138 |
self._x = PositionEntry.Dimension(WidgetsWrapper(widgets, 'x'), self._on_dimension_changed)
|
|
139 |
self._y = PositionEntry.Dimension(WidgetsWrapper(widgets, 'y'), self._on_dimension_changed)
|
|
140 |
|
|
141 |
self._on_gdk_screen_changed()
|
|
142 |
|
|
143 |
self._screen_overlay.connect('get-child-position', self._on_screen_overlay_get_child_position)
|
|
144 |
self._screen_overlay.connect('screen-changed', self._on_gdk_screen_changed)
|
|
145 |
|
|
146 |
def _get_value(self):
|
|
147 |
return self._x.value + ' ' + self._y.value
|
|
148 |
|
|
149 |
def _set_value(self, value):
|
|
150 |
if value:
|
|
151 |
x, _, y = value.partition(' ')
|
|
152 |
self._x.value = x
|
|
153 |
self._y.value = y or x
|
|
154 |
self._anchors[self._x.anchor, self._y.anchor].props.active = True
|
|
155 |
|
|
156 |
def _get_corrected_position(self, p, screen, window, anchor):
|
|
157 |
if anchor == 'center':
|
|
158 |
p -= window / 2
|
|
159 |
elif anchor == 'end':
|
|
160 |
p -= window
|
|
161 |
|
|
162 |
if p + window > screen:
|
|
163 |
p = screen - window
|
|
164 |
if p < 0:
|
|
165 |
p = 0
|
|
166 |
|
|
167 |
return int(p)
|
|
168 |
|
|
169 |
def _on_dimension_changed(self, dimension):
|
|
170 |
self._last_window_allocation = None
|
|
171 |
self._screen_overlay.queue_resize()
|
|
172 |
self._emit_changed()
|
|
173 |
|
|
174 |
def _on_screen_overlay_get_child_position(self, overlay, child, allocation):
|
|
175 |
screen = overlay.get_allocation()
|
|
176 |
|
|
177 |
if self._last_window_allocation and self._last_overlay_size == (screen.width, screen.height):
|
|
178 |
allocation.x, allocation.y, allocation.width, allocation.height = self._last_window_allocation
|
|
179 |
return True
|
|
180 |
self._last_overlay_size = screen.width, screen.height
|
|
181 |
|
|
182 |
scale = screen.width / self._screen_size[0]
|
|
183 |
|
|
184 |
width = int(self.ASSUMED_WINDOW_SIZE[0] * scale)
|
|
185 |
height = int(self.ASSUMED_WINDOW_SIZE[1] * scale)
|
|
186 |
|
|
187 |
# Set desired size
|
|
188 |
child.set_size_request(width, height)
|
|
189 |
# And check what actually we have now
|
|
190 |
width, height = child.size_request().width, child.size_request().height
|
|
191 |
|
|
192 |
x = self._get_corrected_position(int(self._x.get_value_for_screen(self._screen_size[0]) * scale),
|
|
193 |
screen.width, width, self._x.anchor)
|
|
194 |
y = self._get_corrected_position(int(self._y.get_value_for_screen(self._screen_size[1]) * scale),
|
|
195 |
screen.height, height, self._y.anchor)
|
|
196 |
|
|
197 |
self._last_window_allocation = x, y, width, height
|
|
198 |
allocation.x, allocation.y, allocation.width, allocation.height = x, y, width, height
|
|
199 |
|
|
200 |
return True
|
|
201 |
|
|
202 |
def _on_anchor_toggled(self, toggle, x, y):
|
|
203 |
if not toggle.props.active:
|
|
204 |
return
|
|
205 |
self._x.anchor = x
|
|
206 |
self._y.anchor = y
|
|
207 |
self._last_window_allocation = None
|
|
208 |
self._screen_overlay.queue_resize()
|
|
209 |
self._emit_changed()
|
|
210 |
|
|
211 |
def _on_gdk_screen_changed(self, widget = None, prev_screen = None):
|
|
212 |
screen = self._screen_overlay.get_toplevel().get_screen()
|
|
213 |
geometry = screen.get_monitor_geometry(screen.get_primary_monitor())
|
|
214 |
self._screen_size = geometry.width, geometry.height
|
|
215 |
self._screen_frame.props.ratio = geometry.width / geometry.height
|
|
216 |
|
|
217 |
|