tkwrapper 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6cf6fe96ebab8380076797dfe3f4ed9747d3f3709cc8d58e1bf1c012d28af572
4
- data.tar.gz: b0956e96f04f7daf0b6d6f09317e7b2e73d010e866b57f678fa2fd7ba658fd16
3
+ metadata.gz: a31665aae98cd470f88451d45cd6332030c8fe874e18faa1040bcd42103d9ace
4
+ data.tar.gz: 93f428a63f6bf0832bf91accfdec1f14715cbfc6de2e9055f29b4cb146da0bf7
5
5
  SHA512:
6
- metadata.gz: 848528e93f0dabed494f7e234789edc0108b4460b11964bc661c9457aeabcf2a022565461d4ae3d566de853ad2e96ec06b4112c2abf9f1520912c999675c9bd1
7
- data.tar.gz: 613ec39818df4edb5714f0eac7f3aac73c7b403ed0a5d2371103a2e5419ec0c8bc763a2ec068b12538e2476cd29f1e0f49622af3cbcdfa7c7a734edc783c5c62
6
+ metadata.gz: 461b4344678f1d7a374ad6d1d01ffbab5c4865a9c513457c7145b21e9582e9d0922d3666a93adf6ed8f57428c6485b8e693694438db1966a7967c01c0ae6e3fc
7
+ data.tar.gz: f0a8d152e7aac88206454340c2fbce7e605117a85e2dc0cd550bffc4db9e49d09dd21b4610e52e19a87dc6313ce37b98c9a4bc4e8c44bf033b3f84fe41289833
@@ -19,12 +19,9 @@ module Util
19
19
  end
20
20
  end
21
21
 
22
- def self.recursive_transform_key_value!(hash, &block)
23
- hash.transform_keys! do |key|
24
- value = hash[key]
25
- next recursive_transform_key_value!(value, &block) if value.is_a?(Hash)
26
-
27
- block.call(key, value)
22
+ def self.clone_recursive(hash)
23
+ hash.transform_values do |value|
24
+ value.is_a?(Hash) ? clone_recursive(value) : value
28
25
  end
29
26
  end
30
27
  end
@@ -5,8 +5,8 @@ class TkWrapper::Widgets::AutoResizeEntry < TkWrapper::Widgets::Entry
5
5
  attr_accessor :min_width, :add_width
6
6
 
7
7
  def initialize(config: {}, childs: [])
8
- @min_width = config.delete(:min_width) || 0
9
- @add_width = config.delete(:add_width) || 0
8
+ @min_width = config[:min_width] || 0
9
+ @add_width = config[:add_width] || 0
10
10
  super(config: config, childs: childs)
11
11
  end
12
12
 
@@ -18,6 +18,14 @@ class TkWrapper::Widgets::AutoResizeEntry < TkWrapper::Widgets::Entry
18
18
  resize
19
19
  end
20
20
 
21
+ def value=(value)
22
+ tk_widget.textvariable.value = value
23
+ end
24
+
25
+ def value
26
+ tk_widget.textvariable.value
27
+ end
28
+
21
29
  def config_for_dummy_label
22
30
  grid_info = TkGrid.info(tk_widget)
