comparison src/luan_editor/window.luan @ 44:779ea6d9d8fb

list window work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 19 May 2025 14:53:41 -0600
parents 48c35fdccb31
children 0c83b27bb32d
comparison
equal deleted inserted replaced
43:48c35fdccb31 44:779ea6d9d8fb
21 local create_empty_border = Border.create_empty_border or error() 21 local create_empty_border = Border.create_empty_border or error()
22 local new_label = require("luan:swing/Label.luan").new or error() 22 local new_label = require("luan:swing/Label.luan").new or error()
23 local make_find_panel = require "classpath:luan_editor/find.luan" 23 local make_find_panel = require "classpath:luan_editor/find.luan"
24 local add_menu_bar = require "classpath:luan_editor/menu.luan" 24 local add_menu_bar = require "classpath:luan_editor/menu.luan"
25 local Swing = require "luan:swing/Swing.luan" 25 local Swing = require "luan:swing/Swing.luan"
26 local run_later = Swing.run_later or error()
26 local File_chooser = require "luan:swing/File_chooser.luan" 27 local File_chooser = require "luan:swing/File_chooser.luan"
27 local choose_file = File_chooser.awt_choose_file or error() 28 local choose_file = File_chooser.awt_choose_file or error()
28 local Option_pane = require "luan:swing/Option_pane.luan" 29 local Option_pane = require "luan:swing/Option_pane.luan"
29 local show_message_dialog = Option_pane.show_message_dialog or error() 30 local show_message_dialog = Option_pane.show_message_dialog or error()
30 local new_radio_button = require("luan:swing/Radio_button.luan").new or error() 31 local new_radio_button = require("luan:swing/Radio_button.luan").new or error()
62 config_file.write_text( stringify(config).."\n" ) 63 config_file.write_text( stringify(config).."\n" )
63 end 64 end
64 65
65 local list_view = new_panel{ 66 local list_view = new_panel{
66 --layout = new_mig_layout("insets 0,wrap,fill") 67 --layout = new_mig_layout("insets 0,wrap,fill")
67 layout = new_mig_layout("wrap,debug","[grow]") 68 layout = new_mig_layout("wrap","[grow]")
68 } 69 }
69 local list_scroll_pane = new_scroll_pane{ 70 local list_scroll_pane = new_scroll_pane{
70 view = list_view 71 view = list_view
71 } 72 }
72 local list_window = new_dialog{ 73 local list_window = new_dialog{
73 preferred_size = { width=200, height=400 } 74 --preferred_size = { width=200, height=400 }
75 size = { width=200, height=400 }
76 --maximum_size = { width=200, height=400 }
74 content_pane = list_scroll_pane 77 content_pane = list_scroll_pane
75 focusable_window_state = false 78 focusable_window_state = false
76 } 79 }
80 local function show_list_window()
81 list_window.visible = true
82 list_scroll_pane.scroll_to_right()
83 end
84
77 local list_group = new_button_group() 85 local list_group = new_button_group()
78 86
79 local function new_list_window_item(text) 87 local function new_list_window_item(text)
80 local rb = new_radio_button{ 88 local rb = new_radio_button{
81 constraints = "grow" 89 constraints = "grow"
84 horizontal_alignment = "right" 92 horizontal_alignment = "right"
85 } 93 }
86 list_view.add(rb) 94 list_view.add(rb)
87 list_group.add(rb) 95 list_group.add(rb)
88 list_scroll_pane.scroll_to_right() 96 list_scroll_pane.scroll_to_right()
89 list_window.pack() 97 list_window.validate()
90 return rb 98 return rb
91 end 99 end
92 local function remove_list_window_item(list_window_item) 100 local function remove_list_window_item(list_window_item)
93 list_view.remove(list_window_item) 101 list_view.remove(list_window_item)
94 list_group.remove(list_window_item) 102 list_group.remove(list_window_item)
95 list_window.pack() 103 list_window.validate()
96 list_window.repaint() 104 end
97 end 105
106 local black = int_to_color(0x000000)
107 local blue = int_to_color(0x0000FF)
108 local grey = int_to_color(0x888888)
98 109
99 local function new_window(file,document) 110 local function new_window(file,document)
100 local window = {} 111 local window = {}
101 if file == nil or not file.exists() then 112 if file == nil or not file.exists() then
102 window.has_file = false 113 window.has_file = false
133 text_area.document.clear_unedited() 144 text_area.document.clear_unedited()
134 end 145 end
135 end 146 end
136 end 147 end
137 text_area.set_selection(0) 148 text_area.set_selection(0)
149 local list_window_item = new_list_window_item(title)
150 list_window_item.add_action_listener(function(_)
151 text_area.request_focus()
152 end)
153 --window.list_window_item = list_window_item
138 local status_bar = new_label{ 154 local status_bar = new_label{
139 constraints = "span,growx" 155 constraints = "span,growx"
140 text = " " 156 text = " "
141 border = create_empty_border(2,16,4,16) 157 border = create_empty_border(2,16,4,16)
142 } 158 }
150 new_scroll_pane{ 166 new_scroll_pane{
151 constraints = "grow" 167 constraints = "grow"
152 view = text_area 168 view = text_area
153 row_header_view = new_text_area_line_numbers{ 169 row_header_view = new_text_area_line_numbers{
154 text_area = text_area 170 text_area = text_area
155 foreground_color = int_to_color(0x888888) 171 foreground_color = grey
156 border = create_empty_border(0,8,0,8) 172 border = create_empty_border(0,8,0,8)
157 } 173 }
158 } 174 }
159 find_panel 175 find_panel
160 status_bar 176 status_bar
165 frame.add_close_listener(function() 181 frame.add_close_listener(function()
166 n_windows = n_windows - 1 182 n_windows = n_windows - 1
167 if n_windows == 0 then 183 if n_windows == 0 then
168 Luan.exit() 184 Luan.exit()
169 end 185 end
170 remove_list_window_item(window.list_window_item) 186 remove_list_window_item(list_window_item)
171 end) 187 end)
172 frame.add_window_focus_listener(function() 188 frame.add_window_focus_listener(function()
173 window.list_window_item.is_selected = true 189 list_window_item.is_selected = true
190 list_window_item.scroll_into_view_vertically()
174 end) 191 end)
175 frame.add_resize_stopped_listener( 200, function() 192 frame.add_resize_stopped_listener( 200, function()
176 --logger.info(stringify(frame.size)) 193 --logger.info(stringify(frame.size))
177 config.size = frame.size 194 config.size = frame.size
178 save_config() 195 save_config()
180 frame.add_move_stopped_listener( 200, function() 197 frame.add_move_stopped_listener( 200, function()
181 --logger.info(stringify(frame.location)) 198 --logger.info(stringify(frame.location))
182 config.location = frame.location 199 config.location = frame.location
183 save_config() 200 save_config()
184 end) 201 end)
185 local function set_title() 202 local function undo_listener()
203 local is_unedited = text_area.document.is_unedited()
204 if window.is_unedited == is_unedited then
205 return
206 end
207 window.is_unedited = is_unedited
186 local s = title 208 local s = title
187 if not text_area.document.is_unedited() then 209 if not is_unedited then
188 s = s.." *" 210 s = s.." *"
189 end 211 end
190 frame.title = s 212 frame.title = s
191 end 213 list_window_item.foreground_color = is_unedited and black or blue
192 set_title() 214 end
193 local list_window_item = new_list_window_item(title) 215 undo_listener()
194 list_window_item.add_action_listener(function(_) 216 --window.undo_listener = undo_listener -- dont gc
195 text_area.request_focus() 217 text_area.document.add_undo_listener(undo_listener)
196 end)
197 window.list_window_item = list_window_item
198 window.set_title = set_title -- dont gc
199 text_area.document.add_undo_listener(set_title)
200 window.new = new_window 218 window.new = new_window
201 function window.open() 219 function window.open()
202 local new_file = choose_file{ 220 local new_file = choose_file{
203 action = "load" 221 action = "load"
204 parent = frame 222 parent = frame
308 end 326 end
309 function window.duplicate() 327 function window.duplicate()
310 local new = new_window(file,text_area.document) 328 local new = new_window(file,text_area.document)
311 new.text_area.set_selection( text_area.get_selection() ) 329 new.text_area.set_selection( text_area.get_selection() )
312 end 330 end
313 function window.show_list_window() 331 window.show_list_window = show_list_window
314 list_window.visible = true
315 list_scroll_pane.scroll_to_right()
316 end
317 add_menu_bar(window) 332 add_menu_bar(window)
318 frame.pack() 333 frame.pack()
319 if config.location ~= nil then 334 if config.location ~= nil then
320 frame.location = config.location 335 frame.location = config.location
321 end 336 end