mirror of
https://github.com/TimeCrafters/timecrafters_configuration_tool_desktop.git
synced 2025-12-15 05:22:34 +00:00
73 lines
1.9 KiB
Ruby
73 lines
1.9 KiB
Ruby
module TAC
|
|
class Dialog
|
|
class NamePromptDialog < Dialog
|
|
NameStub = Struct.new(:name)
|
|
|
|
def build
|
|
background Gosu::Color::GRAY
|
|
para "Name", width: 1.0, text_align: :center
|
|
@name_error = para "", color: TAC::Palette::TACNET_CONNECTION_ERROR
|
|
@name_error.hide
|
|
@name = edit_line @options[:renaming] ? @options[:renaming].name : "", filter: method(:name_filter), width: 1.0, autofocus: true, focus: true
|
|
|
|
@name.subscribe(:changed) do |sender, value|
|
|
valid?
|
|
end
|
|
|
|
flow width: 1.0, margin_top: THEME_DIALOG_BUTTON_PADDING do
|
|
button "Cancel", width: 0.5 do
|
|
close
|
|
end
|
|
|
|
accept_label = @options[:renaming] ? "Update" : "Add"
|
|
accept_label = @options[:accept_label] if @options[:accept_label]
|
|
|
|
button accept_label, width: 0.5 do
|
|
try_commit
|
|
end
|
|
end
|
|
end
|
|
|
|
def try_commit
|
|
if valid?
|
|
if @options[:renaming]
|
|
@options[:callback_method].call(@options[:renaming], @name.value.strip)
|
|
else
|
|
@options[:callback_method].call(@name.value.strip)
|
|
end
|
|
|
|
close
|
|
end
|
|
end
|
|
|
|
def valid?
|
|
name = @name.value.strip
|
|
|
|
if @name.value.strip.empty?
|
|
@name_error.value = "Name cannot be blank. Name cannot only be whitespace."
|
|
@name_error.show
|
|
|
|
return false
|
|
|
|
### 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
|
|
|
|
return false
|
|
else
|
|
@name_error.value = ""
|
|
@name_error.hide
|
|
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end |