首页 > 解决方案 > 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

标签: autocompletegtkgtk3

解决方案


推荐阅读