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