aboutsummaryrefslogtreecommitdiffstats
path: root/core/mui/gtk_retriever_header_widget.rb
blob: 4f3d67ac410c46320739204d810cb2fb356900ec (plain)
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
# -*- coding: utf-8 -*-

module Gtk
  # message_detail_viewプラグインなどで使われている、ヘッダ部分のユーザ情報。
  # コンストラクタにはUserではなくMessageなど、userを保持しているRetrieverを渡すことに注意。
  # このウィジェットによって表示されるタイムスタンプをクリックすると、
  # コンストラクタに渡されたretrieverのperma_linkを開くようになっている。
  class RetrieverHeaderWidget < Gtk::EventBox
    def initialize(retriever, *args)
      type_strict retriever => Retriever::Model
      super(*args)
      ssc_atonce(:visibility_notify_event, &widget_style_setter)
      add(Gtk::VBox.new(false, 0).
           closeup(Gtk::HBox.new(false, 0).
                     closeup(icon(retriever.user).top).
                     closeup(Gtk::VBox.new(false, 0).
                              closeup(idname(retriever.user).left).
                              closeup(Gtk::Label.new(retriever.user[:name]).left))).
           closeup(post_date(retriever).right))
    end

    private

    def background_color
      style = Gtk::Style.new()
      style.set_bg(Gtk::STATE_NORMAL, 0xFFFF, 0xFFFF, 0xFFFF)
      style end

    def icon(user)
      type_strict user.profile_image_url_large => String

      icon_alignment = Gtk::Alignment.new(0.5, 0, 0, 0)
                       .set_padding(*[UserConfig[:profile_icon_margin]]*4)

      icon = Gtk::EventBox.new.
             add(icon_alignment.add(Gtk::WebIcon.new(user.profile_image_url_large, UserConfig[:profile_icon_size], UserConfig[:profile_icon_size])))
      icon.ssc(:button_press_event, &icon_opener(user.profile_image_url_large))
      icon.ssc_atonce(:realize, &cursor_changer(Gdk::Cursor.new(Gdk::Cursor::HAND2)))
      icon.ssc_atonce(:visibility_notify_event, &widget_style_setter)
      icon end

    def idname(user)
      label = Gtk::EventBox.new.
              add(Gtk::Label.new.
                   set_markup("<b><u><span foreground=\"#0000ff\">#{Pango.escape(user.idname)}</span></u></b>"))
      label.ssc(:button_press_event, &profile_opener(user))
      label.ssc_atonce(:realize, &cursor_changer(Gdk::Cursor.new(Gdk::Cursor::HAND2)))
      label.ssc_atonce(:visibility_notify_event, &widget_style_setter)
      label end

    def post_date(retriever)
      label = Gtk::EventBox.new.
              add(Gtk::Label.new(retriever.created.strftime('%Y/%m/%d %H:%M:%S')))
      label.ssc(:button_press_event, &message_opener(retriever)) if retriever.perma_link
      label.ssc_atonce(:realize, &cursor_changer(Gdk::Cursor.new(Gdk::Cursor::HAND2)))
      label.ssc_atonce(:visibility_notify_event, &widget_style_setter)
      label end

    def icon_opener(url)
      type_strict url => String
      proc do
        Plugin.call(:openimg_open, url)
        true end end

    def profile_opener(user)
      type_strict user => Retriever::Model
      proc do
        Plugin.call(:show_profile, Service.primary, user)
        true end end

    def message_opener(message)
      type_strict message => Retriever::Model
      proc do
        Gtk.openurl(retriever.perma_link)
        true end end

    memoize def cursor_changer(cursor)
      proc do |w|
        w.window.cursor = cursor
        false end end

    memoize def widget_style_setter
      ->(widget, *_rest) do
        widget.style = background_color
        false end end

  end
end