| 1 |
|
| 2 |
require 'userconfig' |
| 3 |
|
| 4 |
require 'gtk3' |
| 5 |
require 'monitor' |
| 6 |
require_if_exist 'Win32API' |
| 7 |
|
| 8 |
class GLib::Instantiatable |
| 9 |
削除されたGLib Objectでない場合のみブロックを実行する点が異なる。 |
| 10 |
あれば、ブロックを実行せずにシグナルをselfから切り離す。 |
| 11 |
ultするが、このメソッドを使えば正常にクラッシュする。 |
| 12 |
|
| 13 |
イベントとブロックの連想配列 |
| 14 |
ントの名前 |
| 15 |
、値に呼び出すブロックを持つHash |
| 16 |
ちどれか一つでも削除されていたらブロックを実行しない |
| 17 |
、イベントが発生したらこのブロックが呼ばれる |
| 18 |
|
| 19 |
同じ |
| 20 |
def safety_signal_connect(signal, *related, &proc) |
| 21 |
case signal |
| 22 |
when Hash |
| 23 |
signal.each{ |name, callback| |
| 24 |
safety_signal_connect(name, *related, &callback) } |
| 25 |
when String, Symbol |
| 26 |
related.each{ |gobj| |
| 27 |
raise ArgumentError.new(gobj.to_s) unless gobj.is_a?(GLib::Object) } |
| 28 |
if related |
| 29 |
sid = signal_connect(signal) do |*args| |
| 30 |
begin |
| 31 |
if not(destroyed?) |
| 32 |
if (related.any?(&:destroyed?)) |
| 33 |
signal_handler_disconnect(sid) |
| 34 |
else |
| 35 |
proc.call(*args) |
| 36 |
end |
| 37 |
end |
| 38 |
rescue Exception => err |
| 39 |
Gtk.exception = err |
| 40 |
end |
| 41 |
end |
| 42 |
else |
| 43 |
signal_connect(signal) do |*args| |
| 44 |
if !destroyed? |
| 45 |
proc.call(*args) |
| 46 |
end |
| 47 |
rescue Exception => err |
| 48 |
Gtk.exception = err |
| 49 |
end |
| 50 |
end |
| 51 |
else |
| 52 |
raise ArgumentError, "First argument should Hash, String, or Symbol." end end |
| 53 |
alias ssc safety_signal_connect |
| 54 |
|
| 55 |
が発生した最初の一度だけ呼ぶ |
| 56 |
def safety_signal_connect_atonce(signal, *related, &proc) |
| 57 |
called = false |
| 58 |
sid = ssc(signal, *related) { |*args| |
| 59 |
unless called |
| 60 |
called = true |
| 61 |
signal_handler_disconnect(sid) |
| 62 |
proc.call(*args) end } |
| 63 |
sid end |
| 64 |
alias ssc_atonce safety_signal_connect_atonce |
| 65 |
|
| 66 |
private |
| 67 |
def __track(&proc) |
| 68 |
type_strict proc => :call |
| 69 |
trace = caller(3) |
| 70 |
lambda{ |*args| |
| 71 |
begin |
| 72 |
proc.call(*args) |
| 73 |
rescue Exception => e |
| 74 |
now = caller.size + 1 タックが1つ多い |
| 75 |
|
| 76 |
Gtk.exception = e |
| 77 |
into_debug_mode(e, proc.binding) |
| 78 |
raise e end |
| 79 |
} |
| 80 |
end |
| 81 |
|
| 82 |
end |
| 83 |
|
| 84 |
class Gdk::Rectangle |
| 85 |
def to_s |
| 86 |
"#<Gtk::Rectangle x=#{x} y=#{y} width=#{width} height=#{height}>" |
| 87 |
end |
| 88 |
end |
| 89 |
|
| 90 |
module Gtk |
| 91 |
NO_ACTION = '(割り当てなし)'.freeze |
| 92 |
PRESS_WITH_CONTROL = 'Control + '.freeze |
| 93 |
PRESS_WITH_SHIFT = 'Shift + '.freeze |
| 94 |
PRESS_WITH_ALT = 'Alt + '.freeze |
| 95 |
PRESS_WITH_SUPER = 'Super + '.freeze |
| 96 |
PRESS_WITH_HYPER = 'Hyper + '.freeze |
| 97 |
KonamiCache = File.expand_path(File.join(Environment::CACHE, 'core', 'konami.png')) |
| 98 |
|
| 99 |
class << self |
| 100 |
attr_accessor :exception, :konami |
| 101 |
attr_reader :konami_image |
| 102 |
end |
| 103 |
|
| 104 |
self.konami = false |
| 105 |
|
| 106 |
def self.konami_load |
| 107 |
return if @konami |
| 108 |
if FileTest.exist? KonamiCache |
| 109 |
@konami_image = GdkPixbuf::Pixbuf.new(file: KonamiCache, width: 41, height: 52) |
| 110 |
@konami = true |
| 111 |
else |
| 112 |
Thread.new do |
| 113 |
tmpfile = File.join(Environment::TMPDIR, '600eur') |
| 114 |
URI('https://mikutter.hachune.net/img/konami.png').open('rb') do |konami| |
| 115 |
File.open(tmpfile, 'wb'){ |cache| IO.copy_stream konami, cache } |
| 116 |
end |
| 117 |
FileUtils.mkdir_p(File.dirname(KonamiCache)) |
| 118 |
FileUtils.mv(tmpfile, KonamiCache) |
| 119 |
@konami_image = GdkPixbuf::Pixbuf.new(file: KonamiCache, width: 41, height: 52) |
| 120 |
@konami = true |
| 121 |
rescue => exception |
| 122 |
error exception |
| 123 |
end |
| 124 |
end |
| 125 |
end |
| 126 |
|
| 127 |
def self.keyname(key) |
| 128 |
type_strict key => Array |
| 129 |
return NO_ACTION if key.empty? or key[0] == 0 or not key.all? |
| 130 |
|
| 131 |
r = "" |
| 132 |
r << PRESS_WITH_CONTROL if (key[1] & :control_mask) != 0 |
| 133 |
r << PRESS_WITH_SHIFT if (key[1] & :shift_mask) != 0 |
| 134 |
r << PRESS_WITH_ALT if (key[1] & :mod1_mask) != 0 |
| 135 |
r << PRESS_WITH_SUPER if (key[1] & :super_mask) != 0 |
| 136 |
r << PRESS_WITH_HYPER if (key[1] & :hyper_mask) != 0 |
| 137 |
return r + Gdk::Keyval.to_name(key[0]) end |
| 138 |
|
| 139 |
def self.buttonname(key) |
| 140 |
type_strict key => Array |
| 141 |
type, button, state = key |
| 142 |
return NO_ACTION if key.empty? or type == 0 or not key.all? |
| 143 |
r = "" |
| 144 |
r << PRESS_WITH_CONTROL if (state & Gdk::ModifierType::CONTROL_MASK) != 0 |
| 145 |
r << PRESS_WITH_SHIFT if (state & Gdk::ModifierType::SHIFT_MASK) != 0 |
| 146 |
r << PRESS_WITH_ALT if (state & Gdk::ModifierType::MOD1_MASK) != 0 |
| 147 |
r << PRESS_WITH_SUPER if (state & Gdk::ModifierType::SUPER_MASK) != 0 |
| 148 |
r << PRESS_WITH_HYPER if (state & Gdk::ModifierType::HYPER_MASK) != 0 |
| 149 |
r << "Button #{button} " |
| 150 |
case type |
| 151 |
when Gdk::EventType::BUTTON_PRESS |
| 152 |
r << 'Click'.freeze |
| 153 |
when Gdk::EventType::BUTTON2_PRESS |
| 154 |
r << 'Double Click'.freeze |
| 155 |
when Gdk::EventType::BUTTON3_PRESS |
| 156 |
r << 'Triple Click'.freeze |
| 157 |
else |
| 158 |
return NO_ACTION end |
| 159 |
return r end |
| 160 |
|
| 161 |
end |
| 162 |
|
| 163 |
|
| 164 |
排他制御 |
| 165 |
ロックしようとするとエラーを発生させる。 |
| 166 |
ドではない疑いがある箇所は必ずGtk::Lockを使う。 |
| 167 |
|
| 168 |
class Gtk::Lock |
| 169 |
、実行後に _unlock_ する。 |
| 170 |
の実行結果を返す。 |
| 171 |
def self.synchronize |
| 172 |
begin |
| 173 |
lock |
| 174 |
yield |
| 175 |
ensure |
| 176 |
unlock |
| 177 |
end |
| 178 |
end |
| 179 |
|
| 180 |
でこの関数を呼ぶと例外を発生させる。 |
| 181 |
def self.lock |
| 182 |
raise 'Gtk lock can mainthread only' if Thread.main != Thread.current |
| 183 |
end |
| 184 |
|
| 185 |
def self.unlock |
| 186 |
end |
| 187 |
end |
| 188 |
|
| 189 |
|
| 190 |
class Gtk::Widget |
| 191 |
extend Gem::Deprecate |
| 192 |
|
| 193 |
トを上寄せで配置する |
| 194 |
def top |
| 195 |
Gtk::Alignment.new(0.0, 0, 0, 0).add(self) |
| 196 |
end |
| 197 |
deprecate :top, "set_valign(:start)", 2018, 9 |
| 198 |
|
| 199 |
を横方向に中央寄せで配置する |
| 200 |
def center |
| 201 |
Gtk::Alignment.new(0.5, 0, 0, 0).add(self) |
| 202 |
end |
| 203 |
deprecate :center, "set_halign(:center)", 2018, 9 |
| 204 |
|
| 205 |
トを左寄せで配置する |
| 206 |
def left |
| 207 |
Gtk::Alignment.new(0, 0, 0, 0).add(self) |
| 208 |
end |
| 209 |
deprecate :left, "set_halign(:start)", 2018, 9 |
| 210 |
|
| 211 |
トを右寄せで配置する |
| 212 |
def right |
| 213 |
Gtk::Alignment.new(1.0, 0, 0, 0).add(self) |
| 214 |
end |
| 215 |
deprecate :right, "set_halign(:end)", 2018, 9 |
| 216 |
|
| 217 |
ールチップ _text_ をつける |
| 218 |
def tooltip(text) |
| 219 |
self.tooltip_text = text |
| 220 |
self end |
| 221 |
deprecate :tooltip, "set_tooltip_text(text)", 2018, 9 |
| 222 |
|
| 223 |
end |
| 224 |
|
| 225 |
class Gtk::Box |
| 226 |
extend Gem::Deprecate |
| 227 |
|
| 228 |
れたウィジェットは無理に親の幅に合わせられることがない。 |
| 229 |
等価。 |
| 230 |
def closeup(widget) |
| 231 |
pack_start(widget, expand: false) |
| 232 |
end |
| 233 |
deprecate :closeup, "pack_start(widget, expand: false)", 2018, 9 |
| 234 |
end |
| 235 |
|
| 236 |
class Gtk::TextBuffer < GLib::Object |
| 237 |
_size_ 文字後ろを表すイテレータの2要素からなる配列を返す。 |
| 238 |
def get_range(idx, size) |
| 239 |
[self.get_iter_at(offset: idx), self.get_iter_at(offset: idx + size)] |
| 240 |
end |
| 241 |
end |
| 242 |
|
| 243 |
class Gtk::Clipboard |
| 244 |
リップボードにコピーする |
| 245 |
def self.copy(t) |
| 246 |
Gtk::Clipboard.get(Gdk::Atom.intern('CLIPBOARD', true)).text = t |
| 247 |
end |
| 248 |
|
| 249 |
ードから文字列を取得する |
| 250 |
def self.paste |
| 251 |
Gtk::Clipboard.get(Gdk::Atom.intern('CLIPBOARD', true)).wait_for_text |
| 252 |
end |
| 253 |
end |
| 254 |
|
| 255 |
class Gtk::Dialog |
| 256 |
ダイアログを表示する。 |
| 257 |
def self.alert(message) |
| 258 |
Gtk::Lock.synchronize{ |
| 259 |
dialog = Gtk::MessageDialog.new(nil, |
| 260 |
Gtk::Dialog::DESTROY_WITH_PARENT, |
| 261 |
Gtk::MessageDialog::QUESTION, |
| 262 |
Gtk::MessageDialog::BUTTONS_CLOSE, |
| 263 |
message) |
| 264 |
dialog.run |
| 265 |
dialog.destroy |
| 266 |
} |
| 267 |
end |
| 268 |
|
| 269 |
質問を表示する。 |
| 270 |
、それ以外が押されたらfalseを返す |
| 271 |
def self.confirm(message) |
| 272 |
Gtk::Lock.synchronize{ |
| 273 |
dialog = Gtk::MessageDialog.new(parent: nil, |
| 274 |
flags: Gtk::DialogFlags::DESTROY_WITH_PARENT, |
| 275 |
type: Gtk::MessageType::QUESTION, |
| 276 |
buttons: Gtk::ButtonsType::YES_NO, |
| 277 |
message: message) |
| 278 |
res = dialog.run |
| 279 |
dialog.destroy |
| 280 |
res == Gtk::ResponseType::YES |
| 281 |
} |
| 282 |
end |
| 283 |
end |
| 284 |
|
| 285 |
class Gtk::Notebook |
| 286 |
ェットが何番目のタブかを返す |
| 287 |
|
| 288 |
ウィジェット |
| 289 |
|
| 290 |
見つからない場合nil) |
| 291 |
def get_tab_pos_by_tab(label) |
| 292 |
n_pages.times do |page_num| |
| 293 |
if(get_tab_label(get_nth_page(page_num)) == label) |
| 294 |
return page_num |
| 295 |
end |
| 296 |
end |
| 297 |
nil |
| 298 |
end |
| 299 |
|
| 300 |
査し、 _&block_ を呼び出す。 |
| 301 |
い場合、Enumeratorを返す。 |
| 302 |
def each_pages(&block) |
| 303 |
if block |
| 304 |
n_pages.times.map(&method(:get_nth_page)).each(&block) |
| 305 |
else |
| 306 |
to_enum(:each_pages) |
| 307 |
end |
| 308 |
end |
| 309 |
end |
| 310 |
|
| 311 |
class Cairo::Context |
| 312 |
class << self |
| 313 |
def dummy |
| 314 |
@dummy ||= Cairo::Context.new(Cairo::ImageSurface.new(Cairo::Format::ARGB32, 1, 1)) |
| 315 |
end |
| 316 |
end |
| 317 |
end |
| 318 |
|
| 319 |
class Pango::FontDescription |
| 320 |
する時の一辺の大きさを返す |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
(px) |
| 325 |
def forecast_font_size |
| 326 |
Pango::FontDescription.forecast_font_size(self) |
| 327 |
end |
| 328 |
|
| 329 |
@forecast_font_description = Hash.new |
| 330 |
def self.forecast_font_size(fd) |
| 331 |
@forecast_font_description[fd.hash] ||= |
| 332 |
PangoCairo::FontMap |
| 333 |
.default |
| 334 |
.create_context |
| 335 |
.then(&Pango::Layout.method(:new)) |
| 336 |
.then do |layout| |
| 337 |
layout.font_description = fd |
| 338 |
layout.text = '.' |
| 339 |
layout.pixel_size[1] |
| 340 |
end |
| 341 |
end |
| 342 |
end |
| 343 |
|
| 344 |
class Gtk::ListStore |
| 345 |
def model |
| 346 |
self end |
| 347 |
end |
| 348 |
|
| 349 |
module Gtk |
| 350 |
ているブラウザで開く |
| 351 |
class << self |
| 352 |
def openurl(url) |
| 353 |
Plugin.call(:open, url) |
| 354 |
end |
| 355 |
end |
| 356 |
end |
| 357 |
|
| 358 |
module Gdk |
| 359 |
class << self |
| 360 |
def scale(val) |
| 361 |
case UserConfig[:ui_scale] |
| 362 |
when :auto |
| 363 |
@resolution ||= Gdk::Visual.system.screen.resolution |
| 364 |
@resolution < 0 and @resolution = 96 |
| 365 |
val * @resolution / 96 |
| 366 |
else |
| 367 |
val * UserConfig[:ui_scale] |
| 368 |
end.to_i |
| 369 |
end |
| 370 |
end |
| 371 |
end |
| 372 |
|
| 373 |
class Gtk::Style |
| 374 |
CSS_PSEUDO_CLASS_BY_STATE_TYPE = { |
| 375 |
Gtk::StateType::NORMAL => '', |
| 376 |
Gtk::StateType::ACTIVE => ':active', |
| 377 |
Gtk::StateType::SELECTED => ':focus', |
| 378 |
Gtk::StateType::PRELIGHT => ':hover', |
| 379 |
Gtk::StateType::INSENSITIVE => ':disabled' |
| 380 |
} |
| 381 |
|
| 382 |
換のために用意したが使われないことを祈る |
| 383 |
def to_style_provider |
| 384 |
Gtk::CssProvider.new.tap do |provider| |
| 385 |
styles = {} |
| 386 |
CSS_PSEUDO_CLASS_BY_STATE_TYPE.each do |type, pseudo_class| |
| 387 |
color = bg[type] |
| 388 |
if color |
| 389 |
selector = "*#{pseudo_class}" |
| 390 |
styles[selector] ||= '' |
| 391 |
styles[selector] += "background-color: rgb(#{color[0] / 256}, #{color[1] / 256}, #{color[2] / 256});" |
| 392 |
end |
| 393 |
end |
| 394 |
css = styles.map { |selector, style| "#{selector} { #{style} }" }.join(' ') |
| 395 |
provider.load_from_data(css) |
| 396 |
end |
| 397 |
rescue NotImplementedError => e |
| 398 |
正しくアクセスできない可能性がある |
| 399 |
error e |
| 400 |
Gtk::CssProvider.new |
| 401 |
end |
| 402 |
end |
| 403 |
|
| 404 |
unless Kernel.const_defined?(:GdkPixbuf) |
| 405 |
module GdkPixbuf |
| 406 |
Pixbuf = Gdk::Pixbuf |
| 407 |
PixbufLoader = Gdk::PixbufLoader |
| 408 |
PixbufError = Gdk::PixbufError |
| 409 |
end |
| 410 |
end |
| 411 |
|
| 412 |
module MUI |
| 413 |
Skin = ::Skin |
| 414 |
|
| 415 |
module ColorConverter |
| 416 |
refine NilClass do |
| 417 |
def rgba |
| 418 |
Gdk::RGBA.new |
| 419 |
end |
| 420 |
end |
| 421 |
|
| 422 |
refine Array do |
| 423 |
def rgba |
| 424 |
case first |
| 425 |
when Integer |
| 426 |
|
| 427 |
Gdk::RGBA.new(*map { |i| i.to_f / 65535 }) |
| 428 |
when Float |
| 429 |
|
| 430 |
Gdk::RGBA.new(*self) |
| 431 |
else |
| 432 |
raise TypeError, 'receiver values are not compatible' |
| 433 |
end |
| 434 |
end |
| 435 |
end |
| 436 |
|
| 437 |
refine Gdk::RGBA do |
| 438 |
def to_color_array |
| 439 |
to_a[0..2].map { |f| f * 65535 }.map(&:to_i) |
| 440 |
end |
| 441 |
end |
| 442 |
end |
| 443 |
end |