xremap/mrblib/xkremap/config_dsl.rb

55 lines
1.2 KiB
Ruby
Raw Normal View History

2016-11-29 17:53:53 +00:00
module Xkremap
class ConfigDSL
# @param [Xkremap::Config] config
2016-11-29 19:07:22 +00:00
def initialize(config, win = Config::AnyWindow)
2016-11-29 17:53:53 +00:00
@config = config
2016-11-29 19:07:22 +00:00
@window = win
2016-11-29 17:53:53 +00:00
end
def remap(from_str, options = {})
2016-11-29 21:24:05 +00:00
# Array() doesn't work for Config::Execute somehow.
to_strs = options.fetch(:to)
to_strs = [to_strs] unless to_strs.is_a?(Array)
2016-11-29 19:07:22 +00:00
@config.remaps_by_window[@window] << Config::Remap.new(
2016-11-29 17:53:53 +00:00
compile_exp(from_str),
to_strs.map { |str| compile_exp(str) }
)
end
2016-11-29 19:07:22 +00:00
def window(options = {}, &block)
win = Config::Window.new(options[:class_only], options[:class_not])
ConfigDSL.new(@config, win).instance_exec(&block)
end
2016-11-29 21:24:05 +00:00
def execute(str)
Config::Execute.new(str, :execute)
end
def press(str)
key = compile_exp(str)
key.action = :press
key
end
def release(str)
key = compile_exp(str)
key.action = :release
key
end
2016-11-29 17:53:53 +00:00
private
def compile_exp(exp)
2016-11-29 21:24:05 +00:00
case exp
when Config::Key, Config::Execute
exp
2016-11-29 21:24:05 +00:00
when String
KeyExpression.compile(exp)
else
raise "unexpected expression: #{exp.inspect}"
end
2016-11-29 17:53:53 +00:00
end
end
end