// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- /* * Copyright 2011 Red Hat, Inc * * 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 2, or (at your option) * any later version. * * 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. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ const Gtk = imports.gi.Gtk; const Lang = imports.lang; const Signals = imports.signals; const St = imports.gi.St; const Gdm = imports.gi.Gdm; const SessionListItem = new Lang.Class({ Name: 'SessionListItem', _init: function(id, name) { this.id = id; this.actor = new St.Button({ style_class: 'login-dialog-session-list-item', button_mask: St.ButtonMask.ONE | St.ButtonMask.THREE, can_focus: true, reactive: true, x_fill: true, x_align: St.Align.START }); this._box = new St.BoxLayout({ style_class: 'login-dialog-session-list-item-box' }); this.actor.add_actor(this._box); this.actor.connect('clicked', Lang.bind(this, this._onClicked)); this._dot = new St.DrawingArea({ style_class: 'login-dialog-session-list-item-dot' }); this._dot.connect('repaint', Lang.bind(this, this._onRepaintDot)); this._box.add_actor(this._dot); this.setShowDot(false); let label = new St.Label({ style_class: 'login-dialog-session-list-item-label', text: name }); this.actor.label_actor = label; this._box.add_actor(label); }, setShowDot: function(show) { if (show) this._dot.opacity = 255; else this._dot.opacity = 0; }, _onRepaintDot: function(area) { let cr = area.get_context(); let [width, height] = area.get_surface_size(); let color = area.get_theme_node().get_foreground_color(); cr.setSourceRGBA (color.red / 255, color.green / 255, color.blue / 255, color.alpha / 255); cr.arc(width / 2, height / 2, width / 3, 0, 2 * Math.PI); cr.fill(); cr.$dispose(); }, _onClicked: function() { this.emit('activate'); } }); Signals.addSignalMethods(SessionListItem.prototype); const SessionList = new Lang.Class({ Name: 'SessionList', _init: function() { this.actor = new St.Bin(); this._box = new St.BoxLayout({ style_class: 'login-dialog-session-list', vertical: true}); this.actor.child = this._box; this._button = new St.Button({ style_class: 'login-dialog-session-list-button', button_mask: St.ButtonMask.ONE | St.ButtonMask.THREE, can_focus: true, x_fill: true, y_fill: true }); let box = new St.BoxLayout(); this._button.add_actor(box); this._triangle = new St.Label({ style_class: 'login-dialog-session-list-triangle', text: '\u25B8' }); box.add_actor(this._triangle); let label = new St.Label({ style_class: 'login-dialog-session-list-label', text: _("Session…") }); box.add_actor(label); this._button.connect('clicked', Lang.bind(this, this._onClicked)); this._box.add_actor(this._button); this._scrollView = new St.ScrollView({ style_class: 'login-dialog-session-list-scroll-view'}); this._scrollView.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC); this._box.add_actor(this._scrollView); this._itemList = new St.BoxLayout({ style_class: 'login-dialog-session-item-list', vertical: true }); this._scrollView.add_actor(this._itemList); this._scrollView.hide(); this.isOpen = false; this._populate(); }, open: function() { if (this.isOpen) return; this._button.add_style_pseudo_class('open'); this._scrollView.show(); this._triangle.set_text('\u25BE'); this.isOpen = true; }, close: function() { if (!this.isOpen) return; this._button.remove_style_pseudo_class('open'); this._scrollView.hide(); this._triangle.set_text('\u25B8'); this.isOpen = false; }, _onClicked: function() { if (!this.isOpen) this.open(); else this.close(); }, updateSensitivity: function(sensitive) { this._button.reactive = sensitive; this._button.can_focus = sensitive; for (let id in this._items) this._items[id].actor.reactive = sensitive; }, setActiveSession: function(sessionId) { if (sessionId == this._activeSessionId) return; if (this._activeSessionId) this._items[this._activeSessionId].setShowDot(false); this._items[sessionId].setShowDot(true); this._activeSessionId = sessionId; this.emit('session-activated', this._activeSessionId); }, _populate: function() { this._itemList.destroy_all_children(); this._activeSessionId = null; this._items = {}; let ids = Gdm.get_session_ids(); ids.sort(); if (ids.length <= 1) { this._box.hide(); this._button.hide(); } else { this._button.show(); this._box.show(); } for (let i = 0; i < ids.length; i++) { let [sessionName, sessionDescription] = Gdm.get_session_name_and_description(ids[i]); let item = new SessionListItem(ids[i], sessionName); this._itemList.add_actor(item.actor); this._items[ids[i]] = item; if (!this._activeSessionId) this.setActiveSession(ids[i]); item.connect('activate', Lang.bind(this, function() { this.setActiveSession(item.id); })); } } }); Signals.addSignalMethods(SessionList.prototype);