tkwrapper 1.4.0 → 1.5.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: 81c1971220f794d1e17288aac5b75221393727be934288a4993a621c35b3282d
4
- data.tar.gz: f2a841f6f3a6a7ee040c85fe0f2081234780bf1ff0bf4aeb70de9cf4c2a75055
3
+ metadata.gz: abffb1cc7ae4efaf8b26ebdd49acca56837989829713473f21971f8a5f5a9063
4
+ data.tar.gz: 1e659e3049a41d4aff307f700ac27cc86d014ec844134aa65bc7bd20c2f692db
5
5
  SHA512:
6
- metadata.gz: 22cf843324096411be2441088df910d723ecc353aa6b54dd64ba5703b2fcd47bec3c2424a38e910fefb187fdcf23ec60dd44640de3f0657a3cb2969e84da016c
7
- data.tar.gz: acb296b0e78c570e7d80ba96d91c4ebf62ecfd1ce7c97fac088546d599e0468a23dafac325ee14ae9fa0d4d08a9c8c70e5972df849346a05d8e2ac3ee9fb1154
6
+ metadata.gz: efbad85fe488198ff115feac31872163da0da8b796ef0a21babf33d21976e14da0f809a93a502cbfc0f9225e4446e30c1b567680af2b3c7e7eae862cf0b5f64b
7
+ data.tar.gz: 7d0addd32901d46c6d0175219ee83dd4478f699bc05ec7804f0a76dac02b1950a11f7a22dafa494996961bc8f25582c1f6be4f36c3cf19c9b3a3e5050cb0df9f
data/lib/tkwrapper.rb CHANGED
@@ -2,7 +2,12 @@
2
2
 
3
3
  LIB_DIR = __dir__
4
4
 
5
- module TkWrapper end
5
+ module TkWrapper
6
+ module Util
7
+ module Tk
8
+ end
9
+ end
10
+ end
6
11
 
7
12
  require_relative 'widgets/widgets'
8
13
 
@@ -2,42 +2,32 @@
2
2
 
3
3
  require "#{LIB_DIR}/widgets/base/manager"
4
4
  require "#{LIB_DIR}/widgets/base/matcher"
5
- require_relative 'tk'
5
+ require "#{LIB_DIR}/widgets/base/matches"
6
6
 
7
7
  class TkWrapper::Util::Tk::Finder
8
+ Match = TkWrapper::Widgets::Base::Match
8
9
  Matcher = TkWrapper::Widgets::Base::Matcher
10
+ Matches = TkWrapper::Widgets::Base::Matches
9
11
 
10
- def initialize(widgets: nil)
12
+ def initialize(widgets: nil, lookup: nil)
13
+ @lookup = lookup
11
14
  @widgets = widgets
12
15
  end
13
16
 
14
- def each_widget_match(widgets, matchers, &block)
15
- widgets.each do |widget|
16
- widget.ids.each do |id|
17
- matchers.each do |matcher|
18
- (match = matcher.match(id, widget)) && block.call(match)
19
- end
20
- end
17
+ def iter(comparators, widgets = @widgets, lookup = @lookup)
18
+ Enumerator.new do |y|
19
+ comparators = each_widget_lookup_match(lookup, comparators) { |m| y << m }
20
+ each_widget_comparator_match(widgets, comparators) { |m| y << m }
21
21
  end
22
22
  end
23
23
 
24
- def find(comparators, widgets = @widgets)
25
- matchers = create_value_matchers(comparators)
26
-
27
- each_widget_match(widgets, matchers) do |match|
28
- return match.widget if match
29
- end
24
+ def find(comparators, widgets = @widgets, lookup = @lookup)
25
+ iter(comparators, widgets, lookup, &:itself).first
30
26
  end
31
27
 
32
- def find_all(comparators, widgets = @widgets)
33
- matchers = create_value_matchers(comparators)
34
- matches = TkWrapper::Widgets::Base::Matches.new
35
-
36
- each_widget_match(widgets, matchers) do |match|
37
- matches.push(match)
38
- end
39
-
40
- matches
28
+ def find_all(comparators, widgets = @widgets, lookup = @lookup)
29
+ it = iter(comparators, widgets, lookup, &:itself)
30
+ it.each_with_object(Matches.new) { |match, matches| matches.push(match) }
41
31
  end
42
32
 
43
33
  private
@@ -46,4 +36,30 @@ class TkWrapper::Util::Tk::Finder
46
36
  comparators = [comparators] unless comparators.is_a?(Array)
47
37
  comparators.map { |comparator| Matcher.new(comparator: comparator) }
48
38
  end
39
+
40
+ def each_widget_lookup_match(lookup, comparators, &block)
41
+ return comparators unless lookup
42
+
43
+ comparators.filter do |comparator|
44
+ next true unless [String, Symbol].include?(comparator.class)
45
+
46
+ (lookup[comparator] || []).each do |widget|
47
+ block.call(Match.new(comparator, widget: widget))
48
+ end
49
+
50
+ false
51
+ end
52
+ end
53
+
54
+ def each_widget_comparator_match(widgets, comparators, &block)
55
+ matchers = create_value_matchers(comparators)
56
+
57
+ widgets.each do |widget|
58
+ widget.ids.each do |id|
59
+ matchers.each do |matcher|
60
+ (match = matcher.match(id, widget)) && block.call(match)
61
+ end
62
+ end
63
+ end
64
+ end
49
65
  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(**args)
