Mercurial Hosting > editor
annotate src/luan_editor/Window.luan @ 76:1beb4c57c269 default tip
monospaced sans-serif
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 18 Jun 2025 20:20:21 -0600 |
parents | 7f5b3824f4d4 |
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 | |
38 | 136 local function new_window(file,document) |
37 | 137 local window = {} |
39 | 138 if file == nil or not file.exists() then |
139 window.has_file = false | |
140 elseif file.is_file() then | |
141 window.has_file = true | |
142 else | |
143 show_message_dialog(nil,"Not a file") | |
144 if n_windows == 0 then | |
145 Luan.exit() | |
146 else | |
147 return | |
148 end | |
149 end | |
150 window.has_file = file~=nil and file.exists() | |
37 | 151 local text_area = new_text_area{ |
152 wrap_style_word = true | |
153 line_wrap = config.line_wrap | |
154 whitespace_visible = config.whitespace_visible | |
155 tab_size = config.tab_size | |
57 | 156 --font = { family="Monospaced", size=13 } |
76 | 157 font_changes = { family=monospaced } |
37 | 158 } |
159 window.text_area = text_area | |
160 local title = file and file.canonical().to_string() or "new" | |
38 | 161 if document ~= nil then |
162 text_area.document = document | |
163 elseif file ~= nil then | |
67 | 164 local document_info = documents[title] |
165 if document_info ~= nil then | |
166 text_area.document = document_info.document | |
167 document_info.count = document_info.count + 1 | |
39 | 168 else |
67 | 169 document_info = { document = text_area.document, count = 1 } |
170 documents[title] = document_info | |
39 | 171 if file.exists() then |
172 text_area.text = file.read_text() | |
173 text_area.document.clear_unedited() | |
174 end | |
37 | 175 end |
176 end | |
177 text_area.set_selection(0) | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
178 local list_window_item = { |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
179 window = window |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
180 } |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
181 add_list_window_item(list_window_item) |
37 | 182 local status_bar = new_label{ |
183 constraints = "span,growx" | |
184 text = " " | |
185 border = create_empty_border(2,16,4,16) | |
186 } | |
187 window.status_bar = status_bar | |
188 local find_panel = make_find_panel(window) | |
189 local frame = new_frame{ | |
190 preferred_size = config.size | |
191 content_pane = new_panel{ | |
192 layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]") | |
193 children = { | |
194 new_scroll_pane{ | |
195 constraints = "grow" | |
196 view = text_area | |
197 row_header_view = new_text_area_line_numbers{ | |
198 text_area = text_area | |
44 | 199 foreground_color = grey |
37 | 200 border = create_empty_border(0,8,0,8) |
201 } | |
202 } | |
203 find_panel | |
204 status_bar | |
205 } | |
206 } | |
207 } | |
208 window.frame = frame | |
209 frame.add_close_listener(function() | |
210 n_windows = n_windows - 1 | |
211 if n_windows == 0 then | |
212 Luan.exit() | |
213 end | |
44 | 214 remove_list_window_item(list_window_item) |
73 | 215 local document_info = documents[title] |
216 if document_info ~= nil then | |
217 document_info.count = document_info.count - 1 | |
218 if document_info.count == 0 then | |
219 documents[title] = nil | |
220 end | |
67 | 221 end |
37 | 222 end) |
43 | 223 frame.add_window_focus_listener(function() |
56 | 224 list_view.selected_value = list_window_item |
69 | 225 if list_window.is_visible then |
226 list_window.to_front() | |
227 end | |
43 | 228 end) |
37 | 229 frame.add_resize_stopped_listener( 200, function() |
230 --logger.info(stringify(frame.size)) | |
231 config.size = frame.size | |
232 save_config() | |
233 end) | |
234 frame.add_move_stopped_listener( 200, function() | |
235 --logger.info(stringify(frame.location)) | |
236 config.location = frame.location | |
237 save_config() | |
238 end) | |
44 | 239 local function undo_listener() |
240 local is_unedited = text_area.document.is_unedited() | |
241 if window.is_unedited == is_unedited then | |
242 return | |
243 end | |
244 window.is_unedited = is_unedited | |
37 | 245 local s = title |
44 | 246 if not is_unedited then |
37 | 247 s = s.." *" |
248 end | |
249 frame.title = s | |
48 | 250 list_window_item.text = title |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
251 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
|
252 list_view.repaint(list_window_item) |
37 | 253 end |
44 | 254 undo_listener() |
255 --window.undo_listener = undo_listener -- dont gc | |
256 text_area.document.add_undo_listener(undo_listener) | |
48 | 257 function window.title() |
258 return title | |
259 end | |
37 | 260 function window.open() |
261 local new_file = choose_file{ | |
262 action = "load" | |
263 parent = frame | |
264 directory = file and file.parent() | |
265 } | |
266 if new_file ~= nil then | |
267 new_window(new_file) | |
268 end | |
269 end | |
270 function window.save() | |
271 if file == nil then | |
272 file = choose_file{ | |
273 action = "save" | |
274 parent = frame | |
275 } | |
276 if file == nil then | |
277 return false | |
278 end | |
279 title = file.canonical().to_string() | |
67 | 280 documents[title] = { document = text_area.document, count = 1 } |
48 | 281 window.is_unedited = nil |
282 undo_listener() | |
37 | 283 end |
39 | 284 try |
285 file.write_text(text_area.text) | |
286 catch e | |
287 show_message_dialog( frame, e.get_message() ) | |
288 return false | |
289 end | |
37 | 290 text_area.document.set_unedited() |
291 return true | |
292 end | |
293 function window.revert() | |
294 local selection = text_area.get_selection() | |
295 local text = file.read_text() | |
296 text_area.text = text | |
297 text_area.set_selection(min(selection,#text+1)) | |
298 text_area.document.set_unedited() | |
299 status_bar.text = "Reverted" | |
300 end | |
301 local function selection_lines() | |
302 local start_seletion, end_selection = text_area.get_selection() | |
303 local end_ = end_selection == start_seletion and end_selection or end_selection - 1 | |
304 local start_line = text_area.get_line_from_position(start_seletion) | |
305 local end_line = text_area.get_line_from_position(end_) | |
306 local start_pos = text_area.get_line_start_position(start_line) | |
307 local end_pos = text_area.get_line_end_position(end_line) | |
308 local text = text_area.text | |
309 text = sub_string(text,start_pos,end_pos-2) | |
310 return { | |
311 text = text | |
312 start_pos = start_pos | |
313 length = #text | |
314 lines = end_line - start_line + 1 | |
315 start_seletion = start_seletion | |
316 end_selection = end_selection | |
317 } | |
318 end | |
319 function window.indent() | |
320 local r = selection_lines() | |
321 local text = r.text | |
322 local start_pos = r.start_pos | |
323 text = "\t"..replace(text,"\n","\n\t") | |
324 text_area.replace(start_pos,r.length,text) | |
325 --logger.info(stringify{text_area.get_selection()}) | |
326 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines ) | |
327 end | |
328 function window.unindent() | |
329 local r = selection_lines() | |
330 local text = r.text | |
331 text = "\n"..text | |
332 local start_seletion = r.start_seletion | |
68 | 333 if starts_with(text,"\n\t") and start_seletion > r.start_pos then |
37 | 334 start_seletion = start_seletion - 1 |
335 end | |
336 local len1 = #text | |
337 text = replace(text,"\n\t","\n") | |
338 local len2 = #text | |
339 local end_selection = r.end_selection - (len1 - len2) | |
340 text = sub_string(text,2) | |
341 text_area.replace(r.start_pos,r.length,text) | |
342 text_area.set_selection(start_seletion,end_selection) | |
343 end | |
344 function window.cursor_column() | |
345 local cursor_pos = text_area.get_selection() | |
346 local line = text_area.get_line_from_position(cursor_pos) | |
347 local start_line_pos = text_area.get_line_start_position(line) | |
348 return cursor_pos - start_line_pos + 1 | |
349 end | |
350 function window.goto(line) | |
351 local pos = text_area.get_line_start_position(line) | |
352 text_area.set_selection(pos) | |
353 end | |
354 function window.set_line_wrap(line_wrap) | |
355 text_area.line_wrap = line_wrap | |
356 config.line_wrap = line_wrap | |
357 save_config() | |
358 end | |
359 function window.set_whitespace_visible(whitespace_visible) | |
360 text_area.whitespace_visible = whitespace_visible | |
361 config.whitespace_visible = whitespace_visible | |
362 save_config() | |
363 end | |
364 function window.set_tab_size(tab_size) | |
365 text_area.tab_size = tab_size | |
366 config.tab_size = tab_size | |
367 save_config() | |
368 end | |
38 | 369 function window.duplicate() |
370 local new = new_window(file,text_area.document) | |
371 new.text_area.set_selection( text_area.get_selection() ) | |
372 end | |
55 | 373 function window.paste_files() |
374 --logger.info("paste_files "..stringify(Clipboard.get_files())) | |
375 local files = Clipboard.get_files() | |
376 if files == nil then | |
377 return false | |
378 end | |
75 | 379 local fn = thread_safe_function( new_window ) |
380 Thread.run( function() | |
381 for _, file in ipairs(files) do | |
382 swing_run( function() | |
383 fn(file) | |
384 end ) | |
385 sleep(100) | |
386 end | |
387 end, true ) | |
55 | 388 return true |
389 end | |
56 | 390 local add_menu_bar = require "classpath:luan_editor/menu.luan" |
37 | 391 add_menu_bar(window) |
392 frame.pack() | |
46 | 393 local location = config.location |
394 if location ~= nil then | |
395 frame.location = location | |
37 | 396 end |
69 | 397 frame.is_visible = true |
37 | 398 text_area.request_focus_in_window() |
399 n_windows = n_windows + 1 | |
38 | 400 return window |
37 | 401 end |
56 | 402 Window.new_window = new_window |
37 | 403 |
75 | 404 function Window.open_window(file_path) |
405 if file_path == nil then | |
56 | 406 local window = list_view.selected_value.window |
407 window.frame.to_front() | |
408 window.text_area.request_focus_in_window() | |
75 | 409 else |
410 local file = new_file(file_path) | |
411 new_window(file) | |
56 | 412 end |
413 end | |
414 | |
415 return Window |