Package list lightdm-gtk-greeter-settings / 6ea1eaa
Convert "monitors" options to strings Andrew P. 7 years ago
4 changed file(s) with 114 addition(s) and 111 deletion(s). Raw diff Collapse all Expand all
102102 <property name="show_border">False</property>
103103 <property name="scrollable">True</property>
104104 <property name="group_name">monitors</property>
105 <signal name="switch-page" handler="on_monitors_notebook_switch_page" swapped="no"/>
105 <signal name="switch-page" handler="on_switch_page" swapped="no"/>
106106 <child>
107107 <object class="GtkBox" id="empty_page">
108108 <property name="visible">True</property>
1919
2020 from gi.repository import Gtk
2121
22 from lightdm_gtk_greeter_settings.helpers import WidgetsWrapper
22 from lightdm_gtk_greeter_settings.helpers import (
23 bool2string,
24 WidgetsWrapper)
2325 from lightdm_gtk_greeter_settings.MultiheadSetupDialog import MultiheadSetupDialog
2426 from lightdm_gtk_greeter_settings.OptionEntry import BaseEntry
2527 from lightdm_gtk_greeter_settings.OptionGroup import BaseGroup
4749 name = name[len(self.GROUP_PREFIX):].strip()
4850 entry = MonitorEntry(self._widgets)
4951 entry['background'] = section.get('background', None)
50 entry['user-background'] = section.getboolean('user-background', None)
51 entry['laptop'] = section.getboolean('laptop', None)
52 entry['user-background'] = bool2string(section.getboolean('user-background', None), 1)
53 entry['laptop'] = bool2string(section.getboolean('laptop', None), True)
5254 self._entries[name] = entry
5355 self.entry_added.emit(entry, name)
5456
3636 C_ = lambda t: lightdm_gtk_greeter_settings.helpers.C_('option|multihead', t)
3737
3838
39 class PageOptions:
39 class MonitorConfig:
4040 name = None
4141 background = None
4242 background_disabled = None
7777 def init_window(self):
7878 self._widgets = self.Widgets(builder=self.builder)
7979
80 # To set size of widgets.notebook to size of widgets.editor
81 self.realize()
82
8083 self._widgets.notebook.remove_page(self._widgets.notebook.page_num(self._widgets.editor))
8184
8285 self._current_page = None
8386 self._defaults = {}
84 self._options = {}
87 self._configs = {}
8588
8689 self._option_name = OptionEntry.StringEntry(WidgetsWrapper(self.builder, 'name'))
8790 self._option_bg = OptionEntry.BackgroundEntry(WidgetsWrapper(self.builder, 'background'))
108111 item.connect('activate', self.on_add_button_clicked, name)
109112
110113 def set_model(self, values):
111 self._widgets.notebook.handler_block_by_func(self.on_monitors_notebook_switch_page)
114 self._widgets.notebook.handler_block_by_func(self.on_switch_page)
112115 for page in self._widgets.notebook.get_children():
113 self._remove_monitor(page, update=False)
114 self._widgets.notebook.handler_unblock_by_func(self.on_monitors_notebook_switch_page)
115
116 self._options.clear()
116 self._remove_page(page, update=False)
117 self._widgets.notebook.handler_unblock_by_func(self.on_switch_page)
118
119 self._configs.clear()
117120 for name, entry in values.items():
118 options = PageOptions()
119 options.name = name
120 options.background = entry['background']
121 options.user_background = entry['user-background']
122 options.laptop = entry['laptop']
123
124 options.background_disabled = self._get_first_not_none(
125 options.background, self._defaults.get('background'), '')
126 options.user_background_disabled = self._get_first_not_none(
127 options.user_background, self._defaults.get('user-background'), 'true')
128 options.laptop_disabled = self._get_first_not_none(
129 options.laptop, self._defaults.get('laptop'), 'false')
130
131 self._add_monitor(options)
132
133 self._widgets.empty.props.visible = not self._options
121 config = MonitorConfig()
122 config.name = name
123 config.background = entry['background']
124 config.user_background = entry['user-background']
125 config.laptop = entry['laptop']
126
127 config.background_disabled = self._get_first_not_none(
128 config.background, self._defaults.get('background'), '')
129 config.user_background_disabled = self._get_first_not_none(
130 config.user_background, self._defaults.get('user-background'), 'true')
131 config.laptop_disabled = self._get_first_not_none(
132 config.laptop, self._defaults.get('laptop'), 'false')
133
134 self._add_page(config)
135
136 self._widgets.empty.props.visible = not self._configs
134137
135138 self._update_monitors_list()
136139
137140 def get_model(self):
138141 sections = []
139142 for page in self._widgets.notebook.get_children():
140 options = self._options.get(page)
141 if not options or not options.name:
143 config = self._configs.get(page)
144 if not config or not config.name:
142145 continue
143 sections.append((options.name,
144 {'background': options.background,
145 'user-background': options.user_background,
146 'laptop': options.laptop}))
146 sections.append((config.name,
147 {'background': config.background,
148 'user-background': config.user_background,
149 'laptop': config.laptop}))
147150 return sections
148151
149152 def set_defaults(self, values):
152155 def _get_first_not_none(self, *values, fallback=None):
153156 return next((v for v in values if v is not None), fallback)
154157
155 def _add_monitor(self, options):
158 def _add_page(self, config):
156159 holder = Gtk.Box()
157160 holder.show()
158161
159 options.label = Gtk.Label(options.name)
160 options.error_image = Gtk.Image.new_from_icon_name('dialog-warning', Gtk.IconSize.INVALID)
161 options.error_image.set_pixel_size(16)
162 options.error_image.set_no_show_all(True)
162 config.label = Gtk.Label(config.name)
163 config.error_image = Gtk.Image.new_from_icon_name('dialog-warning', Gtk.IconSize.INVALID)
164 config.error_image.set_pixel_size(16)
165 config.error_image.set_no_show_all(True)
163166
164167 close_button = Gtk.Button()
165168 close_button.set_focus_on_click(False)
166169 close_button.set_relief(Gtk.ReliefStyle.NONE)
167 close_button.connect('clicked', lambda w, p: self._remove_monitor(p), holder)
170 close_button.connect('clicked', lambda w, p: self._remove_page(p), holder)
168171 close_image = Gtk.Image.new_from_icon_name('stock_close', Gtk.IconSize.INVALID)
169172 close_image.set_pixel_size(16)
170173 close_button.add(close_image)
171174
172175 label_box = Gtk.Box(Gtk.Orientation.HORIZONTAL)
173 label_box.pack_start(options.error_image, False, False, 1)
174 label_box.pack_start(options.label, False, False, 3)
176 label_box.pack_start(config.error_image, False, False, 1)
177 label_box.pack_start(config.label, False, False, 3)
175178 label_box.pack_start(close_button, False, False, 0)
176179
177180 label_eventbox = Gtk.EventBox()
178181 label_eventbox.add(label_box)
179182 label_eventbox.show_all()
180183
181 self._options[holder] = options
184 self._configs[holder] = config
182185
183186 if self._widgets.empty.get_parent():
184187 self._widgets.empty.hide()
186189 current_idx = self._widgets.notebook.get_current_page()
187190 current_idx = self._widgets.notebook.insert_page(holder, label_eventbox, current_idx + 1)
188191
189 # self._widgets.notebook.set_tab_reorderable(holder, True)
190
191192 return current_idx
192193
193 def _remove_monitor(self, page, update=True):
194 if page not in self._options:
194 def _remove_page(self, page, update=True):
195 if not self._configs.pop(page, None):
195196 return
196
197 self._options.pop(page)
198197
199198 if page == self._widgets.editor.props.parent:
200199 page.remove(self._widgets.editor)
201200
202201 if update:
203202 self._update_monitors_list()
204 if not self._options:
203 if not self._configs:
205204 self._widgets.empty.show()
206205
207206 self._widgets.notebook.remove_page(self._widgets.notebook.page_num(page))
208207
209 def _apply_monitor_options(self, options):
210 for entry, value, fallback in \
211 ((self._option_bg, options.background, options.background_disabled),
212 (self._option_user_bg, options.user_background, options.user_background_disabled),
213 (self._option_laptop, options.laptop, options.laptop_disabled)):
214 entry.handler_block_by_func(self._on_option_changed)
215 entry.value = fallback if value is None else value
216 entry.enabled = value is not None
217 entry.handler_unblock_by_func(self._on_option_changed)
218
219 self._option_name.value = options.name or ''
220 self._on_option_changed()
221 self._on_name_changed()
208 def _update_monitors_list(self):
209 configs = set(config.name for config in self._configs.values())
210 used_count = 0
211 self._widgets.name_combo.get_model().clear()
212 for name, item in self._available_monitors:
213 used = name in configs
214 if used:
215 used_count += 1
216 item.props.visible = not used
217 if not used:
218 self._widgets.name_combo.append_text(name)
219
220 show_button = used_count < len(self._available_monitors)
221 self._widgets.name_combo.props.button_sensitivity = (Gtk.SensitivityType.ON if show_button
222 else Gtk.SensitivityType.OFF)
223 self._widgets.editor_add_menu_button.props.visible = show_button
224 self._widgets.empty_add_menu_button.props.visible = show_button
222225
223226 def _on_option_changed(self, entry=None):
224 options = self._options.get(self._current_page)
225 options.background_disabled = self._option_bg.value
226 options.background = options.background_disabled if self._option_bg.enabled else None
227 options.user_background_disabled = self._option_user_bg.value
228 options.user_background = (options.user_background_disabled
229 if self._option_user_bg.enabled else None)
230 options.laptop_disabled = self._option_laptop.value
231 options.laptop = options.laptop_disabled if self._option_laptop.enabled else None
232
233 if not options.background or Gdk.RGBA().parse(options.background):
227 config = self._configs.get(self._current_page)
228 config.background_disabled = self._option_bg.value
229 config.background = config.background_disabled if self._option_bg.enabled else None
230 config.user_background_disabled = self._option_user_bg.value
231 config.user_background = (config.user_background_disabled
232 if self._option_user_bg.enabled else None)
233 config.laptop_disabled = self._option_laptop.value
234 config.laptop = config.laptop_disabled if self._option_laptop.enabled else None
235
236 if not config.background or Gdk.RGBA().parse(config.background):
234237 self._option_bg.error = None
235238 else:
236 self._option_bg.error = check_path_accessibility(options.background)
239 self._option_bg.error = check_path_accessibility(config.background)
237240
238241 def _on_name_changed(self, entry=None):
239 options = self._options[self._current_page]
240 options.name = self._option_name.value.strip()
242 config = self._configs[self._current_page]
243 config.name = self._option_name.value.strip()
241244 self._update_monitors_list()
242245
243246 markup = None
244247 error = None
245248
246 if not options.name:
249 if not config.name:
247250 markup = C_('<i>No name</i>')
248251 error = C_('The name can\'t be empty. Configuration will not be saved.')
249 elif any(options.name == o.name and p != self._current_page
250 for p, o in self._options.items()):
252 elif any(config.name == o.name and p != self._current_page
253 for p, o in self._configs.items()):
251254 error = (C_('"{name}" is already defined. Only last configuration will be saved.')
252 .format(name=options.name))
255 .format(name=config.name))
253256
254257 if markup:
255 options.label.set_markup(markup)
258 config.label.set_markup(markup)
256259 else:
257 options.label.set_label(options.name)
260 config.label.set_label(config.name)
258261
259262 self._option_name.error = error
260263
263266 self._widgets.name.set_position(0)
264267
265268 def on_add_button_clicked(self, widget, name=""):
266 options = PageOptions()
267 options.name = name
268
269 options.background_disabled = self._get_first_not_none(
269 config = MonitorConfig()
270 config.name = name
271
272 config.background_disabled = self._get_first_not_none(
270273 self._defaults.get('background'), '')
271 options.user_background_disabled = self._get_first_not_none(
274 config.user_background_disabled = self._get_first_not_none(
272275 self._defaults.get('user-background'), 'true')
273 options.laptop = self._get_first_not_none(self._defaults.get('laptop'), 'false')
274
275 self._widgets.notebook.set_current_page(self._add_monitor(options))
276 config.laptop_disabled = self._get_first_not_none(self._defaults.get('laptop'), 'false')
277
278 self._widgets.notebook.set_current_page(self._add_page(config))
276279 if name:
277280 self._update_monitors_list()
278281
279 def on_monitors_notebook_switch_page(self, notebook, page, page_idx):
282 def aaa(self, *args):
283 print(self._widgets.notebook.get_current_page(), args)
284
285 def on_switch_page(self, notebook, page, page_idx):
280286 if page == self._widgets.empty:
281287 self._current_page = None
282288 buttons = self._widgets.editor_add_menu_button, self._widgets.empty_add_menu_button
288294 if old_parent:
289295 old_parent.remove(self._widgets.editor)
290296 page.add(self._widgets.editor)
291 self._apply_monitor_options(self._options[page])
297
298 config = self._configs[page]
299 for entry, value, fallback in \
300 ((self._option_bg, config.background, config.background_disabled),
301 (self._option_user_bg, config.user_background, config.user_background_disabled),
302 (self._option_laptop, config.laptop, config.laptop_disabled)):
303 entry.handler_block_by_func(self._on_option_changed)
304 entry.value = fallback if value is None else value
305 entry.enabled = value is not None
306 entry.handler_unblock_by_func(self._on_option_changed)
307
308 self._option_name.value = config.name or ''
309 self._on_option_changed()
310 self._on_name_changed()
292311
293312 GLib.idle_add(self._focus_name_entry)
294313
295314 buttons[0].props.popup = None
296315 buttons[1].props.popup = self._widgets.available_menu
297
298 def _update_monitors_list(self):
299 used_monitors = set(options.name for options in self._options.values())
300 used_count = 0
301 self._widgets.name_combo.get_model().clear()
302 for name, item in self._available_monitors:
303 used = name in used_monitors
304 if used:
305 used_count += 1
306 item.props.visible = not used
307 if not used:
308 self._widgets.name_combo.append_text(name)
309
310 show_button = used_count < len(self._available_monitors)
311 self._widgets.name_combo.props.button_sensitivity = (Gtk.SensitivityType.ON if show_button
312 else Gtk.SensitivityType.OFF)
313 self._widgets.editor_add_menu_button.props.visible = show_button
314 self._widgets.empty_add_menu_button.props.visible = show_button
116116 return get_greeter_version._version
117117
118118
119 def bool2string(value):
120 return 'true' if value else 'false'
119 def bool2string(value, skip_none=False):
120 return 'true' if value else 'false' if not skip_none or value is not None else None
121121
122122
123123 def string2bool(value, fallback=False):