Split off launcher settings into a 'form' parent module to share the implementation between initial setup and settings dialog

This commit is contained in:
2026-11-04 00:01:43 -06:00
parent 13d6c3339b
commit aa1145f1dc
4 changed files with 135 additions and 147 deletions

View File

@@ -1,100 +1,31 @@
module W3DHubLauncher
class Dialog
class LauncherSettings < W3DHubLauncher::Dialog
include W3DHubLauncher::Form::SettingsForm
def setup
super
stack(width: 0.75, max_width: 1280, height: 0.75, max_height: 800, style_class: [:dialog]) do
stack(width: 0.75, max_width: 1280, height: 0.75, max_height: 800, style_class: [:dialog], padding_left: PADDING, padding_right: PADDING) do |e|
flow(width: 1.0, background_nine_slice: NINE_SLICE_ROUNDED_TOP, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0) do
banner "Launcher Settings", width: 1.0, text_align: :center
end
item_height = 40 + PADDING
settings = MemCache[:settings]
form_setup
stack(width: 1.0, fill: true, scroll: true, margin_left: PADDING, margin_right: PADDING, padding: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: ALPHA_BLACK) do
flow(width: 1.0, height: item_height, margin_top: PADDING) do
tagline "Nickname", height: 1.0, text_v_align: :center
@nickname = edit_line settings&.preferences&.nickname || Etc.getlogin, fill: true
end
inscription "Nickname to use when joining servers.", margin_left: PADDING
flow(width: 1.0, height: item_height, margin_top: HALF_PADDING) do
tagline "Launcher package cache directory", height: 1.0, text_v_align: :center
@launcher_package_cache_directory = edit_line settings&.preferences&.launcher_package_cache_directory || DEFAULT_PACKAGE_CACHE_PATH, fill: true
button "Browse..."
end
inscription "Location where the launcher will download application packages.", margin_left: PADDING
flow(width: 1.0, height: item_height, margin_top: HALF_PADDING) do
tagline "Application installation directory", height: 1.0, text_v_align: :center
@application_installation_directory = edit_line settings&.preferences&.application_installation_directory || DEFAULT_APPLICATIONS_PATH, fill: true
button "Browse..."
end
inscription "Location where the launcher will install new applications.", margin_left: PADDING
flow(width: 1.0, height: item_height, margin_top: HALF_PADDING) do
tagline "Wine prefix path", height: 1.0, text_v_align: :center
@wine_prefix_path = edit_line settings&.preferences&.wine_prefix_path || "", fill: true
button "Browse..."
end
inscription "Location of wine prefix to use. Leave blank to use default.", margin_left: PADDING
flow(width: 1.0, height: item_height, margin_top: HALF_PADDING) do
tagline "Wine command", height: 1.0, text_v_align: :center
@wine_command = edit_line settings&.preferences&.wine_command || "wine", fill: true
button "Browse..."
end
inscription "Path to wine executable. Use <c=f80>wine</c> for system installed wine.", margin_left: PADDING
flow(width: 1.0, height: item_height, margin_top: HALF_PADDING) do
tagline "Winetricks command", height: 1.0, text_v_align: :center
@winetricks_command = edit_line settings&.preferences&.winetricks_command || "winetricks", fill: true
button "Browse..."
end
inscription "Path to winetricks executable. Use <c=f80>winetricks</c> for system installed winetricks.", margin_left: PADDING
end
flow(width: 1.0, padding: PADDING, background_nine_slice: NINE_SLICE_ROUNDED_BOTTOM, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0) do
flow(width: 1.0, padding_top: PADDING, padding_bottom: PADDING, background_nine_slice: NINE_SLICE_ROUNDED_BOTTOM, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0) do
button "Close" do
pop_state
end
flow(fill: true)
button "Save", **CTA_BUTTON_THEME do |btn|
btn.enabled = false
settings = MemCache[:settings]
# update user preferences
settings.preferences.nickname = @nickname.value.strip
settings.preferences.launcher_package_cache_directory = @launcher_package_cache_directory.value.strip
settings.preferences.application_installation_directory = @application_installation_directory.value.strip
settings.preferences.wine_prefix_path = @wine_prefix_path.value.strip
settings.preferences.wine_command = @wine_command.value.strip
settings.preferences.winetricks_command = @winetricks_command.value.strip
Worker::Api.update_settings(settings) do |result|
if result.okay?
on_main_thread(proc {
MemCache[:settings] = settings
pop_state
})
else
# FIXME: Somehow something went wrong... show an error message.
btn.enabled = true
end
end
@accept_button = button "Save", **CTA_BUTTON_THEME do |btn|
form_commit(btn)
end
end
end
end
# FIXME: Check settings for validity before saving
def valid?
true
end
end
end
end

