Ducktape on libui for native file open dialogs

This commit is contained in:
2024-03-11 13:45:10 -05:00
parent c1ca3ec80e
commit 02307f1789
4 changed files with 19 additions and 21 deletions

View File

@@ -31,7 +31,7 @@ class W3DHub
flow(width: 1.0, fill: true) do
@game_path = edit_line "#{@game&.path}", fill: true, height: 1.0
button "Browse...", width: 128, height: 1.0, enabled: W3DHub.unix?, tip: W3DHub.unix? ? "Browse for game executable" : "Not available on Windows" do
button "Browse...", width: 128, height: 1.0, tip: "Browse for game executable" do
path = W3DHub.ask_file
@game_path.value = path if !path.empty? && File.exist?(path)
end

View File

@@ -147,26 +147,16 @@ class W3DHub
end
def self.ask_file(title: "Open File", filter: "*game*.exe")
if W3DHub.unix?
# search for command
cmds = %w{ zenity matedialog qarma kdialog }
result_ptr = LibUI.open_file(LIBUI_WINDOW)
result = result_ptr.null? ? "" : result_ptr.to_s.gsub("\\", "/")
command = cmds.find do |cmd|
cmd if system("which #{cmd}")
result
end
path = case File.basename(command)
when "zenity", "matedialog", "qarma"
`#{command} --file-selection --title "#{title}" --file-filter "#{filter}"`
when "kdialog"
`#{command} --title "#{title}" --getopenfilename . "#{filter}"`
else
raise "No known command found for system file selection dialog!"
end
def self.ask_folder(title: "Open Folder")
result_ptr = LibUI.open_folder(window)
result = result_ptr.null? ? "" : result_ptr.to_s.gsub("\\", "/")
path.strip
else
raise NotImplementedError
end
result
end
end

View File

@@ -26,7 +26,7 @@ class W3DHub
flow(width: 1.0, fill: true) do
@game_path = edit_line "", fill: true, height: 1.0
button "Browse...", width: 128, height: 1.0, enabled: W3DHub.unix?, tip: W3DHub.unix? ? "Browse for game executable" : "Not available on Windows" do
button "Browse...", width: 128, height: 1.0, tip: "Browse for game executable" do
path = W3DHub.ask_file
@game_path.value = path if !path.empty? && File.exist?(path)
end

View File

@@ -15,7 +15,6 @@ require "time"
require "base64"
require "zip"
require "excon"
require "libui"
class W3DHub
W3DHUB_DEBUG = ARGV.join.include?("--debug")
@@ -81,6 +80,15 @@ require "i18n"
require "websocket-client-simple"
require "English"
require "sdl2"
require "libui"
# Using a WHOLE ui library for: native file/folder open dialogs...
LibUI.init
LIBUI_WINDOW = LibUI.new_window("", 100, 100, 0)
at_exit do
LibUI.control_destroy(LIBUI_WINDOW)
LibUI.uninit
end
I18n.load_path << Dir["#{W3DHub::GAME_ROOT_PATH}/locales/*.yml"]
I18n.default_locale = :en