Mercurial Hosting > editor
comparison editor.luan @ 29:01b8a25b38aa
work
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 13 Apr 2025 12:25:57 -0600 |
parents | bdb8754f1211 |
children | 8e32ad89c2a1 |
comparison
equal
deleted
inserted
replaced
28:bdb8754f1211 | 29:01b8a25b38aa |
---|---|
50 return function(_) | 50 return function(_) |
51 fn() | 51 fn() |
52 end | 52 end |
53 end | 53 end |
54 | 54 |
55 local function make_menu_bar(window) | 55 local function add_menu_bar(window) |
56 local document = window.text_area.document | 56 local document = window.text_area.document |
57 local revert = new_menu_item{ | 57 local revert = new_menu_item{ |
58 text = "Revert" | 58 text = "Revert" |
59 enabled = window.has_file | 59 enabled = window.has_file |
60 action_listener = action_listener(window.revert) | 60 action_listener = action_listener(window.revert) |
75 end | 75 end |
76 window.update_undo_redo = update_undo_redo -- dont gc | 76 window.update_undo_redo = update_undo_redo -- dont gc |
77 update_undo_redo() | 77 update_undo_redo() |
78 document.add_undo_listener(update_undo_redo) | 78 document.add_undo_listener(update_undo_redo) |
79 | 79 |
80 return new_menu_bar{ | 80 local menu_bar = new_menu_bar{ |
81 menus = { | 81 menus = { |
82 new_menu{ | 82 new_menu{ |
83 text = "File" | 83 text = "File" |
84 menu_items = { | 84 menu_items = { |
85 new_menu_item{ | 85 new_menu_item{ |
143 text = "Select All" | 143 text = "Select All" |
144 accelerator = "meta A" | 144 accelerator = "meta A" |
145 action_listener = action_listener(window.text_area.select_all) | 145 action_listener = action_listener(window.text_area.select_all) |
146 } | 146 } |
147 separator | 147 separator |
148 new_menu_item{ | 148 new_check_box_menu_item{ |
149 text = "Find and Replace" | 149 text = "Find and Replace" |
150 accelerator = "meta F" | 150 accelerator = "meta F" |
151 action_listener = action_listener(window.show_find_dialog) | 151 action_listener = function(event) |
152 window.show_find_panel(event.source.state) | |
153 end | |
152 } | 154 } |
153 } | 155 } |
154 } | 156 } |
155 new_menu{ | 157 new_menu{ |
156 text = "View" | 158 text = "View" |
187 } | 189 } |
188 } | 190 } |
189 } | 191 } |
190 } | 192 } |
191 } | 193 } |
194 window.frame.set_menu_bar(menu_bar) | |
192 end | 195 end |
193 | 196 |
194 local function get_matches(text,s) | 197 local function get_matches(text,s) |
195 local n = #s | 198 local n = #s |
196 if n == 0 then | 199 if n == 0 then |
207 i = j + n | 210 i = j + n |
208 end | 211 end |
209 return matches | 212 return matches |
210 end | 213 end |
211 | 214 |
212 local function make_find_dialog(window) | 215 local function make_find_panel(window) |
213 local text_area = window.text_area | 216 local text_area = window.text_area |
214 local find_field, output | 217 local find_field, output |
215 local function find_match(event) | 218 local function find_match(event) |
216 --logger.info("action "..event.action) | 219 --logger.info("action "..event.action) |
217 local s = find_field.text | 220 local s = find_field.text |
254 else | 257 else |
255 error(action) | 258 error(action) |
256 end | 259 end |
257 end | 260 end |
258 find_field = new_text_field{ | 261 find_field = new_text_field{ |
259 constraints = "wrap,growx" | 262 constraints = "growx" |
260 columns = 20 | 263 columns = 20 |
261 action = "next" | 264 action = "next" |
262 action_listener = find_match | 265 action_listener = find_match |
263 } | 266 } |
264 output = new_label{ | 267 output = new_label{ |
265 constraints = "span,grow" | 268 constraints = "span" |
266 text = "testing" | 269 text = "testing" |
267 } | 270 } |
268 local dialog = new_dialog{ | 271 local find_panel = new_panel{ |
269 owner_frame = window.frame | 272 constraints = "growy 0,growx" |
270 content_pane = new_panel{ | 273 layout = new_mig_layout("","[][grow][grow 0]") |
271 layout = new_mig_layout("","[][grow]") | 274 visible = false |
272 children = { | 275 children = { |
273 new_label{ | 276 new_label{ |
274 constraints = "right" | 277 constraints = "right" |
275 text = "Find:" | 278 text = "Find:" |
279 } | |
280 find_field | |
281 new_panel{ | |
282 constraints = "wrap" | |
283 layout = new_mig_layout("insets 0") | |
284 children = { | |
285 new_button{ | |
286 text = "Find Next" | |
287 action = "next" | |
288 action_listener = find_match | |
289 } | |
290 new_button{ | |
291 text = "Find Previous" | |
292 action = "previous" | |
293 action_listener = find_match | |
294 } | |
276 } | 295 } |
277 find_field | 296 } |
278 new_label{ | 297 new_label{ |
279 constraints = "right" | 298 constraints = "right" |
280 text = "Replace:" | 299 text = "Replace:" |
281 } | 300 } |
282 new_text_field{ | 301 new_text_field{ |
283 constraints = "wrap,growx" | 302 constraints = "growx" |
284 columns = 20 | 303 columns = 20 |
285 } | 304 } |
286 new_panel{ | 305 new_button{ |
287 constraints = "span,wrap" | 306 constraints = "wrap" |
288 layout = new_mig_layout("insets 0") | 307 text = "Replace" |
289 children = { | 308 } |
290 new_button{ | 309 output |
291 text = "Find Next" | |
292 action = "next" | |
293 action_listener = find_match | |
294 } | |
295 new_button{ | |
296 text = "Find Previous" | |
297 action = "previous" | |
298 action_listener = find_match | |
299 } | |
300 } | |
301 } | |
302 output | |
303 } | |
304 } | 310 } |
305 } | 311 } |
306 dialog.pack() | 312 function window.show_find_panel(visible) |
307 local was_shown = false | 313 find_panel.visible = visible |
308 function window.show_find_dialog() | 314 if visible then |
309 if not was_shown then | 315 find_field.request_focus_in_window() |
310 was_shown = true | 316 end |
311 dialog.move_into_owner() | 317 end |
312 end | 318 return find_panel |
313 dialog.visible = true | |
314 end | |
315 end | 319 end |
316 | 320 |
317 local n_windows = 0 | 321 local n_windows = 0 |
318 local documents = {} | 322 local documents = {} |
319 | 323 |
320 function new_window(file) | 324 function new_window(file) |
321 local window = {} | 325 local window = {} |
322 window.has_file = file~=nil and file.is_file() | 326 window.has_file = file~=nil and file.is_file() |
323 local text_area = new_text_area{ | 327 local text_area = new_text_area{ |
324 rows = 10 | |
325 columns = 20 | |
326 wrap_style_word = true | 328 wrap_style_word = true |
327 line_wrap = true | 329 line_wrap = true |
328 tab_size = 4 | 330 tab_size = 4 |
329 font = { family="Monospaced", size=13 } | 331 font = { family="Monospaced", size=13 } |
330 border = create_empty_border(0,4,0,4) | |
331 } | 332 } |
332 window.text_area = text_area | 333 window.text_area = text_area |
333 local title = file and file.canonical().to_string() or "new" | 334 local title = file and file.canonical().to_string() or "new" |
334 if file ~= nil then | 335 if file ~= nil then |
335 local document = documents[title] | 336 local document = documents[title] |
342 text_area.text = file.read_text() | 343 text_area.text = file.read_text() |
343 text_area.document.clear_unedited() | 344 text_area.document.clear_unedited() |
344 end | 345 end |
345 end | 346 end |
346 text_area.set_selection(0) | 347 text_area.set_selection(0) |
348 local find_panel = make_find_panel(window) | |
347 local frame = new_frame{ | 349 local frame = new_frame{ |
348 content_pane = new_scroll_pane{ | 350 preferred_size = { width=700, height=700 } |
349 view = text_area | 351 content_pane = new_panel{ |
350 border = no_border | 352 layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]") |
351 row_header_view = new_text_area_line_numbers{ | 353 children = { |
352 text_area = text_area | 354 new_scroll_pane{ |
353 foreground_color = int_to_color(0x888888) | 355 constraints = "grow" |
354 border = create_empty_border(0,8,0,8) | 356 view = text_area |
357 row_header_view = new_text_area_line_numbers{ | |
358 text_area = text_area | |
359 foreground_color = int_to_color(0x888888) | |
360 border = create_empty_border(0,8,0,8) | |
361 } | |
362 } | |
363 find_panel | |
355 } | 364 } |
356 } | 365 } |
357 } | 366 } |
358 window.frame = frame | 367 window.frame = frame |
359 frame.add_close_listener(function() | 368 frame.add_close_listener(function() |
457 end | 466 end |
458 function window.goto(line) | 467 function window.goto(line) |
459 local pos = text_area.get_line_start_position(line) | 468 local pos = text_area.get_line_start_position(line) |
460 text_area.set_selection(pos) | 469 text_area.set_selection(pos) |
461 end | 470 end |
462 make_find_dialog(window) | 471 add_menu_bar(window) |
463 local menu_bar = make_menu_bar(window) | |
464 frame.set_menu_bar(menu_bar) | |
465 frame.pack() | 472 frame.pack() |
466 frame.visible = true | 473 frame.visible = true |
467 text_area.request_focus_in_window() | 474 text_area.request_focus_in_window() |
468 n_windows = n_windows + 1 | 475 n_windows = n_windows + 1 |
469 end | 476 end |
470 | |
471 --[[ | |
472 for _, laf in ipairs(Swing.get_installed_look_and_feels()) do | |
473 logger.info(laf) | |
474 end | |
475 ]] | |
476 --logger.info(Swing.get_look_and_feel()) | |
477 Swing.ui_manager_put("control",int_to_color(0xedeff2)) | |
478 Swing.set_look_and_feel("javax.swing.plaf.nimbus.NimbusLookAndFeel") | |
479 | 477 |
480 Swing.run(function() | 478 Swing.run(function() |
481 local args = Luan.arg | 479 local args = Luan.arg |
482 if #args == 0 then | 480 if #args == 0 then |
483 new_window() | 481 new_window() |