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