From b811a83c60a6c9000466bb399e9ddb11cc4e19aa Mon Sep 17 00:00:00 2001 From: cyberarm Date: Tue, 8 Sep 2020 20:11:58 -0500 Subject: [PATCH] Fixed issues with Action dialog --- lib/dialogs/action_dialog.rb | 18 +++++++-- lib/dialogs/name_prompt_dialog.rb | 12 +++++- lib/states/editor.rb | 61 +++++++++++++++++------------- lib/window.rb | 4 +- timecrafters_configuration_tool.rb | 1 + 5 files changed, 62 insertions(+), 34 deletions(-) diff --git a/lib/dialogs/action_dialog.rb b/lib/dialogs/action_dialog.rb index 2273514..43abd0b 100644 --- a/lib/dialogs/action_dialog.rb +++ b/lib/dialogs/action_dialog.rb @@ -4,8 +4,6 @@ module TAC def build background Gosu::Color::GRAY - @type = @options[:action].type if @options[:action] - label "Name" @name_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR @name_error.hide @@ -19,7 +17,7 @@ module TAC close end - button @options[:action] ? "Update" : "Add", width: 0.475 do |b| + button @options[:action] ? @options[:accept_label] ? @options[:accept_label] : "Update" : "Add", width: 0.475 do |b| if valid? if @options[:action] @options[:callback_method].call(@options[:action], @name.value.strip, @comment.value.strip) @@ -35,11 +33,23 @@ module TAC def valid? valid = true + name = @name.value.strip - if @name.value.strip.empty? + if name.empty? @name_error.value = "Error: Name cannot be blank\n or only whitespace." @name_error.show valid = false + + ### TODO: Handle case when renaming a cloned Action + # elsif @options[:action] && @options[:action].name == name + # @name_error.value = "" + # @name_error.hide + + elsif @options[:list].find { |action| action.name == name} + @name_error.value = "Error: Name is not unique!" + @name_error.show + valid = false + else @name_error.value = "" @name_error.hide diff --git a/lib/dialogs/name_prompt_dialog.rb b/lib/dialogs/name_prompt_dialog.rb index b3318bd..0b82d2f 100644 --- a/lib/dialogs/name_prompt_dialog.rb +++ b/lib/dialogs/name_prompt_dialog.rb @@ -40,12 +40,22 @@ module TAC end def valid? + name = @name.value.strip + 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 } + + ### TODO: Handle case when renaming a cloned Group + # elsif @options[:renaming] && @options[:renaming].name == name + # @name_error.value = "" + # @name_error.hide + + # return true + + elsif @options[:list] && @options[:list].find { |i| i.name == name } @name_error.value = "Name is not unique!" @name_error.show diff --git a/lib/states/editor.rb b/lib/states/editor.rb index d67d06b..6dfdee4 100644 --- a/lib/states/editor.rb +++ b/lib/states/editor.rb @@ -125,20 +125,21 @@ module TAC label "Actions", text_size: THEME_SUBHEADING_TEXT_SIZE button get_image("#{TAC::ROOT_PATH}/media/icons/plus.png"), image_width: THEME_ICON_SIZE, tip: "Add action" do if @active_group - push_state(TAC::Dialog::NamePromptDialog, title: "Create Action", list: @active_group.actions, callback_method: method(:create_action)) + push_state(TAC::Dialog::ActionDialog, title: "Create Action", list: @active_group.actions, callback_method: method(:create_action)) else push_state(TAC::Dialog::AlertDialog, title: "Error", message: "Unable to create action,\nno group selected.") end end button get_image("#{TAC::ROOT_PATH}/media/icons/button2.png"), image_width: THEME_ICON_SIZE, tip: "Clone currently selected action" do if @active_group && @active_action - push_state(Dialog::NamePromptDialog, title: "Clone Action", renaming: @active_action, accept_label: "Clone", list: @active_group.actions, callback_method: proc { |action, name| - clone = TAC::Config::Action.from_json( JSON.parse( @active_action.to_json, symbolize_names: true )) - clone.name = name - @active_group.actions << clone - window.backend.config_changed! + push_state(Dialog::ActionDialog, title: "Clone Action", action: @active_action, accept_label: "Clone", list: @active_group.actions, callback_method: proc { |action, name, comment| + clone = TAC::Config::Action.from_json( JSON.parse( @active_action.to_json, symbolize_names: true )) + clone.name = name + clone.comment = comment + @active_group.actions << clone + window.backend.config_changed! - populate_actions_list(@active_group) + populate_actions_list(@active_group) }) end end @@ -241,15 +242,16 @@ module TAC populate_groups_list end - def create_action(name) - @active_group.actions << TAC::Config::Action.new(name: name, enabled: true, variables: []) + def create_action(name, comment) + @active_group.actions << TAC::Config::Action.new(name: name, comment: comment, enabled: true, variables: []) window.backend.config_changed! populate_actions_list(@active_group) end - def update_action(action, name) + def update_action(action, name, comment) action.name = name + action.comment = comment window.backend.config_changed! populate_actions_list(@active_group) @@ -324,28 +326,33 @@ module TAC @actions_list.clear do actions.each_with_index do |action, i| - flow width: 1.0, **THEME_ITEM_CONTAINER_PADDING do + stack width: 1.0, **THEME_ITEM_CONTAINER_PADDING do background i.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR - button action.name, width: 0.8 do - @active_action = action - @active_action_label.value = action.name + flow width: 1.0 do + button action.name, width: 0.8 do + @active_action = action + @active_action_label.value = action.name - populate_variables_list(action) + populate_variables_list(action) + end + + action_enabled_toggle = toggle_button tip: "Enable action", checked: action.enabled + action_enabled_toggle.subscribe(:changed) do |sender, value| + action.enabled = value + window.backend.config_changed! + end + + button get_image("#{TAC::ROOT_PATH}/media/icons/gear.png"), image_width: THEME_ICON_SIZE, tip: "Edit action" do + push_state(Dialog::ActionDialog, title: "Rename Action", action: action, list: @active_group.actions, callback_method: method(:update_action)) + end + + button get_image("#{TAC::ROOT_PATH}/media/icons/trashcan.png"), image_width: THEME_ICON_SIZE, tip: "Delete action", **THEME_DANGER_BUTTON do + push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Delete action and all\nof its variables?", callback_method: proc { delete_action(action) }) + end end - action_enabled_toggle = toggle_button tip: "Enable action", checked: action.enabled - action_enabled_toggle.subscribe(:changed) do |sender, value| - action.enabled = value - window.backend.config_changed! - end - - button get_image("#{TAC::ROOT_PATH}/media/icons/gear.png"), image_width: THEME_ICON_SIZE, tip: "Edit action" do - push_state(Dialog::NamePromptDialog, title: "Rename Action", renaming: action, list: @active_group.actions, callback_method: method(:update_action)) - end - button get_image("#{TAC::ROOT_PATH}/media/icons/trashcan.png"), image_width: THEME_ICON_SIZE, tip: "Delete action", **THEME_DANGER_BUTTON do - push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Delete action and all\nof its variables?", callback_method: proc { delete_action(action) }) - end + label "#{action.comment}" unless action.comment.empty? end end end diff --git a/lib/window.rb b/lib/window.rb index a2b4c98..bbf2203 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -15,9 +15,9 @@ module TAC def close if @backend.config_changed? - push_state(Dialog::ConfirmDialog, title: "Unsaved Config", message: "Config has unsaved changes\nthat will be lost if\nyou continue!", callback_method: proc { cleanup_and_close }) + push_state(Dialog::ConfirmDialog, title: "Unsaved Config", message: "Config has unsaved changes\nthat will be lost if you continue!", callback_method: proc { cleanup_and_close }) elsif @backend.settings_changed? - push_state(Dialog::ConfirmDialog, title: "Unsaved Settings", message: "Settings has unsaved changes\nthat will be lost if\nyou continue!", callback_method: proc { cleanup_and_close }) + push_state(Dialog::ConfirmDialog, title: "Unsaved Settings", message: "Settings has unsaved changes\nthat will be lost if you continue!", callback_method: proc { cleanup_and_close }) else cleanup_and_close end diff --git a/timecrafters_configuration_tool.rb b/timecrafters_configuration_tool.rb index 96f444c..e0e58bf 100644 --- a/timecrafters_configuration_tool.rb +++ b/timecrafters_configuration_tool.rb @@ -25,6 +25,7 @@ require_relative "lib/dialog" require_relative "lib/dialogs/alert_dialog" require_relative "lib/dialogs/confirm_dialog" require_relative "lib/dialogs/name_prompt_dialog" +require_relative "lib/dialogs/action_dialog" require_relative "lib/dialogs/variable_dialog" require_relative "lib/dialogs/tacnet_dialog" require_relative "lib/dialogs/tacnet_status_dialog"