File: mikutterwindow.rb

package info (click to toggle)
mikutter 5.0.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,700 kB
  • sloc: ruby: 21,307; sh: 181; makefile: 19
file content (113 lines) | stat: -rw-r--r-- 3,213 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-

require 'pathname'

# PostBoxや複数のペインを持つWindow
module Plugin::Gtk3
  class MikutterWindow < Gtk::Window

    attr_reader :panes, :statusbar

    def initialize(imaginally, plugin)
      type_strict plugin => Plugin
      super()

      @imaginally = imaginally
      @plugin = plugin

      @container = Gtk::Box.new(:vertical, 0)
      @panes = Gtk::Grid.new.tap do |panes|
        panes.column_spacing = 6
        panes.column_homogeneous = true
      end
      header = Gtk::Box.new(:horizontal, 0)
      @postboxes = Gtk::Box.new(:vertical, 0)

      header.pack_start(WorldShifter.new, expand: false)
        .pack_start(@postboxes, expand: true, fill: true)

      @container.pack_start(header, expand: false)
        .pack_start(@panes, expand: true, fill: true)
        .pack_start(create_statusbar, expand: false)

      add(@container)

      set_size_request(240, 240)

      Plugin[:gtk3].on_userconfig_modify do |key, newval|
        refresh if key == :postbox_visibility
      end
      Plugin[:gtk3].on_world_after_created do |new_world|
        refresh
      end
      Plugin[:gtk3].on_world_destroy do |deleted_world|
        refresh
      end
    end

    def add_postbox(i_postbox)
      options = {postboxstorage: @postboxes, delegate_other: true}.merge(i_postbox.options||{})
      if options[:delegate_other]
        i_window = i_postbox.ancestor_of(Plugin::GUI::Window)
        options[:delegate_other] = postbox_delegation_generator(i_window) end
      postbox = Gtk::PostBox.new(**options)
      @postboxes.add postbox
      set_focus(postbox.post) unless options[:delegated_by]
      postbox.no_show_all = false
      postbox.show_all if visible?
      postbox end

    private

    def postbox_delegation_generator(window)
      ->(params) do
        postbox = Plugin::GUI::Postbox.instance
        postbox.options = params
        window << postbox end end

    def refresh
      @postboxes.children.each(&(visible? ? :show_all : :hide))
    end

    # ステータスバーを返す
    # ==== Return
    # Gtk::Statusbar
    def create_statusbar
      statusbar = Gtk::Statusbar.new
      statusbar.push(statusbar.get_context_id("system"), @plugin._("Statusbar default message"))
      @statusbar = statusbar.pack_start(status_button(Gtk::Box.new(:horizontal)), expand: false)
    end

    # ステータスバーに表示するWindowレベルのボタンを _container_ にpackする。
    # 返された時点では空で、後からボタンが入る(showメソッドは自動的に呼ばれる)。
    # ==== Args
    # [container] packするコンテナ
    # ==== Return
    # container
    def status_button(container)
      current_world, = Plugin.filtering(:world_current, nil)
      ToolbarGenerator.generate(
        container,
        Plugin::GUI::Event.new(
          event: :window_toolbar,
          widget: @imaginally,
          messages: [],
          world: current_world
        ),
        :window
      )
    end

    def visible?
      case UserConfig[:postbox_visibility]
      when :always
        true
      when :auto
        !!Plugin.collect(:worlds).first
      else
        false
      end
    end

  end
end