| 1 |
|
| 2 |
|
| 3 |
module Plugin::OpenimgGtk |
| 4 |
class Window < Gtk::Window |
| 5 |
attr_reader :photo |
| 6 |
|
| 7 |
def initialize(photo, next_opener) |
| 8 |
super() |
| 9 |
@photo = photo |
| 10 |
@image_surface = loading_surface |
| 11 |
@next_opener = next_opener |
| 12 |
window_settings |
| 13 |
ssc :key_release_event do |_, ev| |
| 14 |
destroy if ::Gtk.keyname([ev.keyval, ev.state]) == 'Escape' |
| 15 |
end |
| 16 |
ssc(:destroy, &:destroy) |
| 17 |
end |
| 18 |
|
| 19 |
def start_loading |
| 20 |
Thread.new { |
| 21 |
Plugin.filtering(:openimg_pixbuf_from_display_url, photo, nil, nil) |
| 22 |
}.next { |_, pixbufloader, complete_promise| |
| 23 |
if pixbufloader.is_a?(GdkPixbuf::PixbufLoader) |
| 24 |
pixbufloader.ssc(:area_updated, self, &pixbufloader_area_update_handler) |
| 25 |
complete_promise.next { |
| 26 |
progress(pixbufloader.pixbuf) |
| 27 |
}.trap { |exception| |
| 28 |
error exception |
| 29 |
@image_surface = error_surface |
| 30 |
redraw(repaint: true) |
| 31 |
}.next do |
| 32 |
pixbufloader.close |
| 33 |
end |
| 34 |
else |
| 35 |
warn "cant open: #{photo}" |
| 36 |
@image_surface = error_surface |
| 37 |
redraw(repaint: true) |
| 38 |
end |
| 39 |
}.trap do |exception| |
| 40 |
error exception |
| 41 |
@image_surface = error_surface |
| 42 |
redraw(repaint: true) |
| 43 |
end |
| 44 |
self |
| 45 |
end |
| 46 |
|
| 47 |
private |
| 48 |
|
| 49 |
def pixbufloader_area_update_handler |
| 50 |
rect = nil |
| 51 |
->(pixbufloader, *xywh) do |
| 52 |
new_rect = PartialUpdateRect.new(*xywh) |
| 53 |
atomic do |
| 54 |
if rect |
| 55 |
rect = rect.expand(new_rect) |
| 56 |
else |
| 57 |
rect = new_rect |
| 58 |
Delayer.new do |
| 59 |
atomic do |
| 60 |
progress(pixbufloader.pixbuf, partial: rect) |
| 61 |
rect = nil |
| 62 |
end |
| 63 |
end |
| 64 |
end |
| 65 |
end |
| 66 |
true |
| 67 |
end |
| 68 |
end |
| 69 |
|
| 70 |
def window_settings |
| 71 |
set_title(photo.perma_link.to_s) |
| 72 |
set_role('mikutter_image_preview'.freeze) |
| 73 |
set_type_hint(:dialog) |
| 74 |
set_default_size(*default_size) |
| 75 |
add(Gtk::Box.new(:vertical).pack_start(w_toolbar) |
| 76 |
.pack_start(w_wrap, fill: true, expand: true)) |
| 77 |
end |
| 78 |
|
| 79 |
def redraw(repaint: true) |
| 80 |
return if w_wrap.destroyed? |
| 81 |
gdk_window = w_wrap.window |
| 82 |
return unless gdk_window |
| 83 |
win_rect = PartialUpdateRect.new(*gdk_window.geometry) |
| 84 |
return if win_rect.area == 0 |
| 85 |
gdk_window.create_cairo_context.save do |context| |
| 86 |
if repaint |
| 87 |
context.set_source_color(Cairo::Color::BLACK) |
| 88 |
context.paint |
| 89 |
end |
| 90 |
*point, rate = win_rect.inscribed(@image_surface) |
| 91 |
context.translate(*point) |
| 92 |
context.scale(rate, rate) |
| 93 |
context.set_source(Cairo::SurfacePattern.new(@image_surface)) |
| 94 |
context.paint |
| 95 |
end |
| 96 |
rescue StandardError => exception |
| 97 |
error exception |
| 98 |
end |
| 99 |
|
| 100 |
の場合は全範囲) |
| 101 |
def progress(pixbuf, partial: nil) |
| 102 |
return unless pixbuf |
| 103 |
context = nil |
| 104 |
size_changed = false |
| 105 |
unless (@image_surface.width == pixbuf.width) && (@image_surface.height == pixbuf.height) |
| 106 |
size_changed = true |
| 107 |
@image_surface = Cairo::ImageSurface.new(pixbuf.width, pixbuf.height) |
| 108 |
context = Cairo::Context.new(@image_surface) |
| 109 |
context.save do |
| 110 |
context.set_source_color(Cairo::Color::BLACK) |
| 111 |
context.paint |
| 112 |
end |
| 113 |
end |
| 114 |
context ||= Cairo::Context.new(@image_surface) |
| 115 |
context.save do |
| 116 |
case partial |
| 117 |
in x:, y:, width: w, height: h |
| 118 |
context.set_source_pixbuf(pixbuf) |
| 119 |
context.rectangle(x, y, w, h) |
| 120 |
context.fill |
| 121 |
in nil |
| 122 |
context.set_source_color(Cairo::Color::BLACK) |
| 123 |
context.paint |
| 124 |
context.set_source_pixbuf(pixbuf) |
| 125 |
context.paint |
| 126 |
end |
| 127 |
end |
| 128 |
redraw(repaint: !partial || size_changed) |
| 129 |
end |
| 130 |
|
| 131 |
|
| 132 |
たち |
| 133 |
|
| 134 |
|
| 135 |
def w_wrap |
| 136 |
@w_wrap ||= ::Gtk::DrawingArea.new.tap do |w| |
| 137 |
w.ssc(:size_allocate, &gen_wrap_size_allocate) |
| 138 |
w.ssc(:draw, &gen_wrap_expose_event) |
| 139 |
end |
| 140 |
end |
| 141 |
|
| 142 |
def w_toolbar |
| 143 |
@w_toolbar ||= ::Gtk::Toolbar.new.tap { |w| w.insert(w_browser, 0) } |
| 144 |
end |
| 145 |
|
| 146 |
def w_browser |
| 147 |
@w_browser ||= ::Gtk::ToolButton.new( |
| 148 |
icon_widget: Gtk::Image.new(pixbuf: Skin[:forward].pixbuf(width: 24, height: 24)) |
| 149 |
).tap do |w| |
| 150 |
w.ssc(:clicked, &gen_browser_clicked) |
| 151 |
end |
| 152 |
end |
| 153 |
|
| 154 |
|
| 155 |
ントハンドラ |
| 156 |
|
| 157 |
|
| 158 |
def gen_browser_clicked |
| 159 |
proc do |
| 160 |
@next_opener.forward |
| 161 |
false |
| 162 |
end |
| 163 |
end |
| 164 |
|
| 165 |
def gen_wrap_expose_event |
| 166 |
proc do |_widget| |
| 167 |
redraw(repaint: true) |
| 168 |
true |
| 169 |
end |
| 170 |
end |
| 171 |
|
| 172 |
def gen_wrap_size_allocate |
| 173 |
last_size = nil |
| 174 |
proc do |widget| |
| 175 |
if widget.window && last_size != widget.window.geometry[2, 2] |
| 176 |
last_size = widget.window.geometry[2, 2] |
| 177 |
redraw(repaint: true) |
| 178 |
end |
| 179 |
false |
| 180 |
end |
| 181 |
end |
| 182 |
|
| 183 |
|
| 184 |
の他 |
| 185 |
|
| 186 |
|
| 187 |
def default_size |
| 188 |
rect = case UserConfig[:openimg_window_size_reference] |
| 189 |
when :full |
| 190 |
screen |
| 191 |
when :mainwindow |
| 192 |
mainwindow = Plugin[:gtk3].widgetof(Plugin::GUI::Window.instance(:default)) |
| 193 |
screen.get_monitor_geometry(screen.get_monitor(mainwindow.window)) |
| 194 |
when :manual |
| 195 |
monitor = UserConfig[:openimg_window_size_reference_manual_num] |
| 196 |
max_monitor = screen.n_monitors |
| 197 |
if monitor > max_monitor |
| 198 |
monitor = 0 |
| 199 |
end |
| 200 |
screen.get_monitor_geometry(monitor) |
| 201 |
end |
| 202 |
|
| 203 |
@size || [rect.width * UserConfig[:openimg_window_size_width_percent].fdiv(100), |
| 204 |
rect.height * UserConfig[:openimg_window_size_height_percent].fdiv(100)] |
| 205 |
end |
| 206 |
|
| 207 |
def loading_surface |
| 208 |
Cairo::ImageSurface.from_png(Skin.get_path('loading.png')) |
| 209 |
end |
| 210 |
|
| 211 |
def error_surface |
| 212 |
Cairo::ImageSurface.from_png(Skin.get_path('notfound.png')) |
| 213 |
end |
| 214 |
end |
| 215 |
|
| 216 |
PartialUpdateRect = Data.define(:x, :y, :width, :height) do |
| 217 |
def area |
| 218 |
width * height |
| 219 |
end |
| 220 |
|
| 221 |
def expand(rect) |
| 222 |
PartialUpdateRect.new( |
| 223 |
x: [x, rect.x].min, |
| 224 |
y: [y, rect.y].min, |
| 225 |
width: [width, rect.width].max, |
| 226 |
height: [height, rect.height].max |
| 227 |
) |
| 228 |
end |
| 229 |
|
| 230 |
た場合の位置 _x_, _y_, 拡大率 _rate_ を返す |
| 231 |
入れる矩形 |
| 232 |
|
| 233 |
def inscribed(other) |
| 234 |
if (width * other.height) > (height * other.width) |
| 235 |
rate = height.fdiv(other.height) |
| 236 |
[(width - other.width * rate) / 2, 0, rate] |
| 237 |
else |
| 238 |
rate = width.fdiv(other.width) |
| 239 |
[0, (height - other.height * rate) / 2, rate] |
| 240 |
end |
| 241 |
end |
| 242 |
end |
| 243 |
end |