Debian Codegraph @

plugin/gtk3/widget/mikutterwindow.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 require 'pathname'
4
5 # PostBoxや複数のペインを持つWindow
6 module Plugin::Gtk3
7 class MikutterWindow < Gtk::Window
8 attr_reader :panes, :statusbar
9
10 def initialize(imaginally, plugin)
11 type_strict plugin => Plugin
12 super()
13
14 @imaginally = imaginally
15 @plugin = plugin
16
17 @container = Gtk::Box.new(:vertical, 0)
18 @panes = Gtk::Grid.new.tap do |panes|
19 panes.column_spacing = 6
20 panes.column_homogeneous = true
21 end
22 header = Gtk::Box.new(:horizontal, 0)
23 @postboxes = Gtk::Box.new(:vertical, 0)
24
25 header.pack_start(WorldShifter.new, expand: false)
26 .pack_start(@postboxes, expand: true, fill: true)
27
28 @container.pack_start(header, expand: false)
29 .pack_start(@panes, expand: true, fill: true)
30 .pack_start(create_statusbar, expand: false)
31
32 add(@container)
33
34 set_size_request(240, 240)
35
36 Plugin[:gtk3].on_userconfig_modify do |key, _newval|
37 refresh if key == :postbox_visibility
38 end
39 Plugin[:gtk3].on_world_after_created do |_new_world|
40 refresh
41 end
42 Plugin[:gtk3].on_world_destroy do |_deleted_world|
43 refresh
44 end
45 end
46
47 def add_postbox(i_postbox)
48 options = { postboxstorage: @postboxes, delegate_other: true }.merge(i_postbox.options || {})
49 if options[:delegate_other]
50 i_window = i_postbox.ancestor_of(Plugin::GUI::Window)
51 options[:delegate_other] = postbox_delegation_generator(i_window) end
52 postbox = Gtk::PostBox.new(**options)
53 @postboxes.add postbox
54 set_focus(postbox.post) unless options[:delegated_by]
55 postbox.no_show_all = false
56 postbox.show_all if visible?
57 postbox
58 end
59
60 private
61
62 def postbox_delegation_generator(window)
63 ->(params) do
64 postbox = Plugin::GUI::Postbox.instance
65 postbox.options = params
66 window << postbox
67 end
68 end
69
70 def refresh
71 @postboxes.children.each(&(visible? ? :show_all : :hide))
72 end
73
74 # ステータスバーを返す
75 # ==== Return
76 # Gtk::Statusbar
77 def create_statusbar
78 statusbar = Gtk::Statusbar.new
79 statusbar.push(statusbar.get_context_id('system'), @plugin._('Statusbar default message'))
80 @statusbar = statusbar.pack_start(status_button(Gtk::Box.new(:horizontal)), expand: false)
81 end
82
83 # ステータスバーに表示するWindowレベルのボタンを _container_ にpackする。
84 # 返された時点では空で、後からボタンが入る(showメソッドは自動的に呼ばれる)。
85 # ==== Args
86 # [container] packするコンテナ
87 # ==== Return
88 # container
89 def status_button(container)
90 current_world, = Plugin.filtering(:world_current, nil)
91 ToolbarGenerator.generate(
92 container,
93 Plugin::GUI::Event.new(
94 event: :window_toolbar,
95 widget: @imaginally,
96 messages: [],
97 world: current_world
98 ),
99 :window
100 )
101 end
102
103 def visible?
104 case UserConfig[:postbox_visibility]
105 when :always
106 true
107 when :auto
108 !!Plugin.collect(:worlds).first
109 else
110 false
111 end
112 end
113 end
114 end