Debian Codegraph @

plugin/gui/hierarchy_parent.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # ウィンドウパーツ階層構造の親
3 # これをincludeするクラスは、クラスメソッドとしてactiveを実装している必要がある
4
5 module Plugin::GUI::HierarchyParent
6
7 class << self
8 def included(klass)
9 klass.extend(Extended)
10 end
11 end
12
13 attr_reader :active_child
14
15 # 子を追加する
16 # ==== Args
17 # [child] 子のインスタンス
18 # [index] 挿入するインデックス
19 # ==== Return
20 # self
21 def add_child(child, index=children.size)
22 type_strict child => Plugin::GUI::HierarchyChild
23 return self if child.parent == self
24 children.insert(index, child)
25 child.set_parent(self)
26 @active_child ||= child
27 self end
28 alias << add_child
29
30 # _child_ のn順番を変更する。 _new_index_ 以降はひとつづつシフトする
31 # ==== Args
32 # [child] 子
33 # [new_index] 新しく入れるインデックス
34 def reorder_child(child, new_index)
35 type_strict child => Plugin::GUI::HierarchyChild
36 if children.include?(child)
37 @children.delete(child)
38 @children.insert(new_index, child)
39 Plugin.call(:gui_child_reordered, self, child, new_index)
40 else
41 error "the widget #{child.inspect} is not child of #{self.inspect}" end
42 self end
43
44 # 子を削除する
45 # ==== Args
46 # [child] 削除する子
47 # ==== Return
48 # self
49 def remove(child)
50 children.delete(child)
51 if @active_child == child
52 @active_child = nil end
53 self end
54
55 # 子の配列を返す
56 # ==== Return
57 # 子の配列
58 def children
59 @children ||= [] end
60
61 # 子 _child_ をアクティブに設定する
62 # ==== Args
63 # [child] アクティブにする子
64 # [by_toolkit] UIツールキットの入力でアクティブになった場合真
65 # ==== Return
66 # self
67 def set_active_child(child, by_toolkit=false)
68 type_strict child => tcor(Plugin::GUI::HierarchyChild, NilClass)
69 @active_child = child
70 Plugin.call(:gui_child_activated, self, child, by_toolkit) if child
71 self end
72
73 # このインスタンス以下の、アクティブな祖先のリストを返す。
74 # ==== Return
75 # アクティブな子、孫、…のリスト 又は空の配列
76 def active_chain
77 if @active_child
78 result = [@active_child]
79 ancestors = @active_child.respond_to?(:active_chain) && @active_child.active_chain
80 ancestors ? result + ancestors : result
81 else
82 [] end end
83
84 # active_chain が返すインスタンスのうち、最初に _klass_ とis_a関係にあるものを返す。
85 # ==== Args
86 # [klass] クラス
87 # ==== Return
88 # 一致する祖先か見つからなければnil
89 def active_class_of(klass)
90 active_chain.find{ |child| child.is_a? klass } end
91
92 module Extended
93 # 子を追加するデフォルトのインスタンスを返す
94 def add_default
95 active end
96 end
97 end