8
- @min_width = args[:config][:min_width] || 0
9
- @add_width = args[:config][:add_width] || 0
8
+ @min_width = args.dig(:config, :min_width) || 0
9
+ @add_width = args.dig(:config, :add_width) || 0
10
10
  super(**args)
11
11
  end
12
12
 
@@ -27,7 +27,7 @@ class TkWrapper::Widgets::AutoResizeEntry < TkWrapper::Widgets::Entry
27
27
  end
28
28
 
29
29
  def resize
30
- max_width = @cell.bbox[2]
30
+ max_width = [@cell.bbox[2], 0].max
31
31
  text_width = @font.measure(value)
32
32
  new_width = [[@min_width, text_width + @add_width].max, max_width].min
33
33
  tk_widget.width = 0
@@ -10,8 +10,12 @@ class TkWrapper::Widgets::Base::ComparatorItemStore
10
10
  @comparator_map = {} # for lookup using comparisons by Matcher class
11
11
  end
12
12
 
13
+ def map_key?(key)
14
+ [String, Symbol].include?(key)
15
+ end
16
+
13
17
  def push(key, *items)
14
- if [String, Symbol].include?(key)
18
+ if map_key?(key)
15
19
  (@key_map[key] ||= []).concat(items)
16
20
  else
17
21
  (@comparator_map[key] ||= []).concat(items)
@@ -25,6 +29,11 @@ class TkWrapper::Widgets::Base::ComparatorItemStore
25
29
  end
26
30
  end
27
31
 
32
+ def [](key)
33
+ items = map_key?(key) ? @key_map[key] : @comparator_map[key]
34
+ items&.length == 1 ? items.first : items
35
+ end
36
+
28
37
  private
29
38
 
30
39
  def items_from_key_map(id)
@@ -1,13 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'comparator_item_store'
4
+ require_relative 'widget_store'
4
5
 
5
6
  class TkWrapper::Widgets::Base::Manager
6
7
  ComparatorItemStore = TkWrapper::Widgets::Base::ComparatorItemStore
8
+ WidgetStore = TkWrapper::Widgets::Base::WidgetStore
9
+
10
+ attr_reader :widgets
7
11
 
8
12
  def initialize
9
13
  @configurations = ComparatorItemStore.new
10
14
  @modifications = ComparatorItemStore.new
15
+ @widgets = WidgetStore.new
11
16
  end
12
17
 
13
18
  def add_configurations(matcher = nil, configuration = nil, **configurations)
@@ -31,4 +31,8 @@ class TkWrapper::Widgets::Base::Matches
31
31
  else @matches[key]
32
32
  end
33
33
  end
34
+
35
+ def first
36
+ @matches.first&.[](1)
37
+ end
34
38
  end
@@ -49,6 +49,7 @@ class TkWrapper::Widgets::Base::Widget
49
49
  @config.merge(*@manager.configurations(self)) if @manager
50
50
  self.configure if configure
51
51
  @manager&.execute_modifications(self)
52
+ @manager&.widgets&.push(self)
52
53
  @childs.each { |child| child.build(self, manager: @manager) }
53
54
  end
54
55
 
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "#{LIB_DIR}/util/tk/finder"
4
+ require_relative 'comparator_item_store'
5
+
6
+ class TkWrapper::Widgets::Base::WidgetStore
7
+ extend Forwardable
8
+
9
+ def_delegators :@finder, :find, :find_all, :iter
10
+
11
+ def initialize
12
+ @lookup = {}
13
+ @finder = TkWrapper::Util::Tk::Finder.new(widgets: self, lookup: @lookup)
14
+ end
15
+
16
+ def push(widget)
17
+ widget.ids.each do |id|
18
+ (@lookup[id] ||= []).push(widget)
19
+ end
20
+ end
21
+
22
+ def each(&block)
23
+ @lookup.each_value do |widgets|
24
+ widgets.each { |widget| block.call(widget) }
25
+ end
26
+ end
27
+
28
+ def [](key)
29
+ @lookup[key].size == 1 ? @lookup[key].first : @lookup[key]
30
+ end
31
+
32
+ private
33
+
34
+ def map_key?(key)
35
+ [String, Symbol].include?(key)
36
+ end
37
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tkwrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.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-16 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-12-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.4'
13
27
  description:
14
28
  email: reception@e.mail.de
15
29
  executables: []
@@ -33,6 +47,7 @@ files:
33
47
  - lib/widgets/base/matcher.rb
34
48
  - lib/widgets/base/matches.rb
35
49
  - lib/widgets/base/widget.rb
50
+ - lib/widgets/base/widget_store.rb
36
51
  - lib/widgets/entry.rb
37
52
  - lib/widgets/frame.rb
38
53
  - lib/widgets/grid.rb