changeset 92:aefa7f8bb407 default tip

better open files
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 09 Jul 2025 22:59:59 -0600 (54 minutes ago)
parents ff999e959b74
children
files src/luan_editor/Window.luan src/luan_editor/editor.luan src/luan_editor/menu.luan
diffstat 3 files changed, 44 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan_editor/Window.luan	Fri Jul 04 15:55:22 2025 -0600
+++ b/src/luan_editor/Window.luan	Wed Jul 09 22:59:59 2025 -0600
@@ -2,6 +2,7 @@
 local error = Luan.error
 local stringify = Luan.stringify or error()
 local ipairs = Luan.ipairs or error()
+local pairs = Luan.pairs or error()
 local Parsers = require "luan:Parsers.luan"
 local json_string = Parsers.json_string or error()
 local json_parse = Parsers.json_parse or error()
@@ -11,6 +12,8 @@
 local sub_string = String.sub or error()
 local replace = String.replace or error()
 local starts_with = String.starts_with or error()
+local Table = require "luan:Table.luan"
+local is_empty = Table.is_empty or error()
 local Io = require "luan:Io.luan"
 local Thread = require "luan:Thread.luan"
 local sleep = Thread.sleep or error()
@@ -52,7 +55,7 @@
 local monospaced = fonts["Lucida Console"] or fonts["Monospaced"] or error()
 -- logger.info(monospaced)
 
-local n_windows = 0
+local windows_set = {}
 local documents = {}
 
 local function bool(val,default)
@@ -135,8 +138,19 @@
 
 local new_window
 
+local function open_file(file)
+	local title = file.canonical().to_string()
+	for window in pairs(windows_set) do
+		if window.title == title then
+			window.to_front()
+			return
+		end
+	end
+	new_window(file)
+end
+
 local function open_files(files)
-	local fn = thread_safe_function( new_window )
+	local fn = thread_safe_function( open_file )
 	Thread.run( function()
 		for _, file in ipairs(files) do
 			swing_run( function()
@@ -146,6 +160,7 @@
 		end
 	end, true )
 end
+Window.open_files = open_files
 
 function new_window(file,document)
 	local window = {}
@@ -155,7 +170,7 @@
 		window.has_file = true
 	else
 		show_message_dialog(nil,"Not a file")
-		if n_windows == 0 then
+		if is_empty(windows_set) then
 			Luan.exit()
 		else
 			return
@@ -175,6 +190,7 @@
 	}
 	window.text_area = text_area
 	local title = file and file.canonical().to_string() or "new"
+	window.title = title
 	if document ~= nil then
 		text_area.document = document
 	elseif file ~= nil then
@@ -224,8 +240,8 @@
 	}
 	window.frame = frame
 	frame.add_close_listener(function()
-		n_windows = n_windows - 1
-		if n_windows == 0 then
+		windows_set[window] = nil
+		if is_empty(windows_set) then
 			Luan.exit()
 		end
 		remove_list_window_item(list_window_item)
@@ -271,9 +287,6 @@
 	undo_listener()
 	--window.undo_listener = undo_listener  -- dont gc
 	text_area.document.add_undo_listener(undo_listener)
-	function window.title()
-		return title
-	end
 	function window.open()
 		local new_files = choose_file{
 			action = "load"
@@ -396,6 +409,10 @@
 		open_files(files)
 		return true
 	end
+	function window.to_front()
+		frame.to_front()
+		text_area.request_focus_in_window()
+	end
 	local add_menu_bar = require "classpath:luan_editor/menu.luan"
 	add_menu_bar(window)
 	frame.pack()
@@ -405,20 +422,13 @@
 	end
 	frame.is_visible = true
 	text_area.request_focus_in_window()
-	n_windows = n_windows + 1
+	windows_set[window] = true
 	return window
 end
 Window.new_window = new_window
 
-function Window.open_window(file_path)
-	if file_path == nil then
-		local window = list_view.selected_value.window
-		window.frame.to_front()
-		window.text_area.request_focus_in_window()
-	else
-		local file = new_file(file_path)
-		new_window(file)
-	end
+function Window.to_front()
+	list_view.selected_value.window.to_front()
 end
 
 return Window
--- a/src/luan_editor/editor.luan	Fri Jul 04 15:55:22 2025 -0600
+++ b/src/luan_editor/editor.luan	Wed Jul 09 22:59:59 2025 -0600
@@ -16,25 +16,25 @@
 local logger = Logging.logger "editor/editor"
 
 
+local function open_args(args)
+	local files = {}
+	for _, arg in ipairs(args) do
+		files[#files+1] = new_file(arg)
+	end
+	swing_run( function()
+		local Window = require "classpath:luan_editor/Window.luan"
+		Window.open_files(files)
+	end )
+end
+
 local function open(args)
 	if #args == 0 then
 		swing_run( function()
 			local Window = require "classpath:luan_editor/Window.luan"
-			local new_window = Window.new_window or error()
-
-			new_window()
+			Window.new_window()
 		end )
 	else
-		for _, arg in ipairs(args) do
-			local file = new_file(arg)
-			swing_run( function()
-				local Window = require "classpath:luan_editor/Window.luan"
-				local new_window = Window.new_window or error()
-
-				new_window(file)
-			end )
-			sleep(100)
-		end
+		open_args(args)
 	end
 end
 
@@ -42,20 +42,10 @@
 	if #args == 0 then
 		swing_run( function()
 			local Window = require "classpath:luan_editor/Window.luan"
-			local open_window = Window.open_window or error()
-
-			open_window(nil)
+			Window.to_front()
 		end )
 	else
-		for _, arg in ipairs(args) do
-			swing_run( function()
-				local Window = require "classpath:luan_editor/Window.luan"
-				local open_window = Window.open_window or error()
-
-				open_window(arg)
-			end )
-			sleep(100)
-		end
+		open_args(args)
 	end
 end
 
--- a/src/luan_editor/menu.luan	Fri Jul 04 15:55:22 2025 -0600
+++ b/src/luan_editor/menu.luan	Wed Jul 09 22:59:59 2025 -0600
@@ -40,7 +40,7 @@
 		text = "File Path"
 		enabled = window.has_file
 		action_listener = function(_)
-			status_bar.text = window.title()
+			status_bar.text = window.title
 		end
 	}
 	local undo = new_menu_item{