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