23
31
  { config: { grid: {
@@ -29,9 +37,9 @@ class TkWrapper::Widgets::AutoResizeEntry < TkWrapper::Widgets::Entry
29
37
  end
30
38
 
31
39
  def create_dummy_label_with_same_size(&block)
32
- label = Label.new(**config_for_dummy_label)
40
+ label = TkWrapper::Widgets::Label.new(**config_for_dummy_label)
33
41
  label.build(@parent)
34
- label.tk_widget.text = tk_widget.textvariable.value
42
+ label.tk_widget.text = value
35
43
  label.tk_widget.lower
36
44
  result = block.call(label)
37
45
  label.tk_widget.destroy
@@ -5,7 +5,7 @@ require "#{LIB_DIR}/util/hash_recursive.rb"
5
5
  require_relative 'base'
6
6
 
7
7
  class TkWrapper::Widgets::Base::Configuration
8
- attr_accessor :config
8
+ attr_reader :config
9
9
 
10
10
  GRID_SPECIAL_VALUES = {
11
11
  onecell: {
@@ -20,32 +20,24 @@ class TkWrapper::Widgets::Base::Configuration
20
20
  }
21
21
  }.freeze
22
22
 
23
- NON_TK_OPTIONS = %i[id tk_class tearoff weights menu].freeze
23
+ NON_TK_OPTIONS = %i[
24
+ id tk_class tearoff weights menu min_width add_width
25
+ ].freeze
24
26
 
25
27
  def initialize(config)
26
- @config = parse!(config)
28
+ @config = parse_and_clone(config)
27
29
  end
28
30
 
29
- def merge!(*configurations)
31
+ def merge(*configurations)
30
32
  configurations = configurations.map do |configuration|
31
- next configuration.config if configuration.is_a?(self.class)
33
+ configuration = configuration.config if configuration.is_a?(self.class)
32
34
 
33
- parse!(configuration)
35
+ parse_and_clone(configuration)
34
36
  end
35
37
 
36
38
  Util.merge_recursive!(@config, *configurations)
37
39
  end
38
40
 
39
- def parse!(config)
40
- Util.each_recursive(config) do |hash, key, value|
41
- next if value.is_a?(Hash)
42
-
43
- hash[key] = parse_value(key, value)
44
- end
45
-
46
- config
47
- end
48
-
49
41
  def [](key)
50
42
  key = key.to_sym
51
43
  return self.class.new(@config[key]) if @config[key].is_a?(Hash)
@@ -55,7 +47,13 @@ class TkWrapper::Widgets::Base::Configuration
55
47
 
56
48
  def []=(key, value)
57
49
  key = key.to_sym
58
- @config[key] = parse_value(key, value)
50
+ @config[key] = parse_and_clone(value, key)
51
+ end
52
+
53
+ def parse_and_clone(value, key = nil)
54
+ return parse_value(key, value) unless value.is_a?(Hash)
55
+
56
+ value.each_with_object({}) { |(k, v), h| h[k] = parse_and_clone(v, k) }
59
57
  end
60
58
 
61
59
  def parse_value(key, value)
@@ -114,6 +112,6 @@ class TkWrapper::Widgets::Base::Configuration
114
112
  end
115
113
 
116
114
  def merge_global_configurations(manager, widget)
117
- merge!(*manager.configurations(widget))
115
+ merge(*manager.configurations(widget))
118
116
  end
119
117
  end
@@ -3,30 +3,60 @@
3
3
  # manages widgets and their global configurations
4
4
  class TkWrapper::Widgets::Base::Manager
5
5
  def initialize
6
- @configurations = []
7
- @modifications = []
6
+ @configuration_matchers = { regex: {}, map: {} }
7
+ @modification_matchers = { regex: {}, map: {} }
8
8
  end
9
9
 
10
- def add_configuration(matcher, configuration)
11
- @configurations.push([matcher, configuration])
10
+ def add_configurations(matcher = nil, config = nil, **configs)
11
+ add_matcher(matcher, config, @configuration_matchers) if config
12
+
13
+ configs.each { |mat, cfg| add_matcher(mat, cfg, @configuration_matchers) }
12
14
  end
13
15
 
14
16
  def add_modification(matcher, &callback)
15
- @modifications.push([matcher, callback])
17
+ add_matcher(matcher, callback, @modification_matchers)
16
18
  end
17
19
 
18
20
  def configurations(widget)
19
- @configurations.filter_map do |(matcher, config)|
20
- config if widget.check_match(matcher)
21
- end
21
+ configs = find_matching_items(widget.ids, @configuration_matchers)
22
+ configs.map { |config| config[0] }
22
23
  end
23
24
 
24
25
  def execute_modifications(widget)
25
- @modifications.each do |(matcher, callback)|
26
- next unless (match = widget.check_match(matcher))
26
+ callbacks = find_matching_items(widget.ids, @modification_matchers)
27
+ callbacks.each do |callback|
28
+ callback, match = callback
29
+ match ? callback.call(widget, match) : callback.call(widget)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def find_matching_items(keys, container)
36
+ keys.each_with_object([]) do |key, items|
37
+ items.concat(
38
+ items_from_map(key, container),
39
+ items_by_regex(key, container)
40
+ )
41
+ end
42
+ end
43
+
44
+ def items_from_map(key, container)
45
+ (container[:map][key] || []).map { |item| [item, nil] } || []
46
+ end
47
+
48
+ def items_by_regex(key, container)
49
+ container[:regex].each_with_object([]) do |(matcher, items), merged_items|
50
+ match = matcher.match(key)
51
+ merged_items.concat(items.map { |item| [item, match] }) if match
52
+ end
53
+ end
27
54
 
28
- arguments = match.is_a?(MatchData) ? [widget, match] : [widget]
29
- callback.call(*arguments)
55
+ def add_matcher(matcher, item, container)
56
+ if matcher.is_a?(Regexp)
57
+ (container[:regex][matcher] ||= []).push(item)
58
+ else
59
+ (container[:map][matcher] ||= []).push(item)
30
60
  end
31
61
  end
32
62
  end
@@ -18,14 +18,22 @@ class TkWrapper::Widgets::Base::Widget
18
18
  @manager ||= TkWrapper::Widgets::Base::Manager.new
19
19
  end
20
20
 
21
- def self.config(matcher, configuration)
22
- manager.add_configuration(matcher, configuration)
21
+ def self.config(matcher = nil, configuration = nil, **configurations)
22
+ manager.add_configurations(matcher, configuration, **configurations)
23
23
  end
24
24
 
25
25
  def self.modify(matcher, &callback)
26
26
  manager.add_modification(matcher, &callback)
27
27
  end
28
28
 
29
+ def ids
30
+ case @id
31
+ when Array then @id
32
+ when nil then []
33
+ else [@id]
34
+ end
35
+ end
36
+
29
37
  def manager
30
38
  TkWrapper::Widgets::Base::Widget.manager
31
39
  end
@@ -60,6 +68,11 @@ class TkWrapper::Widgets::Base::Widget
60
68
  @childs.each { |child| child.build(self) }
61
69
  end
62
70
 
71
+ def push(child)
72
+ @childs.push(child)
73
+ child.build(self)
74
+ end
75
+
63
76
  def configure
64
77
  @config.merge_global_configurations(manager, self)
65
78
  @config.configure_tk_widget(tk_widget)
data/lib/widgets/grid.rb CHANGED
@@ -4,6 +4,8 @@
4
4
  class TkWrapper::Widgets::Grid < TkWrapper::Widgets::Base::Widget
5
5
  attr_reader :parent, :matrix
6
6
 
7
+ Widget = TkWrapper::Widgets::Base::Widget
8
+
7
9
  def tk_class
8
10
  TkWidgets::Frame
9
11
  end
@@ -20,7 +22,7 @@ class TkWrapper::Widgets::Grid < TkWrapper::Widgets::Base::Widget
20
22
  row.each_with_index do |cell, col_i|
21
23
  next unless cell.is_a?(Widget)
22
24
 
23
- (cell.config[:grid] ||= {}).merge!({ row: row_i, column: col_i })
25
+ cell.config.merge({ grid: { row: row_i, column: col_i } })
24
26
 
25
27
  configure_colspan(cell, row_i, col_i)
26
28
  configure_rowspan(cell, row_i, col_i)
@@ -33,7 +35,7 @@ class TkWrapper::Widgets::Grid < TkWrapper::Widgets::Base::Widget
33
35
  colspan = cols_after_cell.reduce(1) do |span, content|
34
36
  content == :right ? span + 1 : (break span)
35
37
  end
36
- (cell.config[:grid] ||= {}).merge!({ columnspan: colspan })
38
+ cell.config.merge({ grid: { columnspan: colspan } })
37
39
  end
38
40
 
39
41
  def configure_rowspan(cell, row_i, col_i)
@@ -41,6 +43,6 @@ class TkWrapper::Widgets::Grid < TkWrapper::Widgets::Base::Widget
41
43
  rowspan = rows_after_cell.reduce(1) do |span, row|
42
44
  row[col_i] == :bottom ? span + 1 : (break span)
43
45
  end
44
- (cell.config[:grid] ||= {}).merge!({ rowspan: rowspan })
46
+ cell.config.merge({ grid: { rowspan: rowspan } })
45
47
  end
46
48
  end
data/lib/widgets/menu.rb CHANGED
@@ -29,7 +29,7 @@ class TkWrapper::Widgets::Menu < TkWrapper::Widgets::Base::Widget
29
29
  end
30
30
 
31
31
  def self.create(structure: [], config: {})
32
- Menu.new(
32
+ new(
33
33
  config: config,
34
34
  childs: structure.map { |entry| create_subentry(entry) }
35
35
  )
@@ -44,8 +44,8 @@ class TkWrapper::Widgets::Menu < TkWrapper::Widgets::Base::Widget
44
44
 
45
45
  def self.create_subentry(entry)
46
46
  case entry
47
- in { config: config, structure: structure }
48
- return Cascade.new config: config, childs: create_subentries(structure)
47
+ in { config: config, structure: st }
48
+ return Cascade.new config: config, childs: create_subentries(st)
49
49
  else
50
50
  return Command.new config: entry
51
51
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tkwrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Schnitzler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-12 00:00:00.000000000 Z
11
+ date: 2021-12-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: reception@e.mail.de