File: radioButton.js

package info (click to toggle)
cinnamon 3.2.7-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,624 kB
  • sloc: ansic: 33,269; python: 18,048; xml: 1,504; makefile: 780; sh: 90; cpp: 54
file content (200 lines) | stat: -rw-r--r-- 6,268 bytes parent folder | download | duplicates (2)
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
const Clutter = imports.gi.Clutter;
const Pango = imports.gi.Pango;
const Cinnamon = imports.gi.Cinnamon;
const St = imports.gi.St;
const Signals = imports.signals;

const Lang = imports.lang;

function RadioButtonContainer() {
   this._init();
}
RadioButtonContainer.prototype = {
    _init: function() {
        this.actor = new Cinnamon.GenericContainer({ y_align: St.Align.MIDDLE });
        this.actor.connect('get-preferred-width',
                           Lang.bind(this, this._getPreferredWidth));
        this.actor.connect('get-preferred-height',
                           Lang.bind(this, this._getPreferredHeight));
        this.actor.connect('allocate',
                           Lang.bind(this, this._allocate));
        this.actor.connect('style-changed', Lang.bind(this,
            function() {
                let node = this.actor.get_theme_node();
                this._spacing = node.get_length('spacing');
            }));
        this.actor.request_mode = Clutter.RequestMode.HEIGHT_FOR_WIDTH;

        this._box = new St.Bin();
        this.actor.add_actor(this._box);

        this.label = new St.Label();
        this.label.clutter_text.set_line_wrap(false);
        this.label.clutter_text.set_ellipsize(Pango.EllipsizeMode.NONE);
        this.actor.add_actor(this.label, { y_fill: true, y_align: St.Align.END });

        this._spacing = 0;
    },

    _getPreferredWidth: function(actor, forHeight, alloc) {
        let [minWidth, natWidth] = this._box.get_preferred_width(forHeight);

        alloc.min_size = minWidth + this._spacing;
        alloc.natural_size = natWidth + this._spacing;
    },

    _getPreferredHeight: function(actor, forWidth, alloc) {
        let [minBoxHeight, natBoxHeight] =
            this._box.get_preferred_height(-1);
        let [minLabelHeight, natLabelHeight] =
            this.label.get_preferred_height(-1);

        alloc.min_size = Math.max(minBoxHeight, 2 * minLabelHeight);
        alloc.natural_size = Math.max(natBoxHeight, 2 * natLabelHeight);
    },

    _allocate: function(actor, box, flags) {
        let availWidth = box.x2 - box.x1;
        let availHeight = box.y2 - box.y1;

        let childBox = new Clutter.ActorBox();
        let [minBoxWidth, natBoxWidth] =
            this._box.get_preferred_width(-1);
        let [minBoxHeight, natBoxHeight] =
            this._box.get_preferred_height(-1);
        childBox.x1 = box.x1;
        childBox.x2 = box.x1 + natBoxWidth;
        childBox.y1 = box.y1;
        childBox.y2 = box.y1 + natBoxHeight;
        this._box.allocate(childBox, flags);

        childBox.x1 = box.x1 + natBoxWidth + this._spacing;
        childBox.x2 = availWidth - childBox.x1;
        childBox.y1 = box.y1;
        childBox.y2 = box.y2;
        this.label.allocate(childBox, flags);
    }
};

function RadioBox(state) {
    this._init(state);
}

RadioBox.prototype = {
    _init: function(state) {
        this.actor = new St.Button({ style_class: 'radiobutton',
                                     button_mask: St.ButtonMask.ONE,
                                     toggle_mode: true,
                                     can_focus: true,
                                     x_fill: true,
                                     y_fill: true,
                                     y_align: St.Align.MIDDLE });

        this.actor._delegate = this;
        this.actor.checked = state;
        // FIXME: The current size is big and the container only is useful,
        // because the current theme. Can be fixed the theme also?
        this.actor.style = 'width: 12px;';
        this._container = new St.Bin();
        this.actor.set_child(this._container);
    },

    setToggleState: function(state) {
        this.actor.checked = state;
    },

    toggle: function() {
        this.setToggleState(!this.actor.checked);
    },

    destroy: function() {
        this.actor.destroy();
    }
};

function RadioButton(label) {
   this._init(label);
}

RadioButton.prototype = {
    __proto__: RadioBox.prototype,

    _init: function(label) {
        RadioBox.prototype._init.call(this, false);
        this._container.destroy();
        this._container = new RadioButtonContainer();
        this.actor.set_child(this._container.actor);

        if (label)
            this.setLabel(label);
    },

    setLabel: function(label) {
        this._container.label.set_text(label);
    },

    getLabelActor: function() {
        return this._container.label;
    }
};

function RadioButtonGroup() {
   this._init();
}

RadioButtonGroup.prototype = {
   _init: function() {
      this.actor = new St.BoxLayout({ vertical: true, width: 250 });
      this._buttons = [];
      this._activeId = null;
   },

   addButton: function(buttonId, label) {
      this.radioButton = new RadioButton(label);
      this.radioButton.actor.connect("clicked", 
         Lang.bind(this, function(actor) {
            this.buttonClicked(actor, buttonId); 
         }));

      this._buttons.push({ id: buttonId, button: this.radioButton });
      this.actor.add(this.radioButton.actor, { x_fill: true, y_fill: false, y_align: St.Align.MIDDLE });
   },

   radioChanged: function(actor) {

   },

   buttonClicked: function(actor, buttonId) {
      for (var i in this._buttons) {
         if (buttonId != this._buttons[i]['id'] && this._buttons[i]['button'].actor.checked) {
            this._buttons[i]['button'].actor.checked = false;
         }
         else if (buttonId == this._buttons[i]['id'] && !this._buttons[i]['button'].actor.checked) {
            this._buttons[i]['button'].actor.checked = true;
         }
      }
      
      // Only trigger real changes to radio selection.
      if (buttonId != this._activeId) {
         this._activeId = buttonId;
         this.emit('radio-changed', this._activeId);
      }
   },

   setActive: function(buttonId) {
      for (var i in this._buttons) {
         this._buttons[i]['button'].actor.checked = 
           (buttonId == this._buttons[i]['id']);
      }

      if (this._activeId != buttonId) {
         this._activeId = buttonId;
         this.emit('radio-changed', this._activeId);
      }
   },

   getActive: function() {
      return this._activeId;
   }
}
Signals.addSignalMethods(RadioButtonGroup.prototype);