mirror of
https://github.com/TimeCrafters/timecrafters_configuration_tool_desktop.git
synced 2025-12-16 05:42:35 +00:00
Initial implementation of search, updated PacketHandler to behave more more like android app
This commit is contained in:
@@ -7,14 +7,311 @@ module TAC
|
||||
menu_bar.clear do
|
||||
search = edit_line "", width: 0.9, height: 1.0
|
||||
button get_image("#{TAC::ROOT_PATH}/media/icons/zoom.png"), image_height: 1.0 do
|
||||
# do search
|
||||
body.clear do
|
||||
label "Search results for: #{search.value.strip}"
|
||||
label "TODO: Search Results."
|
||||
unless search.value.strip.empty?
|
||||
search_results = search_config(search.value.downcase.strip)
|
||||
|
||||
body.clear do
|
||||
flow(width: 1.0, height: 1.0) do
|
||||
stack(width: 0.495, height: 1.0) do
|
||||
shared_index = 0
|
||||
if search_results.results.size.zero?
|
||||
subtitle "No results for: \"#{search.value.strip}\""
|
||||
else
|
||||
subtitle "Search results for: \"#{search.value.strip}\""
|
||||
end
|
||||
|
||||
if search_results.groups.size.positive?
|
||||
title "Groups"
|
||||
|
||||
search_results.groups.each do |result|
|
||||
stack(width: 1.0, **THEME_ITEM_CONTAINER_PADDING) do
|
||||
background shared_index.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR
|
||||
button result.highlight(result.group.name), width: 1.0
|
||||
end
|
||||
|
||||
shared_index += 1
|
||||
end
|
||||
end
|
||||
|
||||
if search_results.actions.size.positive?
|
||||
title "Actions"
|
||||
|
||||
search_results.actions.each do |result|
|
||||
stack(width: 1.0, **THEME_ITEM_CONTAINER_PADDING) do
|
||||
background shared_index.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR
|
||||
button result.highlight(result.action.name), width: 1.0
|
||||
|
||||
if result.from_comment?
|
||||
para result.highlight(result.action.comment), width: 1.0
|
||||
end
|
||||
end
|
||||
|
||||
shared_index += 1
|
||||
end
|
||||
end
|
||||
|
||||
if search_results.variables.size.positive?
|
||||
title "Variables"
|
||||
|
||||
search_results.variables.each do |result|
|
||||
stack(width: 1.0, **THEME_ITEM_CONTAINER_PADDING) do
|
||||
background shared_index.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR
|
||||
button "#{result.highlight(result.variable.name)} [#{result.highlight(result.variable.value)}]", width: 1.0
|
||||
end
|
||||
|
||||
shared_index += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
stack(width: 0.495, height: 1.0) do
|
||||
if search_results.group_presets.size.positive?
|
||||
title "Group Presets"
|
||||
|
||||
search_results.group_presets.each do |result|
|
||||
stack(width: 1.0, **THEME_ITEM_CONTAINER_PADDING) do
|
||||
background shared_index.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR
|
||||
button result.highlight(result.group.name), width: 1.0
|
||||
end
|
||||
|
||||
shared_index += 1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
if search_results.action_presets.size.positive?
|
||||
title "Action Presets"
|
||||
|
||||
search_results.action_presets.each do |result|
|
||||
stack(width: 1.0, **THEME_ITEM_CONTAINER_PADDING) do
|
||||
background shared_index.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR
|
||||
button result.highlight(result.action.name), width: 1.0
|
||||
|
||||
if result.from_comment?
|
||||
para result.highlight(result.action.comment), width: 1.0
|
||||
end
|
||||
end
|
||||
|
||||
shared_index += 1
|
||||
end
|
||||
end
|
||||
|
||||
if search_results.variables_from_presets.size.positive?
|
||||
title "Variables from Presets"
|
||||
|
||||
search_results.variables_from_presets.each do |result|
|
||||
stack(width: 1.0, **THEME_ITEM_CONTAINER_PADDING) do
|
||||
background shared_index.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR
|
||||
button "#{result.highlight(result.variable.name)} [#{result.highlight(result.variable.value)}]", width: 1.0
|
||||
end
|
||||
|
||||
shared_index += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def search_config(query)
|
||||
search_results = SearchResults.new
|
||||
|
||||
search_groups(query, search_results)
|
||||
search_actions(query, search_results)
|
||||
search_variables(query, search_results)
|
||||
search_presets(query, search_results)
|
||||
|
||||
return search_results
|
||||
end
|
||||
|
||||
def search_groups(query, search_results)
|
||||
window.backend.config.groups.each do |group|
|
||||
if group.name.downcase.include?(query)
|
||||
result = SearchResult.new(group: group, query: query, is_group: true, is_from_name: true)
|
||||
search_results.results << result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def search_actions(query, search_results)
|
||||
window.backend.config.groups.each do |group|
|
||||
group.actions.each do |action|
|
||||
if action.name.downcase.include?(query)
|
||||
result = SearchResult.new(group: group, action: action, query: query, is_action: true, is_from_name: true)
|
||||
search_results.results << result
|
||||
end
|
||||
|
||||
if action.comment.downcase.include?(query)
|
||||
result = SearchResult.new(group: group, action: action, query: query, is_action: true, is_from_comment: true)
|
||||
search_results.results << result
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def search_variables(query, search_results)
|
||||
window.backend.config.groups.each do |group|
|
||||
group.actions.each do |action|
|
||||
action.variables.each do |variable|
|
||||
if variable.name.downcase.include?(query)
|
||||
result = SearchResult.new(group: group, action: action, variable: variable, is_variable: true, query: query, is_from_name: true)
|
||||
search_results.results << result
|
||||
end
|
||||
|
||||
if variable.value.downcase.include?(query)
|
||||
result = SearchResult.new(group: group, action: action, variable: variable, is_variable: true, query: query, is_from_value: true)
|
||||
search_results.results << result
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def search_presets(query, search_results)
|
||||
window.backend.config.presets.groups.each do |group|
|
||||
if group.name.downcase.include?(query)
|
||||
result = SearchResult.new(group: group, query: query, is_group: true, is_from_name: true, is_preset: true)
|
||||
search_results.results << result
|
||||
end
|
||||
|
||||
group.actions.each do |action|
|
||||
if action.name.downcase.include?(query)
|
||||
result = SearchResult.new(group: group, action: action, query: query, is_action: true, is_from_name: true, is_preset: true)
|
||||
search_results.results << result
|
||||
end
|
||||
|
||||
if action.comment.downcase.include?(query)
|
||||
result = SearchResult.new(group: group, action: action, query: query, is_action: true, is_from_comment: true, is_preset: true)
|
||||
search_results.results << result
|
||||
end
|
||||
|
||||
action.variables.each do |variable|
|
||||
if variable.name.downcase.include?(query)
|
||||
result = SearchResult.new(group: group, action: action, variable: variable, is_variable: true, query: query, is_from_name: true, is_preset: true)
|
||||
search_results.results << result
|
||||
end
|
||||
|
||||
if variable.value.downcase.include?(query)
|
||||
result = SearchResult.new(group: group, action: action, variable: variable, is_variable: true, query: query, is_from_value: true, is_preset: true)
|
||||
search_results.results << result
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
window.backend.config.presets.actions.each do |action|
|
||||
if action.name.downcase.include?(query)
|
||||
result = SearchResult.new(group: nil, action: action, query: query, is_action: true, is_from_name: true, is_preset: true)
|
||||
search_results.results << result
|
||||
end
|
||||
|
||||
if action.comment.downcase.include?(query)
|
||||
result = SearchResult.new(group: nil, action: action, query: query, is_action: true, is_from_comment: true, is_preset: true)
|
||||
search_results.results << result
|
||||
end
|
||||
|
||||
action.variables.each do |variable|
|
||||
if variable.name.downcase.include?(query)
|
||||
result = SearchResult.new(group: nil, action: action, variable: variable, is_variable: true, query: query, is_from_name: true, is_preset: true)
|
||||
search_results.results << result
|
||||
end
|
||||
|
||||
if variable.value.downcase.include?(query)
|
||||
result = SearchResult.new(group: nil, action: action, variable: variable, is_variable: true, query: query, is_from_value: true, is_preset: true)
|
||||
search_results.results << result
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class SearchResults
|
||||
attr_reader :results
|
||||
|
||||
def initialize
|
||||
@results = []
|
||||
end
|
||||
|
||||
def groups
|
||||
@results.select { |result| result.group? && !result.preset? }
|
||||
end
|
||||
|
||||
def actions
|
||||
@results.select { |result| result.action? && !result.preset? }
|
||||
end
|
||||
|
||||
def variables
|
||||
@results.select { |result| result.variable? && !result.preset? }
|
||||
end
|
||||
|
||||
def group_presets
|
||||
@results.select { |result| result.group? && result.preset? }
|
||||
end
|
||||
|
||||
def action_presets
|
||||
@results.select { |result| result.action? && result.preset? }
|
||||
end
|
||||
|
||||
def variables_from_presets
|
||||
@results.select { |result| result.variable? && result.preset? }
|
||||
end
|
||||
end
|
||||
|
||||
class SearchResult
|
||||
attr_reader :group, :action, :variable, :query
|
||||
|
||||
def initialize(query:, group:, action: nil, variable: nil,
|
||||
is_group: false, is_action: false, is_variable: false,
|
||||
is_from_name: false, is_from_value: false, is_from_comment: false, is_preset: false)
|
||||
@group = group
|
||||
@action = action
|
||||
@variable = variable
|
||||
@query = query
|
||||
|
||||
@is_group = is_group
|
||||
@is_action = is_action
|
||||
@is_variable = is_variable
|
||||
|
||||
@is_from_name = is_from_name
|
||||
@is_from_value = is_from_value
|
||||
@is_from_comment = is_from_comment
|
||||
@is_preset = is_preset
|
||||
end
|
||||
|
||||
def group?
|
||||
@is_group
|
||||
end
|
||||
|
||||
def action?
|
||||
@is_action
|
||||
end
|
||||
|
||||
def variable?
|
||||
@is_variable
|
||||
end
|
||||
|
||||
def from_name?
|
||||
@is_from_name
|
||||
end
|
||||
|
||||
def from_value?
|
||||
@is_from_value
|
||||
end
|
||||
|
||||
def from_comment?
|
||||
@is_from_comment
|
||||
end
|
||||
|
||||
def preset?
|
||||
@is_preset
|
||||
end
|
||||
|
||||
def highlight(string)
|
||||
string.gsub(/#{@query}/i, "<b><c=ff00ff>#{@query}</c></b>")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user