Mercurial Hosting > editor
annotate src/luan_editor/Window.luan @ 84:eb6c42083100 default tip
open multiple files
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 26 Jun 2025 23:14:21 -0600 |
parents | 1beb4c57c269 |
children |
rev | line source |
---|---|
37 | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | |
3 local stringify = Luan.stringify or error() | |
55 | 4 local ipairs = Luan.ipairs or error() |
52 | 5 local Parsers = require "luan:Parsers.luan" |
6 local json_string = Parsers.json_string or error() | |
7 local json_parse = Parsers.json_parse or error() | |
37 | 8 local Math = require "luan:Math.luan" |
9 local min = Math.min or error() | |
10 local String = require "luan:String.luan" | |
11 local sub_string = String.sub or error() | |
12 local replace = String.replace or error() | |
13 local starts_with = String.starts_with or error() | |
14 local Io = require "luan:Io.luan" | |
75 | 15 local Thread = require "luan:Thread.luan" |
16 local sleep = Thread.sleep or error() | |
17 local thread_safe_function = Thread.thread_safe_function or error() | |
56 | 18 local new_file = Io.schemes.file or error() |
37 | 19 local new_text_area = require("luan:swing/Text_area.luan").new or error() |
20 local new_frame = require("luan:swing/Frame.luan").new or error() | |
41 | 21 local new_dialog = require("luan:swing/Dialog.luan").new or error() |
37 | 22 local new_panel = require("luan:swing/Component.luan").new_panel or error() |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
23 local new_list = require("luan:swing/List.luan").new or error() |
37 | 24 local Layout = require "luan:swing/Layout.luan" |
25 local new_mig_layout = Layout.new_mig_layout or error() | |
26 local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error() | |
27 local new_text_area_line_numbers = require("luan:swing/Text_area_line_numbers.luan").new or error() | |
28 local int_to_color = require("luan:swing/Color.luan").int_to_color or error() | |
29 local Border = require "luan:swing/Border.luan" | |
30 local create_empty_border = Border.create_empty_border or error() | |
31 local new_label = require("luan:swing/Label.luan").new or error() | |
32 local make_find_panel = require "classpath:luan_editor/find.luan" | |
33 local File_chooser = require "luan:swing/File_chooser.luan" | |
34 local choose_file = File_chooser.awt_choose_file or error() | |
39 | 35 local Option_pane = require "luan:swing/Option_pane.luan" |
36 local show_message_dialog = Option_pane.show_message_dialog or error() | |
55 | 37 local Clipboard = require "luan:swing/Clipboard.luan" |
75 | 38 local Swing_runner = require "luan:swing/Swing_runner.luan" |
39 local swing_run = Swing_runner.run or error() | |
76 | 40 local Font = require "luan:swing/Font.luan" |
53 | 41 local Java = require "classpath:luan_editor/Java.luan" |
37 | 42 local Logging = require "luan:logging/Logging.luan" |
56 | 43 local logger = Logging.logger "editor/Window" |
37 | 44 |
45 | |
56 | 46 local Window = {} |
47 | |
76 | 48 local fonts = {} |
49 for _, font in ipairs( Font.get_available_font_family_names() ) do | |
50 fonts[font] = font | |
51 end | |
52 local monospaced = fonts["Lucida Console"] or fonts["Monospaced"] or error() | |
53 -- logger.info(monospaced) | |
54 | |
37 | 55 local n_windows = 0 |
56 local documents = {} | |
57 | |
58 local function bool(val,default) | |
59 if val ~= nil then | |
60 return val | |
61 else | |
62 return default | |
63 end | |
64 end | |
65 | |
75 | 66 local config_file = Io.uri("file:"..Java.config_path) |
37 | 67 local config = {} |
68 if config_file.exists() then | |
69 try | |
52 | 70 config = json_parse(config_file.read_text()) |
37 | 71 catch e |
72 logger.error(e) | |
73 end | |
74 end | |
75 config.size = config.size or { width=700, height=700 } | |
76 config.line_wrap = bool( config.line_wrap, true ) | |
77 config.whitespace_visible = bool( config.whitespace_visible, false ) | |
78 config.tab_size = config.tab_size or 4 | |
46 | 79 config.list_window = config.list_window or {} |
80 config.list_window.size = config.list_window.size or { width=200, height=400 } | |
37 | 81 |
82 local function save_config() | |
52 | 83 config_file.write_text( json_string(config).."\n" ) |
37 | 84 end |
85 | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
86 local list_view = new_list{ |
41 | 87 --layout = new_mig_layout("insets 0,wrap,fill") |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
88 --layout = new_mig_layout("wrap","[grow]") |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
89 horizontal_alignment = "right" |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
90 hover_background = int_to_color(0xEEEEEE) |
41 | 91 } |
92 local list_scroll_pane = new_scroll_pane{ | |
93 view = list_view | |
94 } | |
95 local list_window = new_dialog{ | |
46 | 96 size = config.list_window.size |
41 | 97 content_pane = list_scroll_pane |
43 | 98 focusable_window_state = false |
41 | 99 } |
46 | 100 list_window.add_resize_stopped_listener( 200, function() |
101 --logger.info(stringify(list_window.size)) | |
102 config.list_window.size = list_window.size | |
103 save_config() | |
104 end) | |
105 list_window.add_move_stopped_listener( 200, function() | |
106 --logger.info(stringify(list_window.location)) | |
107 config.list_window.location = list_window.location | |
108 save_config() | |
109 end) | |
56 | 110 function Window.show_list_window() |
46 | 111 local location = config.list_window.location |
112 if location ~= nil then | |
113 list_window.location = location | |
114 end | |
69 | 115 list_window.is_visible = true |
44 | 116 list_scroll_pane.scroll_to_right() |
117 end | |
118 | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
119 local function add_list_window_item(item) |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
120 list_view.add_element(item) |
41 | 121 list_scroll_pane.scroll_to_right() |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
122 end |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
123 local function remove_list_window_item(item) |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
124 list_view.remove_element(item) --or error() |
41 | 125 end |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
126 list_view.add_list_selection_listener( function(item) |
49 | 127 if item ~= nil then |
128 item.window.text_area.request_focus() | |
129 end | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
130 end ) |
41 | 131 |
44 | 132 local black = int_to_color(0x000000) |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
133 local dark_blue = int_to_color(0x0000C0) |
44 | 134 local grey = int_to_color(0x888888) |
135 | |
84 | 136 local new_window |
137 | |
138 local function open_files(files) | |
139 local fn = thread_safe_function( new_window ) | |
140 Thread.run( function() | |
141 for _, file in ipairs(files) do | |
142 swing_run( function() | |
143 fn(file) | |
144 end ) | |
145 sleep(100) | |
146 end | |
147 end, true ) | |
148 end | |
149 | |
150 function new_window(file,document) | |
37 | 151 local window = {} |
39 | 152 if file == nil or not file.exists() then |
153 window.has_file = false | |
154 elseif file.is_file() then | |
155 window.has_file = true | |
156 else | |
157 show_message_dialog(nil,"Not a file") | |
158 if n_windows == 0 then | |
159 Luan.exit() | |
160 else | |
161 return | |
162 end | |
163 end | |
164 window.has_file = file~=nil and file.exists() | |
37 | 165 local text_area = new_text_area{ |
166 wrap_style_word = true | |
167 line_wrap = config.line_wrap | |
168 whitespace_visible = config.whitespace_visible | |
169 tab_size = config.tab_size | |
57 | 170 --font = { family="Monospaced", size=13 } |
76 | 171 font_changes = { family=monospaced } |
37 | 172 } |
173 window.text_area = text_area | |
174 local title = file and file.canonical().to_string() or "new" | |
38 | 175 if document ~= nil then |
176 text_area.document = document | |
177 elseif file ~= nil then | |
67 | 178 local document_info = documents[title] |
179 if document_info ~= nil then | |
180 text_area.document = document_info.document | |
181 document_info.count = document_info.count + 1 | |
39 | 182 else |
67 | 183 document_info = { document = text_area.document, count = 1 } |
184 documents[title] = document_info | |
39 | 185 if file.exists() then |
186 text_area.text = file.read_text() | |
187 text_area.document.clear_unedited() | |
188 end | |
37 | 189 end |
190 end | |
191 text_area.set_selection(0) | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
192 local list_window_item = { |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
193 window = window |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
194 } |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
195 add_list_window_item(list_window_item) |
37 | 196 local status_bar = new_label{ |
197 constraints = "span,growx" | |
198 text = " " | |
199 border = create_empty_border(2,16,4,16) | |
200 } | |
201 window.status_bar = status_bar | |
202 local find_panel = make_find_panel(window) | |
203 local frame = new_frame{ | |
204 preferred_size = config.size | |
205 content_pane = new_panel{ | |
206 layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]") | |
207 children = { | |
208 new_scroll_pane{ | |
209 constraints = "grow" | |
210 view = text_area | |
211 row_header_view = new_text_area_line_numbers{ | |
212 text_area = text_area | |
44 | 213 foreground_color = grey |
37 | 214 border = create_empty_border(0,8,0,8) |
215 } | |
216 } | |
217 find_panel | |
218 status_bar | |
219 } | |
220 } | |
221 } | |
222 window.frame = frame | |
223 frame.add_close_listener(function() | |
224 n_windows = n_windows - 1 | |
225 if n_windows == 0 then | |
226 Luan.exit() | |
227 end | |
44 | 228 remove_list_window_item(list_window_item) |
73 | 229 local document_info = documents[title] |
230 if document_info ~= nil then | |
231 document_info.count = document_info.count - 1 | |
232 if document_info.count == 0 then | |
233 documents[title] = nil | |
234 end | |
67 | 235 end |
37 | 236 end) |
43 | 237 frame.add_window_focus_listener(function() |
56 | 238 list_view.selected_value = list_window_item |
69 | 239 if list_window.is_visible then |
240 list_window.to_front() | |
241 end | |
43 | 242 end) |
37 | 243 frame.add_resize_stopped_listener( 200, function() |
244 --logger.info(stringify(frame.size)) | |
245 config.size = frame.size | |
246 save_config() | |
247 end) | |
248 frame.add_move_stopped_listener( 200, function() | |
249 --logger.info(stringify(frame.location)) | |
250 config.location = frame.location | |
251 save_config() | |
252 end) | |
44 | 253 local function undo_listener() |
254 local is_unedited = text_area.document.is_unedited() | |
255 if window.is_unedited == is_unedited then | |
256 return | |
257 end | |
258 window.is_unedited = is_unedited | |
37 | 259 local s = title |
44 | 260 if not is_unedited then |
37 | 261 s = s.." *" |
262 end | |
263 frame.title = s | |
48 | 264 list_window_item.text = title |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
265 list_window_item.foreground_color = is_unedited and black or dark_blue |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
266 list_view.repaint(list_window_item) |
37 | 267 end |
44 | 268 undo_listener() |
269 --window.undo_listener = undo_listener -- dont gc | |
270 text_area.document.add_undo_listener(undo_listener) | |
48 | 271 function window.title() |
272 return title | |
273 end | |
37 | 274 function window.open() |
84 | 275 local new_files = choose_file{ |
37 | 276 action = "load" |
277 parent = frame | |
278 directory = file and file.parent() | |
84 | 279 multiple = true |
37 | 280 } |
84 | 281 open_files(new_files) |
37 | 282 end |
283 function window.save() | |
284 if file == nil then | |
84 | 285 local files = choose_file{ |
37 | 286 action = "save" |
287 parent = frame | |
288 } | |
84 | 289 if #files == 0 then |
37 | 290 return false |
291 end | |
84 | 292 file = files[1] |
37 | 293 title = file.canonical().to_string() |
67 | 294 documents[title] = { document = text_area.document, count = 1 } |
48 | 295 window.is_unedited = nil |
296 undo_listener() | |
37 | 297 end |
39 | 298 try |
299 file.write_text(text_area.text) | |
300 catch e | |
301 show_message_dialog( frame, e.get_message() ) | |
302 return false | |
303 end | |
37 | 304 text_area.document.set_unedited() |
305 return true | |
306 end | |
307 function window.revert() | |
308 local selection = text_area.get_selection() | |
309 local text = file.read_text() | |
310 text_area.text = text | |
311 text_area.set_selection(min(selection,#text+1)) | |
312 text_area.document.set_unedited() | |
313 status_bar.text = "Reverted" | |
314 end | |
315 local function selection_lines() | |
316 local start_seletion, end_selection = text_area.get_selection() | |
317 local end_ = end_selection == start_seletion and end_selection or end_selection - 1 | |
318 local start_line = text_area.get_line_from_position(start_seletion) | |
319 local end_line = text_area.get_line_from_position(end_) | |
320 local start_pos = text_area.get_line_start_position(start_line) | |
321 local end_pos = text_area.get_line_end_position(end_line) | |
322 local text = text_area.text | |
323 text = sub_string(text,start_pos,end_pos-2) | |
324 return { | |
325 text = text | |
326 start_pos = start_pos | |
327 length = #text | |
328 lines = end_line - start_line + 1 | |
329 start_seletion = start_seletion | |
330 end_selection = end_selection | |
331 } | |
332 end | |
333 function window.indent() | |
334 local r = selection_lines() | |
335 local text = r.text | |
336 local start_pos = r.start_pos | |
337 text = "\t"..replace(text,"\n","\n\t") | |
338 text_area.replace(start_pos,r.length,text) | |
339 --logger.info(stringify{text_area.get_selection()}) | |
340 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines ) | |
341 end | |
342 function window.unindent() | |
343 local r = selection_lines() | |
344 local text = r.text | |
345 text = "\n"..text | |
346 local start_seletion = r.start_seletion | |
68 | 347 if starts_with(text,"\n\t") and start_seletion > r.start_pos then |
37 | 348 start_seletion = start_seletion - 1 |
349 end | |
350 local len1 = #text | |
351 text = replace(text,"\n\t","\n") | |
352 local len2 = #text | |
353 local end_selection = r.end_selection - (len1 - len2) | |
354 text = sub_string(text,2) | |
355 text_area.replace(r.start_pos,r.length,text) | |
356 text_area.set_selection(start_seletion,end_selection) | |
357 end | |
358 function window.cursor_column() | |
359 local cursor_pos = text_area.get_selection() | |
360 local line = text_area.get_line_from_position(cursor_pos) | |
361 local start_line_pos = text_area.get_line_start_position(line) | |
362 return cursor_pos - start_line_pos + 1 | |
363 end | |
364 function window.goto(line) | |
365 local pos = text_area.get_line_start_position(line) | |
366 text_area.set_selection(pos) | |
367 end | |
368 function window.set_line_wrap(line_wrap) | |
369 text_area.line_wrap = line_wrap | |
370 config.line_wrap = line_wrap | |
371 save_config() | |
372 end | |
373 function window.set_whitespace_visible(whitespace_visible) | |
374 text_area.whitespace_visible = whitespace_visible | |
375 config.whitespace_visible = whitespace_visible | |
376 save_config() | |
377 end | |
378 function window.set_tab_size(tab_size) | |
379 text_area.tab_size = tab_size | |
380 config.tab_size = tab_size | |
381 save_config() | |
382 end | |
38 | 383 function window.duplicate() |
384 local new = new_window(file,text_area.document) | |
385 new.text_area.set_selection( text_area.get_selection() ) | |
386 end | |
55 | 387 function window.paste_files() |
388 --logger.info("paste_files "..stringify(Clipboard.get_files())) | |
389 local files = Clipboard.get_files() | |
390 if files == nil then | |
391 return false | |
392 end | |
84 | 393 open_files(files) |
55 | 394 return true |
395 end | |
56 | 396 local add_menu_bar = require "classpath:luan_editor/menu.luan" |
37 | 397 add_menu_bar(window) |
398 frame.pack() | |
46 | 399 local location = config.location |
400 if location ~= nil then | |
401 frame.location = location | |
37 | 402 end |
69 | 403 frame.is_visible = true |
37 | 404 text_area.request_focus_in_window() |
405 n_windows = n_windows + 1 | |
38 | 406 return window |
37 | 407 end |
56 | 408 Window.new_window = new_window |
37 | 409 |
75 | 410 function Window.open_window(file_path) |
411 if file_path == nil then | |
56 | 412 local window = list_view.selected_value.window |
413 window.frame.to_front() | |
414 window.text_area.request_focus_in_window() | |
75 | 415 else |
416 local file = new_file(file_path) | |
417 new_window(file) | |
56 | 418 end |
419 end | |
420 | |
421 return Window |