| 1 |
|
| 2 |
require 'mui/gtk_extension' |
| 3 |
require 'mui/gtk_contextmenu' |
| 4 |
require 'plugin' |
| 5 |
require 'miku/miku' |
| 6 |
|
| 7 |
require 'gtk3' |
| 8 |
require 'uri' |
| 9 |
|
| 10 |
class Gtk::IntelligentTextview < Gtk::TextView |
| 11 |
extend Gem::Deprecate |
| 12 |
|
| 13 |
attr_accessor :fonts |
| 14 |
attr_writer :style_generator |
| 15 |
alias :get_background= :style_generator= |
| 16 |
deprecate :get_background=, "style_generator=", 2017, 02 |
| 17 |
|
| 18 |
@@linkrule = MIKU::Cons.list([URI.regexp(['http','https']), |
| 19 |
lambda{ |u, clicked| self.openurl(u) }, |
| 20 |
lambda{ |u, clicked| |
| 21 |
Gtk::ContextMenu.new(['リンクのURLをコピー', ret_nth, lambda{ |opt, w| Gtk::Clipboard.copy(u) }], |
| 22 |
['開く', ret_nth, lambda{ |opt, w| self.openurl(u) }]). |
| 23 |
popup(clicked, true)}]) |
| 24 |
@@widgetrule = [] |
| 25 |
|
| 26 |
def self.addlinkrule(reg, leftclick, rightclick=nil) |
| 27 |
@@linkrule = MIKU::Cons.new([reg, leftclick, rightclick].freeze, @@linkrule).freeze end |
| 28 |
|
| 29 |
def self.addwidgetrule(reg, widget = nil, &block) |
| 30 |
@@widgetrule = @@widgetrule.unshift([reg, (widget || block)]) end |
| 31 |
|
| 32 |
開く |
| 33 |
def self.openurl(url) |
| 34 |
Plugin.call(:open, url) |
| 35 |
false end |
| 36 |
|
| 37 |
def initialize(msg = nil, default_fonts = {}, *rest, style: nil) |
| 38 |
super(*rest) |
| 39 |
@fonts = default_fonts |
| 40 |
@style_generator = style |
| 41 |
self.editable = false |
| 42 |
self.cursor_visible = false |
| 43 |
self.wrap_mode = :char |
| 44 |
gen_body(msg) if msg |
| 45 |
style_context.add_class('intelligentTextView') |
| 46 |
Gtk::CssProvider.new.tap do |provider| |
| 47 |
provider.load_from_data(<<~CSS) |
| 48 |
.intelligentTextView, .intelligentTextView > text { |
| 49 |
background-color: transparent; |
| 50 |
} |
| 51 |
CSS |
| 52 |
style_context.add_provider(provider, Gtk::StyleProvider::PRIORITY_APPLICATION) |
| 53 |
end |
| 54 |
end |
| 55 |
|
| 56 |
ェットの背景色を返す |
| 57 |
|
| 58 |
|
| 59 |
def style_generator |
| 60 |
if @style_generator.respond_to? :to_proc |
| 61 |
@style_generator.to_proc.call |
| 62 |
elsif @style_generator |
| 63 |
@style_generator |
| 64 |
end |
| 65 |
end |
| 66 |
alias :get_background :style_generator |
| 67 |
deprecate :get_background, "style_generator", 2017, 02 |
| 68 |
|
| 69 |
ベートにする |
| 70 |
def set_cursor(textview, cursor) |
| 71 |
textview.get_window(:text).set_cursor(Gdk::Cursor.new(cursor)) |
| 72 |
end |
| 73 |
|
| 74 |
def bg_modifier(color = style_generator) |
| 75 |
if color.is_a? Gtk::Style |
| 76 |
warn 'Gtk::IntelligentTextview#bg_modifier(Gtk::Style) is deprecated.' |
| 77 |
color = color.to_style_provider |
| 78 |
end |
| 79 |
if color.is_a? Gtk::StyleProvider |
| 80 |
style_context.remove_provider(@style_provider) if @style_provider |
| 81 |
style_context.add_provider(color, Gtk::StyleProvider::PRIORITY_APPLICATION) |
| 82 |
@style_provider = color |
| 83 |
elsif color && get_window(:text).respond_to?(:background=) |
| 84 |
get_window(:text).background = color |
| 85 |
end |
| 86 |
queue_draw |
| 87 |
end |
| 88 |
|
| 89 |
sg_ に内容を差し替える。 |
| 90 |
|
| 91 |
する文字列 |
| 92 |
|
| 93 |
|
| 94 |
def rewind(msg) |
| 95 |
set_buffer(Gtk::TextBuffer.new) |
| 96 |
gen_body(msg) |
| 97 |
end |
| 98 |
|
| 99 |
private |
| 100 |
|
| 101 |
def fonts2tags(fonts) |
| 102 |
tags = Hash.new |
| 103 |
tags['font'] = UserConfig[fonts['font']] if fonts.has_key?('font') |
| 104 |
if fonts.has_key?('foreground') |
| 105 |
tags['foreground_gdk'] = Gdk::Color.new(*UserConfig[fonts['foreground']]) end |
| 106 |
tags |
| 107 |
end |
| 108 |
|
| 109 |
def gen_body(msg, fonts={}) |
| 110 |
tag_shell = buffer.create_tag('shell', fonts2tags(fonts)) |
| 111 |
case msg |
| 112 |
when String |
| 113 |
type_strict fonts => Hash |
| 114 |
tags = fonts2tags(fonts) |
| 115 |
buffer.insert(buffer.start_iter, msg, { tags: %w[shell] }) |
| 116 |
apply_links |
| 117 |
apply_inner_widget |
| 118 |
when Enumerable |
| 119 |
pos = buffer.end_iter |
| 120 |
msg.each_with_index do |note, index| |
| 121 |
case |
| 122 |
when UserConfig[:miraclepainter_expand_custom_emoji] && note.respond_to?(:inline_photo) |
| 123 |
photo = note.inline_photo |
| 124 |
font_size = tag_shell.font_desc.forecast_font_size |
| 125 |
start = pos.offset |
| 126 |
pixbuf = photo.load_pixbuf(width: font_size, height: font_size) do |loaded_pixbuf| |
| 127 |
unless buffer.destroyed? |
| 128 |
insertion_start = buffer.get_iter_at(offset: start) |
| 129 |
buffer.delete(insertion_start, buffer.get_iter_at(offset: start+1)) |
| 130 |
buffer.insert(insertion_start, loaded_pixbuf) |
| 131 |
end |
| 132 |
end |
| 133 |
buffer.insert(pos, pixbuf) |
| 134 |
when clickable?(note) |
| 135 |
tagname = "tag#{index}" |
| 136 |
create_tag_ifnecessary(tagname, buffer, |
| 137 |
->(_tagname, _textview){ |
| 138 |
Plugin.call(:open, note) |
| 139 |
}, nil) |
| 140 |
start = pos.offset |
| 141 |
buffer.insert(pos, note.description) |
| 142 |
buffer.apply_tag(tagname, buffer.get_iter_at(offset: start), pos) |
| 143 |
else |
| 144 |
buffer.insert(pos, note.description, { tags: %w[shell] }) |
| 145 |
end |
| 146 |
end |
| 147 |
end |
| 148 |
set_events(tag_shell) |
| 149 |
self |
| 150 |
end |
| 151 |
|
| 152 |
def set_events(tag_shell) |
| 153 |
self.signal_connect('realize'){ bg_modifier } |
| 154 |
self.signal_connect('visibility-notify-event'){ |
| 155 |
if fonts['font'] and tag_shell.font != UserConfig[fonts['font']] |
| 156 |
tag_shell.font = UserConfig[fonts['font']] end |
| 157 |
if fonts['foreground'] and tag_shell.foreground_gdk.to_s != UserConfig[fonts['foreground']] |
| 158 |
tag_shell.foreground_gdk = Gdk::Color.new(*UserConfig[fonts['foreground']]) end |
| 159 |
false } |
| 160 |
self.signal_connect('event'){ |
| 161 |
set_cursor(self, :xterm) |
| 162 |
false } |
| 163 |
end |
| 164 |
|
| 165 |
def create_tag_ifnecessary(tagname, buffer, leftclick, rightclick) |
| 166 |
tag = buffer.create_tag(tagname, "underline" => Pango::Underline::SINGLE) |
| 167 |
tag.signal_connect('event'){ |this, textview, event, iter| |
| 168 |
result = false |
| 169 |
if(event.is_a?(Gdk::EventButton)) and |
| 170 |
(event.event_type == Gdk::Event::BUTTON_RELEASE) and |
| 171 |
not(textview.buffer.selection_bounds) |
| 172 |
if (event.button == 1 and leftclick) |
| 173 |
leftclick.call(tagname, textview) |
| 174 |
elsif(event.button == 3 and rightclick) |
| 175 |
rightclick.call(tagname, textview) |
| 176 |
result = true end |
| 177 |
elsif(event.is_a?(Gdk::EventMotion)) |
| 178 |
set_cursor(textview, :hand2) |
| 179 |
end |
| 180 |
result } |
| 181 |
tag end |
| 182 |
|
| 183 |
def apply_links |
| 184 |
@@linkrule.each{ |param| |
| 185 |
reg, left, right = param |
| 186 |
buffer.text.scan(reg) { |
| 187 |
match = Regexp.last_match |
| 188 |
index = buffer.text[0, match.begin(0)].size |
| 189 |
body = match.to_s.freeze |
| 190 |
create_tag_ifnecessary(body, buffer, left, right) if not buffer.tag_table.lookup(body) |
| 191 |
range = buffer.get_range(index, body.size) |
| 192 |
buffer.apply_tag(body, *range) |
| 193 |
} } end |
| 194 |
|
| 195 |
def apply_inner_widget |
| 196 |
offset = 0 |
| 197 |
@@widgetrule.each{ |param| |
| 198 |
reg, widget_generator = param |
| 199 |
buffer.text.scan(reg) { |match| |
| 200 |
match = Regexp.last_match |
| 201 |
index = [buffer.text.size, match.begin(0)].min |
| 202 |
body = match.to_s.freeze |
| 203 |
range = buffer.get_range(index, body.size + offset) |
| 204 |
widget = widget_generator.call(body) |
| 205 |
if widget |
| 206 |
self.add_child_at_anchor(widget, buffer.create_child_anchor(range[1])) |
| 207 |
offset += 1 end } } end |
| 208 |
|
| 209 |
def clickable?(model) |
| 210 |
has_model_intent = Plugin.collect(:intent_select_by_model_slug, model.class.slug, Pluggaloid::COLLECT).first |
| 211 |
return true if has_model_intent |
| 212 |
Plugin.collect(:model_of_uri, model.uri).any? do |model_slug| |
| 213 |
Plugin.collect(:intent_select_by_model_slug, model_slug, Pluggaloid::COLLECT).first |
| 214 |
end |
| 215 |
end |
| 216 |
end |