| 1 |
|
| 2 |
|
| 3 |
module Plugin::Shortcutkey |
| 4 |
class ShortcutKeyListView < ::Gtk::CRUD |
| 5 |
|
| 6 |
COLUMN_KEYBIND = 0 |
| 7 |
COLUMN_COMMAND_ICON = 1 |
| 8 |
COLUMN_COMMAND = 2 |
| 9 |
COLUMN_SLUG = 3 |
| 10 |
COLUMN_WORLD_FACE = 4 |
| 11 |
COLUMN_WORLD = 5 |
| 12 |
COLUMN_ID = 6 |
| 13 |
|
| 14 |
attr_accessor :filter_entry |
| 15 |
|
| 16 |
def initialize(plugin) |
| 17 |
type_strict plugin => Plugin |
| 18 |
@plugin = plugin |
| 19 |
super() |
| 20 |
set_model(Gtk::TreeModelFilter.new(model)) |
| 21 |
model.set_visible_func do |model, iter| |
| 22 |
if defined?(@filter_entry) and @filter_entry |
| 23 |
query = @filter_entry.text |
| 24 |
Enumerator.new {|y| |
| 25 |
y << iter[COLUMN_KEYBIND] << iter[COLUMN_COMMAND] << iter[COLUMN_SLUG] |
| 26 |
iter[COLUMN_WORLD].yield_self do |w| |
| 27 |
y << w.uri << w.title if w.is_a? Diva::Model |
| 28 |
end |
| 29 |
}.any? {|x| x.to_s.include?(query) } |
| 30 |
else |
| 31 |
true |
| 32 |
end |
| 33 |
end |
| 34 |
commands = Plugin.filtering(:command, Hash.new).first |
| 35 |
worlds = world_selections(key: ->(k) { k.uri.to_s }) |
| 36 |
shortcutkeys.each do |id, behavior| |
| 37 |
slug = behavior[:slug] |
| 38 |
iter = model.model.append |
| 39 |
iter[COLUMN_ID] = id |
| 40 |
iter[COLUMN_KEYBIND] = behavior[:key] |
| 41 |
iter[COLUMN_COMMAND] = behavior[:name] |
| 42 |
iter[COLUMN_SLUG] = slug |
| 43 |
iter[COLUMN_WORLD] = worlds[behavior[:world]] |
| 44 |
update_iter(iter, force: false) |
| 45 |
commands.dig(slug, :icon)&.yield_self {|icon| |
| 46 |
icon.is_a?(Proc) ? icon.call(nil) : icon |
| 47 |
}&.yield_self {|icon| |
| 48 |
Diva::Model(:photo)[icon] |
| 49 |
}&.yield_self do |icon| |
| 50 |
iter[COLUMN_COMMAND_ICON] = icon.load_pixbuf(width: Gdk.scale(16), height: Gdk.scale(16)) do |pixbuf| |
| 51 |
iter[COLUMN_COMMAND_ICON] = pixbuf if not destroyed? |
| 52 |
end |
| 53 |
end |
| 54 |
end |
| 55 |
end |
| 56 |
|
| 57 |
def column_schemer |
| 58 |
[{ kind: :text, |
| 59 |
widget: :keyconfig, |
| 60 |
type: String, |
| 61 |
label: @plugin._('キーバインド') |
| 62 |
}, |
| 63 |
[{ kind: :pixbuf, |
| 64 |
type: GdkPixbuf::Pixbuf, |
| 65 |
label: @plugin._('機能名') |
| 66 |
}, |
| 67 |
{ kind: :text, |
| 68 |
type: String, |
| 69 |
expand: true |
| 70 |
} |
| 71 |
], |
| 72 |
{ kind: :text, |
| 73 |
widget: :chooseone, |
| 74 |
args: command_dict_slug_to_name, |
| 75 |
type: Symbol |
| 76 |
}, |
| 77 |
{ kind: :text, |
| 78 |
label: @plugin._('アカウント'), |
| 79 |
type: String |
| 80 |
}, |
| 81 |
{ widget: :chooseone, |
| 82 |
args: worlds_dict_slug_to_name, |
| 83 |
type: Object |
| 84 |
}, |
| 85 |
{ type: Integer |
| 86 |
}, |
| 87 |
].freeze |
| 88 |
end |
| 89 |
|
| 90 |
def command_dict_slug_to_name |
| 91 |
@command_dict_slug_to_name ||= |
| 92 |
Plugin.filtering(:command, Hash.new) |
| 93 |
.first |
| 94 |
.values |
| 95 |
.to_h {|x| [x[:slug], x[:name]] } |
| 96 |
end |
| 97 |
|
| 98 |
def worlds_dict_slug_to_name |
| 99 |
Plugin.collect(:worlds).to_h {|x| [x.uri, x.title] } |
| 100 |
end |
| 101 |
|
| 102 |
def shortcutkeys |
| 103 |
UserConfig[:shortcutkey_keybinds] || {}.freeze |
| 104 |
end |
| 105 |
|
| 106 |
def new_serial |
| 107 |
@new_serial ||= (shortcutkeys.keys.max || 0) |
| 108 |
@new_serial += 1 end |
| 109 |
|
| 110 |
def on_created(iter) |
| 111 |
iter[COLUMN_ID] = new_serial |
| 112 |
merge_key_bind(iter) |
| 113 |
end |
| 114 |
|
| 115 |
def on_updated(iter) |
| 116 |
merge_key_bind(iter) |
| 117 |
end |
| 118 |
|
| 119 |
def on_deleted(iter) |
| 120 |
bind = shortcutkeys.dup |
| 121 |
bind.delete(iter[COLUMN_ID].to_i) |
| 122 |
UserConfig[:shortcutkey_keybinds] = bind |
| 123 |
end |
| 124 |
|
| 125 |
def popup_input_window(defaults = []) |
| 126 |
values = defaults.dup |
| 127 |
result = nil |
| 128 |
defaults.freeze |
| 129 |
window = KeyConfigWindow.new(@plugin._("設定 - %{software_name}") % {software_name: Environment::NAME}) |
| 130 |
window.transient_for = toplevel |
| 131 |
window.modal = true |
| 132 |
window.destroy_with_parent = true |
| 133 |
btn_ok = ::Gtk::Button.new(label: @plugin._("OK")) |
| 134 |
btn_cancel = ::Gtk::Button.new(label: @plugin._("キャンセル")) |
| 135 |
box = ::Gtk::ButtonBox.new :horizontal |
| 136 |
box.layout_style = :end |
| 137 |
box.spacing = 6 |
| 138 |
box << btn_cancel << btn_ok |
| 139 |
grid = ::Gtk::Grid.new |
| 140 |
grid.orientation = :vertical |
| 141 |
grid.row_spacing = 12 |
| 142 |
grid.margin = 12 |
| 143 |
grid << (::Gtk::Grid.new.tap do |grid| |
| 144 |
grid.orientation = :vertical |
| 145 |
grid.row_spacing = 12 |
| 146 |
c_box = command_box(values) |
| 147 |
c_box.expand = true |
| 148 |
grid << key_box(values) << world_box(values) << c_box |
| 149 |
end) << box |
| 150 |
|
| 151 |
window << grid |
| 152 |
window.show_all |
| 153 |
|
| 154 |
window.ssc(:destroy) { ::Gtk::main_quit } |
| 155 |
btn_cancel.ssc(:clicked) { window.destroy } |
| 156 |
btn_ok.ssc(:clicked) { |
| 157 |
error = catch(:validate) { |
| 158 |
throw :validate, @plugin._("キーバインドを選択してください") unless (values[COLUMN_KEYBIND] && values[COLUMN_KEYBIND] != "") |
| 159 |
throw :validate, @plugin._("コマンドを選択してください") unless command_dict_slug_to_name.has_key?(values[COLUMN_SLUG]&.to_sym) |
| 160 |
result = values |
| 161 |
window.destroy |
| 162 |
nil |
| 163 |
} |
| 164 |
if error |
| 165 |
dialog = ::Gtk::MessageDialog.new(parent: window, |
| 166 |
flags: ::Gtk::DialogFlags::DESTROY_WITH_PARENT, |
| 167 |
type: ::Gtk::MessageType::WARNING, |
| 168 |
buttons: ::Gtk::ButtonsType::OK, |
| 169 |
message: error) |
| 170 |
dialog.run |
| 171 |
dialog.destroy end } |
| 172 |
::Gtk::main |
| 173 |
result end |
| 174 |
|
| 175 |
private |
| 176 |
|
| 177 |
def update_iter(iter, force: false) |
| 178 |
if force |
| 179 |
iter[COLUMN_COMMAND] = name_of(iter) |
| 180 |
else |
| 181 |
iter[COLUMN_COMMAND] ||= name_of(iter) |
| 182 |
end |
| 183 |
iter[COLUMN_WORLD_FACE] = |
| 184 |
if iter[COLUMN_WORLD] |
| 185 |
iter[COLUMN_WORLD].title |
| 186 |
else |
| 187 |
@plugin._('カレントアカウント') |
| 188 |
end |
| 189 |
end |
| 190 |
|
| 191 |
def merge_key_bind(iter) |
| 192 |
update_iter(iter, force: true) |
| 193 |
UserConfig[:shortcutkey_keybinds] = shortcutkeys.merge( |
| 194 |
iter[COLUMN_ID].to_i => bind_of(iter) |
| 195 |
) |
| 196 |
end |
| 197 |
|
| 198 |
def bind_of(iter) |
| 199 |
{ |
| 200 |
key: -iter[COLUMN_KEYBIND].to_s, |
| 201 |
name: -iter[COLUMN_COMMAND].to_s, |
| 202 |
slug: iter[COLUMN_SLUG].to_sym, |
| 203 |
world: |
| 204 |
if iter[COLUMN_WORLD] |
| 205 |
iter[COLUMN_WORLD].uri&.to_s |
| 206 |
end |
| 207 |
} |
| 208 |
end |
| 209 |
|
| 210 |
def name_of(iter) |
| 211 |
name = Plugin.filtering(:command, Hash.new).first[iter[COLUMN_SLUG].to_sym][:name] |
| 212 |
name = name.call(nil) if name.is_a? Proc |
| 213 |
name |
| 214 |
end |
| 215 |
|
| 216 |
def key_box(results) |
| 217 |
container = ::Gtk::Box.new(:horizontal, 0) |
| 218 |
button = ::Gtk::KeyConfig.new(@plugin._('キーバインド'), results[COLUMN_KEYBIND]) |
| 219 |
button.width_request = HYDE |
| 220 |
button.change_hook = lambda { |new| results[COLUMN_KEYBIND] = new } |
| 221 |
container.pack_start(::Gtk::Label.new(@plugin._('キーバインド')), expand: false, fill: true, padding: 0) |
| 222 |
container.pack_start(Gtk::Alignment.new(1.0, 0.5, 0, 0).add(button), expand: true, fill: true, padding: 0) end |
| 223 |
|
| 224 |
def world_box(results) |
| 225 |
faces = world_selections(value: :title) |
| 226 |
Mtk.chooseone( |
| 227 |
->(new) { |
| 228 |
case new |
| 229 |
when :current |
| 230 |
results[COLUMN_WORLD_FACE] = @plugin._('カレントアカウント') |
| 231 |
results[COLUMN_WORLD] = nil |
| 232 |
when nil |
| 233 |
results[COLUMN_WORLD] |
| 234 |
else |
| 235 |
results[COLUMN_WORLD_FACE] = faces[new] |
| 236 |
results[COLUMN_WORLD] = new |
| 237 |
end |
| 238 |
}, |
| 239 |
@plugin._('アカウント'), |
| 240 |
faces |
| 241 |
) |
| 242 |
end |
| 243 |
|
| 244 |
def command_box(results) |
| 245 |
treeview = CommandList.new(@plugin, results) |
| 246 |
|
| 247 |
sw = ::Gtk::ScrolledWindow.new |
| 248 |
sw.expand = true |
| 249 |
sw.set_policy :never, :automatic |
| 250 |
sw << treeview |
| 251 |
|
| 252 |
filter_entry = treeview.filter_entry = Gtk::Entry.new |
| 253 |
filter_entry.primary_icon_pixbuf = Skin[:search].pixbuf(width: 24, height: 24) |
| 254 |
filter_entry.ssc(:changed) { |
| 255 |
treeview.model.refilter |
| 256 |
false } |
| 257 |
|
| 258 |
grid = ::Gtk::Grid.new |
| 259 |
grid.orientation = :vertical |
| 260 |
grid.row_spacing = 12 |
| 261 |
grid << filter_entry << sw |
| 262 |
end |
| 263 |
|
| 264 |
def world_selections(key: :itself, value: :itself) |
| 265 |
{ |
| 266 |
current: @plugin._('カレントアカウント'), |
| 267 |
**Plugin.collect(:worlds).to_h do |w| |
| 268 |
[key.to_proc.call(w), value.to_proc.call(w)] |
| 269 |
end |
| 270 |
} |
| 271 |
end |
| 272 |
|
| 273 |
class KeyConfigWindow < ::Gtk::Window |
| 274 |
def initialize(*args) |
| 275 |
super |
| 276 |
set_size_request(640, 480) |
| 277 |
self.window_position = ::Gtk::WindowPosition::CENTER |
| 278 |
end |
| 279 |
end |
| 280 |
|
| 281 |
class CommandList < ::Gtk::TreeView |
| 282 |
include Gtk::TreeViewPrettyScroll |
| 283 |
|
| 284 |
COL_ICON = 0 |
| 285 |
COL_NAME = 1 |
| 286 |
COL_SLUG = 2 |
| 287 |
|
| 288 |
attr_accessor :filter_entry |
| 289 |
|
| 290 |
def initialize(plugin, results) |
| 291 |
type_strict plugin => Plugin |
| 292 |
@plugin = plugin |
| 293 |
super(::Gtk::TreeModelFilter.new(::Gtk::TreeStore.new(::GdkPixbuf::Pixbuf, String, Symbol))) |
| 294 |
model.set_visible_func { |model, iter| |
| 295 |
if defined?(@filter_entry) and @filter_entry |
| 296 |
iter_match(iter, @filter_entry.text) |
| 297 |
else |
| 298 |
true |
| 299 |
end |
| 300 |
} |
| 301 |
append_column ::Gtk::TreeViewColumn.new("", ::Gtk::CellRendererPixbuf.new, pixbuf: COL_ICON) |
| 302 |
append_column ::Gtk::TreeViewColumn.new(@plugin._("コマンド名"), ::Gtk::CellRendererText.new, text: COL_NAME) |
| 303 |
append_column ::Gtk::TreeViewColumn.new(@plugin._("スラッグ"), ::Gtk::CellRendererText.new, text: COL_SLUG) |
| 304 |
parents = Hash.new { |h, k| |
| 305 |
h[k] = iter = model.model.append(nil) |
| 306 |
iter[COL_NAME] = k.to_s |
| 307 |
iter |
| 308 |
} |
| 309 |
Plugin.filtering(:command, Hash.new).first.map { |slug, command| |
| 310 |
iter = model.model.append(parents[command[:role]]) |
| 311 |
icon = icon_model(command[:icon]) |
| 312 |
if icon |
| 313 |
iter[COL_ICON] = icon.load_pixbuf(width: Gdk.scale(16), height: Gdk.scale(16)) do |pixbuf| |
| 314 |
iter[COL_ICON] = pixbuf if not destroyed? |
| 315 |
end |
| 316 |
end |
| 317 |
name = command[:name] |
| 318 |
name = name.call(nil) if name.is_a? Proc |
| 319 |
iter[COL_NAME] = name |
| 320 |
iter[COL_SLUG] = slug |
| 321 |
if results[Plugin::Shortcutkey::ShortcutKeyListView::COLUMN_SLUG].to_s == slug.to_s |
| 322 |
expand_row(iter.parent.path, true) |
| 323 |
selection.select_iter(iter) |
| 324 |
end |
| 325 |
} |
| 326 |
ssc(:cursor_changed) do |
| 327 |
iter = selection.selected |
| 328 |
if iter |
| 329 |
results[Plugin::Shortcutkey::ShortcutKeyListView::COLUMN_COMMAND] = iter[COL_NAME] |
| 330 |
results[Plugin::Shortcutkey::ShortcutKeyListView::COLUMN_SLUG] = iter[COL_SLUG] |
| 331 |
end |
| 332 |
false |
| 333 |
end |
| 334 |
selected = selection.selected |
| 335 |
if selected |
| 336 |
scroll_to_cell(selected.path, nil, false, 0.5, 0) |
| 337 |
end |
| 338 |
end |
| 339 |
|
| 340 |
private |
| 341 |
|
| 342 |
def icon_model(icon) |
| 343 |
case icon |
| 344 |
when Proc |
| 345 |
icon_model(icon.call(nil)) |
| 346 |
when Diva::Model |
| 347 |
icon |
| 348 |
when String, URI, Addressable::URI, Diva::URI |
| 349 |
Plugin.collect(:photo_filter, icon, Pluggaloid::COLLECT).first |
| 350 |
end |
| 351 |
end |
| 352 |
|
| 353 |
def iter_match(iter, text) |
| 354 |
if [COL_NAME, COL_SLUG].any? { |column| iter[column].to_s.include?(text) } |
| 355 |
true |
| 356 |
elsif iter.has_child? |
| 357 |
iter.n_children.times.any? { |i| iter_match(iter.nth_child(i), text) } |
| 358 |
end |
| 359 |
end |
| 360 |
end |
| 361 |
end |
| 362 |
end |