Debian Codegraph @

plugin/modelviewer/modelviewer.rb

raw symbols SCIP
1 # frozen_string_literal: true
2
3 UserConfig[:profile_icon_size] ||= 48
4 UserConfig[:profile_icon_margin] ||= 4
5
6 Plugin.create :modelviewer do
7 defdsl :defmodelviewer do |model_class, &block|
8 model_class = Diva::Model(model_class) unless model_class.is_a?(Class)
9 filter_modelviewer_models do |models|
10 models << model_class.spec
11 [models]
12 end
13 intent(
14 model_class,
15 label: _('%{model}の詳細') % { model: model_class.spec&.name || model_class.name },
16 slug: :"modelviewer:#{model_class.slug}"
17 ) do |token|
18 model = token.model
19 tab_slug = :"modelviewer:#{model_class.slug}:#{model.uri.hash}"
20 cluster_slug = :"modelviewer-cluster:#{model_class.slug}:#{model.uri.hash}"
21 if Plugin::GUI::Tab.exist?(tab_slug)
22 Plugin::GUI::Tab.instance(tab_slug).active!
23 else
24 tab(tab_slug, _('%{title}について') % { title: model.title }) do
25 set_icon model.icon if model.respond_to?(:icon)
26 set_deletable true
27 temporary_tab true
28 shrink
29 nativewidget Plugin[:modelviewer].header(token, &block)
30 expand
31 Plugin[:modelviewer].cluster_initialize(model, cluster(cluster_slug))
32 active!
33 end
34 end
35 end
36 end
37
38 # プロフィールタブを定義する
39 # ==== Args
40 # [slug] タブスラッグ
41 # [title] タブのタイトル
42 defdsl :deffragment do |model_class, slug, title=slug.to_s, &block|
43 model_class = Diva::Model(model_class) unless model_class.is_a?(Class)
44 add_event_filter(:"modelviewer_#{model_class.slug}_fragments") do |tabs, model|
45 i_fragment = Plugin::GUI::Fragment.instance(:"modelviewer-fragment:#{slug}:#{model.uri}", title)
46 i_fragment.instance_eval_with_delegate(self, model, &block)
47 tabs << i_fragment
48 [tabs, model]
49 end
50 end
51
52 on_gui_child_reordered do |i_cluster, i_fragment, _|
53 kind, = i_fragment.slug.to_s.split(':', 2)
54 if kind == 'modelviewer-fragment'
55 _, cluster_kind, = i_cluster.slug.to_s.split(':', 3)
56 store("order-#{cluster_kind}", i_cluster.children.map { |f| f.slug.to_s.split(':', 3)[1] })
57 end
58 end
59
60 def cluster_initialize(model, i_cluster)
61 _, cluster_kind, = i_cluster.slug.to_s.split(':', 3)
62 order = at("order-#{cluster_kind}", [])
63 fragments = Plugin.collect(:"modelviewer_#{model.class.slug}_fragments", Pluggaloid::COLLECT, model).sort_by { |i_fragment|
64 _, fragment_kind, = i_fragment.slug.to_s.split(':', 3)
65 order.index(fragment_kind) || Float::INFINITY
66 }.to_a
67 fragments.each(&i_cluster.method(:add_child))
68 fragments.first&.active!
69 end
70
71 def header(intent_token, &column_generator)
72 model = intent_token.model
73
74 icon = model_icon model
75 icon.margin = UserConfig[:profile_icon_margin]
76 icon.valign = :start
77
78 title = title_widget model
79 title.hexpand = true
80
81 table = header_table(model, column_generator.(model))
82 table.hexpand = true
83
84 grid = Gtk::Grid.new
85 grid.margin_top = grid.margin_bottom = 4
86 grid.attach_next_to icon, nil, :left, 1, 2
87 grid.attach_next_to title, icon, :right, 1, 1
88 grid.attach_next_to table, title, :bottom, 1, 1
89 grid
90 end
91
92 def model_icon(model)
93 return ::Gtk::EventBox.new unless model.respond_to?(:icon)
94 icon = ::Gtk::EventBox.new.add(::Gtk::WebIcon.new(model.icon, UserConfig[:profile_icon_size], UserConfig[:profile_icon_size]).set_tooltip_text(_('アイコンを開く')))
95 icon.ssc(:button_press_event) do
96 Plugin.call(:open, model.icon)
97 true
98 end
99 icon.ssc(:realize) do |this|
100 this.window.set_cursor(Gdk::Cursor.new(:hand2))
101 false
102 end
103 icon
104 end
105
106 # modelのtitleを表示する
107 # ==== Args
108 # [model] 表示するmodel
109 # [intent_token] ユーザを開くときに利用するIntent
110 # ==== Return
111 # ユーザの名前の部分のGtkコンテナ
112 def title_widget(model)
113 score = [
114 Plugin::Score::HyperLinkNote.new(
115 description: model.title,
116 uri: model.uri
117 )
118 ]
119 ::Gtk::IntelligentTextview.new(score)
120 end
121
122 # modelのtitleを表示する
123 # ==== Args
124 # [model] 表示するmodel
125 # [intent_token] ユーザを開くときに利用するIntent
126 # ==== Return
127 # ユーザの名前の部分のGtkコンテナ
128 def cell_widget(model_or_str)
129 case model_or_str
130 when Diva::Model
131 ::Gtk::IntelligentTextview.new(
132 Plugin[:modelviewer].score_of(model_or_str),
133 )
134 else
135 Gtk::Label.new model_or_str.to_s
136 end
137 end
138
139 def header_table(model, rows)
140 grid = Gtk::Grid.new
141 grid.row_spacing = 4
142 grid.column_spacing = 16
143
144 rows.each do |header, content|
145 label_header = Gtk::Label.new header
146 label_header.halign = :end
147
148 widget_content = cell_widget content
149 widget_content.halign = :start
150
151 grid.attach_next_to label_header, nil, :bottom, 1, 1
152 grid.attach_next_to widget_content, label_header, :right, 1, 1
153 end
154 grid
155 end
156 end