Mercurial Hosting > editor
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 55:77ee3a37475a | 56:6059b4e22d47 |
|---|---|
| 1 local Luan = require "luan:Luan.luan" | |
| 2 local error = Luan.error | |
| 3 local stringify = Luan.stringify or error() | |
| 4 local ipairs = Luan.ipairs or error() | |
| 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() | |
| 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" | |
| 15 local new_file = Io.schemes.file or error() | |
| 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() | |
| 18 local new_dialog = require("luan:swing/Dialog.luan").new or error() | |
| 19 local new_panel = require("luan:swing/Component.luan").new_panel or error() | |
| 20 local new_list = require("luan:swing/List.luan").new or error() | |
| 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" | |
| 31 local run_later = Swing.run_later or error() | |
| 32 local File_chooser = require "luan:swing/File_chooser.luan" | |
| 33 local choose_file = File_chooser.awt_choose_file or error() | |
| 34 local Option_pane = require "luan:swing/Option_pane.luan" | |
| 35 local show_message_dialog = Option_pane.show_message_dialog or error() | |
| 36 local Clipboard = require "luan:swing/Clipboard.luan" | |
| 37 local Java = require "classpath:luan_editor/Java.luan" | |
| 38 local Logging = require "luan:logging/Logging.luan" | |
| 39 local logger = Logging.logger "editor/Window" | |
| 40 | |
| 41 | |
| 42 local Window = {} | |
| 43 | |
| 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 | |
| 55 local config_file = Io.uri("file:"..Java.home_dir.."/.luan_editor/config.json") | |
| 56 local config = {} | |
| 57 if config_file.exists() then | |
| 58 try | |
| 59 config = json_parse(config_file.read_text()) | |
| 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 | |
| 68 config.list_window = config.list_window or {} | |
| 69 config.list_window.size = config.list_window.size or { width=200, height=400 } | |
| 70 | |
| 71 local function save_config() | |
| 72 config_file.write_text( json_string(config).."\n" ) | |
| 73 end | |
| 74 | |
| 75 local list_view = new_list{ | |
| 76 --layout = new_mig_layout("insets 0,wrap,fill") | |
| 77 --layout = new_mig_layout("wrap","[grow]") | |
| 78 horizontal_alignment = "right" | |
| 79 hover_background = int_to_color(0xEEEEEE) | |
| 80 } | |
| 81 local list_scroll_pane = new_scroll_pane{ | |
| 82 view = list_view | |
| 83 } | |
| 84 local list_window = new_dialog{ | |
| 85 size = config.list_window.size | |
| 86 content_pane = list_scroll_pane | |
| 87 focusable_window_state = false | |
| 88 } | |
| 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) | |
| 99 function Window.show_list_window() | |
| 100 local location = config.list_window.location | |
| 101 if location ~= nil then | |
| 102 list_window.location = location | |
| 103 end | |
| 104 list_window.visible = true | |
| 105 list_scroll_pane.scroll_to_right() | |
| 106 end | |
| 107 | |
| 108 local function add_list_window_item(item) | |
| 109 list_view.add_element(item) | |
| 110 list_scroll_pane.scroll_to_right() | |
| 111 end | |
| 112 local function remove_list_window_item(item) | |
| 113 list_view.remove_element(item) --or error() | |
| 114 end | |
| 115 list_view.add_list_selection_listener( function(item) | |
| 116 if item ~= nil then | |
| 117 item.window.text_area.request_focus() | |
| 118 end | |
| 119 end ) | |
| 120 | |
| 121 local black = int_to_color(0x000000) | |
| 122 local dark_blue = int_to_color(0x0000C0) | |
| 123 local grey = int_to_color(0x888888) | |
| 124 | |
| 125 local function new_window(file,document) | |
| 126 local window = {} | |
| 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() | |
| 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" | |
| 149 if document ~= nil then | |
| 150 text_area.document = document | |
| 151 elseif file ~= nil then | |
| 152 local document = documents[title] | |
| 153 if document ~= nil then | |
| 154 text_area.document = document | |
| 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 | |
| 161 end | |
| 162 end | |
| 163 text_area.set_selection(0) | |
| 164 local list_window_item = { | |
| 165 window = window | |
| 166 } | |
| 167 add_list_window_item(list_window_item) | |
| 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 | |
| 185 foreground_color = grey | |
| 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 | |
| 200 remove_list_window_item(list_window_item) | |
| 201 end) | |
| 202 frame.add_window_focus_listener(function() | |
| 203 list_view.selected_value = list_window_item | |
| 204 end) | |
| 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) | |
| 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 | |
| 221 local s = title | |
| 222 if not is_unedited then | |
| 223 s = s.." *" | |
| 224 end | |
| 225 frame.title = s | |
| 226 list_window_item.text = title | |
| 227 list_window_item.foreground_color = is_unedited and black or dark_blue | |
| 228 list_view.repaint(list_window_item) | |
| 229 end | |
| 230 undo_listener() | |
| 231 --window.undo_listener = undo_listener -- dont gc | |
| 232 text_area.document.add_undo_listener(undo_listener) | |
| 233 window.new = new_window | |
| 234 function window.title() | |
| 235 return title | |
| 236 end | |
| 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 | |
| 258 window.is_unedited = nil | |
| 259 undo_listener() | |
| 260 end | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 361 local add_menu_bar = require "classpath:luan_editor/menu.luan" | |
| 362 add_menu_bar(window) | |
| 363 frame.pack() | |
| 364 local location = config.location | |
| 365 if location ~= nil then | |
| 366 frame.location = location | |
| 367 end | |
| 368 frame.visible = true | |
| 369 text_area.request_focus_in_window() | |
| 370 n_windows = n_windows + 1 | |
| 371 return window | |
| 372 end | |
| 373 Window.new_window = new_window | |
| 374 | |
| 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 |
