Debian Codegraph @

plugin/gui/hierarchy_child.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # ウィンドウパーツ階層構造の子
3
4 module Plugin::GUI::HierarchyChild
5
6 class << self
7 def included(klass)
8 if klass.include?(Plugin::GUI::HierarchyParent)
9 raise "Plugin::GUI::HierarchyChild must be included before the Plugin::GUI::HierarchyParent."
10 end
11 klass.extend(Extended)
12 end
13 end
14
15 attr_reader :parent
16
17 # 親を _parent_ に設定
18 # ==== Args
19 # [parent] 親
20 # ==== Return
21 # self
22 def set_parent(parent)
23 type_strict parent => @parent_class
24 return self if @parent == parent
25 @parent.remove(self) if @parent
26 @parent = parent
27 if self.class.set_parent_event
28 Plugin.call(self.class.set_parent_event, self, parent) end
29 self end
30
31 def active_class_of(klass)
32 self if is_a? klass end
33
34 # 先祖のうち、 _klass_ と is_a? 関係にあるものを返す
35 # ==== Args
36 # [klass] 探すクラス
37 # ==== Return
38 # マッチしたウィジェットかnil
39 def ancestor_of(klass)
40 return self if is_a?(klass)
41 case @parent
42 when Plugin::GUI::HierarchyChild
43 @parent.ancestor_of(klass)
44 when klass
45 @parent
46 end
47 end
48
49 # 親を再帰的に辿り、selfをアクティブに設定する
50 # ==== Args
51 # [just_this] 再帰的に呼び出されたのではなく、直接これをアクティブに指定されたなら真
52 # [by_toolkit] UIツールキットの入力でアクティブになった場合真
53 # ==== Return
54 # self
55 def active!(just_this=true, by_toolkit=false)
56 @parent.set_active_child(self, by_toolkit).active!(false, by_toolkit)
57 self end
58
59 module Extended
60 attr_reader :parent_class
61
62 # set_parentが呼ばれた時に発生させるイベントを設定する
63 def set_parent_event(event = nil)
64 if event
65 @set_parent_event = event
66 else
67 @set_parent_event end end
68
69 # 親クラスを設定する。親にはこのクラスのインスタンス以外認めない
70 # ==== Args
71 # [klass] 親クラス
72 def set_parent_class(klass)
73 @parent_class = klass end
74
75 # 親クラスを再帰的にたどっていって、一番上の親クラスを返す
76 def ancestor
77 if @parent_class.respond_to? :ancestor
78 @parent_class.ancestor
79 else
80 @parent_class end end
81
82 # 現在アクティブなインスタンスを返す
83 # ==== Return
84 # アクティブなインスタンス又はnil
85 def active
86 widget = ancestor.active
87 widget.active_class_of(self) if widget end
88
89 end
90
91 end