116
lib/forms/settings_form.rb Normal file
View File

@@ -0,0 +1,116 @@
module W3DHubLauncher
module Form
module SettingsForm
include GuiExt
def form_setup(settings = MemCache[:settings])
item_height = 40 + PADDING - HALF_PADDING
stack(width: 1.0, fill: true, scroll: true, padding: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: ALPHA_BLACK) do
flow(width: 1.0, height: item_height - HALF_PADDING) do
tagline "Nickname", height: 1.0, text_v_align: :center
@nickname = edit_line settings&.preferences&.nickname || Etc.getlogin, fill: true, filter: method(:form_filter_nickname)
end
inscription "Nickname to use when joining servers. Nickname must be a least 3 characters long and no more than 40. Only A-z, 0-9, _, -, and [] characters allowed.", margin_left: PADDING
flow(width: 1.0, height: item_height, margin_top: HALF_PADDING) do
tagline "Launcher package cache directory", height: 1.0, text_v_align: :center
@launcher_package_cache_directory = edit_line settings&.preferences&.launcher_package_cache_directory || DEFAULT_PACKAGE_CACHE_PATH, fill: true
button "Browse..." do
Gosu.choose_file do |result|
pp result
end
end
end
inscription "Location where the launcher will download application packages.", margin_left: PADDING
flow(width: 1.0, height: item_height, margin_top: HALF_PADDING) do
tagline "Application installation directory", height: 1.0, text_v_align: :center
@application_installation_directory = edit_line settings&.preferences&.application_installation_directory || DEFAULT_APPLICATIONS_PATH, fill: true
button "Browse..." do
Gosu.choose_file do |result|
pp result
end
end
end
inscription "Location where the launcher will install new applications.", margin_left: PADDING
flow(width: 1.0, height: item_height, margin_top: HALF_PADDING) do
tagline "Wine prefix path", height: 1.0, text_v_align: :center
@wine_prefix_path = edit_line settings&.preferences&.wine_prefix_path || "", fill: true
button "Browse..." do
Gosu.choose_file do |result|
pp result
end
end
end
inscription "Location of wine prefix to use. Leave blank to use default.", margin_left: PADDING
flow(width: 1.0, height: item_height, margin_top: HALF_PADDING) do
tagline "Wine command", height: 1.0, text_v_align: :center
@wine_command = edit_line settings&.preferences&.wine_command || "wine", fill: true
button "Browse..." do
Gosu.choose_file do |result|
pp result
end
end
end
inscription "Path to wine executable. Use <c=f80>wine</c> for system installed wine.", margin_left: PADDING
flow(width: 1.0, height: item_height, margin_top: HALF_PADDING) do
tagline "Winetricks command", height: 1.0, text_v_align: :center
@winetricks_command = edit_line settings&.preferences&.winetricks_command || "winetricks", fill: true
button "Browse..." do
Gosu.choose_file do |result|
pp result
end
end
end
inscription "Path to winetricks executable. Use <c=f80>winetricks</c> for system installed winetricks.", margin_left: PADDING
end
end
def form_commit(btn)
btn.enabled = false
@settings = MemCache[:settings]
# update user preferences
@settings.preferences.nickname = @nickname.value.strip
@settings.preferences.launcher_package_cache_directory = @launcher_package_cache_directory.value.strip
@settings.preferences.application_installation_directory = @application_installation_directory.value.strip
@settings.preferences.wine_prefix_path = @wine_prefix_path.value.strip
@settings.preferences.wine_command = @wine_command.value.strip
@settings.preferences.winetricks_command = @winetricks_command.value.strip
Worker::Api.update_settings(@settings) do |result|
if result.okay?
on_main_thread(proc {
MemCache[:settings] = @settings
pop_state
})
else
# FIXME: Somehow something went wrong... show an error message.
btn.enabled = true
end
end
end
def form_valid?
form_valid_nickname?(@nickname.value)
end
def form_filter_nickname(entry)
if @nickname.value.length + 1 <= 40 && (entry =~ /^[a-z0-9_\-\[\]]+$/i)
entry
else
""
end
end
def form_valid_nickname?(value)
value.length.between?(3, 40) && (value =~ /^[a-z0-9_\-\[\]]+$/i)
end
end
end
end

View File

