From dc88caf0fb1119039d5a40bc16c0c952480a61c3 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Tue, 23 Jun 2020 10:32:18 -0500 Subject: [PATCH] Changes --- lib/backend.rb | 2 +- lib/dialogs/name_prompt_dialog.rb | 34 +++++++++++++++++++++++------ lib/states/editor.rb | 2 +- lib/states/manage_configurations.rb | 27 ++++++++++++++--------- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/lib/backend.rb b/lib/backend.rb index e37ec4c..f134f5a 100644 --- a/lib/backend.rb +++ b/lib/backend.rb @@ -3,7 +3,7 @@ module TAC attr_reader :config, :settings, :tacnet def initialize load_settings - load_config(@settings.config) if @settings.config + load_config(@settings.config) if @settings.config && File.exist?("#{TAC::CONFIGS_PATH}/#{@settings.config}.json") @tacnet = TACNET.new @config_changed = false diff --git a/lib/dialogs/name_prompt_dialog.rb b/lib/dialogs/name_prompt_dialog.rb index 0113d7a..b3318bd 100644 --- a/lib/dialogs/name_prompt_dialog.rb +++ b/lib/dialogs/name_prompt_dialog.rb @@ -1,6 +1,8 @@ module TAC class Dialog class NamePromptDialog < Dialog + NameStub = Struct.new(:name) + def build background Gosu::Color::GRAY flow width: 1.0 do @@ -10,6 +12,10 @@ module TAC @name_error = label "", color: TAC::Palette::TACNET_CONNECTION_ERROR @name_error.hide + @name.subscribe(:changed) do |sender, value| + valid? + end + flow width: 1.0 do button "Cancel", width: 0.475 do close @@ -19,12 +25,7 @@ module TAC accept_label = @options[:accept_label] if @options[:accept_label] button accept_label, width: 0.475 do - if @name.value.strip.empty? - @name_error.value = "Name cannot be blank.\nName cannot only be whitespace." - @name_error.show - elsif @options[:list] && @options[:list].find { |i| i.name == @name.value.strip } - @name_error.value = "Name is not unique!" - @name_error.show + unless valid? else if @options[:renaming] @options[:callback_method].call(@options[:renaming], @name.value.strip) @@ -38,8 +39,27 @@ module TAC end end + def valid? + if @name.value.strip.empty? + @name_error.value = "Name cannot be blank.\nName cannot only be whitespace." + @name_error.show + + return false + elsif @options[:list] && @options[:list].find { |i| i.name == @name.value.strip } + @name_error.value = "Name is not unique!" + @name_error.show + + return false + else + @name_error.value = "" + @name_error.hide + + return true + end + end + def filter(text) - text.match(/[A-Za-z0-9,._\-]/) ? text : "" + text.match(/[A-Za-z0-9._\- ]/) ? text : "" end end end diff --git a/lib/states/editor.rb b/lib/states/editor.rb index 6331e47..0413fab 100644 --- a/lib/states/editor.rb +++ b/lib/states/editor.rb @@ -171,7 +171,7 @@ module TAC end end - unless window.backend.settings.config + if window.backend.settings.config == nil || window.backend.config == nil push_state(ManageConfigurations) else populate_groups_list diff --git a/lib/states/manage_configurations.rb b/lib/states/manage_configurations.rb index f4c2f48..38e297a 100644 --- a/lib/states/manage_configurations.rb +++ b/lib/states/manage_configurations.rb @@ -46,8 +46,11 @@ module TAC end def populate_configs + @config_files = Dir.glob("#{TAC::CONFIGS_PATH}/*.json") + @config_files_list = @config_files.map { |file| Dialog::NamePromptDialog::NameStub.new(File.basename(file, ".json")) } + @configs_list.clear do - Dir.glob("#{TAC::CONFIGS_PATH}/*.json").each_with_index do |config_file, i| + @config_files.each_with_index do |config_file, i| flow width: 1.0, **THEME_ITEM_CONTAINER_PADDING do background i.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR @@ -58,17 +61,21 @@ module TAC end button get_image("#{TAC::ROOT_PATH}/media/icons/gear.png"), image_width: THEME_ICON_SIZE, tip: "Rename configuration" do - push_state(Dialog::NamePromptDialog, title: "Rename Config", callback_method: proc { |new_name| - FileUtils.mv( - "#{TAC::CONFIGS_PATH}/#{name}.json", - "#{TAC::CONFIGS_PATH}/#{new_name}.json" - ) + push_state(Dialog::NamePromptDialog, title: "Rename Config", renaming: @config_files_list.find { |c| c.name == name }, list: @config_files_list, accept_label: "Update", callback_method: proc { |old_name, new_name| + if not File.exist?("#{TAC::CONFIGS_PATH}/#{new_name}.json") + FileUtils.mv( + "#{TAC::CONFIGS_PATH}/#{name}.json", + "#{TAC::CONFIGS_PATH}/#{new_name}.json" + ) - if window.backend.settings.config == name - change_config(new_name) + if window.backend.settings.config == name + change_config(new_name) + end + + populate_configs + else + push_state(Dialog::AlertDialog, title: "Config Rename Failed", message: "File already exists at\n#{TAC::CONFIGS_PATH}/#{new_name}.json}") end - - populate_configs }) end