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