class Gtk2_expander_settings

A class that helps saving and restoring settings for expanders using a database.

Constants

ALLOWED_ARGS
DB_SCHEMA

Public Class Methods

new(args) click to toggle source
# File lib/gtk2_expander_settings.rb, line 18
def initialize(args)
  #Load arguments and database.
  args.each do |key, val|
    raise "Invalid argument: '#{key}'." if !ALLOWED_ARGS.include?(key)
  end
  
  @db, @expander, @name = args[:db], args[:expander], args[:name]
  
  Knj::Db::Revision.new.init_db("db" => @db, "schema" => DB_SCHEMA)
  
  #Load or initialize saved data in database.
  if @data = @db.single(:Gtk2_expander_settings, :name => @name)
    @id = @data[:id]
  else
    @id = @db.insert(:Gtk2_expander_settings, {:name => @name, :saved => 0}, :return_id => true)
    @data = @db.single(:Gtk2_expander_settings, :id => @id)
  end
  
  #Restore saved value. Use timeouts to give window time to initialize first (to load code that may effect the expander like in OpenAll-Time-Applet).
  if @data[:saved].to_i == 1
    if @data[:expanded].to_i == 1 and !@expander.expanded?
      Gtk.timeout_add(25) do
        @expander.activate
        false
      end
    elsif @data[:expanded].to_i == 0 and @expander.expanded?
      Gtk.timeout_add(25) do
        @expander.activate
        false
      end
    end
  end
  
  #Connect signals in order to save new values.
  @expander.signal_connect_after("activate", &self.method(:on_expander_activated))
end

Public Instance Methods

on_expander_activated(*args) click to toggle source

Called after the expander is activated.

# File lib/gtk2_expander_settings.rb, line 56
def on_expander_activated(*args)
  if @expander.expanded?
    exp_val = 1
  else
    exp_val = 0
  end
  
  @db.update(:Gtk2_expander_settings, {:expanded => exp_val, :saved => 1}, {:id => @id})
end