changeset 1917:f5f4c79d375f default tip

1 process
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 18 Apr 2025 19:17:23 -0600
parents 3fc4b465e9b0
children
files src/luan/modules/Rpc.luan src/luan/modules/editor/editor.luan src/luan/modules/swing/Swing.luan src/luan/modules/swing/SwingLuan.java src/luan/modules/swing/TextAreaLuan.java
diffstat 5 files changed, 78 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/Rpc.luan	Thu Apr 17 23:27:42 2025 -0600
+++ b/src/luan/modules/Rpc.luan	Fri Apr 18 19:17:23 2025 -0600
@@ -23,7 +23,6 @@
 local Table = require "luan:Table.luan"
 local java_to_table_deep = Table.java_to_table_deep or error()
 local unpack = Table.unpack or error()
-local ThreadLuan = require "java:luan.modules.ThreadLuan"
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "Rpc"
 
@@ -170,13 +169,14 @@
 	return responder
 end_function
 
-function Rpc.remote(domain)
+function Rpc.remote(domain,port)
+	port = port or Rpc.port
 	local socket
 	if Rpc.cipher_suites == nil then
-		socket = Socket.new(domain,Rpc.port)
+		socket = Socket.new(domain,port)
 	else
 		try
-			socket = IoUtils.getSSLSocketFactory().createSocket(domain,Rpc.port)
+			socket = IoUtils.getSSLSocketFactory().createSocket(domain,port)
 		catch e
 			e.java.showCause = false
 			e.throw()
@@ -199,9 +199,8 @@
 	return t
 end_function
 
-function Rpc.serve(port,fns)
+local function new_server_socket(port)
 	port = port or Rpc.port
-	fns = fns or Rpc.functions
 	local socket_server
 	if Rpc.cipher_suites == nil then
 		socket_server = ServerSocket.new(port)
@@ -209,6 +208,12 @@
 		socket_server = IoUtils.getSSLServerSocketFactory().createServerSocket(port)
 		socket_server.setEnabledCipherSuites(Rpc.cipher_suites)
 	end
+	return socket_server
+end
+Rpc.new_server_socket = new_server_socket
+
+local function serve_socket(socket_server,fns,multithreaded)
+	fns = fns or Rpc.functions
 	while true do
 		try
 			local socket = socket_server.accept()
@@ -225,11 +230,21 @@
 					responder and responder.after_close and responder.after_close()
 				end
 			end
-			Thread.run(server)
+			if multithreaded then
+				Thread.run(server)
+			else
+				server()
+			end
 		catch e
 			logger.error(e)
 		end
 	end_while
+end
+Rpc.serve_socket = serve_socket
+
+function Rpc.serve(port,fns)
+	local socket_server = new_server_socket(port)
+	serve_socket(socket_server,fns,true)
 end_function
 
 return Rpc
--- a/src/luan/modules/editor/editor.luan	Thu Apr 17 23:27:42 2025 -0600
+++ b/src/luan/modules/editor/editor.luan	Fri Apr 18 19:17:23 2025 -0600
@@ -1,13 +1,52 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local ipairs = Luan.ipairs or error()
+local String = require "luan:String.luan"
+local contains = String.contains or error()
 local Io = require "luan:Io.luan"
 local new_file = Io.schemes.file or error()
+local Rpc = require "luan:Rpc.luan"
 local Swing = require "luan:swing/Swing.luan"
+local swing_run = Swing.run or error()
+local to_front = Swing.to_front or error()
 local new_window = require "luan:editor/window.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "editor/editor"
 
 
-Swing.run(function()
+local port = 56587
+
+Rpc.cipher_suites = nil
+local server_socket
+try
+	server_socket = Rpc.new_server_socket(port)
+catch e
+	--logger.info(e.get_message())
+	if not contains( e.get_message(), "java.net.BindException" ) then
+		e.throw()
+	end
+	local host = Rpc.remote("localhost",port)
+	local args = Luan.arg
+	for _, arg in ipairs(args) do
+		local file = new_file(arg)
+		file = file.canonical().to_string()
+		host.open(file)
+	end
+	host.to_front()
+	return
+end
+function Rpc.functions.open(file_path)
+	swing_run(function()
+		local file = new_file(file_path)
+		new_window(file)
+	end)
+end
+function Rpc.functions.to_front()
+	swing_run(to_front)
+end
+
+
+swing_run(function()
 	local args = Luan.arg
 	if #args == 0 then
 		new_window()
@@ -18,3 +57,5 @@
 		end
 	end
 end)
+
+Rpc.serve_socket(server_socket,nil,false)
--- a/src/luan/modules/swing/Swing.luan	Thu Apr 17 23:27:42 2025 -0600
+++ b/src/luan/modules/swing/Swing.luan	Fri Apr 18 19:17:23 2025 -0600
@@ -6,6 +6,7 @@
 local URI = require "java:java.net.URI"
 local Insets = require "java:java.awt.Insets"
 local Desktop = require "java:java.awt.Desktop"
+local Window = require "java:java.awt.Window"
 local UIManager = require "java:javax.swing.UIManager"
 local FlatLightLaf = require "java:com.formdev.flatlaf.FlatLightLaf"
 local SwingLuan = require "java:luan.modules.swing.SwingLuan"
@@ -34,6 +35,7 @@
 Swing.set_look_and_feel = UIManager.setLookAndFeel
 
 Swing.run = SwingLuan.run
+Swing.run_later = SwingLuan.runLater
 
 Swing.new_insets = Insets.new  -- top, left, bottom, right
 Swing.no_insets = Insets.new(0,0,0,0)
@@ -46,4 +48,11 @@
 
 Swing.home_dir = System.getProperty("user.home")
 
+function Swing.to_front()
+	local windows = {Window.getOwnerlessWindows()}
+	for _, window in ipairs(windows) do
+		window.toFront()
+	end
+end
+
 return Swing
--- a/src/luan/modules/swing/SwingLuan.java	Thu Apr 17 23:27:42 2025 -0600
+++ b/src/luan/modules/swing/SwingLuan.java	Fri Apr 18 19:17:23 2025 -0600
@@ -56,6 +56,10 @@
 		SwingUtilities.invokeAndWait(runnable(luan,fn));
 	}
 
+	public static void runLater(Luan luan,LuanFunction fn) throws InterruptedException, InvocationTargetException {
+		SwingUtilities.invokeLater(runnable(luan,fn));
+	}
+
 	public static ActionListener newActionListener(final Luan luan,final LuanFunction fn) throws LuanException {
 		if( fn == null )
 			exception("function is null");
--- a/src/luan/modules/swing/TextAreaLuan.java	Thu Apr 17 23:27:42 2025 -0600
+++ b/src/luan/modules/swing/TextAreaLuan.java	Fri Apr 18 19:17:23 2025 -0600
@@ -22,7 +22,7 @@
 public class TextAreaLuan extends JTextArea implements DocumentUpdateListener {
 	private static final Logger logger = LoggerFactory.getLogger(TextAreaLuan.class);
 
-	private static final DefaultCaret flatLafCaret = new DefaultCaret() {
+	private final DefaultCaret flatLafCaret = new DefaultCaret() {
 		private final Timer blinkTimer = new Timer(500, new ActionListener() {
 			@Override public void actionPerformed(ActionEvent evt) {
 				setVisible(!isVisible());