Class: Dumon::GtkTrayUi

Inherits:
GtkUi show all
Defined in:
lib/dumon/ui.rb

Overview

This class represents a user interface represented by system tray icon and its context menu.

Instance Attribute Summary

Attributes inherited from Ui

#omanager

Instance Method Summary collapse

Methods inherited from GtkUi

#about, #quit, #render

Methods inherited from Ui

#about, #new_omanager, #quit, #render

Constructor Details

#initializeGtkTrayUi

:nodoc:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dumon/ui.rb', line 95

def initialize #:nodoc:
  super

  # storage of preferred resolution for next rendering (will be cleared by output changing)
  # {"LVDS1" => "1600x900", "VGA1" => "800x600"}
  @selected_resolution = {}

  # initial primary output
  @primary_output = :none

  @tray = Gtk::StatusIcon.new
  @tray.visible = true
  @tray.pixbuf = Gdk::Pixbuf.new(::File.join(::File.dirname(__FILE__), '..', 'monitor24.png'))
  @tray.tooltip = "Dual Monitor Manager"

  @tray.signal_connect('popup-menu') do |w, button, activate_time|
    menu = self.create_menu
    menu.show_all
    menu.popup(nil, nil, button, activate_time)
  end
end

Instance Method Details

#create_menuObject

Reads info about currently usable outputs and construct corresponding structure of context menu.



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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/dumon/ui.rb', line 119

def create_menu
  outputs = self.omanager.read

  rslt = Gtk::Menu.new

  # resolutions (submenu)
  outputs.keys.each do |o|
    item = Gtk::MenuItem.new(o)
    submenu = Gtk::Menu.new
    item.set_submenu(submenu)

    # to be marked with '*'
    defres = self.omanager.default_resolution(o)

    # radio buttons group
    radios = []

    outputs[o][:resolutions].each do |res|
      si = Gtk::RadioMenuItem.new(radios, defres === res ? "#{res} [*]" : res)
      si.active = (@selected_resolution[o] === res or (@selected_resolution[o].nil? and outputs[o][:current] === res))
      radios << si
      si.signal_connect('activate') do
        # only store your preferred resolution for next rendering
        @selected_resolution[o] = res if si.active? # only activation, ignore deactivation
      end
      submenu.append(si)
    end
    rslt.append(item)
  end

  # separator
  item = Gtk::SeparatorMenuItem.new
  rslt.append(item)

  # single outputs
  outputs.keys.each do |o|
    item = Gtk::MenuItem.new("only #{o}")
    item.signal_connect('activate') do
      self.omanager.switch({:mode=>:single, :out=>o, :resolution=>@selected_resolution[o]})
      # clear preferred resolution, by next rendering will be read from real state
      @selected_resolution.clear
    end
    rslt.append(item)
  end

  # mirror
  item = Gtk::MenuItem.new('mirror')
  if outputs.keys.size >= 2
    submenu = Gtk::Menu.new
    item.set_submenu(submenu)
  else
    item.sensitive = false
  end

  self.omanager.common_resolutions.each do |res|
    si = Gtk::MenuItem.new(res)
    si.signal_connect('activate') { self.omanager.switch({:mode=>:mirror, :resolution=>res}) }
    submenu.append(si)
  end
  rslt.append(item)

  # separator
  item = Gtk::SeparatorMenuItem.new
  rslt.append(item)

  # primary output
  item = Gtk::MenuItem.new('Primary output')
  submenu = Gtk::Menu.new
  item.set_submenu(submenu)
  item.sensitive = (outputs.keys.size >= 2)

  radios = []
  prims = outputs.keys.clone << :none
  prims.each do |o|
    si = Gtk::RadioMenuItem.new(radios, o.to_s)
    si.active = (@primary_output.to_s == o.to_s)
    radios << si
    si.signal_connect('activate') { @primary_output = o.to_s if si.active? }
    submenu.append(si)
  end
  rslt.append(item)

  # sequence
  if outputs.keys.size >= 2
    o0 = outputs.keys[0]
    o1 = outputs.keys[1]
    item = Gtk::MenuItem.new("#{o0} left of #{o1}")
    item.signal_connect('activate') do
      omanager.switch({:mode=>:hsequence, :outs=>[o0, o1], :resolutions=>[@selected_resolution[o0], @selected_resolution[o1]], :primary=>@primary_output})
      # clear preferred resolution, by next rendering will be read from real state
      @selected_resolution.clear
    end
    rslt.append(item)
    item = Gtk::MenuItem.new("#{o1} left of #{o0}")
    item.signal_connect('activate') do
      omanager.switch({:mode=>:hsequence, :outs=>[o1, o0], :resolutions=>[@selected_resolution[o1], @selected_resolution[o0]], :primary=>@primary_output})
      @selected_resolution.clear
    end
    rslt.append(item)
    item = Gtk::MenuItem.new("#{o0} above #{o1}")
    item.signal_connect('activate') do
      omanager.switch({:mode=>:vsequence, :outs=>[o0, o1], :resolutions=>[@selected_resolution[o0], @selected_resolution[o1]], :primary=>@primary_output})
      @selected_resolution.clear
    end
    rslt.append(item)
    item = Gtk::MenuItem.new("#{o1} above #{o0}")
    item.signal_connect('activate') do
      omanager.switch({:mode=>:vsequence, :outs=>[o1, o0], :resolutions=>[@selected_resolution[o1], @selected_resolution[o0]], :primary=>@primary_output})
      @selected_resolution.clear
    end
    rslt.append(item)
  end

  # separator
  rslt.append(Gtk::SeparatorMenuItem.new)

  # Profiles
  item = Gtk::MenuItem.new('Profiles...')
  item.signal_connect('activate') { self.profile_management_dialog }
  rslt.append(item)

  # About
  item = Gtk::ImageMenuItem.new(Gtk::Stock::ABOUT)
  item.signal_connect('activate') { self.about }
  rslt.append(item)
  # Quit
  item = Gtk::ImageMenuItem.new(Gtk::Stock::QUIT)
  item.signal_connect('activate') { Dumon::App.instance.quit }
  rslt.append(item)

  rslt
end

#profile_management_dialogObject

Function to open a dialog box for profile management.



254
255
256
257
# File 'lib/dumon/ui.rb', line 254

def profile_management_dialog
  dialog = Dumon::GtkProfileDlg.new
  dialog.show
end