Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberarm committed Jun 23, 2020
1 parent ffd8146 commit dc88caf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Backend
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
Expand Down
34 changes: 27 additions & 7 deletions lib/dialogs/name_prompt_dialog.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +12,10 @@ def build
@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
Expand All @@ -19,12 +25,7 @@ def build
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)
Expand All @@ -38,8 +39,27 @@ def build
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
Expand Down
2 changes: 1 addition & 1 deletion lib/states/editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def setup
end
end

unless window.backend.settings.config
if window.backend.settings.config == nil || window.backend.config == nil
push_state(ManageConfigurations)
else
populate_groups_list
Expand Down
29 changes: 18 additions & 11 deletions lib/states/manage_configurations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ def setup
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

Expand All @@ -58,17 +61,21 @@ def populate_configs
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"
)

if window.backend.settings.config == name
change_config(new_name)
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)
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

Expand Down

0 comments on commit dc88caf

Please sign in to comment.