Mercurial Hosting > editor
comparison src/luan_editor/Window.luan @ 92:aefa7f8bb407
better open files
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 09 Jul 2025 22:59:59 -0600 |
parents | ccc7fc9636ab |
children |
comparison
equal
deleted
inserted
replaced
91:ff999e959b74 | 92:aefa7f8bb407 |
---|---|
1 local Luan = require "luan:Luan.luan" | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | 2 local error = Luan.error |
3 local stringify = Luan.stringify or error() | 3 local stringify = Luan.stringify or error() |
4 local ipairs = Luan.ipairs or error() | 4 local ipairs = Luan.ipairs or error() |
5 local pairs = Luan.pairs or error() | |
5 local Parsers = require "luan:Parsers.luan" | 6 local Parsers = require "luan:Parsers.luan" |
6 local json_string = Parsers.json_string or error() | 7 local json_string = Parsers.json_string or error() |
7 local json_parse = Parsers.json_parse or error() | 8 local json_parse = Parsers.json_parse or error() |
8 local Math = require "luan:Math.luan" | 9 local Math = require "luan:Math.luan" |
9 local min = Math.min or error() | 10 local min = Math.min or error() |
10 local String = require "luan:String.luan" | 11 local String = require "luan:String.luan" |
11 local sub_string = String.sub or error() | 12 local sub_string = String.sub or error() |
12 local replace = String.replace or error() | 13 local replace = String.replace or error() |
13 local starts_with = String.starts_with or error() | 14 local starts_with = String.starts_with or error() |
15 local Table = require "luan:Table.luan" | |
16 local is_empty = Table.is_empty or error() | |
14 local Io = require "luan:Io.luan" | 17 local Io = require "luan:Io.luan" |
15 local Thread = require "luan:Thread.luan" | 18 local Thread = require "luan:Thread.luan" |
16 local sleep = Thread.sleep or error() | 19 local sleep = Thread.sleep or error() |
17 local thread_safe_function = Thread.thread_safe_function or error() | 20 local thread_safe_function = Thread.thread_safe_function or error() |
18 local new_file = Io.schemes.file or error() | 21 local new_file = Io.schemes.file or error() |
50 fonts[font] = font | 53 fonts[font] = font |
51 end | 54 end |
52 local monospaced = fonts["Lucida Console"] or fonts["Monospaced"] or error() | 55 local monospaced = fonts["Lucida Console"] or fonts["Monospaced"] or error() |
53 -- logger.info(monospaced) | 56 -- logger.info(monospaced) |
54 | 57 |
55 local n_windows = 0 | 58 local windows_set = {} |
56 local documents = {} | 59 local documents = {} |
57 | 60 |
58 local function bool(val,default) | 61 local function bool(val,default) |
59 if val ~= nil then | 62 if val ~= nil then |
60 return val | 63 return val |
133 local dark_blue = int_to_color(0x0000C0) | 136 local dark_blue = int_to_color(0x0000C0) |
134 local grey = int_to_color(0x888888) | 137 local grey = int_to_color(0x888888) |
135 | 138 |
136 local new_window | 139 local new_window |
137 | 140 |
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 | |
138 local function open_files(files) | 152 local function open_files(files) |
139 local fn = thread_safe_function( new_window ) | 153 local fn = thread_safe_function( open_file ) |
140 Thread.run( function() | 154 Thread.run( function() |
141 for _, file in ipairs(files) do | 155 for _, file in ipairs(files) do |
142 swing_run( function() | 156 swing_run( function() |
143 fn(file) | 157 fn(file) |
144 end ) | 158 end ) |
145 sleep(100) | 159 sleep(100) |
146 end | 160 end |
147 end, true ) | 161 end, true ) |
148 end | 162 end |
163 Window.open_files = open_files | |
149 | 164 |
150 function new_window(file,document) | 165 function new_window(file,document) |
151 local window = {} | 166 local window = {} |
152 if file == nil or not file.exists() then | 167 if file == nil or not file.exists() then |
153 window.has_file = false | 168 window.has_file = false |
154 elseif file.is_file() then | 169 elseif file.is_file() then |
155 window.has_file = true | 170 window.has_file = true |
156 else | 171 else |
157 show_message_dialog(nil,"Not a file") | 172 show_message_dialog(nil,"Not a file") |
158 if n_windows == 0 then | 173 if is_empty(windows_set) then |
159 Luan.exit() | 174 Luan.exit() |
160 else | 175 else |
161 return | 176 return |
162 end | 177 end |
163 end | 178 end |
173 files = open_files | 188 files = open_files |
174 } | 189 } |
175 } | 190 } |
176 window.text_area = text_area | 191 window.text_area = text_area |
177 local title = file and file.canonical().to_string() or "new" | 192 local title = file and file.canonical().to_string() or "new" |
193 window.title = title | |
178 if document ~= nil then | 194 if document ~= nil then |
179 text_area.document = document | 195 text_area.document = document |
180 elseif file ~= nil then | 196 elseif file ~= nil then |
181 local document_info = documents[title] | 197 local document_info = documents[title] |
182 if document_info ~= nil then | 198 if document_info ~= nil then |
222 } | 238 } |
223 } | 239 } |
224 } | 240 } |
225 window.frame = frame | 241 window.frame = frame |
226 frame.add_close_listener(function() | 242 frame.add_close_listener(function() |
227 n_windows = n_windows - 1 | 243 windows_set[window] = nil |
228 if n_windows == 0 then | 244 if is_empty(windows_set) then |
229 Luan.exit() | 245 Luan.exit() |
230 end | 246 end |
231 remove_list_window_item(list_window_item) | 247 remove_list_window_item(list_window_item) |
232 local document_info = documents[title] | 248 local document_info = documents[title] |
233 if document_info ~= nil then | 249 if document_info ~= nil then |
269 list_view.repaint(list_window_item) | 285 list_view.repaint(list_window_item) |
270 end | 286 end |
271 undo_listener() | 287 undo_listener() |
272 --window.undo_listener = undo_listener -- dont gc | 288 --window.undo_listener = undo_listener -- dont gc |
273 text_area.document.add_undo_listener(undo_listener) | 289 text_area.document.add_undo_listener(undo_listener) |
274 function window.title() | |
275 return title | |
276 end | |
277 function window.open() | 290 function window.open() |
278 local new_files = choose_file{ | 291 local new_files = choose_file{ |
279 action = "load" | 292 action = "load" |
280 parent = frame | 293 parent = frame |
281 directory = file and file.parent() | 294 directory = file and file.parent() |
394 return false | 407 return false |
395 end | 408 end |
396 open_files(files) | 409 open_files(files) |
397 return true | 410 return true |
398 end | 411 end |
412 function window.to_front() | |
413 frame.to_front() | |
414 text_area.request_focus_in_window() | |
415 end | |
399 local add_menu_bar = require "classpath:luan_editor/menu.luan" | 416 local add_menu_bar = require "classpath:luan_editor/menu.luan" |
400 add_menu_bar(window) | 417 add_menu_bar(window) |
401 frame.pack() | 418 frame.pack() |
402 local location = config.location | 419 local location = config.location |
403 if location ~= nil then | 420 if location ~= nil then |
404 frame.location = location | 421 frame.location = location |
405 end | 422 end |
406 frame.is_visible = true | 423 frame.is_visible = true |
407 text_area.request_focus_in_window() | 424 text_area.request_focus_in_window() |
408 n_windows = n_windows + 1 | 425 windows_set[window] = true |
409 return window | 426 return window |
410 end | 427 end |
411 Window.new_window = new_window | 428 Window.new_window = new_window |
412 | 429 |
413 function Window.open_window(file_path) | 430 function Window.to_front() |
414 if file_path == nil then | 431 list_view.selected_value.window.to_front() |
415 local window = list_view.selected_value.window | |
416 window.frame.to_front() | |
417 window.text_area.request_focus_in_window() | |
418 else | |
419 local file = new_file(file_path) | |
420 new_window(file) | |
421 end | |
422 end | 432 end |
423 | 433 |
424 return Window | 434 return Window |