Mercurial Hosting > editor
annotate src/luan_editor/window.luan @ 48:ca5ae0a36db7
minor
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Tue, 20 May 2025 20:16:58 -0600 |
| parents | f66f704118e3 |
| children | c17c5312e8de |
| 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 = { |
| 48 | 155 --text = title |
|
47
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 | |
| 48 | 217 list_window_item.text = title |
|
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
218 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
|
219 list_view.repaint(list_window_item) |
| 37 | 220 end |
| 44 | 221 undo_listener() |
| 222 --window.undo_listener = undo_listener -- dont gc | |
| 223 text_area.document.add_undo_listener(undo_listener) | |
| 37 | 224 window.new = new_window |
| 48 | 225 function window.title() |
| 226 return title | |
| 227 end | |
| 37 | 228 function window.open() |
| 229 local new_file = choose_file{ | |
| 230 action = "load" | |
| 231 parent = frame | |
| 232 directory = file and file.parent() | |
| 233 } | |
| 234 if new_file ~= nil then | |
| 235 new_window(new_file) | |
| 236 end | |
| 237 end | |
| 238 function window.save() | |
| 239 if file == nil then | |
| 240 file = choose_file{ | |
| 241 action = "save" | |
| 242 parent = frame | |
| 243 } | |
| 244 if file == nil then | |
| 245 return false | |
| 246 end | |
| 247 title = file.canonical().to_string() | |
| 248 documents[title] = text_area.document | |
| 48 | 249 window.is_unedited = nil |
| 250 undo_listener() | |
| 37 | 251 end |
| 39 | 252 try |
| 253 file.write_text(text_area.text) | |
| 254 catch e | |
| 255 show_message_dialog( frame, e.get_message() ) | |
| 256 return false | |
| 257 end | |
| 37 | 258 text_area.document.set_unedited() |
| 259 return true | |
| 260 end | |
| 261 function window.revert() | |
| 262 local selection = text_area.get_selection() | |
| 263 local text = file.read_text() | |
| 264 text_area.text = text | |
| 265 text_area.set_selection(min(selection,#text+1)) | |
| 266 text_area.document.set_unedited() | |
| 267 status_bar.text = "Reverted" | |
| 268 end | |
| 269 local function selection_lines() | |
| 270 local start_seletion, end_selection = text_area.get_selection() | |
| 271 local end_ = end_selection == start_seletion and end_selection or end_selection - 1 | |
| 272 local start_line = text_area.get_line_from_position(start_seletion) | |
| 273 local end_line = text_area.get_line_from_position(end_) | |
| 274 local start_pos = text_area.get_line_start_position(start_line) | |
| 275 local end_pos = text_area.get_line_end_position(end_line) | |
| 276 local text = text_area.text | |
| 277 text = sub_string(text,start_pos,end_pos-2) | |
| 278 return { | |
| 279 text = text | |
| 280 start_pos = start_pos | |
| 281 length = #text | |
| 282 lines = end_line - start_line + 1 | |
| 283 start_seletion = start_seletion | |
| 284 end_selection = end_selection | |
| 285 } | |
| 286 end | |
| 287 function window.indent() | |
| 288 local r = selection_lines() | |
| 289 local text = r.text | |
| 290 local start_pos = r.start_pos | |
| 291 text = "\t"..replace(text,"\n","\n\t") | |
| 292 text_area.replace(start_pos,r.length,text) | |
| 293 --logger.info(stringify{text_area.get_selection()}) | |
| 294 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines ) | |
| 295 end | |
| 296 function window.unindent() | |
| 297 local r = selection_lines() | |
| 298 local text = r.text | |
| 299 text = "\n"..text | |
| 300 local start_seletion = r.start_seletion | |
| 301 if starts_with(text,"\n\t") then | |
| 302 start_seletion = start_seletion - 1 | |
| 303 end | |
| 304 local len1 = #text | |
| 305 text = replace(text,"\n\t","\n") | |
| 306 local len2 = #text | |
| 307 local end_selection = r.end_selection - (len1 - len2) | |
| 308 text = sub_string(text,2) | |
| 309 text_area.replace(r.start_pos,r.length,text) | |
| 310 text_area.set_selection(start_seletion,end_selection) | |
| 311 end | |
| 312 function window.cursor_column() | |
| 313 local cursor_pos = text_area.get_selection() | |
| 314 local line = text_area.get_line_from_position(cursor_pos) | |
| 315 local start_line_pos = text_area.get_line_start_position(line) | |
| 316 return cursor_pos - start_line_pos + 1 | |
| 317 end | |
| 318 function window.goto(line) | |
| 319 local pos = text_area.get_line_start_position(line) | |
| 320 text_area.set_selection(pos) | |
| 321 end | |
| 322 function window.set_line_wrap(line_wrap) | |
| 323 text_area.line_wrap = line_wrap | |
| 324 config.line_wrap = line_wrap | |
| 325 save_config() | |
| 326 end | |
| 327 function window.set_whitespace_visible(whitespace_visible) | |
| 328 text_area.whitespace_visible = whitespace_visible | |
| 329 config.whitespace_visible = whitespace_visible | |
| 330 save_config() | |
| 331 end | |
| 332 function window.set_tab_size(tab_size) | |
| 333 text_area.tab_size = tab_size | |
| 334 config.tab_size = tab_size | |
| 335 save_config() | |
| 336 end | |
| 38 | 337 function window.duplicate() |
| 338 local new = new_window(file,text_area.document) | |
| 339 new.text_area.set_selection( text_area.get_selection() ) | |
| 340 end | |
| 44 | 341 window.show_list_window = show_list_window |
| 37 | 342 add_menu_bar(window) |
| 343 frame.pack() | |
| 46 | 344 local location = config.location |
| 345 if location ~= nil then | |
| 346 frame.location = location | |
| 37 | 347 end |
| 348 frame.visible = true | |
| 349 text_area.request_focus_in_window() | |
| 350 n_windows = n_windows + 1 | |
| 38 | 351 return window |
| 37 | 352 end |
| 353 | |
| 354 return new_window |
