autocomplete - GTK EntryCompletion 通过插入而不是替换
问题描述
我已经在条目中构建了一个带有自动完成功能的 GTK 应用程序,但我希望选择的“完成”字符串替换光标触摸的唯一单词,而它当前替换条目中的所有文本。
我可以set_match_func
在 EntryCompletion 上使用仅基于与光标相邻的单词提供匹配,但我看不到如何覆盖文本插入行为。我有办法做到这一点吗?
我正在使用 gtk3 在 Ruby 中工作。(我链接了 gtk2 的文档,因为在我的一生中,我无法在 Ruby 中找到完整的 gtk3 文档。)
编辑这是我的实现(在 Ruby 中),它缺少所需的“插入”行为:
module MyAutocomplete
# Add autocomplete to a Gtk::Entry object
def self.add entry, &block
model = Gtk::ListStore.new String
model.append.set_value 0, 'sd'
model.append.set_value 0, 'foo'
model.append.set_value 0, 'six'
completion = Gtk::EntryCompletion.new
completion.set_minimum_key_length 0
completion.set_text_column 0
completion.set_inline_completion true
completion.set_model model
completion.set_match_func do |*args|
self.match_func *args
end
yield(model, completion) if block_given?
entry.set_completion completion
end
def self.match_func(entry_completion, entry_value, list_obj)
len = 0 # Counts characters into the entry text
cursor = entry_completion.entry.position
entry_text = entry_completion.entry.text
entry_tokens = entry_text.scan(/[\w+@]+|[^\w@]+/)
current_token = entry_tokens.find { |tok|
(len += tok.length) >= cursor && tok =~ /\w/
}
obj_text = list_obj.get_value(0)
return current_token && obj_text.start_with?(current_token)
end
end
解决方案
推荐阅读
- django-rest-framework - 使用相同查找应用多个值时如何执行“或”?
- select - 过滤依赖于另一个值的值
- c# - 向 webservice 传递参数并接收值
- highcharts - 可以像这样设置 HIGHCHARTS 样式吗?
- go - 为什么比赛检测器在这里报告比赛条件?
- azure - 在 Azure 上发布特定的 appsettings.json 文件
- reactjs - React 似乎在修改变量名,然后抱怨它们不存在
- java - 我可以知道后台用户是否忘记保存他的更改吗?
- algorithm - 以这种方式在 3 个字符串中的最长公共子序列 LCS(LCS(string ,string),string)
- mysql - 从对象创建动态 Mysql 插入