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