@@ -3,87 +3,27 @@ module W3DHubLauncher
module Boot
class InitialSetup < CyberarmEngine::Page
include GuiExt
include W3DHubLauncher::Form::SettingsForm
def setup
stack(width: 1.0, background_nine_slice: NINE_SLICE_ROUNDED, background_nine_slice_from_edge: 8, background_nine_slice_color: 0x11_ffffff, padding: HALF_PADDING) do
# initialize default settings
@settings = Worker::Api::Settings.new({})
stack(width: 1.0, background_nine_slice: NINE_SLICE_ROUNDED_TOP, background_nine_slice_from_edge: 8, background_nine_slice_color: 0x11_ffffff, padding: HALF_PADDING) do
banner "Welcome to #{NAME}", width: 1.0, text_align: :center
tagline "Your gateway to the world of W3D Hub games.", width: 1.0, text_align: :center
end
stack(width: 1.0, fill: true, padding: PADDING) do
stack(width: 1.0, fill: true, scroll: true) do
title "Initial Setup"
caption "Please confirm launcher's default settings and make any desired adjustments. These can be changed in the launcher settings later.", font: FONT_REGULAR, margin_left: PADDING
stack(width: 1.0, fill: true, padding: PADDING) do |e|
title "Initial Setup"
caption "Please confirm launcher's default settings and make any desired adjustments. These can be changed in the launcher settings later.", font: FONT_REGULAR, margin_left: PADDING
flow(width: 1.0, height: 40, margin_top: PADDING) do
tagline "Nickname", height: 1.0, text_v_align: :center
@nickname = edit_line Etc.getlogin, fill: true
end
inscription "Nickname to use when joining servers.", margin_left: PADDING
flow(width: 1.0, height: 40, margin_top: HALF_PADDING) do
tagline "Launcher package cache directory", height: 1.0, text_v_align: :center
@launcher_package_cache_directory = edit_line DEFAULT_PACKAGE_CACHE_PATH, fill: true
button "Browse..."
end
inscription "Location where the launcher will download application packages.", margin_left: PADDING
flow(width: 1.0, height: 40, margin_top: HALF_PADDING) do
tagline "Application installation directory", height: 1.0, text_v_align: :center
@application_installation_directory = edit_line DEFAULT_APPLICATIONS_PATH, fill: true
button "Browse..."
end
inscription "Location where the launcher will install new applications.", margin_left: PADDING
flow(width: 1.0, height: 40, margin_top: HALF_PADDING) do
tagline "Wine prefix path", height: 1.0, text_v_align: :center
@wine_prefix_path = edit_line "", fill: true
button "Browse..."
end
inscription "Location of wine prefix to use. Leave blank to use default.", margin_left: PADDING
flow(width: 1.0, height: 40, margin_top: HALF_PADDING) do
tagline "Wine command", height: 1.0, text_v_align: :center
@wine_command = edit_line "wine", fill: true
button "Browse..."
end
inscription "Path to wine executable. Use <c=f80>wine</c> for system installed wine.", margin_left: PADDING
flow(width: 1.0, height: 40, margin_top: HALF_PADDING) do
tagline "Winetricks command", height: 1.0, text_v_align: :center
@winetricks_command = edit_line "winetricks", fill: true
button "Browse..."
end
inscription "Path to winetricks executable. Use <c=f80>winetricks</c> for system installed winetricks.", margin_left: PADDING
end
form_setup(nil)
flow(width: 1.0, padding_top: PADDING) do
flow(fill: true)
button "Accept", **CTA_BUTTON_THEME do |btn|
btn.enabled = false
# initialize default settings
settings = Worker::Api::Settings.new({})
# update user preferences
settings.preferences.nickname = @nickname.value.strip
settings.preferences.launcher_package_cache_directory = @launcher_package_cache_directory.value.strip
settings.preferences.application_installation_directory = @application_installation_directory.value.strip
settings.preferences.wine_prefix_path = @wine_prefix_path.value.strip
settings.preferences.wine_command = @wine_command.value.strip
settings.preferences.winetricks_command = @winetricks_command.value.strip
Worker::Api.update_settings(settings) do |result|
if result.okay?
on_main_thread(proc {
MemCache[:settings] = settings
page(Page::Boot::StartUp)
})
else
# FIXME: Somehow something went wrong... show an error message.
btn.enabled = true
end
end
@accept_button = button "Accept", **CTA_BUTTON_THEME do |btn|
form_commit(btn)
end
end
end

View File

@@ -32,6 +32,7 @@ require_relative "lib/gui_ext"
require_relative "lib/state"
require_relative "lib/dialog"
require_relative "lib/theme"
require_relative "lib/forms/settings_form"
require_relative "lib/pages/games"
require_relative "lib/pages/server_browser"
require_relative "lib/pages/boot/terms"