tkwrapper 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42d786c8eda480c18150ef9b6c3e67fc04c14d2c9ec70a037907dee5a9273264
4
- data.tar.gz: b669a2881c794270a80da8332a55393c90a7bb3115273b5c28fc5681fb43faf5
3
+ metadata.gz: a31665aae98cd470f88451d45cd6332030c8fe874e18faa1040bcd42103d9ace
4
+ data.tar.gz: 93f428a63f6bf0832bf91accfdec1f14715cbfc6de2e9055f29b4cb146da0bf7
5
5
  SHA512:
6
- metadata.gz: 1c583155569bc2adf08a5177c35e123e45a69c997ecb3aa70d78841ab4418406a7daa83277c7efe7654585c77932b58094a4bace040d5c9555de30e4e493c9b0
7
- data.tar.gz: 63cb56447bc90a44a86251cfc5ef4507a5482063abb2f24a906610d6d23c3ba8cb341bf64a34ae027577ffbf6bb47ab4935e88a7ab56198e39b44e7f2d0cb890
6
+ metadata.gz: 461b4344678f1d7a374ad6d1d01ffbab5c4865a9c513457c7145b21e9582e9d0922d3666a93adf6ed8f57428c6485b8e693694438db1966a7967c01c0ae6e3fc
7
+ data.tar.gz: f0a8d152e7aac88206454340c2fbce7e605117a85e2dc0cd550bffc4db9e49d09dd21b4610e52e19a87dc6313ce37b98c9a4bc4e8c44bf033b3f84fe41289833
@@ -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: {
@@ -31,7 +39,7 @@ class TkWrapper::Widgets::AutoResizeEntry < TkWrapper::Widgets::Entry
31
39
  def create_dummy_label_with_same_size(&block)
32
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
@@ -20,7 +20,9 @@ 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
28
  @config = parse_and_clone(config)
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tkwrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Schnitzler