Class: TkWrapper::Widgets::Base::Configuration
- Inherits:
-
Object
- Object
- TkWrapper::Widgets::Base::Configuration
- Defined in:
- lib/widgets/base/configuration.rb
Constant Summary collapse
- GRID_SPECIAL_VALUES =
{ onecell: { weights: { rows: [1], cols: [1] } }, fullcell: { column: 0, row: 0, sticky: 'nsew' }, default: { weights: { rows: [1], cols: [1] }, column: 0, row: 0, sticky: 'nsew' } }.freeze
- NON_TK_OPTIONS =
i[id tk_class tearoff weights ].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #configure_tearoff ⇒ Object
- #configure_tk_widget(tk_widget) ⇒ Object
- #configure_weights(tk_widget) ⇒ Object
- #grid(only_tk_options: false) ⇒ Object
-
#initialize(config) ⇒ Configuration
constructor
A new instance of Configuration.
- #merge!(*configurations) ⇒ Object
- #merge_global_configurations(manager, widget) ⇒ Object
- #parse!(config) ⇒ Object
- #parse_grid_value(value) ⇒ Object
- #parse_value(key, value) ⇒ Object
Constructor Details
#initialize(config) ⇒ Configuration
25 26 27 |
# File 'lib/widgets/base/configuration.rb', line 25 def initialize(config) @config = parse!(config) end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
8 9 10 |
# File 'lib/widgets/base/configuration.rb', line 8 def config @config end |
Instance Method Details
#[](key) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/widgets/base/configuration.rb', line 49 def [](key) key = key.to_sym return self.class.new(@config[key]) if @config[key].is_a?(Hash) @config[key] end |
#[]=(key, value) ⇒ Object
56 57 58 59 |
# File 'lib/widgets/base/configuration.rb', line 56 def []=(key, value) key = key.to_sym @config[key] = parse_value(key, value) end |
#configure_tearoff ⇒ Object
110 111 112 113 114 |
# File 'lib/widgets/base/configuration.rb', line 110 def configure_tearoff return if (tearoff = @config[:tearoff]).nil? TkOption.add '*tearOff', (tearoff ? 1 : 0) end |
#configure_tk_widget(tk_widget) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/widgets/base/configuration.rb', line 82 def () @config.each do |option, value| next if NON_TK_OPTIONS.include?(option) if option == :grid grid = grid(only_tk_options: true) next if grid.empty? next .grid(grid) if option == :grid end [option] = value end configure_weights() end |
#configure_weights(tk_widget) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/widgets/base/configuration.rb', line 98 def configure_weights() return unless (weights = @config.dig(:grid, :weights)) (weights[:rows] || []).each_with_index do |weight, index| TkGrid.rowconfigure(, index, weight: weight) end (weights[:cols] || []).each_with_index do |weight, index| TkGrid.columnconfigure(, index, weight: weight) end end |
#grid(only_tk_options: false) ⇒ Object
76 77 78 79 80 |
# File 'lib/widgets/base/configuration.rb', line 76 def grid(only_tk_options: false) return @config[:grid] unless @config[:grid].reject { |option, _| NON_TK_OPTIONS.include?(option) } end |
#merge!(*configurations) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/widgets/base/configuration.rb', line 29 def merge!(*configurations) configurations = configurations.map do |configuration| next configuration.config if configuration.is_a?(self.class) parse!(configuration) end Util.merge_recursive!(@config, *configurations) end |
#merge_global_configurations(manager, widget) ⇒ Object
116 117 118 |
# File 'lib/widgets/base/configuration.rb', line 116 def merge_global_configurations(manager, ) merge!(*manager.configurations()) end |
#parse!(config) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/widgets/base/configuration.rb', line 39 def parse!(config) Util.each_recursive(config) do |hash, key, value| next if value.is_a?(Hash) hash[key] = parse_value(key, value) end config end |
#parse_grid_value(value) ⇒ Object
70 71 72 73 74 |
# File 'lib/widgets/base/configuration.rb', line 70 def parse_grid_value(value) return GRID_SPECIAL_VALUES[value] if value.is_a?(Symbol) value end |
#parse_value(key, value) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/widgets/base/configuration.rb', line 61 def parse_value(key, value) case key when :grid parse_grid_value(value) else value end end |